master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "tool_setup.h"
26#include "tool_cfgable.h"
27#include "tool_cb_prg.h"
28#include "tool_filetime.h"
29#include "tool_formparse.h"
30#include "tool_getparam.h"
31#include "tool_helpers.h"
32#include "tool_libinfo.h"
33#include "tool_msgs.h"
34#include "tool_paramhlp.h"
35#include "tool_parsecfg.h"
36#include "tool_main.h"
37#include "tool_stderr.h"
38#include "tool_help.h"
39#include "var.h"
41#define ALLOW_BLANK TRUE
42#define DENY_BLANK FALSE
44static ParameterError getstr(char **str, const char *val, bool allowblank)
45{
46 if(*str)
47 curlx_safefree(*str);
48 DEBUGASSERT(val);
49 if(!allowblank && !val[0])
50 return PARAM_BLANK_STRING;
52 *str = curlx_strdup(val);
53 if(!*str)
54 return PARAM_NO_MEM;
56 return PARAM_OK;
57}
59static ParameterError getstrn(char **str, const char *val,
60 size_t len, bool allowblank)
61{
62 if(*str)
63 curlx_safefree(*str);
64 DEBUGASSERT(val);
65 if(!allowblank && !val[0])
66 return PARAM_BLANK_STRING;
68 *str = curlx_memdup0(val, len);
69 if(!*str)
70 return PARAM_NO_MEM;
72 return PARAM_OK;
73}
75/* this array MUST be alphasorted based on the 'lname' */
76static const struct LongShort aliases[] = {
77 {"abstract-unix-socket", ARG_FILE, ' ', C_ABSTRACT_UNIX_SOCKET},
78 {"alpn", ARG_BOOL|ARG_NO|ARG_TLS, ' ', C_ALPN},
79 {"alt-svc", ARG_STRG, ' ', C_ALT_SVC},
80 {"anyauth", ARG_NONE, ' ', C_ANYAUTH},
81 {"append", ARG_BOOL, 'a', C_APPEND},
82 {"aws-sigv4", ARG_STRG, ' ', C_AWS_SIGV4},
83 {"basic", ARG_BOOL, ' ', C_BASIC},
84 {"buffer", ARG_BOOL|ARG_NO, 'N', C_BUFFER},
85 {"ca-native", ARG_BOOL|ARG_TLS, ' ', C_CA_NATIVE},
86 {"cacert", ARG_FILE|ARG_TLS, ' ', C_CACERT},
87 {"capath", ARG_FILE|ARG_TLS, ' ', C_CAPATH},
88 {"cert", ARG_FILE|ARG_TLS|ARG_CLEAR, 'E', C_CERT},
89 {"cert-status", ARG_BOOL|ARG_TLS, ' ', C_CERT_STATUS},
90 {"cert-type", ARG_STRG|ARG_TLS, ' ', C_CERT_TYPE},
91 {"ciphers", ARG_STRG|ARG_TLS, ' ', C_CIPHERS},
92 {"clobber", ARG_BOOL|ARG_NO, ' ', C_CLOBBER},
93 {"compressed", ARG_BOOL, ' ', C_COMPRESSED},
94 {"compressed-ssh", ARG_BOOL, ' ', C_COMPRESSED_SSH},
95 {"config", ARG_FILE, 'K', C_CONFIG},
96 {"connect-timeout", ARG_SECS, ' ', C_CONNECT_TIMEOUT},
97 {"connect-to", ARG_STRG, ' ', C_CONNECT_TO},
98 {"continue-at", ARG_STRG, 'C', C_CONTINUE_AT},
99 {"cookie", ARG_STRG, 'b', C_COOKIE},
100 {"cookie-jar", ARG_STRG, 'c', C_COOKIE_JAR},
101 {"create-dirs", ARG_BOOL, ' ', C_CREATE_DIRS},
102 {"create-file-mode", ARG_STRG, ' ', C_CREATE_FILE_MODE},
103 {"crlf", ARG_BOOL, ' ', C_CRLF},
104 {"crlfile", ARG_FILE|ARG_TLS, ' ', C_CRLFILE},
105 {"curves", ARG_STRG|ARG_TLS, ' ', C_CURVES},
106 {"data", ARG_STRG, 'd', C_DATA},
107 {"data-ascii", ARG_STRG, ' ', C_DATA_ASCII},
108 {"data-binary", ARG_STRG, ' ', C_DATA_BINARY},
109 {"data-raw", ARG_STRG, ' ', C_DATA_RAW},
110 {"data-urlencode", ARG_STRG, ' ', C_DATA_URLENCODE},
111 {"delegation", ARG_STRG, ' ', C_DELEGATION},
112 {"digest", ARG_BOOL, ' ', C_DIGEST},
113 {"disable", ARG_BOOL, 'q', C_DISABLE},
114 {"disable-eprt", ARG_BOOL, ' ', C_DISABLE_EPRT},
115 {"disable-epsv", ARG_BOOL, ' ', C_DISABLE_EPSV},
116 {"disallow-username-in-url", ARG_BOOL, ' ', C_DISALLOW_USERNAME_IN_URL},
117 {"dns-interface", ARG_STRG, ' ', C_DNS_INTERFACE},
118 {"dns-ipv4-addr", ARG_STRG, ' ', C_DNS_IPV4_ADDR},
119 {"dns-ipv6-addr", ARG_STRG, ' ', C_DNS_IPV6_ADDR},
120 {"dns-servers", ARG_STRG, ' ', C_DNS_SERVERS},
121 {"doh-cert-status", ARG_BOOL|ARG_TLS, ' ', C_DOH_CERT_STATUS},
122 {"doh-insecure", ARG_BOOL|ARG_TLS, ' ', C_DOH_INSECURE},
123 {"doh-url", ARG_STRG, ' ', C_DOH_URL},
124 {"dump-ca-embed", ARG_NONE|ARG_TLS, ' ', C_DUMP_CA_EMBED},
125 {"dump-header", ARG_FILE, 'D', C_DUMP_HEADER},
126 {"ech", ARG_STRG|ARG_TLS, ' ', C_ECH},
127 {"egd-file", ARG_STRG|ARG_DEPR, ' ', C_EGD_FILE},
128 {"engine", ARG_STRG|ARG_TLS, ' ', C_ENGINE},
129 {"eprt", ARG_BOOL, ' ', C_EPRT},
130 {"epsv", ARG_BOOL, ' ', C_EPSV},
131 {"etag-compare", ARG_FILE, ' ', C_ETAG_COMPARE},
132 {"etag-save", ARG_FILE, ' ', C_ETAG_SAVE},
133 {"expect100-timeout", ARG_SECS, ' ', C_EXPECT100_TIMEOUT},
134 {"fail", ARG_BOOL, 'f', C_FAIL},
135 {"fail-early", ARG_BOOL, ' ', C_FAIL_EARLY},
136 {"fail-with-body", ARG_BOOL, ' ', C_FAIL_WITH_BODY},
137 {"false-start", ARG_BOOL, ' ', C_FALSE_START},
138 {"follow", ARG_BOOL, ' ', C_FOLLOW},
139 {"form", ARG_STRG, 'F', C_FORM},
140 {"form-escape", ARG_BOOL, ' ', C_FORM_ESCAPE},
141 {"form-string", ARG_STRG, ' ', C_FORM_STRING},
142 {"ftp-account", ARG_STRG, ' ', C_FTP_ACCOUNT},
143 {"ftp-alternative-to-user", ARG_STRG, ' ', C_FTP_ALTERNATIVE_TO_USER},
144 {"ftp-create-dirs", ARG_BOOL, ' ', C_FTP_CREATE_DIRS},
145 {"ftp-method", ARG_STRG, ' ', C_FTP_METHOD},
146 {"ftp-pasv", ARG_NONE, ' ', C_FTP_PASV},
147 {"ftp-port", ARG_STRG, 'P', C_FTP_PORT},
148 {"ftp-pret", ARG_BOOL, ' ', C_FTP_PRET},
149 {"ftp-skip-pasv-ip", ARG_BOOL, ' ', C_FTP_SKIP_PASV_IP},
150 {"ftp-ssl", ARG_BOOL|ARG_TLS, ' ', C_FTP_SSL},
151 {"ftp-ssl-ccc", ARG_BOOL|ARG_TLS, ' ', C_FTP_SSL_CCC},
152 {"ftp-ssl-ccc-mode", ARG_STRG|ARG_TLS, ' ', C_FTP_SSL_CCC_MODE},
153 {"ftp-ssl-control", ARG_BOOL|ARG_TLS, ' ', C_FTP_SSL_CONTROL},
154 {"ftp-ssl-reqd", ARG_BOOL|ARG_TLS, ' ', C_FTP_SSL_REQD},
155 {"get", ARG_BOOL, 'G', C_GET},
156 {"globoff", ARG_BOOL, 'g', C_GLOBOFF},
157 {"happy-eyeballs-timeout-ms", ARG_UNUM, ' ', C_HAPPY_EYEBALLS_TIMEOUT_MS},
158 {"haproxy-clientip", ARG_STRG, ' ', C_HAPROXY_CLIENTIP},
159 {"haproxy-protocol", ARG_BOOL, ' ', C_HAPROXY_PROTOCOL},
160 {"head", ARG_BOOL, 'I', C_HEAD},
161 {"header", ARG_STRG, 'H', C_HEADER},
162 {"help", ARG_STRG, 'h', C_HELP},
163 {"hostpubmd5", ARG_STRG, ' ', C_HOSTPUBMD5},
164 {"hostpubsha256", ARG_STRG, ' ', C_HOSTPUBSHA256},
165 {"hsts", ARG_STRG|ARG_TLS, ' ', C_HSTS},
166 {"http0.9", ARG_BOOL, ' ', C_HTTP0_9},
167 {"http1.0", ARG_NONE, '0', C_HTTP1_0},
168 {"http1.1", ARG_NONE, ' ', C_HTTP1_1},
169 {"http2", ARG_NONE, ' ', C_HTTP2},
170 {"http2-prior-knowledge", ARG_NONE, ' ', C_HTTP2_PRIOR_KNOWLEDGE},
171 {"http3", ARG_NONE|ARG_TLS, ' ', C_HTTP3},
172 {"http3-only", ARG_NONE|ARG_TLS, ' ', C_HTTP3_ONLY},
173#ifndef CURL_DISABLE_HTTPSIG
174 {"httpsig-algo", ARG_STRG, ' ', C_HTTPSIG_ALGORITHM},
175 {"httpsig-headers", ARG_STRG, ' ', C_HTTPSIG_HEADERS},
176 {"httpsig-key", ARG_FILE|ARG_CLEAR, ' ', C_HTTPSIG_KEY},
177 {"httpsig-keyid", ARG_STRG, ' ', C_HTTPSIG_KEYID},
178#endif
179 {"ignore-content-length", ARG_BOOL, ' ', C_IGNORE_CONTENT_LENGTH},
180 {"include", ARG_BOOL, ' ', C_INCLUDE},
181 {"insecure", ARG_BOOL, 'k', C_INSECURE},
182 {"interface", ARG_STRG, ' ', C_INTERFACE},
183 {"ip-tos", ARG_STRG, ' ', C_IP_TOS},
184#ifndef CURL_DISABLE_IPFS
185 {"ipfs-gateway", ARG_STRG, ' ', C_IPFS_GATEWAY},
186#endif
187 {"ipv4", ARG_NONE, '4', C_IPV4},
188 {"ipv6", ARG_NONE, '6', C_IPV6},
189 {"json", ARG_STRG, ' ', C_JSON},
190 {"junk-session-cookies", ARG_BOOL, 'j', C_JUNK_SESSION_COOKIES},
191 {"keepalive", ARG_BOOL|ARG_NO, ' ', C_KEEPALIVE},
192 {"keepalive-cnt", ARG_UNUM, ' ', C_KEEPALIVE_CNT},
193 {"keepalive-time", ARG_UNUM, ' ', C_KEEPALIVE_TIME},
194 {"key", ARG_FILE, ' ', C_KEY},
195 {"key-type", ARG_STRG|ARG_TLS, ' ', C_KEY_TYPE},
196 {"knownhosts", ARG_FILE, ' ', C_KNOWNHOSTS},
197 {"krb", ARG_STRG|ARG_DEPR, ' ', C_KRB},
198 {"krb4", ARG_STRG|ARG_DEPR, ' ', C_KRB4},
199 {"libcurl", ARG_STRG, ' ', C_LIBCURL},
200 {"limit-rate", ARG_STRG, ' ', C_LIMIT_RATE},
201 {"list-only", ARG_BOOL, 'l', C_LIST_ONLY},
202 {"local-port", ARG_STRG, ' ', C_LOCAL_PORT},
203 {"location", ARG_BOOL, 'L', C_LOCATION},
204 {"location-trusted", ARG_BOOL, ' ', C_LOCATION_TRUSTED},
205 {"login-options", ARG_STRG, ' ', C_LOGIN_OPTIONS},
206 {"mail-auth", ARG_STRG, ' ', C_MAIL_AUTH},
207 {"mail-from", ARG_STRG, ' ', C_MAIL_FROM},
208 {"mail-rcpt", ARG_STRG, ' ', C_MAIL_RCPT},
209 {"mail-rcpt-allowfails", ARG_BOOL, ' ', C_MAIL_RCPT_ALLOWFAILS},
210 {"manual", ARG_BOOL, 'M', C_MANUAL},
211 {"max-filesize", ARG_STRG, ' ', C_MAX_FILESIZE},
212 {"max-redirs", ARG_STRG, ' ', C_MAX_REDIRS},
213 {"max-time", ARG_SECS, 'm', C_MAX_TIME},
214 {"metalink", ARG_BOOL|ARG_DEPR, ' ', C_METALINK},
215 {"mptcp", ARG_BOOL, ' ', C_MPTCP},
216 {"negotiate", ARG_BOOL, ' ', C_NEGOTIATE},
217 {"netrc", ARG_BOOL, 'n', C_NETRC},
218 {"netrc-file", ARG_FILE, ' ', C_NETRC_FILE},
219 {"netrc-optional", ARG_BOOL, ' ', C_NETRC_OPTIONAL},
220 {"next", ARG_NONE, ':', C_NEXT},
221 {"noproxy", ARG_STRG, ' ', C_NOPROXY},
222 {"npn", ARG_BOOL|ARG_DEPR, ' ', C_NPN},
223 {"ntlm", ARG_BOOL, ' ', C_NTLM},
224 {"ntlm-wb", ARG_BOOL|ARG_DEPR, ' ', C_NTLM_WB},
225 {"oauth2-bearer", ARG_STRG|ARG_CLEAR, ' ', C_OAUTH2_BEARER},
226 {"out-null", ARG_BOOL, ' ', C_OUT_NULL},
227 {"output", ARG_FILE, 'o', C_OUTPUT},
228 {"output-dir", ARG_STRG, ' ', C_OUTPUT_DIR},
229 {"parallel", ARG_BOOL, 'Z', C_PARALLEL},
230 {"parallel-immediate", ARG_BOOL, ' ', C_PARALLEL_IMMEDIATE},
231 {"parallel-max", ARG_UNUM, ' ', C_PARALLEL_MAX},
232 {"parallel-max-host", ARG_UNUM, ' ', C_PARALLEL_HOST},
233 {"pass", ARG_STRG|ARG_CLEAR, ' ', C_PASS},
234 {"path-as-is", ARG_BOOL, ' ', C_PATH_AS_IS},
235 {"pinnedpubkey", ARG_STRG|ARG_TLS, ' ', C_PINNEDPUBKEY},
236 {"post301", ARG_BOOL, ' ', C_POST301},
237 {"post302", ARG_BOOL, ' ', C_POST302},
238 {"post303", ARG_BOOL, ' ', C_POST303},
239 {"preproxy", ARG_STRG, ' ', C_PREPROXY},
240 {"progress-bar", ARG_BOOL, '#', C_PROGRESS_BAR},
241 {"progress-meter", ARG_BOOL|ARG_NO, ' ', C_PROGRESS_METER},
242 {"proto", ARG_STRG, ' ', C_PROTO},
243 {"proto-default", ARG_STRG, ' ', C_PROTO_DEFAULT},
244 {"proto-redir", ARG_STRG, ' ', C_PROTO_REDIR},
245 {"proxy", ARG_STRG, 'x', C_PROXY},
246 {"proxy-anyauth", ARG_BOOL, ' ', C_PROXY_ANYAUTH},
247 {"proxy-basic", ARG_BOOL, ' ', C_PROXY_BASIC},
248 {"proxy-ca-native", ARG_BOOL|ARG_TLS, ' ', C_PROXY_CA_NATIVE},
249 {"proxy-cacert", ARG_FILE|ARG_TLS, ' ', C_PROXY_CACERT},
250 {"proxy-capath", ARG_FILE|ARG_TLS, ' ', C_PROXY_CAPATH},
251 {"proxy-cert", ARG_FILE|ARG_TLS|ARG_CLEAR, ' ',
252 C_PROXY_CERT},
253 {"proxy-cert-type", ARG_STRG|ARG_TLS, ' ', C_PROXY_CERT_TYPE},
254 {"proxy-ciphers", ARG_STRG|ARG_TLS, ' ', C_PROXY_CIPHERS},
255 {"proxy-crlfile", ARG_FILE|ARG_TLS, ' ', C_PROXY_CRLFILE},
256 {"proxy-digest", ARG_BOOL, ' ', C_PROXY_DIGEST},
257 {"proxy-header", ARG_STRG, ' ', C_PROXY_HEADER},
258 {"proxy-http2", ARG_BOOL, ' ', C_PROXY_HTTP2},
259 {"proxy-http3", ARG_BOOL, ' ', C_PROXY_HTTP3},
260 {"proxy-insecure", ARG_BOOL, ' ', C_PROXY_INSECURE},
261 {"proxy-key", ARG_FILE|ARG_TLS, ' ', C_PROXY_KEY},
262 {"proxy-key-type", ARG_STRG|ARG_TLS, ' ', C_PROXY_KEY_TYPE},
263 {"proxy-negotiate", ARG_BOOL, ' ', C_PROXY_NEGOTIATE},
264 {"proxy-ntlm", ARG_BOOL, ' ', C_PROXY_NTLM},
265 {"proxy-pass", ARG_STRG|ARG_CLEAR, ' ', C_PROXY_PASS},
266 {"proxy-pinnedpubkey", ARG_STRG|ARG_TLS, ' ', C_PROXY_PINNEDPUBKEY},
267 {"proxy-service-name", ARG_STRG, ' ', C_PROXY_SERVICE_NAME},
268 {"proxy-ssl-allow-beast", ARG_BOOL|ARG_TLS, ' ',
269 C_PROXY_SSL_ALLOW_BEAST},
270 {"proxy-ssl-auto-client-cert", ARG_BOOL|ARG_TLS, ' ',
271 C_PROXY_SSL_AUTO_CLIENT_CERT},
272 {"proxy-tls13-ciphers", ARG_STRG|ARG_TLS, ' ', C_PROXY_TLS13_CIPHERS},
273 {"proxy-tlsauthtype", ARG_STRG|ARG_TLS|ARG_DEPR, ' ', C_PROXY_TLSAUTHTYPE},
274 {"proxy-tlspassword", ARG_STRG|ARG_TLS|ARG_CLEAR|ARG_DEPR, ' ',
275 C_PROXY_TLSPASSWORD},
276 {"proxy-tlsuser", ARG_STRG|ARG_TLS|ARG_CLEAR|ARG_DEPR, ' ',
277 C_PROXY_TLSUSER},
278 {"proxy-tlsv1", ARG_NONE|ARG_TLS, ' ', C_PROXY_TLSV1},
279 {"proxy-user", ARG_STRG|ARG_CLEAR, 'U', C_PROXY_USER},
280 {"proxy1.0", ARG_STRG, ' ', C_PROXY1_0},
281 {"proxytunnel", ARG_BOOL, 'p', C_PROXYTUNNEL},
282 {"pubkey", ARG_STRG, ' ', C_PUBKEY},
283 {"quote", ARG_STRG, 'Q', C_QUOTE},
284 {"random-file", ARG_FILE|ARG_DEPR, ' ', C_RANDOM_FILE},
285 {"range", ARG_STRG, 'r', C_RANGE},
286 {"rate", ARG_STRG, ' ', C_RATE},
287 {"raw", ARG_BOOL, ' ', C_RAW},
288 {"referer", ARG_STRG, 'e', C_REFERER},
289 {"remote-header-name", ARG_BOOL, 'J', C_REMOTE_HEADER_NAME},
290 {"remote-name", ARG_BOOL, 'O', C_REMOTE_NAME},
291 {"remote-name-all", ARG_BOOL, ' ', C_REMOTE_NAME_ALL},
292 {"remote-time", ARG_BOOL, 'R', C_REMOTE_TIME},
293 {"remove-on-error", ARG_BOOL, ' ', C_REMOVE_ON_ERROR},
294 {"request", ARG_STRG, 'X', C_REQUEST},
295 {"request-target", ARG_STRG, ' ', C_REQUEST_TARGET},
296 {"resolve", ARG_STRG, ' ', C_RESOLVE},
297 {"retry", ARG_UNUM, ' ', C_RETRY},
298 {"retry-all-errors", ARG_BOOL, ' ', C_RETRY_ALL_ERRORS},
299 {"retry-connrefused", ARG_BOOL, ' ', C_RETRY_CONNREFUSED},
300 {"retry-delay", ARG_SECS, ' ', C_RETRY_DELAY},
301 {"retry-max-time", ARG_SECS, ' ', C_RETRY_MAX_TIME},
302 {"sasl-authzid", ARG_STRG, ' ', C_SASL_AUTHZID},
303 {"sasl-ir", ARG_BOOL, ' ', C_SASL_IR},
304 {"service-name", ARG_STRG, ' ', C_SERVICE_NAME},
305 {"sessionid", ARG_BOOL|ARG_NO, ' ', C_SESSIONID},
306 {"show-error", ARG_BOOL, 'S', C_SHOW_ERROR},
307 {"show-headers", ARG_BOOL, 'i', C_SHOW_HEADERS},
308 {"sigalgs", ARG_STRG|ARG_TLS, ' ',
309 C_SIGNATURE_ALGORITHMS},
310 {"silent", ARG_BOOL, 's', C_SILENT},
311 {"skip-existing", ARG_BOOL, ' ', C_SKIP_EXISTING},
312 {"socks4", ARG_STRG, ' ', C_SOCKS4},
313 {"socks4a", ARG_STRG, ' ', C_SOCKS4A},
314 {"socks5", ARG_STRG, ' ', C_SOCKS5},
315 {"socks5-basic", ARG_BOOL, ' ', C_SOCKS5_BASIC},
316 {"socks5-gssapi", ARG_BOOL, ' ', C_SOCKS5_GSSAPI},
317 {"socks5-gssapi-nec", ARG_BOOL, ' ', C_SOCKS5_GSSAPI_NEC},
318 {"socks5-gssapi-service", ARG_STRG, ' ', C_SOCKS5_GSSAPI_SERVICE},
319 {"socks5-hostname", ARG_STRG, ' ', C_SOCKS5_HOSTNAME},
320 {"speed-limit", ARG_UNUM, 'Y', C_SPEED_LIMIT},
321 {"speed-time", ARG_UNUM, 'y', C_SPEED_TIME},
322 {"ssl", ARG_BOOL|ARG_TLS, ' ', C_SSL},
323 {"ssl-allow-beast", ARG_BOOL|ARG_TLS, ' ', C_SSL_ALLOW_BEAST},
324 {"ssl-auto-client-cert", ARG_BOOL|ARG_TLS, ' ',
325 C_SSL_AUTO_CLIENT_CERT},
326 {"ssl-no-revoke", ARG_BOOL|ARG_TLS, ' ', C_SSL_NO_REVOKE},
327 {"ssl-reqd", ARG_BOOL|ARG_TLS, ' ', C_SSL_REQD},
328 {"ssl-revoke-best-effort", ARG_BOOL|ARG_TLS, ' ',
329 C_SSL_REVOKE_BEST_EFFORT},
330 {"ssl-sessions", ARG_FILE|ARG_TLS, ' ', C_SSL_SESSIONS},
331 {"sslv2", ARG_NONE|ARG_DEPR, '2', C_SSLV2},
332 {"sslv3", ARG_NONE|ARG_DEPR, '3', C_SSLV3},
333 {"stderr", ARG_FILE, ' ', C_STDERR},
334 {"styled-output", ARG_BOOL, ' ', C_STYLED_OUTPUT},
335 {"suppress-connect-headers", ARG_BOOL, ' ', C_SUPPRESS_CONNECT_HEADERS},
336 {"tcp-fastopen", ARG_BOOL, ' ', C_TCP_FASTOPEN},
337 {"tcp-nodelay", ARG_BOOL, ' ', C_TCP_NODELAY},
338 {"telnet-option", ARG_STRG, 't', C_TELNET_OPTION},
339#ifdef DEBUGBUILD
340 {"test-duphandle", ARG_BOOL, ' ', C_TEST_DUPHANDLE},
341 {"test-event", ARG_BOOL, ' ', C_TEST_EVENT},
342#endif
343 {"tftp-blksize", ARG_UNUM, ' ', C_TFTP_BLKSIZE},
344 {"tftp-no-options", ARG_BOOL, ' ', C_TFTP_NO_OPTIONS},
345 {"time-cond", ARG_STRG, 'z', C_TIME_COND},
346 {"tls-earlydata", ARG_BOOL|ARG_TLS, ' ', C_TLS_EARLYDATA},
347 {"tls-max", ARG_STRG|ARG_TLS, ' ', C_TLS_MAX},
348 {"tls13-ciphers", ARG_STRG|ARG_TLS, ' ', C_TLS13_CIPHERS},
349 {"tlsauthtype", ARG_STRG|ARG_TLS|ARG_DEPR, ' ', C_TLSAUTHTYPE},
350 {"tlspassword", ARG_STRG|ARG_TLS|ARG_CLEAR|ARG_DEPR, ' ', C_TLSPASSWORD},
351 {"tlsuser", ARG_STRG|ARG_TLS|ARG_CLEAR|ARG_DEPR, ' ', C_TLSUSER},
352 {"tlsv1", ARG_NONE|ARG_TLS, '1', C_TLSV1},
353 {"tlsv1.0", ARG_NONE|ARG_TLS, ' ', C_TLSV1_0},
354 {"tlsv1.1", ARG_NONE|ARG_TLS, ' ', C_TLSV1_1},
355 {"tlsv1.2", ARG_NONE|ARG_TLS, ' ', C_TLSV1_2},
356 {"tlsv1.3", ARG_NONE|ARG_TLS, ' ', C_TLSV1_3},
357 {"tr-encoding", ARG_BOOL, ' ', C_TR_ENCODING},
358 {"trace", ARG_FILE, ' ', C_TRACE},
359 {"trace-ascii", ARG_FILE, ' ', C_TRACE_ASCII},
360 {"trace-config", ARG_STRG, ' ', C_TRACE_CONFIG},
361 {"trace-ids", ARG_BOOL, ' ', C_TRACE_IDS},
362 {"trace-time", ARG_BOOL, ' ', C_TRACE_TIME},
363 {"unix-socket", ARG_FILE, ' ', C_UNIX_SOCKET},
364 {"upload-file", ARG_FILE, 'T', C_UPLOAD_FILE},
365 {"upload-flags", ARG_STRG, ' ', C_UPLOAD_FLAGS},
366 {"url", ARG_STRG, ' ', C_URL},
367 {"url-query", ARG_STRG, ' ', C_URL_QUERY},
368 {"use-ascii", ARG_BOOL, 'B', C_USE_ASCII},
369 {"user", ARG_STRG|ARG_CLEAR, 'u', C_USER},
370 {"user-agent", ARG_STRG, 'A', C_USER_AGENT},
371 {"variable", ARG_STRG, ' ', C_VARIABLE},
372 {"verbose", ARG_BOOL, 'v', C_VERBOSE},
373 {"version", ARG_BOOL, 'V', C_VERSION},
374 {"vlan-priority", ARG_UNUM, ' ', C_VLAN_PRIORITY},
375#ifdef USE_WATT32
376 {"wdebug", ARG_BOOL, ' ', C_WDEBUG},
377#endif
378 {"write-out", ARG_STRG, 'w', C_WRITE_OUT},
379 {"xattr", ARG_BOOL, ' ', C_XATTR},
380};
382/* Split the argument of -E to 'certname' and 'passphrase' separated by colon.
383 * We allow ':' and '\' to be escaped by '\' so that we can use certificate
384 * nicknames containing ':'. See <https://sourceforge.net/p/curl/bugs/1196/>
385 * for details.
386 *
387 * Unit test 1394
388 */
389UNITTEST ParameterError parse_cert_parameter(const char *cert_parameter,
390 char **certname,
391 char **passphrase)
392{
393 size_t param_length = strlen(cert_parameter);
394 size_t span;
395 const char *param_place = NULL;
396 char *certname_place = NULL;
397 ParameterError err = PARAM_OK;
398 *certname = NULL;
399 *passphrase = NULL;
401 /* most trivial assumption: cert_parameter is empty */
402 if(param_length == 0)
403 return PARAM_BLANK_STRING;
405 /* next less trivial: cert_parameter starts 'pkcs11:' and thus
406 * looks like a RFC7512 PKCS#11 URI which can be used as-is.
407 * Also if cert_parameter contains no colon nor backslash, this
408 * means no passphrase was given and no characters escaped */
409 if(curl_strnequal(cert_parameter, "pkcs11:", 7) ||
410 !strpbrk(cert_parameter, ":\\")) {
411 *certname = curlx_strdup(cert_parameter);
412 if(!*certname)
413 return PARAM_NO_MEM;
414 return PARAM_OK;
415 }
416 /* deal with escaped chars; find unescaped colon if it exists */
417 certname_place = curlx_malloc(param_length + 1);
418 if(!certname_place) {
419 err = PARAM_NO_MEM;
420 goto done;
421 }
423 *certname = certname_place;
424 param_place = cert_parameter;
425 while(*param_place) {
426 span = strcspn(param_place, ":\\");
427 memcpy(certname_place, param_place, span);
428 param_place += span;
429 certname_place += span;
430 /* we ate all the non-special chars. now we are on either a special
431 * char or the end of the string. */
432 switch(*param_place) {
433 case '\0':
434 break;
435 case '\\':
436 param_place++;
437 switch(*param_place) {
438 case '\0':
439 *certname_place++ = '\\';
440 break;
441 case '\\':
442 *certname_place++ = '\\';
443 param_place++;
444 break;
445 case ':':
446 *certname_place++ = ':';
447 param_place++;
448 break;
449 default:
450 *certname_place++ = '\\';
451 *certname_place++ = *param_place;
452 param_place++;
453 break;
454 }
455 break;
456 case ':':
457 /* Since we live in a world of weirdness and confusion, the Windows
458 dudes can use : when using drive letters and thus c:\file:password
459 needs to work. In order not to break compatibility, we still use : as
460 separator, but we try to detect when it is used for a filename! On
461 Windows. */
462#ifdef _WIN32
463 if((param_place == &cert_parameter[1]) &&
464 (cert_parameter[2] == '\\' || cert_parameter[2] == '/') &&
465 ISALPHA(cert_parameter[0])) {
466 /* colon in the second column, followed by a backslash, and the
467 first character is an alphabetic letter:
469 this is a drive letter colon */
470 *certname_place++ = ':';
471 param_place++;
472 break;
473 }
474#endif
475 /* escaped colons and Windows drive letter colons were handled
476 * above; if we are still here, this is a separating colon */
477 param_place++;
478 if(*param_place) {
479 *passphrase = curlx_strdup(param_place);
480 if(!*passphrase)
481 err = PARAM_NO_MEM;
482 }
483 goto done;
484 }
485 }
486done:
487 if(err) {
488 curlx_safefree(*certname);
489 }
490 else
491 *certname_place = '\0';
492 return err;
493}
495/* Replace (in-place) '%20' by '+' according to RFC1866 */
496static size_t replace_url_encoded_space_by_plus(char *url)
497{
498 size_t orig_len = strlen(url);
499 size_t orig_index = 0;
500 size_t new_index = 0;
502 while(orig_index < orig_len) {
503 if((url[orig_index] == '%') &&
504 (url[orig_index + 1] == '2') &&
505 (url[orig_index + 2] == '0')) {
506 url[new_index] = '+';
507 orig_index += 3;
508 }
509 else {
510 if(new_index != orig_index) {
511 url[new_index] = url[orig_index];
512 }
513 orig_index++;
514 }
515 new_index++;
516 }
518 url[new_index] = 0; /* terminate string */
520 return new_index; /* new size */
521}
523static ParameterError GetFileAndPassword(const char *nextarg, char **file,
524 char **password)
525{
526 char *certname, *passphrase;
527 ParameterError err;
528 /* nextarg is never NULL here */
529 err = parse_cert_parameter(nextarg, &certname, &passphrase);
530 if(!err) {
531 curlx_free(*file);
532 *file = certname;
533 if(passphrase) {
534 curlx_free(*password);
535 *password = passphrase;
536 }
537 }
538 return err;
539}
541struct sizeunit {
542 char unit; /* single lowercase ASCII letter */
543 curl_off_t mul;
544 size_t mlen; /* number of digits in 'mul', when written in decimal */
545};
547static const struct sizeunit *getunit(char unit)
548{
549 static const struct sizeunit list[] = {
550 { 'p', (curl_off_t)1125899906842624, 16 }, /* Peta */
551 { 't', (curl_off_t)1099511627776, 13 }, /* Tera */
552 { 'g', 1073741824, 10 }, /* Giga */
553 { 'm', 1048576, 7 }, /* Mega */
554 { 'k', 1024, 4 }, /* Kilo */
555 };
557 size_t i;
558 for(i = 0; i < CURL_ARRAYSIZE(list); i++)
559 if((unit | 0x20) == list[i].unit)
560 return &list[i];
561 return NULL;
562}
564/* Get a size parameter for '--limit-rate' or '--max-filesize'.
565 We support P, T, G, M and K (case insensitive) suffixes.
567 Unit test 1623
568 */
569UNITTEST ParameterError GetSizeParameter(const char *arg, curl_off_t *out)
570{
571 const char *unit = arg;
572 curl_off_t value;
573 curl_off_t prec = 0;
574 size_t plen = 0;
575 curl_off_t add = 0;
576 curl_off_t mul = 1;
577 int rc;
579 rc = curlx_str_number(&unit, &value, CURL_OFF_T_MAX);
580 if(rc == STRE_OVERFLOW)
581 return PARAM_NUMBER_TOO_LARGE;
582 else if(rc)
583 return PARAM_BAD_NUMERIC;
585 if(!curlx_str_single(&unit, '.')) {
586 const char *s = unit;
587 if(curlx_str_number(&unit, &prec, CURL_OFF_T_MAX))
588 return PARAM_BAD_NUMERIC;
589 plen = unit - s;
590 }
592 if(strlen(unit) > 1)
593 return PARAM_BAD_USE;
594 else if(!*unit || ((*unit | 0x20) == 'b')) {
595 if(plen)
596 /* cannot handle partial bytes */
597 return PARAM_BAD_USE;
598 }
599 else {
600 const struct sizeunit *su = getunit(*unit);
601 if(!su)
602 return PARAM_BAD_USE;
603 mul = su->mul;
605 if(prec) {
606 /* precision was provided */
607 curl_off_t frac = 1;
609 /* too many precision digits, trim them */
610 while(su->mlen <= plen) {
611 prec /= 10;
612 plen--;
613 }
615 while(plen--)
616 frac *= 10;
618 if((CURL_OFF_T_MAX / mul) > prec)
619 add = mul * prec / frac;
620 else
621 add = (mul / frac) * prec;
622 }
623 }
624 if(value > ((CURL_OFF_T_MAX - add) / mul))
625 return PARAM_NUMBER_TOO_LARGE;
627 *out = (value * mul) + add;
628 return PARAM_OK;
629}
631#ifdef HAVE_WRITABLE_ARGV
632static void cleanarg(char *str)
633{
634 /* now that getstr has copied the contents of nextarg, wipe the next
635 * argument out so that the username:password is not displayed in the
636 * system process list */
637 if(str) {
638 size_t len = strlen(str);
639 memset(str, '*', len);
640 }
641}
642#else
643#define cleanarg(x) tool_nop_stmt
644#endif
646/* the maximum size we allow the dynbuf generated string */
647#define MAX_DATAURLENCODE (500 * 1024 * 1024)
649/* --data-urlencode */
650static ParameterError data_urlencode(const char *nextarg,
651 char **postp,
652 size_t *lenp)
653{
654 /* [name]=[content], we encode the content part only
655 * [name]@[filename]
656 *
657 * Case 2: we first load the file using that name and then encode
658 * the content.
659 */
660 ParameterError err;
661 const char *p = strchr(nextarg, '=');
662 size_t nlen;
663 char is_file;
664 char *postdata = NULL;
665 size_t size = 0;
666 if(!p)
667 /* there was no '=' letter, check for a '@' instead */
668 p = strchr(nextarg, '@');
669 if(p) {
670 nlen = p - nextarg; /* length of the name part */
671 is_file = *p++; /* pass the separator */
672 }
673 else {
674 /* neither @ nor =, so no name and it is not a file */
675 nlen = 0;
676 is_file = 0;
677 p = nextarg;
678 }
679 if('@' == is_file) {
680 FILE *file;
681 /* a '@' letter, it means that a filename or - (stdin) follows */
682 if(!strcmp("-", p)) {
683 file = stdin;
684 CURL_BINMODE(stdin);
685 }
686 else {
687 file = curlx_fopen(p, "rb");
688 if(!file) {
689 errorf("Failed to open %s", p);
690 return PARAM_READ_ERROR;
691 }
692 }
694 err = file2memory(&postdata, &size, file);
696 if(file && (file != stdin))
697 curlx_fclose(file);
698 if(err)
699 return err;
700 }
701 else {
702 err = getstr(&postdata, p, ALLOW_BLANK);
703 if(err)
704 goto error;
705 size = strlen(postdata);
706 }
708 if(!postdata) {
709 /* no data from the file, point to a zero byte string to make this
710 get sent as a POST anyway */
711 postdata = curlx_strdup("");
712 if(!postdata)
713 return PARAM_NO_MEM;
714 size = 0;
715 }
716 if(size > INT_MAX) {
717 /* curl_easy_escape() takes an int length */
718 errorf("too much data to URL encode");
719 curlx_free(postdata);
720 return PARAM_BAD_USE;
721 }
722 else {
723 char *enc = curl_easy_escape(NULL, postdata, (int)size);
724 curlx_safefree(postdata); /* no matter if it worked or not */
725 if(enc) {
726 char *n;
727 replace_url_encoded_space_by_plus(enc);
728 if(nlen > 0) { /* only append '=' if we have a name */
729 struct dynbuf dyn;
730 curlx_dyn_init(&dyn, MAX_DATAURLENCODE);
731 if(curlx_dyn_addn(&dyn, nextarg, nlen) ||
732 curlx_dyn_addn(&dyn, "=", 1) ||
733 curlx_dyn_add(&dyn, enc)) {
734 curl_free(enc);
735 return PARAM_NO_MEM;
736 }
737 curl_free(enc);
738 n = curlx_dyn_ptr(&dyn);
739 size = curlx_dyn_len(&dyn);
740 }
741 else {
742 /* make sure we return "our memory" */
743 n = curlx_strdup(enc);
744 curl_free(enc);
745 if(!n)
746 return PARAM_NO_MEM;
747 size = strlen(n);
748 }
749 postdata = n;
750 }
751 else
752 return PARAM_NO_MEM;
753 }
754 *postp = postdata;
755 *lenp = size;
756 return PARAM_OK;
757error:
758 return err;
759}
761static void sethttpver(struct OperationConfig *config, long httpversion)
762{
763 if(config->httpversion &&
764 (config->httpversion != httpversion))
765 warnf("Overrides previous HTTP version option");
767 config->httpversion = httpversion;
768}
770static CURLcode set_trace_config(const char *token)
771{
772 CURLcode result = CURLE_OK;
773 const char *next, *name;
774 bool toggle;
776 while(token) {
777 size_t len;
778 next = strchr(token, ',');
780 if(next)
781 len = next - token;
782 else
783 len = strlen(token);
785 switch(*token) {
786 case '-':
787 toggle = FALSE;
788 name = token + 1;
789 len--;
790 break;
791 case '+':
792 toggle = TRUE;
793 name = token + 1;
794 len--;
795 break;
796 default:
797 toggle = TRUE;
798 name = token;
799 break;
800 }
802 if((len == 3) && curl_strnequal(name, "all", 3)) {
803 global->traceids = toggle;
804 global->tracetime = toggle;
805 if(toggle)
806 result = curl_global_trace("all,-lib-ids");
807 else
808 result = curl_global_trace(token);
809 if(result)
810 goto out;
811 }
812 else if((len == 3) && curl_strnequal(name, "ids", 3)) {
813 global->traceids = toggle;
814 }
815 else if((len == 4) && curl_strnequal(name, "time", 4)) {
816 global->tracetime = toggle;
817 }
818 else {
819 char buffer[64];
820 curl_msnprintf(buffer, sizeof(buffer), "%c%.*s,-lib-ids",
821 toggle ? '+' : '-', (int)len, name);
822 result = curl_global_trace(buffer);
823 if(result)
824 goto out;
825 }
826 if(next) {
827 next++;
828 if(*next == ' ')
829 next++;
830 }
831 token = next;
832 }
833out:
834 return result;
835}
837static int findarg(const void *a, const void *b)
838{
839 const struct LongShort *aa = a;
840 const struct LongShort *bb = b;
841 return strcmp(aa->lname, bb->lname);
842}
844const struct LongShort *findshortopt(char letter)
845{
846 static const struct LongShort *singles[128 - ' ']; /* ASCII => pointer */
847 static bool singles_done = FALSE;
848 if((letter >= 127) || (letter <= ' '))
849 return NULL;
851 if(!singles_done) {
852 unsigned int j;
853 for(j = 0; j < CURL_ARRAYSIZE(aliases); j++) {
854 if(aliases[j].letter != ' ') {
855 unsigned char l = (unsigned char)aliases[j].letter;
856 singles[l - ' '] = &aliases[j];
857 }
858 }
859 singles_done = TRUE;
860 }
861 return singles[letter - ' '];
862}
864struct TOSEntry {
865 const char *name;
866 unsigned char value;
867};
869static const struct TOSEntry tos_entries[] = {
870 { "AF11", 0x28 },
871 { "AF12", 0x30 },
872 { "AF13", 0x38 },
873 { "AF21", 0x48 },
874 { "AF22", 0x50 },
875 { "AF23", 0x58 },
876 { "AF31", 0x68 },
877 { "AF32", 0x70 },
878 { "AF33", 0x78 },
879 { "AF41", 0x88 },
880 { "AF42", 0x90 },
881 { "AF43", 0x98 },
882 { "CE", 0x03 },
883 { "CS0", 0x00 },
884 { "CS1", 0x20 },
885 { "CS2", 0x40 },
886 { "CS3", 0x60 },
887 { "CS4", 0x80 },
888 { "CS5", 0xa0 },
889 { "CS6", 0xc0 },
890 { "CS7", 0xe0 },
891 { "ECT0", 0x02 },
892 { "ECT1", 0x01 },
893 { "EF", 0xb8 },
894 { "LE", 0x04 },
895 { "LOWCOST", 0x02 },
896 { "LOWDELAY", 0x10 },
897 { "MINCOST", 0x02 },
898 { "RELIABILITY", 0x04 },
899 { "THROUGHPUT", 0x08 },
900 { "VOICE-ADMIT", 0xb0 }
901};
903static int find_tos(const void *a, const void *b)
904{
905 const struct TOSEntry *aa = a;
906 const struct TOSEntry *bb = b;
907 return strcmp(aa->name, bb->name);
908}
910#define MAX_QUERY_LEN 100000 /* larger is not likely to ever work */
911static ParameterError url_query(const char *nextarg,
912 struct OperationConfig *config)
913{
914 size_t size = 0;
915 ParameterError err = PARAM_OK;
916 char *query;
917 struct dynbuf dyn;
918 curlx_dyn_init(&dyn, MAX_QUERY_LEN);
920 if(*nextarg == '+') {
921 /* use without encoding */
922 query = curlx_strdup(&nextarg[1]);
923 if(!query)
924 err = PARAM_NO_MEM;
925 }
926 else
927 err = data_urlencode(nextarg, &query, &size);
929 if(!err) {
930 if(config->query) {
931 CURLcode result = curlx_dyn_addf(&dyn, "%s&%s", config->query, query);
932 curlx_free(query);
933 if(result)
934 err = PARAM_NO_MEM;
935 else {
936 curlx_free(config->query);
937 config->query = curlx_dyn_ptr(&dyn);
938 }
939 }
940 else
941 config->query = query;
942 }
943 return err;
944}
946static ParameterError set_data(cmdline_t cmd,
947 const char *nextarg,
948 struct OperationConfig *config)
949{
950 char *postdata = NULL;
951 FILE *file;
952 size_t size = 0;
953 ParameterError err = PARAM_OK;
955 if(cmd == C_DATA_URLENCODE) { /* --data-urlencode */
956 err = data_urlencode(nextarg, &postdata, &size);
957 if(err)
958 return err;
959 }
960 else if('@' == *nextarg && (cmd != C_DATA_RAW)) {
961 /* the data begins with a '@' letter, it means that a filename
962 or - (stdin) follows */
963 nextarg++; /* pass the @ */
965 if(!strcmp("-", nextarg)) {
966 file = stdin;
967 if(cmd == C_DATA_BINARY) /* forced data-binary */
968 CURL_BINMODE(stdin);
969 }
970 else {
971 file = curlx_fopen(nextarg, "rb");
972 if(!file) {
973 errorf("Failed to open %s", nextarg);
974 return PARAM_READ_ERROR;
975 }
976 }
978 if((cmd == C_DATA_BINARY) || /* --data-binary */
979 (cmd == C_JSON) /* --json */)
980 /* forced binary */
981 err = file2memory(&postdata, &size, file);
982 else {
983 err = file2string(&postdata, file);
984 if(postdata)
985 size = strlen(postdata);
986 }
988 if(file && (file != stdin))
989 curlx_fclose(file);
990 if(err)
991 return err;
993 if(!postdata) {
994 /* no data from the file, point to a zero byte string to make this
995 get sent as a POST anyway */
996 postdata = curlx_strdup("");
997 if(!postdata)
998 return PARAM_NO_MEM;
999 }
1000 }
1001 else {
1002 err = getstr(&postdata, nextarg, ALLOW_BLANK);
1003 if(err)
1004 return err;
1005 size = strlen(postdata);
1006 }
1007 if(cmd == C_JSON)
1008 config->jsoned = TRUE;
1010 /* skip separator append for --json */
1011 if(curlx_dyn_len(&config->postdata) && !err && (cmd != C_JSON) &&
1012 curlx_dyn_addn(&config->postdata, "&", 1))
1013 err = PARAM_NO_MEM;
1015 if(!err && curlx_dyn_addn(&config->postdata, postdata, size))
1016 err = PARAM_NO_MEM;
1018 curlx_safefree(postdata);
1020 config->postfields = curlx_dyn_ptr(&config->postdata);
1021 return err;
1022}
1024static ParameterError set_rate(const char *nextarg)
1025{
1026 /* --rate */
1027 /* support a few different suffixes, extract the suffix first, then
1028 get the number and convert to per hour.
1029 /s == per second
1030 /m == per minute
1031 /h == per hour (default)
1032 /d == per day (24 hours)
1033 */
1034 ParameterError err = PARAM_OK;
1035 const char *p = nextarg;
1036 curl_off_t denominator;
1037 curl_off_t numerator = 60 * 60 * 1000; /* default per hour */
1039 if(curlx_str_number(&p, &denominator, CURL_OFF_T_MAX))
1040 return PARAM_BAD_NUMERIC;
1042 if(denominator < 1)
1043 return PARAM_BAD_USE;
1045 if(!curlx_str_single(&p, '/')) {
1046 curl_off_t numunits;
1048 if(curlx_str_number(&p, &numunits, CURL_OFF_T_MAX))
1049 numunits = 1;
1051 switch(*p) {
1052 case 's': /* per second */
1053 numerator = 1000;
1054 break;
1055 case 'm': /* per minute */
1056 numerator = 60 * 1000;
1057 break;
1058 case 'h': /* per hour */
1059 break;
1060 case 'd': /* per day */
1061 numerator = 24 * 60 * 60 * 1000;
1062 break;
1063 default:
1064 errorf("unsupported --rate unit");
1065 err = PARAM_BAD_USE;
1066 break;
1067 }
1069 if((CURL_OFF_T_MAX / numerator) < numunits) {
1070 /* overflow, too large number */
1071 errorf("too large --rate unit");
1072 err = PARAM_NUMBER_TOO_LARGE;
1073 }
1074 else
1075 /* this typecast is okay based on the check above */
1076 numerator *= numunits;
1077 }
1079 if(err)
1080 ;
1081 else if(denominator > numerator)
1082 err = PARAM_NUMBER_TOO_LARGE;
1083 else
1084 global->ms_per_transfer = numerator / denominator;
1086 return err;
1087}
1089const struct LongShort *findlongopt(const char *opt)
1090{
1091 struct LongShort key;
1092 key.lname = opt;
1094 return bsearch(&key, aliases, CURL_ARRAYSIZE(aliases),
1095 sizeof(aliases[0]), findarg);
1096}
1098static ParameterError add_url(struct OperationConfig *config,
1099 const char *thisurl,
1100 bool remote_noglob)
1101{
1102 ParameterError err = PARAM_OK;
1103 struct getout *url;
1105 if(!config->url_get)
1106 config->url_get = config->url_list;
1108 if(config->url_get) {
1109 /* there is a node here, if it already is filled-in continue to find
1110 an "empty" node */
1111 while(config->url_get && config->url_get->urlset)
1112 config->url_get = config->url_get->next;
1113 }
1115 /* now there might or might not be an available node to fill in! */
1117 if(config->url_get)
1118 /* existing node */
1119 url = config->url_get;
1120 else
1121 /* there was no free node, create one! */
1122 config->url_get = url = new_getout(config);
1124 if(!url)
1125 return PARAM_NO_MEM;
1126 else {
1127 /* fill in the URL */
1128 err = getstr(&url->url, thisurl, DENY_BLANK);
1129 url->urlset = TRUE;
1130 if(remote_noglob)
1131 url->useremote = url->noglob = TRUE;
1132 if(!err && (++config->num_urls > 1) &&
1133 (config->etag_save_file || config->etag_compare_file)) {
1134 errorf("The etag options only work on a single URL");
1135 return PARAM_BAD_USE;
1136 }
1137 }
1138 return err;
1139}
1141static ParameterError parse_url(struct OperationConfig *config,
1142 const char *nextarg)
1143{
1144 /* nextarg is never NULL here */
1145 if(nextarg[0] == '@') {
1146 /* read URLs from a file, treat all as -O */
1147 struct dynbuf line;
1148 ParameterError err = PARAM_OK;
1149 bool error = FALSE;
1150 bool fromstdin = !strcmp("-", &nextarg[1]);
1151 FILE *f;
1153 if(fromstdin)
1154 f = stdin;
1155 else
1156 f = curlx_fopen(&nextarg[1], FOPEN_READTEXT);
1157 if(f) {
1158 curlx_dyn_init(&line, 8092);
1159 while(my_get_line(f, &line, &error, NULL)) {
1160 const char *ptr = curlx_dyn_ptr(&line);
1161 err = add_url(config, ptr, TRUE);
1162 if(err)
1163 break;
1164 }
1165 if(!fromstdin)
1166 curlx_fclose(f);
1167 curlx_dyn_free(&line);
1168 if(error || err)
1169 return PARAM_READ_ERROR;
1170 return PARAM_OK;
1171 }
1172 return PARAM_READ_ERROR; /* file not found */
1173 }
1174 return add_url(config, nextarg, FALSE);
1175}
1177static ParameterError parse_localport(struct OperationConfig *config,
1178 const char *nextarg)
1179{
1180 const char *pp = NULL;
1181 const char *p = nextarg;
1182 char buffer[22];
1183 size_t plen = 0;
1184 while(ISDIGIT(*p))
1185 p++;
1186 plen = p - nextarg;
1187 if(*p) {
1188 pp = p;
1189 /* check for ' - [end]' */
1190 if(ISBLANK(*pp))
1191 pp++;
1192 if(*pp != '-')
1193 return PARAM_BAD_USE;
1194 pp++;
1195 if(ISBLANK(*pp))
1196 pp++;
1197 }
1198 curl_msnprintf(buffer, sizeof(buffer), "%.*s", (int)plen, nextarg);
1199 if(str2unummax(&config->localport, buffer, 65535))
1200 return PARAM_BAD_USE;
1201 if(!pp)
1202 config->localportrange = 1; /* default number of ports to try */
1203 else {
1204 if(str2unummax(&config->localportrange, pp, 65535))
1205 return PARAM_BAD_USE;
1206 config->localportrange -= (config->localport - 1);
1207 if(config->localportrange < 1)
1208 return PARAM_BAD_USE;
1209 }
1210 return PARAM_OK;
1211}
1213static ParameterError parse_continue_at(struct OperationConfig *config,
1214 const char *nextarg)
1215{
1216 ParameterError err = PARAM_OK;
1217 if(config->range) {
1218 errorf("--continue-at is mutually exclusive with --range");
1219 return PARAM_BAD_USE;
1220 }
1221 if(config->rm_partial) {
1222 errorf("--continue-at is mutually exclusive with --remove-on-error");
1223 return PARAM_BAD_USE;
1224 }
1225 if(config->file_clobber_mode == CLOBBER_NEVER) {
1226 errorf("--continue-at is mutually exclusive with --no-clobber");
1227 return PARAM_BAD_USE;
1228 }
1229 /* This makes us continue an ftp transfer at given position */
1230 if(strcmp(nextarg, "-")) {
1231 err = str2offset(&config->resume_from, nextarg);
1232 config->resume_from_current = FALSE;
1233 }
1234 else {
1235 config->resume_from_current = TRUE;
1236 config->resume_from = 0;
1237 }
1238 config->use_resume = TRUE;
1239 return err;
1240}
1242static ParameterError parse_ech(struct OperationConfig *config,
1243 const char *nextarg)
1244{
1245 ParameterError err = PARAM_OK;
1246 if(!feature_ech)
1247 err = PARAM_LIBCURL_DOESNT_SUPPORT;
1248 else if(strlen(nextarg) > 4 && curl_strnequal("pn:", nextarg, 3)) {
1249 /* a public_name */
1250 err = getstr(&config->ech_public, nextarg, DENY_BLANK);
1251 }
1252 else if(strlen(nextarg) > 5 && curl_strnequal("ecl:", nextarg, 4)) {
1253 /* an ECHConfigList */
1254 if('@' != *(nextarg + 4)) {
1255 err = getstr(&config->ech_config, nextarg, DENY_BLANK);
1256 }
1257 else {
1258 /* Indirect case: @filename or @- for stdin */
1259 char *tmpcfg = NULL;
1260 FILE *file;
1262 nextarg += 5; /* skip over 'ecl:@' */
1263 if(!strcmp("-", nextarg)) {
1264 file = stdin;
1265 }
1266 else {
1267 file = curlx_fopen(nextarg, FOPEN_READTEXT);
1268 }
1269 if(!file) {
1270 warnf("Could not read file \"%s\" "
1271 "specified for \"--ech ecl:\" option",
1272 nextarg);
1273 return PARAM_BAD_USE;
1274 }
1275 err = file2string(&tmpcfg, file);
1276 if(file != stdin)
1277 curlx_fclose(file);
1278 if(err)
1279 return err;
1280 {
1281 char *tmp = curl_maprintf("ecl:%s", tmpcfg);
1282 curlx_free(tmpcfg);
1283 if(!tmp)
1284 return PARAM_NO_MEM;
1285 config->ech_config = curlx_strdup(tmp);
1286 curl_free(tmp);
1287 if(!config->ech_config)
1288 return PARAM_NO_MEM;
1289 }
1290 } /* file done */
1291 }
1292 else {
1293 /* Simple case: a string, with a keyword */
1294 err = getstr(&config->ech, nextarg, DENY_BLANK);
1295 }
1296 return err;
1297}
1299static ParameterError parse_header(struct OperationConfig *config,
1300 cmdline_t cmd,
1301 const char *nextarg)
1302{
1303 ParameterError err = PARAM_OK;
1305 /* A custom header to append to a list */
1306 if(nextarg[0] == '@') {
1307 /* read many headers from a file or stdin */
1308 bool use_stdin = !strcmp(&nextarg[1], "-");
1309 FILE *file = use_stdin ? stdin : curlx_fopen(&nextarg[1], FOPEN_READTEXT);
1310 if(!file) {
1311 errorf("Failed to open %s", &nextarg[1]);
1312 err = PARAM_READ_ERROR;
1313 }
1314 else {
1315 struct dynbuf line;
1316 bool error = FALSE;
1317 curlx_dyn_init(&line, 1024 * 100);
1318 while(my_get_line(file, &line, &error, NULL)) {
1319 const char *ptr = curlx_dyn_ptr(&line);
1320 err = add2list(cmd == C_PROXY_HEADER ? /* --proxy-header? */
1321 &config->proxyheaders :
1322 &config->headers, ptr);
1323 if(err)
1324 break;
1325 }
1326 if(error)
1327 err = PARAM_READ_ERROR;
1328 curlx_dyn_free(&line);
1329 if(!use_stdin)
1330 curlx_fclose(file);
1331 }
1332 }
1333 else {
1334 if(!strchr(nextarg, ':') && !strchr(nextarg, ';')) {
1335 warnf("The provided %s header '%s' does not look like a header?",
1336 (cmd == C_PROXY_HEADER) ? "proxy" : "HTTP", nextarg);
1337 }
1338 if(cmd == C_PROXY_HEADER) /* --proxy-header */
1339 err = add2list(&config->proxyheaders, nextarg);
1340 else
1341 err = add2list(&config->headers, nextarg);
1342 }
1343 return err;
1344}
1346static ParameterError parse_output(struct OperationConfig *config,
1347 const char *nextarg)
1348{
1349 ParameterError err = PARAM_OK;
1350 struct getout *url;
1352 /* output file */
1353 if(!config->url_out)
1354 config->url_out = config->url_list;
1355 if(config->url_out) {
1356 /* there is a node here, if it already is filled-in continue to find
1357 an "empty" node */
1358 while(config->url_out && config->url_out->outset)
1359 config->url_out = config->url_out->next;
1360 }
1362 /* now there might or might not be an available node to fill in! */
1364 if(config->url_out)
1365 /* existing node */
1366 url = config->url_out;
1367 else {
1368 /* there was no free node, create one! */
1369 config->url_out = url = new_getout(config);
1370 }
1372 if(!url)
1373 return PARAM_NO_MEM;
1375 /* fill in the outfile */
1376 if(nextarg)
1377 err = getstr(&url->outfile, nextarg, DENY_BLANK);
1378 else
1379 url->outfile = NULL;
1380 url->useremote = FALSE; /* switch off */
1381 url->outset = TRUE;
1382 url->out_null = !nextarg;
1383 return err;
1384}
1386static ParameterError parse_remote_name(struct OperationConfig *config,
1387 bool toggle)
1388{
1389 ParameterError err = PARAM_OK;
1390 struct getout *url;
1392 if(!toggle && !config->remote_name_all)
1393 return err; /* nothing to do */
1395 /* output file */
1396 if(!config->url_out)
1397 config->url_out = config->url_list;
1398 if(config->url_out) {
1399 /* there is a node here, if it already is filled-in continue to find
1400 an "empty" node */
1401 while(config->url_out && config->url_out->outset)
1402 config->url_out = config->url_out->next;
1403 }
1405 /* now there might or might not be an available node to fill in! */
1407 if(config->url_out)
1408 /* existing node */
1409 url = config->url_out;
1410 else {
1411 /* there was no free node, create one! */
1412 config->url_out = url = new_getout(config);
1413 }
1415 if(!url)
1416 return PARAM_NO_MEM;
1418 url->outfile = NULL; /* leave it */
1419 url->useremote = toggle;
1420 url->outset = TRUE;
1421 url->out_null = FALSE;
1422 return PARAM_OK;
1423}
1425static ParameterError parse_quote(struct OperationConfig *config,
1426 const char *nextarg)
1427{
1428 ParameterError err = PARAM_OK;
1430 /* QUOTE command to send to FTP server */
1431 switch(nextarg[0]) {
1432 case '-':
1433 /* prefixed with a dash makes it a POST TRANSFER one */
1434 nextarg++;
1435 err = add2list(&config->postquote, nextarg);
1436 break;
1437 case '+':
1438 /* prefixed with a plus makes it an immediately-before-transfer one */
1439 nextarg++;
1440 err = add2list(&config->prequote, nextarg);
1441 break;
1442 default:
1443 err = add2list(&config->quote, nextarg);
1444 break;
1445 }
1446 return err;
1447}
1449static ParameterError parse_range(struct OperationConfig *config,
1450 const char *nextarg)
1451{
1452 ParameterError err = PARAM_OK;
1453 curl_off_t value;
1454 const char *orig = nextarg;
1456 if(config->use_resume) {
1457 errorf("--continue-at is mutually exclusive with --range");
1458 return PARAM_BAD_USE;
1459 }
1460 if(!curlx_str_number(&nextarg, &value, CURL_OFF_T_MAX) &&
1461 curlx_str_single(&nextarg, '-')) {
1462 /* Specifying a range WITHOUT A DASH does create an illegal HTTP range
1463 (and does not actually be range by definition). The man page previously
1464 claimed that to be a good way, why this code is added to work around
1465 it. */
1466 char buffer[32];
1467 warnf("A specified range MUST include at least one dash (-). "
1468 "Appending one for you");
1469 curl_msnprintf(buffer, sizeof(buffer), "%" CURL_FORMAT_CURL_OFF_T "-",
1470 value);
1471 curlx_free(config->range);
1472 config->range = curlx_strdup(buffer);
1473 if(!config->range)
1474 err = PARAM_NO_MEM;
1475 }
1476 else {
1477 /* byte range requested */
1478 while(*nextarg) {
1479 if(!ISDIGIT(*nextarg) && *nextarg != '-' && *nextarg != ',') {
1480 warnf("Invalid character is found in given range. "
1481 "A specified range MUST have only digits in "
1482 "\'start\'-\'stop\'. The server's response to this "
1483 "request is uncertain.");
1484 break;
1485 }
1486 nextarg++;
1487 }
1488 err = getstr(&config->range, orig, DENY_BLANK);
1489 }
1490 return err;
1491}
1493static ParameterError parse_upload_file(struct OperationConfig *config,
1494 const char *nextarg)
1495{
1496 ParameterError err = PARAM_OK;
1497 struct getout *url;
1499 /* we are uploading */
1500 if(!config->url_ul)
1501 config->url_ul = config->url_list;
1502 if(config->url_ul) {
1503 /* there is a node here, if it already is filled-in continue to find
1504 an "empty" node */
1505 while(config->url_ul && config->url_ul->uploadset)
1506 config->url_ul = config->url_ul->next;
1507 }
1509 /* now there might or might not be an available node to fill in! */
1511 if(config->url_ul)
1512 /* existing node */
1513 url = config->url_ul;
1514 else
1515 /* there was no free node, create one! */
1516 config->url_ul = url = new_getout(config);
1518 if(!url)
1519 return PARAM_NO_MEM;
1521 url->uploadset = TRUE; /* mark -T used */
1522 if(!*nextarg)
1523 url->noupload = TRUE;
1524 else {
1525 /* "-" equals stdin, but keep the string around for now */
1526 err = getstr(&url->infile, nextarg, DENY_BLANK);
1527 }
1528 return err;
1529}
1531static size_t verbose_nopts;
1533static ParameterError parse_verbose(bool toggle)
1534{
1535 ParameterError err = PARAM_OK;
1537 /* This option is a super-boolean with side effect when applied
1538 * more than once in the same argument flag, like `-vvv`. */
1539 if(!toggle) {
1540 global->verbosity = 0;
1541 if(set_trace_config("-all"))
1542 err = PARAM_NO_MEM;
1543 global->tracetype = TRACE_NONE;
1544 return err;
1545 }
1546 else if(!verbose_nopts) {
1547 /* first `-v` in an argument resets to base verbosity */
1548 global->verbosity = 0;
1549 if(!global->trace_set && set_trace_config("-all"))
1550 return PARAM_NO_MEM;
1551 }
1552 /* the '%' thing here causes the trace get sent to stderr */
1553 switch(global->verbosity) {
1554 case 0:
1555 global->verbosity = 1;
1556 curlx_free(global->trace_dump);
1557 global->trace_dump = curlx_strdup("%");
1558 if(!global->trace_dump)
1559 err = PARAM_NO_MEM;
1560 else {
1561 if(global->tracetype && (global->tracetype != TRACE_PLAIN))
1562 warnf("-v, --verbose overrides an earlier trace option");
1563 global->tracetype = TRACE_PLAIN;
1564 }
1565 break;
1566 case 1:
1567 global->verbosity = 2;
1568 if(set_trace_config("ids,time,protocol"))
1569 err = PARAM_NO_MEM;
1570 break;
1571 case 2:
1572 global->verbosity = 3;
1573 global->tracetype = TRACE_ASCII;
1574 if(set_trace_config("ssl,read,write"))
1575 err = PARAM_NO_MEM;
1576 break;
1577 case 3:
1578 global->verbosity = 4;
1579 if(set_trace_config("network"))
1580 err = PARAM_NO_MEM;
1581 break;
1582 default:
1583 /* no effect for now */
1584 break;
1585 }
1586 return err;
1587}
1589static ParameterError parse_writeout(struct OperationConfig *config,
1590 const char *nextarg)
1591{
1592 ParameterError err = PARAM_OK;
1594 /* get the output string */
1595 if('@' == *nextarg) {
1596 /* the data begins with a '@' letter, it means that a filename
1597 or - (stdin) follows */
1598 FILE *file;
1599 const char *fname;
1600 nextarg++; /* pass the @ */
1601 if(!strcmp("-", nextarg)) {
1602 fname = "<stdin>";
1603 file = stdin;
1604 }
1605 else {
1606 fname = nextarg;
1607 file = curlx_fopen(fname, FOPEN_READTEXT);
1608 if(!file) {
1609 errorf("Failed to open %s", fname);
1610 return PARAM_READ_ERROR;
1611 }
1612 }
1613 curlx_safefree(config->writeout);
1614 err = file2string(&config->writeout, file);
1615 if(file && (file != stdin))
1616 curlx_fclose(file);
1617 if(err)
1618 return err;
1619 if(!config->writeout)
1620 warnf("Failed to read %s", fname);
1621 }
1622 else
1623 err = getstr(&config->writeout, nextarg, ALLOW_BLANK);
1625 return err;
1626}
1628static ParameterError parse_time_cond(struct OperationConfig *config,
1629 const char *nextarg)
1630{
1631 ParameterError err = PARAM_OK;
1633 switch(*nextarg) {
1634 case '+':
1635 nextarg++;
1636 FALLTHROUGH();
1637 default:
1638 /* If-Modified-Since: (section 14.28 in RFC2068) */
1639 config->timecond = CURL_TIMECOND_IFMODSINCE;
1640 break;
1641 case '-':
1642 /* If-Unmodified-Since: (section 14.24 in RFC2068) */
1643 config->timecond = CURL_TIMECOND_IFUNMODSINCE;
1644 nextarg++;
1645 break;
1646 case '=':
1647 /* Last-Modified: (section 14.29 in RFC2068) */
1648 config->timecond = CURL_TIMECOND_LASTMOD;
1649 nextarg++;
1650 break;
1651 }
1652 config->condtime = (curl_off_t)curl_getdate(nextarg, NULL);
1653 if(config->condtime == -1) {
1654 curl_off_t value;
1655 /* now let's see if it is a filename to get the time from instead! */
1656 int rc = getfiletime(nextarg, &value);
1657 if(!rc)
1658 /* pull the time out from the file */
1659 config->condtime = value;
1660 else {
1661 /* failed, remove time condition */
1662 config->timecond = CURL_TIMECOND_NONE;
1663 warnf("Illegal date format for -z, --time-cond (and not "
1664 "a filename). Disabling time condition. "
1665 "See curl_getdate(3) for valid date syntax.");
1666 }
1667 }
1668 return err;
1669}
1671struct flagmap {
1672 const char *name;
1673 size_t len;
1674 unsigned char flag;
1675};
1677static const struct flagmap flag_table[] = {
1678 { "answered", 8, CURLULFLAG_ANSWERED },
1679 { "deleted", 7, CURLULFLAG_DELETED },
1680 { "draft", 5, CURLULFLAG_DRAFT },
1681 { "flagged", 7, CURLULFLAG_FLAGGED },
1682 { "seen", 4, CURLULFLAG_SEEN },
1683 { NULL, 0, 0 }
1684};
1686static ParameterError parse_upload_flags(struct OperationConfig *config,
1687 const char *flag)
1688{
1689 ParameterError err = PARAM_OK;
1691 while(flag) {
1692 bool negate;
1693 const struct flagmap *map;
1694 size_t len;
1695 const char *next = strchr(flag, ','); /* Find next comma or end */
1696 if(next)
1697 len = next - flag;
1698 else
1699 len = strlen(flag);
1701 negate = (*flag == '-');
1702 if(negate) {
1703 flag++;
1704 len--;
1705 }
1707 for(map = flag_table; map->name; map++) {
1708 if((len == map->len) && !strncmp(flag, map->name, map->len)) {
1709 if(negate)
1710 config->upload_flags &= (unsigned char)~map->flag;
1711 else
1712 config->upload_flags |= map->flag;
1713 break;
1714 }
1715 }
1717 if(!map->name) {
1718 err = PARAM_OPTION_UNKNOWN;
1719 break;
1720 }
1722 if(next)
1723 /* move over the comma */
1724 next++;
1725 flag = next;
1726 }
1728 return err;
1729}
1731/* if 'toggle' is TRUE, set the 'bits' in 'modify'.
1732 if 'toggle' is FALSE, clear the 'bits' in 'modify' */
1733static void togglebit(bool toggle, unsigned long *modify, unsigned long bits)
1734{
1735 if(toggle)
1736 *modify |= bits;
1737 else
1738 *modify &= ~bits;
1739}
1741/* opt_depr is the function that handles ARG_DEPR options */
1742static void opt_depr(const struct LongShort *a)
1743{
1744 warnf("--%s is deprecated and has no function anymore", a->lname);
1745}
1747static ParameterError opt_sslver(struct OperationConfig *config,
1748 unsigned char ver)
1749{
1750 if(config->ssl_version_max &&
1751 (config->ssl_version_max < ver)) {
1752 errorf("Minimum TLS version set higher than max");
1753 return PARAM_BAD_USE;
1754 }
1755 config->ssl_version = ver;
1756 return PARAM_OK;
1757}
1759/* opt_none is the function that handles ARG_NONE options */
1760static ParameterError opt_none(struct OperationConfig *config,
1761 const struct LongShort *a)
1762{
1763 ParameterError err = PARAM_OK;
1764 switch(a->cmd) {
1765 case C_ANYAUTH: /* --anyauth */
1766 config->authtype = CURLAUTH_ANY;
1767 break;
1768 case C_DUMP_CA_EMBED: /* --dump-ca-embed */
1769 return PARAM_CA_EMBED_REQUESTED;
1770 case C_FTP_PASV: /* --ftp-pasv */
1771 curlx_safefree(config->ftpport);
1772 break;
1774 case C_HTTP1_0: /* --http1.0 */
1775 /* HTTP version 1.0 */
1776 sethttpver(config, CURL_HTTP_VERSION_1_0);
1777 break;
1778 case C_HTTP1_1: /* --http1.1 */
1779 /* HTTP version 1.1 */
1780 sethttpver(config, CURL_HTTP_VERSION_1_1);
1781 break;
1782 case C_HTTP2: /* --http2 */
1783 /* HTTP version 2.0 */
1784 if(!feature_http2)
1785 return PARAM_LIBCURL_DOESNT_SUPPORT;
1786 sethttpver(config, CURL_HTTP_VERSION_2_0);
1787 break;
1788 case C_HTTP2_PRIOR_KNOWLEDGE: /* --http2-prior-knowledge */
1789 /* HTTP version 2.0 over clean TCP */
1790 if(!feature_http2)
1791 return PARAM_LIBCURL_DOESNT_SUPPORT;
1792 sethttpver(config, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
1793 break;
1794 case C_HTTP3: /* --http3: */
1795 /* Try HTTP/3, allow fallback */
1796 if(!feature_http3)
1797 return PARAM_LIBCURL_DOESNT_SUPPORT;
1798 else
1799 sethttpver(config, CURL_HTTP_VERSION_3);
1800 break;
1801 case C_HTTP3_ONLY: /* --http3-only */
1802 /* Try HTTP/3 without fallback */
1803 if(!feature_http3)
1804 return PARAM_LIBCURL_DOESNT_SUPPORT;
1805 else
1806 sethttpver(config, CURL_HTTP_VERSION_3ONLY);
1807 break;
1808 case C_TLSV1: /* --tlsv1 */
1809 err = opt_sslver(config, 1);
1810 break;
1811 case C_TLSV1_0: /* --tlsv1.0 */
1812 err = opt_sslver(config, 1);
1813 break;
1814 case C_TLSV1_1: /* --tlsv1.1 */
1815 err = opt_sslver(config, 2);
1816 break;
1817 case C_TLSV1_2: /* --tlsv1.2 */
1818 err = opt_sslver(config, 3);
1819 break;
1820 case C_TLSV1_3: /* --tlsv1.3 */
1821 err = opt_sslver(config, 4);
1822 break;
1823 case C_IPV4: /* --ipv4 */
1824 config->ip_version = CURL_IPRESOLVE_V4;
1825 break;
1826 case C_IPV6: /* --ipv6 */
1827 config->ip_version = CURL_IPRESOLVE_V6;
1828 break;
1829 case C_NEXT: /* --next */
1830 return PARAM_NEXT_OPERATION;
1831 case C_PROXY_TLSV1: /* --proxy-tlsv1 */
1832 /* TLS version 1 for proxy */
1833 config->proxy_ssl_version = CURL_SSLVERSION_TLSv1;
1834 break;
1835 }
1836 return err;
1837}
1839/* opt_bool is the function that handles boolean options */
1840static ParameterError opt_bool(struct OperationConfig *config,
1841 const struct LongShort *a,
1842 bool toggle)
1843{
1844 switch(a->cmd) {
1845 case C_ALPN: /* --alpn */
1846 config->noalpn = !toggle;
1847 break;
1848 case C_DISABLE_EPSV: /* --disable-epsv */
1849 config->disable_epsv = toggle;
1850 break;
1851 case C_DISALLOW_USERNAME_IN_URL: /* --disallow-username-in-url */
1852 config->disallow_username_in_url = toggle;
1853 break;
1854 case C_EPSV: /* --epsv */
1855 config->disable_epsv = !toggle;
1856 break;
1857 case C_COMPRESSED: /* --compressed */
1858 if(toggle && !(feature_libz || feature_brotli || feature_zstd))
1859 return PARAM_LIBCURL_DOESNT_SUPPORT;
1860 else
1861 config->encoding = toggle;
1862 break;
1863 case C_TR_ENCODING: /* --tr-encoding */
1864 config->tr_encoding = toggle;
1865 break;
1866 case C_DIGEST: /* --digest */
1867 togglebit(toggle, &config->authtype, CURLAUTH_DIGEST);
1868 break;
1869 case C_FTP_CREATE_DIRS: /* --ftp-create-dirs */
1870 config->ftp_create_dirs = toggle;
1871 break;
1872 case C_CREATE_DIRS: /* --create-dirs */
1873 config->create_dirs = toggle;
1874 break;
1875 case C_PROXY_NTLM: /* --proxy-ntlm */
1876 if(!feature_ntlm)
1877 return PARAM_LIBCURL_DOESNT_SUPPORT;
1878 else
1879 config->proxyntlm = toggle;
1880 break;
1881 case C_CRLF: /* --crlf */
1882 config->crlf = toggle;
1883 break;
1884 case C_HAPROXY_PROTOCOL: /* --haproxy-protocol */
1885 config->haproxy_protocol = toggle;
1886 break;
1887 case C_NEGOTIATE: /* --negotiate */
1888 if(!feature_spnego && toggle)
1889 return PARAM_LIBCURL_DOESNT_SUPPORT;
1890 togglebit(toggle, &config->authtype, CURLAUTH_NEGOTIATE);
1891 break;
1892 case C_NTLM: /* --ntlm */
1893 if(!feature_ntlm && toggle)
1894 return PARAM_LIBCURL_DOESNT_SUPPORT;
1895 togglebit(toggle, &config->authtype, CURLAUTH_NTLM);
1896 break;
1897 case C_OUT_NULL: /* --out-null */
1898 return parse_output(config, NULL);
1899 case C_BASIC: /* --basic */
1900 togglebit(toggle, &config->authtype, CURLAUTH_BASIC);
1901 break;
1902#ifdef USE_WATT32
1903 case C_WDEBUG: /* --wdebug */
1904 dbug_init();
1905 break;
1906#endif
1907 case C_DISABLE_EPRT: /* --disable-eprt */
1908 config->disable_eprt = toggle;
1909 break;
1910 case C_EPRT: /* --eprt */
1911 config->disable_eprt = !toggle;
1912 break;
1913 case C_XATTR: /* --xattr */
1914 config->xattr = toggle;
1915 break;
1916 case C_FTP_SSL: /* --ftp-ssl */
1917 case C_SSL: /* --ssl */
1918 config->ftp_ssl = toggle;
1919 if(config->ftp_ssl)
1920 warnf("--%s is an insecure option, consider --ssl-reqd instead",
1921 a->lname);
1922 if(toggle && config->ftp_ssl_control) {
1923 config->ftp_ssl_control = FALSE;
1924 warnf("--%s overrides --ftp-ssl-control", a->lname);
1925 }
1926 break;
1927 case C_FTP_SSL_CCC: /* --ftp-ssl-ccc */
1928 config->ftp_ssl_ccc = toggle;
1929 if(!config->ftp_ssl_ccc_mode)
1930 config->ftp_ssl_ccc_mode = CURLFTPSSL_CCC_PASSIVE;
1931 break;
1932 case C_TCP_NODELAY: /* --tcp-nodelay */
1933 config->tcp_nodelay = toggle;
1934 break;
1935 case C_PROXY_DIGEST: /* --proxy-digest */
1936 config->proxydigest = toggle;
1937 break;
1938 case C_PROXY_BASIC: /* --proxy-basic */
1939 config->proxybasic = toggle;
1940 break;
1941 case C_RETRY_CONNREFUSED: /* --retry-connrefused */
1942 config->retry_connrefused = toggle;
1943 break;
1944 case C_RETRY_ALL_ERRORS: /* --retry-all-errors */
1945 config->retry_all_errors = toggle;
1946 break;
1947 case C_PROXY_NEGOTIATE: /* --proxy-negotiate */
1948 if(!feature_spnego)
1949 return PARAM_LIBCURL_DOESNT_SUPPORT;
1950 else
1951 config->proxynegotiate = toggle;
1952 break;
1953 case C_FORM_ESCAPE: /* --form-escape */
1954 togglebit(toggle, &config->mime_options, CURLMIMEOPT_FORMESCAPE);
1955 break;
1956 case C_PROXY_ANYAUTH: /* --proxy-anyauth */
1957 config->proxyanyauth = toggle;
1958 break;
1959 case C_TRACE_TIME: /* --trace-time */
1960 global->tracetime = toggle;
1961 break;
1962 case C_IGNORE_CONTENT_LENGTH: /* --ignore-content-length */
1963 config->ignorecl = toggle;
1964 break;
1965 case C_FTP_SKIP_PASV_IP: /* --ftp-skip-pasv-ip */
1966 config->ftp_skip_ip = toggle;
1967 break;
1968 case C_FTP_SSL_REQD: /* --ftp-ssl-reqd */
1969 case C_SSL_REQD: /* --ssl-reqd */
1970 config->ftp_ssl_reqd = toggle;
1971 break;
1972 case C_SESSIONID: /* --sessionid */
1973 config->disable_sessionid = !toggle;
1974 break;
1975 case C_FTP_SSL_CONTROL: /* --ftp-ssl-control */
1976 config->ftp_ssl_control = toggle;
1977 if(toggle && config->ftp_ssl) {
1978 config->ftp_ssl = FALSE;
1979 warnf("--%s overrides --ssl", a->lname);
1980 }
1981 break;
1982 case C_RAW: /* --raw */
1983 config->raw = toggle;
1984 break;
1985 case C_KEEPALIVE: /* --keepalive */
1986 config->nokeepalive = !toggle;
1987 break;
1988 case C_POST301: /* --post301 */
1989 config->post301 = toggle;
1990 break;
1991 case C_POST302: /* --post302 */
1992 config->post302 = toggle;
1993 break;
1994 case C_POST303: /* --post303 */
1995 config->post303 = toggle;
1996 break;
1997 case C_SOCKS5_GSSAPI_NEC: /* --socks5-gssapi-nec */
1998 config->socks5_gssapi_nec = toggle;
1999 break;
2000 case C_FTP_PRET: /* --ftp-pret */
2001 config->ftp_pret = toggle;
2002 break;
2003 case C_SASL_IR: /* --sasl-ir */
2004 config->sasl_ir = toggle;
2005 break;
2006#ifdef DEBUGBUILD
2007 case C_TEST_DUPHANDLE: /* --test-duphandle */
2008 global->test_duphandle = toggle;
2009 break;
2010 case C_TEST_EVENT: /* --test-event */
2011 global->test_event_based = toggle;
2012 break;
2013#endif
2014 case C_PATH_AS_IS: /* --path-as-is */
2015 config->path_as_is = toggle;
2016 break;
2017 case C_TFTP_NO_OPTIONS: /* --tftp-no-options */
2018 config->tftp_no_options = toggle;
2019 break;
2020 case C_TLS_EARLYDATA: /* --tls-earlydata */
2021 config->ssl_allow_earlydata = toggle;
2022 break;
2023 case C_SUPPRESS_CONNECT_HEADERS: /* --suppress-connect-headers */
2024 config->suppress_connect_headers = toggle;
2025 break;
2026 case C_COMPRESSED_SSH: /* --compressed-ssh */
2027 config->ssh_compression = toggle;
2028 break;
2029 case C_TRACE_IDS: /* --trace-ids */
2030 global->traceids = toggle;
2031 break;
2032 case C_PROGRESS_METER: /* --progress-meter */
2033 global->noprogress = !toggle;
2034 break;
2035 case C_PROGRESS_BAR: /* --progress-bar */
2036 global->progressmode = toggle ? CURL_PROGRESS_BAR : CURL_PROGRESS_STATS;
2037 break;
2038 case C_HTTP0_9: /* --http0.9 */
2039 config->http09_allowed = toggle;
2040 break;
2041 case C_PROXY_HTTP2: /* --proxy-http2 */
2042 if(!feature_httpsproxy || !feature_http2)
2043 return PARAM_LIBCURL_DOESNT_SUPPORT;
2045 config->proxyver = toggle ? CURLPROXY_HTTPS2 : CURLPROXY_HTTPS;
2046 break;
2047 case C_PROXY_HTTP3: /* --proxy-http3 */
2048#ifndef USE_PROXY_HTTP3
2049 if(toggle)
2050 return PARAM_LIBCURL_DOESNT_SUPPORT;
2051 config->proxyver = CURLPROXY_HTTPS;
2052#else
2053 if(!feature_httpsproxy || !feature_http3)
2054 return PARAM_LIBCURL_DOESNT_SUPPORT;
2056 config->proxyver = toggle ? CURLPROXY_HTTPS3 : CURLPROXY_HTTPS;
2057#endif
2058 break;
2059 case C_APPEND: /* --append */
2060 config->ftp_append = toggle;
2061 break;
2062 case C_USE_ASCII: /* --use-ascii */
2063 config->use_ascii = toggle;
2064 break;
2065 case C_CA_NATIVE: /* --ca-native */
2066 config->native_ca_store = toggle;
2067 break;
2068 case C_PROXY_CA_NATIVE: /* --proxy-ca-native */
2069 config->proxy_native_ca_store = toggle;
2070 break;
2071 case C_SSL_ALLOW_BEAST: /* --ssl-allow-beast */
2072 config->ssl_allow_beast = toggle;
2073 break;
2074 case C_SSL_AUTO_CLIENT_CERT: /* --ssl-auto-client-cert */
2075 config->ssl_auto_client_cert = toggle;
2076 break;
2077 case C_PROXY_SSL_AUTO_CLIENT_CERT: /* --proxy-ssl-auto-client-cert */
2078 config->proxy_ssl_auto_client_cert = toggle;
2079 break;
2080 case C_CERT_STATUS: /* --cert-status */
2081 config->verifystatus = toggle;
2082 break;
2083 case C_DOH_CERT_STATUS: /* --doh-cert-status */
2084 config->doh_verifystatus = toggle;
2085 break;
2086 case C_FALSE_START: /* --false-start */
2087 opt_depr(a);
2088 break;
2089 case C_SSL_NO_REVOKE: /* --ssl-no-revoke */
2090 config->ssl_no_revoke = toggle;
2091 break;
2092 case C_SSL_REVOKE_BEST_EFFORT: /* --ssl-revoke-best-effort */
2093 config->ssl_revoke_best_effort = toggle;
2094 break;
2095 case C_TCP_FASTOPEN: /* --tcp-fastopen */
2096 config->tcp_fastopen = toggle;
2097 break;
2098 case C_PROXY_SSL_ALLOW_BEAST: /* --proxy-ssl-allow-beast */
2099 config->proxy_ssl_allow_beast = toggle;
2100 break;
2101 case C_PROXY_INSECURE: /* --proxy-insecure */
2102 config->proxy_insecure_ok = toggle;
2103 break;
2104 case C_SOCKS5_BASIC: /* --socks5-basic */
2105 togglebit(toggle, &config->socks5_auth, CURLAUTH_BASIC);
2106 break;
2107 case C_SOCKS5_GSSAPI: /* --socks5-gssapi */
2108 togglebit(toggle, &config->socks5_auth, CURLAUTH_GSSAPI);
2109 break;
2110 case C_FAIL_EARLY: /* --fail-early */
2111 global->fail_early = toggle;
2112 break;
2113 case C_STYLED_OUTPUT: /* --styled-output */
2114 global->styled_output = toggle;
2115 break;
2116 case C_MAIL_RCPT_ALLOWFAILS: /* --mail-rcpt-allowfails */
2117 config->mail_rcpt_allowfails = toggle;
2118 break;
2119 case C_REMOVE_ON_ERROR: /* --remove-on-error */
2120 if(config->use_resume && toggle) {
2121 errorf("--continue-at is mutually exclusive with --remove-on-error");
2122 return PARAM_BAD_USE;
2123 }
2124 config->rm_partial = toggle;
2125 break;
2126 case C_FAIL: /* --fail without body */
2127 if(toggle && (config->fail == FAIL_WITH_BODY))
2128 warnf("--fail deselects --fail-with-body here");
2129 config->fail = toggle ? FAIL_WO_BODY : FAIL_NONE;
2130 break;
2131 case C_FAIL_WITH_BODY: /* --fail-with-body */
2132 if(toggle && (config->fail == FAIL_WO_BODY))
2133 warnf("--fail-with-body deselects --fail here");
2134 config->fail = toggle ? FAIL_WITH_BODY : FAIL_NONE;
2135 break;
2136 case C_GLOBOFF: /* --globoff */
2137 config->globoff = toggle;
2138 break;
2139 case C_GET: /* --get */
2140 config->use_httpget = toggle;
2141 break;
2142 case C_INCLUDE: /* --include */
2143 case C_SHOW_HEADERS: /* --show-headers */
2144 config->show_headers = toggle;
2145 break;
2146 case C_JUNK_SESSION_COOKIES: /* --junk-session-cookies */
2147 config->cookiesession = toggle;
2148 break;
2149 case C_HEAD: /* --head */
2150 config->no_body = toggle;
2151 config->show_headers = toggle;
2152 if(SetHTTPrequest(config->no_body ? TOOL_HTTPREQ_HEAD :
2153 TOOL_HTTPREQ_GET, &config->httpreq))
2154 return PARAM_BAD_USE;
2155 break;
2156 case C_REMOTE_HEADER_NAME: /* --remote-header-name */
2157 config->content_disposition = toggle;
2158 config->suppress_connect_headers = toggle;
2159 break;
2160 case C_INSECURE: /* --insecure */
2161 config->insecure_ok = toggle;
2162 break;
2163 case C_DOH_INSECURE: /* --doh-insecure */
2164 config->doh_insecure_ok = toggle;
2165 break;
2166 case C_LIST_ONLY: /* --list-only */
2167 config->dirlistonly = toggle; /* only list names of the FTP directory */
2168 break;
2169 case C_MANUAL: /* --manual */
2170 if(toggle) /* --no-manual shows no manual... */
2171 return PARAM_MANUAL_REQUESTED;
2172 break;
2173 case C_NETRC_OPTIONAL: /* --netrc-optional */
2174 config->netrc_opt = toggle;
2175 break;
2176 case C_NETRC: /* --netrc */
2177 config->netrc = toggle;
2178 break;
2179 case C_BUFFER: /* --buffer */
2180 config->nobuffer = !toggle;
2181 break;
2182 case C_REMOTE_NAME_ALL: /* --remote-name-all */
2183 config->remote_name_all = toggle;
2184 break;
2185 case C_CLOBBER: /* --clobber */
2186 if(config->use_resume && !toggle) {
2187 errorf("--continue-at is mutually exclusive with --no-clobber");
2188 return PARAM_BAD_USE;
2189 }
2190 config->file_clobber_mode = toggle ? CLOBBER_ALWAYS : CLOBBER_NEVER;
2191 break;
2192 case C_REMOTE_NAME: /* --remote-name */
2193 return parse_remote_name(config, toggle);
2194 case C_PROXYTUNNEL: /* --proxytunnel */
2195 config->proxytunnel = toggle;
2196 break;
2197 case C_DISABLE: /* --disable */
2198 /* if used first, already taken care of, we do it like this so we do not
2199 cause an error! */
2200 break;
2201 case C_REMOTE_TIME: /* --remote-time */
2202 config->remote_time = toggle;
2203 break;
2204 case C_SILENT: /* --silent */
2205 global->silent = toggle;
2206 break;
2207 case C_SKIP_EXISTING: /* --skip-existing */
2208 config->skip_existing = toggle;
2209 break;
2210 case C_SHOW_ERROR: /* --show-error */
2211 global->showerror = toggle;
2212 break;
2213 case C_VERBOSE: /* --verbose */
2214 return parse_verbose(toggle);
2215 case C_VERSION: /* --version */
2216 if(toggle) /* --no-version yields no output! */
2217 return PARAM_VERSION_INFO_REQUESTED;
2218 break;
2219 case C_PARALLEL: /* --parallel */
2220 global->parallel = toggle;
2221 break;
2222 case C_PARALLEL_IMMEDIATE: /* --parallel-immediate */
2223 global->parallel_connect = toggle;
2224 break;
2225 case C_MPTCP: /* --mptcp */
2226 config->mptcp = toggle;
2227 break;
2228 case C_LOCATION_TRUSTED: /* --location-trusted */
2229 config->unrestricted_auth = toggle;
2230 FALLTHROUGH();
2231 case C_LOCATION: /* --location */
2232 if(config->followlocation == CURLFOLLOW_OBEYCODE)
2233 warnf("--location overrides --follow");
2234 config->followlocation = toggle ? CURLFOLLOW_ALL : 0;
2235 break;
2236 case C_FOLLOW: /* --follow */
2237 if(config->followlocation == CURLFOLLOW_ALL)
2238 warnf("--follow overrides --location");
2239 config->followlocation = toggle ? CURLFOLLOW_OBEYCODE : 0;
2240 break;
2241 default:
2242 return PARAM_OPTION_UNKNOWN;
2243 }
2244 return PARAM_OK;
2245}
2247static ParameterError existingfile(char **store,
2248 const struct LongShort *a,
2249 const char *filename)
2250{
2251 curlx_struct_stat info;
2252 if(curlx_stat(filename, &info)) {
2253 errorf("The file '%s' provided to --%s does not exist",
2254 filename, a->lname);
2255 return PARAM_BAD_USE;
2256 }
2257 return getstr(store, filename, DENY_BLANK);
2258}
2260/* opt_file handles file options */
2261static ParameterError opt_file(struct OperationConfig *config,
2262 const struct LongShort *a,
2263 const char *nextarg,
2264 int max_recursive)
2265{
2266 ParameterError err = PARAM_OK;
2267 if((nextarg[0] == '-') && nextarg[1]) {
2268 /* if the filename looks like a command line option */
2269 warnf("The filename argument '%s' looks like a flag.", nextarg);
2270 }
2271 switch(a->cmd) {
2272 case C_ABSTRACT_UNIX_SOCKET: /* --abstract-unix-socket */
2273 config->abstract_unix_socket = TRUE;
2274 err = getstr(&config->unix_socket_path, nextarg, DENY_BLANK);
2275 break;
2276 case C_CACERT: /* --cacert */
2277 err = existingfile(&config->cacert, a, nextarg);
2278 break;
2279 case C_CAPATH: /* --capath */
2280 err = getstr(&config->capath, nextarg, DENY_BLANK);
2281 break;
2282 case C_CERT: /* --cert */
2283 err = GetFileAndPassword(nextarg, &config->cert, &config->key_passwd);
2284 break;
2285 case C_CONFIG: /* --config */
2286 if(--max_recursive < 0) {
2287 errorf("Max config file recursion level reached (%d)",
2288 CONFIG_MAX_LEVELS);
2289 err = PARAM_BAD_USE;
2290 }
2291 else {
2292 err = parseconfig(nextarg, max_recursive, NULL);
2293 }
2294 break;
2295 case C_CRLFILE: /* --crlfile */
2296 err = existingfile(&config->crlfile, a, nextarg);
2297 break;
2298 case C_DUMP_HEADER: /* --dump-header */
2299 err = getstr(&config->headerfile, nextarg, DENY_BLANK);
2300 break;
2301 case C_ETAG_SAVE: /* --etag-save */
2302 if(config->num_urls > 1) {
2303 errorf("The etag options only work on a single URL");
2304 err = PARAM_BAD_USE;
2305 }
2306 else
2307 err = getstr(&config->etag_save_file, nextarg, DENY_BLANK);
2308 break;
2309 case C_ETAG_COMPARE: /* --etag-compare */
2310 if(config->num_urls > 1) {
2311 errorf("The etag options only work on a single URL");
2312 err = PARAM_BAD_USE;
2313 }
2314 else
2315 err = getstr(&config->etag_compare_file, nextarg, DENY_BLANK);
2316 break;
2317 case C_KEY: /* --key */
2318 err = getstr(&config->key, nextarg, DENY_BLANK);
2319 break;
2320 case C_KNOWNHOSTS: /* --knownhosts */
2321 err = existingfile(&config->knownhosts, a, nextarg);
2322 break;
2323 case C_NETRC_FILE: /* --netrc-file */
2324 err = existingfile(&config->netrc_file, a, nextarg);
2325 break;
2326 case C_OUTPUT: /* --output */
2327 err = parse_output(config, nextarg);
2328 break;
2329 case C_PROXY_CACERT: /* --proxy-cacert */
2330 err = existingfile(&config->proxy_cacert, a, nextarg);
2331 break;
2332 case C_PROXY_CAPATH: /* --proxy-capath */
2333 err = getstr(&config->proxy_capath, nextarg, DENY_BLANK);
2334 break;
2335 case C_PROXY_CERT: /* --proxy-cert */
2336 err = GetFileAndPassword(nextarg, &config->proxy_cert,
2337 &config->proxy_key_passwd);
2338 break;
2339 case C_PROXY_CRLFILE: /* --proxy-crlfile */
2340 err = existingfile(&config->proxy_crlfile, a, nextarg);
2341 break;
2342 case C_PROXY_KEY: /* --proxy-key */
2343 err = getstr(&config->proxy_key, nextarg, ALLOW_BLANK);
2344 break;
2345 case C_SSL_SESSIONS: /* --ssl-sessions */
2346 if(feature_ssls_export)
2347 err = getstr(&global->ssl_sessions, nextarg, DENY_BLANK);
2348 else
2349 err = PARAM_LIBCURL_DOESNT_SUPPORT;
2350 break;
2351 case C_STDERR: /* --stderr */
2352 tool_set_stderr_file(nextarg);
2353 break;
2354 case C_TRACE: /* --trace */
2355 err = getstr(&global->trace_dump, nextarg, DENY_BLANK);
2356 if(!err) {
2357 if(global->tracetype && (global->tracetype != TRACE_BIN))
2358 warnf("--trace overrides an earlier trace/verbose option");
2359 global->tracetype = TRACE_BIN;
2360 }
2361 break;
2362 case C_TRACE_ASCII: /* --trace-ascii */
2363 err = getstr(&global->trace_dump, nextarg, DENY_BLANK);
2364 if(!err) {
2365 if(global->tracetype && (global->tracetype != TRACE_ASCII))
2366 warnf("--trace-ascii overrides an earlier trace/verbose option");
2367 global->tracetype = TRACE_ASCII;
2368 }
2369 break;
2370 case C_UNIX_SOCKET: /* --unix-socket */
2371 config->abstract_unix_socket = FALSE;
2372 err = getstr(&config->unix_socket_path, nextarg, DENY_BLANK);
2373 break;
2374 case C_UPLOAD_FILE: /* --upload-file */
2375 err = parse_upload_file(config, nextarg);
2376 break;
2377 case C_HTTPSIG_KEY: /* --httpsig-key */
2378 if(!feature_httpsig)
2379 err = PARAM_LIBCURL_DOESNT_SUPPORT;
2380 else
2381 err = getstr(&config->httpsig_key, nextarg, DENY_BLANK);
2382 break;
2383 }
2384 return err;
2385}
2387/* options that accept unsigned numbers */
2388static ParameterError opt_unum(struct OperationConfig *config,
2389 const struct LongShort *a,
2390 const char *nextarg)
2391{
2392 ParameterError err = PARAM_OK;
2393 long val;
2394 err = str2unum(&val, nextarg);
2395 if(err)
2396 return err;
2397 switch(a->cmd) {
2398 case C_VLAN_PRIORITY: /* --vlan-priority */
2399 if(val > 7)
2400 return PARAM_NUMBER_TOO_LARGE;
2401 config->vlan_priority = val;
2402 break;
2403 case C_RETRY: /* --retry */
2404 config->req_retry = val;
2405 break;
2406 case C_KEEPALIVE_TIME: /* --keepalive-time */
2407 config->alivetime = val;
2408 break;
2409 case C_KEEPALIVE_CNT: /* --keepalive-cnt */
2410 config->alivecnt = val;
2411 break;
2412 case C_TFTP_BLKSIZE: /* --tftp-blksize */
2413 config->tftp_blksize = val;
2414 break;
2415 case C_HAPPY_EYEBALLS_TIMEOUT_MS: /* --happy-eyeballs-timeout-ms */
2416 config->happy_eyeballs_timeout_ms = val;
2417 /* 0 is a valid value for this timeout */
2418 break;
2419 case C_SPEED_TIME: /* --speed-time */
2420 /* low speed time */
2421 config->low_speed_time = val;
2422 if(!config->low_speed_limit)
2423 config->low_speed_limit = 1;
2424 break;
2425 case C_SPEED_LIMIT: /* --speed-limit */
2426 /* low speed limit */
2427 config->low_speed_limit = val;
2428 if(!config->low_speed_time)
2429 config->low_speed_time = 30;
2430 break;
2431 case C_PARALLEL_HOST: /* --parallel-max-host */
2432 if(val > MAX_PARALLEL_HOST)
2433 global->parallel_host = MAX_PARALLEL_HOST;
2434 else if(val < 1)
2435 global->parallel_host = PARALLEL_HOST_DEFAULT;
2436 else
2437 global->parallel_host = (unsigned short)val;
2438 break;
2439 case C_PARALLEL_MAX: /* --parallel-max */
2440 if(val > MAX_PARALLEL)
2441 global->parallel_max = MAX_PARALLEL;
2442 else if(val < 1)
2443 global->parallel_max = PARALLEL_DEFAULT;
2444 else
2445 global->parallel_max = (unsigned short)val;
2446 break;
2447 default:
2448 DEBUGASSERT(0);
2449 return PARAM_OPTION_UNKNOWN;
2450 }
2451 return err;
2452}
2454/* handle options that accept seconds */
2455static ParameterError opt_secs(struct OperationConfig *config,
2456 const struct LongShort *a,
2457 const char *nextarg)
2458{
2459 long val;
2460 ParameterError err = secs2ms(&val, nextarg);
2461 if(err)
2462 return err;
2463 switch(a->cmd) {
2464 case C_CONNECT_TIMEOUT: /* --connect-timeout */
2465 config->connecttimeout_ms = val;
2466 break;
2467 case C_RETRY_DELAY: /* --retry-delay */
2468 if(val >= INT32_MAX)
2469 val = INT32_MAX;
2470 config->retry_delay_ms = (uint32_t)val;
2471 break;
2472 case C_RETRY_MAX_TIME: /* --retry-max-time */
2473 config->retry_maxtime_ms = val;
2474 break;
2475 case C_EXPECT100_TIMEOUT: /* --expect100-timeout */
2476 config->expect100timeout_ms = val;
2477 break;
2478 case C_MAX_TIME: /* --max-time */
2479 /* specified max time */
2480 config->timeout_ms = val;
2481 break;
2482 default:
2483 DEBUGASSERT(0);
2484 return PARAM_OPTION_UNKNOWN;
2485 }
2486 return err;
2487}
2489/* opt_string handles string options */
2490static ParameterError opt_string(struct OperationConfig *config,
2491 const struct LongShort *a,
2492 const char *nextarg)
2493{
2494 ParameterError err = PARAM_OK;
2495 curl_off_t value;
2496 static const char * const redir_protos[] = {
2497 "http",
2498 "https",
2499 "ftp",
2500 "ftps",
2501 NULL
2502 };
2503 if(!nextarg)
2504 nextarg = "";
2506 switch(a->cmd) {
2507 case C_DNS_IPV4_ADDR: /* --dns-ipv4-addr */
2508 if(!curlinfo->ares_num) /* c-ares is needed for this */
2509 return PARAM_LIBCURL_DOESNT_SUPPORT;
2510 /* addr in dot notation */
2511 return getstr(&config->dns_ipv4_addr, nextarg, DENY_BLANK);
2513 case C_DNS_IPV6_ADDR: /* --dns-ipv6-addr */
2514 if(!curlinfo->ares_num) /* c-ares is needed for this */
2515 return PARAM_LIBCURL_DOESNT_SUPPORT;
2516 /* addr in dot notation */
2517 return getstr(&config->dns_ipv6_addr, nextarg, DENY_BLANK);
2519 case C_OAUTH2_BEARER: /* --oauth2-bearer */
2520 config->authtype |= CURLAUTH_BEARER;
2521 return getstr(&config->oauth_bearer, nextarg, DENY_BLANK);
2523 case C_DOH_URL: /* --doh-url */
2524 err = getstr(&config->doh_url, nextarg, ALLOW_BLANK);
2525 if(!err && config->doh_url && !config->doh_url[0])
2526 /* if given a blank string, make it NULL again */
2527 curlx_safefree(config->doh_url);
2528 break;
2530 case C_CIPHERS: /* -- ciphers */
2531 err = getstr(&config->cipher_list, nextarg, DENY_BLANK);
2532 break;
2534 case C_DNS_INTERFACE: /* --dns-interface */
2535 if(!curlinfo->ares_num) /* c-ares is needed for this */
2536 err = PARAM_LIBCURL_DOESNT_SUPPORT;
2537 else
2538 /* interface name */
2539 err = getstr(&config->dns_interface, nextarg, DENY_BLANK);
2540 break;
2541 case C_DNS_SERVERS: /* --dns-servers */
2542 if(!curlinfo->ares_num) /* c-ares is needed for this */
2543 err = PARAM_LIBCURL_DOESNT_SUPPORT;
2544 else
2545 /* IP addrs of DNS servers */
2546 err = getstr(&config->dns_servers, nextarg, DENY_BLANK);
2547 break;
2548 case C_LIMIT_RATE: /* --limit-rate */
2549 err = GetSizeParameter(nextarg, &value);
2550 if(!err) {
2551 config->recvpersecond = value;
2552 config->sendpersecond = value;
2553 }
2554 break;
2555 case C_RATE:
2556 err = set_rate(nextarg);
2557 break;
2558 case C_CREATE_FILE_MODE: /* --create-file-mode */
2559 err = oct2nummax(&config->create_file_mode, nextarg, 0777);
2560 break;
2561 case C_MAX_REDIRS: /* --max-redirs */
2562 /* specified max no of redirects (http(s)), this accepts -1 as a
2563 special condition */
2564 err = str2num(&config->maxredirs, nextarg);
2565 if(!err && (config->maxredirs < -1))
2566 err = PARAM_BAD_NUMERIC;
2567 break;
2568#ifndef CURL_DISABLE_IPFS
2569 case C_IPFS_GATEWAY: /* --ipfs-gateway */
2570 err = getstr(&config->ipfs_gateway, nextarg, DENY_BLANK);
2571 break;
2572#endif
2573 case C_AWS_SIGV4: /* --aws-sigv4 */
2574 config->authtype |= CURLAUTH_AWS_SIGV4;
2575 err = getstr(&config->aws_sigv4, nextarg, ALLOW_BLANK);
2576 break;
2577 case C_HTTPSIG_ALGORITHM: /* --httpsig-algo */
2578 if(!feature_httpsig)
2579 err = PARAM_LIBCURL_DOESNT_SUPPORT;
2580 else {
2581 config->authtype |= CURLAUTH_HTTPSIG;
2582 err = getstr(&config->httpsig_algorithm, nextarg, DENY_BLANK);
2583 }
2584 break;
2585 case C_HTTPSIG_KEYID: /* --httpsig-keyid */
2586 if(!feature_httpsig)
2587 err = PARAM_LIBCURL_DOESNT_SUPPORT;
2588 else
2589 err = getstr(&config->httpsig_keyid, nextarg, DENY_BLANK);
2590 break;
2591 case C_HTTPSIG_HEADERS: /* --httpsig-headers */
2592 if(!feature_httpsig)
2593 err = PARAM_LIBCURL_DOESNT_SUPPORT;
2594 else
2595 err = getstr(&config->httpsig_headers, nextarg, DENY_BLANK);
2596 break;
2597 case C_INTERFACE: /* --interface */
2598 /* interface */
2599 err = getstr(&config->iface, nextarg, DENY_BLANK);
2600 break;
2601 case C_HAPROXY_CLIENTIP: /* --haproxy-clientip */
2602 err = getstr(&config->haproxy_clientip, nextarg, DENY_BLANK);
2603 break;
2604 case C_MAX_FILESIZE: /* --max-filesize */
2605 err = GetSizeParameter(nextarg, &value);
2606 if(!err)
2607 config->max_filesize = value;
2608 break;
2609 case C_URL: /* --url */
2610 err = parse_url(config, nextarg);
2611 break;
2612 case C_SOCKS5: /* --socks5 */
2613 /* socks5 proxy to use, and resolves the name locally and passes on the
2614 resolved address */
2615 err = getstr(&config->proxy, nextarg, DENY_BLANK);
2616 config->proxyver = CURLPROXY_SOCKS5;
2617 break;
2618 case C_SOCKS4: /* --socks4 */
2619 err = getstr(&config->proxy, nextarg, DENY_BLANK);
2620 config->proxyver = CURLPROXY_SOCKS4;
2621 break;
2622 case C_SOCKS4A: /* --socks4a */
2623 err = getstr(&config->proxy, nextarg, DENY_BLANK);
2624 config->proxyver = CURLPROXY_SOCKS4A;
2625 break;
2626 case C_SOCKS5_HOSTNAME: /* --socks5-hostname */
2627 err = getstr(&config->proxy, nextarg, DENY_BLANK);
2628 config->proxyver = CURLPROXY_SOCKS5_HOSTNAME;
2629 break;
2630 case C_IP_TOS: { /* --ip-tos */
2631 struct TOSEntry find;
2632 const struct TOSEntry *entry;
2633 find.name = nextarg;
2634 entry = bsearch(&find, tos_entries,
2635 CURL_ARRAYSIZE(tos_entries),
2636 sizeof(*tos_entries), find_tos);
2637 if(entry)
2638 config->ip_tos = entry->value;
2639 else /* numeric tos value */
2640 err = str2unummax(&config->ip_tos, nextarg, 0xFF);
2641 break;
2642 }
2643 case C_FTP_ACCOUNT: /* --ftp-account */
2644 err = getstr(&config->ftp_account, nextarg, DENY_BLANK);
2645 break;
2646 case C_FTP_METHOD: /* --ftp-method */
2647 config->ftp_filemethod = ftpfilemethod(nextarg);
2648 break;
2649 case C_LOCAL_PORT: /* --local-port */
2650 err = parse_localport(config, nextarg);
2651 break;
2652 case C_FTP_ALTERNATIVE_TO_USER: /* --ftp-alternative-to-user */
2653 err = getstr(&config->ftp_alternative_to_user, nextarg, DENY_BLANK);
2654 break;
2655 case C_LIBCURL: /* --libcurl */
2656#ifdef CURL_DISABLE_LIBCURL_OPTION
2657 warnf("--libcurl option was disabled at build-time");
2658 err = PARAM_OPTION_UNKNOWN;
2659#else
2660 err = getstr(&global->libcurl, nextarg, DENY_BLANK);
2661#endif
2662 break;
2663 case C_NOPROXY: /* --noproxy */
2664 /* This specifies the noproxy list */
2665 err = getstr(&config->noproxy, nextarg, ALLOW_BLANK);
2666 break;
2667 case C_PROXY1_0: /* --proxy1.0 */
2668 /* http 1.0 proxy */
2669 err = getstr(&config->proxy, nextarg, DENY_BLANK);
2670 config->proxyver = CURLPROXY_HTTP_1_0;
2671 break;
2672 case C_MAIL_FROM: /* --mail-from */
2673 err = getstr(&config->mail_from, nextarg, DENY_BLANK);
2674 break;
2675 case C_MAIL_RCPT: /* --mail-rcpt */
2676 /* append receiver to a list */
2677 err = add2list(&config->mail_rcpt, nextarg);
2678 break;
2679 case C_PROTO: /* --proto */
2680 config->proto_present = TRUE;
2681 err = proto2num(built_in_protos, &config->proto_str, nextarg);
2682 break;
2683 case C_PROTO_REDIR: /* --proto-redir */
2684 config->proto_redir_present = TRUE;
2685 if(proto2num(redir_protos, &config->proto_redir_str, nextarg))
2686 err = PARAM_BAD_USE;
2687 break;
2688 case C_RESOLVE: /* --resolve */
2689 err = add2list(&config->resolve, nextarg);
2690 break;
2691 case C_DELEGATION: /* --delegation */
2692 config->gssapi_delegation = delegation(nextarg);
2693 break;
2694 case C_MAIL_AUTH: /* --mail-auth */
2695 err = getstr(&config->mail_auth, nextarg, DENY_BLANK);
2696 break;
2697 case C_SASL_AUTHZID: /* --sasl-authzid */
2698 err = getstr(&config->sasl_authzid, nextarg, DENY_BLANK);
2699 break;
2700 case C_PROXY_SERVICE_NAME: /* --proxy-service-name */
2701 case C_SOCKS5_GSSAPI_SERVICE: /* --socks5-gssapi-service */
2702 err = getstr(&config->proxy_service_name, nextarg, DENY_BLANK);
2703 break;
2704 case C_SERVICE_NAME: /* --service-name */
2705 err = getstr(&config->service_name, nextarg, DENY_BLANK);
2706 break;
2707 case C_PROTO_DEFAULT: /* --proto-default */
2708 err = getstr(&config->proto_default, nextarg, DENY_BLANK);
2709 if(!err)
2710 err = check_protocol(config->proto_default);
2711 break;
2712 case C_CONNECT_TO: /* --connect-to */
2713 err = add2list(&config->connect_to, nextarg);
2714 break;
2715 case C_TLS_MAX: /* --tls-max */
2716 err = str2tls_max(&config->ssl_version_max, nextarg);
2717 if(!err && (config->ssl_version_max < config->ssl_version)) {
2718 errorf("--tls-max set lower than minimum accepted version");
2719 err = PARAM_BAD_USE;
2720 }
2721 break;
2722 case C_TRACE_CONFIG: /* --trace-config */
2723 global->trace_set = TRUE;
2724 if(set_trace_config(nextarg))
2725 err = PARAM_NO_MEM;
2726 break;
2727 case C_VARIABLE: /* --variable */
2728 err = setvariable(nextarg);
2729 break;
2730 case C_TLS13_CIPHERS: /* --tls13-ciphers */
2731 err = getstr(&config->cipher13_list, nextarg, DENY_BLANK);
2732 break;
2733 case C_PROXY_TLS13_CIPHERS: /* --proxy-tls13-ciphers */
2734 err = getstr(&config->proxy_cipher13_list, nextarg, DENY_BLANK);
2735 break;
2736 case C_USER_AGENT: /* --user-agent */
2737 err = getstr(&config->useragent, nextarg, ALLOW_BLANK);
2738 break;
2739 case C_ALT_SVC: /* --alt-svc */
2740 if(!feature_altsvc)
2741 err = PARAM_LIBCURL_DOESNT_SUPPORT;
2742 else
2743 err = getstr(&config->altsvc, nextarg, ALLOW_BLANK);
2744 break;
2745 case C_HSTS: /* --hsts */
2746 if(!feature_hsts)
2747 err = PARAM_LIBCURL_DOESNT_SUPPORT;
2748 else
2749 err = getstr(&config->hsts, nextarg, ALLOW_BLANK);
2750 break;
2751 case C_COOKIE: /* --cookie */
2752 if(strchr(nextarg, '=')) {
2753 /* A cookie string must have a =-letter */
2754 err = add2list(&config->cookies, nextarg);
2755 break;
2756 }
2757 else {
2758 /* We have a cookie file to read from! */
2759 err = add2list(&config->cookiefiles, nextarg);
2760 }
2761 break;
2762 case C_COOKIE_JAR: /* --cookie-jar */
2763 err = getstr(&config->cookiejar, nextarg, DENY_BLANK);
2764 break;
2765 case C_CONTINUE_AT: /* --continue-at */
2766 err = parse_continue_at(config, nextarg);
2767 break;
2768 case C_DATA: /* --data */
2769 case C_DATA_ASCII: /* --data-ascii */
2770 case C_DATA_BINARY: /* --data-binary */
2771 case C_DATA_URLENCODE: /* --data-urlencode */
2772 case C_JSON: /* --json */
2773 case C_DATA_RAW: /* --data-raw */
2774 err = set_data((cmdline_t)a->cmd, nextarg, config);
2775 break;
2776 case C_URL_QUERY: /* --url-query */
2777 err = url_query(nextarg, config);
2778 break;
2779 case C_REFERER: { /* --referer */
2780 size_t len = strlen(nextarg);
2781 /* does it end with ;auto ? */
2782 if(len >= 5 && !strcmp(";auto", &nextarg[len - 5])) {
2783 /* Automatic referer requested, this may be combined with a set initial
2784 one */
2785 config->autoreferer = TRUE;
2786 len -= 5;
2787 }
2788 else
2789 config->autoreferer = FALSE;
2791 if(len)
2792 err = getstrn(&config->referer, nextarg, len, ALLOW_BLANK);
2793 else
2794 curlx_safefree(config->referer);
2795 }
2796 break;
2797 case C_CERT_TYPE: /* --cert-type */
2798 err = getstr(&config->cert_type, nextarg, DENY_BLANK);
2799 break;
2800 case C_KEY_TYPE: /* --key-type */
2801 err = getstr(&config->key_type, nextarg, DENY_BLANK);
2802 break;
2803 case C_PASS: /* --pass */
2804 err = getstr(&config->key_passwd, nextarg, DENY_BLANK);
2805 break;
2806 case C_ENGINE: /* --engine */
2807 err = getstr(&config->engine, nextarg, DENY_BLANK);
2808 if(!err &&
2809 config->engine && !strcmp(config->engine, "list")) {
2810 err = PARAM_ENGINES_REQUESTED;
2811 }
2812 break;
2813 case C_ECH: /* --ech */
2814 err = parse_ech(config, nextarg);
2815 break;
2816 case C_PUBKEY: /* --pubkey */
2817 err = getstr(&config->pubkey, nextarg, DENY_BLANK);
2818 break;
2819 case C_HOSTPUBMD5: /* --hostpubmd5 */
2820 err = getstr(&config->hostpubmd5, nextarg, DENY_BLANK);
2821 if(!err && (!config->hostpubmd5 || strlen(config->hostpubmd5) != 32))
2822 err = PARAM_BAD_USE;
2823 break;
2824 case C_HOSTPUBSHA256: /* --hostpubsha256 */
2825 err = getstr(&config->hostpubsha256, nextarg, DENY_BLANK);
2826 break;
2827 case C_PINNEDPUBKEY: /* --pinnedpubkey */
2828 err = getstr(&config->pinnedpubkey, nextarg, DENY_BLANK);
2829 break;
2830 case C_PROXY_PINNEDPUBKEY: /* --proxy-pinnedpubkey */
2831 err = getstr(&config->proxy_pinnedpubkey, nextarg, DENY_BLANK);
2832 break;
2833 case C_PROXY_CERT_TYPE: /* --proxy-cert-type */
2834 err = getstr(&config->proxy_cert_type, nextarg, DENY_BLANK);
2835 break;
2836 case C_PROXY_KEY_TYPE: /* --proxy-key-type */
2837 err = getstr(&config->proxy_key_type, nextarg, DENY_BLANK);
2838 break;
2839 case C_PROXY_PASS: /* --proxy-pass */
2840 err = getstr(&config->proxy_key_passwd, nextarg, ALLOW_BLANK);
2841 break;
2842 case C_PROXY_CIPHERS: /* --proxy-ciphers */
2843 err = getstr(&config->proxy_cipher_list, nextarg, DENY_BLANK);
2844 break;
2845 case C_LOGIN_OPTIONS: /* --login-options */
2846 err = getstr(&config->login_options, nextarg, ALLOW_BLANK);
2847 break;
2848 case C_CURVES: /* --curves */
2849 err = getstr(&config->ssl_ec_curves, nextarg, DENY_BLANK);
2850 break;
2851 case C_SIGNATURE_ALGORITHMS: /* --sigalgs */
2852 err = getstr(&config->ssl_signature_algorithms, nextarg, DENY_BLANK);
2853 break;
2854 case C_FORM: /* --form */
2855 case C_FORM_STRING: /* --form-string */
2856 /* "form data" simulation, this is a little advanced so lets do our best
2857 to sort this out slowly and carefully */
2858 if(formparse(nextarg, &config->mimeroot, &config->mimecurrent,
2859 (a->cmd == C_FORM_STRING))) /* literal string */
2860 err = PARAM_BAD_USE;
2861 else if(SetHTTPrequest(TOOL_HTTPREQ_MIMEPOST, &config->httpreq))
2862 err = PARAM_BAD_USE;
2863 break;
2864 case C_REQUEST_TARGET: /* --request-target */
2865 err = getstr(&config->request_target, nextarg, DENY_BLANK);
2866 break;
2867 case C_HEADER: /* --header */
2868 case C_PROXY_HEADER: /* --proxy-header */
2869 err = parse_header(config, (cmdline_t)a->cmd, nextarg);
2870 break;
2871 case C_OUTPUT_DIR: /* --output-dir */
2872 err = getstr(&config->output_dir, nextarg, DENY_BLANK);
2873 break;
2874 case C_FTP_PORT: /* --ftp-port */
2875 /* This makes the FTP sessions use PORT instead of PASV */
2876 /* use <eth0> or <192.168.10.10> style addresses. Anything except
2877 this makes us try to get the "default" address.
2878 NOTE: this is a changed behavior since the released 4.1!
2879 */
2880 err = getstr(&config->ftpport, nextarg, DENY_BLANK);
2881 break;
2882 case C_FTP_SSL_CCC_MODE: /* --ftp-ssl-ccc-mode */
2883 config->ftp_ssl_ccc = TRUE;
2884 config->ftp_ssl_ccc_mode = ftpcccmethod(nextarg);
2885 break;
2886 case C_QUOTE: /* --quote */
2887 err = parse_quote(config, nextarg);
2888 break;
2889 case C_RANGE: /* --range */
2890 err = parse_range(config, nextarg);
2891 break;
2892 case C_TELNET_OPTION: /* --telnet-option */
2893 /* Telnet options */
2894 err = add2list(&config->telnet_options, nextarg);
2895 break;
2896 case C_USER: /* --user */
2897 /* user:password */
2898 err = getstr(&config->userpwd, nextarg, ALLOW_BLANK);
2899 break;
2900 case C_PROXY_USER: /* --proxy-user */
2901 /* Proxy user:password */
2902 err = getstr(&config->proxyuserpwd, nextarg, ALLOW_BLANK);
2903 break;
2904 case C_WRITE_OUT: /* --write-out */
2905 err = parse_writeout(config, nextarg);
2906 break;
2907 case C_PREPROXY: /* --preproxy */
2908 err = getstr(&config->preproxy, nextarg, DENY_BLANK);
2909 break;
2910 case C_PROXY: /* --proxy */
2911 /* --proxy */
2912 err = getstr(&config->proxy, nextarg, ALLOW_BLANK);
2913 if(config->proxyver != CURLPROXY_HTTPS2 &&
2914 config->proxyver != CURLPROXY_HTTPS3)
2915 config->proxyver = CURLPROXY_HTTP;
2916 break;
2917 case C_REQUEST: /* --request */
2918 /* set custom request */
2919 err = getstr(&config->customrequest, nextarg, DENY_BLANK);
2920 break;
2921 case C_TIME_COND: /* --time-cond */
2922 err = parse_time_cond(config, nextarg);
2923 break;
2924 case C_UPLOAD_FLAGS: /* --upload-flags */
2925 err = parse_upload_flags(config, nextarg);
2926 break;
2927 }
2928 return err;
2929}
2931/* detect e2 80 80 - e2 80 ff */
2932static bool has_leading_unicode(const unsigned char *arg)
2933{
2934 return (arg[0] == 0xe2) && (arg[1] == 0x80) && (arg[2] & 0x80);
2935}
2937/* the longest command line option, excluding the leading -- */
2938#define MAX_OPTION_LEN 26
2940ParameterError getparameter(const char *flag, /* f or -long-flag */
2941 const char *nextarg, /* NULL if unset */
2942 bool *usedarg, /* set to TRUE if the arg
2943 has been used */
2944 struct OperationConfig *config,
2945 int max_recursive)
2946{
2947 const char *parse = NULL;
2948 bool longopt = FALSE;
2949 bool singleopt = FALSE; /* when true means '-o foo' used '-ofoo' */
2950 ParameterError err = PARAM_OK;
2951 bool toggle = TRUE; /* how to switch boolean options, on or off. Controlled
2952 by using --OPTION or --no-OPTION */
2953 bool nextalloc = FALSE; /* if nextarg is allocated */
2954 bool consumearg = TRUE; /* the argument comes separate */
2955 const struct LongShort *a = NULL;
2956 verbose_nopts = 0; /* options processed in `flag`*/
2958 *usedarg = FALSE; /* default is that we do not use the arg */
2960 if(('-' != flag[0]) || ('-' == flag[1])) {
2961 /* this should be a long name */
2962 const char *word = ('-' == flag[0]) ? flag + 2 : flag;
2963 bool noflagged = FALSE;
2964 bool expand = FALSE;
2965 const char *p;
2966 struct Curl_str out;
2968 if(!strncmp(word, "no-", 3)) {
2969 /* disable this option but ignore the "no-" part when looking for it */
2970 word += 3;
2971 toggle = FALSE;
2972 noflagged = TRUE;
2973 }
2974 else if(!strncmp(word, "expand-", 7)) {
2975 /* variable expansions is to be done on the argument */
2976 word += 7;
2977 expand = TRUE;
2978 }
2980 p = word;
2981 /* is there an '=' ? */
2982 if(!curlx_str_until(&p, &out, MAX_OPTION_LEN, '=') &&
2983 !curlx_str_single(&p, '=')) {
2984 /* there is an equal sign */
2985 char tempword[MAX_OPTION_LEN + 1];
2986 memcpy(tempword, curlx_str(&out), curlx_strlen(&out));
2987 tempword[curlx_strlen(&out)] = 0;
2988 a = findlongopt(tempword);
2989 nextarg = p;
2990 consumearg = FALSE; /* it is not separate */
2991 }
2992 else
2993 a = findlongopt(word);
2995 if(a) {
2996 longopt = TRUE;
2997 }
2998 else {
2999 err = PARAM_OPTION_UNKNOWN;
3000 goto error;
3001 }
3002 if(noflagged && (ARGTYPE(a->desc) != ARG_BOOL)) {
3003 /* --no- prefixed an option that is not boolean! */
3004 err = PARAM_NO_PREFIX;
3005 goto error;
3006 }
3007 else if(expand && nextarg) {
3008 struct dynbuf nbuf;
3009 bool replaced;
3011 if(ARGTYPE(a->desc) < ARG_STRG) {
3012 /* --expand on an option that does not take an argument */
3013 err = PARAM_EXPAND_ERROR;
3014 goto error;
3015 }
3016 err = varexpand(nextarg, &nbuf, &replaced);
3017 if(err) {
3018 curlx_dyn_free(&nbuf);
3019 goto error;
3020 }
3021 if(replaced) {
3022 nextarg = curlx_dyn_ptr(&nbuf);
3023 nextalloc = TRUE;
3024 }
3025 }
3026 }
3027 else {
3028 flag++; /* prefixed with one dash, pass it */
3029 parse = flag;
3030 }
3032 do {
3033 /* we can loop here if we have multiple single-letters */
3034 if(!longopt) {
3035 a = findshortopt(*parse);
3036 if(!a) {
3037 err = PARAM_OPTION_UNKNOWN;
3038 break;
3039 }
3040 toggle = !(a->desc & ARG_NO);
3041 }
3042 if((a->desc & ARG_TLS) && !feature_ssl) {
3043 err = PARAM_LIBCURL_DOESNT_SUPPORT;
3044 break;
3045 }
3046 else if(ARGTYPE(a->desc) >= ARG_STRG) {
3047 /* this option requires an extra parameter */
3048 if(!longopt && parse[1]) {
3049 nextarg = &parse[1]; /* this is the actual extra parameter */
3050 singleopt = TRUE; /* do not loop anymore after this */
3051 }
3052 else if(a->cmd == C_HELP) {
3053 /* --help is special */
3054 tool_help((nextarg && *nextarg) ? nextarg : NULL);
3055 err = PARAM_HELP_REQUESTED;
3056 break;
3057 }
3058 else if(!nextarg) {
3059 err = PARAM_REQUIRES_PARAMETER;
3060 break;
3061 }
3062 else {
3063 *usedarg = consumearg; /* mark it as used */
3064 }
3065 if(a->desc & ARG_DEPR) {
3066 if(a->desc & ARG_CLEAR)
3067 cleanarg(CURL_UNCONST(nextarg));
3068 opt_depr(a);
3069 break;
3070 }
3072 if(has_leading_unicode((const unsigned char *)nextarg)) {
3073 if(a->desc & ARG_CLEAR)
3074 warnf("The argument to --%s starts with a Unicode character. "
3075 "Maybe ASCII was intended?", a->lname);
3076 else
3077 warnf("The argument '%s' starts with a Unicode character. "
3078 "Maybe ASCII was intended?", nextarg);
3079 }
3080 switch(ARGTYPE(a->desc)) {
3081 case ARG_FILE:
3082 err = opt_file(config, a, nextarg, max_recursive);
3083 break;
3084 case ARG_STRG:
3085 err = opt_string(config, a, nextarg);
3086 break;
3087 case ARG_SECS:
3088 err = opt_secs(config, a, nextarg);
3089 break;
3090 case ARG_UNUM:
3091 err = opt_unum(config, a, nextarg);
3092 break;
3093 default:
3094 DEBUGASSERT(0);
3095 err = PARAM_OPTION_UNKNOWN;
3096 break;
3097 }
3098 if(a->desc & ARG_CLEAR)
3099 cleanarg(CURL_UNCONST(nextarg));
3100 }
3101 else {
3102 if(a->desc & ARG_DEPR) {
3103 opt_depr(a);
3104 break;
3105 }
3106 /* ARG_NONE | ARG_BOOL */
3107 if(ARGTYPE(a->desc) == ARG_BOOL)
3108 err = opt_bool(config, a, toggle);
3109 else
3110 err = opt_none(config, a);
3111 }
3113 ++verbose_nopts; /* processed one option from `flag` input, loop for
3114 more */
3115 } while(!longopt && !singleopt && *++parse && !*usedarg && !err);
3117error:
3118 if(nextalloc)
3119 curlx_free(CURL_UNCONST(nextarg));
3120 return err;
3121}
3123ParameterError parse_args(int argc, argv_item_t argv[])
3124{
3125 int i;
3126 bool stillflags;
3127 const char *orig_opt = NULL;
3128 ParameterError err = PARAM_OK;
3129 struct OperationConfig *config = global->first;
3131 stillflags = TRUE;
3132 for(i = 1; i < argc && !err; i++) {
3133 orig_opt = convert_tchar_to_UTF8(argv[i]);
3134 if(!orig_opt)
3135 return PARAM_NO_MEM;
3137 if(stillflags && ('-' == orig_opt[0])) {
3138 bool passarg;
3140 if(!strcmp("--", orig_opt))
3141 /* This indicates the end of the flags and thus enables the
3142 following (URL) argument to start with -. */
3143 stillflags = FALSE;
3144 else {
3145 const char *nextarg = NULL;
3146 if(i < (argc - 1)) {
3147 nextarg = convert_tchar_to_UTF8(argv[i + 1]);
3148 if(!nextarg) {
3149 unicodefree(CURL_UNCONST(orig_opt));
3150 return PARAM_NO_MEM;
3151 }
3152 }
3154 err = getparameter(orig_opt, nextarg, &passarg, config,
3155 CONFIG_MAX_LEVELS);
3157 unicodefree(CURL_UNCONST(nextarg));
3158 config = global->last;
3159 if(err == PARAM_NEXT_OPERATION) {
3160 /* Reset err as PARAM_NEXT_OPERATION is only used here and not
3161 returned from this function */
3162 err = PARAM_OK;
3164 if(config->url_list && config->url_list->url) {
3165 /* Allocate the next config */
3166 config->next = config_alloc();
3167 if(config->next) {
3168 /* Update the last config pointer */
3169 global->last = config->next;
3171 /* Move onto the new config */
3172 config->next->prev = config;
3173 config = config->next;
3174 }
3175 else
3176 err = PARAM_NO_MEM;
3177 }
3178 else {
3179 errorf("missing URL before --next");
3180 err = PARAM_BAD_USE;
3181 }
3182 }
3183 else if(!err && passarg)
3184 i++; /* we are supposed to skip this */
3185 }
3186 }
3187 else {
3188 bool used;
3190 /* add the URL please */
3191 err = getparameter("--url", orig_opt, &used, config, 0);
3192 }
3194 if(!err) {
3195 unicodefree(CURL_UNCONST(orig_opt));
3196 orig_opt = NULL;
3197 }
3198 }
3200 if(!err && config->content_disposition && config->resume_from_current)
3201 err = PARAM_CONTDISP_RESUME_FROM;
3203 if(err &&
3204 err != PARAM_HELP_REQUESTED &&
3205 err != PARAM_MANUAL_REQUESTED &&
3206 err != PARAM_VERSION_INFO_REQUESTED &&
3207 err != PARAM_ENGINES_REQUESTED &&
3208 err != PARAM_CA_EMBED_REQUESTED) {
3209 const char *reason = param2text(err);
3211 if(orig_opt && strcmp(":", orig_opt))
3212 helpf("option %s: %s", orig_opt, reason);
3213 else
3214 helpf("%s", reason);
3215 }
3217 unicodefree(CURL_UNCONST(orig_opt));
3218 return err;
3219}
Diagnostics4 git calls · 307 ms
CallStatusSizeTime0307 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=297 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=6 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch20028 KiB298 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=29124 git.response.cache=MISS git.response.duration_ms=298 git.fetch.wants=9cbc21d0355d51d0bcfc21ce60c8ba698460a390 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=29124 git.pkt.payload.max=29103 git.sideband.band1.bytes=29102 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e25a21ba43a23e
- Request
- 74590e55…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format