]> granicus.if.org Git - curl/commitdiff
tool: Moved --stderr to the global config
authorSteve Holme <steve_holme@hotmail.com>
Sat, 1 Mar 2014 13:00:31 +0000 (13:00 +0000)
committerSteve Holme <steve_holme@hotmail.com>
Sat, 1 Mar 2014 13:03:20 +0000 (13:03 +0000)
12 files changed:
src/tool_cb_dbg.c
src/tool_cb_prg.c
src/tool_cfgable.c
src/tool_cfgable.h
src/tool_formparse.c
src/tool_getparam.c
src/tool_main.c
src/tool_metalink.c
src/tool_metalink.h
src/tool_msgs.c
src/tool_operate.c
src/tool_paramhlp.c

index e921165322f1ee523e14bf3ef205cda0c632db02..8a75fb6931d5e4172870997aed7931e7b8aa3fb5 100644 (file)
@@ -46,7 +46,7 @@ int tool_debug_cb(CURL *handle, curl_infotype type,
 {
   struct OperationConfig *operation = userdata;
   struct GlobalConfig *config = operation->global;
-  FILE *output = operation->errors;
+  FILE *output = config->errors;
   const char *text;
   struct timeval tv;
   struct tm *now;
@@ -77,7 +77,7 @@ int tool_debug_cb(CURL *handle, curl_infotype type,
       config->trace_stream = stdout;
     else if(curlx_strequal("%", config->trace_dump))
       /* Ok, this is somewhat hackish but we do it undocumented for now */
-      config->trace_stream = operation->errors;  /* aka stderr */
+      config->trace_stream = config->errors;  /* aka stderr */
     else {
       config->trace_stream = fopen(config->trace_dump, "w");
       config->trace_fopened = TRUE;
index 2b0611ca9aa0e2cbbc33ef0e752fc17a55db4731..9e3c5fb4e40c04e3ab4172ae77a3ef3499f03d0a 100644 (file)
@@ -146,5 +146,5 @@ void progressbarinit(struct ProgressData *bar,
   bar->width = scr_size[0] - 1;
 #endif
 
-  bar->out = config->errors;
+  bar->out = config->global->errors;
 }
index 6ca06ff01a589f17aff066f5b82b2d5f3dd57d7f..444cddc42925ca6b6f37b0b35d0e72786822afc6 100644 (file)
@@ -30,7 +30,6 @@ void config_init(struct OperationConfig* config)
 {
   memset(config, 0, sizeof(struct OperationConfig));
 
-  config->errors = stderr; /* default errors to stderr */
   config->postfieldsize = -1;
   config->use_httpget = FALSE;
   config->create_dirs = FALSE;
@@ -116,10 +115,6 @@ static void free_config_fields(struct OperationConfig *config)
 
   Curl_safefree(config->writeout);
 
-  if(config->errors_fopened && config->errors)
-    fclose(config->errors);
-  config->errors = NULL;
-
   curl_slist_free_all(config->quote);
   curl_slist_free_all(config->postquote);
   curl_slist_free_all(config->prequote);
index d7493431acf1dacefc574dfb4385f3815f1f0549..efbba0f2f28126884570a31ce795b572fd3572b9 100644 (file)
@@ -138,8 +138,6 @@ struct OperationConfig {
   bool proxyanyauth;
   char *writeout;           /* %-styled format string to output */
   bool writeenv;            /* write results to environment, if available */
-  FILE *errors;             /* errors stream, defaults to stderr */
-  bool errors_fopened;      /* whether errors stream isn't stderr */
   struct curl_slist *quote;
   struct curl_slist *postquote;
   struct curl_slist *prequote;
@@ -220,6 +218,8 @@ struct GlobalConfig {
                                       0 => -s is used to NOT show errors
                                       1 => -S has been used to show errors */
   bool mute;                      /* don't show messages, --silent given */
+  FILE *errors;                   /* Error stream, defaults to stderr */
+  bool errors_fopened;            /* Whether error stream isn't stderr */
   char *trace_dump;               /* file to dump the network trace to */
   FILE *trace_stream;
   bool trace_fopened;
index 5198822fd963d9bc541aed3cd782d2ba43a78acc..d8fc8fd6cecaa5f23c5109fe88641da6ad95662f 100644 (file)
@@ -163,7 +163,7 @@ int formparse(struct OperationConfig *config,
     /* Allocate the contents */
     contents = strdup(contp+1);
     if(!contents) {
-      fprintf(config->errors, "out of memory\n");
+      fprintf(config->global->errors, "out of memory\n");
       return 1;
     }
     contp = contents;
@@ -277,7 +277,7 @@ int formparse(struct OperationConfig *config,
         }
         forms = malloc((count+1)*sizeof(struct curl_forms));
         if(!forms) {
-          fprintf(config->errors, "Error building form post!\n");
+          fprintf(config->global->errors, "Error building form post!\n");
           Curl_safefree(contents);
           FreeMultiInfo(&multi_start, &multi_current);
           return 4;
index d858ff5f3fc7ed07ff9e771496dfea77712350c4..c6377ed60f93c8ff99d7cae54afa789cdcb20b27 100644 (file)
@@ -681,14 +681,14 @@ ParameterError getparameter(char *flag,    /* f or -long-flag */
           if(!newfile)
             warnf(config, "Failed to open %s!\n", nextarg);
           else {
-            if(config->errors_fopened)
-              fclose(config->errors);
-            config->errors = newfile;
-            config->errors_fopened = TRUE;
+            if(global->errors_fopened)
+              fclose(global->errors);
+            global->errors = newfile;
+            global->errors_fopened = TRUE;
           }
         }
         else
-          config->errors = stdout;
+          global->errors = stdout;
         break;
       case 'w': /* --interface */
         /* interface */
@@ -1875,9 +1875,9 @@ ParameterError parse_args(struct GlobalConfig *config, int argc,
     const char *reason = param2text(result);
 
     if(!curlx_strequal(":", orig_opt))
-      helpf(operation->errors, "option %s: %s\n", orig_opt, reason);
+      helpf(config->errors, "option %s: %s\n", orig_opt, reason);
     else
-      helpf(operation->errors, "%s\n", reason);
+      helpf(config->errors, "%s\n", reason);
   }
 
   return result;
index 1e9156aca31032f0ac262429c8a607739f32dea4..8a613cb21e0fd7dcac08410d766d3009ea36e18c 100644 (file)
@@ -132,6 +132,7 @@ static CURLcode main_init(struct GlobalConfig *config)
 
   /* Initialise the global config */
   config->showerror = -1;             /* Will show errors */
+  config->errors = stderr;            /* Default errors to stderr */
 
   /* Allocate the initial operate config */
   config->first = config->last = malloc(sizeof(struct OperationConfig));
@@ -179,6 +180,10 @@ static void free_config_fields(struct GlobalConfig *config)
 {
   Curl_safefree(config->trace_dump);
 
+  if(config->errors_fopened && config->errors)
+    fclose(config->errors);
+  config->errors = NULL;
+
   if(config->trace_fopened && config->trace_stream)
     fclose(config->trace_stream);
   config->trace_stream = NULL;
index a9fdf62e962040c50203d6cfb7926d4d7852a074..cd8df41f997458b2db4a63933d00d8551eb36579 100644 (file)
@@ -594,7 +594,7 @@ static int check_hash(const char *filename,
   return check_ok;
 }
 
-int metalink_check_hash(struct OperationConfig *config,
+int metalink_check_hash(struct GlobalConfig *config,
                         metalinkfile *mlfile,
                         const char *filename)
 {
@@ -602,8 +602,7 @@ int metalink_check_hash(struct OperationConfig *config,
   fprintf(config->errors, "Metalink: validating (%s)...\n", filename);
   if(mlfile->checksum == NULL) {
     fprintf(config->errors,
-            "Metalink: validating (%s) FAILED (digest missing)\n",
-            filename);
+            "Metalink: validating (%s) FAILED (digest missing)\n", filename);
     return -2;
   }
   rv = check_hash(filename, mlfile->checksum->digest_def,
@@ -727,7 +726,7 @@ int parse_metalink(struct OperationConfig *config, struct OutStruct *outs,
     return -1;
   }
   if(metalink->files == NULL) {
-    fprintf(config->errors, "Metalink: parsing (%s) WARNING "
+    fprintf(config->global->errors, "Metalink: parsing (%s) WARNING "
             "(missing or invalid file name)\n",
             metalink_url);
     metalink_delete(metalink);
@@ -737,7 +736,7 @@ int parse_metalink(struct OperationConfig *config, struct OutStruct *outs,
     struct getout *url;
     /* Skip an entry which has no resource. */
     if(!(*files)->resources) {
-      fprintf(config->errors, "Metalink: parsing (%s) WARNING "
+      fprintf(config->global->errors, "Metalink: parsing (%s) WARNING "
               "(missing or invalid resource)\n",
               metalink_url, (*files)->name);
       continue;
@@ -764,8 +763,8 @@ int parse_metalink(struct OperationConfig *config, struct OutStruct *outs,
       mlfile = new_metalinkfile(*files);
       if(!mlfile->checksum) {
         warnings = TRUE;
-        fprintf(config->errors, "Metalink: parsing (%s) WARNING "
-                "(digest missing)\n",
+        fprintf(config->global->errors,
+                "Metalink: parsing (%s) WARNING (digest missing)\n",
                 metalink_url);
       }
       /* Set name as url */
@@ -809,7 +808,7 @@ size_t metalink_write_cb(void *buffer, size_t sz, size_t nmemb,
   if(rv == 0)
     return sz * nmemb;
   else {
-    fprintf(config->errors, "Metalink: parsing FAILED\n");
+    fprintf(config->global->errors, "Metalink: parsing FAILED\n");
     return failure;
   }
 }
index 7dc21e53d90e92878f7ec855722e23c83030d5c6..339fdac0f1a784382018e3c3d471b865750dcab7 100644 (file)
@@ -142,7 +142,7 @@ int check_metalink_content_type(const char *content_type);
  *   No checksum in Metalink supported, hash algorithm not available, or
  *   Metalink does not contain checksum.
  */
-int metalink_check_hash(struct OperationConfig *config,
+int metalink_check_hash(struct GlobalConfig *config,
                         metalinkfile *mlfile,
                         const char *filename);
 
index c92fbace2530fffd75bbe0ac401147a5ab4e347a..3311b55f5fea35ba73bb8d424bef62dbb90fdd7f 100644 (file)
@@ -52,7 +52,7 @@ void warnf(struct OperationConfig *config, const char *fmt, ...)
 
     ptr = print_buffer;
     while(len > 0) {
-      fputs(WARN_PREFIX, config->errors);
+      fputs(WARN_PREFIX, config->global->errors);
 
       if(len > (int)WARN_TEXTWIDTH) {
         int cut = WARN_TEXTWIDTH-1;
@@ -65,13 +65,13 @@ void warnf(struct OperationConfig *config, const char *fmt, ...)
              max text width then! */
           cut = WARN_TEXTWIDTH-1;
 
-        (void)fwrite(ptr, cut + 1, 1, config->errors);
-        fputs("\n", config->errors);
+        (void)fwrite(ptr, cut + 1, 1, config->global->errors);
+        fputs("\n", config->global->errors);
         ptr += cut+1; /* skip the space too */
         len -= cut;
       }
       else {
-        fputs(ptr, config->errors);
+        fputs(ptr, config->global->errors);
         len = 0;
       }
     }
index 67808d44228da4cd60bf9251fb63aaa9b1ea2b4d..9683f3d91a58824aa84422b1c168a436de6e5f18 100644 (file)
@@ -224,7 +224,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
 
   /* Check we have a url */
   if(!config->url_list || !config->url_list->url) {
-    helpf(config->errors, "no URL specified!\n");
+    helpf(global->errors, "no URL specified!\n");
     res = CURLE_FAILED_INIT;
     goto quit_curl;
   }
@@ -247,7 +247,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
       config->cacert = strdup(env);
       if(!config->cacert) {
         curl_free(env);
-        helpf(config->errors, "out of memory\n");
+        helpf(global->errors, "out of memory\n");
         res = CURLE_OUT_OF_MEMORY;
         goto quit_curl;
       }
@@ -258,7 +258,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
         config->capath = strdup(env);
         if(!config->capath) {
           curl_free(env);
-          helpf(config->errors, "out of memory\n");
+          helpf(global->errors, "out of memory\n");
           res = CURLE_OUT_OF_MEMORY;
           goto quit_curl;
         }
@@ -269,7 +269,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
           config->cacert = strdup(env);
           if(!config->cacert) {
             curl_free(env);
-            helpf(config->errors, "out of memory\n");
+            helpf(global->errors, "out of memory\n");
             res = CURLE_OUT_OF_MEMORY;
             goto quit_curl;
           }
@@ -294,7 +294,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
       httpgetfields = strdup(config->postfields);
       Curl_safefree(config->postfields);
       if(!httpgetfields) {
-        helpf(config->errors, "out of memory\n");
+        helpf(global->errors, "out of memory\n");
         res = CURLE_OUT_OF_MEMORY;
         goto quit_curl;
       }
@@ -316,7 +316,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
 #ifndef CURL_DISABLE_LIBCURL_OPTION
   res = easysrc_init();
   if(res) {
-    helpf(config->errors, "out of memory\n");
+    helpf(global->errors, "out of memory\n");
     goto quit_curl;
   }
 #endif
@@ -399,7 +399,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
     if(urlnode->outfile) {
       outfiles = strdup(urlnode->outfile);
       if(!outfiles) {
-        helpf(config->errors, "out of memory\n");
+        helpf(global->errors, "out of memory\n");
         res = CURLE_OUT_OF_MEMORY;
         break;
       }
@@ -410,7 +410,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
     if(!config->globoff && infiles) {
       /* Unless explicitly shut off */
       res = glob_url(&inglob, infiles, &infilenum,
-                     global->showerror?config->errors:NULL);
+                     global->showerror?global->errors:NULL);
       if(res) {
         Curl_safefree(outfiles);
         break;
@@ -436,12 +436,12 @@ static CURLcode operate_do(struct GlobalConfig *global,
         if(inglob) {
           res = glob_next_url(&uploadfile, inglob);
           if(res == CURLE_OUT_OF_MEMORY)
-            helpf(config->errors, "out of memory\n");
+            helpf(global->errors, "out of memory\n");
         }
         else if(!up) {
           uploadfile = strdup(infiles);
           if(!uploadfile) {
-            helpf(config->errors, "out of memory\n");
+            helpf(global->errors, "out of memory\n");
             res = CURLE_OUT_OF_MEMORY;
           }
         }
@@ -461,7 +461,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
         /* Unless explicitly shut off, we expand '{...}' and '[...]'
            expressions and return total number of URLs in pattern set */
         res = glob_url(&urls, urlnode->url, &urlnum,
-                       global->showerror?config->errors:NULL);
+                       global->showerror?global->errors:NULL);
         if(res) {
           Curl_safefree(uploadfile);
           break;
@@ -555,7 +555,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
             if(res)
               goto show_error;
             if((!outfile || !*outfile) && !config->content_disposition) {
-              helpf(config->errors, "Remote file name has no length!\n");
+              helpf(global->errors, "Remote file name has no length!\n");
               res = CURLE_WRITE_ERROR;
               goto quit_urls;
             }
@@ -585,7 +585,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
              file output call */
 
           if(config->create_dirs || metalink) {
-            res = create_dir_hierarchy(outfile, config->errors);
+            res = create_dir_hierarchy(outfile, global->errors);
             /* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */
             if(res == CURLE_WRITE_ERROR)
               goto quit_urls;
@@ -624,7 +624,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
             FILE *file = fopen(outfile, config->resume_from?"ab":"wb");
 #endif
             if(!file) {
-              helpf(config->errors, "Can't open '%s'!\n", outfile);
+              helpf(global->errors, "Can't open '%s'!\n", outfile);
               res = CURLE_WRITE_ERROR;
               goto quit_urls;
             }
@@ -686,7 +686,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
           if((infd == -1) || fstat(infd, &fileinfo))
 #endif
           {
-            helpf(config->errors, "Can't open '%s'!\n", uploadfile);
+            helpf(global->errors, "Can't open '%s'!\n", uploadfile);
             if(infd != -1) {
               close(infd);
               infd = STDIN_FILENO;
@@ -754,7 +754,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
         }
 
         if(urlnum > 1 && !global->mute) {
-          fprintf(config->errors, "\n[%lu/%lu]: %s --> %s\n",
+          fprintf(global->errors, "\n[%lu/%lu]: %s --> %s\n",
                   li+1, urlnum, this_url, outfile ? outfile : "<stdout>");
           if(separator)
             printf("%s%s\n", CURLseparator, this_url);
@@ -799,8 +799,8 @@ static CURLcode operate_do(struct GlobalConfig *global,
           this_url = urlbuffer; /* use our new URL instead! */
         }
 
-        if(!config->errors)
-          config->errors = stderr;
+        if(!global->errors)
+          global->errors = stderr;
 
         if((!outfile || !strcmp(outfile, "-")) && !config->use_ascii) {
           /* We get the output to stdout and we have not got the ASCII/text
@@ -1098,7 +1098,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
         my_setopt_enum(curl, CURLOPT_TIMECONDITION, (long)config->timecond);
         my_setopt(curl, CURLOPT_TIMEVALUE, (long)config->condtime);
         my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
-        my_setopt(curl, CURLOPT_STDERR, config->errors);
+        my_setopt(curl, CURLOPT_STDERR, global->errors);
 
         /* three new ones in libcurl 7.3: */
         my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
@@ -1357,11 +1357,12 @@ static CURLcode operate_do(struct GlobalConfig *global,
               res = CURLE_OUT_OF_MEMORY;
               goto show_error;
             }
-            fprintf(config->errors, "Metalink: parsing (%s) metalink/XML...\n",
-                    this_url);
+            fprintf(config->global->errors,
+                    "Metalink: parsing (%s) metalink/XML...\n", this_url);
           }
           else if(metalink)
-            fprintf(config->errors, "Metalink: fetching (%s) from (%s)...\n",
+            fprintf(config->global->errors,
+                    "Metalink: fetching (%s) from (%s)...\n",
                     mlfile->filename, this_url);
 #endif /* USE_METALINK */
 
@@ -1461,7 +1462,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
                 /* We have written data to a output file, we truncate file
                  */
                 if(!global->mute)
-                  fprintf(config->errors, "Throwing away %"
+                  fprintf(global->errors, "Throwing away %"
                           CURL_FORMAT_CURL_OFF_T " bytes\n",
                           outs.bytes);
                 fflush(outs.stream);
@@ -1471,7 +1472,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
                   /* when truncate fails, we can't just append as then we'll
                      create something strange, bail out */
                   if(!global->mute)
-                    fprintf(config->errors,
+                    fprintf(global->errors,
                             "failed to truncate, exiting\n");
                   res = CURLE_WRITE_ERROR;
                   goto quit_urls;
@@ -1507,7 +1508,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
                 if(response != 200 && response != 206) {
                   metalink_next_res = 1;
-                  fprintf(config->errors,
+                  fprintf(global->errors,
                           "Metalink: fetching (%s) from (%s) FAILED "
                           "(HTTP status code %d)\n",
                           mlfile->filename, this_url, response);
@@ -1516,7 +1517,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
             }
             else {
               metalink_next_res = 1;
-              fprintf(config->errors,
+              fprintf(global->errors,
                       "Metalink: fetching (%s) from (%s) FAILED (%s)\n",
                       mlfile->filename, this_url,
                       (errorbuffer[0]) ?
@@ -1524,7 +1525,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
             }
           }
           if(metalink && !metalink_next_res)
-            fprintf(config->errors, "Metalink: fetching (%s) from (%s) OK\n",
+            fprintf(global->errors, "Metalink: fetching (%s) from (%s) OK\n",
                     mlfile->filename, this_url);
 
           /* In all ordinary cases, just break out of loop here */
@@ -1562,10 +1563,10 @@ static CURLcode operate_do(struct GlobalConfig *global,
         else
 #endif
         if(res && global->showerror) {
-          fprintf(config->errors, "curl: (%d) %s\n", res, (errorbuffer[0]) ?
+          fprintf(global->errors, "curl: (%d) %s\n", res, (errorbuffer[0]) ?
                   errorbuffer : curl_easy_strerror((CURLcode)res));
           if(res == CURLE_SSL_CACERT)
-            fprintf(config->errors, "%s%s",
+            fprintf(global->errors, "%s%s",
                     CURL_CA_CERT_ERRORMSG1, CURL_CA_CERT_ERRORMSG2);
         }
 
@@ -1598,7 +1599,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
           if(!res && rc) {
             /* something went wrong in the writing process */
             res = CURLE_WRITE_ERROR;
-            fprintf(config->errors, "(%d) Failed writing body\n", res);
+            fprintf(global->errors, "(%d) Failed writing body\n", res);
           }
         }
         else if(!outs.s_isreg && outs.stream) {
@@ -1607,7 +1608,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
           if(!res && rc) {
             /* something went wrong in the writing process */
             res = CURLE_WRITE_ERROR;
-            fprintf(config->errors, "(%d) Failed writing body\n", res);
+            fprintf(global->errors, "(%d) Failed writing body\n", res);
           }
         }
 
@@ -1639,13 +1640,14 @@ static CURLcode operate_do(struct GlobalConfig *global,
         if(!metalink && config->use_metalink && res == CURLE_OK) {
           int rv = parse_metalink(config, &outs, this_url);
           if(rv == 0)
-            fprintf(config->errors, "Metalink: parsing (%s) OK\n", this_url);
+            fprintf(config->global->errors, "Metalink: parsing (%s) OK\n",
+                    this_url);
           else if(rv == -1)
-            fprintf(config->errors, "Metalink: parsing (%s) FAILED\n",
+            fprintf(config->global->errors, "Metalink: parsing (%s) FAILED\n",
                     this_url);
         }
         else if(metalink && res == CURLE_OK && !metalink_next_res) {
-          int rv = metalink_check_hash(config, mlfile, outs.filename);
+          int rv = metalink_check_hash(global, mlfile, outs.filename);
           if(rv == 0) {
             metalink_next_res = 1;
           }
@@ -1795,7 +1797,7 @@ CURLcode operate(struct GlobalConfig *config, int argc, argv_item_t argv[])
 
     /* If we had no arguments then make sure a url was specified in .curlrc */
     if((argc < 2) && (!config->first->url_list)) {
-      helpf(config->first->errors, NULL);
+      helpf(config->errors, NULL);
       result = CURLE_FAILED_INIT;
     }
   }
index 0bbede996e08417556a2a339e0ff369000a8664d..18da0265e97a07d025182b5f2a7fa9d9a13e2991 100644 (file)
@@ -504,7 +504,7 @@ CURLcode get_args(struct OperationConfig *config, const size_t i)
   if(!config->useragent) {
     config->useragent = my_useragent();
     if(!config->useragent) {
-      helpf(config->errors, "out of memory\n");
+      helpf(config->global->errors, "out of memory\n");
       result = CURLE_OUT_OF_MEMORY;
     }
   }