From 07f94f82a243f1c178463c4266cd1a5a1348a5b4 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Tue, 28 Apr 2015 10:39:34 -0500 Subject: [PATCH] usage() should check fprintf() result (fix #771) --- main.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/main.c b/main.c index f40f2e0d..9791b226 100644 --- a/main.c +++ b/main.c @@ -23,28 +23,34 @@ static const char* progname; */ static void usage(int code) { FILE *f = stderr; + if (code == 0) f = stdout; - fprintf(f, "\njq - commandline JSON processor [version %s]\n", JQ_VERSION); - fprintf(f, "Usage: %s [options] [file...]\n\n", progname); - fprintf(f, "\tjq is a tool for processing JSON inputs, applying the\n"); - fprintf(f, "\tgiven filter to its JSON text inputs and producing the\n"); - fprintf(f, "\tfilter's results as JSON on standard output.\n"); - fprintf(f, "\tThe simplest filter is ., which is the identity filter,\n"); - fprintf(f, "\tcopying jq's input to its output.\n"); - fprintf(f, "\tFor more advanced filters see the jq(1) manpage (\"man jq\")\n"); - fprintf(f, "\tand/or http://stedolan.github.com/jq\n\n"); - fprintf(f, "\tSome of the options include:\n"); - fprintf(f, "\t -h\t\tthis message;\n"); - fprintf(f, "\t -c\t\tcompact instead of pretty-printed output;\n"); - fprintf(f, "\t -n\t\tuse `null` as the single input value;\n"); - fprintf(f, "\t -s\t\tread (slurp) all inputs into an array; apply filter to it;\n"); - fprintf(f, "\t -r\t\toutput raw strings, not JSON texts;\n"); - fprintf(f, "\t -R\t\tread raw strings, not JSON texts;\n"); - fprintf(f, "\t --arg a v\tset variable $a to value ;\n"); - fprintf(f, "\t --argjson a v\tset variable $a to JSON value ;\n"); - fprintf(f, "\t --argfile a f\tset variable $a to JSON texts read from ;\n"); - fprintf(f, "\tSee the manpage for more options.\n"); + +#ifdef BAIL +#undef BAIL +#endif +#define BAIL(expr) do { if ((expr) < 0 && code == 0) code = 2; } while (0) + BAIL(fprintf(f, "\njq - commandline JSON processor [version %s]\n", JQ_VERSION)); + BAIL(fprintf(f, "Usage: %s [options] [file...]\n\n", progname)); + BAIL(fprintf(f, "\tjq is a tool for processing JSON inputs, applying the\n")); + BAIL(fprintf(f, "\tgiven filter to its JSON text inputs and producing the\n")); + BAIL(fprintf(f, "\tfilter's results as JSON on standard output.\n")); + BAIL(fprintf(f, "\tThe simplest filter is ., which is the identity filter,\n")); + BAIL(fprintf(f, "\tcopying jq's input to its output.\n")); + BAIL(fprintf(f, "\tFor more advanced filters see the jq(1) manpage (\"man jq\")\n")); + BAIL(fprintf(f, "\tand/or http://stedolan.github.com/jq\n\n")); + BAIL(fprintf(f, "\tSome of the options include:\n")); + BAIL(fprintf(f, "\t -h\t\tthis message;\n")); + BAIL(fprintf(f, "\t -c\t\tcompact instead of pretty-printed output;\n")); + BAIL(fprintf(f, "\t -n\t\tuse `null` as the single input value;\n")); + BAIL(fprintf(f, "\t -s\t\tread (slurp) all inputs into an array; apply filter to it;\n")); + BAIL(fprintf(f, "\t -r\t\toutput raw strings, not JSON texts;\n")); + BAIL(fprintf(f, "\t -R\t\tread raw strings, not JSON texts;\n")); + BAIL(fprintf(f, "\t --arg a v\tset variable $a to value ;\n")); + BAIL(fprintf(f, "\t --argjson a v\tset variable $a to JSON value ;\n")); + BAIL(fprintf(f, "\t --argfile a f\tset variable $a to JSON texts read from ;\n")); + BAIL(fprintf(f, "\tSee the manpage for more options.\n")); exit(code); }