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