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