commands.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. return ESP_ERR_INVALID_ARG;
  85. }
  86. if (strchr(cmd->command, ' ') != NULL) {
  87. return ESP_ERR_INVALID_ARG;
  88. }
  89. item->command = cmd->command;
  90. item->help = cmd->help;
  91. if (cmd->hint) {
  92. /* Prepend a space before the hint. It separates command name and
  93. * the hint. arg_print_syntax below adds this space as well.
  94. */
  95. asprintf(&item->hint, " %s", cmd->hint);
  96. } else if (cmd->argtable) {
  97. /* Generate hint based on cmd->argtable */
  98. char* buf = NULL;
  99. size_t buf_size = 0;
  100. FILE* f = open_memstream(&buf, &buf_size);
  101. if (f != NULL) {
  102. arg_print_syntax(f, cmd->argtable, NULL);
  103. fclose(f);
  104. }
  105. item->hint = buf;
  106. }
  107. item->argtable = cmd->argtable;
  108. item->func = cmd->func;
  109. cmd_item_t* last = SLIST_FIRST(&s_cmd_list);
  110. if (last == NULL) {
  111. SLIST_INSERT_HEAD(&s_cmd_list, item, next);
  112. } else {
  113. cmd_item_t* it;
  114. while ((it = SLIST_NEXT(last, next)) != NULL) {
  115. last = it;
  116. }
  117. SLIST_INSERT_AFTER(last, item, next);
  118. }
  119. return ESP_OK;
  120. }
  121. void esp_console_get_completion(const char *buf, linenoiseCompletions *lc)
  122. {
  123. size_t len = strlen(buf);
  124. if (len == 0) {
  125. return;
  126. }
  127. cmd_item_t* it;
  128. SLIST_FOREACH(it, &s_cmd_list, next) {
  129. /* Check if command starts with buf */
  130. if (strncmp(buf, it->command, len) == 0) {
  131. linenoiseAddCompletion(lc, it->command);
  132. }
  133. }
  134. }
  135. const char* esp_console_get_hint(const char *buf, int *color, int *bold)
  136. {
  137. int len = strlen(buf);
  138. cmd_item_t* it;
  139. SLIST_FOREACH(it, &s_cmd_list, next) {
  140. if (strlen(it->command) == len &&
  141. strncmp(buf, it->command, len) == 0) {
  142. *color = s_config.hint_color;
  143. *bold = s_config.hint_bold;
  144. return it->hint;
  145. }
  146. }
  147. return NULL;
  148. }
  149. static const cmd_item_t* find_command_by_name(const char* name)
  150. {
  151. const cmd_item_t* cmd = NULL;
  152. cmd_item_t* it;
  153. SLIST_FOREACH(it, &s_cmd_list, next) {
  154. if (strcmp(name, it->command) == 0) {
  155. cmd = it;
  156. break;
  157. }
  158. }
  159. return cmd;
  160. }
  161. esp_err_t esp_console_run(const char* cmdline, int* cmd_ret)
  162. {
  163. if (s_tmp_line_buf == NULL) {
  164. return ESP_ERR_INVALID_STATE;
  165. }
  166. char** argv = (char**) calloc(s_config.max_cmdline_args, sizeof(char*));
  167. if (argv == NULL) {
  168. return ESP_ERR_NO_MEM;
  169. }
  170. strlcpy(s_tmp_line_buf, cmdline, s_config.max_cmdline_length);
  171. size_t argc = esp_console_split_argv(s_tmp_line_buf, argv,
  172. s_config.max_cmdline_args);
  173. if (argc == 0) {
  174. return ESP_ERR_INVALID_ARG;
  175. }
  176. const cmd_item_t* cmd = find_command_by_name(argv[0]);
  177. if (cmd == NULL) {
  178. return ESP_ERR_NOT_FOUND;
  179. }
  180. *cmd_ret = (*cmd->func)(argc, argv);
  181. free(argv);
  182. return ESP_OK;
  183. }
  184. static int help_command(int argc, char** argv)
  185. {
  186. cmd_item_t* it;
  187. /* Print summary of each command */
  188. SLIST_FOREACH(it, &s_cmd_list, next) {
  189. if (it->help == NULL) {
  190. continue;
  191. }
  192. /* First line: command name and hint
  193. * Pad all the hints to the same column
  194. */
  195. const char* hint = (it->hint) ? it->hint : "";
  196. printf("%-s %s\n", it->command, hint);
  197. /* Second line: print help.
  198. * Argtable has a nice helper function for this which does line
  199. * wrapping.
  200. */
  201. printf(" "); // arg_print_formatted does not indent the first line
  202. arg_print_formatted(stdout, 2, 78, it->help);
  203. /* Finally, print the list of arguments */
  204. if (it->argtable) {
  205. arg_print_glossary(stdout, (void**) it->argtable, " %12s %s\n");
  206. }
  207. printf("\n");
  208. }
  209. return 0;
  210. }
  211. esp_err_t esp_console_register_help_command()
  212. {
  213. esp_console_cmd_t command = {
  214. .command = "help",
  215. .help = "Print the list of registered commands",
  216. .func = &help_command
  217. };
  218. return esp_console_cmd_register(&command);
  219. }