PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tool_getparam.c

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