console: simplify examples

1. simplify console examples
2. add "quit" command
3. support console command overwrite
4. add API reference
This commit is contained in:
suda-morris
2020-02-03 18:01:04 +08:00
parent 13623ef430
commit 75cadc2e41
31 changed files with 794 additions and 1288 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2016-2017 Espressif Systems (Shanghai) PTE LTD
// Copyright 2016-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
#include <sys/param.h>
#include "esp_log.h"
#include "esp_console.h"
#include "esp_system.h"
#include "linenoise/linenoise.h"
#include "argtable3/argtable3.h"
#include "sys/queue.h"
@@ -56,6 +57,9 @@ static const cmd_item_t *find_command_by_name(const char *name);
esp_err_t esp_console_init(const esp_console_config_t *config)
{
if (!config) {
return ESP_ERR_INVALID_ARG;
}
if (s_tmp_line_buf) {
return ESP_ERR_INVALID_STATE;
}
@@ -76,8 +80,10 @@ esp_err_t esp_console_deinit(void)
return ESP_ERR_INVALID_STATE;
}
free(s_tmp_line_buf);
s_tmp_line_buf = NULL;
cmd_item_t *it, *tmp;
SLIST_FOREACH_SAFE(it, &s_cmd_list, next, tmp) {
SLIST_REMOVE(&s_cmd_list, it, cmd_item_, next);
free(it->hint);
free(it);
}
@@ -86,18 +92,25 @@ esp_err_t esp_console_deinit(void)
esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
{
cmd_item_t *item = (cmd_item_t *) calloc(1, sizeof(*item));
if (item == NULL) {
return ESP_ERR_NO_MEM;
}
if (cmd->command == NULL) {
free(item);
cmd_item_t *item = NULL;
if (!cmd || cmd->command == NULL) {
return ESP_ERR_INVALID_ARG;
}
if (strchr(cmd->command, ' ') != NULL) {
free(item);
return ESP_ERR_INVALID_ARG;
}
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);
}
item->command = cmd->command;
item->help = cmd->help;
if (cmd->hint) {
@@ -166,8 +179,10 @@ static const cmd_item_t *find_command_by_name(const char *name)
{
const cmd_item_t *cmd = NULL;
cmd_item_t *it;
int len = strlen(name);
SLIST_FOREACH(it, &s_cmd_list, next) {
if (strcmp(name, it->command) == 0) {
if (strlen(it->command) == len &&
strcmp(name, it->command) == 0) {
cmd = it;
break;
}
@@ -231,7 +246,6 @@ static int help_command(int argc, char **argv)
return 0;
}
esp_err_t esp_console_register_help_command(void)
{
esp_console_cmd_t command = {