680688ab7d3fc59a932eb25e35c094593dd47f89
[platform/upstream/curl.git] / src / tool_parsecfg.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, 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 #define ISSEP(x) (((x) == '=') || ((x) == ':'))
39
40 static const char *unslashquote(const char *line, char *param);
41 static char *my_get_line(FILE *fp);
42
43 /* return 0 on everything-is-fine, and non-zero otherwise */
44 int parseconfig(const char *filename,
45                 struct Configurable *config)
46 {
47   int res;
48   FILE *file;
49   char filebuffer[512];
50   bool usedarg;
51   char *home;
52   int rc = 0;
53
54   if(!filename || !*filename) {
55     /* NULL or no file name attempts to load .curlrc from the homedir! */
56
57 #ifndef __AMIGA__
58     filename = CURLRC;   /* sensible default */
59     home = homedir();    /* portable homedir finder */
60     if(home) {
61       if(strlen(home) < (sizeof(filebuffer) - strlen(CURLRC))) {
62         snprintf(filebuffer, sizeof(filebuffer),
63                  "%s%s%s", home, DIR_CHAR, CURLRC);
64
65 #ifdef WIN32
66         /* Check if the file exists - if not, try CURLRC in the same
67          * directory as our executable
68          */
69         file = fopen(filebuffer, "r");
70         if(file != NULL) {
71           fclose(file);
72           filename = filebuffer;
73         }
74         else {
75           /* Get the filename of our executable. GetModuleFileName is
76            * already declared via inclusions done in setup header file.
77            * We assume that we are using the ASCII version here.
78            */
79           int n = GetModuleFileName(0, filebuffer, sizeof(filebuffer));
80           if(n > 0 && n < (int)sizeof(filebuffer)) {
81             /* We got a valid filename - get the directory part */
82             char *lastdirchar = strrchr(filebuffer, '\\');
83             if(lastdirchar) {
84               size_t remaining;
85               *lastdirchar = 0;
86               /* If we have enough space, build the RC filename */
87               remaining = sizeof(filebuffer) - strlen(filebuffer);
88               if(strlen(CURLRC) < remaining - 1) {
89                 snprintf(lastdirchar, remaining,
90                          "%s%s", DIR_CHAR, CURLRC);
91                 /* Don't bother checking if it exists - we do
92                  * that later
93                  */
94                 filename = filebuffer;
95               }
96             }
97           }
98         }
99 #else /* WIN32 */
100         filename = filebuffer;
101 #endif /* WIN32 */
102       }
103       Curl_safefree(home); /* we've used it, now free it */
104     }
105
106 # else /* __AMIGA__ */
107     /* On AmigaOS all the config files are into env:
108      */
109     filename = "ENV:" CURLRC;
110
111 #endif
112   }
113
114   if(strcmp(filename,"-"))
115     file = fopen(filename, "r");
116   else
117     file = stdin;
118
119   if(file) {
120     char *line;
121     char *aline;
122     char *option;
123     char *param;
124     int lineno = 0;
125     bool alloced_param;
126
127     while(NULL != (aline = my_get_line(file))) {
128       lineno++;
129       line = aline;
130       alloced_param=FALSE;
131
132       /* line with # in the first non-blank column is a comment! */
133       while(*line && ISSPACE(*line))
134         line++;
135
136       switch(*line) {
137       case '#':
138       case '/':
139       case '\r':
140       case '\n':
141       case '*':
142       case '\0':
143         Curl_safefree(aline);
144         continue;
145       }
146
147       /* the option keywords starts here */
148       option = line;
149       while(*line && !ISSPACE(*line) && !ISSEP(*line))
150         line++;
151       /* ... and has ended here */
152
153       if(*line)
154         *line++ = '\0'; /* zero terminate, we have a local copy of the data */
155
156 #ifdef DEBUG_CONFIG
157       fprintf(stderr, "GOT: %s\n", option);
158 #endif
159
160       /* pass spaces and separator(s) */
161       while(*line && (ISSPACE(*line) || ISSEP(*line)))
162         line++;
163
164       /* the parameter starts here (unless quoted) */
165       if(*line == '\"') {
166         /* quoted parameter, do the quote dance */
167         line++;
168         param = malloc(strlen(line) + 1); /* parameter */
169         if(!param) {
170           /* out of memory */
171           Curl_safefree(aline);
172           rc = 1;
173           break;
174         }
175         alloced_param = TRUE;
176         (void)unslashquote(line, param);
177       }
178       else {
179         param = line; /* parameter starts here */
180         while(*line && !ISSPACE(*line))
181           line++;
182         *line = '\0'; /* zero terminate */
183       }
184
185       if(param && !*param) {
186         /* do this so getparameter can check for required parameters.
187            Otherwise it always thinks there's a parameter. */
188         if(alloced_param)
189           Curl_safefree(param);
190         param = NULL;
191       }
192
193 #ifdef DEBUG_CONFIG
194       fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
195 #endif
196       res = getparameter(option, param, &usedarg, config);
197
198       if(param && *param && !usedarg)
199         /* we passed in a parameter that wasn't used! */
200         res = PARAM_GOT_EXTRA_PARAMETER;
201
202       if(res != PARAM_OK) {
203         /* the help request isn't really an error */
204         if(!strcmp(filename, "-")) {
205           filename = (char *)"<stdin>";
206         }
207         if(PARAM_HELP_REQUESTED != res) {
208           const char *reason = param2text(res);
209           warnf(config, "%s:%d: warning: '%s' %s\n",
210                 filename, lineno, option, reason);
211         }
212       }
213
214       if(alloced_param)
215         Curl_safefree(param);
216
217       Curl_safefree(aline);
218     }
219     if(file != stdin)
220       fclose(file);
221   }
222   else
223     rc = 1; /* couldn't open the file */
224
225   return rc;
226 }
227
228 /*
229  * Copies the string from line to the buffer at param, unquoting
230  * backslash-quoted characters and NUL-terminating the output string.
231  * Stops at the first non-backslash-quoted double quote character or the
232  * end of the input string. param must be at least as long as the input
233  * string.  Returns the pointer after the last handled input character.
234  */
235 static const char *unslashquote(const char *line, char *param)
236 {
237   while(*line && (*line != '\"')) {
238     if(*line == '\\') {
239       char out;
240       line++;
241
242       /* default is to output the letter after the backslash */
243       switch(out = *line) {
244       case '\0':
245         continue; /* this'll break out of the loop */
246       case 't':
247         out = '\t';
248         break;
249       case 'n':
250         out = '\n';
251         break;
252       case 'r':
253         out = '\r';
254         break;
255       case 'v':
256         out = '\v';
257         break;
258       }
259       *param++ = out;
260       line++;
261     }
262     else
263       *param++ = *line++;
264   }
265   *param = '\0'; /* always zero terminate */
266   return line;
267 }
268
269 /*
270  * Reads a line from the given file, ensuring is NUL terminated.
271  * The pointer must be freed by the caller.
272  * NULL is returned on an out of memory condition.
273  */
274 static char *my_get_line(FILE *fp)
275 {
276   char buf[4096];
277   char *nl = NULL;
278   char *line = NULL;
279
280   do {
281     if(NULL == fgets(buf, sizeof(buf), fp))
282       break;
283     if(!line) {
284       line = strdup(buf);
285       if(!line)
286         return NULL;
287     }
288     else {
289       char *ptr;
290       size_t linelen = strlen(line);
291       ptr = realloc(line, linelen + strlen(buf) + 1);
292       if(!ptr) {
293         Curl_safefree(line);
294         return NULL;
295       }
296       line = ptr;
297       strcpy(&line[linelen], buf);
298     }
299     nl = strchr(line, '\n');
300   } while(!nl);
301
302   if(nl)
303     *nl = '\0';
304
305   return line;
306 }
307