Imported Upstream version 7.44.0
[platform/upstream/curl.git] / src / tool_getparam.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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   {"$6", "socks5-gssapi-service",    TRUE},
162   {"$7", "socks5-gssapi-nec",        FALSE},
163   {"$O", "proxy-service-name",       TRUE},
164   {"$P", "service-name",             TRUE},
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   {"$M", "unix-socket",              TRUE},
180   {"$N", "path-as-is",               FALSE},
181   {"0",   "http1.0",                 FALSE},
182   {"01",  "http1.1",                 FALSE},
183   {"02",  "http2",                   FALSE},
184   {"1",  "tlsv1",                    FALSE},
185   {"10",  "tlsv1.0",                 FALSE},
186   {"11",  "tlsv1.1",                 FALSE},
187   {"12",  "tlsv1.2",                 FALSE},
188   {"2",  "sslv2",                    FALSE},
189   {"3",  "sslv3",                    FALSE},
190   {"4",  "ipv4",                     FALSE},
191   {"6",  "ipv6",                     FALSE},
192   {"a",  "append",                   FALSE},
193   {"A",  "user-agent",               TRUE},
194   {"b",  "cookie",                   TRUE},
195   {"B",  "use-ascii",                FALSE},
196   {"c",  "cookie-jar",               TRUE},
197   {"C",  "continue-at",              TRUE},
198   {"d",  "data",                     TRUE},
199   {"dr", "data-raw",                 TRUE},
200   {"da", "data-ascii",               TRUE},
201   {"db", "data-binary",              TRUE},
202   {"de", "data-urlencode",           TRUE},
203   {"D",  "dump-header",              TRUE},
204   {"e",  "referer",                  TRUE},
205   {"E",  "cert",                     TRUE},
206   {"Ea", "cacert",                   TRUE},
207   {"Eb", "cert-type",                TRUE},
208   {"Ec", "key",                      TRUE},
209   {"Ed", "key-type",                 TRUE},
210   {"Ee", "pass",                     TRUE},
211   {"Ef", "engine",                   TRUE},
212   {"Eg", "capath ",                  TRUE},
213   {"Eh", "pubkey",                   TRUE},
214   {"Ei", "hostpubmd5",               TRUE},
215   {"Ej", "crlfile",                  TRUE},
216   {"Ek", "tlsuser",                  TRUE},
217   {"El", "tlspassword",              TRUE},
218   {"Em", "tlsauthtype",              TRUE},
219   {"En", "ssl-allow-beast",          FALSE},
220   {"Eo", "login-options",            TRUE},
221   {"Ep", "pinnedpubkey",             TRUE},
222   {"Eq", "cert-status",              FALSE},
223   {"Er", "false-start",              FALSE},
224   {"Es", "ssl-no-revoke",            FALSE},
225   {"f",  "fail",                     FALSE},
226   {"F",  "form",                     TRUE},
227   {"Fs", "form-string",              TRUE},
228   {"g",  "globoff",                  FALSE},
229   {"G",  "get",                      FALSE},
230   {"h",  "help",                     FALSE},
231   {"H",  "header",                   TRUE},
232   {"Hp", "proxy-header",             TRUE},
233   {"i",  "include",                  FALSE},
234   {"I",  "head",                     FALSE},
235   {"j",  "junk-session-cookies",     FALSE},
236   {"J",  "remote-header-name",       FALSE},
237   {"k",  "insecure",                 FALSE},
238   {"K",  "config",                   TRUE},
239   {"l",  "list-only",                FALSE},
240   {"L",  "location",                 FALSE},
241   {"Lt", "location-trusted",         FALSE},
242   {"m",  "max-time",                 TRUE},
243   {"M",  "manual",                   FALSE},
244   {"n",  "netrc",                    FALSE},
245   {"no", "netrc-optional",           FALSE},
246   {"ne", "netrc-file",               TRUE},
247   {"N",  "buffer",                   FALSE},
248          /* 'buffer' listed as --no-buffer in the help */
249   {"o",  "output",                   TRUE},
250   {"O",  "remote-name",              FALSE},
251   {"Oa", "remote-name-all",          FALSE},
252   {"p",  "proxytunnel",              FALSE},
253   {"P",  "ftpport",                  TRUE},
254          /* 'ftpport' old version */
255   {"P",  "ftp-port",                 TRUE},
256   {"q",  "disable",                  FALSE},
257   {"Q",  "quote",                    TRUE},
258   {"r",  "range",                    TRUE},
259   {"R",  "remote-time",              FALSE},
260   {"s",  "silent",                   FALSE},
261   {"S",  "show-error",               FALSE},
262   {"t",  "telnet-options",           TRUE},
263          /* 'telnet-options' documented as telnet-option */
264   {"T",  "upload-file",              TRUE},
265   {"u",  "user",                     TRUE},
266   {"U",  "proxy-user",               TRUE},
267   {"v",  "verbose",                  FALSE},
268   {"V",  "version",                  FALSE},
269   {"w",  "write-out",                TRUE},
270   {"x",  "proxy",                    TRUE},
271   {"X",  "request",                  TRUE},
272   {"X",  "http-request",             TRUE},
273          /* 'http-request' OBSOLETE VERSION */
274   {"Y",  "speed-limit",              TRUE},
275   {"y",  "speed-time",               TRUE},
276   {"z",  "time-cond",                TRUE},
277   {"#",  "progress-bar",             FALSE},
278   {":",  "next",                     FALSE},
279   {"~",  "xattr",                    FALSE},
280 };
281
282 /* Split the argument of -E to 'certname' and 'passphrase' separated by colon.
283  * We allow ':' and '\' to be escaped by '\' so that we can use certificate
284  * nicknames containing ':'.  See <https://sourceforge.net/p/curl/bugs/1196/>
285  * for details. */
286 #ifndef UNITTESTS
287 static
288 #endif
289 void parse_cert_parameter(const char *cert_parameter,
290                           char **certname,
291                           char **passphrase)
292 {
293   size_t param_length = strlen(cert_parameter);
294   size_t span;
295   const char *param_place = NULL;
296   char *certname_place = NULL;
297   *certname = NULL;
298   *passphrase = NULL;
299
300   /* most trivial assumption: cert_parameter is empty */
301   if(param_length == 0)
302     return;
303
304   /* next less trivial: cert_parameter contains no colon nor backslash; this
305    * means no passphrase was given and no characters escaped */
306   if(!strpbrk(cert_parameter, ":\\")) {
307     *certname = strdup(cert_parameter);
308     return;
309   }
310   /* deal with escaped chars; find unescaped colon if it exists */
311   certname_place = malloc(param_length + 1);
312   if(!certname_place)
313     return;
314
315   *certname = certname_place;
316   param_place = cert_parameter;
317   while(*param_place) {
318     span = strcspn(param_place, ":\\");
319     strncpy(certname_place, param_place, span);
320     param_place += span;
321     certname_place += span;
322     /* we just ate all the non-special chars. now we're on either a special
323      * char or the end of the string. */
324     switch(*param_place) {
325     case '\0':
326       break;
327     case '\\':
328       param_place++;
329       switch(*param_place) {
330         case '\0':
331           *certname_place++ = '\\';
332           break;
333         case '\\':
334           *certname_place++ = '\\';
335           param_place++;
336           break;
337         case ':':
338           *certname_place++ = ':';
339           param_place++;
340           break;
341         default:
342           *certname_place++ = '\\';
343           *certname_place++ = *param_place;
344           param_place++;
345           break;
346       }
347       break;
348     case ':':
349       /* Since we live in a world of weirdness and confusion, the win32
350          dudes can use : when using drive letters and thus c:\file:password
351          needs to work. In order not to break compatibility, we still use : as
352          separator, but we try to detect when it is used for a file name! On
353          windows. */
354 #ifdef WIN32
355       if(param_place &&
356           (param_place == &cert_parameter[1]) &&
357           (cert_parameter[2] == '\\' || cert_parameter[2] == '/') &&
358           (ISALPHA(cert_parameter[0])) ) {
359         /* colon in the second column, followed by a backslash, and the
360            first character is an alphabetic letter:
361
362            this is a drive letter colon */
363         *certname_place++ = ':';
364         param_place++;
365         break;
366       }
367 #endif
368       /* escaped colons and Windows drive letter colons were handled
369        * above; if we're still here, this is a separating colon */
370       param_place++;
371       if(strlen(param_place) > 0) {
372         *passphrase = strdup(param_place);
373       }
374       goto done;
375     }
376   }
377 done:
378   *certname_place = '\0';
379 }
380
381 ParameterError getparameter(char *flag,    /* f or -long-flag */
382                             char *nextarg, /* NULL if unset */
383                             bool *usedarg, /* set to TRUE if the arg
384                                               has been used */
385                             struct GlobalConfig *global,
386                             struct OperationConfig *config)
387 {
388   char letter;
389   char subletter = '\0'; /* subletters can only occur on long options */
390   int rc;
391   const char *parse = NULL;
392   unsigned int j;
393   time_t now;
394   int hit = -1;
395   bool longopt = FALSE;
396   bool singleopt = FALSE; /* when true means '-o foo' used '-ofoo' */
397   ParameterError err;
398   bool toggle = TRUE; /* how to switch boolean options, on or off. Controlled
399                          by using --OPTION or --no-OPTION */
400
401
402   if(('-' != flag[0]) ||
403      (('-' == flag[0]) && ('-' == flag[1]))) {
404     /* this should be a long name */
405     char *word = ('-' == flag[0]) ? flag+2 : flag;
406     size_t fnam = strlen(word);
407     int numhits = 0;
408
409     if(!strncmp(word, "no-", 3)) {
410       /* disable this option but ignore the "no-" part when looking for it */
411       word += 3;
412       toggle = FALSE;
413     }
414
415     for(j = 0; j < sizeof(aliases)/sizeof(aliases[0]); j++) {
416       if(curlx_strnequal(aliases[j].lname, word, fnam)) {
417         longopt = TRUE;
418         numhits++;
419         if(curlx_raw_equal(aliases[j].lname, word)) {
420           parse = aliases[j].letter;
421           hit = j;
422           numhits = 1; /* a single unique hit */
423           break;
424         }
425         parse = aliases[j].letter;
426         hit = j;
427       }
428     }
429     if(numhits > 1) {
430       /* this is at least the second match! */
431       return PARAM_OPTION_AMBIGUOUS;
432     }
433     if(hit < 0) {
434       return PARAM_OPTION_UNKNOWN;
435     }
436   }
437   else {
438     flag++; /* prefixed with one dash, pass it */
439     hit = -1;
440     parse = flag;
441   }
442
443   do {
444     /* we can loop here if we have multiple single-letters */
445
446     if(!longopt) {
447       letter = (char)*parse;
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(&global->trace_dump, nextarg);
529         if(global->tracetype && (global->tracetype != TRACE_BIN))
530           warnf(global, "--trace overrides an earlier trace/verbose option\n");
531         global->tracetype = TRACE_BIN;
532         break;
533       case 'G': /* --npn */
534         config->nonpn = (!toggle)?TRUE:FALSE;
535         break;
536       case 'h': /* --trace-ascii */
537         GetStr(&global->trace_dump, nextarg);
538         if(global->tracetype && (global->tracetype != TRACE_ASCII))
539           warnf(global,
540                 "--trace-ascii overrides an earlier trace/verbose option\n");
541         global->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(global, "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_SPNEGO)
603             config->authtype |= CURLAUTH_NEGOTIATE;
604           else
605             return PARAM_LIBCURL_DOESNT_SUPPORT;
606         }
607         else
608           config->authtype &= ~CURLAUTH_NEGOTIATE;
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, FOPEN_WRITETEXT);
684           if(!newfile)
685             warnf(global, "Failed to open %s!\n", nextarg);
686           else {
687             if(global->errors_fopened)
688               fclose(global->errors);
689             global->errors = newfile;
690             global->errors_fopened = TRUE;
691           }
692         }
693         else
694           global->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           GetStr(&config->krblevel, nextarg);
704         else
705           return PARAM_LIBCURL_DOESNT_SUPPORT;
706         break;
707       case 'y': /* --max-filesize */
708         err = str2offset(&config->max_filesize, nextarg);
709         if(err)
710           return err;
711         break;
712       case 'z': /* --disable-eprt */
713         config->disable_eprt = toggle;
714         break;
715       case 'Z': /* --eprt */
716         config->disable_eprt = (!toggle)?TRUE:FALSE;
717         break;
718
719       default: /* the URL! */
720       {
721         struct getout *url;
722         if(config->url_get || ((config->url_get = config->url_list) != NULL)) {
723           /* there's a node here, if it already is filled-in continue to find
724              an "empty" node */
725           while(config->url_get && (config->url_get->flags & GETOUT_URL))
726             config->url_get = config->url_get->next;
727         }
728
729         /* now there might or might not be an available node to fill in! */
730
731         if(config->url_get)
732           /* existing node */
733           url = config->url_get;
734         else
735           /* there was no free node, create one! */
736           url = new_getout(config);
737
738         if(!url)
739           return PARAM_NO_MEM;
740         else {
741           /* fill in the URL */
742           GetStr(&url->url, nextarg);
743           url->flags |= GETOUT_URL;
744         }
745       }
746       }
747       break;
748     case '$': /* more options without a short option */
749       switch(subletter) {
750       case 'a': /* --ftp-ssl */
751         if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
752           return PARAM_LIBCURL_DOESNT_SUPPORT;
753         config->ftp_ssl = toggle;
754         break;
755       case 'b': /* --ftp-pasv */
756         Curl_safefree(config->ftpport);
757         break;
758       case 'c': /* --socks5 specifies a socks5 proxy to use, and resolves
759                    the name locally and passes on the resolved address */
760         GetStr(&config->socksproxy, nextarg);
761         config->socksver = CURLPROXY_SOCKS5;
762         break;
763       case 't': /* --socks4 specifies a socks4 proxy to use */
764         GetStr(&config->socksproxy, nextarg);
765         config->socksver = CURLPROXY_SOCKS4;
766         break;
767       case 'T': /* --socks4a specifies a socks4a proxy to use */
768         GetStr(&config->socksproxy, nextarg);
769         config->socksver = CURLPROXY_SOCKS4A;
770         break;
771       case '2': /* --socks5-hostname specifies a socks5 proxy and enables name
772                    resolving with the proxy */
773         GetStr(&config->socksproxy, nextarg);
774         config->socksver = CURLPROXY_SOCKS5_HOSTNAME;
775         break;
776       case 'd': /* --tcp-nodelay option */
777         config->tcp_nodelay = toggle;
778         break;
779       case 'e': /* --proxy-digest */
780         config->proxydigest = toggle;
781         break;
782       case 'f': /* --proxy-basic */
783         config->proxybasic = toggle;
784         break;
785       case 'g': /* --retry */
786         err = str2unum(&config->req_retry, nextarg);
787         if(err)
788           return err;
789         break;
790       case 'h': /* --retry-delay */
791         err = str2unum(&config->retry_delay, nextarg);
792         if(err)
793           return err;
794         break;
795       case 'i': /* --retry-max-time */
796         err = str2unum(&config->retry_maxtime, nextarg);
797         if(err)
798           return err;
799         break;
800
801       case 'k': /* --proxy-negotiate */
802         if(curlinfo->features & CURL_VERSION_SPNEGO)
803           config->proxynegotiate = toggle;
804         else
805           return PARAM_LIBCURL_DOESNT_SUPPORT;
806         break;
807
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         global->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(global, "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(global,
870               "--libcurl option was disabled at build-time!\n");
871         return PARAM_OPTION_UNKNOWN;
872 #else
873         GetStr(&global->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       case '6': /* --socks5-gssapi-service */
901         GetStr(&config->socks5_gssapi_service, nextarg);
902         break;
903       case '7': /* --socks5-gssapi-nec*/
904         config->socks5_gssapi_nec = toggle;
905         break;
906       case 'O': /* --proxy-service-name */
907         GetStr(&config->proxy_service_name, nextarg);
908         break;
909       case 'P': /* --service-name */
910         GetStr(&config->service_name, nextarg);
911         break;
912       case '8': /* --proxy1.0 */
913         /* http 1.0 proxy */
914         GetStr(&config->proxy, nextarg);
915         config->proxyver = CURLPROXY_HTTP_1_0;
916         break;
917       case '9': /* --tftp-blksize */
918         err = str2unum(&config->tftp_blksize, nextarg);
919         if(err)
920           return err;
921         break;
922       case 'A': /* --mail-from */
923         GetStr(&config->mail_from, nextarg);
924         break;
925       case 'B': /* --mail-rcpt */
926         /* append receiver to a list */
927         err = add2list(&config->mail_rcpt, nextarg);
928         if(err)
929           return err;
930         break;
931       case 'C': /* --ftp-pret */
932         config->ftp_pret = toggle;
933         break;
934       case 'D': /* --proto */
935         config->proto_present = TRUE;
936         if(proto2num(config, &config->proto, nextarg))
937           return PARAM_BAD_USE;
938         break;
939       case 'E': /* --proto-redir */
940         config->proto_redir_present = TRUE;
941         if(proto2num(config, &config->proto_redir, nextarg))
942           return PARAM_BAD_USE;
943         break;
944       case 'F': /* --resolve */
945         err = add2list(&config->resolve, nextarg);
946         if(err)
947           return err;
948         break;
949       case 'G': /* --delegation LEVEL */
950         config->gssapi_delegation = delegation(config, nextarg);
951         break;
952       case 'H': /* --mail-auth */
953         GetStr(&config->mail_auth, nextarg);
954         break;
955       case 'J': /* --metalink */
956         {
957 #ifdef USE_METALINK
958           int mlmaj, mlmin, mlpatch;
959           metalink_get_version(&mlmaj, &mlmin, &mlpatch);
960           if((mlmaj*10000)+(mlmin*100)+mlpatch < CURL_REQ_LIBMETALINK_VERS) {
961             warnf(global,
962                   "--metalink option cannot be used because the version of "
963                   "the linked libmetalink library is too old. "
964                   "Required: %d.%d.%d, found %d.%d.%d\n",
965                   CURL_REQ_LIBMETALINK_MAJOR,
966                   CURL_REQ_LIBMETALINK_MINOR,
967                   CURL_REQ_LIBMETALINK_PATCH,
968                   mlmaj, mlmin, mlpatch);
969             return PARAM_BAD_USE;
970           }
971           else
972             config->use_metalink = toggle;
973 #else
974           warnf(global, "--metalink option is ignored because the binary is "
975                 "built without the Metalink support.\n");
976 #endif
977           break;
978         }
979       case 'K': /* --sasl-ir */
980         config->sasl_ir = toggle;
981         break;
982       case 'L': /* --test-event */
983 #ifdef CURLDEBUG
984         config->test_event_based = toggle;
985 #else
986         warnf(global, "--test-event is ignored unless a debug build!\n");
987 #endif
988         break;
989       case 'M': /* --unix-socket */
990         GetStr(&config->unix_socket_path, nextarg);
991         break;
992       case 'N': /* --path-as-is */
993         config->path_as_is = toggle;
994         break;
995       }
996       break;
997     case '#': /* --progress-bar */
998       if(toggle)
999         global->progressmode = CURL_PROGRESS_BAR;
1000       else
1001         global->progressmode = CURL_PROGRESS_STATS;
1002       break;
1003     case ':': /* --next */
1004       return PARAM_NEXT_OPERATION;
1005     case '~': /* --xattr */
1006       config->xattr = toggle;
1007       break;
1008     case '0': /* --http* options */
1009       switch(subletter) {
1010       case '\0':
1011         /* HTTP version 1.0 */
1012         config->httpversion = CURL_HTTP_VERSION_1_0;
1013         break;
1014       case '1':
1015         /* HTTP version 1.1 */
1016         config->httpversion = CURL_HTTP_VERSION_1_1;
1017         break;
1018       case '2':
1019         /* HTTP version 2.0 */
1020         config->httpversion = CURL_HTTP_VERSION_2_0;
1021         break;
1022       }
1023       break;
1024     case '1': /* --tlsv1* options */
1025       switch(subletter) {
1026       case '\0':
1027         /* TLS version 1.x */
1028         config->ssl_version = CURL_SSLVERSION_TLSv1;
1029         break;
1030       case '0':
1031         /* TLS version 1.0 */
1032         config->ssl_version = CURL_SSLVERSION_TLSv1_0;
1033         break;
1034       case '1':
1035         /* TLS version 1.1 */
1036         config->ssl_version = CURL_SSLVERSION_TLSv1_1;
1037         break;
1038       case '2':
1039         /* TLS version 1.2 */
1040         config->ssl_version = CURL_SSLVERSION_TLSv1_2;
1041         break;
1042       }
1043       break;
1044     case '2':
1045       /* SSL version 2 */
1046       config->ssl_version = CURL_SSLVERSION_SSLv2;
1047       break;
1048     case '3':
1049       /* SSL version 3 */
1050       config->ssl_version = CURL_SSLVERSION_SSLv3;
1051       break;
1052     case '4':
1053       /* IPv4 */
1054       config->ip_version = 4;
1055       break;
1056     case '6':
1057       /* IPv6 */
1058       config->ip_version = 6;
1059       break;
1060     case 'a':
1061       /* This makes the FTP sessions use APPE instead of STOR */
1062       config->ftp_append = toggle;
1063       break;
1064     case 'A':
1065       /* This specifies the User-Agent name */
1066       GetStr(&config->useragent, nextarg);
1067       break;
1068     case 'b': /* cookie string coming up: */
1069       if(nextarg[0] == '@') {
1070         nextarg++;
1071       }
1072       else if(strchr(nextarg, '=')) {
1073         /* A cookie string must have a =-letter */
1074         GetStr(&config->cookie, nextarg);
1075         break;
1076       }
1077       /* We have a cookie file to read from! */
1078       GetStr(&config->cookiefile, nextarg);
1079       break;
1080     case 'B':
1081       /* use ASCII/text when transferring */
1082       config->use_ascii = toggle;
1083       break;
1084     case 'c':
1085       /* get the file name to dump all cookies in */
1086       GetStr(&config->cookiejar, nextarg);
1087       break;
1088     case 'C':
1089       /* This makes us continue an ftp transfer at given position */
1090       if(!curlx_strequal(nextarg, "-")) {
1091         err = str2offset(&config->resume_from, nextarg);
1092         if(err)
1093           return err;
1094         config->resume_from_current = FALSE;
1095       }
1096       else {
1097         config->resume_from_current = TRUE;
1098         config->resume_from = 0;
1099       }
1100       config->use_resume=TRUE;
1101       break;
1102     case 'd':
1103       /* postfield data */
1104     {
1105       char *postdata = NULL;
1106       FILE *file;
1107       size_t size = 0;
1108       bool raw_mode = (subletter == 'r');
1109
1110       if(subletter == 'e') { /* --data-urlencode*/
1111         /* [name]=[content], we encode the content part only
1112          * [name]@[file name]
1113          *
1114          * Case 2: we first load the file using that name and then encode
1115          * the content.
1116          */
1117         const char *p = strchr(nextarg, '=');
1118         size_t nlen;
1119         char is_file;
1120         if(!p)
1121           /* there was no '=' letter, check for a '@' instead */
1122           p = strchr(nextarg, '@');
1123         if(p) {
1124           nlen = p - nextarg; /* length of the name part */
1125           is_file = *p++; /* pass the separator */
1126         }
1127         else {
1128           /* neither @ nor =, so no name and it isn't a file */
1129           nlen = is_file = 0;
1130           p = nextarg;
1131         }
1132         if('@' == is_file) {
1133           /* a '@' letter, it means that a file name or - (stdin) follows */
1134           if(curlx_strequal("-", p)) {
1135             file = stdin;
1136             set_binmode(stdin);
1137           }
1138           else {
1139             file = fopen(p, "rb");
1140             if(!file)
1141               warnf(global,
1142                     "Couldn't read data from file \"%s\", this makes "
1143                     "an empty POST.\n", nextarg);
1144           }
1145
1146           err = file2memory(&postdata, &size, file);
1147
1148           if(file && (file != stdin))
1149             fclose(file);
1150           if(err)
1151             return err;
1152         }
1153         else {
1154           GetStr(&postdata, p);
1155           if(postdata)
1156             size = strlen(postdata);
1157         }
1158
1159         if(!postdata) {
1160           /* no data from the file, point to a zero byte string to make this
1161              get sent as a POST anyway */
1162           postdata = strdup("");
1163           if(!postdata)
1164             return PARAM_NO_MEM;
1165           size = 0;
1166         }
1167         else {
1168           char *enc = curl_easy_escape(config->easy, postdata, (int)size);
1169           Curl_safefree(postdata); /* no matter if it worked or not */
1170           if(enc) {
1171             /* now make a string with the name from above and append the
1172                encoded string */
1173             size_t outlen = nlen + strlen(enc) + 2;
1174             char *n = malloc(outlen);
1175             if(!n) {
1176               curl_free(enc);
1177               return PARAM_NO_MEM;
1178             }
1179             if(nlen > 0) { /* only append '=' if we have a name */
1180               snprintf(n, outlen, "%.*s=%s", nlen, nextarg, enc);
1181               size = outlen-1;
1182             }
1183             else {
1184               strcpy(n, enc);
1185               size = outlen-2; /* since no '=' was inserted */
1186             }
1187             curl_free(enc);
1188             postdata = n;
1189           }
1190           else
1191             return PARAM_NO_MEM;
1192         }
1193       }
1194       else if('@' == *nextarg && !raw_mode) {
1195         /* the data begins with a '@' letter, it means that a file name
1196            or - (stdin) follows */
1197         nextarg++; /* pass the @ */
1198
1199         if(curlx_strequal("-", nextarg)) {
1200           file = stdin;
1201           if(subletter == 'b') /* forced data-binary */
1202             set_binmode(stdin);
1203         }
1204         else {
1205           file = fopen(nextarg, "rb");
1206           if(!file)
1207             warnf(global, "Couldn't read data from file \"%s\", this makes "
1208                   "an empty POST.\n", nextarg);
1209         }
1210
1211         if(subletter == 'b')
1212           /* forced binary */
1213           err = file2memory(&postdata, &size, file);
1214         else {
1215           err = file2string(&postdata, file);
1216           if(postdata)
1217             size = strlen(postdata);
1218         }
1219
1220         if(file && (file != stdin))
1221           fclose(file);
1222         if(err)
1223           return err;
1224
1225         if(!postdata) {
1226           /* no data from the file, point to a zero byte string to make this
1227              get sent as a POST anyway */
1228           postdata = strdup("");
1229           if(!postdata)
1230             return PARAM_NO_MEM;
1231         }
1232       }
1233       else {
1234         GetStr(&postdata, nextarg);
1235         if(postdata)
1236           size = strlen(postdata);
1237       }
1238
1239 #ifdef CURL_DOES_CONVERSIONS
1240       if(subletter != 'b') {
1241         /* NOT forced binary, convert to ASCII */
1242         if(convert_to_network(postdata, strlen(postdata))) {
1243           Curl_safefree(postdata);
1244           return PARAM_NO_MEM;
1245         }
1246       }
1247 #endif
1248
1249       if(config->postfields) {
1250         /* we already have a string, we append this one with a separating
1251            &-letter */
1252         char *oldpost = config->postfields;
1253         curl_off_t oldlen = config->postfieldsize;
1254         curl_off_t newlen = oldlen + curlx_uztoso(size) + 2;
1255         config->postfields = malloc((size_t)newlen);
1256         if(!config->postfields) {
1257           Curl_safefree(oldpost);
1258           Curl_safefree(postdata);
1259           return PARAM_NO_MEM;
1260         }
1261         memcpy(config->postfields, oldpost, (size_t)oldlen);
1262         /* use byte value 0x26 for '&' to accommodate non-ASCII platforms */
1263         config->postfields[oldlen] = '\x26';
1264         memcpy(&config->postfields[oldlen+1], postdata, size);
1265         config->postfields[oldlen+1+size] = '\0';
1266         Curl_safefree(oldpost);
1267         Curl_safefree(postdata);
1268         config->postfieldsize += size+1;
1269       }
1270       else {
1271         config->postfields = postdata;
1272         config->postfieldsize = curlx_uztoso(size);
1273       }
1274     }
1275     /*
1276       We can't set the request type here, as this data might be used in
1277       a simple GET if -G is used. Already or soon.
1278
1279       if(SetHTTPrequest(HTTPREQ_SIMPLEPOST, &config->httpreq)) {
1280         Curl_safefree(postdata);
1281         return PARAM_BAD_USE;
1282       }
1283     */
1284     break;
1285     case 'D':
1286       /* dump-header to given file name */
1287       GetStr(&config->headerfile, nextarg);
1288       break;
1289     case 'e':
1290     {
1291       char *ptr = strstr(nextarg, ";auto");
1292       if(ptr) {
1293         /* Automatic referer requested, this may be combined with a
1294            set initial one */
1295         config->autoreferer = TRUE;
1296         *ptr = 0; /* zero terminate here */
1297       }
1298       else
1299         config->autoreferer = FALSE;
1300       GetStr(&config->referer, nextarg);
1301     }
1302     break;
1303     case 'E':
1304       switch(subletter) {
1305       case 'a': /* CA info PEM file */
1306         /* CA info PEM file */
1307         GetStr(&config->cacert, nextarg);
1308         break;
1309       case 'b': /* cert file type */
1310         GetStr(&config->cert_type, nextarg);
1311         break;
1312       case 'c': /* private key file */
1313         GetStr(&config->key, nextarg);
1314         break;
1315       case 'd': /* private key file type */
1316         GetStr(&config->key_type, nextarg);
1317         break;
1318       case 'e': /* private key passphrase */
1319         GetStr(&config->key_passwd, nextarg);
1320         cleanarg(nextarg);
1321         break;
1322       case 'f': /* crypto engine */
1323         GetStr(&config->engine, nextarg);
1324         if(config->engine && curlx_raw_equal(config->engine, "list"))
1325           return PARAM_ENGINES_REQUESTED;
1326         break;
1327       case 'g': /* CA info PEM file */
1328         /* CA cert directory */
1329         GetStr(&config->capath, nextarg);
1330         break;
1331       case 'h': /* --pubkey public key file */
1332         GetStr(&config->pubkey, nextarg);
1333         break;
1334       case 'i': /* --hostpubmd5 md5 of the host public key */
1335         GetStr(&config->hostpubmd5, nextarg);
1336         if(!config->hostpubmd5 || strlen(config->hostpubmd5) != 32)
1337           return PARAM_BAD_USE;
1338         break;
1339       case 'j': /* CRL info PEM file */
1340         /* CRL file */
1341         GetStr(&config->crlfile, nextarg);
1342         break;
1343       case 'k': /* TLS username */
1344         if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
1345           GetStr(&config->tls_username, nextarg);
1346         else
1347           return PARAM_LIBCURL_DOESNT_SUPPORT;
1348         break;
1349       case 'l': /* TLS password */
1350         if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
1351           GetStr(&config->tls_password, nextarg);
1352         else
1353           return PARAM_LIBCURL_DOESNT_SUPPORT;
1354         break;
1355       case 'm': /* TLS authentication type */
1356         if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
1357           GetStr(&config->tls_authtype, nextarg);
1358           if(!strequal(config->tls_authtype, "SRP"))
1359             return PARAM_LIBCURL_DOESNT_SUPPORT; /* only support TLS-SRP */
1360         }
1361         else
1362           return PARAM_LIBCURL_DOESNT_SUPPORT;
1363         break;
1364       case 'n': /* no empty SSL fragments, --ssl-allow-beast */
1365         if(curlinfo->features & CURL_VERSION_SSL)
1366           config->ssl_allow_beast = toggle;
1367         break;
1368
1369       case 'o': /* --login-options */
1370         GetStr(&config->login_options, nextarg);
1371         break;
1372
1373       case 'p': /* Pinned public key DER file */
1374         /* Pinned public key DER file */
1375         GetStr(&config->pinnedpubkey, nextarg);
1376         break;
1377
1378       case 'q': /* --cert-status */
1379         config->verifystatus = TRUE;
1380         break;
1381
1382       case 'r': /* --false-start */
1383         config->falsestart = TRUE;
1384         break;
1385
1386       case 's': /* --ssl-no-revoke */
1387         if(curlinfo->features & CURL_VERSION_SSL)
1388           config->ssl_no_revoke = TRUE;
1389         break;
1390
1391       default: /* certificate file */
1392       {
1393         char *certname, *passphrase;
1394         parse_cert_parameter(nextarg, &certname, &passphrase);
1395         Curl_safefree(config->cert);
1396         config->cert = certname;
1397         if(passphrase) {
1398           Curl_safefree(config->key_passwd);
1399           config->key_passwd = passphrase;
1400         }
1401         cleanarg(nextarg);
1402       }
1403       }
1404       break;
1405     case 'f':
1406       /* fail hard on errors  */
1407       config->failonerror = toggle;
1408       break;
1409     case 'F':
1410       /* "form data" simulation, this is a little advanced so lets do our best
1411          to sort this out slowly and carefully */
1412       if(formparse(config,
1413                    nextarg,
1414                    &config->httppost,
1415                    &config->last_post,
1416                    (subletter=='s')?TRUE:FALSE)) /* 's' means literal string */
1417         return PARAM_BAD_USE;
1418       if(SetHTTPrequest(config, HTTPREQ_POST, &config->httpreq))
1419         return PARAM_BAD_USE;
1420       break;
1421
1422     case 'g': /* g disables URLglobbing */
1423       config->globoff = toggle;
1424       break;
1425
1426     case 'G': /* HTTP GET */
1427       config->use_httpget = TRUE;
1428       break;
1429
1430     case 'h': /* h for help */
1431       if(toggle) {
1432         return PARAM_HELP_REQUESTED;
1433       }
1434       /* we now actually support --no-help too! */
1435       break;
1436     case 'H':
1437       /* A custom header to append to a list */
1438       if(subletter == 'p') /* --proxy-header */
1439         err = add2list(&config->proxyheaders, nextarg);
1440       else
1441         err = add2list(&config->headers, nextarg);
1442       if(err)
1443         return err;
1444       break;
1445     case 'i':
1446       config->include_headers = toggle; /* include the headers as well in the
1447                                            general output stream */
1448       break;
1449     case 'j':
1450       config->cookiesession = toggle;
1451       break;
1452     case 'I':
1453       /*
1454        * no_body will imply include_headers later on
1455        */
1456       config->no_body = toggle;
1457       if(SetHTTPrequest(config,
1458                         (config->no_body)?HTTPREQ_HEAD:HTTPREQ_GET,
1459                         &config->httpreq))
1460         return PARAM_BAD_USE;
1461       break;
1462     case 'J': /* --remote-header-name */
1463       if(config->include_headers) {
1464         warnf(global,
1465               "--include and --remote-header-name cannot be combined.\n");
1466         return PARAM_BAD_USE;
1467       }
1468       config->content_disposition = toggle;
1469       break;
1470     case 'k': /* allow insecure SSL connects */
1471       config->insecure_ok = toggle;
1472       break;
1473     case 'K': /* parse config file */
1474       if(parseconfig(nextarg, global))
1475         warnf(global, "error trying read config from the '%s' file\n",
1476               nextarg);
1477       break;
1478     case 'l':
1479       config->dirlistonly = toggle; /* only list the names of the FTP dir */
1480       break;
1481     case 'L':
1482       config->followlocation = toggle; /* Follow Location: HTTP headers */
1483       switch (subletter) {
1484       case 't':
1485         /* Continue to send authentication (user+password) when following
1486          * locations, even when hostname changed */
1487         config->unrestricted_auth = toggle;
1488         break;
1489       }
1490       break;
1491     case 'm':
1492       /* specified max time */
1493       err = str2udouble(&config->timeout, nextarg);
1494       if(err)
1495         return err;
1496       break;
1497     case 'M': /* M for manual, huge help */
1498       if(toggle) { /* --no-manual shows no manual... */
1499 #ifdef USE_MANUAL
1500         return PARAM_MANUAL_REQUESTED;
1501 #else
1502         warnf(global,
1503               "built-in manual was disabled at build-time!\n");
1504         return PARAM_OPTION_UNKNOWN;
1505 #endif
1506       }
1507       break;
1508     case 'n':
1509       switch(subletter) {
1510       case 'o': /* CA info PEM file */
1511         /* use .netrc or URL */
1512         config->netrc_opt = toggle;
1513         break;
1514       case 'e': /* netrc-file */
1515         GetStr(&config->netrc_file, nextarg);
1516         break;
1517       default:
1518         /* pick info from .netrc, if this is used for http, curl will
1519            automatically enfore user+password with the request */
1520         config->netrc = toggle;
1521         break;
1522       }
1523       break;
1524     case 'N':
1525       /* disable the output I/O buffering. note that the option is called
1526          --buffer but is mostly used in the negative form: --no-buffer */
1527       if(longopt)
1528         config->nobuffer = (!toggle)?TRUE:FALSE;
1529       else
1530         config->nobuffer = toggle;
1531       break;
1532     case 'O': /* --remote-name */
1533       if(subletter == 'a') { /* --remote-name-all */
1534         config->default_node_flags = toggle?GETOUT_USEREMOTE:0;
1535         break;
1536       }
1537       /* fall-through! */
1538     case 'o': /* --output */
1539       /* output file */
1540     {
1541       struct getout *url;
1542       if(config->url_out || ((config->url_out = config->url_list) != NULL)) {
1543         /* there's a node here, if it already is filled-in continue to find
1544            an "empty" node */
1545         while(config->url_out && (config->url_out->flags & GETOUT_OUTFILE))
1546           config->url_out = config->url_out->next;
1547       }
1548
1549       /* now there might or might not be an available node to fill in! */
1550
1551       if(config->url_out)
1552         /* existing node */
1553         url = config->url_out;
1554       else
1555         /* there was no free node, create one! */
1556         url = new_getout(config);
1557
1558       if(!url)
1559         return PARAM_NO_MEM;
1560       else {
1561         /* fill in the outfile */
1562         if('o' == letter) {
1563           GetStr(&url->outfile, nextarg);
1564           url->flags &= ~GETOUT_USEREMOTE; /* switch off */
1565         }
1566         else {
1567           url->outfile = NULL; /* leave it */
1568           if(toggle)
1569             url->flags |= GETOUT_USEREMOTE;  /* switch on */
1570           else
1571             url->flags &= ~GETOUT_USEREMOTE; /* switch off */
1572         }
1573         url->flags |= GETOUT_OUTFILE;
1574       }
1575     }
1576     break;
1577     case 'P':
1578       /* This makes the FTP sessions use PORT instead of PASV */
1579       /* use <eth0> or <192.168.10.10> style addresses. Anything except
1580          this will make us try to get the "default" address.
1581          NOTE: this is a changed behaviour since the released 4.1!
1582       */
1583       GetStr(&config->ftpport, nextarg);
1584       break;
1585     case 'p':
1586       /* proxy tunnel for non-http protocols */
1587       config->proxytunnel = toggle;
1588       break;
1589
1590     case 'q': /* if used first, already taken care of, we do it like
1591                  this so we don't cause an error! */
1592       break;
1593     case 'Q':
1594       /* QUOTE command to send to FTP server */
1595       switch(nextarg[0]) {
1596       case '-':
1597         /* prefixed with a dash makes it a POST TRANSFER one */
1598         nextarg++;
1599         err = add2list(&config->postquote, nextarg);
1600         break;
1601       case '+':
1602         /* prefixed with a plus makes it a just-before-transfer one */
1603         nextarg++;
1604         err = add2list(&config->prequote, nextarg);
1605         break;
1606       default:
1607         err = add2list(&config->quote, nextarg);
1608         break;
1609       }
1610       if(err)
1611         return err;
1612       break;
1613     case 'r':
1614       /* Specifying a range WITHOUT A DASH will create an illegal HTTP range
1615          (and won't actually be range by definition). The man page previously
1616          claimed that to be a good way, why this code is added to work-around
1617          it. */
1618       if(ISDIGIT(*nextarg) && !strchr(nextarg, '-')) {
1619         char buffer[32];
1620         curl_off_t off;
1621         warnf(global,
1622               "A specified range MUST include at least one dash (-). "
1623               "Appending one for you!\n");
1624         off = curlx_strtoofft(nextarg, NULL, 10);
1625         snprintf(buffer, sizeof(buffer), "%" CURL_FORMAT_CURL_OFF_T "-", off);
1626         Curl_safefree(config->range);
1627         config->range = strdup(buffer);
1628         if(!config->range)
1629           return PARAM_NO_MEM;
1630       }
1631       {
1632         /* byte range requested */
1633         char *tmp_range;
1634         tmp_range = nextarg;
1635         while(*tmp_range != '\0') {
1636           if(!ISDIGIT(*tmp_range) && *tmp_range != '-' && *tmp_range != ',') {
1637             warnf(global, "Invalid character is found in given range. "
1638                   "A specified range MUST have only digits in "
1639                   "\'start\'-\'stop\'. The server's response to this "
1640                   "request is uncertain.\n");
1641             break;
1642           }
1643           tmp_range++;
1644         }
1645         /* byte range requested */
1646         GetStr(&config->range, nextarg);
1647       }
1648       break;
1649     case 'R':
1650       /* use remote file's time */
1651       config->remote_time = toggle;
1652       break;
1653     case 's':
1654       /* don't show progress meter, don't show errors : */
1655       if(toggle)
1656         global->mute = global->noprogress = TRUE;
1657       else
1658         global->mute = global->noprogress = FALSE;
1659       if(global->showerror < 0)
1660         /* if still on the default value, set showerror to the reverse of
1661            toggle. This is to allow -S and -s to be used in an independent
1662            order but still have the same effect. */
1663         global->showerror = (!toggle)?TRUE:FALSE; /* toggle off */
1664       break;
1665     case 'S':
1666       /* show errors */
1667       global->showerror = toggle?1:0; /* toggle on if used with -s */
1668       break;
1669     case 't':
1670       /* Telnet options */
1671       err = add2list(&config->telnet_options, nextarg);
1672       if(err)
1673         return err;
1674       break;
1675     case 'T':
1676       /* we are uploading */
1677     {
1678       struct getout *url;
1679       if(config->url_out || ((config->url_out = config->url_list) != NULL)) {
1680         /* there's a node here, if it already is filled-in continue to find
1681            an "empty" node */
1682         while(config->url_out && (config->url_out->flags & GETOUT_UPLOAD))
1683           config->url_out = config->url_out->next;
1684       }
1685
1686       /* now there might or might not be an available node to fill in! */
1687
1688       if(config->url_out)
1689         /* existing node */
1690         url = config->url_out;
1691       else
1692         /* there was no free node, create one! */
1693         url = new_getout(config);
1694
1695       if(!url)
1696         return PARAM_NO_MEM;
1697       else {
1698         url->flags |= GETOUT_UPLOAD; /* mark -T used */
1699         if(!*nextarg)
1700           url->flags |= GETOUT_NOUPLOAD;
1701         else {
1702           /* "-" equals stdin, but keep the string around for now */
1703           GetStr(&url->infile, nextarg);
1704         }
1705       }
1706     }
1707     break;
1708     case 'u':
1709       /* user:password  */
1710       GetStr(&config->userpwd, nextarg);
1711       cleanarg(nextarg);
1712       break;
1713     case 'U':
1714       /* Proxy user:password  */
1715       GetStr(&config->proxyuserpwd, nextarg);
1716       cleanarg(nextarg);
1717       break;
1718     case 'v':
1719       if(toggle) {
1720         /* the '%' thing here will cause the trace get sent to stderr */
1721         Curl_safefree(global->trace_dump);
1722         global->trace_dump = strdup("%");
1723         if(!global->trace_dump)
1724           return PARAM_NO_MEM;
1725         if(global->tracetype && (global->tracetype != TRACE_PLAIN))
1726           warnf(global,
1727                 "-v, --verbose overrides an earlier trace/verbose option\n");
1728         global->tracetype = TRACE_PLAIN;
1729       }
1730       else
1731         /* verbose is disabled here */
1732         global->tracetype = TRACE_NONE;
1733       break;
1734     case 'V':
1735       if(toggle)    /* --no-version yields no output! */
1736         return PARAM_VERSION_INFO_REQUESTED;
1737       break;
1738
1739     case 'w':
1740       /* get the output string */
1741       if('@' == *nextarg) {
1742         /* the data begins with a '@' letter, it means that a file name
1743            or - (stdin) follows */
1744         FILE *file;
1745         const char *fname;
1746         nextarg++; /* pass the @ */
1747         if(curlx_strequal("-", nextarg)) {
1748           fname = "<stdin>";
1749           file = stdin;
1750         }
1751         else {
1752           fname = nextarg;
1753           file = fopen(nextarg, FOPEN_READTEXT);
1754         }
1755         err = file2string(&config->writeout, file);
1756         if(file && (file != stdin))
1757           fclose(file);
1758         if(err)
1759           return err;
1760         if(!config->writeout)
1761           warnf(global, "Failed to read %s", fname);
1762       }
1763       else
1764         GetStr(&config->writeout, nextarg);
1765       break;
1766     case 'x':
1767       /* proxy */
1768       GetStr(&config->proxy, nextarg);
1769       config->proxyver = CURLPROXY_HTTP;
1770       break;
1771     case 'X':
1772       /* set custom request */
1773       GetStr(&config->customrequest, nextarg);
1774       break;
1775     case 'y':
1776       /* low speed time */
1777       err = str2unum(&config->low_speed_time, nextarg);
1778       if(err)
1779         return err;
1780       if(!config->low_speed_limit)
1781         config->low_speed_limit = 1;
1782       break;
1783     case 'Y':
1784       /* low speed limit */
1785       err = str2unum(&config->low_speed_limit, nextarg);
1786       if(err)
1787         return err;
1788       if(!config->low_speed_time)
1789         config->low_speed_time = 30;
1790       break;
1791     case 'z': /* time condition coming up */
1792       switch(*nextarg) {
1793       case '+':
1794         nextarg++;
1795         /* FALLTHROUGH */
1796       default:
1797         /* If-Modified-Since: (section 14.28 in RFC2068) */
1798         config->timecond = CURL_TIMECOND_IFMODSINCE;
1799         break;
1800       case '-':
1801         /* If-Unmodified-Since:  (section 14.24 in RFC2068) */
1802         config->timecond = CURL_TIMECOND_IFUNMODSINCE;
1803         nextarg++;
1804         break;
1805       case '=':
1806         /* Last-Modified:  (section 14.29 in RFC2068) */
1807         config->timecond = CURL_TIMECOND_LASTMOD;
1808         nextarg++;
1809         break;
1810       }
1811       now = time(NULL);
1812       config->condtime=curl_getdate(nextarg, &now);
1813       if(-1 == (int)config->condtime) {
1814         /* now let's see if it is a file name to get the time from instead! */
1815         struct_stat statbuf;
1816         if(-1 == stat(nextarg, &statbuf)) {
1817           /* failed, remove time condition */
1818           config->timecond = CURL_TIMECOND_NONE;
1819           warnf(global,
1820                 "Illegal date format for -z, --timecond (and not "
1821                 "a file name). Disabling time condition. "
1822                 "See curl_getdate(3) for valid date syntax.\n");
1823         }
1824         else {
1825           /* pull the time out from the file */
1826           config->condtime = statbuf.st_mtime;
1827         }
1828       }
1829       break;
1830     default: /* unknown flag */
1831       return PARAM_OPTION_UNKNOWN;
1832     }
1833     hit = -1;
1834
1835   } while(!longopt && !singleopt && *++parse && !*usedarg);
1836
1837   return PARAM_OK;
1838 }
1839
1840 ParameterError parse_args(struct GlobalConfig *config, int argc,
1841                           argv_item_t argv[])
1842 {
1843   int i;
1844   bool stillflags;
1845   char *orig_opt = NULL;
1846   ParameterError result = PARAM_OK;
1847   struct OperationConfig *operation = config->first;
1848
1849   for(i = 1, stillflags = TRUE; i < argc && !result; i++) {
1850     orig_opt = argv[i];
1851
1852     if(stillflags && ('-' == argv[i][0])) {
1853       char *nextarg;
1854       bool passarg;
1855       char *flag = argv[i];
1856
1857       if(curlx_strequal("--", argv[i]))
1858         /* This indicates the end of the flags and thus enables the
1859            following (URL) argument to start with -. */
1860         stillflags = FALSE;
1861       else {
1862         nextarg = (i < (argc - 1)) ? argv[i + 1] : NULL;
1863
1864         result = getparameter(flag, nextarg, &passarg, config, operation);
1865         if(result == PARAM_NEXT_OPERATION) {
1866           /* Reset result as PARAM_NEXT_OPERATION is only used here and not
1867              returned from this function */
1868           result = PARAM_OK;
1869
1870           if(operation->url_list && operation->url_list->url) {
1871             /* Allocate the next config */
1872             operation->next = malloc(sizeof(struct OperationConfig));
1873             if(operation->next) {
1874               /* Initialise the newly created config */
1875               config_init(operation->next);
1876
1877               /* Copy the easy handle */
1878               operation->next->easy = config->easy;
1879
1880               /* Set the global config pointer */
1881               operation->next->global = config;
1882
1883               /* Update the last operation pointer */
1884               config->last = operation->next;
1885
1886               /* Move onto the new config */
1887               operation->next->prev = operation;
1888               operation = operation->next;
1889             }
1890             else
1891               result = PARAM_NO_MEM;
1892           }
1893         }
1894         else if(!result && passarg)
1895           i++; /* we're supposed to skip this */
1896       }
1897     }
1898     else {
1899       bool used;
1900
1901       /* Just add the URL please */
1902       result = getparameter((char *)"--url", argv[i], &used, config,
1903                             operation);
1904     }
1905   }
1906
1907   if(result && result != PARAM_HELP_REQUESTED &&
1908      result != PARAM_MANUAL_REQUESTED &&
1909      result != PARAM_VERSION_INFO_REQUESTED &&
1910      result != PARAM_ENGINES_REQUESTED) {
1911     const char *reason = param2text(result);
1912
1913     if(orig_opt && !curlx_strequal(":", orig_opt))
1914       helpf(config->errors, "option %s: %s\n", orig_opt, reason);
1915     else
1916       helpf(config->errors, "%s\n", reason);
1917   }
1918
1919   return result;
1920 }