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