2021-05-17 01:41:32 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-08-15 08:11:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_console.h"
|
2020-02-03 10:01:04 +00:00
|
|
|
#include "esp_system.h"
|
2017-08-15 08:11:19 +00:00
|
|
|
#include "linenoise/linenoise.h"
|
|
|
|
#include "argtable3/argtable3.h"
|
2019-03-14 09:29:32 +00:00
|
|
|
#include "sys/queue.h"
|
2017-08-15 08:11:19 +00:00
|
|
|
|
|
|
|
#define ANSI_COLOR_DEFAULT 39 /** Default foreground color */
|
|
|
|
|
|
|
|
typedef struct cmd_item_ {
|
|
|
|
/**
|
|
|
|
* Command name (statically allocated by application)
|
|
|
|
*/
|
2018-04-30 08:52:45 +00:00
|
|
|
const char *command;
|
2017-08-15 08:11:19 +00:00
|
|
|
/**
|
|
|
|
* Help text (statically allocated by application), may be NULL.
|
|
|
|
*/
|
2018-04-30 08:52:45 +00:00
|
|
|
const char *help;
|
2017-08-15 08:11:19 +00:00
|
|
|
/**
|
|
|
|
* Hint text, usually lists possible arguments, dynamically allocated.
|
|
|
|
* May be NULL.
|
|
|
|
*/
|
2018-04-30 08:52:45 +00:00
|
|
|
char *hint;
|
2017-08-15 08:11:19 +00:00
|
|
|
esp_console_cmd_func_t func; //!< pointer to the command handler
|
2018-04-30 08:52:45 +00:00
|
|
|
void *argtable; //!< optional pointer to arg table
|
2017-08-15 08:11:19 +00:00
|
|
|
SLIST_ENTRY(cmd_item_) next; //!< next command in the list
|
|
|
|
} cmd_item_t;
|
|
|
|
|
|
|
|
/** linked list of command structures */
|
|
|
|
static SLIST_HEAD(cmd_list_, cmd_item_) s_cmd_list;
|
|
|
|
|
|
|
|
/** run-time configuration options */
|
|
|
|
static esp_console_config_t s_config;
|
|
|
|
|
|
|
|
/** temporary buffer used for command line parsing */
|
2018-04-30 08:52:45 +00:00
|
|
|
static char *s_tmp_line_buf;
|
2017-08-15 08:11:19 +00:00
|
|
|
|
2018-04-30 08:52:45 +00:00
|
|
|
static const cmd_item_t *find_command_by_name(const char *name);
|
2017-08-15 08:11:19 +00:00
|
|
|
|
2018-04-30 08:52:45 +00:00
|
|
|
esp_err_t esp_console_init(const esp_console_config_t *config)
|
2017-08-15 08:11:19 +00:00
|
|
|
{
|
2020-02-03 10:01:04 +00:00
|
|
|
if (!config) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2017-08-15 08:11:19 +00:00
|
|
|
if (s_tmp_line_buf) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
memcpy(&s_config, config, sizeof(s_config));
|
|
|
|
if (s_config.hint_color == 0) {
|
|
|
|
s_config.hint_color = ANSI_COLOR_DEFAULT;
|
|
|
|
}
|
|
|
|
s_tmp_line_buf = calloc(config->max_cmdline_length, 1);
|
|
|
|
if (s_tmp_line_buf == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
esp_err_t esp_console_deinit(void)
|
2017-08-15 08:11:19 +00:00
|
|
|
{
|
|
|
|
if (!s_tmp_line_buf) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
free(s_tmp_line_buf);
|
2020-02-03 10:01:04 +00:00
|
|
|
s_tmp_line_buf = NULL;
|
2017-08-15 08:11:19 +00:00
|
|
|
cmd_item_t *it, *tmp;
|
|
|
|
SLIST_FOREACH_SAFE(it, &s_cmd_list, next, tmp) {
|
2020-02-03 10:01:04 +00:00
|
|
|
SLIST_REMOVE(&s_cmd_list, it, cmd_item_, next);
|
2017-08-15 08:11:19 +00:00
|
|
|
free(it->hint);
|
|
|
|
free(it);
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
|
|
|
|
{
|
2020-02-03 10:01:04 +00:00
|
|
|
cmd_item_t *item = NULL;
|
|
|
|
if (!cmd || cmd->command == NULL) {
|
2017-08-15 08:11:19 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (strchr(cmd->command, ' ') != NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2020-02-03 10:01:04 +00:00
|
|
|
item = (cmd_item_t *)find_command_by_name(cmd->command);
|
|
|
|
if (!item) {
|
|
|
|
// not registered before
|
|
|
|
item = calloc(1, sizeof(*item));
|
|
|
|
if (item == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// remove from list and free the old hint, because we will alloc new hint for the command
|
|
|
|
SLIST_REMOVE(&s_cmd_list, item, cmd_item_, next);
|
|
|
|
free(item->hint);
|
|
|
|
}
|
2017-08-15 08:11:19 +00:00
|
|
|
item->command = cmd->command;
|
|
|
|
item->help = cmd->help;
|
|
|
|
if (cmd->hint) {
|
|
|
|
/* Prepend a space before the hint. It separates command name and
|
|
|
|
* the hint. arg_print_syntax below adds this space as well.
|
|
|
|
*/
|
2018-07-20 05:25:45 +00:00
|
|
|
int unused __attribute__((unused));
|
|
|
|
unused = asprintf(&item->hint, " %s", cmd->hint);
|
2017-08-15 08:11:19 +00:00
|
|
|
} else if (cmd->argtable) {
|
|
|
|
/* Generate hint based on cmd->argtable */
|
2018-04-30 08:52:45 +00:00
|
|
|
char *buf = NULL;
|
2017-08-22 17:07:03 +00:00
|
|
|
size_t buf_size = 0;
|
2018-04-30 08:52:45 +00:00
|
|
|
FILE *f = open_memstream(&buf, &buf_size);
|
2017-08-22 17:07:03 +00:00
|
|
|
if (f != NULL) {
|
|
|
|
arg_print_syntax(f, cmd->argtable, NULL);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2017-08-15 08:11:19 +00:00
|
|
|
item->hint = buf;
|
|
|
|
}
|
|
|
|
item->argtable = cmd->argtable;
|
|
|
|
item->func = cmd->func;
|
2018-04-30 08:52:45 +00:00
|
|
|
cmd_item_t *last = SLIST_FIRST(&s_cmd_list);
|
2017-08-15 08:11:19 +00:00
|
|
|
if (last == NULL) {
|
|
|
|
SLIST_INSERT_HEAD(&s_cmd_list, item, next);
|
|
|
|
} else {
|
2018-04-30 08:52:45 +00:00
|
|
|
cmd_item_t *it;
|
2017-08-15 08:11:19 +00:00
|
|
|
while ((it = SLIST_NEXT(last, next)) != NULL) {
|
|
|
|
last = it;
|
|
|
|
}
|
|
|
|
SLIST_INSERT_AFTER(last, item, next);
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_console_get_completion(const char *buf, linenoiseCompletions *lc)
|
|
|
|
{
|
|
|
|
size_t len = strlen(buf);
|
|
|
|
if (len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-30 08:52:45 +00:00
|
|
|
cmd_item_t *it;
|
2017-08-15 08:11:19 +00:00
|
|
|
SLIST_FOREACH(it, &s_cmd_list, next) {
|
|
|
|
/* Check if command starts with buf */
|
|
|
|
if (strncmp(buf, it->command, len) == 0) {
|
|
|
|
linenoiseAddCompletion(lc, it->command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 08:52:45 +00:00
|
|
|
const char *esp_console_get_hint(const char *buf, int *color, int *bold)
|
2017-08-15 08:11:19 +00:00
|
|
|
{
|
2020-11-17 04:48:35 +00:00
|
|
|
size_t len = strlen(buf);
|
2018-04-30 08:52:45 +00:00
|
|
|
cmd_item_t *it;
|
2017-08-15 08:11:19 +00:00
|
|
|
SLIST_FOREACH(it, &s_cmd_list, next) {
|
|
|
|
if (strlen(it->command) == len &&
|
|
|
|
strncmp(buf, it->command, len) == 0) {
|
|
|
|
*color = s_config.hint_color;
|
|
|
|
*bold = s_config.hint_bold;
|
|
|
|
return it->hint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-30 08:52:45 +00:00
|
|
|
static const cmd_item_t *find_command_by_name(const char *name)
|
2017-08-15 08:11:19 +00:00
|
|
|
{
|
2018-04-30 08:52:45 +00:00
|
|
|
const cmd_item_t *cmd = NULL;
|
|
|
|
cmd_item_t *it;
|
2020-11-17 04:48:35 +00:00
|
|
|
size_t len = strlen(name);
|
2017-08-15 08:11:19 +00:00
|
|
|
SLIST_FOREACH(it, &s_cmd_list, next) {
|
2020-02-03 10:01:04 +00:00
|
|
|
if (strlen(it->command) == len &&
|
|
|
|
strcmp(name, it->command) == 0) {
|
2017-08-15 08:11:19 +00:00
|
|
|
cmd = it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2018-04-30 08:52:45 +00:00
|
|
|
esp_err_t esp_console_run(const char *cmdline, int *cmd_ret)
|
2017-08-15 08:11:19 +00:00
|
|
|
{
|
|
|
|
if (s_tmp_line_buf == NULL) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2018-04-30 08:52:45 +00:00
|
|
|
char **argv = (char **) calloc(s_config.max_cmdline_args, sizeof(char *));
|
2017-08-15 08:11:19 +00:00
|
|
|
if (argv == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
strlcpy(s_tmp_line_buf, cmdline, s_config.max_cmdline_length);
|
|
|
|
|
|
|
|
size_t argc = esp_console_split_argv(s_tmp_line_buf, argv,
|
2018-04-30 08:52:45 +00:00
|
|
|
s_config.max_cmdline_args);
|
2017-10-12 23:12:08 +00:00
|
|
|
if (argc == 0) {
|
2018-04-30 08:52:45 +00:00
|
|
|
free(argv);
|
2017-10-12 23:12:08 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2018-04-30 08:52:45 +00:00
|
|
|
const cmd_item_t *cmd = find_command_by_name(argv[0]);
|
2017-08-15 08:11:19 +00:00
|
|
|
if (cmd == NULL) {
|
2018-04-30 08:52:45 +00:00
|
|
|
free(argv);
|
2017-08-15 08:11:19 +00:00
|
|
|
return ESP_ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
*cmd_ret = (*cmd->func)(argc, argv);
|
|
|
|
free(argv);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-30 08:52:45 +00:00
|
|
|
static int help_command(int argc, char **argv)
|
2017-08-15 08:11:19 +00:00
|
|
|
{
|
2018-04-30 08:52:45 +00:00
|
|
|
cmd_item_t *it;
|
2017-08-15 08:11:19 +00:00
|
|
|
|
|
|
|
/* Print summary of each command */
|
|
|
|
SLIST_FOREACH(it, &s_cmd_list, next) {
|
|
|
|
if (it->help == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* First line: command name and hint
|
|
|
|
* Pad all the hints to the same column
|
|
|
|
*/
|
2018-04-30 08:52:45 +00:00
|
|
|
const char *hint = (it->hint) ? it->hint : "";
|
2017-08-15 08:11:19 +00:00
|
|
|
printf("%-s %s\n", it->command, hint);
|
|
|
|
/* Second line: print help.
|
|
|
|
* Argtable has a nice helper function for this which does line
|
|
|
|
* wrapping.
|
|
|
|
*/
|
|
|
|
printf(" "); // arg_print_formatted does not indent the first line
|
|
|
|
arg_print_formatted(stdout, 2, 78, it->help);
|
|
|
|
/* Finally, print the list of arguments */
|
|
|
|
if (it->argtable) {
|
2018-04-30 08:52:45 +00:00
|
|
|
arg_print_glossary(stdout, (void **) it->argtable, " %12s %s\n");
|
2017-08-15 08:11:19 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
esp_err_t esp_console_register_help_command(void)
|
2017-08-15 08:11:19 +00:00
|
|
|
{
|
|
|
|
esp_console_cmd_t command = {
|
2018-04-30 08:52:45 +00:00
|
|
|
.command = "help",
|
|
|
|
.help = "Print the list of registered commands",
|
|
|
|
.func = &help_command
|
2017-08-15 08:11:19 +00:00
|
|
|
};
|
|
|
|
return esp_console_cmd_register(&command);
|
|
|
|
}
|