tool: Fixed line longer than 79 characters from commit 705a4cb549
[platform/upstream/curl.git] / src / tool_parsecfg.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 #include "tool_setup.h"
23
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
26 #include "curlx.h"
27
28 #include "tool_cfgable.h"
29 #include "tool_getparam.h"
30 #include "tool_helpers.h"
31 #include "tool_homedir.h"
32 #include "tool_msgs.h"
33 #include "tool_parsecfg.h"
34
35 #include "memdebug.h" /* keep this as LAST include */
36
37 #define CURLRC DOT_CHAR "curlrc"
38
39 /* only acknowledge colon or equals as separators if the option was not
40    specified with an initial dash! */
41 #define ISSEP(x,dash) (!dash && (((x) == '=') || ((x) == ':')))
42
43 static const char *unslashquote(const char *line, char *param);
44 static char *my_get_line(FILE *fp);
45
46 /* return 0 on everything-is-fine, and non-zero otherwise */
47 int parseconfig(const char *filename, struct OperationConfig *config)
48 {
49   int res;
50   FILE *file;
51   char filebuffer[512];
52   bool usedarg;
53   char *home;
54   int rc = 0;
55
56   if(!filename || !*filename) {
57     /* NULL or no file name attempts to load .curlrc from the homedir! */
58
59 #ifndef __AMIGA__
60     filename = CURLRC;   /* sensible default */
61     home = homedir();    /* portable homedir finder */
62     if(home) {
63       if(strlen(home) < (sizeof(filebuffer) - strlen(CURLRC))) {
64         snprintf(filebuffer, sizeof(filebuffer),
65                  "%s%s%s", home, DIR_CHAR, CURLRC);
66
67 #ifdef WIN32
68         /* Check if the file exists - if not, try CURLRC in the same
69          * directory as our executable
70          */
71         file = fopen(filebuffer, "r");
72         if(file != NULL) {
73           fclose(file);
74           filename = filebuffer;
75         }
76         else {
77           /* Get the filename of our executable. GetModuleFileName is
78            * already declared via inclusions done in setup header file.
79            * We assume that we are using the ASCII version here.
80            */
81           int n = GetModuleFileName(0, filebuffer, sizeof(filebuffer));
82           if(n > 0 && n < (int)sizeof(filebuffer)) {
83             /* We got a valid filename - get the directory part */
84             char *lastdirchar = strrchr(filebuffer, '\\');
85             if(lastdirchar) {
86               size_t remaining;
87               *lastdirchar = 0;
88               /* If we have enough space, build the RC filename */
89               remaining = sizeof(filebuffer) - strlen(filebuffer);
90               if(strlen(CURLRC) < remaining - 1) {
91                 snprintf(lastdirchar, remaining,
92                          "%s%s", DIR_CHAR, CURLRC);
93                 /* Don't bother checking if it exists - we do
94                  * that later
95                  */
96                 filename = filebuffer;
97               }
98             }
99           }
100         }
101 #else /* WIN32 */
102         filename = filebuffer;
103 #endif /* WIN32 */
104       }
105       Curl_safefree(home); /* we've used it, now free it */
106     }
107
108 # else /* __AMIGA__ */
109     /* On AmigaOS all the config files are into env:
110      */
111     filename = "ENV:" CURLRC;
112
113 #endif
114   }
115
116   if(strcmp(filename,"-"))
117     file = fopen(filename, "r");
118   else
119     file = stdin;
120
121   if(file) {
122     char *line;
123     char *aline;
124     char *option;
125     char *param;
126     int lineno = 0;
127     bool alloced_param;
128     bool dashed_option;
129
130     while(NULL != (aline = my_get_line(file))) {
131       lineno++;
132       line = aline;
133       alloced_param=FALSE;
134
135       /* line with # in the first non-blank column is a comment! */
136       while(*line && ISSPACE(*line))
137         line++;
138
139       switch(*line) {
140       case '#':
141       case '/':
142       case '\r':
143       case '\n':
144       case '*':
145       case '\0':
146         Curl_safefree(aline);
147         continue;
148       }
149
150       /* the option keywords starts here */
151       option = line;
152
153       /* the option starts with a dash? */
154       dashed_option = option[0]=='-'?TRUE:FALSE;
155
156       while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
157         line++;
158       /* ... and has ended here */
159
160       if(*line)
161         *line++ = '\0'; /* zero terminate, we have a local copy of the data */
162
163 #ifdef DEBUG_CONFIG
164       fprintf(stderr, "GOT: %s\n", option);
165 #endif
166
167       /* pass spaces and separator(s) */
168       while(*line && (ISSPACE(*line) || ISSEP(*line, dashed_option)))
169         line++;
170
171       /* the parameter starts here (unless quoted) */
172       if(*line == '\"') {
173         /* quoted parameter, do the quote dance */
174         line++;
175         param = malloc(strlen(line) + 1); /* parameter */
176         if(!param) {
177           /* out of memory */
178           Curl_safefree(aline);
179           rc = 1;
180           break;
181         }
182         alloced_param = TRUE;
183         (void)unslashquote(line, param);
184       }
185       else {
186         param = line; /* parameter starts here */
187         while(*line && !ISSPACE(*line))
188           line++;
189         *line = '\0'; /* zero terminate */
190
191         /* to detect mistakes better, see if there's data following */
192         line++;
193         /* pass all spaces */
194         while(*line && ISSPACE(*line))
195           line++;
196
197         switch(*line) {
198         case '\0':
199         case '\r':
200         case '\n':
201         case '#': /* comment */
202           break;
203         default:
204           warnf(config, "%s:%d: warning: '%s' uses unquoted white space in the"
205                 " line that may cause side-effects!\n",
206                 filename, lineno, option);
207         }
208       }
209
210       if(param && !*param) {
211         /* do this so getparameter can check for required parameters.
212            Otherwise it always thinks there's a parameter. */
213         if(alloced_param)
214           Curl_safefree(param);
215         param = NULL;
216       }
217
218 #ifdef DEBUG_CONFIG
219       fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
220 #endif
221       res = getparameter(option, param, &usedarg, config);
222
223       if(param && *param && !usedarg)
224         /* we passed in a parameter that wasn't used! */
225         res = PARAM_GOT_EXTRA_PARAMETER;
226
227       if(res != PARAM_OK) {
228         /* the help request isn't really an error */
229         if(!strcmp(filename, "-")) {
230           filename = (char *)"<stdin>";
231         }
232         if(res != PARAM_HELP_REQUESTED &&
233            res != PARAM_MANUAL_REQUESTED &&
234            res != PARAM_VERSION_INFO_REQUESTED &&
235            res != PARAM_ENGINES_REQUESTED) {
236           const char *reason = param2text(res);
237           warnf(config, "%s:%d: warning: '%s' %s\n",
238                 filename, lineno, option, reason);
239         }
240       }
241
242       if(alloced_param)
243         Curl_safefree(param);
244
245       Curl_safefree(aline);
246     }
247     if(file != stdin)
248       fclose(file);
249   }
250   else
251     rc = 1; /* couldn't open the file */
252
253   return rc;
254 }
255
256 /*
257  * Copies the string from line to the buffer at param, unquoting
258  * backslash-quoted characters and NUL-terminating the output string.
259  * Stops at the first non-backslash-quoted double quote character or the
260  * end of the input string. param must be at least as long as the input
261  * string.  Returns the pointer after the last handled input character.
262  */
263 static const char *unslashquote(const char *line, char *param)
264 {
265   while(*line && (*line != '\"')) {
266     if(*line == '\\') {
267       char out;
268       line++;
269
270       /* default is to output the letter after the backslash */
271       switch(out = *line) {
272       case '\0':
273         continue; /* this'll break out of the loop */
274       case 't':
275         out = '\t';
276         break;
277       case 'n':
278         out = '\n';
279         break;
280       case 'r':
281         out = '\r';
282         break;
283       case 'v':
284         out = '\v';
285         break;
286       }
287       *param++ = out;
288       line++;
289     }
290     else
291       *param++ = *line++;
292   }
293   *param = '\0'; /* always zero terminate */
294   return line;
295 }
296
297 /*
298  * Reads a line from the given file, ensuring is NUL terminated.
299  * The pointer must be freed by the caller.
300  * NULL is returned on an out of memory condition.
301  */
302 static char *my_get_line(FILE *fp)
303 {
304   char buf[4096];
305   char *nl = NULL;
306   char *line = NULL;
307
308   do {
309     if(NULL == fgets(buf, sizeof(buf), fp))
310       break;
311     if(!line) {
312       line = strdup(buf);
313       if(!line)
314         return NULL;
315     }
316     else {
317       char *ptr;
318       size_t linelen = strlen(line);
319       ptr = realloc(line, linelen + strlen(buf) + 1);
320       if(!ptr) {
321         Curl_safefree(line);
322         return NULL;
323       }
324       line = ptr;
325       strcpy(&line[linelen], buf);
326     }
327     nl = strchr(line, '\n');
328   } while(!nl);
329
330   if(nl)
331     *nl = '\0';
332
333   return line;
334 }
335