commands.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright 2016-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <sys/param.h>
  18. #include "esp_log.h"
  19. #include "esp_console.h"
  20. #include "esp_system.h"
  21. #include "linenoise/linenoise.h"
  22. #include "argtable3/argtable3.h"
  23. #include "sys/queue.h"
  24. #define ANSI_COLOR_DEFAULT 39 /** Default foreground color */
  25. typedef struct cmd_item_ {
  26. /**
  27. * Command name (statically allocated by application)
  28. */
  29. const char *command;
  30. /**
  31. * Help text (statically allocated by application), may be NULL.
  32. */
  33. const char *help;
  34. /**
  35. * Hint text, usually lists possible arguments, dynamically allocated.
  36. * May be NULL.
  37. */
  38. char *hint;
  39. esp_console_cmd_func_t func; //!< pointer to the command handler
  40. void *argtable; //!< optional pointer to arg table
  41. SLIST_ENTRY(cmd_item_) next; //!< next command in the list
  42. } cmd_item_t;
  43. /** linked list of command structures */
  44. static SLIST_HEAD(cmd_list_, cmd_item_) s_cmd_list;
  45. /** run-time configuration options */
  46. static esp_console_config_t s_config;
  47. /** temporary buffer used for command line parsing */
  48. static char *s_tmp_line_buf;
  49. static const cmd_item_t *find_command_by_name(const char *name);
  50. esp_err_t esp_console_init(const esp_console_config_t *config)
  51. {
  52. if (!config) {
  53. return ESP_ERR_INVALID_ARG;
  54. }
  55. if (s_tmp_line_buf) {
  56. return ESP_ERR_INVALID_STATE;
  57. }
  58. memcpy(&s_config, config, sizeof(s_config));
  59. if (s_config.hint_color == 0) {
  60. s_config.hint_color = ANSI_COLOR_DEFAULT;
  61. }
  62. s_tmp_line_buf = calloc(config->max_cmdline_length, 1);
  63. if (s_tmp_line_buf == NULL) {
  64. return ESP_ERR_NO_MEM;
  65. }
  66. return ESP_OK;
  67. }
  68. esp_err_t esp_console_deinit(void)
  69. {
  70. if (!s_tmp_line_buf) {
  71. return ESP_ERR_INVALID_STATE;
  72. }
  73. free(s_tmp_line_buf);
  74. s_tmp_line_buf = NULL;
  75. cmd_item_t *it, *tmp;
  76. SLIST_FOREACH_SAFE(it, &s_cmd_list, next, tmp) {
  77. SLIST_REMOVE(&s_cmd_list, it, cmd_item_, next);
  78. free(it->hint);
  79. free(it);
  80. }
  81. return ESP_OK;
  82. }
  83. esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
  84. {
  85. cmd_item_t *item = NULL;
  86. if (!cmd || cmd->command == NULL) {
  87. return ESP_ERR_INVALID_ARG;
  88. }
  89. if (strchr(cmd->command, ' ') != NULL) {
  90. return ESP_ERR_INVALID_ARG;
  91. }
  92. item = (cmd_item_t *)find_command_by_name(cmd->command);
  93. if (!item) {
  94. // not registered before
  95. item = calloc(1, sizeof(*item));
  96. if (item == NULL) {
  97. return ESP_ERR_NO_MEM;
  98. }
  99. } else {
  100. // remove from list and free the old hint, because we will alloc new hint for the command
  101. SLIST_REMOVE(&s_cmd_list, item, cmd_item_, next);
  102. free(item->hint);
  103. }
  104. item->command = cmd->command;
  105. item->help = cmd->help;
  106. if (cmd->hint) {
  107. /* Prepend a space before the hint. It separates command name and
  108. * the hint. arg_print_syntax below adds this space as well.
  109. */
  110. int unused __attribute__((unused));
  111. unused = asprintf(&item->hint, " %s", cmd->hint);
  112. } else if (cmd->argtable) {
  113. /* Generate hint based on cmd->argtable */
  114. char *buf = NULL;
  115. size_t buf_size = 0;
  116. FILE *f = open_memstream(&buf, &buf_size);
  117. if (f != NULL) {
  118. arg_print_syntax(f, cmd->argtable, NULL);
  119. fclose(f);
  120. }
  121. item->hint = buf;
  122. }
  123. item->argtable = cmd->argtable;
  124. item->func = cmd->func;
  125. cmd_item_t *last = SLIST_FIRST(&s_cmd_list);
  126. if (last == NULL) {
  127. SLIST_INSERT_HEAD(&s_cmd_list, item, next);
  128. } else {
  129. cmd_item_t *it;
  130. while ((it = SLIST_NEXT(last, next)) != NULL) {
  131. last = it;
  132. }
  133. SLIST_INSERT_AFTER(last, item, next);
  134. }
  135. return ESP_OK;
  136. }
  137. void esp_console_get_completion(const char *buf, linenoiseCompletions *lc)
  138. {
  139. size_t len = strlen(buf);
  140. if (len == 0) {
  141. return;
  142. }
  143. cmd_item_t *it;
  144. SLIST_FOREACH(it, &s_cmd_list, next) {
  145. /* Check if command starts with buf */
  146. if (strncmp(buf, it->command, len) == 0) {
  147. linenoiseAddCompletion(lc, it->command);
  148. }
  149. }
  150. }
  151. const char *esp_console_get_hint(const char *buf, int *color, int *bold)
  152. {
  153. size_t len = strlen(buf);
  154. cmd_item_t *it;
  155. SLIST_FOREACH(it, &s_cmd_list, next) {
  156. if (strlen(it->command) == len &&
  157. strncmp(buf, it->command, len) == 0) {
  158. *color = s_config.hint_color;
  159. *bold = s_config.hint_bold;
  160. return it->hint;
  161. }
  162. }
  163. return NULL;
  164. }
  165. static const cmd_item_t *find_command_by_name(const char *name)
  166. {
  167. const cmd_item_t *cmd = NULL;
  168. cmd_item_t *it;
  169. size_t len = strlen(name);
  170. SLIST_FOREACH(it, &s_cmd_list, next) {
  171. if (strlen(it->command) == len &&
  172. strcmp(name, it->command) == 0) {
  173. cmd = it;
  174. break;
  175. }
  176. }
  177. return cmd;
  178. }
  179. esp_err_t esp_console_run(const char *cmdline, int *cmd_ret)
  180. {
  181. if (s_tmp_line_buf == NULL) {
  182. return ESP_ERR_INVALID_STATE;
  183. }
  184. char **argv = (char **) calloc(s_config.max_cmdline_args, sizeof(char *));
  185. if (argv == NULL) {
  186. return ESP_ERR_NO_MEM;
  187. }
  188. strlcpy(s_tmp_line_buf, cmdline, s_config.max_cmdline_length);
  189. size_t argc = esp_console_split_argv(s_tmp_line_buf, argv,
  190. s_config.max_cmdline_args);
  191. if (argc == 0) {
  192. free(argv);
  193. return ESP_ERR_INVALID_ARG;
  194. }
  195. const cmd_item_t *cmd = find_command_by_name(argv[0]);
  196. if (cmd == NULL) {
  197. free(argv);
  198. return ESP_ERR_NOT_FOUND;
  199. }
  200. *cmd_ret = (*cmd->func)(argc, argv);
  201. free(argv);
  202. return ESP_OK;
  203. }
  204. static int help_command(int argc, char **argv)
  205. {
  206. cmd_item_t *it;
  207. /* Print summary of each command */
  208. SLIST_FOREACH(it, &s_cmd_list, next) {
  209. if (it->help == NULL) {
  210. continue;
  211. }
  212. /* First line: command name and hint
  213. * Pad all the hints to the same column
  214. */
  215. const char *hint = (it->hint) ? it->hint : "";
  216. printf("%-s %s\n", it->command, hint);
  217. /* Second line: print help.
  218. * Argtable has a nice helper function for this which does line
  219. * wrapping.
  220. */
  221. printf(" "); // arg_print_formatted does not indent the first line
  222. arg_print_formatted(stdout, 2, 78, it->help);
  223. /* Finally, print the list of arguments */
  224. if (it->argtable) {
  225. arg_print_glossary(stdout, (void **) it->argtable, " %12s %s\n");
  226. }
  227. printf("\n");
  228. }
  229. return 0;
  230. }
  231. esp_err_t esp_console_register_help_command(void)
  232. {
  233. esp_console_cmd_t command = {
  234. .command = "help",
  235. .help = "Print the list of registered commands",
  236. .func = &help_command
  237. };
  238. return esp_console_cmd_register(&command);
  239. }