xref: /curl/src/tool_help.c (revision a362962b)
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 #define ENABLE_CURLX_PRINTF
26 /* use our own printf() functions */
27 #include "curlx.h"
28 
29 #include "tool_help.h"
30 #include "tool_libinfo.h"
31 #include "tool_util.h"
32 #include "tool_version.h"
33 #include "tool_cb_prg.h"
34 
35 #include "memdebug.h" /* keep this as LAST include */
36 
37 #ifdef MSDOS
38 #  define USE_WATT32
39 #endif
40 
41 struct category_descriptors {
42   const char *opt;
43   const char *desc;
44   curlhelp_t category;
45 };
46 
47 static const struct category_descriptors categories[] = {
48   {"auth", "Different types of authentication methods", CURLHELP_AUTH},
49   {"connection", "Low level networking operations",
50    CURLHELP_CONNECTION},
51   {"curl", "The command line tool itself", CURLHELP_CURL},
52   {"dns", "General DNS options", CURLHELP_DNS},
53   {"file", "FILE protocol options", CURLHELP_FILE},
54   {"ftp", "FTP protocol options", CURLHELP_FTP},
55   {"http", "HTTP and HTTPS protocol options", CURLHELP_HTTP},
56   {"imap", "IMAP protocol options", CURLHELP_IMAP},
57   /* important is left out because it is the default help page */
58   {"misc", "Options that don't fit into any other category", CURLHELP_MISC},
59   {"output", "Filesystem output", CURLHELP_OUTPUT},
60   {"pop3", "POP3 protocol options", CURLHELP_POP3},
61   {"post", "HTTP Post specific options", CURLHELP_POST},
62   {"proxy", "All options related to proxies", CURLHELP_PROXY},
63   {"scp", "SCP protocol options", CURLHELP_SCP},
64   {"sftp", "SFTP protocol options", CURLHELP_SFTP},
65   {"smtp", "SMTP protocol options", CURLHELP_SMTP},
66   {"ssh", "SSH protocol options", CURLHELP_SSH},
67   {"telnet", "TELNET protocol options", CURLHELP_TELNET},
68   {"tftp", "TFTP protocol options", CURLHELP_TFTP},
69   {"tls", "All TLS/SSL related options", CURLHELP_TLS},
70   {"ech", "All Encrypted Client Hello (ECH) options", CURLHELP_ECH},
71   {"upload", "All options for uploads",
72    CURLHELP_UPLOAD},
73   {"verbose", "Options related to any kind of command line output of curl",
74    CURLHELP_VERBOSE},
75   {NULL, NULL, CURLHELP_HIDDEN}
76 };
77 
print_category(curlhelp_t category,unsigned int cols)78 static void print_category(curlhelp_t 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       int opt = (int)longopt;
101       size_t desclen = strlen(helptext[i].desc);
102       if(opt + desclen >= (cols - 2)) {
103         if(desclen < (cols - 2))
104           opt = (cols - 3) - (int)desclen;
105         else
106           opt = 0;
107       }
108       printf(" %-*s  %s\n", 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; categories[i].opt; ++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; categories[i].opt; ++i)
130     printf(" %-11s %s\n", categories[i].opt, categories[i].desc);
131 }
132 
133 
tool_help(char * category)134 void tool_help(char *category)
135 {
136   unsigned int cols = get_terminal_columns();
137   puts("Usage: curl [options...] <url>");
138   /* If no category was provided */
139   if(!category) {
140     const char *category_note = "\nThis is not the full help, this "
141       "menu is stripped into categories.\nUse \"--help category\" to get "
142       "an overview of all categories.\nFor all options use the manual"
143       " or \"--help all\".";
144     print_category(CURLHELP_IMPORTANT, cols);
145     puts(category_note);
146   }
147   /* Lets print everything if "all" was provided */
148   else if(curl_strequal(category, "all"))
149     /* Print everything except hidden */
150     print_category(~(CURLHELP_HIDDEN), cols);
151   /* Lets handle the string "category" differently to not print an errormsg */
152   else if(curl_strequal(category, "category"))
153     get_categories();
154   /* Otherwise print category and handle the case if the cat was not found */
155   else if(get_category_content(category, cols)) {
156     puts("Invalid category provided, here is a list of all categories:\n");
157     get_categories();
158   }
159   free(category);
160 }
161 
is_debug(void)162 static bool is_debug(void)
163 {
164   const char *const *builtin;
165   for(builtin = feature_names; *builtin; ++builtin)
166     if(curl_strequal("debug", *builtin))
167       return TRUE;
168   return FALSE;
169 }
170 
tool_version_info(void)171 void tool_version_info(void)
172 {
173   const char *const *builtin;
174   if(is_debug())
175     fprintf(tool_stderr, "WARNING: this libcurl is Debug-enabled, "
176             "do not use in production\n\n");
177 
178   printf(CURL_ID "%s\n", curl_version());
179 #ifdef CURL_PATCHSTAMP
180   printf("Release-Date: %s, security patched: %s\n",
181          LIBCURL_TIMESTAMP, CURL_PATCHSTAMP);
182 #else
183   printf("Release-Date: %s\n", LIBCURL_TIMESTAMP);
184 #endif
185   if(built_in_protos[0]) {
186     const char *insert = NULL;
187     /* we have ipfs and ipns support if libcurl has http support */
188     for(builtin = built_in_protos; *builtin; ++builtin) {
189       if(insert) {
190         /* update insertion so ipfs will be printed in alphabetical order */
191         if(strcmp(*builtin, "ipfs") < 0)
192           insert = *builtin;
193         else
194           break;
195       }
196       else if(!strcmp(*builtin, "http")) {
197         insert = *builtin;
198       }
199     }
200     printf("Protocols:");
201     for(builtin = built_in_protos; *builtin; ++builtin) {
202       /* Special case: do not list rtmp?* protocols.
203          They may only appear together with "rtmp" */
204       if(!curl_strnequal(*builtin, "rtmp", 4) || !builtin[0][4])
205         printf(" %s", *builtin);
206       if(insert && insert == *builtin) {
207         printf(" ipfs ipns");
208         insert = NULL;
209       }
210     }
211     puts(""); /* newline */
212   }
213   if(feature_names[0]) {
214     printf("Features:");
215     for(builtin = feature_names; *builtin; ++builtin)
216       printf(" %s", *builtin);
217     puts(""); /* newline */
218   }
219   if(strcmp(CURL_VERSION, curlinfo->version)) {
220     printf("WARNING: curl and libcurl versions do not match. "
221            "Functionality may be affected.\n");
222   }
223 }
224 
tool_list_engines(void)225 void tool_list_engines(void)
226 {
227   CURL *curl = curl_easy_init();
228   struct curl_slist *engines = NULL;
229 
230   /* Get the list of engines */
231   curl_easy_getinfo(curl, CURLINFO_SSL_ENGINES, &engines);
232 
233   puts("Build-time engines:");
234   if(engines) {
235     for(; engines; engines = engines->next)
236       printf("  %s\n", engines->data);
237   }
238   else {
239     puts("  <none>");
240   }
241 
242   /* Cleanup the list of engines */
243   curl_slist_free_all(engines);
244   curl_easy_cleanup(curl);
245 }
246