Debian Codegraph @

deps/curl/src/tool_parsecfg.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, Daniel Stenberg, <[email protected]>, 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.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 /* only acknowledge colon or equals as separators if the option was not
38 specified with an initial dash! */
39 #define ISSEP(x,dash) (!dash && (((x) == '=') || ((x) == ':')))
40
41 static const char *unslashquote(const char *line, char *param);
42 static char *my_get_line(FILE *fp);
43
44 #ifdef WIN32
45 static FILE *execpath(const char *filename)
46 {
47 char filebuffer[512];
48 /* Get the filename of our executable. GetModuleFileName is already declared
49 * via inclusions done in setup header file. We assume that we are using
50 * the ASCII version here.
51 */
52 unsigned long len = GetModuleFileNameA(0, filebuffer, sizeof(filebuffer));
53 if(len > 0 && len < sizeof(filebuffer)) {
54 /* We got a valid filename - get the directory part */
55 char *lastdirchar = strrchr(filebuffer, '\\');
56 if(lastdirchar) {
57 size_t remaining;
58 *lastdirchar = 0;
59 /* If we have enough space, build the RC filename */
60 remaining = sizeof(filebuffer) - strlen(filebuffer);
61 if(strlen(filename) < remaining - 1) {
62 msnprintf(lastdirchar, remaining, "%s%s", DIR_CHAR, filename);
63 return fopen(filebuffer, FOPEN_READTEXT);
64 }
65 }
66 }
67
68 return NULL;
69 }
70 #endif
71
72
73 /* return 0 on everything-is-fine, and non-zero otherwise */
74 int parseconfig(const char *filename, struct GlobalConfig *global)
75 {
76 FILE *file = NULL;
77 bool usedarg = FALSE;
78 int rc = 0;
79 struct OperationConfig *operation = global->first;
80 char *pathalloc = NULL;
81
82 if(!filename || !*filename) {
83 /* NULL or no file name attempts to load .curlrc from the homedir! */
84
85 char *home = homedir(); /* portable homedir finder */
86 #ifndef WIN32
87 if(home) {
88 pathalloc = curl_maprintf("%s%s.curlrc", home, DIR_CHAR);
89 if(!pathalloc) {
90 free(home);
91 return 1; /* out of memory */
92 }
93 filename = pathalloc;
94 }
95 #else /* Windows */
96 if(home) {
97 int i = 0;
98 char prefix = '.';
99 do {
100 /* if it was allocated in a previous attempt */
101 curl_free(pathalloc);
102 /* check for .curlrc then _curlrc in the home dir */
103 pathalloc = curl_maprintf("%s%s%ccurlrc", home, DIR_CHAR, prefix);
104 if(!pathalloc) {
105 free(home);
106 return 1; /* out of memory */
107 }
108
109 /* Check if the file exists - if not, try _curlrc */
110 file = fopen(pathalloc, FOPEN_READTEXT);
111 if(file) {
112 filename = pathalloc;
113 break;
114 }
115 prefix = '_';
116 } while(++i < 2);
117 }
118 if(!filename) {
119 /* check for .curlrc then _curlrc in the dir of the executable */
120 file = execpath(".curlrc");
121 if(!file)
122 file = execpath("_curlrc");
123 }
124 #endif
125
126 Curl_safefree(home); /* we've used it, now free it */
127 }
128
129 if(!file && filename) { /* no need to fopen() again */
130 if(strcmp(filename, "-"))
131 file = fopen(filename, FOPEN_READTEXT);
132 else
133 file = stdin;
134 }
135
136 if(file) {
137 char *line;
138 char *aline;
139 char *option;
140 char *param;
141 int lineno = 0;
142 bool dashed_option;
143
144 while(NULL != (aline = my_get_line(file))) {
145 int res;
146 bool alloced_param = FALSE;
147 lineno++;
148 line = aline;
149
150 /* line with # in the first non-blank column is a comment! */
151 while(*line && ISSPACE(*line))
152 line++;
153
154 switch(*line) {
155 case '#':
156 case '/':
157 case '\r':
158 case '\n':
159 case '*':
160 case '\0':
161 Curl_safefree(aline);
162 continue;
163 }
164
165 /* the option keywords starts here */
166 option = line;
167
168 /* the option starts with a dash? */
169 dashed_option = option[0]=='-'?TRUE:FALSE;
170
171 while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
172 line++;
173 /* ... and has ended here */
174
175 if(*line)
176 *line++ = '\0'; /* zero terminate, we have a local copy of the data */
177
178 #ifdef DEBUG_CONFIG
179 fprintf(stderr, "GOT: %s\n", option);
180 #endif
181
182 /* pass spaces and separator(s) */
183 while(*line && (ISSPACE(*line) || ISSEP(*line, dashed_option)))
184 line++;
185
186 /* the parameter starts here (unless quoted) */
187 if(*line == '\"') {
188 /* quoted parameter, do the quote dance */
189 line++;
190 param = malloc(strlen(line) + 1); /* parameter */
191 if(!param) {
192 /* out of memory */
193 Curl_safefree(aline);
194 rc = 1;
195 break;
196 }
197 alloced_param = TRUE;
198 (void)unslashquote(line, param);
199 }
200 else {
201 param = line; /* parameter starts here */
202 while(*line && !ISSPACE(*line))
203 line++;
204
205 if(*line) {
206 *line = '\0'; /* zero terminate */
207
208 /* to detect mistakes better, see if there's data following */
209 line++;
210 /* pass all spaces */
211 while(*line && ISSPACE(*line))
212 line++;
213
214 switch(*line) {
215 case '\0':
216 case '\r':
217 case '\n':
218 case '#': /* comment */
219 break;
220 default:
221 warnf(operation->global, "%s:%d: warning: '%s' uses unquoted "
222 "white space in the line that may cause side-effects!\n",
223 filename, lineno, option);
224 }
225 }
226 if(!*param)
227 /* do this so getparameter can check for required parameters.
228 Otherwise it always thinks there's a parameter. */
229 param = NULL;
230 }
231
232 #ifdef DEBUG_CONFIG
233 fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
234 #endif
235 res = getparameter(option, param, &usedarg, global, operation);
236
237 if(!res && param && *param && !usedarg)
238 /* we passed in a parameter that wasn't used! */
239 res = PARAM_GOT_EXTRA_PARAMETER;
240
241 if(res == PARAM_NEXT_OPERATION) {
242 if(operation->url_list && operation->url_list->url) {
243 /* Allocate the next config */
244 operation->next = malloc(sizeof(struct OperationConfig));
245 if(operation->next) {
246 /* Initialise the newly created config */
247 config_init(operation->next);
248
249 /* Set the global config pointer */
250 operation->next->global = global;
251
252 /* Update the last operation pointer */
253 global->last = operation->next;
254
255 /* Move onto the new config */
256 operation->next->prev = operation;
257 operation = operation->next;
258 }
259 else
260 res = PARAM_NO_MEM;
261 }
262 }
263
264 if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
265 /* the help request isn't really an error */
266 if(!strcmp(filename, "-")) {
267 filename = "<stdin>";
268 }
269 if(res != PARAM_HELP_REQUESTED &&
270 res != PARAM_MANUAL_REQUESTED &&
271 res != PARAM_VERSION_INFO_REQUESTED &&
272 res != PARAM_ENGINES_REQUESTED) {
273 const char *reason = param2text(res);
274 warnf(operation->global, "%s:%d: warning: '%s' %s\n",
275 filename, lineno, option, reason);
276 }
277 }
278
279 if(alloced_param)
280 Curl_safefree(param);
281
282 Curl_safefree(aline);
283 }
284 if(file != stdin)
285 fclose(file);
286 }
287 else
288 rc = 1; /* couldn't open the file */
289
290 curl_free(pathalloc);
291 return rc;
292 }
293
294 /*
295 * Copies the string from line to the buffer at param, unquoting
296 * backslash-quoted characters and NUL-terminating the output string.
297 * Stops at the first non-backslash-quoted double quote character or the
298 * end of the input string. param must be at least as long as the input
299 * string. Returns the pointer after the last handled input character.
300 */
301 static const char *unslashquote(const char *line, char *param)
302 {
303 while(*line && (*line != '\"')) {
304 if(*line == '\\') {
305 char out;
306 line++;
307
308 /* default is to output the letter after the backslash */
309 switch(out = *line) {
310 case '\0':
311 continue; /* this'll break out of the loop */
312 case 't':
313 out = '\t';
314 break;
315 case 'n':
316 out = '\n';
317 break;
318 case 'r':
319 out = '\r';
320 break;
321 case 'v':
322 out = '\v';
323 break;
324 }
325 *param++ = out;
326 line++;
327 }
328 else
329 *param++ = *line++;
330 }
331 *param = '\0'; /* always zero terminate */
332 return line;
333 }
334
335 /*
336 * Reads a line from the given file, ensuring is NUL terminated.
337 * The pointer must be freed by the caller.
338 * NULL is returned on an out of memory condition.
339 */
340 static char *my_get_line(FILE *fp)
341 {
342 char buf[4096];
343 char *nl = NULL;
344 char *line = NULL;
345
346 do {
347 if(NULL == fgets(buf, sizeof(buf), fp))
348 break;
349 if(!line) {
350 line = strdup(buf);
351 if(!line)
352 return NULL;
353 }
354 else {
355 char *ptr;
356 size_t linelen = strlen(line);
357 ptr = realloc(line, linelen + strlen(buf) + 1);
358 if(!ptr) {
359 Curl_safefree(line);
360 return NULL;
361 }
362 line = ptr;
363 strcpy(&line[linelen], buf);
364 }
365 nl = strchr(line, '\n');
366 } while(!nl);
367
368 if(nl)
369 *nl = '\0';
370
371 return line;
372 }