Skip to content

Commit

Permalink
Remove -i option (jqlang#704)
Browse files Browse the repository at this point in the history
In-place editing should be implemented with builtins for file I/O.
  • Loading branch information
nicowilliams committed Mar 6, 2015
1 parent 3d2ab93 commit b82c231
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 62 deletions.
4 changes: 0 additions & 4 deletions docs/content/3.manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ sections:
Like `-r` but jq won't print a newline after each output.
* `--in-place` / `-i`:
Edit the (first) file in-place.
* `-f filename` / `--from-file filename`:
Read filter from the file rather than from a command line, like
Expand Down
65 changes: 12 additions & 53 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ static void usage(int code) {
fprintf(f, "\t -h\t\tthis message;\n");
fprintf(f, "\t -c\t\tcompact instead of pretty-printed output;\n");
fprintf(f, "\t -n\t\tuse `null` as the single input value;\n");
fprintf(f, "\t -i\t\tedit the [first] file in-place;\n");
fprintf(f, "\t -s\t\tread (slurp) all inputs into an array; apply filter to it;\n");
fprintf(f, "\t -r\t\toutput raw strings, not JSON texts;\n");
fprintf(f, "\t -R\t\tread raw strings, not JSON texts;\n");
Expand Down Expand Up @@ -94,11 +93,10 @@ enum {
RAW_NO_LF = 1024,
UNBUFFERED_OUTPUT = 2048,
EXIT_STATUS = 4096,
IN_PLACE = 8192,
SEQ = 16384,
RUN_TESTS = 32768,
SEQ = 8192,
RUN_TESTS = 16384,
/* debugging only */
DUMP_DISASM = 65536,
DUMP_DISASM = 32768,
};
static int options = 0;

Expand Down Expand Up @@ -151,7 +149,7 @@ int main(int argc, char* argv[]) {
int ret = 0;
int compiled = 0;
int parser_flags = 0;
char *t = NULL;
int nfiles = 0;

if (argc) progname = argv[0];

Expand All @@ -171,18 +169,17 @@ int main(int argc, char* argv[]) {
size_t short_opts = 0;
jv program_arguments = jv_array();
jv lib_search_paths = jv_null();
char *first_file = 0;
for (int i=1; i<argc; i++, short_opts = 0) {
if (further_args_are_files) {
jq_util_input_add_input(input_state, jv_string(argv[i]));
first_file = first_file ? first_file : argv[i];
nfiles++;
} else if (!strcmp(argv[i], "--")) {
if (!program) usage(2);
further_args_are_files = 1;
} else if (!isoptish(argv[i])) {
if (program) {
jq_util_input_add_input(input_state, jv_string(argv[i]));
first_file = first_file ? first_file : argv[i];
nfiles++;
} else {
program = argv[i];
}
Expand Down Expand Up @@ -250,10 +247,6 @@ int main(int argc, char* argv[]) {
options |= RAW_OUTPUT | RAW_NO_LF;
if (!short_opts) continue;
}
if (isoption(argv[i], 'i', "in-place", &short_opts)) {
options |= IN_PLACE;
if (!short_opts) continue;
}
if (isoption(argv[i], 0, "seq", &short_opts)) {
options |= SEQ;
if (!short_opts) continue;
Expand Down Expand Up @@ -355,13 +348,12 @@ int main(int argc, char* argv[]) {
}
}

int dumpopts;
int dumpopts = 0;
#ifndef WIN32
/* Disable colour by default on Windows builds as Windows
terminals tend not to display it correctly */
#ifdef WIN32
dumpopts = 0;
#else
dumpopts = isatty(fileno(stdout)) ? JV_PRINT_COLOUR : 0;
if (isatty(fileno(stdout)))
dumpopts |= JV_PRINT_COLOUR;
#endif
if (options & SORTED_OUTPUT) dumpopts |= JV_PRINT_SORTED;
if (!(options & COMPACT_OUTPUT)) dumpopts |= JV_PRINT_PRETTY;
Expand Down Expand Up @@ -402,22 +394,6 @@ int main(int argc, char* argv[]) {
#endif

if (!program) usage(2);
if ((options & IN_PLACE)) {
if (first_file == 0) usage(2);
if (strcmp(first_file, "-") == 0) usage(2);
size_t tlen = strlen(first_file) + 7;
t = jv_mem_alloc(tlen);
int n = snprintf(t, tlen,"%sXXXXXX", first_file);
assert(n > 0 && (size_t)n < tlen);
if (mkstemp(t) == -1) {
fprintf(stderr, "Error: %s creating temporary file", strerror(errno));
exit(3);
}
if (freopen(t, "w", stdout) == NULL) {
fprintf(stderr, "Error: %s redirecting stdout to temporary file", strerror(errno));
exit(3);
}
}

if ((options & PROVIDE_NULL) && (options & (RAW_INPUT | SLURP))) {
fprintf(stderr, "%s: --null-input cannot be used with --raw-input or --slurp\n", progname);
Expand Down Expand Up @@ -469,8 +445,9 @@ int main(int argc, char* argv[]) {
// Let jq program call `debug` builtin and have that go somewhere
jq_set_debug_cb(jq, debug_cb, &dumpopts);

if (first_file == 0)
if (nfiles == 0)
jq_util_input_add_input(input_state, jv_string("-"));

if (options & PROVIDE_NULL) {
ret = process(jq, jv_null(), jq_flags, dumpopts);
} else {
Expand Down Expand Up @@ -502,24 +479,6 @@ int main(int argc, char* argv[]) {
if (ret != 0)
goto out;

if ((options & IN_PLACE)) {
FILE *devnull;
#ifdef WIN32
devnull = freopen("NUL", "w+", stdout);
#else
devnull = freopen("/dev/null", "w+", stdout);
#endif
if (devnull == NULL) {
fprintf(stderr, "Error: %s opening /dev/null\n", strerror(errno));
exit(3);
}
assert(first_file != 0 && strcmp(first_file, "-") != 0);
if (rename(t, first_file) == -1) {
fprintf(stderr, "Error: %s renaming temporary file\n", strerror(errno));
exit(3);
}
jv_mem_free(t);
}
out:
jq_util_input_free(&input_state);
jq_teardown(&jq);
Expand Down
5 changes: 0 additions & 5 deletions tests/run
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ EOF
printf '1\n'|./jq -ces --seq '. == [1]' >/dev/null 2> $d/out
cmp $d/out $d/expected

$VALGRIND $Q ./jq -n '0, 1, 2' > $d/a
$VALGRIND $Q ./jq -n '3, 4, 5' > $d/b
$VALGRIND $Q ./jq -i '.+1' $d/a $d/b
$VALGRIND $Q ./jq -se '. == [1,2,3,4,5,6]' $d/a

## Test streaming parser

## If we add an option to stream to the `import ... as $symbol;` directive
Expand Down

0 comments on commit b82c231

Please sign in to comment.