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