Skip to content
Myr

Make it easier to use jq with shebangs (fix #1044)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Allow a continuation on a comment immediately after a shebang to make this traditional hack possible: #!/bin/sh # this next line is ignored by jq \ exec jq -f "$0" "$@" # jq code follows But continue only on the first line following a shebang, and only if it's a comment.

authored by Nicolas Williams · committed by David Tolnay · parent 856a4b2 · Browse files · 3 files+22 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+22 added−1 removed

  • tests:+6 added−0 removed
  • other:+16 added−1 removed

3 files+22 added−1 removed

modified:src/main.c+16 added-1 removed
127 unmodified lines
128 DUMP_DISASM = 32768,129};130static int options = 0;131132static const char *skip_shebang(const char *p) {133 if (strncmp(p, "#!", sizeof("#!") - 1) != 0)134 return p;135 const char *n = strchr(p, '\n');136 if (n == NULL || n[1] != '#')137 return p;138 n = strchr(n + 1, '\n');139 if (n == NULL || n[1] == '#' || n[1] == '\0' || n[-1] != '\\' || n[-2] == '\\')140 return p;141 n = strchr(n + 1, '\n');142 if (n == NULL)143 return p;144 return n+1;145}146147static int process(jq_state *jq, jv value, int flags, int dumpopts) {148 int ret = 14; // No valid results && -e -> exit(4)149 jq_start(jq, value, flags);150 jv result;
356 unmodified lines
507 ret = 2;508 goto out;509 }510 jq_set_attr(jq, jv_string("PROGRAM_ORIGIN"), jq_realpath(jv_string(dirname(program_origin))));496 compiled = jq_compile_args(jq, jv_string_value(data), jv_copy(program_arguments));511 compiled = jq_compile_args(jq, skip_shebang(jv_string_value(data)), jv_copy(program_arguments));512 free(program_origin);513 jv_free(data);514 } else {515 jq_set_attr(jq, jv_string("PROGRAM_ORIGIN"), jq_realpath(jv_string("."))); // XXX is this good?
73 unmodified lines
added:tests/jq-f-test.sh+4 added
modified:tests/shtest+2 added
1#!/bin/sh23. "${0%/*}/setup"45PATH=$JQBASEDIR:$PATH $JQBASEDIR/tests/jq-f-test.sh > /dev/null67if [ -f "$JQBASEDIR/.libs/libinject_errors.so" ]; then8 # Do some simple error injection tests to check that we're handling9 # I/O errors correctly.10 (
208 unmodified lines