2011-10-04 18:03:20 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2016-12-02 05:25:35 -05:00
|
|
|
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-10-04 18:03:20 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-10-04 18:03:20 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_cfgable.h"
|
|
|
|
#include "tool_getparam.h"
|
|
|
|
#include "tool_helpers.h"
|
2011-10-06 11:39:00 -04:00
|
|
|
#include "tool_homedir.h"
|
2011-10-04 18:03:20 -04:00
|
|
|
#include "tool_msgs.h"
|
|
|
|
#include "tool_parsecfg.h"
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
#define CURLRC DOT_CHAR "curlrc"
|
2013-11-06 17:57:44 -05:00
|
|
|
|
|
|
|
/* only acknowledge colon or equals as separators if the option was not
|
|
|
|
specified with an initial dash! */
|
|
|
|
#define ISSEP(x,dash) (!dash && (((x) == '=') || ((x) == ':')))
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
static const char *unslashquote(const char *line, char *param);
|
|
|
|
static char *my_get_line(FILE *fp);
|
|
|
|
|
|
|
|
/* return 0 on everything-is-fine, and non-zero otherwise */
|
2014-02-23 11:04:39 -05:00
|
|
|
int parseconfig(const char *filename, struct GlobalConfig *global)
|
2011-10-04 18:03:20 -04:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
FILE *file;
|
|
|
|
char filebuffer[512];
|
|
|
|
bool usedarg;
|
|
|
|
char *home;
|
|
|
|
int rc = 0;
|
2014-02-23 11:04:39 -05:00
|
|
|
struct OperationConfig *operation = global->first;
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
if(!filename || !*filename) {
|
|
|
|
/* NULL or no file name attempts to load .curlrc from the homedir! */
|
|
|
|
|
|
|
|
#ifndef __AMIGA__
|
|
|
|
filename = CURLRC; /* sensible default */
|
|
|
|
home = homedir(); /* portable homedir finder */
|
|
|
|
if(home) {
|
|
|
|
if(strlen(home) < (sizeof(filebuffer) - strlen(CURLRC))) {
|
|
|
|
snprintf(filebuffer, sizeof(filebuffer),
|
|
|
|
"%s%s%s", home, DIR_CHAR, CURLRC);
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
/* Check if the file exists - if not, try CURLRC in the same
|
|
|
|
* directory as our executable
|
|
|
|
*/
|
2015-06-01 03:20:18 -04:00
|
|
|
file = fopen(filebuffer, FOPEN_READTEXT);
|
2011-10-04 18:03:20 -04:00
|
|
|
if(file != NULL) {
|
|
|
|
fclose(file);
|
|
|
|
filename = filebuffer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Get the filename of our executable. GetModuleFileName is
|
|
|
|
* already declared via inclusions done in setup header file.
|
|
|
|
* We assume that we are using the ASCII version here.
|
|
|
|
*/
|
2017-04-04 17:02:16 -04:00
|
|
|
int n = GetModuleFileNameA(0, filebuffer, sizeof(filebuffer));
|
2011-10-04 18:03:20 -04:00
|
|
|
if(n > 0 && n < (int)sizeof(filebuffer)) {
|
|
|
|
/* We got a valid filename - get the directory part */
|
|
|
|
char *lastdirchar = strrchr(filebuffer, '\\');
|
|
|
|
if(lastdirchar) {
|
|
|
|
size_t remaining;
|
|
|
|
*lastdirchar = 0;
|
|
|
|
/* If we have enough space, build the RC filename */
|
|
|
|
remaining = sizeof(filebuffer) - strlen(filebuffer);
|
|
|
|
if(strlen(CURLRC) < remaining - 1) {
|
|
|
|
snprintf(lastdirchar, remaining,
|
|
|
|
"%s%s", DIR_CHAR, CURLRC);
|
|
|
|
/* Don't bother checking if it exists - we do
|
|
|
|
* that later
|
|
|
|
*/
|
|
|
|
filename = filebuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* WIN32 */
|
|
|
|
filename = filebuffer;
|
|
|
|
#endif /* WIN32 */
|
|
|
|
}
|
|
|
|
Curl_safefree(home); /* we've used it, now free it */
|
|
|
|
}
|
|
|
|
|
|
|
|
# else /* __AMIGA__ */
|
|
|
|
/* On AmigaOS all the config files are into env:
|
|
|
|
*/
|
|
|
|
filename = "ENV:" CURLRC;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-17 08:41:49 -04:00
|
|
|
if(strcmp(filename, "-"))
|
2015-06-01 03:20:18 -04:00
|
|
|
file = fopen(filename, FOPEN_READTEXT);
|
2011-10-04 18:03:20 -04:00
|
|
|
else
|
|
|
|
file = stdin;
|
|
|
|
|
|
|
|
if(file) {
|
|
|
|
char *line;
|
|
|
|
char *aline;
|
|
|
|
char *option;
|
|
|
|
char *param;
|
|
|
|
int lineno = 0;
|
|
|
|
bool alloced_param;
|
2013-11-06 17:57:44 -05:00
|
|
|
bool dashed_option;
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
while(NULL != (aline = my_get_line(file))) {
|
|
|
|
lineno++;
|
|
|
|
line = aline;
|
|
|
|
alloced_param=FALSE;
|
|
|
|
|
|
|
|
/* line with # in the first non-blank column is a comment! */
|
|
|
|
while(*line && ISSPACE(*line))
|
|
|
|
line++;
|
|
|
|
|
|
|
|
switch(*line) {
|
|
|
|
case '#':
|
|
|
|
case '/':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
case '*':
|
|
|
|
case '\0':
|
|
|
|
Curl_safefree(aline);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the option keywords starts here */
|
|
|
|
option = line;
|
2013-11-06 17:57:44 -05:00
|
|
|
|
|
|
|
/* the option starts with a dash? */
|
|
|
|
dashed_option = option[0]=='-'?TRUE:FALSE;
|
|
|
|
|
|
|
|
while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
|
2011-10-04 18:03:20 -04:00
|
|
|
line++;
|
|
|
|
/* ... and has ended here */
|
|
|
|
|
|
|
|
if(*line)
|
|
|
|
*line++ = '\0'; /* zero terminate, we have a local copy of the data */
|
|
|
|
|
|
|
|
#ifdef DEBUG_CONFIG
|
|
|
|
fprintf(stderr, "GOT: %s\n", option);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* pass spaces and separator(s) */
|
2013-11-06 17:57:44 -05:00
|
|
|
while(*line && (ISSPACE(*line) || ISSEP(*line, dashed_option)))
|
2011-10-04 18:03:20 -04:00
|
|
|
line++;
|
|
|
|
|
|
|
|
/* the parameter starts here (unless quoted) */
|
|
|
|
if(*line == '\"') {
|
|
|
|
/* quoted parameter, do the quote dance */
|
|
|
|
line++;
|
|
|
|
param = malloc(strlen(line) + 1); /* parameter */
|
|
|
|
if(!param) {
|
|
|
|
/* out of memory */
|
|
|
|
Curl_safefree(aline);
|
|
|
|
rc = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
alloced_param = TRUE;
|
|
|
|
(void)unslashquote(line, param);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
param = line; /* parameter starts here */
|
|
|
|
while(*line && !ISSPACE(*line))
|
|
|
|
line++;
|
2013-12-09 02:19:04 -05:00
|
|
|
|
2015-04-16 18:38:50 -04:00
|
|
|
if(*line) {
|
|
|
|
*line = '\0'; /* zero terminate */
|
2013-12-09 02:19:04 -05:00
|
|
|
|
2015-04-16 18:38:50 -04:00
|
|
|
/* to detect mistakes better, see if there's data following */
|
|
|
|
line++;
|
|
|
|
/* pass all spaces */
|
|
|
|
while(*line && ISSPACE(*line))
|
|
|
|
line++;
|
|
|
|
|
|
|
|
switch(*line) {
|
|
|
|
case '\0':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
case '#': /* comment */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warnf(operation->global, "%s:%d: warning: '%s' uses unquoted "
|
|
|
|
"white space in the line that may cause side-effects!\n",
|
|
|
|
filename, lineno, option);
|
|
|
|
}
|
2013-12-09 02:19:04 -05:00
|
|
|
}
|
2016-12-02 05:25:35 -05:00
|
|
|
if(!*param)
|
|
|
|
/* do this so getparameter can check for required parameters.
|
|
|
|
Otherwise it always thinks there's a parameter. */
|
|
|
|
param = NULL;
|
2011-10-04 18:03:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_CONFIG
|
|
|
|
fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
|
|
|
|
#endif
|
2014-02-23 11:04:39 -05:00
|
|
|
res = getparameter(option, param, &usedarg, global, operation);
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
if(param && *param && !usedarg)
|
|
|
|
/* we passed in a parameter that wasn't used! */
|
|
|
|
res = PARAM_GOT_EXTRA_PARAMETER;
|
|
|
|
|
2014-02-23 13:01:36 -05:00
|
|
|
if(res == PARAM_NEXT_OPERATION) {
|
|
|
|
if(operation->url_list && operation->url_list->url) {
|
|
|
|
/* Allocate the next config */
|
|
|
|
operation->next = malloc(sizeof(struct OperationConfig));
|
|
|
|
if(operation->next) {
|
|
|
|
/* Initialise the newly created config */
|
|
|
|
config_init(operation->next);
|
|
|
|
|
|
|
|
/* Copy the easy handle */
|
|
|
|
operation->next->easy = global->easy;
|
|
|
|
|
2014-02-26 16:02:53 -05:00
|
|
|
/* Set the global config pointer */
|
|
|
|
operation->next->global = global;
|
|
|
|
|
2014-02-23 13:01:36 -05:00
|
|
|
/* Update the last operation pointer */
|
|
|
|
global->last = operation->next;
|
|
|
|
|
|
|
|
/* Move onto the new config */
|
|
|
|
operation->next->prev = operation;
|
|
|
|
operation = operation->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = PARAM_NO_MEM;
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 12:50:47 -04:00
|
|
|
|
|
|
|
if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
|
2011-10-04 18:03:20 -04:00
|
|
|
/* the help request isn't really an error */
|
|
|
|
if(!strcmp(filename, "-")) {
|
|
|
|
filename = (char *)"<stdin>";
|
|
|
|
}
|
2014-02-22 15:58:43 -05:00
|
|
|
if(res != PARAM_HELP_REQUESTED &&
|
2014-02-23 05:37:44 -05:00
|
|
|
res != PARAM_MANUAL_REQUESTED &&
|
2014-02-23 05:16:36 -05:00
|
|
|
res != PARAM_VERSION_INFO_REQUESTED &&
|
2014-02-22 15:58:43 -05:00
|
|
|
res != PARAM_ENGINES_REQUESTED) {
|
2011-10-04 18:03:20 -04:00
|
|
|
const char *reason = param2text(res);
|
2015-02-27 15:48:38 -05:00
|
|
|
warnf(operation->global, "%s:%d: warning: '%s' %s\n",
|
2011-10-04 18:03:20 -04:00
|
|
|
filename, lineno, option, reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(alloced_param)
|
|
|
|
Curl_safefree(param);
|
|
|
|
|
|
|
|
Curl_safefree(aline);
|
|
|
|
}
|
|
|
|
if(file != stdin)
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rc = 1; /* couldn't open the file */
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copies the string from line to the buffer at param, unquoting
|
|
|
|
* backslash-quoted characters and NUL-terminating the output string.
|
|
|
|
* Stops at the first non-backslash-quoted double quote character or the
|
|
|
|
* end of the input string. param must be at least as long as the input
|
|
|
|
* string. Returns the pointer after the last handled input character.
|
|
|
|
*/
|
|
|
|
static const char *unslashquote(const char *line, char *param)
|
|
|
|
{
|
|
|
|
while(*line && (*line != '\"')) {
|
|
|
|
if(*line == '\\') {
|
|
|
|
char out;
|
|
|
|
line++;
|
|
|
|
|
|
|
|
/* default is to output the letter after the backslash */
|
|
|
|
switch(out = *line) {
|
|
|
|
case '\0':
|
|
|
|
continue; /* this'll break out of the loop */
|
|
|
|
case 't':
|
|
|
|
out = '\t';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
out = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
out = '\r';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
out = '\v';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*param++ = out;
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*param++ = *line++;
|
|
|
|
}
|
|
|
|
*param = '\0'; /* always zero terminate */
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads a line from the given file, ensuring is NUL terminated.
|
|
|
|
* The pointer must be freed by the caller.
|
|
|
|
* NULL is returned on an out of memory condition.
|
|
|
|
*/
|
|
|
|
static char *my_get_line(FILE *fp)
|
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
char *nl = NULL;
|
2013-03-06 07:27:51 -05:00
|
|
|
char *line = NULL;
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
do {
|
|
|
|
if(NULL == fgets(buf, sizeof(buf), fp))
|
|
|
|
break;
|
2013-03-06 07:27:51 -05:00
|
|
|
if(!line) {
|
|
|
|
line = strdup(buf);
|
|
|
|
if(!line)
|
2011-10-04 18:03:20 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *ptr;
|
2013-03-06 07:27:51 -05:00
|
|
|
size_t linelen = strlen(line);
|
|
|
|
ptr = realloc(line, linelen + strlen(buf) + 1);
|
2011-10-04 18:03:20 -04:00
|
|
|
if(!ptr) {
|
2013-03-06 07:27:51 -05:00
|
|
|
Curl_safefree(line);
|
2011-10-04 18:03:20 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-03-06 07:27:51 -05:00
|
|
|
line = ptr;
|
|
|
|
strcpy(&line[linelen], buf);
|
2011-10-04 18:03:20 -04:00
|
|
|
}
|
2013-03-06 07:27:51 -05:00
|
|
|
nl = strchr(line, '\n');
|
2011-10-04 18:03:20 -04:00
|
|
|
} while(!nl);
|
|
|
|
|
|
|
|
if(nl)
|
|
|
|
*nl = '\0';
|
|
|
|
|
2013-03-06 07:27:51 -05:00
|
|
|
return line;
|
2011-10-04 18:03:20 -04:00
|
|
|
}
|
|
|
|
|