commands.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <sys/param.h>
  10. #include "esp_heap_caps.h"
  11. #include "esp_log.h"
  12. #include "esp_console.h"
  13. #include "esp_system.h"
  14. #include "linenoise/linenoise.h"
  15. #include "argtable3/argtable3.h"
  16. #include "sys/queue.h"
  17. #define ANSI_COLOR_DEFAULT 39 /** Default foreground color */
  18. typedef struct cmd_item_ {
  19. /**
  20. * Command name (statically allocated by application)
  21. */
  22. const char *command;
  23. /**
  24. * Help text (statically allocated by application), may be NULL.
  25. */
  26. const char *help;
  27. /**
  28. * Hint text, usually lists possible arguments, dynamically allocated.
  29. * May be NULL.
  30. */
  31. char *hint;
  32. esp_console_cmd_func_t func; //!< pointer to the command handler
  33. void *argtable; //!< optional pointer to arg table
  34. SLIST_ENTRY(cmd_item_) next; //!< next command in the list
  35. } cmd_item_t;
  36. /** linked list of command structures */
  37. static SLIST_HEAD(cmd_list_, cmd_item_) s_cmd_list;
  38. /** run-time configuration options */
  39. static esp_console_config_t s_config = {
  40. .heap_alloc_caps = MALLOC_CAP_DEFAULT
  41. };
  42. /** temporary buffer used for command line parsing */
  43. static char *s_tmp_line_buf;
  44. static const cmd_item_t *find_command_by_name(const char *name);
  45. esp_err_t esp_console_init(const esp_console_config_t *config)
  46. {
  47. if (!config) {
  48. return ESP_ERR_INVALID_ARG;
  49. }
  50. if (s_tmp_line_buf) {
  51. return ESP_ERR_INVALID_STATE;
  52. }
  53. memcpy(&s_config, config, sizeof(s_config));
  54. if (s_config.hint_color == 0) {
  55. s_config.hint_color = ANSI_COLOR_DEFAULT;
  56. }
  57. if (s_config.heap_alloc_caps == 0) {
  58. s_config.heap_alloc_caps = MALLOC_CAP_DEFAULT;
  59. }
  60. s_tmp_line_buf = heap_caps_calloc(1, config->max_cmdline_length, s_config.heap_alloc_caps);
  61. if (s_tmp_line_buf == NULL) {
  62. return ESP_ERR_NO_MEM;
  63. }
  64. return ESP_OK;
  65. }
  66. esp_err_t esp_console_deinit(void)
  67. {
  68. if (!s_tmp_line_buf) {
  69. return ESP_ERR_INVALID_STATE;
  70. }
  71. free(s_tmp_line_buf);
  72. s_tmp_line_buf = NULL;
  73. cmd_item_t *it, *tmp;
  74. SLIST_FOREACH_SAFE(it, &s_cmd_list, next, tmp) {
  75. SLIST_REMOVE(&s_cmd_list, it, cmd_item_, next);
  76. free(it->hint);
  77. free(it);
  78. }
  79. return ESP_OK;
  80. }
  81. esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
  82. {
  83. cmd_item_t *item = NULL;
  84. if (!cmd || cmd->command == NULL) {
  85. return ESP_ERR_INVALID_ARG;
  86. }
  87. if (strchr(cmd->command, ' ') != NULL) {
  88. return ESP_ERR_INVALID_ARG;
  89. }
  90. item = (cmd_item_t *)find_command_by_name(cmd->command);
  91. if (!item) {
  92. // not registered before
  93. item = heap_caps_calloc(1, sizeof(*item), s_config.heap_alloc_caps);
  94. if (item == NULL) {
  95. return ESP_ERR_NO_MEM;
  96. }
  97. } else {
  98. // remove from list and free the old hint, because we will alloc new hint for the command
  99. SLIST_REMOVE(&s_cmd_list, item, cmd_item_, next);
  100. free(item->hint);
  101. }
  102. item->command = cmd->command;
  103. item->help = cmd->help;
  104. if (cmd->hint) {
  105. /* Prepend a space before the hint. It separates command name and
  106. * the hint. arg_print_syntax below adds this space as well.
  107. */
  108. int unused __attribute__((unused));
  109. unused = asprintf(&item->hint, " %s", cmd->hint);
  110. } else if (cmd->argtable) {
  111. /* Generate hint based on cmd->argtable */
  112. char *buf = NULL;
  113. size_t buf_size = 0;
  114. FILE *f = open_memstream(&buf, &buf_size);
  115. if (f != NULL) {
  116. arg_print_syntax(f, cmd->argtable, NULL);
  117. fclose(f);
  118. }
  119. item->hint = buf;
  120. }
  121. item->argtable = cmd->argtable;
  122. item->func = cmd->func;
  123. cmd_item_t *last = SLIST_FIRST(&s_cmd_list);
  124. if (last == NULL) {
  125. SLIST_INSERT_HEAD(&s_cmd_list, item, next);
  126. } else {
  127. cmd_item_t *it;
  128. while ((it = SLIST_NEXT(last, next)) != NULL) {
  129. last = it;
  130. }
  131. SLIST_INSERT_AFTER(last, item, next);
  132. }
  133. return ESP_OK;
  134. }
  135. void esp_console_get_completion(const char *buf, linenoiseCompletions *lc)
  136. {
  137. size_t len = strlen(buf);
  138. if (len == 0) {
  139. return;
  140. }
  141. cmd_item_t *it;
  142. SLIST_FOREACH(it, &s_cmd_list, next) {
  143. /* Check if command starts with buf */
  144. if (strncmp(buf, it->command, len) == 0) {
  145. linenoiseAddCompletion(lc, it->command);
  146. }
  147. }
  148. }
  149. const char *esp_console_get_hint(const char *buf, int *color, int *bold)
  150. {
  151. size_t len = strlen(buf);
  152. cmd_item_t *it;
  153. SLIST_FOREACH(it, &s_cmd_list, next) {
  154. if (strlen(it->command) == len &&
  155. strncmp(buf, it->command, len) == 0) {
  156. *color = s_config.hint_color;
  157. *bold = s_config.hint_bold;
  158. return it->hint;
  159. }
  160. }
  161. return NULL;
  162. }
  163. static const cmd_item_t *find_command_by_name(const char *name)
  164. {
  165. const cmd_item_t *cmd = NULL;
  166. cmd_item_t *it;
  167. size_t len = strlen(name);
  168. SLIST_FOREACH(it, &s_cmd_list, next) {
  169. if (strlen(it->command) == len &&
  170. strcmp(name, it->command) == 0) {
  171. cmd = it;
  172. break;
  173. }
  174. }
  175. return cmd;
  176. }
  177. esp_err_t esp_console_run(const char *cmdline, int *cmd_ret)
  178. {
  179. if (s_tmp_line_buf == NULL) {
  180. return ESP_ERR_INVALID_STATE;
  181. }
  182. char **argv = (char **) heap_caps_calloc(s_config.max_cmdline_args, sizeof(char *), s_config.heap_alloc_caps);
  183. if (argv == NULL) {
  184. return ESP_ERR_NO_MEM;
  185. }
  186. strlcpy(s_tmp_line_buf, cmdline, s_config.max_cmdline_length);
  187. size_t argc = esp_console_split_argv(s_tmp_line_buf, argv,
  188. s_config.max_cmdline_args);
  189. if (argc == 0) {
  190. free(argv);
  191. return ESP_ERR_INVALID_ARG;
  192. }
  193. const cmd_item_t *cmd = find_command_by_name(argv[0]);
  194. if (cmd == NULL) {
  195. free(argv);
  196. return ESP_ERR_NOT_FOUND;
  197. }
  198. *cmd_ret = (*cmd->func)(argc, argv);
  199. free(argv);
  200. return ESP_OK;
  201. }
  202. static struct {
  203. struct arg_str *help_cmd;
  204. struct arg_end *end;
  205. } help_args;
  206. static void print_arg_help(cmd_item_t *it)
  207. {
  208. /* First line: command name and hint
  209. * Pad all the hints to the same column
  210. */
  211. const char *hint = (it->hint) ? it->hint : "";
  212. printf("%-s %s\n", it->command, hint);
  213. /* Second line: print help.
  214. * Argtable has a nice helper function for this which does line
  215. * wrapping.
  216. */
  217. printf(" "); // arg_print_formatted does not indent the first line
  218. arg_print_formatted(stdout, 2, 78, it->help);
  219. /* Finally, print the list of arguments */
  220. if (it->argtable) {
  221. arg_print_glossary(stdout, (void **) it->argtable, " %12s %s\n");
  222. }
  223. printf("\n");
  224. }
  225. static int help_command(int argc, char **argv)
  226. {
  227. int nerrors = arg_parse(argc, argv, (void **) &help_args);
  228. if (nerrors != 0) {
  229. arg_print_errors(stderr, help_args.end, argv[0]);
  230. return 1;
  231. }
  232. cmd_item_t *it;
  233. int ret_value = 1;
  234. if (help_args.help_cmd->count == 0) {
  235. /* Print summary of each command */
  236. SLIST_FOREACH(it, &s_cmd_list, next) {
  237. if (it->help == NULL) {
  238. continue;
  239. }
  240. print_arg_help(it);
  241. }
  242. ret_value = 0;
  243. } else {
  244. /* Print summary of given command */
  245. bool found_command = false;
  246. SLIST_FOREACH(it, &s_cmd_list, next) {
  247. if (it->help == NULL) {
  248. continue;
  249. }
  250. if (strcmp(help_args.help_cmd->sval[0], it->command) == 0) {
  251. print_arg_help(it);
  252. found_command = true;
  253. ret_value = 0;
  254. break;
  255. }
  256. }
  257. /* If given command has not been found, print error message*/
  258. if (!found_command) {
  259. printf("help: Unrecognized option '%s'. Please use correct command as argument "
  260. "or type 'help' only to print help for all commands\n", help_args.help_cmd->sval[0]);
  261. }
  262. }
  263. return ret_value;
  264. }
  265. esp_err_t esp_console_register_help_command(void)
  266. {
  267. help_args.help_cmd = arg_str0(NULL, NULL, "<string>", "Name of command");
  268. help_args.end = arg_end(1);
  269. esp_console_cmd_t command = {
  270. .command = "help",
  271. .help = "Print the summary of all registered commands if no arguments "
  272. "are given, otherwise print summary of given command.",
  273. .func = &help_command,
  274. .argtable = &help_args
  275. };
  276. return esp_console_cmd_register(&command);
  277. }