xref: /curl/src/tool_parsecfg.c (revision 22652a5a)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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 https://curl.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "tool_setup.h"
25 
26 #include "curlx.h"
27 
28 #include "tool_cfgable.h"
29 #include "tool_getparam.h"
30 #include "tool_helpers.h"
31 #include "tool_findfile.h"
32 #include "tool_msgs.h"
33 #include "tool_parsecfg.h"
34 #include "tool_util.h"
35 #include "dynbuf.h"
36 
37 #include "memdebug.h" /* keep this as LAST include */
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 
45 #define MAX_CONFIG_LINE_LENGTH (10*1024*1024)
46 static bool my_get_line(FILE *fp, struct curlx_dynbuf *, bool *error);
47 
48 
49 /* return 0 on everything-is-fine, and non-zero otherwise */
parseconfig(const char * filename,struct GlobalConfig * global)50 int parseconfig(const char *filename, struct GlobalConfig *global)
51 {
52   FILE *file = NULL;
53   bool usedarg = FALSE;
54   int rc = 0;
55   struct OperationConfig *operation = global->last;
56   char *pathalloc = NULL;
57 
58   if(!filename) {
59     /* NULL means load .curlrc from homedir! */
60     char *curlrc = findfile(".curlrc", CURLRC_DOTSCORE);
61     if(curlrc) {
62       file = fopen(curlrc, FOPEN_READTEXT);
63       if(!file) {
64         free(curlrc);
65         return 1;
66       }
67       filename = pathalloc = curlrc;
68     }
69 #ifdef _WIN32 /* Windows */
70     else {
71       char *fullp;
72       /* check for .curlrc then _curlrc in the dir of the executable */
73       file = Curl_execpath(".curlrc", &fullp);
74       if(!file)
75         file = Curl_execpath("_curlrc", &fullp);
76       if(file)
77         /* this is the filename we read from */
78         filename = fullp;
79     }
80 #endif
81   }
82   else {
83     if(strcmp(filename, "-"))
84       file = fopen(filename, FOPEN_READTEXT);
85     else
86       file = stdin;
87   }
88 
89   if(file) {
90     char *line;
91     char *option;
92     char *param;
93     int lineno = 0;
94     bool dashed_option;
95     struct curlx_dynbuf buf;
96     bool fileerror = FALSE;
97     curlx_dyn_init(&buf, MAX_CONFIG_LINE_LENGTH);
98     DEBUGASSERT(filename);
99 
100     while(!rc && my_get_line(file, &buf, &fileerror)) {
101       ParameterError res;
102       bool alloced_param = FALSE;
103       lineno++;
104       line = curlx_dyn_ptr(&buf);
105       if(!line) {
106         rc = 1; /* out of memory */
107         break;
108       }
109 
110       /* line with # in the first non-blank column is a comment! */
111       while(*line && ISSPACE(*line))
112         line++;
113 
114       switch(*line) {
115       case '#':
116       case '/':
117       case '\r':
118       case '\n':
119       case '*':
120       case '\0':
121         curlx_dyn_reset(&buf);
122         continue;
123       }
124 
125       /* the option keywords starts here */
126       option = line;
127 
128       /* the option starts with a dash? */
129       dashed_option = (option[0] == '-') ? TRUE : FALSE;
130 
131       while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
132         line++;
133       /* ... and has ended here */
134 
135       if(*line)
136         *line++ = '\0'; /* null-terminate, we have a local copy of the data */
137 
138 #ifdef DEBUG_CONFIG
139       fprintf(tool_stderr, "GOT: %s\n", option);
140 #endif
141 
142       /* pass spaces and separator(s) */
143       while(*line && (ISSPACE(*line) || ISSEP(*line, dashed_option)))
144         line++;
145 
146       /* the parameter starts here (unless quoted) */
147       if(*line == '\"') {
148         /* quoted parameter, do the quote dance */
149         line++;
150         param = malloc(strlen(line) + 1); /* parameter */
151         if(!param) {
152           /* out of memory */
153           rc = 1;
154           break;
155         }
156         alloced_param = TRUE;
157         (void)unslashquote(line, param);
158       }
159       else {
160         param = line; /* parameter starts here */
161         while(*line && !ISSPACE(*line))
162           line++;
163 
164         if(*line) {
165           *line = '\0'; /* null-terminate */
166 
167           /* to detect mistakes better, see if there is data following */
168           line++;
169           /* pass all spaces */
170           while(*line && ISSPACE(*line))
171             line++;
172 
173           switch(*line) {
174           case '\0':
175           case '\r':
176           case '\n':
177           case '#': /* comment */
178             break;
179           default:
180             warnf(operation->global, "%s:%d: warning: '%s' uses unquoted "
181                   "whitespace", filename, lineno, option);
182             warnf(operation->global, "This may cause side-effects. "
183                   "Consider using double quotes?");
184           }
185         }
186         if(!*param)
187           /* do this so getparameter can check for required parameters.
188              Otherwise it always thinks there is a parameter. */
189           param = NULL;
190       }
191 
192 #ifdef DEBUG_CONFIG
193       fprintf(tool_stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
194 #endif
195       res = getparameter(option, param, NULL, &usedarg, global, operation);
196       operation = global->last;
197 
198       if(!res && param && *param && !usedarg)
199         /* we passed in a parameter that was not used! */
200         res = PARAM_GOT_EXTRA_PARAMETER;
201 
202       if(res == PARAM_NEXT_OPERATION) {
203         if(operation->url_list && operation->url_list->url) {
204           /* Allocate the next config */
205           operation->next = malloc(sizeof(struct OperationConfig));
206           if(operation->next) {
207             /* Initialise the newly created config */
208             config_init(operation->next);
209 
210             /* Set the global config pointer */
211             operation->next->global = global;
212 
213             /* Update the last operation pointer */
214             global->last = operation->next;
215 
216             /* Move onto the new config */
217             operation->next->prev = operation;
218             operation = operation->next;
219           }
220           else
221             res = PARAM_NO_MEM;
222         }
223       }
224 
225       if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
226         /* the help request is not really an error */
227         if(!strcmp(filename, "-")) {
228           filename = "<stdin>";
229         }
230         if(res != PARAM_HELP_REQUESTED &&
231            res != PARAM_MANUAL_REQUESTED &&
232            res != PARAM_VERSION_INFO_REQUESTED &&
233            res != PARAM_ENGINES_REQUESTED &&
234            res != PARAM_CA_EMBED_REQUESTED) {
235           const char *reason = param2text(res);
236           errorf(operation->global, "%s:%d: '%s' %s",
237                  filename, lineno, option, reason);
238           rc = (int)res;
239         }
240       }
241 
242       if(alloced_param)
243         Curl_safefree(param);
244 
245       curlx_dyn_reset(&buf);
246     }
247     curlx_dyn_free(&buf);
248     if(file != stdin)
249       fclose(file);
250     if(fileerror)
251       rc = 1;
252   }
253   else
254     rc = 1; /* could not open the file */
255 
256   free(pathalloc);
257   return rc;
258 }
259 
260 /*
261  * Copies the string from line to the buffer at param, unquoting
262  * backslash-quoted characters and NUL-terminating the output string.
263  * Stops at the first non-backslash-quoted double quote character or the
264  * end of the input string. param must be at least as long as the input
265  * string. Returns the pointer after the last handled input character.
266  */
unslashquote(const char * line,char * param)267 static const char *unslashquote(const char *line, char *param)
268 {
269   while(*line && (*line != '\"')) {
270     if(*line == '\\') {
271       char out;
272       line++;
273 
274       /* default is to output the letter after the backslash */
275       switch(out = *line) {
276       case '\0':
277         continue; /* this'll break out of the loop */
278       case 't':
279         out = '\t';
280         break;
281       case 'n':
282         out = '\n';
283         break;
284       case 'r':
285         out = '\r';
286         break;
287       case 'v':
288         out = '\v';
289         break;
290       }
291       *param++ = out;
292       line++;
293     }
294     else
295       *param++ = *line++;
296   }
297   *param = '\0'; /* always null-terminate */
298   return line;
299 }
300 
301 /*
302  * Reads a line from the given file, ensuring is NUL terminated.
303  */
my_get_line(FILE * fp,struct curlx_dynbuf * db,bool * error)304 static bool my_get_line(FILE *fp, struct curlx_dynbuf *db,
305                         bool *error)
306 {
307   char buf[4096];
308   *error = FALSE;
309   do {
310     /* fgets() returns s on success, and NULL on error or when end of file
311        occurs while no characters have been read. */
312     if(!fgets(buf, sizeof(buf), fp))
313       /* only if there is data in the line, return TRUE */
314       return curlx_dyn_len(db) ? TRUE : FALSE;
315     if(curlx_dyn_add(db, buf)) {
316       *error = TRUE; /* error */
317       return FALSE; /* stop reading */
318     }
319   } while(!strchr(buf, '\n'));
320 
321   return TRUE; /* continue */
322 }
323