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