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