2011-10-05 06:03:20 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2021-11-19 23:11:29 +08:00
|
|
|
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-10-05 06:03:20 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-10-05 06:03:20 +08: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-07 05:35:15 +08:00
|
|
|
#include "tool_setup.h"
|
2011-10-05 06:03:20 +08: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"
|
2021-11-19 23:11:29 +08:00
|
|
|
#include "tool_findfile.h"
|
2011-10-05 06:03:20 +08:00
|
|
|
#include "tool_msgs.h"
|
|
|
|
#include "tool_parsecfg.h"
|
2020-09-09 21:41:25 +08:00
|
|
|
#include "dynbuf.h"
|
2011-10-05 06:03:20 +08:00
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-10-05 06:03:20 +08:00
|
|
|
|
2013-11-07 06:57:44 +08: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-05 06:03:20 +08:00
|
|
|
|
|
|
|
static const char *unslashquote(const char *line, char *param);
|
2020-09-09 21:41:25 +08:00
|
|
|
|
|
|
|
#define MAX_CONFIG_LINE_LENGTH (100*1024)
|
|
|
|
static bool my_get_line(FILE *fp, struct curlx_dynbuf *, bool *error);
|
2011-10-05 06:03:20 +08:00
|
|
|
|
2019-08-16 17:31:29 +08:00
|
|
|
#ifdef WIN32
|
2021-11-19 23:11:29 +08:00
|
|
|
static FILE *execpath(const char *filename, char **pathp)
|
2019-08-16 17:31:29 +08:00
|
|
|
{
|
2021-11-19 23:11:29 +08:00
|
|
|
static char filebuffer[512];
|
2019-08-16 17:31:29 +08:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
unsigned long len = GetModuleFileNameA(0, filebuffer, sizeof(filebuffer));
|
|
|
|
if(len > 0 && len < 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(filename) < remaining - 1) {
|
2021-11-19 23:11:29 +08:00
|
|
|
FILE *f;
|
2019-08-16 17:31:29 +08:00
|
|
|
msnprintf(lastdirchar, remaining, "%s%s", DIR_CHAR, filename);
|
2021-11-19 23:11:29 +08:00
|
|
|
*pathp = filebuffer;
|
|
|
|
f = fopen(filebuffer, FOPEN_READTEXT);
|
|
|
|
return f;
|
2019-08-16 17:31:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-10-05 06:03:20 +08:00
|
|
|
/* return 0 on everything-is-fine, and non-zero otherwise */
|
2014-02-24 00:04:39 +08:00
|
|
|
int parseconfig(const char *filename, struct GlobalConfig *global)
|
2011-10-05 06:03:20 +08:00
|
|
|
{
|
2019-05-23 23:13:39 +08:00
|
|
|
FILE *file = NULL;
|
2017-08-04 17:49:27 +08:00
|
|
|
bool usedarg = FALSE;
|
2011-10-05 06:03:20 +08:00
|
|
|
int rc = 0;
|
2020-03-19 07:28:19 +08:00
|
|
|
struct OperationConfig *operation = global->last;
|
2019-08-16 17:31:29 +08:00
|
|
|
char *pathalloc = NULL;
|
2021-11-19 23:11:29 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
#define DOTSCORE TRUE /* look for underscore-prefixed name too */
|
|
|
|
#else
|
|
|
|
#define DOTSCORE FALSE
|
|
|
|
#endif
|
2011-10-05 06:03:20 +08:00
|
|
|
|
2021-11-19 23:11:29 +08:00
|
|
|
if(!filename) {
|
|
|
|
/* NULL means load .curlrc from homedir! */
|
|
|
|
char *curlrc = findfile(".curlrc", DOTSCORE);
|
|
|
|
if(curlrc) {
|
|
|
|
file = fopen(curlrc, FOPEN_READTEXT);
|
|
|
|
if(!file) {
|
2021-12-06 15:45:03 +08:00
|
|
|
free(curlrc);
|
2021-11-19 23:11:29 +08:00
|
|
|
return 1;
|
2011-10-05 06:03:20 +08:00
|
|
|
}
|
2021-11-19 23:11:29 +08:00
|
|
|
filename = pathalloc = curlrc;
|
2011-10-05 06:03:20 +08:00
|
|
|
}
|
2021-11-19 23:11:29 +08:00
|
|
|
#ifdef WIN32 /* Windows */
|
|
|
|
else {
|
|
|
|
char *fullp;
|
2019-08-16 17:31:29 +08:00
|
|
|
/* check for .curlrc then _curlrc in the dir of the executable */
|
2021-11-19 23:11:29 +08:00
|
|
|
file = execpath(".curlrc", &fullp);
|
2019-08-16 17:31:29 +08:00
|
|
|
if(!file)
|
2021-11-19 23:11:29 +08:00
|
|
|
file = execpath("_curlrc", &fullp);
|
|
|
|
if(file)
|
|
|
|
/* this is the filename we read from */
|
|
|
|
filename = fullp;
|
2019-08-16 17:31:29 +08:00
|
|
|
}
|
2011-10-05 06:03:20 +08:00
|
|
|
#endif
|
|
|
|
}
|
2021-11-19 23:11:29 +08:00
|
|
|
else {
|
2019-06-02 18:57:19 +08:00
|
|
|
if(strcmp(filename, "-"))
|
|
|
|
file = fopen(filename, FOPEN_READTEXT);
|
|
|
|
else
|
|
|
|
file = stdin;
|
|
|
|
}
|
2011-10-05 06:03:20 +08:00
|
|
|
|
|
|
|
if(file) {
|
|
|
|
char *line;
|
|
|
|
char *option;
|
|
|
|
char *param;
|
|
|
|
int lineno = 0;
|
2013-11-07 06:57:44 +08:00
|
|
|
bool dashed_option;
|
2020-09-09 21:41:25 +08:00
|
|
|
struct curlx_dynbuf buf;
|
|
|
|
bool fileerror;
|
|
|
|
curlx_dyn_init(&buf, MAX_CONFIG_LINE_LENGTH);
|
2021-11-19 23:11:29 +08:00
|
|
|
DEBUGASSERT(filename);
|
2011-10-05 06:03:20 +08:00
|
|
|
|
2020-09-09 21:41:25 +08:00
|
|
|
while(my_get_line(file, &buf, &fileerror)) {
|
2018-06-03 04:52:56 +08:00
|
|
|
int res;
|
|
|
|
bool alloced_param = FALSE;
|
2011-10-05 06:03:20 +08:00
|
|
|
lineno++;
|
2020-09-09 21:41:25 +08:00
|
|
|
line = curlx_dyn_ptr(&buf);
|
|
|
|
if(!line) {
|
|
|
|
rc = 1; /* out of memory */
|
|
|
|
break;
|
|
|
|
}
|
2011-10-05 06:03:20 +08:00
|
|
|
|
|
|
|
/* 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':
|
2020-09-09 21:41:25 +08:00
|
|
|
curlx_dyn_reset(&buf);
|
2011-10-05 06:03:20 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the option keywords starts here */
|
|
|
|
option = line;
|
2013-11-07 06:57:44 +08:00
|
|
|
|
|
|
|
/* the option starts with a dash? */
|
|
|
|
dashed_option = option[0]=='-'?TRUE:FALSE;
|
|
|
|
|
|
|
|
while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
|
2011-10-05 06:03:20 +08:00
|
|
|
line++;
|
|
|
|
/* ... and has ended here */
|
|
|
|
|
|
|
|
if(*line)
|
2020-06-25 17:38:25 +08:00
|
|
|
*line++ = '\0'; /* null-terminate, we have a local copy of the data */
|
2011-10-05 06:03:20 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_CONFIG
|
|
|
|
fprintf(stderr, "GOT: %s\n", option);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* pass spaces and separator(s) */
|
2013-11-07 06:57:44 +08:00
|
|
|
while(*line && (ISSPACE(*line) || ISSEP(*line, dashed_option)))
|
2011-10-05 06:03:20 +08: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 */
|
|
|
|
rc = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
alloced_param = TRUE;
|
|
|
|
(void)unslashquote(line, param);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
param = line; /* parameter starts here */
|
|
|
|
while(*line && !ISSPACE(*line))
|
|
|
|
line++;
|
2013-12-09 15:19:04 +08:00
|
|
|
|
2015-04-17 06:38:50 +08:00
|
|
|
if(*line) {
|
2020-06-25 17:38:25 +08:00
|
|
|
*line = '\0'; /* null-terminate */
|
2013-12-09 15:19:04 +08:00
|
|
|
|
2015-04-17 06:38:50 +08: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 "
|
2020-10-01 03:10:14 +08:00
|
|
|
"whitespace in the line that may cause side-effects!\n",
|
2015-04-17 06:38:50 +08:00
|
|
|
filename, lineno, option);
|
|
|
|
}
|
2013-12-09 15:19:04 +08:00
|
|
|
}
|
2016-12-02 18:25:35 +08:00
|
|
|
if(!*param)
|
|
|
|
/* do this so getparameter can check for required parameters.
|
|
|
|
Otherwise it always thinks there's a parameter. */
|
|
|
|
param = NULL;
|
2011-10-05 06:03:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_CONFIG
|
|
|
|
fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
|
|
|
|
#endif
|
2014-02-24 00:04:39 +08:00
|
|
|
res = getparameter(option, param, &usedarg, global, operation);
|
2020-03-19 07:28:19 +08:00
|
|
|
operation = global->last;
|
2011-10-05 06:03:20 +08:00
|
|
|
|
2017-08-04 17:49:27 +08:00
|
|
|
if(!res && param && *param && !usedarg)
|
2011-10-05 06:03:20 +08:00
|
|
|
/* we passed in a parameter that wasn't used! */
|
|
|
|
res = PARAM_GOT_EXTRA_PARAMETER;
|
|
|
|
|
2014-02-24 02:01:36 +08: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);
|
|
|
|
|
2014-02-27 05:02:53 +08:00
|
|
|
/* Set the global config pointer */
|
|
|
|
operation->next->global = global;
|
|
|
|
|
2014-02-24 02:01:36 +08: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-23 00:50:47 +08:00
|
|
|
|
|
|
|
if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
|
2011-10-05 06:03:20 +08:00
|
|
|
/* the help request isn't really an error */
|
|
|
|
if(!strcmp(filename, "-")) {
|
2017-05-08 02:27:12 +08:00
|
|
|
filename = "<stdin>";
|
2011-10-05 06:03:20 +08:00
|
|
|
}
|
2014-02-23 04:58:43 +08:00
|
|
|
if(res != PARAM_HELP_REQUESTED &&
|
2014-02-23 18:37:44 +08:00
|
|
|
res != PARAM_MANUAL_REQUESTED &&
|
2014-02-23 18:16:36 +08:00
|
|
|
res != PARAM_VERSION_INFO_REQUESTED &&
|
2014-02-23 04:58:43 +08:00
|
|
|
res != PARAM_ENGINES_REQUESTED) {
|
2011-10-05 06:03:20 +08:00
|
|
|
const char *reason = param2text(res);
|
2015-02-28 04:48:38 +08:00
|
|
|
warnf(operation->global, "%s:%d: warning: '%s' %s\n",
|
2011-10-05 06:03:20 +08:00
|
|
|
filename, lineno, option, reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(alloced_param)
|
|
|
|
Curl_safefree(param);
|
|
|
|
|
2020-09-09 21:41:25 +08:00
|
|
|
curlx_dyn_reset(&buf);
|
2011-10-05 06:03:20 +08:00
|
|
|
}
|
2020-09-09 21:41:25 +08:00
|
|
|
curlx_dyn_free(&buf);
|
2011-10-05 06:03:20 +08:00
|
|
|
if(file != stdin)
|
|
|
|
fclose(file);
|
2020-09-09 21:41:25 +08:00
|
|
|
if(fileerror)
|
|
|
|
rc = 1;
|
2011-10-05 06:03:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
rc = 1; /* couldn't open the file */
|
|
|
|
|
2021-12-06 15:45:03 +08:00
|
|
|
free(pathalloc);
|
2011-10-05 06:03:20 +08:00
|
|
|
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++;
|
|
|
|
}
|
2020-06-25 17:38:25 +08:00
|
|
|
*param = '\0'; /* always null-terminate */
|
2011-10-05 06:03:20 +08:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads a line from the given file, ensuring is NUL terminated.
|
|
|
|
*/
|
2020-09-09 21:41:25 +08:00
|
|
|
static bool my_get_line(FILE *fp, struct curlx_dynbuf *db,
|
|
|
|
bool *error)
|
2011-10-05 06:03:20 +08:00
|
|
|
{
|
|
|
|
char buf[4096];
|
2020-09-09 21:41:25 +08:00
|
|
|
*error = FALSE;
|
2011-10-05 06:03:20 +08:00
|
|
|
do {
|
2020-09-09 21:41:25 +08:00
|
|
|
/* fgets() returns s on success, and NULL on error or when end of file
|
|
|
|
occurs while no characters have been read. */
|
|
|
|
if(!fgets(buf, sizeof(buf), fp))
|
|
|
|
/* only if there's data in the line, return TRUE */
|
|
|
|
return curlx_dyn_len(db) ? TRUE : FALSE;
|
|
|
|
if(curlx_dyn_add(db, buf)) {
|
|
|
|
*error = TRUE; /* error */
|
|
|
|
return FALSE; /* stop reading */
|
2011-10-05 06:03:20 +08:00
|
|
|
}
|
2020-09-09 21:41:25 +08:00
|
|
|
} while(!strchr(buf, '\n'));
|
2011-10-05 06:03:20 +08:00
|
|
|
|
2020-09-09 21:41:25 +08:00
|
|
|
return TRUE; /* continue */
|
2011-10-05 06:03:20 +08:00
|
|
|
}
|