Skip to content
Myr

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

9 files+163 added−7 removed

modified:docs/content/3.manual/manual.yml+22 added-1 removed
207 unmodified lines
208 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.211212 Another way to set the exit status is with the `halt_error`213 builtin function.214215 * `--arg name value`:216217 This option passes a value to the jq program as a predefined218 variable. If you run jq with `--arg foo bar`, then `$foo` is
765 unmodified lines
984 body: |985986 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.989990 - title: "`halt`"991 body: |992993 Stops the jq program with no further outputs. jq will exit994 with exit status `0`.995996 - title: "`halt_error`, `halt_error(exit_code)`"997 body: |998999 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.10021003 The given `exit_code` (defaulting to `5`) will be jq's exit1004 status.10051006 For example, `"Error: somthing went wrong\n"|halt_error(1)`.10071008 - title: "`$__loc__`"1009 body: |10101011 Produces an object with a "file" key and a "line" key, with
2172 unmodified lines
modified:src/builtin.c+15 added
1029 unmodified lines
1030 }1031 return env;1032}10331034static 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}10391040static 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}10461047static jv f_get_search_list(jq_state *jq, jv input) {1048 jv_free(input);1049 return jq_get_lib_dirs(jq);1050}
428 unmodified lines
1479 {(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 lines
modified:src/builtin.jq+1 added
1def halt_error: halt_error(5);2def error: error(.);3def map(f): [.[] | f];4def select(f): if f then . else empty end;5def sort_by(f): _sort_by_impl(map([f]));
302 unmodified lines
modified:src/execute.c+41 added
38 unmodified lines
39 int debug_trace_enabled;40 int initial_execution;41 unsigned next_label;4243 int halted;44 jv exit_code;45 jv error_message;4647 jv attrs;48 jq_input_cb input_cb;49 void *input_cb_data;50 jq_msg_cb debug_cb;
237 unmodified lines
288 stack_reset(&jq->stk);289 jv_free(jq->error);290 jq->error = jv_null();291292 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 lines
326 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;337338 if (jq->debug_trace_enabled) {
604 unmodified lines
943 jq->fork_top = 0;944 jq->curr_frame = 0;945 jq->error = jv_null();946947 jq->halted = 0;948 jq->exit_code = jv_invalid();949 jq->error_message = jv_invalid();950951 jq->err_cb = default_err_cb;952 jq->err_cb_data = stderr;953954 jq->attrs = jv_object();
223 unmodified lines
1178void 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}11821183void1184jq_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}11911192int1193jq_halted(jq_state *jq)1194{1195 return jq->halted;1196}11971198jv jq_get_exit_code(jq_state *jq)1199{1200 return jv_copy(jq->exit_code);1201}12021203jv jq_get_error_message(jq_state *jq)1204{1205 return jv_copy(jq->error_message);1206}
modified:src/jq.h+5 added
20 unmodified lines
21void jq_start(jq_state *, jv value, int);22jv jq_next(jq_state *);23void jq_teardown(jq_state **);2425void jq_halt(jq_state *, jv, jv);26int jq_halted(jq_state *);27jv jq_get_exit_code(jq_state *);28jv jq_get_error_message(jq_state *);2930typedef jv (*jq_input_cb)(jq_state *, void *);31void jq_set_input_cb(jq_state *, jq_input_cb, void *);32void jq_get_input_cb(jq_state *, jq_input_cb *, void **);33
32 unmodified lines
modified:src/main.c+28 added-5 removed
135 unmodified lines
136 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};146static int options = 0;147148static const char *skip_shebang(const char *p) {
33 unmodified lines
182 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 lines
645 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 lines
1415if [ -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=-q20else21 VALGRIND=22 VG_EXIT0=23 Q=24fi2526mods=$JQTESTDIR/modules
16 unmodified lines
modified:tests/shtest+33 added
209 unmodified lines
210 echo "Issue #817 regression?" 1>&2211 exit 1212fi213214## Halt215216if ! $VALGRIND $Q $JQ -n halt; then217 echo "jq halt didn't work as expected" 1>&2218 exit 1219fi220if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(1)'; then221 echo "jq halt_error(1) didn't work as expected" 1>&2222 exit 1223elif [ $? -ne 1 ]; then224 echo "jq halt_error(1) had wrong error code" 1>&2225 exit 1226fi227if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(11)'; then228 echo "jq halt_error(11) didn't work as expected" 1>&2229 exit 1230elif [ $? -ne 11 ]; then231 echo "jq halt_error(11) had wrong error code" 1>&2232 exit 1233fi234if [ -n "`$VALGRIND $Q $JQ -n 'halt_error(1)' 2>&1`" ]; then235 echo "jq halt_error(1) had unexpected output" 1>&2236 exit 1237fi238if [ -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 1241fi242if [ "`$VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>&1`" != xyz ]; then243 echo "jq halt_error(1) had unexpected output" 1>&2244 exit 1245fi246247exit 0
modified:jq.1.prebuilt+16 added-1 removed
151 unmodified lines
152.153.IP154Sets 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.IP157Another 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.IP
856 unmodified lines
1019.1020.IP "" 01021.1022.SS "error(message)"1020Produces 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\.1023Produces 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"1026Stops the jq program with no further outputs\. jq will exit with exit status \fB0\fR\.1027.1028.SS "halt_error, halt_error(exit_code)"1029Stops 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.P1032The given \fBexit_code\fR (defaulting to \fB5\fR) will be jq\'s exit status\.1033.1034.P1035For example, \fB"Error: somthing went wrong\en"|halt_error(1)\fR\.1036.1037.SS "$__loc__"1038Produces 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 "" 4
2331 unmodified lines