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