]> granicus.if.org Git - jq/commitdiff
Identify progname in more errors (fix #1860)
authorNicolas Williams <nico@cryptonector.com>
Mon, 25 Mar 2019 04:32:42 +0000 (23:32 -0500)
committerNicolas Williams <nico@cryptonector.com>
Mon, 25 Mar 2019 04:36:18 +0000 (23:36 -0500)
However, we should really use the argv[0] progname, not just jq.
Someday we may want to support aliases which automatically add certain
options, etc.

src/jv_alloc.c
src/main.c
src/util.c

index fd7b25792a134eabeac30652d8518f39d652fe12..5fa67684827b086421d80e01e2795c751f5db947 100644 (file)
@@ -37,7 +37,7 @@ static void memory_exhausted() {
   if (nomem_handler.handler)
     nomem_handler.handler(nomem_handler.data); // Maybe handler() will longjmp() to safety
   // Or not
-  fprintf(stderr, "error: cannot allocate memory\n");
+  fprintf(stderr, "jq: error: cannot allocate memory\n");
   abort();
 }
 #else /* USE_TLS */
@@ -59,16 +59,16 @@ static void tsd_fini(void) {
 
 static void tsd_init(void) {
   if (pthread_key_create(&nomem_handler_key, NULL) != 0) {
-    fprintf(stderr, "error: cannot create thread specific key");
+    fprintf(stderr, "jq: error: cannot create thread specific key");
     abort();
   }
   if (atexit(tsd_fini) != 0) {
-    fprintf(stderr, "error: cannot set an exit handler");
+    fprintf(stderr, "jq: error: cannot set an exit handler");
     abort();
   }
   struct nomem_handler *nomem_handler = calloc(1, sizeof(struct nomem_handler));
   if (pthread_setspecific(nomem_handler_key, nomem_handler) != 0) {
-    fprintf(stderr, "error: cannot set thread specific data");
+    fprintf(stderr, "jq: error: cannot set thread specific data");
     abort();
   }
 }
@@ -80,7 +80,7 @@ void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {
   nomem_handler = pthread_getspecific(nomem_handler_key);
   if (nomem_handler == NULL) {
     handler(data);
-    fprintf(stderr, "error: cannot allocate memory\n");
+    fprintf(stderr, "jq: error: cannot allocate memory\n");
     abort();
   }
   nomem_handler->handler = handler;
@@ -95,7 +95,7 @@ static void memory_exhausted() {
   if (nomem_handler)
     nomem_handler->handler(nomem_handler->data); // Maybe handler() will longjmp() to safety
   // Or not
-  fprintf(stderr, "error: cannot allocate memory\n");
+  fprintf(stderr, "jq: error: cannot allocate memory\n");
   abort();
 }
 
@@ -110,7 +110,7 @@ void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {
 }
 
 static void memory_exhausted() {
-  fprintf(stderr, "error: cannot allocate memory\n");
+  fprintf(stderr, "jq: error: cannot allocate memory\n");
   abort();
 }
 
index 9d539eb21916f18713f3fab957893f592b7984a6..42c2147b3d3c3399fbf63e66f81da6004cf18d37 100644 (file)
@@ -212,12 +212,12 @@ static int process(jq_state *jq, jv value, int flags, int dumpopts) {
     jv_free(exit_code);
     jv error_message = jq_get_error_message(jq);
     if (jv_get_kind(error_message) == JV_KIND_STRING) {
-      fprintf(stderr, "%s", jv_string_value(error_message));
+      fprintf(stderr, "jq: error: %s", jv_string_value(error_message));
     } else if (jv_get_kind(error_message) == JV_KIND_NULL) {
       // Halt with no output
     } else if (jv_is_valid(error_message)) {
       error_message = jv_dump_string(jv_copy(error_message), 0);
-      fprintf(stderr, "%s\n", jv_string_value(error_message));
+      fprintf(stderr, "jq: error: %s\n", jv_string_value(error_message));
     } // else no message on stderr; use --debug-trace to see a message
     fflush(stderr);
     jv_free(error_message);
@@ -586,7 +586,7 @@ int main(int argc, char* argv[]) {
 
   char *origin = strdup(argv[0]);
   if (origin == NULL) {
-    fprintf(stderr, "Error: out of memory\n");
+    fprintf(stderr, "jq: error: out of memory\n");
     exit(1);
   }
   jq_set_attr(jq, jv_string("JQ_ORIGIN"), jv_string(dirname(origin)));
@@ -678,11 +678,11 @@ int main(int argc, char* argv[]) {
       if (!(options & SEQ)) {
         // --seq -> errors are not fatal
         ret = JQ_OK_NO_OUTPUT;
-        fprintf(stderr, "parse error: %s\n", jv_string_value(msg));
+        fprintf(stderr, "jq: parse error: %s\n", jv_string_value(msg));
         jv_free(msg);
         break;
       }
-      fprintf(stderr, "ignoring parse error: %s\n", jv_string_value(msg));
+      fprintf(stderr, "jq: ignoring parse error: %s\n", jv_string_value(msg));
       jv_free(msg);
     }
   }
@@ -693,7 +693,7 @@ int main(int argc, char* argv[]) {
 out:
   badwrite = ferror(stdout);
   if (fclose(stdout)!=0 || badwrite) {
-    fprintf(stderr,"Error: writing output failed: %s\n", strerror(errno));
+    fprintf(stderr,"jq: error: writing output failed: %s\n", strerror(errno));
     ret = JQ_ERROR_SYSTEM;
   }
 
index 99d6020aa0da29eda59b4531f3154c43e63a7922..c5bf5f221f2ffc7c811e055c75daa4a15c52855c 100644 (file)
@@ -275,7 +275,7 @@ static int jq_util_input_read_more(jq_util_input_state *state) {
       // System-level input error on the stream. It will be closed (below).
       // TODO: report it. Can't use 'state->err_cb()' as it is hard-coded for
       //       'open' related problems.
-      fprintf(stderr,"Input error: %s\n", strerror(errno));
+      fprintf(stderr,"jq: error: %s\n", strerror(errno));
     }
     if (state->current_input) {
       if (state->current_input == stdin) {