Require a main program (fix #2785)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicolas Williams · committed by Nico Williams · parent 044b385 · Browse files · 7 files·+61 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+61 added−3 removed
- tests:+33 added−0 removed
- other:+28 added−3 removed
Provenance
- Commit
27a4d57- Tree
6ab7361- Author
Nicolas Williams <nico@cryptonector.com>- Committer
Nico Williams <nico@cryptonector.com>- Parent
044b385
7 files changed+61 added−3 removed
modified:docs/content/manual/manual.yml+13 added-1 removed
79 unmodified lines80 are passed through the provided filter one at a time. The81 output(s) of the filter are written to standard output, as a82 sequence of newline-separated JSON data.83 84+ The simplest and most common filter (or jq program) is `.`,85+ which is the identity operator, copying the inputs of the jq86+ processor to the output stream. Because the default behavior of87+ the jq processor is to read JSON texts from the input stream,88+ and to pretty-print outputs, the `.` program's main use is to89+ validate and pretty-print the inputs. The jq programming90+ language is quite rich and allows for much more than just91+ validation and pretty-printing.92+93 Note: it is important to mind the shell's quoting rules. As a94 general rule it's best to always quote (with single-quote86- characters) the jq program, as too many characters with special95+ characters on Unix shells) the jq program, as too many characters with special96 meaning to jq are also shell meta-characters. For example, `jq97 "foo"` will fail on most Unix shells because that will be the same98 as `jq foo`, which will generally fail because `foo is not99 defined`. When using the Windows command shell (cmd.exe) it's8 unmodified lines108 * Unix shells: `jq '.["foo"]'`109 * Powershell: `jq '.[\"foo\"]'`110 * Windows command shell: `jq ".[\"foo\"]"`111 112+ Note: jq allows user-defined functions, but every jq program113+ must have a top-level expression.114+115 You can affect how jq reads and writes its input and output116 using some command-line options:117 118 * `--null-input` / `-n`:3533 unmodified linesmodified:src/linker.c+6 added
405 unmodified lines406 nerrors = jq_parse(src, &program);407 if (nerrors)408 return nerrors;409 410+ if (!block_has_main(program)) {411+ jq_report_error(jq, jv_string("jq: error: Top-level program not given (try \".\")"));412+ block_free(program);413+ return 1;414+ }415+416 char* home = getenv("HOME");417 if (home) { // silently ignore no $HOME418 /* Import ~/.jq as a library named "" found in $HOME */419 block import = gen_import_meta(gen_import("", NULL, 0),24 unmodified linesadded:tests/no-main-program.jq+1 added
1+def a: .;modified:tests/shtest+30 added
514 unmodified lines515 printf '\033[1;30mnull\033[0m\r\n' > $d/expect516 cmp $d/color $d/expect517 fi518 519+# #2785520+if $VALGRIND $Q $JQ -n -f "$JQTESTDIR/no-main-program.jq" > $d/out 2>&1; then521+ echo "jq -f $JQTESTDIR/no-main-program.jq succeeded"522+ exit 1523+else524+ EC=$?525+ if [ $EC -eq 1 ]; then526+ echo "jq -f $JQTESTDIR/no-main-program.jq failed with memory errors"527+ exit 1528+ fi529+ if [ $EC -ne 3 ]; then530+ echo "jq -f $JQTESTDIR/no-main-program.jq failed with wrong exit code ($EC)"531+ exit 1532+ fi533+fi534+cat > $d/expected <<EOF535+jq: error: Top-level program not given (try ".")536+537+jq: 1 compile error538+EOF539+if ! diff $d/expected $d/out; then540+ echo "jq -f $JQTESTDIR/no-main-program.jq failed but its error message is not the expected one"541+ exit 1542+fi543+544+if ! $VALGRIND $Q $JQ -n -f "$JQTESTDIR/yes-main-program.jq" > $d/out 2>&1; then545+ echo "jq -f $JQTESTDIR/yes-main-program.jq failed"546+ exit 1547+fi548+549 exit 0added:tests/yes-main-program.jq+2 added
1+def a: .;2+0modified:jq.1.prebuilt+7 added-1 removed
33 unmodified lines34 .SH "INVOKING JQ"35 jq filters run on a stream of JSON data\. The input to jq is parsed as a sequence of whitespace\-separated JSON values which are passed through the provided filter one at a time\. The output(s) of the filter are written to standard output, as a sequence of newline\-separated JSON data\.36 .37 .P38-Note: it is important to mind the shell\'s quoting rules\. As a general rule it\'s best to always quote (with single\-quote characters) the jq program, as too many characters with special meaning to jq are also shell meta\-characters\. For example, \fBjq "foo"\fR will fail on most Unix shells because that will be the same as \fBjq foo\fR, which will generally fail because \fBfoo is not defined\fR\. When using the Windows command shell (cmd\.exe) it\'s best to use double quotes around your jq program when given on the command\-line (instead of the \fB\-f program\-file\fR option), but then double\-quotes in the jq program need backslash escaping\. When using the Powershell (\fBpowershell\.exe\fR) or the Powershell Core (\fBpwsh\fR/\fBpwsh\.exe\fR), use single\-quote characters around the jq program and backslash\-escaped double\-quotes (\fB\e"\fR) inside the jq program\.38+The simplest and most common filter (or jq program) is \fB\.\fR, which is the identity operator, copying the inputs of the jq processor to the output stream\. Because the default behavior of the jq processor is to read JSON texts from the input stream, and to pretty\-print outputs, the \fB\.\fR program\'s main use is to validate and pretty\-print the inputs\. The jq programming language is quite rich and allows for much more than just validation and pretty\-printing\.39 .40+.P41+Note: it is important to mind the shell\'s quoting rules\. As a general rule it\'s best to always quote (with single\-quote characters on Unix shells) the jq program, as too many characters with special meaning to jq are also shell meta\-characters\. For example, \fBjq "foo"\fR will fail on most Unix shells because that will be the same as \fBjq foo\fR, which will generally fail because \fBfoo is not defined\fR\. When using the Windows command shell (cmd\.exe) it\'s best to use double quotes around your jq program when given on the command\-line (instead of the \fB\-f program\-file\fR option), but then double\-quotes in the jq program need backslash escaping\. When using the Powershell (\fBpowershell\.exe\fR) or the Powershell Core (\fBpwsh\fR/\fBpwsh\.exe\fR), use single\-quote characters around the jq program and backslash\-escaped double\-quotes (\fB\e"\fR) inside the jq program\.42+.43 .IP "\(bu" 444 Unix shells: \fBjq \'\.["foo"]\'\fR45 .46 .IP "\(bu" 44 unmodified lines51 .52 .IP "" 053 .54 .P55+Note: jq allows user\-defined functions, but every jq program must have a top\-level expression\.56+.57+.P58 You can affect how jq reads and writes its input and output using some command\-line options:59 .60 .TP61 \fB\-\-null\-input\fR / \fB\-n\fR:4035 unmodified linesmodified:Makefile.am+2 added-1 removed
220 unmodified lines221 tests/onig.supp tests/local.supp \222 tests/setup tests/torture/input0.json \223 tests/optional.test tests/man.test tests/manonig.test \224 tests/jq.test tests/onig.test tests/base64.test \225- tests/utf8-truncate.jq tests/jq-f-test.sh225+ tests/utf8-truncate.jq tests/jq-f-test.sh \226+ tests/no-main-program.jq tests/yes-main-program.jq227 228 AM_DISTCHECK_CONFIGURE_FLAGS=--with-oniguruma=builtin229 230 # README.md is expected in GitHub projects, good stuff in it, so we'll14 unmodified lines