• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
34 #include "memdebug.h" /* keep this as LAST include */
35 
36 #ifdef MSDOS
37 #  define USE_WATT32
38 #endif
39 
40 struct category_descriptors {
41   const char *opt;
42   const char *desc;
43   curlhelp_t category;
44 };
45 
46 static const struct category_descriptors categories[] = {
47   {"auth", "Different types of authentication methods", CURLHELP_AUTH},
48   {"connection", "Low level networking operations",
49    CURLHELP_CONNECTION},
50   {"curl", "The command line tool itself", CURLHELP_CURL},
51   {"dns", "General DNS options", CURLHELP_DNS},
52   {"file", "FILE protocol options", CURLHELP_FILE},
53   {"ftp", "FTP protocol options", CURLHELP_FTP},
54   {"http", "HTTP and HTTPS protocol options", CURLHELP_HTTP},
55   {"imap", "IMAP protocol options", CURLHELP_IMAP},
56   /* important is left out because it is the default help page */
57   {"misc", "Options that don't fit into any other category", CURLHELP_MISC},
58   {"output", "Filesystem output", CURLHELP_OUTPUT},
59   {"pop3", "POP3 protocol options", CURLHELP_POP3},
60   {"post", "HTTP Post specific options", CURLHELP_POST},
61   {"proxy", "All options related to proxies", CURLHELP_PROXY},
62   {"scp", "SCP protocol options", CURLHELP_SCP},
63   {"sftp", "SFTP protocol options", CURLHELP_SFTP},
64   {"smtp", "SMTP protocol options", CURLHELP_SMTP},
65   {"ssh", "SSH protocol options", CURLHELP_SSH},
66   {"telnet", "TELNET protocol options", CURLHELP_TELNET},
67   {"tftp", "TFTP protocol options", CURLHELP_TFTP},
68   {"tls", "All TLS/SSL related options", CURLHELP_TLS},
69   {"upload", "All options for uploads",
70    CURLHELP_UPLOAD},
71   {"verbose", "Options related to any kind of command line output of curl",
72    CURLHELP_VERBOSE},
73   {NULL, NULL, CURLHELP_HIDDEN}
74 };
75 
76 extern const struct helptxt helptext[];
77 
78 
print_category(curlhelp_t category)79 static void print_category(curlhelp_t category)
80 {
81   unsigned int i;
82   size_t longopt = 5;
83   size_t longdesc = 5;
84 
85   for(i = 0; helptext[i].opt; ++i) {
86     size_t len;
87     if(!(helptext[i].categories & category))
88       continue;
89     len = strlen(helptext[i].opt);
90     if(len > longopt)
91       longopt = len;
92     len = strlen(helptext[i].desc);
93     if(len > longdesc)
94       longdesc = len;
95   }
96   if(longopt + longdesc > 80)
97     longopt = 80 - longdesc;
98 
99   for(i = 0; helptext[i].opt; ++i)
100     if(helptext[i].categories & category) {
101       printf(" %-*s %s\n", (int)longopt, helptext[i].opt, helptext[i].desc);
102     }
103 }
104 
105 /* Prints category if found. If not, it returns 1 */
get_category_content(const char * category)106 static int get_category_content(const char *category)
107 {
108   unsigned int i;
109   for(i = 0; categories[i].opt; ++i)
110     if(curl_strequal(categories[i].opt, category)) {
111       printf("%s: %s\n", categories[i].opt, categories[i].desc);
112       print_category(categories[i].category);
113       return 0;
114     }
115   return 1;
116 }
117 
118 /* Prints all categories and their description */
get_categories(void)119 static void get_categories(void)
120 {
121   unsigned int i;
122   for(i = 0; categories[i].opt; ++i)
123     printf(" %-11s %s\n", categories[i].opt, categories[i].desc);
124 }
125 
126 
tool_help(char * category)127 void tool_help(char *category)
128 {
129   puts("Usage: curl [options...] <url>");
130   /* If no category was provided */
131   if(!category) {
132     const char *category_note = "\nThis is not the full help, this "
133       "menu is stripped into categories.\nUse \"--help category\" to get "
134       "an overview of all categories.\nFor all options use the manual"
135       " or \"--help all\".";
136     print_category(CURLHELP_IMPORTANT);
137     puts(category_note);
138   }
139   /* Lets print everything if "all" was provided */
140   else if(curl_strequal(category, "all"))
141     /* Print everything except hidden */
142     print_category(~(CURLHELP_HIDDEN));
143   /* Lets handle the string "category" differently to not print an errormsg */
144   else if(curl_strequal(category, "category"))
145     get_categories();
146   /* Otherwise print category and handle the case if the cat was not found */
147   else if(get_category_content(category)) {
148     puts("Invalid category provided, here is a list of all categories:\n");
149     get_categories();
150   }
151   free(category);
152 }
153 
is_debug(void)154 static bool is_debug(void)
155 {
156   const char *const *builtin;
157   for(builtin = feature_names; *builtin; ++builtin)
158     if(curl_strequal("debug", *builtin))
159       return TRUE;
160   return FALSE;
161 }
162 
tool_version_info(void)163 void tool_version_info(void)
164 {
165   const char *const *builtin;
166   if(is_debug())
167     fprintf(stderr, "WARNING: this libcurl is Debug-enabled, "
168             "do not use in production\n\n");
169 
170   printf(CURL_ID "%s\n", curl_version());
171 #ifdef CURL_PATCHSTAMP
172   printf("Release-Date: %s, security patched: %s\n",
173          LIBCURL_TIMESTAMP, CURL_PATCHSTAMP);
174 #else
175   printf("Release-Date: %s\n", LIBCURL_TIMESTAMP);
176 #endif
177   if(built_in_protos[0]) {
178     printf("Protocols:");
179     for(builtin = built_in_protos; *builtin; ++builtin) {
180       /* Special case: do not list rtmp?* protocols.
181          They may only appear together with "rtmp" */
182       if(!curl_strnequal(*builtin, "rtmp", 4) || !builtin[0][4])
183         printf(" %s", *builtin);
184     }
185     puts(""); /* newline */
186   }
187   if(feature_names[0]) {
188     printf("Features:");
189     for(builtin = feature_names; *builtin; ++builtin)
190       printf(" %s", *builtin);
191     puts(""); /* newline */
192   }
193   if(strcmp(CURL_VERSION, curlinfo->version)) {
194     printf("WARNING: curl and libcurl versions do not match. "
195            "Functionality may be affected.\n");
196   }
197 }
198 
tool_list_engines(void)199 void tool_list_engines(void)
200 {
201   CURL *curl = curl_easy_init();
202   struct curl_slist *engines = NULL;
203 
204   /* Get the list of engines */
205   curl_easy_getinfo(curl, CURLINFO_SSL_ENGINES, &engines);
206 
207   puts("Build-time engines:");
208   if(engines) {
209     for(; engines; engines = engines->next)
210       printf("  %s\n", engines->data);
211   }
212   else {
213     puts("  <none>");
214   }
215 
216   /* Cleanup the list of engines */
217   curl_slist_free_all(engines);
218   curl_easy_cleanup(curl);
219 }
220