tool_help.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #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 "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. {"ech", "All Encrypted Client Hello (ECH) options", CURLHELP_ECH},
  66. {"upload", "All options for uploads",
  67. CURLHELP_UPLOAD},
  68. {"verbose", "Options related to any kind of command line output of curl",
  69. CURLHELP_VERBOSE},
  70. {NULL, NULL, CURLHELP_HIDDEN}
  71. };
  72. static void print_category(curlhelp_t category, unsigned int cols)
  73. {
  74. unsigned int i;
  75. size_t longopt = 5;
  76. size_t longdesc = 5;
  77. for(i = 0; helptext[i].opt; ++i) {
  78. size_t len;
  79. if(!(helptext[i].categories & category))
  80. continue;
  81. len = strlen(helptext[i].opt);
  82. if(len > longopt)
  83. longopt = len;
  84. len = strlen(helptext[i].desc);
  85. if(len > longdesc)
  86. longdesc = len;
  87. }
  88. if(longopt + longdesc > cols)
  89. longopt = cols - longdesc;
  90. for(i = 0; helptext[i].opt; ++i)
  91. if(helptext[i].categories & category) {
  92. int opt = (int)longopt;
  93. size_t desclen = strlen(helptext[i].desc);
  94. if(opt + desclen >= (cols - 2)) {
  95. if(desclen < (cols - 2))
  96. opt = (cols - 3) - (int)desclen;
  97. else
  98. opt = 0;
  99. }
  100. printf(" %-*s %s\n", opt, helptext[i].opt, helptext[i].desc);
  101. }
  102. }
  103. /* Prints category if found. If not, it returns 1 */
  104. static int get_category_content(const char *category, unsigned int cols)
  105. {
  106. unsigned int i;
  107. for(i = 0; categories[i].opt; ++i)
  108. if(curl_strequal(categories[i].opt, category)) {
  109. printf("%s: %s\n", categories[i].opt, categories[i].desc);
  110. print_category(categories[i].category, cols);
  111. return 0;
  112. }
  113. return 1;
  114. }
  115. /* Prints all categories and their description */
  116. static void get_categories(void)
  117. {
  118. unsigned int i;
  119. for(i = 0; categories[i].opt; ++i)
  120. printf(" %-11s %s\n", categories[i].opt, categories[i].desc);
  121. }
  122. void tool_help(char *category)
  123. {
  124. unsigned int cols = get_terminal_columns();
  125. puts("Usage: curl [options...] <url>");
  126. /* If no category was provided */
  127. if(!category) {
  128. const char *category_note = "\nThis is not the full help, this "
  129. "menu is stripped into categories.\nUse \"--help category\" to get "
  130. "an overview of all categories.\nFor all options use the manual"
  131. " or \"--help all\".";
  132. print_category(CURLHELP_IMPORTANT, cols);
  133. puts(category_note);
  134. }
  135. /* Lets print everything if "all" was provided */
  136. else if(curl_strequal(category, "all"))
  137. /* Print everything except hidden */
  138. print_category(~(CURLHELP_HIDDEN), cols);
  139. /* Lets handle the string "category" differently to not print an errormsg */
  140. else if(curl_strequal(category, "category"))
  141. get_categories();
  142. /* Otherwise print category and handle the case if the cat was not found */
  143. else if(get_category_content(category, cols)) {
  144. puts("Invalid category provided, here is a list of all categories:\n");
  145. get_categories();
  146. }
  147. free(category);
  148. }
  149. static bool is_debug(void)
  150. {
  151. const char *const *builtin;
  152. for(builtin = feature_names; *builtin; ++builtin)
  153. if(curl_strequal("debug", *builtin))
  154. return TRUE;
  155. return FALSE;
  156. }
  157. void tool_version_info(void)
  158. {
  159. const char *const *builtin;
  160. if(is_debug())
  161. fprintf(tool_stderr, "WARNING: this libcurl is Debug-enabled, "
  162. "do not use in production\n\n");
  163. printf(CURL_ID "%s\n", curl_version());
  164. #ifdef CURL_PATCHSTAMP
  165. printf("Release-Date: %s, security patched: %s\n",
  166. LIBCURL_TIMESTAMP, CURL_PATCHSTAMP);
  167. #else
  168. printf("Release-Date: %s\n", LIBCURL_TIMESTAMP);
  169. #endif
  170. if(built_in_protos[0]) {
  171. const char *insert = NULL;
  172. /* we have ipfs and ipns support if libcurl has http support */
  173. for(builtin = built_in_protos; *builtin; ++builtin) {
  174. if(insert) {
  175. /* update insertion so ipfs will be printed in alphabetical order */
  176. if(strcmp(*builtin, "ipfs") < 0)
  177. insert = *builtin;
  178. else
  179. break;
  180. }
  181. else if(!strcmp(*builtin, "http")) {
  182. insert = *builtin;
  183. }
  184. }
  185. printf("Protocols:");
  186. for(builtin = built_in_protos; *builtin; ++builtin) {
  187. /* Special case: do not list rtmp?* protocols.
  188. They may only appear together with "rtmp" */
  189. if(!curl_strnequal(*builtin, "rtmp", 4) || !builtin[0][4])
  190. printf(" %s", *builtin);
  191. if(insert && insert == *builtin) {
  192. printf(" ipfs ipns");
  193. insert = NULL;
  194. }
  195. }
  196. puts(""); /* newline */
  197. }
  198. if(feature_names[0]) {
  199. printf("Features:");
  200. for(builtin = feature_names; *builtin; ++builtin)
  201. printf(" %s", *builtin);
  202. puts(""); /* newline */
  203. }
  204. if(strcmp(CURL_VERSION, curlinfo->version)) {
  205. printf("WARNING: curl and libcurl versions do not match. "
  206. "Functionality may be affected.\n");
  207. }
  208. }
  209. void tool_list_engines(void)
  210. {
  211. CURL *curl = curl_easy_init();
  212. struct curl_slist *engines = NULL;
  213. /* Get the list of engines */
  214. curl_easy_getinfo(curl, CURLINFO_SSL_ENGINES, &engines);
  215. puts("Build-time engines:");
  216. if(engines) {
  217. for(; engines; engines = engines->next)
  218. printf(" %s\n", engines->data);
  219. }
  220. else {
  221. puts(" <none>");
  222. }
  223. /* Cleanup the list of engines */
  224. curl_slist_free_all(engines);
  225. curl_easy_cleanup(curl);
  226. }