tool_parsecfg.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #define ENABLE_CURLX_PRINTF
00025 /* use our own printf() functions */
00026 #include "curlx.h"
00027 
00028 #include "tool_cfgable.h"
00029 #include "tool_getparam.h"
00030 #include "tool_helpers.h"
00031 #include "tool_homedir.h"
00032 #include "tool_msgs.h"
00033 #include "tool_parsecfg.h"
00034 
00035 #include "memdebug.h" /* keep this as LAST include */
00036 
00037 #define CURLRC DOT_CHAR "curlrc"
00038 
00039 /* only acknowledge colon or equals as separators if the option was not
00040    specified with an initial dash! */
00041 #define ISSEP(x,dash) (!dash && (((x) == '=') || ((x) == ':')))
00042 
00043 static const char *unslashquote(const char *line, char *param);
00044 static char *my_get_line(FILE *fp);
00045 
00046 /* return 0 on everything-is-fine, and non-zero otherwise */
00047 int parseconfig(const char *filename, struct GlobalConfig *global)
00048 {
00049   int res;
00050   FILE *file;
00051   char filebuffer[512];
00052   bool usedarg;
00053   char *home;
00054   int rc = 0;
00055   struct OperationConfig *operation = global->first;
00056 
00057   if(!filename || !*filename) {
00058     /* NULL or no file name attempts to load .curlrc from the homedir! */
00059 
00060 #ifndef __AMIGA__
00061     filename = CURLRC;   /* sensible default */
00062     home = homedir();    /* portable homedir finder */
00063     if(home) {
00064       if(strlen(home) < (sizeof(filebuffer) - strlen(CURLRC))) {
00065         snprintf(filebuffer, sizeof(filebuffer),
00066                  "%s%s%s", home, DIR_CHAR, CURLRC);
00067 
00068 #ifdef WIN32
00069         /* Check if the file exists - if not, try CURLRC in the same
00070          * directory as our executable
00071          */
00072         file = fopen(filebuffer, FOPEN_READTEXT);
00073         if(file != NULL) {
00074           fclose(file);
00075           filename = filebuffer;
00076         }
00077         else {
00078           /* Get the filename of our executable. GetModuleFileName is
00079            * already declared via inclusions done in setup header file.
00080            * We assume that we are using the ASCII version here.
00081            */
00082           int n = GetModuleFileName(0, filebuffer, sizeof(filebuffer));
00083           if(n > 0 && n < (int)sizeof(filebuffer)) {
00084             /* We got a valid filename - get the directory part */
00085             char *lastdirchar = strrchr(filebuffer, '\\');
00086             if(lastdirchar) {
00087               size_t remaining;
00088               *lastdirchar = 0;
00089               /* If we have enough space, build the RC filename */
00090               remaining = sizeof(filebuffer) - strlen(filebuffer);
00091               if(strlen(CURLRC) < remaining - 1) {
00092                 snprintf(lastdirchar, remaining,
00093                          "%s%s", DIR_CHAR, CURLRC);
00094                 /* Don't bother checking if it exists - we do
00095                  * that later
00096                  */
00097                 filename = filebuffer;
00098               }
00099             }
00100           }
00101         }
00102 #else /* WIN32 */
00103         filename = filebuffer;
00104 #endif /* WIN32 */
00105       }
00106       Curl_safefree(home); /* we've used it, now free it */
00107     }
00108 
00109 # else /* __AMIGA__ */
00110     /* On AmigaOS all the config files are into env:
00111      */
00112     filename = "ENV:" CURLRC;
00113 
00114 #endif
00115   }
00116 
00117   if(strcmp(filename, "-"))
00118     file = fopen(filename, FOPEN_READTEXT);
00119   else
00120     file = stdin;
00121 
00122   if(file) {
00123     char *line;
00124     char *aline;
00125     char *option;
00126     char *param;
00127     int lineno = 0;
00128     bool alloced_param;
00129     bool dashed_option;
00130 
00131     while(NULL != (aline = my_get_line(file))) {
00132       lineno++;
00133       line = aline;
00134       alloced_param=FALSE;
00135 
00136       /* line with # in the first non-blank column is a comment! */
00137       while(*line && ISSPACE(*line))
00138         line++;
00139 
00140       switch(*line) {
00141       case '#':
00142       case '/':
00143       case '\r':
00144       case '\n':
00145       case '*':
00146       case '\0':
00147         Curl_safefree(aline);
00148         continue;
00149       }
00150 
00151       /* the option keywords starts here */
00152       option = line;
00153 
00154       /* the option starts with a dash? */
00155       dashed_option = option[0]=='-'?TRUE:FALSE;
00156 
00157       while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
00158         line++;
00159       /* ... and has ended here */
00160 
00161       if(*line)
00162         *line++ = '\0'; /* zero terminate, we have a local copy of the data */
00163 
00164 #ifdef DEBUG_CONFIG
00165       fprintf(stderr, "GOT: %s\n", option);
00166 #endif
00167 
00168       /* pass spaces and separator(s) */
00169       while(*line && (ISSPACE(*line) || ISSEP(*line, dashed_option)))
00170         line++;
00171 
00172       /* the parameter starts here (unless quoted) */
00173       if(*line == '\"') {
00174         /* quoted parameter, do the quote dance */
00175         line++;
00176         param = malloc(strlen(line) + 1); /* parameter */
00177         if(!param) {
00178           /* out of memory */
00179           Curl_safefree(aline);
00180           rc = 1;
00181           break;
00182         }
00183         alloced_param = TRUE;
00184         (void)unslashquote(line, param);
00185       }
00186       else {
00187         param = line; /* parameter starts here */
00188         while(*line && !ISSPACE(*line))
00189           line++;
00190 
00191         if(*line) {
00192           *line = '\0'; /* zero terminate */
00193 
00194           /* to detect mistakes better, see if there's data following */
00195           line++;
00196           /* pass all spaces */
00197           while(*line && ISSPACE(*line))
00198             line++;
00199 
00200           switch(*line) {
00201           case '\0':
00202           case '\r':
00203           case '\n':
00204           case '#': /* comment */
00205             break;
00206           default:
00207             warnf(operation->global, "%s:%d: warning: '%s' uses unquoted "
00208                   "white space in the line that may cause side-effects!\n",
00209                   filename, lineno, option);
00210           }
00211         }
00212         if(!*param)
00213           /* do this so getparameter can check for required parameters.
00214              Otherwise it always thinks there's a parameter. */
00215           param = NULL;
00216       }
00217 
00218 #ifdef DEBUG_CONFIG
00219       fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
00220 #endif
00221       res = getparameter(option, param, &usedarg, global, operation);
00222 
00223       if(param && *param && !usedarg)
00224         /* we passed in a parameter that wasn't used! */
00225         res = PARAM_GOT_EXTRA_PARAMETER;
00226 
00227       if(res == PARAM_NEXT_OPERATION) {
00228         if(operation->url_list && operation->url_list->url) {
00229           /* Allocate the next config */
00230           operation->next = malloc(sizeof(struct OperationConfig));
00231           if(operation->next) {
00232             /* Initialise the newly created config */
00233             config_init(operation->next);
00234 
00235             /* Copy the easy handle */
00236             operation->next->easy = global->easy;
00237 
00238             /* Set the global config pointer */
00239             operation->next->global = global;
00240 
00241             /* Update the last operation pointer */
00242             global->last = operation->next;
00243 
00244             /* Move onto the new config */
00245             operation->next->prev = operation;
00246             operation = operation->next;
00247           }
00248           else
00249             res = PARAM_NO_MEM;
00250         }
00251       }
00252 
00253       if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
00254         /* the help request isn't really an error */
00255         if(!strcmp(filename, "-")) {
00256           filename = (char *)"<stdin>";
00257         }
00258         if(res != PARAM_HELP_REQUESTED &&
00259            res != PARAM_MANUAL_REQUESTED &&
00260            res != PARAM_VERSION_INFO_REQUESTED &&
00261            res != PARAM_ENGINES_REQUESTED) {
00262           const char *reason = param2text(res);
00263           warnf(operation->global, "%s:%d: warning: '%s' %s\n",
00264                 filename, lineno, option, reason);
00265         }
00266       }
00267 
00268       if(alloced_param)
00269         Curl_safefree(param);
00270 
00271       Curl_safefree(aline);
00272     }
00273     if(file != stdin)
00274       fclose(file);
00275   }
00276   else
00277     rc = 1; /* couldn't open the file */
00278 
00279   return rc;
00280 }
00281 
00282 /*
00283  * Copies the string from line to the buffer at param, unquoting
00284  * backslash-quoted characters and NUL-terminating the output string.
00285  * Stops at the first non-backslash-quoted double quote character or the
00286  * end of the input string. param must be at least as long as the input
00287  * string.  Returns the pointer after the last handled input character.
00288  */
00289 static const char *unslashquote(const char *line, char *param)
00290 {
00291   while(*line && (*line != '\"')) {
00292     if(*line == '\\') {
00293       char out;
00294       line++;
00295 
00296       /* default is to output the letter after the backslash */
00297       switch(out = *line) {
00298       case '\0':
00299         continue; /* this'll break out of the loop */
00300       case 't':
00301         out = '\t';
00302         break;
00303       case 'n':
00304         out = '\n';
00305         break;
00306       case 'r':
00307         out = '\r';
00308         break;
00309       case 'v':
00310         out = '\v';
00311         break;
00312       }
00313       *param++ = out;
00314       line++;
00315     }
00316     else
00317       *param++ = *line++;
00318   }
00319   *param = '\0'; /* always zero terminate */
00320   return line;
00321 }
00322 
00323 /*
00324  * Reads a line from the given file, ensuring is NUL terminated.
00325  * The pointer must be freed by the caller.
00326  * NULL is returned on an out of memory condition.
00327  */
00328 static char *my_get_line(FILE *fp)
00329 {
00330   char buf[4096];
00331   char *nl = NULL;
00332   char *line = NULL;
00333 
00334   do {
00335     if(NULL == fgets(buf, sizeof(buf), fp))
00336       break;
00337     if(!line) {
00338       line = strdup(buf);
00339       if(!line)
00340         return NULL;
00341     }
00342     else {
00343       char *ptr;
00344       size_t linelen = strlen(line);
00345       ptr = realloc(line, linelen + strlen(buf) + 1);
00346       if(!ptr) {
00347         Curl_safefree(line);
00348         return NULL;
00349       }
00350       line = ptr;
00351       strcpy(&line[linelen], buf);
00352     }
00353     nl = strchr(line, '\n');
00354   } while(!nl);
00355 
00356   if(nl)
00357     *nl = '\0';
00358 
00359   return line;
00360 }
00361 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07