tool_help.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. #include "tool_panykey.h"
  29. #include "tool_help.h"
  30. #include "tool_libinfo.h"
  31. #include "tool_util.h"
  32. #include "tool_version.h"
  33. #include "memdebug.h" /* keep this as LAST include */
  34. #ifdef MSDOS
  35. # define USE_WATT32
  36. #endif
  37. struct category_descriptors {
  38. const char *opt;
  39. const char *desc;
  40. curlhelp_t category;
  41. };
  42. static const struct category_descriptors categories[] = {
  43. {"auth", "Different types of authentication methods", CURLHELP_AUTH},
  44. {"connection", "Low level networking operations",
  45. CURLHELP_CONNECTION},
  46. {"curl", "The command line tool itself", CURLHELP_CURL},
  47. {"dns", "General DNS options", CURLHELP_DNS},
  48. {"file", "FILE protocol options", CURLHELP_FILE},
  49. {"ftp", "FTP protocol options", CURLHELP_FTP},
  50. {"http", "HTTP and HTTPS protocol options", CURLHELP_HTTP},
  51. {"imap", "IMAP protocol options", CURLHELP_IMAP},
  52. /* important is left out because it is the default help page */
  53. {"misc", "Options that don't fit into any other category", CURLHELP_MISC},
  54. {"output", "Filesystem output", CURLHELP_OUTPUT},
  55. {"pop3", "POP3 protocol options", CURLHELP_POP3},
  56. {"post", "HTTP Post specific options", CURLHELP_POST},
  57. {"proxy", "All options related to proxies", CURLHELP_PROXY},
  58. {"scp", "SCP protocol options", CURLHELP_SCP},
  59. {"sftp", "SFTP protocol options", CURLHELP_SFTP},
  60. {"smtp", "SMTP protocol options", CURLHELP_SMTP},
  61. {"ssh", "SSH protocol options", CURLHELP_SSH},
  62. {"telnet", "TELNET protocol options", CURLHELP_TELNET},
  63. {"tftp", "TFTP protocol options", CURLHELP_TFTP},
  64. {"tls", "All TLS/SSL related options", CURLHELP_TLS},
  65. {"upload", "All options for uploads",
  66. CURLHELP_UPLOAD},
  67. {"verbose", "Options related to any kind of command line output of curl",
  68. CURLHELP_VERBOSE},
  69. {NULL, NULL, CURLHELP_HIDDEN}
  70. };
  71. extern const struct helptxt helptext[];
  72. struct feat {
  73. const char *name;
  74. int bitmask;
  75. };
  76. static const struct feat feats[] = {
  77. {"AsynchDNS", CURL_VERSION_ASYNCHDNS},
  78. {"Debug", CURL_VERSION_DEBUG},
  79. {"TrackMemory", CURL_VERSION_CURLDEBUG},
  80. {"IDN", CURL_VERSION_IDN},
  81. {"IPv6", CURL_VERSION_IPV6},
  82. {"Largefile", CURL_VERSION_LARGEFILE},
  83. {"Unicode", CURL_VERSION_UNICODE},
  84. {"SSPI", CURL_VERSION_SSPI},
  85. {"GSS-API", CURL_VERSION_GSSAPI},
  86. {"Kerberos", CURL_VERSION_KERBEROS5},
  87. {"SPNEGO", CURL_VERSION_SPNEGO},
  88. {"NTLM", CURL_VERSION_NTLM},
  89. {"NTLM_WB", CURL_VERSION_NTLM_WB},
  90. {"SSL", CURL_VERSION_SSL},
  91. {"libz", CURL_VERSION_LIBZ},
  92. {"brotli", CURL_VERSION_BROTLI},
  93. {"zstd", CURL_VERSION_ZSTD},
  94. {"CharConv", CURL_VERSION_CONV},
  95. {"TLS-SRP", CURL_VERSION_TLSAUTH_SRP},
  96. {"HTTP2", CURL_VERSION_HTTP2},
  97. {"HTTP3", CURL_VERSION_HTTP3},
  98. {"UnixSockets", CURL_VERSION_UNIX_SOCKETS},
  99. {"HTTPS-proxy", CURL_VERSION_HTTPS_PROXY},
  100. {"MultiSSL", CURL_VERSION_MULTI_SSL},
  101. {"PSL", CURL_VERSION_PSL},
  102. {"alt-svc", CURL_VERSION_ALTSVC},
  103. {"HSTS", CURL_VERSION_HSTS},
  104. {"gsasl", CURL_VERSION_GSASL},
  105. {"threadsafe", CURL_VERSION_THREADSAFE},
  106. };
  107. static void print_category(curlhelp_t category)
  108. {
  109. unsigned int i;
  110. size_t longopt = 5;
  111. size_t longdesc = 5;
  112. for(i = 0; helptext[i].opt; ++i) {
  113. size_t len;
  114. if(!(helptext[i].categories & category))
  115. continue;
  116. len = strlen(helptext[i].opt);
  117. if(len > longopt)
  118. longopt = len;
  119. len = strlen(helptext[i].desc);
  120. if(len > longdesc)
  121. longdesc = len;
  122. }
  123. if(longopt + longdesc > 80)
  124. longopt = 80 - longdesc;
  125. for(i = 0; helptext[i].opt; ++i)
  126. if(helptext[i].categories & category) {
  127. printf(" %-*s %s\n", (int)longopt, helptext[i].opt, helptext[i].desc);
  128. }
  129. }
  130. /* Prints category if found. If not, it returns 1 */
  131. static int get_category_content(const char *category)
  132. {
  133. unsigned int i;
  134. for(i = 0; categories[i].opt; ++i)
  135. if(curl_strequal(categories[i].opt, category)) {
  136. printf("%s: %s\n", categories[i].opt, categories[i].desc);
  137. print_category(categories[i].category);
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. /* Prints all categories and their description */
  143. static void get_categories(void)
  144. {
  145. unsigned int i;
  146. for(i = 0; categories[i].opt; ++i)
  147. printf(" %-11s %s\n", categories[i].opt, categories[i].desc);
  148. }
  149. void tool_help(char *category)
  150. {
  151. puts("Usage: curl [options...] <url>");
  152. /* If no category was provided */
  153. if(!category) {
  154. const char *category_note = "\nThis is not the full help, this "
  155. "menu is stripped into categories.\nUse \"--help category\" to get "
  156. "an overview of all categories.\nFor all options use the manual"
  157. " or \"--help all\".";
  158. print_category(CURLHELP_IMPORTANT);
  159. puts(category_note);
  160. }
  161. /* Lets print everything if "all" was provided */
  162. else if(curl_strequal(category, "all"))
  163. /* Print everything except hidden */
  164. print_category(~(CURLHELP_HIDDEN));
  165. /* Lets handle the string "category" differently to not print an errormsg */
  166. else if(curl_strequal(category, "category"))
  167. get_categories();
  168. /* Otherwise print category and handle the case if the cat was not found */
  169. else if(get_category_content(category)) {
  170. puts("Invalid category provided, here is a list of all categories:\n");
  171. get_categories();
  172. }
  173. free(category);
  174. }
  175. void tool_version_info(void)
  176. {
  177. const char *const *proto;
  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(curlinfo->protocols) {
  186. printf("Protocols: ");
  187. for(proto = curlinfo->protocols; *proto; ++proto) {
  188. /* Special case: do not list rtmp?* protocols.
  189. They may only appear together with "rtmp" */
  190. if(!curl_strnequal(*proto, "rtmp", 4) || !proto[0][4])
  191. printf("%s ", *proto);
  192. }
  193. puts(""); /* newline */
  194. }
  195. if(curlinfo->features) {
  196. char *featp[ sizeof(feats) / sizeof(feats[0]) + 1];
  197. size_t numfeat = 0;
  198. unsigned int i;
  199. printf("Features:");
  200. for(i = 0; i < sizeof(feats)/sizeof(feats[0]); i++) {
  201. if(curlinfo->features & feats[i].bitmask)
  202. featp[numfeat++] = (char *)feats[i].name;
  203. }
  204. qsort(&featp[0], numfeat, sizeof(char *), struplocompare4sort);
  205. for(i = 0; i< numfeat; i++)
  206. printf(" %s", featp[i]);
  207. puts(""); /* newline */
  208. }
  209. if(strcmp(CURL_VERSION, curlinfo->version)) {
  210. printf("WARNING: curl and libcurl versions do not match. "
  211. "Functionality may be affected.\n");
  212. }
  213. }
  214. void tool_list_engines(void)
  215. {
  216. CURL *curl = curl_easy_init();
  217. struct curl_slist *engines = NULL;
  218. /* Get the list of engines */
  219. curl_easy_getinfo(curl, CURLINFO_SSL_ENGINES, &engines);
  220. puts("Build-time engines:");
  221. if(engines) {
  222. for(; engines; engines = engines->next)
  223. printf(" %s\n", engines->data);
  224. }
  225. else {
  226. puts(" <none>");
  227. }
  228. /* Cleanup the list of engines */
  229. curl_slist_free_all(engines);
  230. curl_easy_cleanup(curl);
  231. }