1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 http://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"
38 #define ISSEP(x) (((x) == '=') || ((x) == ':'))
40 static const char *unslashquote(const char *line, char *param);
41 static char *my_get_line(FILE *fp);
43 /* return 0 on everything-is-fine, and non-zero otherwise */
44 int parseconfig(const char *filename,
45 struct Configurable *config)
54 if(!filename || !*filename) {
55 /* NULL or no file name attempts to load .curlrc from the homedir! */
58 filename = CURLRC; /* sensible default */
59 home = homedir(); /* portable homedir finder */
61 if(strlen(home) < (sizeof(filebuffer) - strlen(CURLRC))) {
62 snprintf(filebuffer, sizeof(filebuffer),
63 "%s%s%s", home, DIR_CHAR, CURLRC);
66 /* Check if the file exists - if not, try CURLRC in the same
67 * directory as our executable
69 file = fopen(filebuffer, "r");
72 filename = filebuffer;
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.
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, '\\');
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
94 filename = filebuffer;
100 filename = filebuffer;
103 Curl_safefree(home); /* we've used it, now free it */
106 # else /* __AMIGA__ */
107 /* On AmigaOS all the config files are into env:
109 filename = "ENV:" CURLRC;
114 if(strcmp(filename,"-"))
115 file = fopen(filename, "r");
127 while(NULL != (aline = my_get_line(file))) {
132 /* line with # in the first non-blank column is a comment! */
133 while(*line && ISSPACE(*line))
143 Curl_safefree(aline);
147 /* the option keywords starts here */
149 while(*line && !ISSPACE(*line) && !ISSEP(*line))
151 /* ... and has ended here */
154 *line++ = '\0'; /* zero terminate, we have a local copy of the data */
157 fprintf(stderr, "GOT: %s\n", option);
160 /* pass spaces and separator(s) */
161 while(*line && (ISSPACE(*line) || ISSEP(*line)))
164 /* the parameter starts here (unless quoted) */
166 /* quoted parameter, do the quote dance */
168 param = malloc(strlen(line) + 1); /* parameter */
171 Curl_safefree(aline);
175 alloced_param = TRUE;
176 (void)unslashquote(line, param);
179 param = line; /* parameter starts here */
180 while(*line && !ISSPACE(*line))
182 *line = '\0'; /* zero terminate */
185 if(param && !*param) {
186 /* do this so getparameter can check for required parameters.
187 Otherwise it always thinks there's a parameter. */
189 Curl_safefree(param);
194 fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
196 res = getparameter(option, param, &usedarg, config);
198 if(param && *param && !usedarg)
199 /* we passed in a parameter that wasn't used! */
200 res = PARAM_GOT_EXTRA_PARAMETER;
202 if(res != PARAM_OK) {
203 /* the help request isn't really an error */
204 if(!strcmp(filename, "-")) {
205 filename = (char *)"<stdin>";
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);
215 Curl_safefree(param);
217 Curl_safefree(aline);
223 rc = 1; /* couldn't open the file */
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.
235 static const char *unslashquote(const char *line, char *param)
237 while(*line && (*line != '\"')) {
242 /* default is to output the letter after the backslash */
243 switch(out = *line) {
245 continue; /* this'll break out of the loop */
265 *param = '\0'; /* always zero terminate */
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.
274 static char *my_get_line(FILE *fp)
281 if(NULL == fgets(buf, sizeof(buf), fp))
290 size_t linelen = strlen(line);
291 ptr = realloc(line, linelen + strlen(buf) + 1);
297 strcpy(&line[linelen], buf);
299 nl = strchr(line, '\n');