Skip to content

Commit

Permalink
Require a main program (fix jqlang#2785)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicowilliams committed Jul 29, 2023
1 parent 70bbd10 commit baa9ab3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
14 changes: 13 additions & 1 deletion docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ sections:
output(s) of the filter are written to standard output, as a
sequence of newline-separated JSON data.
The simplest and most common filter (or jq program) is `.`,
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 `.` 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.
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
characters on Unix shells) the jq program, as too many characters with special
meaning to jq are also shell meta-characters. For example, `jq
"foo"` will fail on most Unix shells because that will be the same
as `jq foo`, which will generally fail because `foo is not
Expand All @@ -101,6 +110,9 @@ sections:
* Powershell: `jq '.[\"foo\"]'`
* Windows command shell: `jq ".[\"foo\"]"`
Note: jq allows user-defined functions, but every jq program
must have a top-level expression.
You can affect how jq reads and writes its input and output
using some command-line options:
Expand Down
8 changes: 7 additions & 1 deletion jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/linker.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ int load_program(jq_state *jq, struct locfile* src, block *out_block) {
if (nerrors)
return nerrors;

if (!block_has_main(program)) {
jq_report_error(jq, jv_string_fmt("jq: error: Top-level program given (try \".\")\n"));
block_free(program);
return 1;
}

char* home = getenv("HOME");
if (home) { // silently ignore no $HOME
/* Import ~/.jq as a library named "" found in $HOME */
Expand Down
1 change: 1 addition & 0 deletions tests/no-main-program.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def a: .;
30 changes: 30 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,34 @@ if command -v script >/dev/null 2>&1; then
cmp $d/color $d/expect
fi

# #2785
if $VALGRIND $Q $JQ -n -f "$JQTESTDIR/no-main-program.jq" > $d/out 2>&1; then
echo "jq -f $JQTESTDIR/no-main-program.jq succeeded"
exit 1
else
EC=$?
if [ $EC -eq 1 ]; then
echo "jq -f $JQTESTDIR/no-main-program.jq faile with memory errors"
exit 1
fi
if [ $EC -ne 3 ]; then
echo "jq -f $JQTESTDIR/no-main-program.jq faile with wrong exit code ($EC)"
exit 1
fi
fi
cat > $d/expected <<EOF
jq: error: Top-level program given (try ".")
jq: 1 compile error
EOF
if ! diff $d/expected $d/out; then
echo "jq -f $JQTESTDIR/no-main-program.jq failed but its error message is not the expected one"
exit 1
fi

if ! $VALGRIND $Q $JQ -n -f "$JQTESTDIR/yes-main-program.jq" > $d/out 2>&1; then
echo "jq -f $JQTESTDIR/yes-main-program.jq failed"
exit 1
fi

exit 0
2 changes: 2 additions & 0 deletions tests/yes-main-program.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def a: .;
0

0 comments on commit baa9ab3

Please sign in to comment.