1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
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"
35 #include "memdebug.h" /* keep this as LAST include */
37 #define CURLRC DOT_CHAR "curlrc"
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) == ':')))
43 static const char *unslashquote(const char *line, char *param);
44 static char *my_get_line(FILE *fp);
46 /* return 0 on everything-is-fine, and non-zero otherwise */
47 int parseconfig(const char *filename, struct GlobalConfig *global)
55 struct OperationConfig *operation = global->first;
57 if(!filename || !*filename) {
58 /* NULL or no file name attempts to load .curlrc from the homedir! */
61 filename = CURLRC; /* sensible default */
62 home = homedir(); /* portable homedir finder */
64 if(strlen(home) < (sizeof(filebuffer) - strlen(CURLRC))) {
65 snprintf(filebuffer, sizeof(filebuffer),
66 "%s%s%s", home, DIR_CHAR, CURLRC);
69 /* Check if the file exists - if not, try CURLRC in the same
70 * directory as our executable
72 file = fopen(filebuffer, FOPEN_READTEXT);
75 filename = filebuffer;
78 /* Get the filename of our executable. GetModuleFileName is
79 * already declared via inclusions done in setup header file.
80 * We assume that we are using the ASCII version here.
82 int n = GetModuleFileNameA(0, filebuffer, sizeof(filebuffer));
83 if(n > 0 && n < (int)sizeof(filebuffer)) {
84 /* We got a valid filename - get the directory part */
85 char *lastdirchar = strrchr(filebuffer, '\\');
89 /* If we have enough space, build the RC filename */
90 remaining = sizeof(filebuffer) - strlen(filebuffer);
91 if(strlen(CURLRC) < remaining - 1) {
92 snprintf(lastdirchar, remaining,
93 "%s%s", DIR_CHAR, CURLRC);
94 /* Don't bother checking if it exists - we do
97 filename = filebuffer;
103 filename = filebuffer;
106 Curl_safefree(home); /* we've used it, now free it */
109 # else /* __AMIGA__ */
110 /* On AmigaOS all the config files are into env:
112 filename = "ENV:" CURLRC;
117 if(strcmp(filename, "-"))
118 file = fopen(filename, FOPEN_READTEXT);
131 while(NULL != (aline = my_get_line(file))) {
134 alloced_param = FALSE;
136 /* line with # in the first non-blank column is a comment! */
137 while(*line && ISSPACE(*line))
147 Curl_safefree(aline);
151 /* the option keywords starts here */
154 /* the option starts with a dash? */
155 dashed_option = option[0]=='-'?TRUE:FALSE;
157 while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
159 /* ... and has ended here */
162 *line++ = '\0'; /* zero terminate, we have a local copy of the data */
165 fprintf(stderr, "GOT: %s\n", option);
168 /* pass spaces and separator(s) */
169 while(*line && (ISSPACE(*line) || ISSEP(*line, dashed_option)))
172 /* the parameter starts here (unless quoted) */
174 /* quoted parameter, do the quote dance */
176 param = malloc(strlen(line) + 1); /* parameter */
179 Curl_safefree(aline);
183 alloced_param = TRUE;
184 (void)unslashquote(line, param);
187 param = line; /* parameter starts here */
188 while(*line && !ISSPACE(*line))
192 *line = '\0'; /* zero terminate */
194 /* to detect mistakes better, see if there's data following */
196 /* pass all spaces */
197 while(*line && ISSPACE(*line))
204 case '#': /* comment */
207 warnf(operation->global, "%s:%d: warning: '%s' uses unquoted "
208 "white space in the line that may cause side-effects!\n",
209 filename, lineno, option);
213 /* do this so getparameter can check for required parameters.
214 Otherwise it always thinks there's a parameter. */
219 fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
221 res = getparameter(option, param, &usedarg, global, operation);
223 if(!res && param && *param && !usedarg)
224 /* we passed in a parameter that wasn't used! */
225 res = PARAM_GOT_EXTRA_PARAMETER;
227 if(res == PARAM_NEXT_OPERATION) {
228 if(operation->url_list && operation->url_list->url) {
229 /* Allocate the next config */
230 operation->next = malloc(sizeof(struct OperationConfig));
231 if(operation->next) {
232 /* Initialise the newly created config */
233 config_init(operation->next);
235 /* Copy the easy handle */
236 operation->next->easy = global->easy;
238 /* Set the global config pointer */
239 operation->next->global = global;
241 /* Update the last operation pointer */
242 global->last = operation->next;
244 /* Move onto the new config */
245 operation->next->prev = operation;
246 operation = operation->next;
253 if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
254 /* the help request isn't really an error */
255 if(!strcmp(filename, "-")) {
256 filename = "<stdin>";
258 if(res != PARAM_HELP_REQUESTED &&
259 res != PARAM_MANUAL_REQUESTED &&
260 res != PARAM_VERSION_INFO_REQUESTED &&
261 res != PARAM_ENGINES_REQUESTED) {
262 const char *reason = param2text(res);
263 warnf(operation->global, "%s:%d: warning: '%s' %s\n",
264 filename, lineno, option, reason);
269 Curl_safefree(param);
271 Curl_safefree(aline);
277 rc = 1; /* couldn't open the file */
283 * Copies the string from line to the buffer at param, unquoting
284 * backslash-quoted characters and NUL-terminating the output string.
285 * Stops at the first non-backslash-quoted double quote character or the
286 * end of the input string. param must be at least as long as the input
287 * string. Returns the pointer after the last handled input character.
289 static const char *unslashquote(const char *line, char *param)
291 while(*line && (*line != '\"')) {
296 /* default is to output the letter after the backslash */
297 switch(out = *line) {
299 continue; /* this'll break out of the loop */
319 *param = '\0'; /* always zero terminate */
324 * Reads a line from the given file, ensuring is NUL terminated.
325 * The pointer must be freed by the caller.
326 * NULL is returned on an out of memory condition.
328 static char *my_get_line(FILE *fp)
335 if(NULL == fgets(buf, sizeof(buf), fp))
344 size_t linelen = strlen(line);
345 ptr = realloc(line, linelen + strlen(buf) + 1);
351 strcpy(&line[linelen], buf);
353 nl = strchr(line, '\n');