commands.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2016-2017 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 "linenoise/linenoise.h"
  21. #include "argtable3/argtable3.h"
  22. #include "rom/queue.h"
  23. #define ANSI_COLOR_DEFAULT 39 /** Default foreground color */
  24. typedef struct cmd_item_ {
  25. /**
  26. * Command name (statically allocated by application)
  27. */
  28. const char *command;
  29. /**
  30. * Help text (statically allocated by application), may be NULL.
  31. */
  32. const char *help;
  33. /**
  34. * Hint text, usually lists possible arguments, dynamically allocated.
  35. * May be NULL.
  36. */
  37. char *hint;
  38. esp_console_cmd_func_t func; //!< pointer to the command handler
  39. void *argtable; //!< optional pointer to arg table
  40. SLIST_ENTRY(cmd_item_) next; //!< next command in the list
  41. } cmd_item_t;
  42. /** linked list of command structures */
  43. static SLIST_HEAD(cmd_list_, cmd_item_) s_cmd_list;
  44. /** run-time configuration options */
  45. static esp_console_config_t s_config;
  46. /** temporary buffer used for command line parsing */
  47. static char *s_tmp_line_buf;
  48. static const cmd_item_t *find_command_by_name(const char *name);
  49. esp_err_t esp_console_init(const esp_console_config_t *config)
  50. {
  51. if (s_tmp_line_buf) {
  52. return ESP_ERR_INVALID_STATE;
  53. }
  54. memcpy(&s_config, config, sizeof(s_config));
  55. if (s_config.hint_color == 0) {
  56. s_config.hint_color = ANSI_COLOR_DEFAULT;
  57. }
  58. s_tmp_line_buf = calloc(config->max_cmdline_length, 1);
  59. if (s_tmp_line_buf == NULL) {
  60. return ESP_ERR_NO_MEM;
  61. }
  62. return ESP_OK;
  63. }
  64. esp_err_t esp_console_deinit()
  65. {
  66. if (!s_tmp_line_buf) {
  67. return ESP_ERR_INVALID_STATE;
  68. }
  69. free(s_tmp_line_buf);
  70. cmd_item_t *it, *tmp;
  71. SLIST_FOREACH_SAFE(it, &s_cmd_list, next, tmp) {
  72. free(it->hint);
  73. free(it);
  74. }
  75. return ESP_OK;
  76. }
  77. esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
  78. {
  79. cmd_item_t *item = (cmd_item_t *) calloc(1, sizeof(*item));
  80. if (item == NULL) {
  81. return ESP_ERR_NO_MEM;
  82. }
  83. if (cmd->command == NULL) {
  84. free(item);
  85. return ESP_ERR_INVALID_ARG;
  86. }
  87. if (strchr(cmd->command, ' ') != NULL) {
  88. free(item);
  89. return ESP_ERR_INVALID_ARG;
  90. }
  91. item->command = cmd->command;
  92. item->help = cmd->help;
  93. if (cmd->hint) {
  94. /* Prepend a space before the hint. It separates command name and
  95. * the hint. arg_print_syntax below adds this space as well.
  96. */
  97. int unused __attribute__((unused));
  98. unused = asprintf(&item->hint, " %s", cmd->hint);
  99. } else if (cmd->argtable) {
  100. /* Generate hint based on cmd->argtable */
  101. char *buf = NULL;
  102. size_t buf_size = 0;
  103. FILE *f = open_memstream(&buf, &buf_size);
  104. if (f != NULL) {
  105. arg_print_syntax(f, cmd->argtable, NULL);
  106. fclose(f);
  107. }
  108. item->hint = buf;
  109. }
  110. item->argtable = cmd->argtable;
  111. item->func = cmd->func;
  112. cmd_item_t *last = SLIST_FIRST(&s_cmd_list);
  113. if (last == NULL) {
  114. SLIST_INSERT_HEAD(&s_cmd_list, item, next);
  115. } else {
  116. cmd_item_t *it;
  117. while ((it = SLIST_NEXT(last, next)) != NULL) {
  118. last = it;
  119. }
  120. SLIST_INSERT_AFTER(last, item, next);
  121. }
  122. return ESP_OK;
  123. }
  124. void esp_console_get_completion(const char *buf, linenoiseCompletions *lc)
  125. {
  126. size_t len = strlen(buf);
  127. if (len == 0) {
  128. return;
  129. }
  130. cmd_item_t *it;
  131. SLIST_FOREACH(it, &s_cmd_list, next) {
  132. /* Check if command starts with buf */
  133. if (strncmp(buf, it->command, len) == 0) {
  134. linenoiseAddCompletion(lc, it->command);
  135. }
  136. }
  137. }
  138. const char *esp_console_get_hint(const char *buf, int *color, int *bold)
  139. {
  140. int len = strlen(buf);
  141. cmd_item_t *it;
  142. SLIST_FOREACH(it, &s_cmd_list, next) {
  143. if (strlen(it->command) == len &&
  144. strncmp(buf, it->command, len) == 0) {
  145. *color = s_config.hint_color;
  146. *bold = s_config.hint_bold;
  147. return it->hint;
  148. }
  149. }
  150. return NULL;
  151. }
  152. static const cmd_item_t *find_command_by_name(const char *name)
  153. {
  154. const cmd_item_t *cmd = NULL;
  155. cmd_item_t *it;
  156. SLIST_FOREACH(it, &s_cmd_list, next) {
  157. if (strcmp(name, it->command) == 0) {
  158. cmd = it;
  159. break;
  160. }
  161. }
  162. return cmd;
  163. }
  164. esp_err_t esp_console_run(const char *cmdline, int *cmd_ret)
  165. {
  166. if (s_tmp_line_buf == NULL) {
  167. return ESP_ERR_INVALID_STATE;
  168. }
  169. char **argv = (char **) calloc(s_config.max_cmdline_args, sizeof(char *));
  170. if (argv == NULL) {
  171. return ESP_ERR_NO_MEM;
  172. }
  173. strlcpy(s_tmp_line_buf, cmdline, s_config.max_cmdline_length);
  174. size_t argc = esp_console_split_argv(s_tmp_line_buf, argv,
  175. s_config.max_cmdline_args);
  176. if (argc == 0) {
  177. free(argv);
  178. return ESP_ERR_INVALID_ARG;
  179. }
  180. const cmd_item_t *cmd = find_command_by_name(argv[0]);
  181. if (cmd == NULL) {
  182. free(argv);
  183. return ESP_ERR_NOT_FOUND;
  184. }
  185. *cmd_ret = (*cmd->func)(argc, argv);
  186. free(argv);
  187. return ESP_OK;
  188. }
  189. static int help_command(int argc, char **argv)
  190. {
  191. cmd_item_t *it;
  192. /* Print summary of each command */
  193. SLIST_FOREACH(it, &s_cmd_list, next) {
  194. if (it->help == NULL) {
  195. continue;
  196. }
  197. /* First line: command name and hint
  198. * Pad all the hints to the same column
  199. */
  200. const char *hint = (it->hint) ? it->hint : "";
  201. printf("%-s %s\n", it->command, hint);
  202. /* Second line: print help.
  203. * Argtable has a nice helper function for this which does line
  204. * wrapping.
  205. */
  206. printf(" "); // arg_print_formatted does not indent the first line
  207. arg_print_formatted(stdout, 2, 78, it->help);
  208. /* Finally, print the list of arguments */
  209. if (it->argtable) {
  210. arg_print_glossary(stdout, (void **) it->argtable, " %12s %s\n");
  211. }
  212. printf("\n");
  213. }
  214. return 0;
  215. }
  216. esp_err_t esp_console_register_help_command()
  217. {
  218. esp_console_cmd_t command = {
  219. .command = "help",
  220. .help = "Print the list of registered commands",
  221. .func = &help_command
  222. };
  223. return esp_console_cmd_register(&command);
  224. }