tool_main: Moved initial OperateConfig creation into main_init()
[platform/upstream/curl.git] / src / tool_getparam.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 #include "tool_setup.h"
23
24 #include "rawstr.h"
25
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
28 #include "curlx.h"
29
30 #include "tool_binmode.h"
31 #include "tool_cfgable.h"
32 #include "tool_cb_prg.h"
33 #include "tool_formparse.h"
34 #include "tool_getparam.h"
35 #include "tool_helpers.h"
36 #include "tool_libinfo.h"
37 #include "tool_metalink.h"
38 #include "tool_msgs.h"
39 #include "tool_paramhlp.h"
40 #include "tool_parsecfg.h"
41
42 #include "memdebug.h" /* keep this as LAST include */
43
44 #ifdef MSDOS
45 #  define USE_WATT32
46 #endif
47
48 #define GetStr(str,val) do { \
49   if(*(str)) { \
50     free(*(str)); \
51     *(str) = NULL; \
52   } \
53   if((val)) {              \
54     *(str) = strdup((val)); \
55     if(!(*(str)))          \
56       return PARAM_NO_MEM; \
57   } \
58 } WHILE_FALSE
59
60 struct LongShort {
61   const char *letter; /* short name option */
62   const char *lname;  /* long name option */
63   bool extraparam;    /* whether it takes an additional argument */
64 };
65
66 static const struct LongShort aliases[]= {
67   /* all these ones, starting with "*" or "$" as a short-option have *no*
68      short option to mention. */
69   {"*",  "url",                      TRUE},
70   {"*4", "dns-ipv4-addr",            TRUE},
71   {"*6", "dns-ipv6-addr",            TRUE},
72   {"*a", "random-file",              TRUE},
73   {"*b", "egd-file",                 TRUE},
74   {"*B", "oauth2-bearer",             TRUE},
75   {"*c", "connect-timeout",          TRUE},
76   {"*d", "ciphers",                  TRUE},
77   {"*D", "dns-interface",            TRUE},
78   {"*e", "disable-epsv",             FALSE},
79   {"*E", "epsv",                     FALSE},
80          /* 'epsv' made like this to make --no-epsv and --epsv to work
81              although --disable-epsv is the documented option */
82 #ifdef USE_ENVIRONMENT
83   {"*f", "environment",              FALSE},
84 #endif
85   {"*F", "dns-servers",              TRUE},
86   {"*g", "trace",                    TRUE},
87   {"*G", "npn",                      FALSE},
88   {"*h", "trace-ascii",              TRUE},
89   {"*H", "alpn",                     FALSE},
90   {"*i", "limit-rate",               TRUE},
91   {"*j", "compressed",               FALSE},
92   {"*J", "tr-encoding",              FALSE},
93   {"*k", "digest",                   FALSE},
94   {"*l", "negotiate",                FALSE},
95   {"*m", "ntlm",                     FALSE},
96   {"*M", "ntlm-wb",                  FALSE},
97   {"*n", "basic",                    FALSE},
98   {"*o", "anyauth",                  FALSE},
99 #ifdef USE_WATT32
100   {"*p", "wdebug",                   FALSE},
101 #endif
102   {"*q", "ftp-create-dirs",          FALSE},
103   {"*r", "create-dirs",              FALSE},
104   {"*s", "max-redirs",               TRUE},
105   {"*t", "proxy-ntlm",               FALSE},
106   {"*u", "crlf",                     FALSE},
107   {"*v", "stderr",                   TRUE},
108   {"*w", "interface",                TRUE},
109   {"*x", "krb" ,                     TRUE},
110   {"*x", "krb4" ,                    TRUE},
111          /* 'krb4' is the previous name */
112   {"*y", "max-filesize",             TRUE},
113   {"*z", "disable-eprt",             FALSE},
114   {"*Z", "eprt",                     FALSE},
115          /* 'eprt' made like this to make --no-eprt and --eprt to work
116              although --disable-eprt is the documented option */
117   {"$a", "ftp-ssl",                  FALSE},
118          /* 'ftp-ssl' deprecated name since 7.20.0 */
119   {"$a", "ssl",                      FALSE},
120          /* 'ssl' new option name in 7.20.0, previously this was ftp-ssl */
121   {"$b", "ftp-pasv",                 FALSE},
122   {"$c", "socks5",                   TRUE},
123   {"$c", "socks",                    TRUE},
124          /* 'socks' is how the option once was documented but we prefer
125             the --socks5 version for explicit version */
126   {"$d", "tcp-nodelay",              FALSE},
127   {"$e", "proxy-digest",             FALSE},
128   {"$f", "proxy-basic",              FALSE},
129   {"$g", "retry",                    TRUE},
130   {"$h", "retry-delay",              TRUE},
131   {"$i", "retry-max-time",           TRUE},
132   {"$k", "proxy-negotiate",          FALSE},
133   {"$m", "ftp-account",              TRUE},
134   {"$n", "proxy-anyauth",            FALSE},
135   {"$o", "trace-time",               FALSE},
136   {"$p", "ignore-content-length",    FALSE},
137   {"$q", "ftp-skip-pasv-ip",         FALSE},
138   {"$r", "ftp-method",               TRUE},
139   {"$s", "local-port",               TRUE},
140   {"$t", "socks4",                   TRUE},
141   {"$T", "socks4a",                  TRUE},
142   {"$u", "ftp-alternative-to-user",  TRUE},
143   {"$v", "ftp-ssl-reqd",             FALSE},
144          /* 'ftp-ssl-reqd' deprecated name since 7.20.0 */
145   {"$v", "ssl-reqd",                 FALSE},
146          /* 'ssl-reqd' new in 7.20.0, previously this was ftp-ssl-reqd */
147   {"$w", "sessionid",                FALSE},
148          /* ¡sessionid' listed as --no-sessionid in the help */
149   {"$x", "ftp-ssl-control",          FALSE},
150   {"$y", "ftp-ssl-ccc",              FALSE},
151   {"$j", "ftp-ssl-ccc-mode",         TRUE},
152   {"$z", "libcurl",                  TRUE},
153   {"$#", "raw",                      FALSE},
154   {"$0", "post301",                  FALSE},
155   {"$1", "keepalive",                FALSE},
156          /* 'keepalive' listed as --no-keepalive in the help */
157   {"$2", "socks5-hostname",          TRUE},
158   {"$3", "keepalive-time",           TRUE},
159   {"$4", "post302",                  FALSE},
160   {"$5", "noproxy",                  TRUE},
161 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
162   {"$6", "socks5-gssapi-service",    TRUE},
163   {"$7", "socks5-gssapi-nec",        FALSE},
164 #endif
165   {"$8", "proxy1.0",                 TRUE},
166   {"$9", "tftp-blksize",             TRUE},
167   {"$A", "mail-from",                TRUE},
168   {"$B", "mail-rcpt",                TRUE},
169   {"$C", "ftp-pret",                 FALSE},
170   {"$D", "proto",                    TRUE},
171   {"$E", "proto-redir",              TRUE},
172   {"$F", "resolve",                  TRUE},
173   {"$G", "delegation",               TRUE},
174   {"$H", "mail-auth",                TRUE},
175   {"$I", "post303",                  FALSE},
176   {"$J", "metalink",                 FALSE},
177   {"$K", "sasl-ir",                  FALSE},
178   {"$L", "test-event",               FALSE},
179   {"0",   "http1.0",                 FALSE},
180   {"01",  "http1.1",                 FALSE},
181   {"02",  "http2",                   FALSE},
182   {"1",  "tlsv1",                    FALSE},
183   {"10",  "tlsv1.0",                 FALSE},
184   {"11",  "tlsv1.1",                 FALSE},
185   {"12",  "tlsv1.2",                 FALSE},
186   {"2",  "sslv2",                    FALSE},
187   {"3",  "sslv3",                    FALSE},
188   {"4",  "ipv4",                     FALSE},
189   {"6",  "ipv6",                     FALSE},
190   {"a",  "append",                   FALSE},
191   {"A",  "user-agent",               TRUE},
192   {"b",  "cookie",                   TRUE},
193   {"B",  "use-ascii",                FALSE},
194   {"c",  "cookie-jar",               TRUE},
195   {"C",  "continue-at",              TRUE},
196   {"d",  "data",                     TRUE},
197   {"da", "data-ascii",               TRUE},
198   {"db", "data-binary",              TRUE},
199   {"de", "data-urlencode",           TRUE},
200   {"D",  "dump-header",              TRUE},
201   {"e",  "referer",                  TRUE},
202   {"E",  "cert",                     TRUE},
203   {"Ea", "cacert",                   TRUE},
204   {"Eb", "cert-type",                TRUE},
205   {"Ec", "key",                      TRUE},
206   {"Ed", "key-type",                 TRUE},
207   {"Ee", "pass",                     TRUE},
208   {"Ef", "engine",                   TRUE},
209   {"Eg", "capath ",                  TRUE},
210   {"Eh", "pubkey",                   TRUE},
211   {"Ei", "hostpubmd5",               TRUE},
212   {"Ej", "crlfile",                  TRUE},
213   {"Ek", "tlsuser",                  TRUE},
214   {"El", "tlspassword",              TRUE},
215   {"Em", "tlsauthtype",              TRUE},
216   {"En", "ssl-allow-beast",          FALSE},
217   {"Eo", "login-options",            TRUE},
218   {"f",  "fail",                     FALSE},
219   {"F",  "form",                     TRUE},
220   {"Fs", "form-string",              TRUE},
221   {"g",  "globoff",                  FALSE},
222   {"G",  "get",                      FALSE},
223   {"h",  "help",                     FALSE},
224   {"H",  "header",                   TRUE},
225   {"i",  "include",                  FALSE},
226   {"I",  "head",                     FALSE},
227   {"j",  "junk-session-cookies",     FALSE},
228   {"J",  "remote-header-name",       FALSE},
229   {"k",  "insecure",                 FALSE},
230   {"K",  "config",                   TRUE},
231   {"l",  "list-only",                FALSE},
232   {"L",  "location",                 FALSE},
233   {"Lt", "location-trusted",         FALSE},
234   {"m",  "max-time",                 TRUE},
235   {"M",  "manual",                   FALSE},
236   {"n",  "netrc",                    FALSE},
237   {"no", "netrc-optional",           FALSE},
238   {"ne", "netrc-file",               TRUE},
239   {"N",  "buffer",                   FALSE},
240          /* 'buffer' listed as --no-buffer in the help */
241   {"o",  "output",                   TRUE},
242   {"O",  "remote-name",              FALSE},
243   {"Oa", "remote-name-all",          FALSE},
244   {"p",  "proxytunnel",              FALSE},
245   {"P",  "ftpport",                  TRUE},
246          /* 'ftpport' old version */
247   {"P",  "ftp-port",                 TRUE},
248   {"q",  "disable",                  FALSE},
249   {"Q",  "quote",                    TRUE},
250   {"r",  "range",                    TRUE},
251   {"R",  "remote-time",              FALSE},
252   {"s",  "silent",                   FALSE},
253   {"S",  "show-error",               FALSE},
254   {"t",  "telnet-options",           TRUE},
255          /* 'telnet-options' documented as telnet-option */
256   {"T",  "upload-file",              TRUE},
257   {"u",  "user",                     TRUE},
258   {"U",  "proxy-user",               TRUE},
259   {"v",  "verbose",                  FALSE},
260   {"V",  "version",                  FALSE},
261   {"w",  "write-out",                TRUE},
262   {"x",  "proxy",                    TRUE},
263   {"X",  "request",                  TRUE},
264   {"X",  "http-request",             TRUE},
265          /* 'http-request' OBSOLETE VERSION */
266   {"Y",  "speed-limit",              TRUE},
267   {"y",  "speed-time",               TRUE},
268   {"z",  "time-cond",                TRUE},
269   {"#",  "progress-bar",             FALSE},
270   {"~",  "xattr",                    FALSE},
271 };
272
273 /* Split the argument of -E to 'certname' and 'passphrase' separated by colon.
274  * We allow ':' and '\' to be escaped by '\' so that we can use certificate
275  * nicknames containing ':'.  See <https://sourceforge.net/p/curl/bugs/1196/>
276  * for details. */
277 #ifndef UNITTESTS
278 static
279 #endif
280 void parse_cert_parameter(const char *cert_parameter,
281                           char **certname,
282                           char **passphrase)
283 {
284   size_t param_length = strlen(cert_parameter);
285   size_t span;
286   const char *param_place = NULL;
287   char *certname_place = NULL;
288   *certname = NULL;
289   *passphrase = NULL;
290
291   /* most trivial assumption: cert_parameter is empty */
292   if(param_length == 0)
293     return;
294
295   /* next less trivial: cert_parameter contains no colon nor backslash; this
296    * means no passphrase was given and no characters escaped */
297   if(!strpbrk(cert_parameter, ":\\")) {
298     *certname = strdup(cert_parameter);
299     return;
300   }
301   /* deal with escaped chars; find unescaped colon if it exists */
302   certname_place = malloc(param_length + 1);
303   if(!certname_place)
304     return;
305
306   *certname = certname_place;
307   param_place = cert_parameter;
308   while(*param_place) {
309     span = strcspn(param_place, ":\\");
310     strncpy(certname_place, param_place, span);
311     param_place += span;
312     certname_place += span;
313     /* we just ate all the non-special chars. now we're on either a special
314      * char or the end of the string. */
315     switch(*param_place) {
316     case '\0':
317       break;
318     case '\\':
319       param_place++;
320       switch(*param_place) {
321         case '\0':
322           *certname_place++ = '\\';
323           break;
324         case '\\':
325           *certname_place++ = '\\';
326           param_place++;
327           break;
328         case ':':
329           *certname_place++ = ':';
330           param_place++;
331           break;
332         default:
333           *certname_place++ = '\\';
334           *certname_place++ = *param_place;
335           param_place++;
336           break;
337       }
338       break;
339     case ':':
340       /* Since we live in a world of weirdness and confusion, the win32
341          dudes can use : when using drive letters and thus c:\file:password
342          needs to work. In order not to break compatibility, we still use : as
343          separator, but we try to detect when it is used for a file name! On
344          windows. */
345 #ifdef WIN32
346       if(param_place &&
347           (param_place == &cert_parameter[1]) &&
348           (cert_parameter[2] == '\\' || cert_parameter[2] == '/') &&
349           (ISALPHA(cert_parameter[0])) ) {
350         /* colon in the second column, followed by a backslash, and the
351            first character is an alphabetic letter:
352
353            this is a drive letter colon */
354         *certname_place++ = ':';
355         param_place++;
356         break;
357       }
358 #endif
359       /* escaped colons and Windows drive letter colons were handled
360        * above; if we're still here, this is a separating colon */
361       param_place++;
362       if(strlen(param_place) > 0) {
363         *passphrase = strdup(param_place);
364       }
365       goto done;
366     }
367   }
368 done:
369   *certname_place = '\0';
370 }
371
372 ParameterError getparameter(char *flag,    /* f or -long-flag */
373                             char *nextarg, /* NULL if unset */
374                             bool *usedarg, /* set to TRUE if the arg
375                                               has been used */
376                             struct OperationConfig *config)
377 {
378   char letter;
379   char subletter = '\0'; /* subletters can only occur on long options */
380   int rc;
381   const char *parse = NULL;
382   unsigned int j;
383   time_t now;
384   int hit = -1;
385   bool longopt = FALSE;
386   bool singleopt = FALSE; /* when true means '-o foo' used '-ofoo' */
387   ParameterError err;
388   bool toggle = TRUE; /* how to switch boolean options, on or off. Controlled
389                          by using --OPTION or --no-OPTION */
390
391
392   if(('-' != flag[0]) ||
393      (('-' == flag[0]) && ('-' == flag[1]))) {
394     /* this should be a long name */
395     char *word = ('-' == flag[0]) ? flag+2 : flag;
396     size_t fnam = strlen(word);
397     int numhits = 0;
398
399     if(!strncmp(word, "no-", 3)) {
400       /* disable this option but ignore the "no-" part when looking for it */
401       word += 3;
402       toggle = FALSE;
403     }
404
405     for(j = 0; j < sizeof(aliases)/sizeof(aliases[0]); j++) {
406       if(curlx_strnequal(aliases[j].lname, word, fnam)) {
407         longopt = TRUE;
408         numhits++;
409         if(curlx_raw_equal(aliases[j].lname, word)) {
410           parse = aliases[j].letter;
411           hit = j;
412           numhits = 1; /* a single unique hit */
413           break;
414         }
415         parse = aliases[j].letter;
416         hit = j;
417       }
418     }
419     if(numhits > 1) {
420       /* this is at least the second match! */
421       return PARAM_OPTION_AMBIGUOUS;
422     }
423     if(hit < 0) {
424       return PARAM_OPTION_UNKNOWN;
425     }
426   }
427   else {
428     flag++; /* prefixed with one dash, pass it */
429     hit = -1;
430     parse = flag;
431   }
432
433   do {
434     /* we can loop here if we have multiple single-letters */
435
436     if(!longopt) {
437       if(NULL != parse) {
438         letter = (char)*parse;
439       }
440       else {
441         letter = '\0';
442       }
443       subletter='\0';
444     }
445     else {
446       letter = parse[0];
447       subletter = parse[1];
448     }
449     *usedarg = FALSE; /* default is that we don't use the arg */
450
451     if(hit < 0) {
452       for(j = 0; j < sizeof(aliases)/sizeof(aliases[0]); j++) {
453         if(letter == aliases[j].letter[0]) {
454           hit = j;
455           break;
456         }
457       }
458       if(hit < 0) {
459         return PARAM_OPTION_UNKNOWN;
460       }
461     }
462
463     if(aliases[hit].extraparam) {
464       /* this option requires an extra parameter */
465       if(!longopt && parse[1]) {
466         nextarg = (char *)&parse[1]; /* this is the actual extra parameter */
467         singleopt = TRUE;   /* don't loop anymore after this */
468       }
469       else if(!nextarg)
470         return PARAM_REQUIRES_PARAMETER;
471       else
472         *usedarg = TRUE; /* mark it as used */
473     }
474
475     switch(letter) {
476     case '*': /* options without a short option */
477       switch(subletter) {
478       case '4': /* --dns-ipv4-addr */
479         /* addr in dot notation */
480         GetStr(&config->dns_ipv4_addr, nextarg);
481         break;
482       case '6': /* --dns-ipv6-addr */
483         /* addr in dot notation */
484         GetStr(&config->dns_ipv6_addr, nextarg);
485         break;
486       case 'a': /* random-file */
487         GetStr(&config->random_file, nextarg);
488         break;
489       case 'b': /* egd-file */
490         GetStr(&config->egd_file, nextarg);
491         break;
492       case 'B': /* XOAUTH2 Bearer */
493         GetStr(&config->xoauth2_bearer, nextarg);
494         break;
495       case 'c': /* connect-timeout */
496         err = str2udouble(&config->connecttimeout, nextarg);
497         if(err)
498           return err;
499         break;
500       case 'd': /* ciphers */
501         GetStr(&config->cipher_list, nextarg);
502         break;
503       case 'D': /* --dns-interface */
504         /* interface name */
505         GetStr(&config->dns_interface, nextarg);
506         break;
507       case 'e': /* --disable-epsv */
508         config->disable_epsv = toggle;
509         break;
510       case 'E': /* --epsv */
511         config->disable_epsv = (!toggle)?TRUE:FALSE;
512         break;
513 #ifdef USE_ENVIRONMENT
514       case 'f':
515         config->writeenv = toggle;
516         break;
517 #endif
518       case 'F': /* --dns-servers */
519         /* IP addrs of DNS servers */
520         GetStr(&config->dns_servers, nextarg);
521         break;
522       case 'g': /* --trace */
523         GetStr(&config->trace_dump, nextarg);
524         if(config->tracetype && (config->tracetype != TRACE_BIN))
525           warnf(config, "--trace overrides an earlier trace/verbose option\n");
526         config->tracetype = TRACE_BIN;
527         break;
528       case 'G': /* --npn */
529         config->nonpn = (!toggle)?TRUE:FALSE;
530         break;
531       case 'h': /* --trace-ascii */
532         GetStr(&config->trace_dump, nextarg);
533         if(config->tracetype && (config->tracetype != TRACE_ASCII))
534           warnf(config,
535                 "--trace-ascii overrides an earlier trace/verbose option\n");
536         config->tracetype = TRACE_ASCII;
537         break;
538       case 'H': /* --alpn */
539         config->noalpn = (!toggle)?TRUE:FALSE;
540         break;
541       case 'i': /* --limit-rate */
542       {
543         /* We support G, M, K too */
544         char *unit;
545         curl_off_t value = curlx_strtoofft(nextarg, &unit, 0);
546
547         if(!*unit)
548           unit = (char *)"b";
549         else if(strlen(unit) > 1)
550           unit = (char *)"w"; /* unsupported */
551
552         switch(*unit) {
553         case 'G':
554         case 'g':
555           value *= 1024*1024*1024;
556           break;
557         case 'M':
558         case 'm':
559           value *= 1024*1024;
560           break;
561         case 'K':
562         case 'k':
563           value *= 1024;
564           break;
565         case 'b':
566         case 'B':
567           /* for plain bytes, leave as-is */
568           break;
569         default:
570           warnf(config, "unsupported rate unit. Use G, M, K or B!\n");
571           return PARAM_BAD_USE;
572         }
573         config->recvpersecond = value;
574         config->sendpersecond = value;
575       }
576       break;
577
578       case 'j': /* --compressed */
579         if(toggle && !(curlinfo->features & CURL_VERSION_LIBZ))
580           return PARAM_LIBCURL_DOESNT_SUPPORT;
581         config->encoding = toggle;
582         break;
583
584       case 'J': /* --tr-encoding */
585         config->tr_encoding = toggle;
586         break;
587
588       case 'k': /* --digest */
589         if(toggle)
590           config->authtype |= CURLAUTH_DIGEST;
591         else
592           config->authtype &= ~CURLAUTH_DIGEST;
593         break;
594
595       case 'l': /* --negotiate */
596         if(toggle) {
597           if(curlinfo->features & CURL_VERSION_GSSNEGOTIATE)
598             config->authtype |= CURLAUTH_GSSNEGOTIATE;
599           else
600             return PARAM_LIBCURL_DOESNT_SUPPORT;
601         }
602         else
603           config->authtype &= ~CURLAUTH_GSSNEGOTIATE;
604         break;
605
606       case 'm': /* --ntlm */
607         if(toggle) {
608           if(curlinfo->features & CURL_VERSION_NTLM)
609             config->authtype |= CURLAUTH_NTLM;
610           else
611             return PARAM_LIBCURL_DOESNT_SUPPORT;
612         }
613         else
614           config->authtype &= ~CURLAUTH_NTLM;
615         break;
616
617       case 'M': /* --ntlm-wb */
618         if(toggle) {
619           if(curlinfo->features & CURL_VERSION_NTLM_WB)
620             config->authtype |= CURLAUTH_NTLM_WB;
621           else
622             return PARAM_LIBCURL_DOESNT_SUPPORT;
623         }
624         else
625           config->authtype &= ~CURLAUTH_NTLM_WB;
626         break;
627
628       case 'n': /* --basic for completeness */
629         if(toggle)
630           config->authtype |= CURLAUTH_BASIC;
631         else
632           config->authtype &= ~CURLAUTH_BASIC;
633         break;
634
635       case 'o': /* --anyauth, let libcurl pick it */
636         if(toggle)
637           config->authtype = CURLAUTH_ANY;
638         /* --no-anyauth simply doesn't touch it */
639         break;
640
641 #ifdef USE_WATT32
642       case 'p': /* --wdebug */
643         dbug_init();
644         break;
645 #endif
646       case 'q': /* --ftp-create-dirs */
647         config->ftp_create_dirs = toggle;
648         break;
649
650       case 'r': /* --create-dirs */
651         config->create_dirs = toggle;
652         break;
653
654       case 's': /* --max-redirs */
655         /* specified max no of redirects (http(s)), this accepts -1 as a
656            special condition */
657         err = str2num(&config->maxredirs, nextarg);
658         if(err)
659           return err;
660         if(config->maxredirs < -1)
661           return PARAM_BAD_NUMERIC;
662         break;
663
664       case 't': /* --proxy-ntlm */
665         if(curlinfo->features & CURL_VERSION_NTLM)
666           config->proxyntlm = toggle;
667         else
668           return PARAM_LIBCURL_DOESNT_SUPPORT;
669         break;
670
671       case 'u': /* --crlf */
672         /* LF -> CRLF conversion? */
673         config->crlf = toggle;
674         break;
675
676       case 'v': /* --stderr */
677         if(strcmp(nextarg, "-")) {
678           FILE *newfile = fopen(nextarg, "wt");
679           if(!newfile)
680             warnf(config, "Failed to open %s!\n", nextarg);
681           else {
682             if(config->errors_fopened)
683               fclose(config->errors);
684             config->errors = newfile;
685             config->errors_fopened = TRUE;
686           }
687         }
688         else
689           config->errors = stdout;
690         break;
691       case 'w': /* --interface */
692         /* interface */
693         GetStr(&config->iface, nextarg);
694         break;
695       case 'x': /* --krb */
696         /* kerberos level string */
697         if(curlinfo->features & (CURL_VERSION_KERBEROS4 |
698                                  CURL_VERSION_GSSNEGOTIATE))
699           GetStr(&config->krblevel, nextarg);
700         else
701           return PARAM_LIBCURL_DOESNT_SUPPORT;
702         break;
703       case 'y': /* --max-filesize */
704         err = str2offset(&config->max_filesize, nextarg);
705         if(err)
706           return err;
707         break;
708       case 'z': /* --disable-eprt */
709         config->disable_eprt = toggle;
710         break;
711       case 'Z': /* --eprt */
712         config->disable_eprt = (!toggle)?TRUE:FALSE;
713         break;
714
715       default: /* the URL! */
716       {
717         struct getout *url;
718         if(config->url_get || ((config->url_get = config->url_list) != NULL)) {
719           /* there's a node here, if it already is filled-in continue to find
720              an "empty" node */
721           while(config->url_get && (config->url_get->flags & GETOUT_URL))
722             config->url_get = config->url_get->next;
723         }
724
725         /* now there might or might not be an available node to fill in! */
726
727         if(config->url_get)
728           /* existing node */
729           url = config->url_get;
730         else
731           /* there was no free node, create one! */
732           url = new_getout(config);
733
734         if(!url)
735           return PARAM_NO_MEM;
736         else {
737           /* fill in the URL */
738           GetStr(&url->url, nextarg);
739           url->flags |= GETOUT_URL;
740         }
741       }
742       }
743       break;
744     case '$': /* more options without a short option */
745       switch(subletter) {
746       case 'a': /* --ftp-ssl */
747         if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
748           return PARAM_LIBCURL_DOESNT_SUPPORT;
749         config->ftp_ssl = toggle;
750         break;
751       case 'b': /* --ftp-pasv */
752         Curl_safefree(config->ftpport);
753         break;
754       case 'c': /* --socks5 specifies a socks5 proxy to use, and resolves
755                    the name locally and passes on the resolved address */
756         GetStr(&config->socksproxy, nextarg);
757         config->socksver = CURLPROXY_SOCKS5;
758         break;
759       case 't': /* --socks4 specifies a socks4 proxy to use */
760         GetStr(&config->socksproxy, nextarg);
761         config->socksver = CURLPROXY_SOCKS4;
762         break;
763       case 'T': /* --socks4a specifies a socks4a proxy to use */
764         GetStr(&config->socksproxy, nextarg);
765         config->socksver = CURLPROXY_SOCKS4A;
766         break;
767       case '2': /* --socks5-hostname specifies a socks5 proxy and enables name
768                    resolving with the proxy */
769         GetStr(&config->socksproxy, nextarg);
770         config->socksver = CURLPROXY_SOCKS5_HOSTNAME;
771         break;
772       case 'd': /* --tcp-nodelay option */
773         config->tcp_nodelay = toggle;
774         break;
775       case 'e': /* --proxy-digest */
776         config->proxydigest = toggle;
777         break;
778       case 'f': /* --proxy-basic */
779         config->proxybasic = toggle;
780         break;
781       case 'g': /* --retry */
782         err = str2unum(&config->req_retry, nextarg);
783         if(err)
784           return err;
785         break;
786       case 'h': /* --retry-delay */
787         err = str2unum(&config->retry_delay, nextarg);
788         if(err)
789           return err;
790         break;
791       case 'i': /* --retry-max-time */
792         err = str2unum(&config->retry_maxtime, nextarg);
793         if(err)
794           return err;
795         break;
796
797       case 'k': /* --proxy-negotiate */
798         if(curlinfo->features & CURL_VERSION_GSSNEGOTIATE)
799           config->proxynegotiate = toggle;
800         else
801           return PARAM_LIBCURL_DOESNT_SUPPORT;
802         break;
803       case 'm': /* --ftp-account */
804         GetStr(&config->ftp_account, nextarg);
805         break;
806       case 'n': /* --proxy-anyauth */
807         config->proxyanyauth = toggle;
808         break;
809       case 'o': /* --trace-time */
810         config->tracetime = toggle;
811         break;
812       case 'p': /* --ignore-content-length */
813         config->ignorecl = toggle;
814         break;
815       case 'q': /* --ftp-skip-pasv-ip */
816         config->ftp_skip_ip = toggle;
817         break;
818       case 'r': /* --ftp-method (undocumented at this point) */
819         config->ftp_filemethod = ftpfilemethod(config, nextarg);
820         break;
821       case 's': /* --local-port */
822         rc = sscanf(nextarg, "%d - %d",
823                     &config->localport,
824                     &config->localportrange);
825         if(!rc)
826           return PARAM_BAD_USE;
827         else if(rc == 1)
828           config->localportrange = 1; /* default number of ports to try */
829         else {
830           config->localportrange -= config->localport;
831           if(config->localportrange < 1) {
832             warnf(config, "bad range input\n");
833             return PARAM_BAD_USE;
834           }
835         }
836         break;
837       case 'u': /* --ftp-alternative-to-user */
838         GetStr(&config->ftp_alternative_to_user, nextarg);
839         break;
840       case 'v': /* --ftp-ssl-reqd */
841         if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
842           return PARAM_LIBCURL_DOESNT_SUPPORT;
843         config->ftp_ssl_reqd = toggle;
844         break;
845       case 'w': /* --no-sessionid */
846         config->disable_sessionid = (!toggle)?TRUE:FALSE;
847         break;
848       case 'x': /* --ftp-ssl-control */
849         if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
850           return PARAM_LIBCURL_DOESNT_SUPPORT;
851         config->ftp_ssl_control = toggle;
852         break;
853       case 'y': /* --ftp-ssl-ccc */
854         config->ftp_ssl_ccc = toggle;
855         if(!config->ftp_ssl_ccc_mode)
856           config->ftp_ssl_ccc_mode = CURLFTPSSL_CCC_PASSIVE;
857         break;
858       case 'j': /* --ftp-ssl-ccc-mode */
859         config->ftp_ssl_ccc = TRUE;
860         config->ftp_ssl_ccc_mode = ftpcccmethod(config, nextarg);
861         break;
862       case 'z': /* --libcurl */
863 #ifdef CURL_DISABLE_LIBCURL_OPTION
864         warnf(config,
865               "--libcurl option was disabled at build-time!\n");
866         return PARAM_OPTION_UNKNOWN;
867 #else
868         GetStr(&config->libcurl, nextarg);
869         break;
870 #endif
871       case '#': /* --raw */
872         config->raw = toggle;
873         break;
874       case '0': /* --post301 */
875         config->post301 = toggle;
876         break;
877       case '1': /* --no-keepalive */
878         config->nokeepalive = (!toggle)?TRUE:FALSE;
879         break;
880       case '3': /* --keepalive-time */
881         err = str2unum(&config->alivetime, nextarg);
882         if(err)
883           return err;
884         break;
885       case '4': /* --post302 */
886         config->post302 = toggle;
887         break;
888       case 'I': /* --post303 */
889         config->post303 = toggle;
890         break;
891       case '5': /* --noproxy */
892         /* This specifies the noproxy list */
893         GetStr(&config->noproxy, nextarg);
894         break;
895 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
896       case '6': /* --socks5-gssapi-service */
897         GetStr(&config->socks5_gssapi_service, nextarg);
898         break;
899       case '7': /* --socks5-gssapi-nec*/
900         config->socks5_gssapi_nec = toggle;
901         break;
902 #endif
903       case '8': /* --proxy1.0 */
904         /* http 1.0 proxy */
905         GetStr(&config->proxy, nextarg);
906         config->proxyver = CURLPROXY_HTTP_1_0;
907         break;
908       case '9': /* --tftp-blksize */
909         err = str2unum(&config->tftp_blksize, nextarg);
910         if(err)
911           return err;
912         break;
913       case 'A': /* --mail-from */
914         GetStr(&config->mail_from, nextarg);
915         break;
916       case 'B': /* --mail-rcpt */
917         /* append receiver to a list */
918         err = add2list(&config->mail_rcpt, nextarg);
919         if(err)
920           return err;
921         break;
922       case 'C': /* --ftp-pret */
923         config->ftp_pret = toggle;
924         break;
925       case 'D': /* --proto */
926         config->proto_present = TRUE;
927         if(proto2num(config, &config->proto, nextarg))
928           return PARAM_BAD_USE;
929         break;
930       case 'E': /* --proto-redir */
931         config->proto_redir_present = TRUE;
932         if(proto2num(config, &config->proto_redir, nextarg))
933           return PARAM_BAD_USE;
934         break;
935       case 'F': /* --resolve */
936         err = add2list(&config->resolve, nextarg);
937         if(err)
938           return err;
939         break;
940       case 'G': /* --delegation LEVEL */
941         config->gssapi_delegation = delegation(config, nextarg);
942         break;
943       case 'H': /* --mail-auth */
944         GetStr(&config->mail_auth, nextarg);
945         break;
946       case 'J': /* --metalink */
947         {
948 #ifdef USE_METALINK
949           int mlmaj, mlmin, mlpatch;
950           metalink_get_version(&mlmaj, &mlmin, &mlpatch);
951           if((mlmaj*10000)+(mlmin*100)+mlpatch < CURL_REQ_LIBMETALINK_VERS) {
952             warnf(config,
953                   "--metalink option cannot be used because the version of "
954                   "the linked libmetalink library is too old. "
955                   "Required: %d.%d.%d, found %d.%d.%d\n",
956                   CURL_REQ_LIBMETALINK_MAJOR,
957                   CURL_REQ_LIBMETALINK_MINOR,
958                   CURL_REQ_LIBMETALINK_PATCH,
959                   mlmaj, mlmin, mlpatch);
960             return PARAM_BAD_USE;
961           }
962           else
963             config->use_metalink = toggle;
964 #else
965           warnf(config, "--metalink option is ignored because the binary is "
966                 "built without the Metalink support.\n");
967 #endif
968           break;
969         }
970       case 'K': /* --sasl-ir */
971         config->sasl_ir = toggle;
972         break;
973       case 'L': /* --test-event */
974 #ifdef CURLDEBUG
975         config->test_event_based = toggle;
976 #else
977         warnf(config, "--test-event is ignored unless a debug build!\n");
978 #endif
979         break;
980       }
981       break;
982     case '#': /* --progress-bar */
983       if(toggle)
984         config->progressmode = CURL_PROGRESS_BAR;
985       else
986         config->progressmode = CURL_PROGRESS_STATS;
987       break;
988     case '~': /* --xattr */
989       config->xattr = toggle;
990       break;
991     case '0': /* --http* options */
992       switch(subletter) {
993       case '\0':
994         /* HTTP version 1.0 */
995         config->httpversion = CURL_HTTP_VERSION_1_0;
996         break;
997       case '1':
998         /* HTTP version 1.1 */
999         config->httpversion = CURL_HTTP_VERSION_1_1;
1000         break;
1001       case '2':
1002         /* HTTP version 2.0 */
1003         config->httpversion = CURL_HTTP_VERSION_2_0;
1004         break;
1005       }
1006       break;
1007     case '1': /* --tlsv1* options */
1008       switch(subletter) {
1009       case '\0':
1010         /* TLS version 1.x */
1011         config->ssl_version = CURL_SSLVERSION_TLSv1;
1012         break;
1013       case '0':
1014         /* TLS version 1.0 */
1015         config->ssl_version = CURL_SSLVERSION_TLSv1_0;
1016         break;
1017       case '1':
1018         /* TLS version 1.1 */
1019         config->ssl_version = CURL_SSLVERSION_TLSv1_1;
1020         break;
1021       case '2':
1022         /* TLS version 1.2 */
1023         config->ssl_version = CURL_SSLVERSION_TLSv1_2;
1024         break;
1025       }
1026       break;
1027     case '2':
1028       /* SSL version 2 */
1029       config->ssl_version = CURL_SSLVERSION_SSLv2;
1030       break;
1031     case '3':
1032       /* SSL version 3 */
1033       config->ssl_version = CURL_SSLVERSION_SSLv3;
1034       break;
1035     case '4':
1036       /* IPv4 */
1037       config->ip_version = 4;
1038       break;
1039     case '6':
1040       /* IPv6 */
1041       config->ip_version = 6;
1042       break;
1043     case 'a':
1044       /* This makes the FTP sessions use APPE instead of STOR */
1045       config->ftp_append = toggle;
1046       break;
1047     case 'A':
1048       /* This specifies the User-Agent name */
1049       GetStr(&config->useragent, nextarg);
1050       break;
1051     case 'b': /* cookie string coming up: */
1052       if(nextarg[0] == '@') {
1053         nextarg++;
1054       }
1055       else if(strchr(nextarg, '=')) {
1056         /* A cookie string must have a =-letter */
1057         GetStr(&config->cookie, nextarg);
1058         break;
1059       }
1060       /* We have a cookie file to read from! */
1061       GetStr(&config->cookiefile, nextarg);
1062       break;
1063     case 'B':
1064       /* use ASCII/text when transferring */
1065       config->use_ascii = toggle;
1066       break;
1067     case 'c':
1068       /* get the file name to dump all cookies in */
1069       GetStr(&config->cookiejar, nextarg);
1070       break;
1071     case 'C':
1072       /* This makes us continue an ftp transfer at given position */
1073       if(!curlx_strequal(nextarg, "-")) {
1074         err = str2offset(&config->resume_from, nextarg);
1075         if(err)
1076           return err;
1077         config->resume_from_current = FALSE;
1078       }
1079       else {
1080         config->resume_from_current = TRUE;
1081         config->resume_from = 0;
1082       }
1083       config->use_resume=TRUE;
1084       break;
1085     case 'd':
1086       /* postfield data */
1087     {
1088       char *postdata = NULL;
1089       FILE *file;
1090       size_t size = 0;
1091
1092       if(subletter == 'e') { /* --data-urlencode*/
1093         /* [name]=[content], we encode the content part only
1094          * [name]@[file name]
1095          *
1096          * Case 2: we first load the file using that name and then encode
1097          * the content.
1098          */
1099         const char *p = strchr(nextarg, '=');
1100         size_t nlen;
1101         char is_file;
1102         if(!p)
1103           /* there was no '=' letter, check for a '@' instead */
1104           p = strchr(nextarg, '@');
1105         if(p) {
1106           nlen = p - nextarg; /* length of the name part */
1107           is_file = *p++; /* pass the separator */
1108         }
1109         else {
1110           /* neither @ nor =, so no name and it isn't a file */
1111           nlen = is_file = 0;
1112           p = nextarg;
1113         }
1114         if('@' == is_file) {
1115           /* a '@' letter, it means that a file name or - (stdin) follows */
1116
1117           if(curlx_strequal("-", p)) {
1118             file = stdin;
1119             set_binmode(stdin);
1120           }
1121           else {
1122             file = fopen(p, "rb");
1123             if(!file)
1124               warnf(config,
1125                     "Couldn't read data from file \"%s\", this makes "
1126                     "an empty POST.\n", nextarg);
1127           }
1128
1129           err = file2memory(&postdata, &size, file);
1130
1131           if(file && (file != stdin))
1132             fclose(file);
1133           if(err)
1134             return err;
1135         }
1136         else {
1137           GetStr(&postdata, p);
1138           if(postdata)
1139             size = strlen(postdata);
1140         }
1141
1142         if(!postdata) {
1143           /* no data from the file, point to a zero byte string to make this
1144              get sent as a POST anyway */
1145           postdata = strdup("");
1146           if(!postdata)
1147             return PARAM_NO_MEM;
1148           size = 0;
1149         }
1150         else {
1151           char *enc = curl_easy_escape(config->easy, postdata, (int)size);
1152           Curl_safefree(postdata); /* no matter if it worked or not */
1153           if(enc) {
1154             /* now make a string with the name from above and append the
1155                encoded string */
1156             size_t outlen = nlen + strlen(enc) + 2;
1157             char *n = malloc(outlen);
1158             if(!n) {
1159               curl_free(enc);
1160               return PARAM_NO_MEM;
1161             }
1162             if(nlen > 0) { /* only append '=' if we have a name */
1163               snprintf(n, outlen, "%.*s=%s", nlen, nextarg, enc);
1164               size = outlen-1;
1165             }
1166             else {
1167               strcpy(n, enc);
1168               size = outlen-2; /* since no '=' was inserted */
1169             }
1170             curl_free(enc);
1171             postdata = n;
1172           }
1173           else
1174             return PARAM_NO_MEM;
1175         }
1176       }
1177       else if('@' == *nextarg) {
1178         /* the data begins with a '@' letter, it means that a file name
1179            or - (stdin) follows */
1180         nextarg++; /* pass the @ */
1181
1182         if(curlx_strequal("-", nextarg)) {
1183           file = stdin;
1184           if(subletter == 'b') /* forced data-binary */
1185             set_binmode(stdin);
1186         }
1187         else {
1188           file = fopen(nextarg, "rb");
1189           if(!file)
1190             warnf(config, "Couldn't read data from file \"%s\", this makes "
1191                   "an empty POST.\n", nextarg);
1192         }
1193
1194         if(subletter == 'b')
1195           /* forced binary */
1196           err = file2memory(&postdata, &size, file);
1197         else {
1198           err = file2string(&postdata, file);
1199           if(postdata)
1200             size = strlen(postdata);
1201         }
1202
1203         if(file && (file != stdin))
1204           fclose(file);
1205         if(err)
1206           return err;
1207
1208         if(!postdata) {
1209           /* no data from the file, point to a zero byte string to make this
1210              get sent as a POST anyway */
1211           postdata = strdup("");
1212           if(!postdata)
1213             return PARAM_NO_MEM;
1214         }
1215       }
1216       else {
1217         GetStr(&postdata, nextarg);
1218         if(postdata)
1219           size = strlen(postdata);
1220       }
1221
1222 #ifdef CURL_DOES_CONVERSIONS
1223       if(subletter != 'b') {
1224         /* NOT forced binary, convert to ASCII */
1225         if(convert_to_network(postdata, strlen(postdata))) {
1226           Curl_safefree(postdata);
1227           return PARAM_NO_MEM;
1228         }
1229       }
1230 #endif
1231
1232       if(config->postfields) {
1233         /* we already have a string, we append this one with a separating
1234            &-letter */
1235         char *oldpost = config->postfields;
1236         curl_off_t oldlen = config->postfieldsize;
1237         curl_off_t newlen = oldlen + size + 2;
1238         config->postfields = malloc((size_t)newlen);
1239         if(!config->postfields) {
1240           Curl_safefree(oldpost);
1241           Curl_safefree(postdata);
1242           return PARAM_NO_MEM;
1243         }
1244         memcpy(config->postfields, oldpost, (size_t)oldlen);
1245         /* use byte value 0x26 for '&' to accommodate non-ASCII platforms */
1246         config->postfields[oldlen] = '\x26';
1247         memcpy(&config->postfields[oldlen+1], postdata, size);
1248         config->postfields[oldlen+1+size] = '\0';
1249         Curl_safefree(oldpost);
1250         Curl_safefree(postdata);
1251         config->postfieldsize += size+1;
1252       }
1253       else {
1254         config->postfields = postdata;
1255         config->postfieldsize = size;
1256       }
1257     }
1258     /*
1259       We can't set the request type here, as this data might be used in
1260       a simple GET if -G is used. Already or soon.
1261
1262       if(SetHTTPrequest(HTTPREQ_SIMPLEPOST, &config->httpreq)) {
1263         Curl_safefree(postdata);
1264         return PARAM_BAD_USE;
1265       }
1266     */
1267     break;
1268     case 'D':
1269       /* dump-header to given file name */
1270       GetStr(&config->headerfile, nextarg);
1271       break;
1272     case 'e':
1273     {
1274       char *ptr = strstr(nextarg, ";auto");
1275       if(ptr) {
1276         /* Automatic referer requested, this may be combined with a
1277            set initial one */
1278         config->autoreferer = TRUE;
1279         *ptr = 0; /* zero terminate here */
1280       }
1281       else
1282         config->autoreferer = FALSE;
1283       GetStr(&config->referer, nextarg);
1284     }
1285     break;
1286     case 'E':
1287       switch(subletter) {
1288       case 'a': /* CA info PEM file */
1289         /* CA info PEM file */
1290         GetStr(&config->cacert, nextarg);
1291         break;
1292       case 'b': /* cert file type */
1293         GetStr(&config->cert_type, nextarg);
1294         break;
1295       case 'c': /* private key file */
1296         GetStr(&config->key, nextarg);
1297         break;
1298       case 'd': /* private key file type */
1299         GetStr(&config->key_type, nextarg);
1300         break;
1301       case 'e': /* private key passphrase */
1302         GetStr(&config->key_passwd, nextarg);
1303         cleanarg(nextarg);
1304         break;
1305       case 'f': /* crypto engine */
1306         GetStr(&config->engine, nextarg);
1307         if(config->engine && curlx_raw_equal(config->engine,"list"))
1308           return PARAM_ENGINES_REQUESTED;
1309         break;
1310       case 'g': /* CA info PEM file */
1311         /* CA cert directory */
1312         GetStr(&config->capath, nextarg);
1313         break;
1314       case 'h': /* --pubkey public key file */
1315         GetStr(&config->pubkey, nextarg);
1316         break;
1317       case 'i': /* --hostpubmd5 md5 of the host public key */
1318         GetStr(&config->hostpubmd5, nextarg);
1319         if(!config->hostpubmd5 || strlen(config->hostpubmd5) != 32)
1320           return PARAM_BAD_USE;
1321         break;
1322       case 'j': /* CRL info PEM file */
1323         /* CRL file */
1324         GetStr(&config->crlfile, nextarg);
1325         break;
1326       case 'k': /* TLS username */
1327         if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
1328           GetStr(&config->tls_username, nextarg);
1329         else
1330           return PARAM_LIBCURL_DOESNT_SUPPORT;
1331         break;
1332       case 'l': /* TLS password */
1333         if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
1334           GetStr(&config->tls_password, nextarg);
1335         else
1336           return PARAM_LIBCURL_DOESNT_SUPPORT;
1337         break;
1338       case 'm': /* TLS authentication type */
1339         if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
1340           GetStr(&config->tls_authtype, nextarg);
1341           if(!strequal(config->tls_authtype, "SRP"))
1342             return PARAM_LIBCURL_DOESNT_SUPPORT; /* only support TLS-SRP */
1343         }
1344         else
1345           return PARAM_LIBCURL_DOESNT_SUPPORT;
1346         break;
1347       case 'n': /* no empty SSL fragments, --ssl-allow-beast */
1348         if(curlinfo->features & CURL_VERSION_SSL)
1349           config->ssl_allow_beast = toggle;
1350         break;
1351
1352       case 'o': /* --login-options */
1353         GetStr(&config->login_options, nextarg);
1354         break;
1355
1356       default: /* certificate file */
1357       {
1358         char *certname, *passphrase;
1359         parse_cert_parameter(nextarg, &certname, &passphrase);
1360         Curl_safefree(config->cert);
1361         config->cert = certname;
1362         if(passphrase) {
1363           Curl_safefree(config->key_passwd);
1364           config->key_passwd = passphrase;
1365         }
1366         cleanarg(nextarg);
1367       }
1368       }
1369       break;
1370     case 'f':
1371       /* fail hard on errors  */
1372       config->failonerror = toggle;
1373       break;
1374     case 'F':
1375       /* "form data" simulation, this is a little advanced so lets do our best
1376          to sort this out slowly and carefully */
1377       if(formparse(config,
1378                    nextarg,
1379                    &config->httppost,
1380                    &config->last_post,
1381                    (subletter=='s')?TRUE:FALSE)) /* 's' means literal string */
1382         return PARAM_BAD_USE;
1383       if(SetHTTPrequest(config, HTTPREQ_POST, &config->httpreq))
1384         return PARAM_BAD_USE;
1385       break;
1386
1387     case 'g': /* g disables URLglobbing */
1388       config->globoff = toggle;
1389       break;
1390
1391     case 'G': /* HTTP GET */
1392       config->use_httpget = TRUE;
1393       break;
1394
1395     case 'h': /* h for help */
1396       if(toggle) {
1397         return PARAM_HELP_REQUESTED;
1398       }
1399       /* we now actually support --no-help too! */
1400       break;
1401     case 'H':
1402       /* A custom header to append to a list */
1403       err = add2list(&config->headers, nextarg);
1404       if(err)
1405         return err;
1406       break;
1407     case 'i':
1408       config->include_headers = toggle; /* include the headers as well in the
1409                                            general output stream */
1410       break;
1411     case 'j':
1412       config->cookiesession = toggle;
1413       break;
1414     case 'I':
1415       /*
1416        * no_body will imply include_headers later on
1417        */
1418       config->no_body = toggle;
1419       if(SetHTTPrequest(config,
1420                         (config->no_body)?HTTPREQ_HEAD:HTTPREQ_GET,
1421                         &config->httpreq))
1422         return PARAM_BAD_USE;
1423       break;
1424     case 'J': /* --remote-header-name */
1425       if(config->include_headers) {
1426         warnf(config,
1427               "--include and --remote-header-name cannot be combined.\n");
1428         return PARAM_BAD_USE;
1429       }
1430       config->content_disposition = toggle;
1431       break;
1432     case 'k': /* allow insecure SSL connects */
1433       config->insecure_ok = toggle;
1434       break;
1435     case 'K': /* parse config file */
1436       if(parseconfig(nextarg, config))
1437         warnf(config, "error trying read config from the '%s' file\n",
1438               nextarg);
1439       break;
1440     case 'l':
1441       config->dirlistonly = toggle; /* only list the names of the FTP dir */
1442       break;
1443     case 'L':
1444       config->followlocation = toggle; /* Follow Location: HTTP headers */
1445       switch (subletter) {
1446       case 't':
1447         /* Continue to send authentication (user+password) when following
1448          * locations, even when hostname changed */
1449         config->unrestricted_auth = toggle;
1450         break;
1451       }
1452       break;
1453     case 'm':
1454       /* specified max time */
1455       err = str2udouble(&config->timeout, nextarg);
1456       if(err)
1457         return err;
1458       break;
1459     case 'M': /* M for manual, huge help */
1460       if(toggle) { /* --no-manual shows no manual... */
1461 #ifdef USE_MANUAL
1462         return PARAM_MANUAL_REQUESTED;
1463 #else
1464         warnf(config,
1465               "built-in manual was disabled at build-time!\n");
1466         return PARAM_OPTION_UNKNOWN;
1467 #endif
1468       }
1469       break;
1470     case 'n':
1471       switch(subletter) {
1472       case 'o': /* CA info PEM file */
1473         /* use .netrc or URL */
1474         config->netrc_opt = toggle;
1475         break;
1476       case 'e': /* netrc-file */
1477         GetStr(&config->netrc_file, nextarg);
1478         break;
1479       default:
1480         /* pick info from .netrc, if this is used for http, curl will
1481            automatically enfore user+password with the request */
1482         config->netrc = toggle;
1483         break;
1484       }
1485       break;
1486     case 'N':
1487       /* disable the output I/O buffering. note that the option is called
1488          --buffer but is mostly used in the negative form: --no-buffer */
1489       if(longopt)
1490         config->nobuffer = (!toggle)?TRUE:FALSE;
1491       else
1492         config->nobuffer = toggle;
1493       break;
1494     case 'O': /* --remote-name */
1495       if(subletter == 'a') { /* --remote-name-all */
1496         config->default_node_flags = toggle?GETOUT_USEREMOTE:0;
1497         break;
1498       }
1499       /* fall-through! */
1500     case 'o': /* --output */
1501       /* output file */
1502     {
1503       struct getout *url;
1504       if(config->url_out || ((config->url_out = config->url_list) != NULL)) {
1505         /* there's a node here, if it already is filled-in continue to find
1506            an "empty" node */
1507         while(config->url_out && (config->url_out->flags & GETOUT_OUTFILE))
1508           config->url_out = config->url_out->next;
1509       }
1510
1511       /* now there might or might not be an available node to fill in! */
1512
1513       if(config->url_out)
1514         /* existing node */
1515         url = config->url_out;
1516       else
1517         /* there was no free node, create one! */
1518         url = new_getout(config);
1519
1520       if(!url)
1521         return PARAM_NO_MEM;
1522       else {
1523         /* fill in the outfile */
1524         if('o' == letter) {
1525           GetStr(&url->outfile, nextarg);
1526           url->flags &= ~GETOUT_USEREMOTE; /* switch off */
1527         }
1528         else {
1529           url->outfile = NULL; /* leave it */
1530           if(toggle)
1531             url->flags |= GETOUT_USEREMOTE;  /* switch on */
1532           else
1533             url->flags &= ~GETOUT_USEREMOTE; /* switch off */
1534         }
1535         url->flags |= GETOUT_OUTFILE;
1536       }
1537     }
1538     break;
1539     case 'P':
1540       /* This makes the FTP sessions use PORT instead of PASV */
1541       /* use <eth0> or <192.168.10.10> style addresses. Anything except
1542          this will make us try to get the "default" address.
1543          NOTE: this is a changed behaviour since the released 4.1!
1544       */
1545       GetStr(&config->ftpport, nextarg);
1546       break;
1547     case 'p':
1548       /* proxy tunnel for non-http protocols */
1549       config->proxytunnel = toggle;
1550       break;
1551
1552     case 'q': /* if used first, already taken care of, we do it like
1553                  this so we don't cause an error! */
1554       break;
1555     case 'Q':
1556       /* QUOTE command to send to FTP server */
1557       switch(nextarg[0]) {
1558       case '-':
1559         /* prefixed with a dash makes it a POST TRANSFER one */
1560         nextarg++;
1561         err = add2list(&config->postquote, nextarg);
1562         break;
1563       case '+':
1564         /* prefixed with a plus makes it a just-before-transfer one */
1565         nextarg++;
1566         err = add2list(&config->prequote, nextarg);
1567         break;
1568       default:
1569         err = add2list(&config->quote, nextarg);
1570         break;
1571       }
1572       if(err)
1573         return err;
1574       break;
1575     case 'r':
1576       /* Specifying a range WITHOUT A DASH will create an illegal HTTP range
1577          (and won't actually be range by definition). The man page previously
1578          claimed that to be a good way, why this code is added to work-around
1579          it. */
1580       if(ISDIGIT(*nextarg) && !strchr(nextarg, '-')) {
1581         char buffer[32];
1582         curl_off_t off;
1583         warnf(config,
1584               "A specified range MUST include at least one dash (-). "
1585               "Appending one for you!\n");
1586         off = curlx_strtoofft(nextarg, NULL, 10);
1587         snprintf(buffer, sizeof(buffer), "%" CURL_FORMAT_CURL_OFF_T "-", off);
1588         Curl_safefree(config->range);
1589         config->range = strdup(buffer);
1590         if(!config->range)
1591           return PARAM_NO_MEM;
1592       }
1593       {
1594         /* byte range requested */
1595         char *tmp_range;
1596         tmp_range = nextarg;
1597         while(*tmp_range != '\0') {
1598           if(!ISDIGIT(*tmp_range) && *tmp_range != '-' && *tmp_range != ',') {
1599             warnf(config,"Invalid character is found in given range. "
1600                   "A specified range MUST have only digits in "
1601                   "\'start\'-\'stop\'. The server's response to this "
1602                   "request is uncertain.\n");
1603             break;
1604           }
1605           tmp_range++;
1606         }
1607         /* byte range requested */
1608         GetStr(&config->range, nextarg);
1609       }
1610       break;
1611     case 'R':
1612       /* use remote file's time */
1613       config->remote_time = toggle;
1614       break;
1615     case 's':
1616       /* don't show progress meter, don't show errors : */
1617       if(toggle)
1618         config->mute = config->noprogress = TRUE;
1619       else
1620         config->mute = config->noprogress = FALSE;
1621       if(config->showerror < 0)
1622         /* if still on the default value, set showerror to the reverse of
1623            toggle. This is to allow -S and -s to be used in an independent
1624            order but still have the same effect. */
1625         config->showerror = (!toggle)?TRUE:FALSE; /* toggle off */
1626       break;
1627     case 'S':
1628       /* show errors */
1629       config->showerror = toggle?1:0; /* toggle on if used with -s */
1630       break;
1631     case 't':
1632       /* Telnet options */
1633       err = add2list(&config->telnet_options, nextarg);
1634       if(err)
1635         return err;
1636       break;
1637     case 'T':
1638       /* we are uploading */
1639     {
1640       struct getout *url;
1641       if(config->url_out || ((config->url_out = config->url_list) != NULL)) {
1642         /* there's a node here, if it already is filled-in continue to find
1643            an "empty" node */
1644         while(config->url_out && (config->url_out->flags & GETOUT_UPLOAD))
1645           config->url_out = config->url_out->next;
1646       }
1647
1648       /* now there might or might not be an available node to fill in! */
1649
1650       if(config->url_out)
1651         /* existing node */
1652         url = config->url_out;
1653       else
1654         /* there was no free node, create one! */
1655         url = new_getout(config);
1656
1657       if(!url)
1658         return PARAM_NO_MEM;
1659       else {
1660         url->flags |= GETOUT_UPLOAD; /* mark -T used */
1661         if(!*nextarg)
1662           url->flags |= GETOUT_NOUPLOAD;
1663         else {
1664           /* "-" equals stdin, but keep the string around for now */
1665           GetStr(&url->infile, nextarg);
1666         }
1667       }
1668     }
1669     break;
1670     case 'u':
1671       /* user:password  */
1672       GetStr(&config->userpwd, nextarg);
1673       cleanarg(nextarg);
1674       break;
1675     case 'U':
1676       /* Proxy user:password  */
1677       GetStr(&config->proxyuserpwd, nextarg);
1678       cleanarg(nextarg);
1679       break;
1680     case 'v':
1681       if(toggle) {
1682         /* the '%' thing here will cause the trace get sent to stderr */
1683         Curl_safefree(config->trace_dump);
1684         config->trace_dump = strdup("%");
1685         if(!config->trace_dump)
1686           return PARAM_NO_MEM;
1687         if(config->tracetype && (config->tracetype != TRACE_PLAIN))
1688           warnf(config,
1689                 "-v, --verbose overrides an earlier trace/verbose option\n");
1690         config->tracetype = TRACE_PLAIN;
1691       }
1692       else
1693         /* verbose is disabled here */
1694         config->tracetype = TRACE_NONE;
1695       break;
1696     case 'V':
1697       if(toggle)    /* --no-version yields no output! */
1698         return PARAM_VERSION_INFO_REQUESTED;
1699       break;
1700
1701     case 'w':
1702       /* get the output string */
1703       if('@' == *nextarg) {
1704         /* the data begins with a '@' letter, it means that a file name
1705            or - (stdin) follows */
1706         FILE *file;
1707         const char *fname;
1708         nextarg++; /* pass the @ */
1709         if(curlx_strequal("-", nextarg)) {
1710           fname = "<stdin>";
1711           file = stdin;
1712         }
1713         else {
1714           fname = nextarg;
1715           file = fopen(nextarg, "r");
1716         }
1717         err = file2string(&config->writeout, file);
1718         if(file && (file != stdin))
1719           fclose(file);
1720         if(err)
1721           return err;
1722         if(!config->writeout)
1723           warnf(config, "Failed to read %s", fname);
1724       }
1725       else
1726         GetStr(&config->writeout, nextarg);
1727       break;
1728     case 'x':
1729       /* proxy */
1730       GetStr(&config->proxy, nextarg);
1731       config->proxyver = CURLPROXY_HTTP;
1732       break;
1733     case 'X':
1734       /* set custom request */
1735       GetStr(&config->customrequest, nextarg);
1736       break;
1737     case 'y':
1738       /* low speed time */
1739       err = str2unum(&config->low_speed_time, nextarg);
1740       if(err)
1741         return err;
1742       if(!config->low_speed_limit)
1743         config->low_speed_limit = 1;
1744       break;
1745     case 'Y':
1746       /* low speed limit */
1747       err = str2unum(&config->low_speed_limit, nextarg);
1748       if(err)
1749         return err;
1750       if(!config->low_speed_time)
1751         config->low_speed_time = 30;
1752       break;
1753     case 'z': /* time condition coming up */
1754       switch(*nextarg) {
1755       case '+':
1756         nextarg++;
1757       default:
1758         /* If-Modified-Since: (section 14.28 in RFC2068) */
1759         config->timecond = CURL_TIMECOND_IFMODSINCE;
1760         break;
1761       case '-':
1762         /* If-Unmodified-Since:  (section 14.24 in RFC2068) */
1763         config->timecond = CURL_TIMECOND_IFUNMODSINCE;
1764         nextarg++;
1765         break;
1766       case '=':
1767         /* Last-Modified:  (section 14.29 in RFC2068) */
1768         config->timecond = CURL_TIMECOND_LASTMOD;
1769         nextarg++;
1770         break;
1771       }
1772       now = time(NULL);
1773       config->condtime=curl_getdate(nextarg, &now);
1774       if(-1 == (int)config->condtime) {
1775         /* now let's see if it is a file name to get the time from instead! */
1776         struct_stat statbuf;
1777         if(-1 == stat(nextarg, &statbuf)) {
1778           /* failed, remove time condition */
1779           config->timecond = CURL_TIMECOND_NONE;
1780           warnf(config,
1781                 "Illegal date format for -z, --timecond (and not "
1782                 "a file name). Disabling time condition. "
1783                 "See curl_getdate(3) for valid date syntax.\n");
1784         }
1785         else {
1786           /* pull the time out from the file */
1787           config->condtime = statbuf.st_mtime;
1788         }
1789       }
1790       break;
1791     default: /* unknown flag */
1792       return PARAM_OPTION_UNKNOWN;
1793     }
1794     hit = -1;
1795
1796   } while(!longopt && !singleopt && *++parse && !*usedarg);
1797
1798   return PARAM_OK;
1799 }
1800
1801 ParameterError parse_args(struct OperationConfig *config, int argc,
1802                           argv_item_t argv[])
1803 {
1804   int i;
1805   bool stillflags;
1806   char *orig_opt;
1807   ParameterError result = PARAM_OK;
1808
1809   for(i = 1, stillflags = TRUE; i < argc && !result; i++) {
1810     orig_opt = argv[i];
1811
1812     if(curlx_strequal(":", argv[i]) &&
1813        (config->url_list && config->url_list->url)) {
1814
1815       /* Allocate the next config */
1816       config->next = malloc(sizeof(struct OperationConfig));
1817       if(config->next) {
1818         /* Initialise the newly created config */
1819         config_init(config->next);
1820
1821         /* Copy the easy handle */
1822         config->next->easy = config->easy;
1823
1824         /* Move onto the new config */
1825         config->next->prev = config;
1826         config = config->next;
1827
1828         /* Reset the flag indicator */
1829         stillflags = TRUE;
1830       }
1831       else
1832         result = PARAM_NO_MEM;
1833     }
1834     else if(stillflags && ('-' == argv[i][0])) {
1835       char *nextarg;
1836       bool passarg;
1837       char *flag = argv[i];
1838
1839       if(curlx_strequal("--", argv[i]))
1840         /* This indicates the end of the flags and thus enables the
1841            following (URL) argument to start with -. */
1842         stillflags = FALSE;
1843       else {
1844         nextarg = (i < (argc - 1)) ? argv[i + 1] : NULL;
1845
1846         result = getparameter(flag, nextarg, &passarg, config);
1847         if(!result && passarg)
1848           i++; /* we're supposed to skip this */
1849       }
1850     }
1851     else {
1852       bool used;
1853
1854       /* Just add the URL please */
1855       result = getparameter((char *)"--url", argv[i], &used, config);
1856     }
1857   }
1858
1859   if(result && result != PARAM_HELP_REQUESTED &&
1860      result != PARAM_MANUAL_REQUESTED &&
1861      result != PARAM_VERSION_INFO_REQUESTED &&
1862      result != PARAM_ENGINES_REQUESTED) {
1863     const char *reason = param2text(result);
1864
1865     if(!curlx_strequal(":", orig_opt))
1866       helpf(config->errors, "option %s: %s\n", orig_opt, reason);
1867     else
1868       helpf(config->errors, "%s\n", reason);
1869   }
1870
1871   return result;
1872 }