1
0
mirror of https://github.com/moparisthebest/curl synced 2024-08-13 17:03:50 -04:00

major config file hack, now works a lot better and slightly different

Added --url to allow URLs to be specified in the config file that way
This commit is contained in:
Daniel Stenberg 2000-11-17 09:47:18 +00:00
parent fdd91b2209
commit 34a2d446e0

View File

@ -213,10 +213,10 @@ int SetHTTPrequest(HttpReq req, HttpReq *store)
if((*store == HTTPREQ_UNSPEC) || if((*store == HTTPREQ_UNSPEC) ||
(*store == req)) { (*store == req)) {
*store = req; *store = req;
return CURLE_OK; return 0;
} }
fprintf(stderr, "You can only select one HTTP request!\n"); fprintf(stderr, "You can only select one HTTP request!\n");
return CURLE_FAILED_INIT; return 1;
} }
static void helpf(char *fmt, ...) static void helpf(char *fmt, ...)
@ -433,11 +433,23 @@ static char *file2memory(FILE *file, long *size)
return NULL; /* no string */ return NULL; /* no string */
} }
static int getparameter(char *flag, /* f or -long-flag */ typedef enum {
char *nextarg, /* NULL if unset */ PARAM_OK,
bool *usedarg, /* set to TRUE if the arg has been PARAM_OPTION_AMBIGUOUS,
used */ PARAM_OPTION_UNKNOWN,
struct Configurable *config) PARAM_REQUIRES_PARAMETER,
PARAM_BAD_USE,
PARAM_HELP_REQUESTED,
PARAM_GOT_EXTRA_PARAMETER,
PARAM_LAST
} ParameterError;
static ParameterError getparameter(char *flag, /* f or -long-flag */
char *nextarg, /* NULL if unset */
bool *usedarg, /* set to TRUE if the arg
has been used */
struct Configurable *config)
{ {
char letter; char letter;
char subletter=0; /* subletters can only occur on long options */ char subletter=0; /* subletters can only occur on long options */
@ -458,6 +470,7 @@ static int getparameter(char *flag, /* f or -long-flag */
{"8", "stderr", TRUE}, {"8", "stderr", TRUE},
{"7", "interface", TRUE}, {"7", "interface", TRUE},
{"6", "krb4", TRUE}, {"6", "krb4", TRUE},
{"5", "url", TRUE},
{"2", "sslv2", FALSE}, {"2", "sslv2", FALSE},
{"3", "sslv3", FALSE}, {"3", "sslv3", FALSE},
@ -513,15 +526,17 @@ static int getparameter(char *flag, /* f or -long-flag */
{"#", "progress-bar",FALSE}, {"#", "progress-bar",FALSE},
}; };
if('-' == flag[0]) { if(('-' != flag[0]) ||
/* try a long name */ (('-' == flag[0]) && ('-' == flag[1]))) {
int fnam=strlen(&flag[1]); /* this should be a long name */
char *word=('-' == flag[0])?flag+2:flag;
int fnam=strlen(word);
int numhits=0; int numhits=0;
for(j=0; j< sizeof(aliases)/sizeof(aliases[0]); j++) { for(j=0; j< sizeof(aliases)/sizeof(aliases[0]); j++) {
if(strnequal(aliases[j].lname, &flag[1], fnam)) { if(strnequal(aliases[j].lname, word, fnam)) {
longopt = TRUE; longopt = TRUE;
numhits++; numhits++;
if(strequal(aliases[j].lname, &flag[1])) { if(strequal(aliases[j].lname, word)) {
parse = aliases[j].letter; parse = aliases[j].letter;
hit = j; hit = j;
numhits = 1; /* a single unique hit */ numhits = 1; /* a single unique hit */
@ -533,15 +548,14 @@ static int getparameter(char *flag, /* f or -long-flag */
} }
if(numhits>1) { if(numhits>1) {
/* this is at least the second match! */ /* this is at least the second match! */
helpf("option --%s is ambiguous\n", &flag[1]); return PARAM_OPTION_AMBIGUOUS;
return CURLE_FAILED_INIT;
} }
if(hit < 0) { if(hit < 0) {
helpf("unknown option -%s.\n", flag); return PARAM_OPTION_UNKNOWN;
return CURLE_FAILED_INIT;
} }
} }
else { else {
flag++; /* prefixed with one dash, pass it */
hit=-1; hit=-1;
parse = flag; parse = flag;
} }
@ -568,19 +582,14 @@ static int getparameter(char *flag, /* f or -long-flag */
} }
} }
if(hit < 0) { if(hit < 0) {
helpf("unknown option -%c.\n", letter); return PARAM_OPTION_UNKNOWN;
return CURLE_FAILED_INIT;
} }
} }
if(hit < 0) { if(hit < 0) {
helpf("unknown option -%c.\n", letter); return PARAM_OPTION_UNKNOWN;
return CURLE_FAILED_INIT;
} }
if(!nextarg && aliases[hit].extraparam) { if((!nextarg || !*nextarg) && aliases[hit].extraparam) {
helpf("option -%s/--%s requires an extra argument!\n", return PARAM_REQUIRES_PARAMETER;
aliases[hit].letter,
aliases[hit].lname);
return CURLE_FAILED_INIT;
} }
else if(nextarg && aliases[hit].extraparam) else if(nextarg && aliases[hit].extraparam)
*usedarg = TRUE; /* mark it as used */ *usedarg = TRUE; /* mark it as used */
@ -638,6 +647,10 @@ static int getparameter(char *flag, /* f or -long-flag */
/* krb4 level string */ /* krb4 level string */
GetStr(&config->krb4level, nextarg); GetStr(&config->krb4level, nextarg);
break; break;
case '5':
/* the URL! */
GetStr(&config->url, nextarg);
break;
case '#': /* added 19990617 larsa */ case '#': /* added 19990617 larsa */
config->progressmode ^= CURL_PROGRESS_BAR; config->progressmode ^= CURL_PROGRESS_BAR;
break; break;
@ -727,7 +740,7 @@ static int getparameter(char *flag, /* f or -long-flag */
if(config->postfields) if(config->postfields)
config->conf |= CONF_POST; config->conf |= CONF_POST;
if(SetHTTPrequest(HTTPREQ_SIMPLEPOST, &config->httpreq)) if(SetHTTPrequest(HTTPREQ_SIMPLEPOST, &config->httpreq))
return CURLE_FAILED_INIT; return PARAM_BAD_USE;
break; break;
case 'D': case 'D':
/* dump-header to given file name */ /* dump-header to given file name */
@ -767,14 +780,14 @@ static int getparameter(char *flag, /* f or -long-flag */
if(curl_formparse(nextarg, if(curl_formparse(nextarg,
&config->httppost, &config->httppost,
&config->last_post)) &config->last_post))
return CURLE_FAILED_INIT; return PARAM_BAD_USE;
if(SetHTTPrequest(HTTPREQ_POST, &config->httpreq)) if(SetHTTPrequest(HTTPREQ_POST, &config->httpreq))
return CURLE_FAILED_INIT; return PARAM_BAD_USE;
break; break;
case 'h': /* h for help */ case 'h': /* h for help */
help(); help();
return CURLE_FAILED_INIT; return PARAM_HELP_REQUESTED;
case 'H': case 'H':
/* A custom header to append to a list */ /* A custom header to append to a list */
config->headers = curl_slist_append(config->headers, nextarg); config->headers = curl_slist_append(config->headers, nextarg);
@ -786,7 +799,7 @@ static int getparameter(char *flag, /* f or -long-flag */
config->conf ^= CONF_HEADER; /* include the HTTP header in the output */ config->conf ^= CONF_HEADER; /* include the HTTP header in the output */
config->conf ^= CONF_NOBODY; /* don't fetch the body at all */ config->conf ^= CONF_NOBODY; /* don't fetch the body at all */
if(SetHTTPrequest(HTTPREQ_HEAD, &config->httpreq)) if(SetHTTPrequest(HTTPREQ_HEAD, &config->httpreq))
return CURLE_FAILED_INIT; return PARAM_BAD_USE;
break; break;
case 'K': case 'K':
res = parseconfig(nextarg, config); res = parseconfig(nextarg, config);
@ -806,7 +819,7 @@ static int getparameter(char *flag, /* f or -long-flag */
break; break;
case 'M': /* M for manual, huge help */ case 'M': /* M for manual, huge help */
hugehelp(); hugehelp();
return CURLE_FAILED_INIT; return PARAM_HELP_REQUESTED;
case 'n': case 'n':
/* pick info from .netrc, if this is used for http, curl will /* pick info from .netrc, if this is used for http, curl will
automatically enfore user+password with the request */ automatically enfore user+password with the request */
@ -889,7 +902,7 @@ static int getparameter(char *flag, /* f or -long-flag */
break; break;
case 'V': case 'V':
printf(CURL_ID "%s\n", curl_version()); printf(CURL_ID "%s\n", curl_version());
return CURLE_FAILED_INIT; return PARAM_HELP_REQUESTED;
case 'w': case 'w':
/* get the output string */ /* get the output string */
if('@' == *nextarg) { if('@' == *nextarg) {
@ -916,7 +929,7 @@ static int getparameter(char *flag, /* f or -long-flag */
/* HTTP request */ /* HTTP request */
GetStr(&config->customrequest, nextarg); GetStr(&config->customrequest, nextarg);
if(SetHTTPrequest(HTTPREQ_CUSTOM, &config->httpreq)) if(SetHTTPrequest(HTTPREQ_CUSTOM, &config->httpreq))
return CURLE_FAILED_INIT; return PARAM_BAD_USE;
break; break;
case 'y': case 'y':
/* low speed time */ /* low speed time */
@ -932,17 +945,13 @@ static int getparameter(char *flag, /* f or -long-flag */
break; break;
default: /* unknown flag */ default: /* unknown flag */
if(letter) return PARAM_OPTION_UNKNOWN;
helpf("Unknown option '%c'\n", letter);
else
helpf("Unknown option\n"); /* short help blurb */
return CURLE_FAILED_INIT;
} }
hit = -1; hit = -1;
} while(*++parse && !*usedarg); } while(*++parse && !*usedarg);
return CURLE_OK; return PARAM_OK;
} }
@ -964,10 +973,10 @@ static int parseconfig(char *filename,
home = curl_getenv("HOME"); /* portable environment reader */ home = curl_getenv("HOME"); /* portable environment reader */
if(!home) if(!home)
return CURLE_OK; return 0;
if(strlen(home)>(sizeof(filebuffer)-strlen(CURLRC))) { if(strlen(home)>(sizeof(filebuffer)-strlen(CURLRC))) {
free(home); free(home);
return CURLE_OK; return 0;
} }
sprintf(filebuffer, "%s%s%s", home, DIR_CHAR, CURLRC); sprintf(filebuffer, "%s%s%s", home, DIR_CHAR, CURLRC);
@ -982,66 +991,127 @@ static int parseconfig(char *filename,
if(file) { if(file) {
char *line; char *line;
char *tok1; char *aline;
char *tok2; char *option;
char *param;
while (NULL != (line = my_get_line(file))) { int lineno=0;
bool alloced_param;
#define isseparator(x) (((x)=='=') || ((x) == ':'))
while (NULL != (aline = my_get_line(file))) {
lineno++;
line = aline;
alloced_param=FALSE;
/* lines with # in the fist column is a comment! */ /* lines with # in the fist column is a comment! */
if ('#' == line[0]) { while(isspace(*line))
line++;
switch(*line) {
case '#':
case '/':
case '\r':
case '\n':
case '*':
case '\0':
free(line); free(line);
continue; continue;
} }
if (NULL == (tok1 = my_get_token(line))) { /* the option keywords starts here */
free(line); option = line;
continue; while(*line && !isspace(*line) && !isseparator(*line))
} line++;
if ('-' != tok1[0]) { /* ... and has ended here */
if (config->url)
free(config->url);
config->url = tok1;
}
while ((NULL != tok1) && ('-' == tok1[0])) { *line++=0; /* zero terminate, we have a local copy of the data */
tok2 = my_get_token(NULL);
while (NULL == tok2) { #ifdef DEBUG_CONFIG
free(line); fprintf(stderr, "GOT: %s\n", option);
if (NULL == (line = my_get_line(file))) #endif
break;
if ('#' == line[0]) /* pass spaces and separator(s) */
continue; while(isspace(*line) || isseparator(*line))
tok2 = my_get_token(line); line++;
/* the parameter starts here (unless quoted) */
if(*line == '\"') {
char *ptr;
/* quoted parameter, do the qoute dance */
line++;
param=strdup(line); /* parameter */
alloced_param=TRUE;
ptr=param;
while(*line && (*line != '\"')) {
if(*line == '\\') {
line++;
if(!*line) {
break;
}
}
*ptr++=*line++;
} }
*ptr=0; /* always zero terminate */
res = getparameter(tok1 + 1, tok2, &usedarg, config); }
free(tok1); else {
if (!usedarg) { param=line; /* parameter starts here */
if (tok2 && ('-' != tok2[0])) { while(*line && !isspace(*line))
/* this is not an option, this is a URL */ line++;
if (config->url) *line=0; /* zero terminate */
free(config->url); }
config->url = tok2; #ifdef DEBUG_CONFIG
fprintf(stderr, "PARAM: \"%s\"\n", param);
#endif
res = getparameter(option, param, &usedarg, config);
if(*param && !usedarg)
/* we passed in a parameter that wasn't used! */
res = PARAM_GOT_EXTRA_PARAMETER;
if(res != PARAM_OK) {
/* the help request isn't really an error */
if(!strcmp(filename, "-")) {
filename="<stdin>";
}
if(PARAM_HELP_REQUESTED != res) {
char *reason;
switch(res) {
default:
case PARAM_GOT_EXTRA_PARAMETER:
reason = "had unsupported trailing garbage";
break;
case PARAM_OPTION_UNKNOWN:
reason = "is unknown";
break;
case PARAM_OPTION_AMBIGUOUS:
reason = "is ambiguous";
break;
case PARAM_REQUIRES_PARAMETER:
reason = "requires parameter";
break;
case PARAM_BAD_USE:
reason = "is badly used here";
break; break;
} }
else fprintf(stderr, "%s:%d: warning: '%s' %s\n",
tok1 = tok2; filename, lineno, option, reason);
} }
else {
free(tok2);
break;
}
if(res)
break; /* error detected */
} }
free(line); if(alloced_param)
free(param);
free(aline);
} }
if(file != stdin) if(file != stdin)
fclose(file); fclose(file);
} }
if(home) if(home)
free(home); free(home);
return CURLE_OK; return 0;
} }
struct OutStruct { struct OutStruct {
@ -1236,8 +1306,9 @@ int main(int argc, char *argv[])
('-' == argv[i][0])) { ('-' == argv[i][0])) {
char *nextarg; char *nextarg;
bool passarg; bool passarg;
char *origopt=argv[i];
char *flag = &argv[i][1]; char *flag = argv[i];
if(strequal("--", argv[i])) if(strequal("--", argv[i]))
/* this indicates the end of the flags and thus enables the /* this indicates the end of the flags and thus enables the
@ -1246,12 +1317,27 @@ int main(int argc, char *argv[])
else { else {
nextarg= (i < argc - 1)? argv[i+1]: NULL; nextarg= (i < argc - 1)? argv[i+1]: NULL;
res = getparameter ( flag, res = getparameter(flag, nextarg, &passarg, &config);
nextarg, if(res) {
&passarg, switch(res) {
&config ); case PARAM_OPTION_AMBIGUOUS:
if(res) helpf("option %s is ambiguous\n", origopt);
return res; break;
case PARAM_OPTION_UNKNOWN:
helpf("option %s is unknown\n", origopt);
break;
case PARAM_REQUIRES_PARAMETER:
helpf("option %s requires an extra argument!\n", origopt);
break;
case PARAM_BAD_USE:
helpf("option %s was wrongly used!\n", origopt);
break;
case PARAM_HELP_REQUESTED:
/* no text */
break;
}
return CURLE_FAILED_INIT;
}
if(passarg) /* we're supposed to skip this */ if(passarg) /* we're supposed to skip this */
i++; i++;
@ -1452,49 +1538,6 @@ int main(int argc, char *argv[])
main_init(); main_init();
#if 0
/* This is code left from the pre-v7 time, left here mainly as a reminder
and possibly as a warning! ;-) */
res = curl_urlget(CURLOPT_FILE, (FILE *)&outs, /* where to store */
CURLOPT_WRITEFUNCTION, my_fwrite, /* what call to write */
CURLOPT_INFILE, infd, /* for uploads */
CURLOPT_INFILESIZE, infilesize, /* size of uploaded file */
CURLOPT_URL, url, /* what to fetch */
CURLOPT_PROXY, config.proxy, /* proxy to use */
CURLOPT_FLAGS, config.conf, /* flags */
CURLOPT_USERPWD, config.userpwd, /* user + passwd */
CURLOPT_PROXYUSERPWD, config.proxyuserpwd, /* Proxy user + passwd */
CURLOPT_RANGE, config.range, /* range of document */
CURLOPT_ERRORBUFFER, errorbuffer,
CURLOPT_TIMEOUT, config.timeout,
CURLOPT_POSTFIELDS, config.postfields,
CURLOPT_REFERER, config.referer,
CURLOPT_USERAGENT, config.useragent,
CURLOPT_FTPPORT, config.ftpport,
CURLOPT_LOW_SPEED_LIMIT, config.low_speed_limit,
CURLOPT_LOW_SPEED_TIME, config.low_speed_time,
CURLOPT_RESUME_FROM, config.use_resume?config.resume_from:0,
CURLOPT_COOKIE, config.cookie,
CURLOPT_HTTPHEADER, config.headers,
CURLOPT_HTTPPOST, config.httppost,
CURLOPT_SSLCERT, config.cert,
CURLOPT_SSLCERTPASSWD, config.cert_passwd,
CURLOPT_CRLF, config.crlf,
CURLOPT_QUOTE, config.quote,
CURLOPT_POSTQUOTE, config.postquote,
CURLOPT_WRITEHEADER, config.headerfile?&heads:NULL,
CURLOPT_COOKIEFILE, config.cookiefile,
CURLOPT_SSLVERSION, config.ssl_version,
CURLOPT_TIMECONDITION, config.timecond,
CURLOPT_TIMEVALUE, config.condtime,
CURLOPT_CUSTOMREQUEST, config.customrequest,
CURLOPT_STDERR, config.errors,
CURLOPT_PROGRESSMODE, config.progressmode,
CURLOPT_WRITEINFO, config.writeout,
CURLOPT_DONE); /* always terminate the list of tags */
#endif
/* The new, v7-style easy-interface! */ /* The new, v7-style easy-interface! */
curl = curl_easy_init(); curl = curl_easy_init();
if(curl) { if(curl) {