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