diff --git a/pkg/interp/bufferrange.go b/pkg/interp/bufferrange.go index 8e6d5ff69..fad0e463c 100644 --- a/pkg/interp/bufferrange.go +++ b/pkg/interp/bufferrange.go @@ -88,25 +88,27 @@ func (of *openFile) ToBufferView() (BufferRange, error) { // TODO: when to close? when bb loses all refs? need to use finalizer somehow? func (i *Interp) _open(c interface{}, a []interface{}) interface{} { var err error - + var f fs.File var path string - path, err = toString(c) - if err != nil { - return fmt.Errorf("%s: %w", path, err) - } - var bEnd int64 - var f fs.File - if path == "" || path == "-" { + switch c.(type) { + case nil: + path = "" f = i.os.Stdin() - } else { + default: + path, err = toString(c) + if err != nil { + return fmt.Errorf("%s: %w", path, err) + } f, err = i.os.FS().Open(path) if err != nil { return err } } + var bEnd int64 var fRS io.ReadSeeker + fFI, err := f.Stat() if err != nil { f.Close() diff --git a/pkg/interp/funcs.jq b/pkg/interp/funcs.jq index e746feb38..2b1e8d012 100644 --- a/pkg/interp/funcs.jq +++ b/pkg/interp/funcs.jq @@ -1,11 +1,3 @@ -def print: stdout; -def println: ., "\n" | stdout; -def debug: - ( ((["DEBUG", .] | tojson), "\n" | stderr) - , . - ); -def debug(f): . as $c | f | debug | $c; - def display($opts): _display(options($opts)); def display: display({}); def d($opts): display($opts); diff --git a/pkg/interp/internal.jq b/pkg/interp/internal.jq index 628693750..3ca755165 100644 --- a/pkg/interp/internal.jq +++ b/pkg/interp/internal.jq @@ -1,3 +1,12 @@ +# is here to be defined as early as possible to allow debugging +def print: stdout; +def println: ., "\n" | stdout; +def debug: + ( ((["DEBUG", .] | tojson), "\n" | stderr) + , . + ); +def debug(f): . as $c | f | debug | $c; + def _global_var($k): _global_state[$k]; def _global_var($k; f): _global_state(_global_state | .[$k] |= f) | .[$k]; diff --git a/pkg/interp/interp.jq b/pkg/interp/interp.jq index 6990cf165..b47bbd355 100644 --- a/pkg/interp/interp.jq +++ b/pkg/interp/interp.jq @@ -36,8 +36,9 @@ def input: | _input_filename(null) as $_ | $h | try + # null input here means stdin ( open - | _input_filename($h) as $_ + | _input_filename($h // "") as $_ | . ) catch @@ -233,10 +234,12 @@ def _main: ), expr_eval_path: $combined_opts.expr_file, filenames: ( - ( if $combined_opts.expr_file then $rest + ( if $combined_opts.filenames then $combined_opts.filenames + elif $combined_opts.expr_file then $rest else $rest[1:] end - | if . == [] then null end + # null means stdin + | if . == [] then [null] end ) ), join_string: ( @@ -291,10 +294,12 @@ def _main: elif $opts.show_formats then _formats_list | println elif - ( ($rest | length) == 0 and + ( $opts.filenames == [null] and + $opts.null_input == false and ($opts.repl | not) and ($opts.expr_file | not) and - $stdin.is_terminal and $stdout.is_terminal + $stdin.is_terminal and + $stdout.is_terminal ) then ( (( _usage($arg0), "\n") | stderr) , null | halt_error(_exit_code_args_error) diff --git a/pkg/interp/options.jq b/pkg/interp/options.jq index 052bc8a65..48cc7ee1f 100644 --- a/pkg/interp/options.jq +++ b/pkg/interp/options.jq @@ -34,9 +34,9 @@ def _opt_build_default_fixed: decode_progress: (env.NO_DECODE_PROGRESS == null), depth: 0, expr: ".", - expr_file: null, expr_eval_path: "arg", - filenames: ["-"], + expr_file: null, + filenames: null, include_path: null, join_string: "\n", null_input: false, @@ -108,14 +108,14 @@ def _opt_cli_arg_options: color: (.color | _opt_toboolean), colors: (.colors | _opt_tostring), compact: (.compact | _opt_toboolean), - decode_file: (.decode_file | _opt_toarray(type == "string")), + decode_file: (.decode_file | _opt_toarray(_opt_is_string_pair)), decode_format: (.decode_format | _opt_tostring), decode_progress: (.decode_progress | _opt_toboolean), depth: (.depth | _opt_tonumber), display_bytes: (.display_bytes | _opt_tonumber), expr: (.expr | _opt_tostring), expr_file: (.expr_file | _opt_tostring), - filename: (.filenames | _opt_toarray(type == "string")), + filenames: (.filenames | _opt_toarray(type == "string")), include_path: (.include_path | _opt_tostring), join_string: (.join_string | _opt_tostring), line_bytes: (.line_bytes | _opt_tonumber), diff --git a/pkg/interp/testdata/args.fqtest b/pkg/interp/testdata/args.fqtest index d04459d7e..b073c02c4 100644 --- a/pkg/interp/testdata/args.fqtest +++ b/pkg/interp/testdata/args.fqtest @@ -35,9 +35,7 @@ null> ^D $ fq -i . /test.mp3 mp3> ^D $ fq -n -exitcode: 2 -stderr: -Usage: fq [OPTIONS] [--] [EXPR] [FILE...] +null $ fq -ni null> ^D $ fq -n 123 diff --git a/pkg/interp/testdata/basic.fqtest b/pkg/interp/testdata/basic.fqtest index 081a77e9b..f214c3a59 100644 --- a/pkg/interp/testdata/basic.fqtest +++ b/pkg/interp/testdata/basic.fqtest @@ -25,7 +25,7 @@ mp3> .headers[0].magic._format null mp3> ^D $ fq -d raw . - |00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.: {} - (raw) + |00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.: {} (raw) 0x0|61 62 63 0a| |abc.| | unknown0: raw bits stdin: abc diff --git a/pkg/interp/testdata/inputs.fqtest b/pkg/interp/testdata/inputs.fqtest index 4d5028039..e5e03e3da 100644 --- a/pkg/interp/testdata/inputs.fqtest +++ b/pkg/interp/testdata/inputs.fqtest @@ -41,6 +41,10 @@ $ fq -d raw -n '(input,input,input,input) | todescription' /a /b /c exitcode: 5 stderr: error: break +$ fq -d raw input_filename +"" +stdin: +test $ fq -d raw input_filename /a /b /c "/a" "/b" diff --git a/pkg/interp/testdata/options.fqtest b/pkg/interp/testdata/options.fqtest index fba9b8257..4d688c7cc 100644 --- a/pkg/interp/testdata/options.fqtest +++ b/pkg/interp/testdata/options.fqtest @@ -18,7 +18,7 @@ $ fq -n options "expr_eval_path": "arg", "expr_file": null, "filenames": [ - "-" + null ], "include_path": null, "join_string": "\n", @@ -58,6 +58,8 @@ $ fq -o compact=true -n options.compact true $ fq -o compact=aaa -n options.compact false +$ fq -n -o 'decode_file=[["a", "/test.mp3"]]' '$a | format' +"mp3" $ fq -o decode_progress=false -n options.decode_progress false $ fq -o decode_progress=aaa -n options.decode_progress @@ -70,10 +72,14 @@ $ fq -o display_bytes=10 -n options.display_bytes 10 $ fq -o display_bytes=true -n options.display_bytes 16 +$ fq -n -o expr=123 +123 $ fq -o expr_file=test.jq -n options.expr_file exitcode: 2 stderr: error: open testdata/test.jq: no such file or directory +$ fq -o 'filenames=["/test.mp3"]' format +"mp3" $ fq -o include_path=path -n options.include_path "path" $ fq -o 'join_string=aaa\n' -n options.join_string