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