Skip to content

Commit

Permalink
Add -0 / --nul-output option for processing values containing newlines
Browse files Browse the repository at this point in the history
Closes: #1271
  • Loading branch information
pabs3 committed Oct 31, 2019
1 parent bda75c3 commit ae88f83
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ae88f83

Please sign in to comment.