#include "tool_setup.h"
#define ENABLE_CURLX_PRINTF
#include "curlx.h"
#include "tool_cfgable.h"
#include "tool_getparam.h"
#include "tool_helpers.h"
#include "tool_homedir.h"
#include "tool_msgs.h"
#include "tool_parsecfg.h"
#include "memdebug.h"
#define CURLRC DOT_CHAR "curlrc"
#define ISSEP(x,dash) (!dash && (((x) == '=') || ((x) == ':')))
static const char *unslashquote(const char *line, char *param);
static char *my_get_line(FILE *fp);
int parseconfig(const char *filename, struct GlobalConfig *global)
{
int res;
FILE *file;
char filebuffer[512];
bool usedarg;
char *home;
int rc = 0;
struct OperationConfig *operation = global->first;
if(!filename || !*filename) {
#ifndef __AMIGA__
filename = CURLRC;
home = homedir();
if(home) {
if(strlen(home) < (sizeof(filebuffer) - strlen(CURLRC))) {
snprintf(filebuffer, sizeof(filebuffer),
"%s%s%s", home, DIR_CHAR, CURLRC);
#ifdef WIN32
file = fopen(filebuffer, "r");
if(file != NULL) {
fclose(file);
filename = filebuffer;
}
else {
int n = GetModuleFileName(0, filebuffer, sizeof(filebuffer));
if(n > 0 && n < (int)sizeof(filebuffer)) {
char *lastdirchar = strrchr(filebuffer, '\\');
if(lastdirchar) {
size_t remaining;
*lastdirchar = 0;
remaining = sizeof(filebuffer) - strlen(filebuffer);
if(strlen(CURLRC) < remaining - 1) {
snprintf(lastdirchar, remaining,
"%s%s", DIR_CHAR, CURLRC);
filename = filebuffer;
}
}
}
}
#else
filename = filebuffer;
#endif
}
Curl_safefree(home);
}
# else
filename = "ENV:" CURLRC;
#endif
}
if(strcmp(filename,"-"))
file = fopen(filename, "r");
else
file = stdin;
if(file) {
char *line;
char *aline;
char *option;
char *param;
int lineno = 0;
bool alloced_param;
bool dashed_option;
while(NULL != (aline = my_get_line(file))) {
lineno++;
line = aline;
alloced_param=FALSE;
while(*line && ISSPACE(*line))
line++;
switch(*line) {
case '#':
case '/':
case '\r':
case '\n':
case '*':
case '\0':
Curl_safefree(aline);
continue;
}
option = line;
dashed_option = option[0]=='-'?TRUE:FALSE;
while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
line++;
if(*line)
*line++ = '\0';
#ifdef DEBUG_CONFIG
fprintf(stderr, "GOT: %s\n", option);
#endif
while(*line && (ISSPACE(*line) || ISSEP(*line, dashed_option)))
line++;
if(*line == '\"') {
line++;
param = malloc(strlen(line) + 1);
if(!param) {
Curl_safefree(aline);
rc = 1;
break;
}
alloced_param = TRUE;
(void)unslashquote(line, param);
}
else {
param = line;
while(*line && !ISSPACE(*line))
line++;
*line = '\0';
line++;
while(*line && ISSPACE(*line))
line++;
switch(*line) {
case '\0':
case '\r':
case '\n':
case '#':
break;
default:
warnf(operation, "%s:%d: warning: '%s' uses unquoted white space in"
" the line that may cause side-effects!\n",
filename, lineno, option);
}
}
if(param && !*param) {
if(alloced_param)
Curl_safefree(param);
param = NULL;
}
#ifdef DEBUG_CONFIG
fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
#endif
res = getparameter(option, param, &usedarg, global, operation);
if(param && *param && !usedarg)
res = PARAM_GOT_EXTRA_PARAMETER;
if(res == PARAM_NEXT_OPERATION) {
if(operation->url_list && operation->url_list->url) {
operation->next = malloc(sizeof(struct OperationConfig));
if(operation->next) {
config_init(operation->next);
operation->next->easy = global->easy;
operation->next->global = global;
global->last = operation->next;
operation->next->prev = operation;
operation = operation->next;
}
else
res = PARAM_NO_MEM;
}
}
if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
if(!strcmp(filename, "-")) {
filename = (char *)"<stdin>";
}
if(res != PARAM_HELP_REQUESTED &&
res != PARAM_MANUAL_REQUESTED &&
res != PARAM_VERSION_INFO_REQUESTED &&
res != PARAM_ENGINES_REQUESTED) {
const char *reason = param2text(res);
warnf(operation, "%s:%d: warning: '%s' %s\n",
filename, lineno, option, reason);
}
}
if(alloced_param)
Curl_safefree(param);
Curl_safefree(aline);
}
if(file != stdin)
fclose(file);
}
else
rc = 1;
return rc;
}
static const char *unslashquote(const char *line, char *param)
{
while(*line && (*line != '\"')) {
if(*line == '\\') {
char out;
line++;
switch(out = *line) {
case '\0':
continue;
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';
return line;
}
static char *my_get_line(FILE *fp)
{
char buf[4096];
char *nl = NULL;
char *line = NULL;
do {
if(NULL == fgets(buf, sizeof(buf), fp))
break;
if(!line) {
line = strdup(buf);
if(!line)
return NULL;
}
else {
char *ptr;
size_t linelen = strlen(line);
ptr = realloc(line, linelen + strlen(buf) + 1);
if(!ptr) {
Curl_safefree(line);
return NULL;
}
line = ptr;
strcpy(&line[linelen], buf);
}
nl = strchr(line, '\n');
} while(!nl);
if(nl)
*nl = '\0';
return line;
}