1 // Copyright 2016-2017 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/param.h>
20 #include "esp_console.h"
21 #include "linenoise/linenoise.h"
22 #include "argtable3/argtable3.h"
23 #include "rom/queue.h"
25 #define ANSI_COLOR_DEFAULT 39 /** Default foreground color */
27 typedef struct cmd_item_ {
29 * Command name (statically allocated by application)
33 * Help text (statically allocated by application), may be NULL.
37 * Hint text, usually lists possible arguments, dynamically allocated.
41 esp_console_cmd_func_t func; //!< pointer to the command handler
42 void* argtable; //!< optional pointer to arg table
43 SLIST_ENTRY(cmd_item_) next; //!< next command in the list
46 /** linked list of command structures */
47 static SLIST_HEAD(cmd_list_, cmd_item_) s_cmd_list;
49 /** run-time configuration options */
50 static esp_console_config_t s_config;
52 /** temporary buffer used for command line parsing */
53 static char* s_tmp_line_buf;
55 static const cmd_item_t* find_command_by_name(const char* name);
57 esp_err_t esp_console_init(const esp_console_config_t* config)
60 return ESP_ERR_INVALID_STATE;
62 memcpy(&s_config, config, sizeof(s_config));
63 if (s_config.hint_color == 0) {
64 s_config.hint_color = ANSI_COLOR_DEFAULT;
66 s_tmp_line_buf = calloc(config->max_cmdline_length, 1);
67 if (s_tmp_line_buf == NULL) {
68 return ESP_ERR_NO_MEM;
73 esp_err_t esp_console_deinit()
75 if (!s_tmp_line_buf) {
76 return ESP_ERR_INVALID_STATE;
80 SLIST_FOREACH_SAFE(it, &s_cmd_list, next, tmp) {
87 esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
89 cmd_item_t *item = (cmd_item_t *) calloc(1, sizeof(*item));
91 return ESP_ERR_NO_MEM;
93 if (cmd->command == NULL) {
94 return ESP_ERR_INVALID_ARG;
96 if (strchr(cmd->command, ' ') != NULL) {
97 return ESP_ERR_INVALID_ARG;
99 item->command = cmd->command;
100 item->help = cmd->help;
102 /* Prepend a space before the hint. It separates command name and
103 * the hint. arg_print_syntax below adds this space as well.
105 asprintf(&item->hint, " %s", cmd->hint);
106 } else if (cmd->argtable) {
107 /* Generate hint based on cmd->argtable */
110 FILE* f = open_memstream(&buf, &buf_size);
112 arg_print_syntax(f, cmd->argtable, NULL);
117 item->argtable = cmd->argtable;
118 item->func = cmd->func;
119 cmd_item_t* last = SLIST_FIRST(&s_cmd_list);
121 SLIST_INSERT_HEAD(&s_cmd_list, item, next);
124 while ((it = SLIST_NEXT(last, next)) != NULL) {
127 SLIST_INSERT_AFTER(last, item, next);
132 void esp_console_get_completion(const char *buf, linenoiseCompletions *lc)
134 size_t len = strlen(buf);
139 SLIST_FOREACH(it, &s_cmd_list, next) {
140 /* Check if command starts with buf */
141 if (strncmp(buf, it->command, len) == 0) {
142 linenoiseAddCompletion(lc, it->command);
147 const char* esp_console_get_hint(const char *buf, int *color, int *bold)
149 int len = strlen(buf);
151 SLIST_FOREACH(it, &s_cmd_list, next) {
152 if (strlen(it->command) == len &&
153 strncmp(buf, it->command, len) == 0) {
154 *color = s_config.hint_color;
155 *bold = s_config.hint_bold;
162 static const cmd_item_t* find_command_by_name(const char* name)
164 const cmd_item_t* cmd = NULL;
166 SLIST_FOREACH(it, &s_cmd_list, next) {
167 if (strcmp(name, it->command) == 0) {
175 esp_err_t esp_console_run(const char* cmdline, int* cmd_ret)
177 if (s_tmp_line_buf == NULL) {
178 return ESP_ERR_INVALID_STATE;
180 char** argv = (char**) calloc(s_config.max_cmdline_args, sizeof(char*));
182 return ESP_ERR_NO_MEM;
184 strlcpy(s_tmp_line_buf, cmdline, s_config.max_cmdline_length);
186 size_t argc = esp_console_split_argv(s_tmp_line_buf, argv,
187 s_config.max_cmdline_args);
189 const cmd_item_t* cmd = find_command_by_name(argv[0]);
191 return ESP_ERR_NOT_FOUND;
193 *cmd_ret = (*cmd->func)(argc, argv);
198 static int help_command(int argc, char** argv)
202 /* Print summary of each command */
203 SLIST_FOREACH(it, &s_cmd_list, next) {
204 if (it->help == NULL) {
207 /* First line: command name and hint
208 * Pad all the hints to the same column
210 const char* hint = (it->hint) ? it->hint : "";
211 printf("%-s %s\n", it->command, hint);
212 /* Second line: print help.
213 * Argtable has a nice helper function for this which does line
216 printf(" "); // arg_print_formatted does not indent the first line
217 arg_print_formatted(stdout, 2, 78, it->help);
218 /* Finally, print the list of arguments */
220 arg_print_glossary(stdout, (void**) it->argtable, " %12s %s\n");
228 esp_err_t esp_console_register_help_command()
230 esp_console_cmd_t command = {
232 .help = "Print the list of registered commands",
233 .func = &help_command
235 return esp_console_cmd_register(&command);