Add `halt`, `halt_error` builtins (fix #386)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicolas Williams · committed by Nicolas Williams · parent 6bac4ed · Browse files · 9 files·+163 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+163 added−7 removed
- tests:+35 added−0 removed
- other:+128 added−7 removed
Provenance
- Commit
8ea21a5- Tree
1a4636d- Author
Nicolas Williams <nico@cryptonector.com>- Committer
Nicolas Williams <nico@cryptonector.com>- Parent
6bac4ed
9 files changed+163 added−7 removed
modified:docs/content/3.manual/manual.yml+22 added-1 removed
207 unmodified lines208 produced. Normally jq exits with 2 if there was any usage209 problem or system error, 3 if there was a jq program compile210 error, or 0 if the jq program ran.211 212+ Another way to set the exit status is with the `halt_error`213+ builtin function.214+215 * `--arg name value`:216 217 This option passes a value to the jq program as a predefined218 variable. If you run jq with `--arg foo bar`, then `$foo` is765 unmodified lines984 body: |985 986 Produces an error, just like `.a` applied to values other than987 null and objects would, but with the given message as the985- error's value.988+ error's value. Errors can be caught with try/catch; see below.989 990+ - title: "`halt`"991+ body: |992+993+ Stops the jq program with no further outputs. jq will exit994+ with exit status `0`.995+996+ - title: "`halt_error`, `halt_error(exit_code)`"997+ body: |998+999+ Stops the jq program with no further outputs. The input will1000+ be printed on `stderr` as raw output (i.e., strings will not1001+ have double quotes) with no decoration, not even a newline.1002+1003+ The given `exit_code` (defaulting to `5`) will be jq's exit1004+ status.1005+1006+ For example, `"Error: somthing went wrong\n"|halt_error(1)`.1007+1008 - title: "`$__loc__`"1009 body: |1010 1011 Produces an object with a "file" key and a "line" key, with2172 unmodified linesmodified:src/builtin.c+15 added
1029 unmodified lines1030 }1031 return env;1032 }1033 1034+static jv f_halt(jq_state *jq, jv input) {1035+ jv_free(input);1036+ jq_halt(jq, jv_invalid(), jv_invalid());1037+ return jv_true();1038+}1039+1040+static jv f_halt_error(jq_state *jq, jv input, jv a) {1041+ if (jv_get_kind(a) != JV_KIND_NUMBER)1042+ return type_error(input, "halt_error/1: number required"); \1043+ jq_halt(jq, a, input);1044+ return jv_true();1045+}1046+1047 static jv f_get_search_list(jq_state *jq, jv input) {1048 jv_free(input);1049 return jq_get_lib_dirs(jq);1050 }428 unmodified lines1479 {(cfunction_ptr)f_max_by_impl, "_max_by_impl", 2},1480 {(cfunction_ptr)f_error, "error", 2},1481 {(cfunction_ptr)f_format, "format", 2},1482 {(cfunction_ptr)f_env, "env", 1},1483+ {(cfunction_ptr)f_halt, "halt", 1},1484+ {(cfunction_ptr)f_halt_error, "halt_error", 2},1485 {(cfunction_ptr)f_get_search_list, "get_search_list", 1},1486 {(cfunction_ptr)f_get_prog_origin, "get_prog_origin", 1},1487 {(cfunction_ptr)f_get_jq_origin, "get_jq_origin", 1},1488 {(cfunction_ptr)f_match, "_match_impl", 4},139 unmodified linesmodified:src/builtin.jq+1 added
1+def halt_error: halt_error(5);2 def error: error(.);3 def map(f): [.[] | f];4 def select(f): if f then . else empty end;5 def sort_by(f): _sort_by_impl(map([f]));302 unmodified linesmodified:src/execute.c+41 added
38 unmodified lines39 int debug_trace_enabled;40 int initial_execution;41 unsigned next_label;42 43+ int halted;44+ jv exit_code;45+ jv error_message;46+47 jv attrs;48 jq_input_cb input_cb;49 void *input_cb_data;50 jq_msg_cb debug_cb;237 unmodified lines288 stack_reset(&jq->stk);289 jv_free(jq->error);290 jq->error = jv_null();291 292+ jq->halted = 0;293+ jv_free(jq->exit_code);294+ jv_free(jq->error_message);295 if (jv_get_kind(jq->path) != JV_KIND_INVALID)296 jv_free(jq->path);297 jq->path = jv_null();298 jv_free(jq->value_at_path);27 unmodified lines326 int backtracking = !jq->initial_execution;327 jq->initial_execution = 0;328 assert(jv_get_kind(jq->error) == JV_KIND_NULL);329 while (1) {330+ if (jq->halted) {331+ if (jq->debug_trace_enabled)332+ printf("\t<halted>\n");333+ return jv_invalid();334+ }335 uint16_t opcode = *pc;336 raising = 0;337 338 if (jq->debug_trace_enabled) {604 unmodified lines943 jq->fork_top = 0;944 jq->curr_frame = 0;945 jq->error = jv_null();946 947+ jq->halted = 0;948+ jq->exit_code = jv_invalid();949+ jq->error_message = jv_invalid();950+951 jq->err_cb = default_err_cb;952 jq->err_cb_data = stderr;953 954 jq->attrs = jv_object();223 unmodified lines1178 void jq_get_debug_cb(jq_state *jq, jq_msg_cb *cb, void **data) {1179 *cb = jq->debug_cb;1180 *data = jq->debug_cb_data;1181 }1182+1183+void1184+jq_halt(jq_state *jq, jv exit_code, jv error_message)1185+{1186+ assert(!jq->halted);1187+ jq->halted = 1;1188+ jq->exit_code = exit_code;1189+ jq->error_message = error_message;1190+}1191+1192+int1193+jq_halted(jq_state *jq)1194+{1195+ return jq->halted;1196+}1197+1198+jv jq_get_exit_code(jq_state *jq)1199+{1200+ return jv_copy(jq->exit_code);1201+}1202+1203+jv jq_get_error_message(jq_state *jq)1204+{1205+ return jv_copy(jq->error_message);1206+}modified:src/jq.h+5 added
20 unmodified lines21 void jq_start(jq_state *, jv value, int);22 jv jq_next(jq_state *);23 void jq_teardown(jq_state **);24 25+void jq_halt(jq_state *, jv, jv);26+int jq_halted(jq_state *);27+jv jq_get_exit_code(jq_state *);28+jv jq_get_error_message(jq_state *);29+30 typedef jv (*jq_input_cb)(jq_state *, void *);31 void jq_set_input_cb(jq_state *, jq_input_cb, void *);32 void jq_get_input_cb(jq_state *, jq_input_cb *, void **);33 32 unmodified linesmodified:src/main.c+28 added-5 removed
135 unmodified lines136 FROM_FILE = 512,137 RAW_NO_LF = 1024,138 UNBUFFERED_OUTPUT = 2048,139 EXIT_STATUS = 4096,140- SEQ = 8192,141- RUN_TESTS = 16384,140+ EXIT_STATUS_EXACT = 8192,141+ SEQ = 16384,142+ RUN_TESTS = 32768,143 /* debugging only */143- DUMP_DISASM = 32768,144+ DUMP_DISASM = 65536,145 };146 static int options = 0;147 148 static const char *skip_shebang(const char *p) {33 unmodified lines182 priv_fwrite("\n", 1, stdout, dumpopts & JV_PRINT_ISATTY);183 if (options & UNBUFFERED_OUTPUT)184 fflush(stdout);185 }186+ if (jq_halted(jq)) {187+ // jq program invoked `halt` or `halt_error`188+ options |= EXIT_STATUS_EXACT;189+ jv exit_code = jq_get_exit_code(jq);190+ if (!jv_is_valid(exit_code))191+ ret = 0;192+ else if (jv_get_kind(exit_code) == JV_KIND_NUMBER)193+ ret = jv_number_value(exit_code);194+ else195+ ret = 5;196+ jv_free(exit_code);197+ jv error_message = jq_get_error_message(jq);198+ if (jv_get_kind(error_message) == JV_KIND_STRING) {199+ fprintf(stderr, "%s", jv_string_value(error_message));200+ } else if (jv_get_kind(error_message) == JV_KIND_NULL) {201+ // Halt with no output202+ } else if (jv_is_valid(error_message)) {203+ error_message = jv_dump_string(jv_copy(error_message), 0);204+ fprintf(stderr, "%s\n", jv_string_value(error_message));205+ } // else no message on stderr; use --debug-trace to see a message206+ fflush(stderr);207+ jv_free(error_message);185- if (jv_invalid_has_msg(jv_copy(result))) {208+ } else if (jv_invalid_has_msg(jv_copy(result))) {209 // Uncaught jq exception210 jv msg = jv_invalid_get_msg(jv_copy(result));211 jv input_pos = jq_util_input_get_position(jq);212 if (jv_get_kind(msg) == JV_KIND_STRING) {432 unmodified lines645 jq_util_input_free(&input_state);646 jq_teardown(&jq);647 if (ret >= 10 && (options & EXIT_STATUS))648 return ret - 10;626- if (ret >= 10)649+ if (ret >= 10 && !(options & EXIT_STATUS_EXACT))650 return 0;651 return ret;652 }modified:tests/setup+2 added
13 unmodified lines14 15 if [ -z "${NO_VALGRIND-}" ] && which valgrind > /dev/null; then16 VALGRIND="valgrind --error-exitcode=1 --leak-check=full \17 --suppressions=$JQTESTDIR/onig.supp"18+ VG_EXIT0=--error-exitcode=019 Q=-q20 else21 VALGRIND=22+ VG_EXIT0=23 Q=24 fi25 26 mods=$JQTESTDIR/modules16 unmodified linesmodified:tests/shtest+33 added
209 unmodified lines210 echo "Issue #817 regression?" 1>&2211 exit 1212 fi213 214+## Halt215+216+if ! $VALGRIND $Q $JQ -n halt; then217+ echo "jq halt didn't work as expected" 1>&2218+ exit 1219+fi220+if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(1)'; then221+ echo "jq halt_error(1) didn't work as expected" 1>&2222+ exit 1223+elif [ $? -ne 1 ]; then224+ echo "jq halt_error(1) had wrong error code" 1>&2225+ exit 1226+fi227+if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(11)'; then228+ echo "jq halt_error(11) didn't work as expected" 1>&2229+ exit 1230+elif [ $? -ne 11 ]; then231+ echo "jq halt_error(11) had wrong error code" 1>&2232+ exit 1233+fi234+if [ -n "`$VALGRIND $Q $JQ -n 'halt_error(1)' 2>&1`" ]; then235+ echo "jq halt_error(1) had unexpected output" 1>&2236+ exit 1237+fi238+if [ -n "`$VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>/dev/null`" ]; then239+ echo "jq halt_error(1) had unexpected output on stdout" 1>&2240+ exit 1241+fi242+if [ "`$VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>&1`" != xyz ]; then243+ echo "jq halt_error(1) had unexpected output" 1>&2244+ exit 1245+fi246+247 exit 0modified:jq.1.prebuilt+16 added-1 removed
151 unmodified lines152 .153 .IP154 Sets the exit status of jq to 0 if the last output values was neither \fBfalse\fR nor \fBnull\fR, 1 if the last output value was either \fBfalse\fR or \fBnull\fR, or 4 if no valid result was ever produced\. Normally jq exits with 2 if there was any usage problem or system error, 3 if there was a jq program compile error, or 0 if the jq program ran\.155 .156+.IP157+Another way to set the exit status is with the \fBhalt_error\fR builtin function\.158+.159 .IP "\(bu" 4160 \fB\-\-arg name value\fR:161 .162 .IP856 unmodified lines1019 .1020 .IP "" 01021 .1022 .SS "error(message)"1020-Produces an error, just like \fB\.a\fR applied to values other than null and objects would, but with the given message as the error\'s value\.1023+Produces an error, just like \fB\.a\fR applied to values other than null and objects would, but with the given message as the error\'s value\. Errors can be caught with try/catch; see below\.1024 .1025+.SS "halt"1026+Stops the jq program with no further outputs\. jq will exit with exit status \fB0\fR\.1027+.1028+.SS "halt_error, halt_error(exit_code)"1029+Stops the jq program with no further outputs\. The input will be printed on \fBstderr\fR as raw output (i\.e\., strings will not have double quotes) with no decoration, not even a newline\.1030+.1031+.P1032+The given \fBexit_code\fR (defaulting to \fB5\fR) will be jq\'s exit status\.1033+.1034+.P1035+For example, \fB"Error: somthing went wrong\en"|halt_error(1)\fR\.1036+.1037 .SS "$__loc__"1038 Produces an object with a "file" key and a "line" key, with the filename and line number where \fB$__loc__\fR occurs, as values\.1039 .1040 .IP "" 42331 unmodified lines