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"
25
26 #include "curlx.h"
27
28 #include "tool_help.h"
29 #include "tool_libinfo.h"
30 #include "tool_util.h"
31 #include "tool_version.h"
32 #include "tool_cb_prg.h"
33 #include "tool_hugehelp.h"
34 #include "tool_getparam.h"
35 #include "terminal.h"
36
37 #include "memdebug.h" /* keep this as LAST include */
38
39 #ifndef ARRAYSIZE
40 #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
41 #endif
42
43 struct category_descriptors {
44 const char *opt;
45 const char *desc;
46 unsigned int category;
47 };
48
49 static const struct category_descriptors categories[] = {
50 /* important is left out because it is the default help page */
51 {"auth", "Authentication methods", CURLHELP_AUTH},
52 {"connection", "Manage connections", CURLHELP_CONNECTION},
53 {"curl", "The command line tool itself", CURLHELP_CURL},
54 {"deprecated", "Legacy", CURLHELP_DEPRECATED},
55 {"dns", "Names and resolving", CURLHELP_DNS},
56 {"file", "FILE protocol", CURLHELP_FILE},
57 {"ftp", "FTP protocol", CURLHELP_FTP},
58 {"global", "Global options", CURLHELP_GLOBAL},
59 {"http", "HTTP and HTTPS protocol", CURLHELP_HTTP},
60 {"imap", "IMAP protocol", CURLHELP_IMAP},
61 {"ldap", "LDAP protocol", CURLHELP_LDAP},
62 {"output", "Filesystem output", CURLHELP_OUTPUT},
63 {"pop3", "POP3 protocol", CURLHELP_POP3},
64 {"post", "HTTP POST specific", CURLHELP_POST},
65 {"proxy", "Options for proxies", CURLHELP_PROXY},
66 {"scp", "SCP protocol", CURLHELP_SCP},
67 {"sftp", "SFTP protocol", CURLHELP_SFTP},
68 {"smtp", "SMTP protocol", CURLHELP_SMTP},
69 {"ssh", "SSH protocol", CURLHELP_SSH},
70 {"telnet", "TELNET protocol", CURLHELP_TELNET},
71 {"tftp", "TFTP protocol", CURLHELP_TFTP},
72 {"timeout", "Timeouts and delays", CURLHELP_TIMEOUT},
73 {"tls", "TLS/SSL related", CURLHELP_TLS},
74 {"upload", "Upload, sending data", CURLHELP_UPLOAD},
75 {"verbose", "Tracing, logging etc", CURLHELP_VERBOSE}
76 };
77
print_category(unsigned int category,unsigned int cols)78 static void print_category(unsigned int category, unsigned int cols)
79 {
80 unsigned int i;
81 size_t longopt = 5;
82 size_t longdesc = 5;
83
84 for(i = 0; helptext[i].opt; ++i) {
85 size_t len;
86 if(!(helptext[i].categories & category))
87 continue;
88 len = strlen(helptext[i].opt);
89 if(len > longopt)
90 longopt = len;
91 len = strlen(helptext[i].desc);
92 if(len > longdesc)
93 longdesc = len;
94 }
95 if(longopt + longdesc > cols)
96 longopt = cols - longdesc;
97
98 for(i = 0; helptext[i].opt; ++i)
99 if(helptext[i].categories & category) {
100 size_t opt = longopt;
101 size_t desclen = strlen(helptext[i].desc);
102 if(opt + desclen >= (cols - 2)) {
103 if(desclen < (cols - 2))
104 opt = (cols - 3) - desclen;
105 else
106 opt = 0;
107 }
108 printf(" %-*s %s\n", (int)opt, helptext[i].opt, helptext[i].desc);
109 }
110 }
111
112 /* Prints category if found. If not, it returns 1 */
get_category_content(const char * category,unsigned int cols)113 static int get_category_content(const char *category, unsigned int cols)
114 {
115 unsigned int i;
116 for(i = 0; i < ARRAYSIZE(categories); ++i)
117 if(curl_strequal(categories[i].opt, category)) {
118 printf("%s: %s\n", categories[i].opt, categories[i].desc);
119 print_category(categories[i].category, cols);
120 return 0;
121 }
122 return 1;
123 }
124
125 /* Prints all categories and their description */
get_categories(void)126 static void get_categories(void)
127 {
128 unsigned int i;
129 for(i = 0; i < ARRAYSIZE(categories); ++i)
130 printf(" %-11s %s\n", categories[i].opt, categories[i].desc);
131 }
132
133 /* Prints all categories as a comma-separated list of given width */
get_categories_list(unsigned int width)134 static void get_categories_list(unsigned int width)
135 {
136 unsigned int i;
137 size_t col = 0;
138 for(i = 0; i < ARRAYSIZE(categories); ++i) {
139 size_t len = strlen(categories[i].opt);
140 if(i == ARRAYSIZE(categories) - 1) {
141 /* final category */
142 if(col + len + 1 < width)
143 printf("%s.\n", categories[i].opt);
144 else
145 /* start a new line first */
146 printf("\n%s.\n", categories[i].opt);
147 }
148 else if(col + len + 2 < width) {
149 printf("%s, ", categories[i].opt);
150 col += len + 2;
151 }
152 else {
153 /* start a new line first */
154 printf("\n%s, ", categories[i].opt);
155 col = len + 2;
156 }
157 }
158 }
159
160 #ifdef USE_MANUAL
161
inithelpscan(struct scan_ctx * ctx,const char * trigger,const char * arg,const char * endarg)162 void inithelpscan(struct scan_ctx *ctx,
163 const char *trigger,
164 const char *arg,
165 const char *endarg)
166 {
167 ctx->trigger = trigger;
168 ctx->tlen = strlen(trigger);
169 ctx->arg = arg;
170 ctx->flen = strlen(arg);
171 ctx->endarg = endarg;
172 ctx->elen = strlen(endarg);
173 DEBUGASSERT((ctx->elen < sizeof(ctx->rbuf)) ||
174 (ctx->flen < sizeof(ctx->rbuf)));
175 ctx->show = 0;
176 ctx->olen = 0;
177 memset(ctx->rbuf, 0, sizeof(ctx->rbuf));
178 }
179
helpscan(unsigned char * buf,size_t len,struct scan_ctx * ctx)180 bool helpscan(unsigned char *buf, size_t len, struct scan_ctx *ctx)
181 {
182 size_t i;
183 for(i = 0; i < len; i++) {
184 if(!ctx->show) {
185 /* wait for the trigger */
186 memmove(&ctx->rbuf[0], &ctx->rbuf[1], ctx->tlen - 1);
187 ctx->rbuf[ctx->tlen - 1] = buf[i];
188 if(!memcmp(ctx->rbuf, ctx->trigger, ctx->tlen))
189 ctx->show++;
190 continue;
191 }
192 /* past the trigger */
193 if(ctx->show == 1) {
194 memmove(&ctx->rbuf[0], &ctx->rbuf[1], ctx->flen - 1);
195 ctx->rbuf[ctx->flen - 1] = buf[i];
196 if(!memcmp(ctx->rbuf, ctx->arg, ctx->flen)) {
197 /* match, now output until endarg */
198 fputs(&ctx->arg[1], stdout);
199 ctx->show++;
200 }
201 continue;
202 }
203 /* show until the end */
204 memmove(&ctx->rbuf[0], &ctx->rbuf[1], ctx->elen - 1);
205 ctx->rbuf[ctx->elen - 1] = buf[i];
206 if(!memcmp(ctx->rbuf, ctx->endarg, ctx->elen))
207 return FALSE;
208
209 if(buf[i] == '\n') {
210 DEBUGASSERT(ctx->olen < sizeof(ctx->obuf));
211 if(ctx->olen == sizeof(ctx->obuf))
212 return FALSE; /* bail out */
213 ctx->obuf[ctx->olen++] = 0;
214 ctx->olen = 0;
215 puts(ctx->obuf);
216 }
217 else {
218 DEBUGASSERT(ctx->olen < sizeof(ctx->obuf));
219 if(ctx->olen == sizeof(ctx->obuf))
220 return FALSE; /* bail out */
221 ctx->obuf[ctx->olen++] = buf[i];
222 }
223 }
224 return TRUE;
225 }
226
227 #endif
228
tool_help(char * category)229 void tool_help(char *category)
230 {
231 unsigned int cols = get_terminal_columns();
232 /* If no category was provided */
233 if(!category) {
234 const char *category_note = "\nThis is not the full help; this "
235 "menu is split into categories.\nUse \"--help category\" to get "
236 "an overview of all categories, which are:";
237 const char *category_note2 =
238 "Use \"--help all\" to list all options"
239 #ifdef USE_MANUAL
240 "\nUse \"--help [option]\" to view documentation for a given option"
241 #endif
242 ;
243 puts("Usage: curl [options...] <url>");
244 print_category(CURLHELP_IMPORTANT, cols);
245 puts(category_note);
246 get_categories_list(cols);
247 puts(category_note2);
248 }
249 /* Lets print everything if "all" was provided */
250 else if(curl_strequal(category, "all"))
251 /* Print everything */
252 print_category(CURLHELP_ALL, cols);
253 /* Lets handle the string "category" differently to not print an errormsg */
254 else if(curl_strequal(category, "category"))
255 get_categories();
256 else if(category[0] == '-') {
257 #ifdef USE_MANUAL
258 /* command line option help */
259 const struct LongShort *a = NULL;
260 if(category[1] == '-') {
261 char *lookup = &category[2];
262 bool noflagged = FALSE;
263 if(!strncmp(lookup, "no-", 3)) {
264 lookup += 3;
265 noflagged = TRUE;
266 }
267 a = findlongopt(lookup);
268 if(a && noflagged && (ARGTYPE(a->desc) != ARG_BOOL))
269 /* a --no- prefix for a non-boolean is not specifying a proper
270 option */
271 a = NULL;
272 }
273 else if(!category[2])
274 a = findshortopt(category[1]);
275 if(!a) {
276 fprintf(tool_stderr, "Incorrect option name to show help for,"
277 " see curl -h\n");
278 }
279 else {
280 char cmdbuf[80];
281 if(a->letter != ' ')
282 msnprintf(cmdbuf, sizeof(cmdbuf), "\n -%c, --", a->letter);
283 else if(a->desc & ARG_NO)
284 msnprintf(cmdbuf, sizeof(cmdbuf), "\n --no-%s", a->lname);
285 else
286 msnprintf(cmdbuf, sizeof(cmdbuf), "\n %s", category);
287 #ifdef USE_MANUAL
288 if(a->cmd == C_XATTR)
289 /* this is the last option, which then ends when FILES starts */
290 showhelp("\nALL OPTIONS\n", cmdbuf, "\nFILES");
291 else
292 showhelp("\nALL OPTIONS\n", cmdbuf, "\n -");
293 #endif
294 }
295 #else
296 fprintf(tool_stderr, "Cannot comply. "
297 "This curl was built without built-in manual\n");
298 #endif
299 }
300 /* Otherwise print category and handle the case if the cat was not found */
301 else if(get_category_content(category, cols)) {
302 puts("Unknown category provided, here is a list of all categories:\n");
303 get_categories();
304 }
305 free(category);
306 }
307
is_debug(void)308 static bool is_debug(void)
309 {
310 const char *const *builtin;
311 for(builtin = feature_names; *builtin; ++builtin)
312 if(curl_strequal("debug", *builtin))
313 return TRUE;
314 return FALSE;
315 }
316
tool_version_info(void)317 void tool_version_info(void)
318 {
319 const char *const *builtin;
320 if(is_debug())
321 fprintf(tool_stderr, "WARNING: this libcurl is Debug-enabled, "
322 "do not use in production\n\n");
323
324 printf(CURL_ID "%s\n", curl_version());
325 #ifdef CURL_PATCHSTAMP
326 printf("Release-Date: %s, security patched: %s\n",
327 LIBCURL_TIMESTAMP, CURL_PATCHSTAMP);
328 #else
329 printf("Release-Date: %s\n", LIBCURL_TIMESTAMP);
330 #endif
331 if(built_in_protos[0]) {
332 #ifndef CURL_DISABLE_IPFS
333 const char *insert = NULL;
334 /* we have ipfs and ipns support if libcurl has http support */
335 for(builtin = built_in_protos; *builtin; ++builtin) {
336 if(insert) {
337 /* update insertion so ipfs will be printed in alphabetical order */
338 if(strcmp(*builtin, "ipfs") < 0)
339 insert = *builtin;
340 else
341 break;
342 }
343 else if(!strcmp(*builtin, "http")) {
344 insert = *builtin;
345 }
346 }
347 #endif /* !CURL_DISABLE_IPFS */
348 printf("Protocols:");
349 for(builtin = built_in_protos; *builtin; ++builtin) {
350 /* Special case: do not list rtmp?* protocols.
351 They may only appear together with "rtmp" */
352 if(!curl_strnequal(*builtin, "rtmp", 4) || !builtin[0][4])
353 printf(" %s", *builtin);
354 #ifndef CURL_DISABLE_IPFS
355 if(insert && insert == *builtin) {
356 printf(" ipfs ipns");
357 insert = NULL;
358 }
359 #endif /* !CURL_DISABLE_IPFS */
360 }
361 puts(""); /* newline */
362 }
363 if(feature_names[0]) {
364 const char **feat_ext;
365 size_t feat_ext_count = feature_count;
366 #ifdef CURL_CA_EMBED
367 ++feat_ext_count;
368 #endif
369 feat_ext = malloc(sizeof(*feature_names) * (feat_ext_count + 1));
370 if(feat_ext) {
371 memcpy((void *)feat_ext, feature_names,
372 sizeof(*feature_names) * feature_count);
373 feat_ext_count = feature_count;
374 #ifdef CURL_CA_EMBED
375 feat_ext[feat_ext_count++] = "CAcert";
376 #endif
377 feat_ext[feat_ext_count] = NULL;
378 qsort((void *)feat_ext, feat_ext_count, sizeof(*feat_ext),
379 struplocompare4sort);
380 printf("Features:");
381 for(builtin = feat_ext; *builtin; ++builtin)
382 printf(" %s", *builtin);
383 puts(""); /* newline */
384 free((void *)feat_ext);
385 }
386 }
387 if(strcmp(CURL_VERSION, curlinfo->version)) {
388 printf("WARNING: curl and libcurl versions do not match. "
389 "Functionality may be affected.\n");
390 }
391 }
392
tool_list_engines(void)393 void tool_list_engines(void)
394 {
395 CURL *curl = curl_easy_init();
396 struct curl_slist *engines = NULL;
397
398 /* Get the list of engines */
399 curl_easy_getinfo(curl, CURLINFO_SSL_ENGINES, &engines);
400
401 puts("Build-time engines:");
402 if(engines) {
403 for(; engines; engines = engines->next)
404 printf(" %s\n", engines->data);
405 }
406 else {
407 puts(" <none>");
408 }
409
410 /* Cleanup the list of engines */
411 curl_slist_free_all(engines);
412 curl_easy_cleanup(curl);
413 }
414