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