From 24564b2d345398a818eb74a194faf3733655d73a Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Mon, 14 Oct 2019 12:55:30 +0800 Subject: Add -0 / --nul-output option for processing values containing newlines Closes: https://github.com/stedolan/jq/issues/1271 --- docs/content/manual/manual.yml | 5 +++++ src/main.c | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/docs/content/manual/manual.yml b/docs/content/manual/manual.yml index 578517be..f50f0e62 100644 --- a/docs/content/manual/manual.yml +++ b/docs/content/manual/manual.yml @@ -199,6 +199,11 @@ sections: Like `-r` but jq won't print a newline after each output. + * `--nul-output` / `-0`: + + Like `-r` but jq will print NUL instead of newline after each output. + This can be useful when the values being output can contain newlines. + * `-f filename` / `--from-file filename`: Read filter from the file rather than from a command line, like diff --git a/src/main.c b/src/main.c index 42c2147b..7022f19e 100644 --- a/src/main.c +++ b/src/main.c @@ -130,6 +130,7 @@ enum { RAW_INPUT = 2, PROVIDE_NULL = 4, RAW_OUTPUT = 8, + RAW_NUL = 16, ASCII_OUTPUT = 32, COLOR_OUTPUT = 64, NO_COLOR_OUTPUT = 128, @@ -196,6 +197,8 @@ static int process(jq_state *jq, jv value, int flags, int dumpopts) { } if (!(options & RAW_NO_LF)) priv_fwrite("\n", 1, stdout, dumpopts & JV_PRINT_ISATTY); + if (options & RAW_NUL) + priv_fwrite("\0", 1, stdout, dumpopts & JV_PRINT_ISATTY); if (options & UNBUFFERED_OUTPUT) fflush(stdout); } @@ -394,6 +397,10 @@ int main(int argc, char* argv[]) { options |= RAW_OUTPUT | RAW_NO_LF; if (!short_opts) continue; } + if (isoption(argv[i], '0', "nul-output", &short_opts)) { + options |= RAW_OUTPUT | RAW_NO_LF | RAW_NUL; + if (!short_opts) continue; + } if (isoption(argv[i], 'b', "binary", &short_opts)) { #ifdef WIN32 fflush(stdout); -- cgit v1.2.3