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