tool_help.c 6.7 KB

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