mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-07-15 08:32:42 +08:00
feat(console): Bring console and its example from esp-idf
Commit ID: c7d0df54
This commit is contained in:
10
components/console/CMakeLists.txt
Normal file
10
components/console/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
set(COMPONENT_ADD_INCLUDEDIRS .)
|
||||
set(COMPONENT_SRCS "commands.c"
|
||||
"split_argv.c"
|
||||
"argtable3/argtable3.c"
|
||||
"linenoise/linenoise.c")
|
||||
|
||||
set(COMPONENT_REQUIRES)
|
||||
|
||||
register_component()
|
||||
|
26
components/console/argtable3/LICENSE
Normal file
26
components/console/argtable3/LICENSE
Normal file
@ -0,0 +1,26 @@
|
||||
Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
|
||||
<sheitmann@users.sourceforge.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of STEWART HEITMANN nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
4956
components/console/argtable3/argtable3.c
Normal file
4956
components/console/argtable3/argtable3.c
Normal file
File diff suppressed because it is too large
Load Diff
306
components/console/argtable3/argtable3.h
Normal file
306
components/console/argtable3/argtable3.h
Normal file
@ -0,0 +1,306 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the argtable3 library.
|
||||
*
|
||||
* Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
|
||||
* <sheitmann@users.sourceforge.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of STEWART HEITMANN nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef ARGTABLE3
|
||||
#define ARGTABLE3
|
||||
|
||||
#include <stdio.h> /* FILE */
|
||||
#include <time.h> /* struct tm */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ARG_REX_ICASE 1
|
||||
|
||||
/* bit masks for arg_hdr.flag */
|
||||
enum
|
||||
{
|
||||
ARG_TERMINATOR=0x1,
|
||||
ARG_HASVALUE=0x2,
|
||||
ARG_HASOPTVALUE=0x4
|
||||
};
|
||||
|
||||
typedef void (arg_resetfn)(void *parent);
|
||||
typedef int (arg_scanfn)(void *parent, const char *argval);
|
||||
typedef int (arg_checkfn)(void *parent);
|
||||
typedef void (arg_errorfn)(void *parent, FILE *fp, int error, const char *argval, const char *progname);
|
||||
|
||||
|
||||
/*
|
||||
* The arg_hdr struct defines properties that are common to all arg_xxx structs.
|
||||
* The argtable library requires each arg_xxx struct to have an arg_hdr
|
||||
* struct as its first data member.
|
||||
* The argtable library functions then use this data to identify the
|
||||
* properties of the command line option, such as its option tags,
|
||||
* datatype string, and glossary strings, and so on.
|
||||
* Moreover, the arg_hdr struct contains pointers to custom functions that
|
||||
* are provided by each arg_xxx struct which perform the tasks of parsing
|
||||
* that particular arg_xxx arguments, performing post-parse checks, and
|
||||
* reporting errors.
|
||||
* These functions are private to the individual arg_xxx source code
|
||||
* and are the pointer to them are initiliased by that arg_xxx struct's
|
||||
* constructor function. The user could alter them after construction
|
||||
* if desired, but the original intention is for them to be set by the
|
||||
* constructor and left unaltered.
|
||||
*/
|
||||
struct arg_hdr
|
||||
{
|
||||
char flag; /* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */
|
||||
const char *shortopts; /* String defining the short options */
|
||||
const char *longopts; /* String defiing the long options */
|
||||
const char *datatype; /* Description of the argument data type */
|
||||
const char *glossary; /* Description of the option as shown by arg_print_glossary function */
|
||||
int mincount; /* Minimum number of occurences of this option accepted */
|
||||
int maxcount; /* Maximum number of occurences if this option accepted */
|
||||
void *parent; /* Pointer to parent arg_xxx struct */
|
||||
arg_resetfn *resetfn; /* Pointer to parent arg_xxx reset function */
|
||||
arg_scanfn *scanfn; /* Pointer to parent arg_xxx scan function */
|
||||
arg_checkfn *checkfn; /* Pointer to parent arg_xxx check function */
|
||||
arg_errorfn *errorfn; /* Pointer to parent arg_xxx error function */
|
||||
void *priv; /* Pointer to private header data for use by arg_xxx functions */
|
||||
};
|
||||
|
||||
struct arg_rem
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
};
|
||||
|
||||
struct arg_lit
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
};
|
||||
|
||||
struct arg_int
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
int *ival; /* Array of parsed argument values */
|
||||
};
|
||||
|
||||
struct arg_dbl
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
double *dval; /* Array of parsed argument values */
|
||||
};
|
||||
|
||||
struct arg_str
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
const char **sval; /* Array of parsed argument values */
|
||||
};
|
||||
|
||||
struct arg_rex
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
const char **sval; /* Array of parsed argument values */
|
||||
};
|
||||
|
||||
struct arg_file
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args*/
|
||||
const char **filename; /* Array of parsed filenames (eg: /home/foo.bar) */
|
||||
const char **basename; /* Array of parsed basenames (eg: foo.bar) */
|
||||
const char **extension; /* Array of parsed extensions (eg: .bar) */
|
||||
};
|
||||
|
||||
struct arg_date
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
const char *format; /* strptime format string used to parse the date */
|
||||
int count; /* Number of matching command line args */
|
||||
struct tm *tmval; /* Array of parsed time values */
|
||||
};
|
||||
|
||||
enum {ARG_ELIMIT=1, ARG_EMALLOC, ARG_ENOMATCH, ARG_ELONGOPT, ARG_EMISSARG};
|
||||
struct arg_end
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of errors encountered */
|
||||
int *error; /* Array of error codes */
|
||||
void **parent; /* Array of pointers to offending arg_xxx struct */
|
||||
const char **argval; /* Array of pointers to offending argv[] string */
|
||||
};
|
||||
|
||||
|
||||
/**** arg_xxx constructor functions *********************************/
|
||||
|
||||
struct arg_rem* arg_rem(const char* datatype, const char* glossary);
|
||||
|
||||
struct arg_lit* arg_lit0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* glossary);
|
||||
struct arg_lit* arg_lit1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char *glossary);
|
||||
struct arg_lit* arg_litn(const char* shortopts,
|
||||
const char* longopts,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_key* arg_key0(const char* keyword,
|
||||
int flags,
|
||||
const char* glossary);
|
||||
struct arg_key* arg_key1(const char* keyword,
|
||||
int flags,
|
||||
const char* glossary);
|
||||
struct arg_key* arg_keyn(const char* keyword,
|
||||
int flags,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char* glossary);
|
||||
|
||||
struct arg_int* arg_int0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_int* arg_int1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_int* arg_intn(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char *datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_dbl* arg_dbl0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_dbl* arg_dbl1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_dbl* arg_dbln(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char *datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_str* arg_str0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_str* arg_str1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_str* arg_strn(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_rex* arg_rex0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* pattern,
|
||||
const char* datatype,
|
||||
int flags,
|
||||
const char* glossary);
|
||||
struct arg_rex* arg_rex1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* pattern,
|
||||
const char* datatype,
|
||||
int flags,
|
||||
const char *glossary);
|
||||
struct arg_rex* arg_rexn(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* pattern,
|
||||
const char* datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
int flags,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_file* arg_file0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_file* arg_file1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_file* arg_filen(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_date* arg_date0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* format,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_date* arg_date1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* format,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_date* arg_daten(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* format,
|
||||
const char* datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_end* arg_end(int maxerrors);
|
||||
|
||||
|
||||
/**** other functions *******************************************/
|
||||
int arg_nullcheck(void **argtable);
|
||||
int arg_parse(int argc, char **argv, void **argtable);
|
||||
void arg_print_option(FILE *fp, const char *shortopts, const char *longopts, const char *datatype, const char *suffix);
|
||||
void arg_print_syntax(FILE *fp, void **argtable, const char *suffix);
|
||||
void arg_print_syntaxv(FILE *fp, void **argtable, const char *suffix);
|
||||
void arg_print_glossary(FILE *fp, void **argtable, const char *format);
|
||||
void arg_print_glossary_gnu(FILE *fp, void **argtable);
|
||||
void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname);
|
||||
void arg_freetable(void **argtable, size_t n);
|
||||
void arg_print_formatted(FILE *fp, const unsigned lmargin, const unsigned rmargin, const char *text);
|
||||
|
||||
/**** deprecated functions, for back-compatibility only ********/
|
||||
void arg_free(void **argtable);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
243
components/console/commands.c
Normal file
243
components/console/commands.c
Normal file
@ -0,0 +1,243 @@
|
||||
// Copyright 2016-2017 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.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/param.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "linenoise/linenoise.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "rom/queue.h"
|
||||
|
||||
#define ANSI_COLOR_DEFAULT 39 /** Default foreground color */
|
||||
|
||||
typedef struct cmd_item_ {
|
||||
/**
|
||||
* Command name (statically allocated by application)
|
||||
*/
|
||||
const char *command;
|
||||
/**
|
||||
* Help text (statically allocated by application), may be NULL.
|
||||
*/
|
||||
const char *help;
|
||||
/**
|
||||
* Hint text, usually lists possible arguments, dynamically allocated.
|
||||
* May be NULL.
|
||||
*/
|
||||
char *hint;
|
||||
esp_console_cmd_func_t func; //!< pointer to the command handler
|
||||
void *argtable; //!< optional pointer to arg table
|
||||
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 */
|
||||
static char *s_tmp_line_buf;
|
||||
|
||||
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 (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;
|
||||
}
|
||||
|
||||
esp_err_t esp_console_deinit()
|
||||
{
|
||||
if (!s_tmp_line_buf) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
free(s_tmp_line_buf);
|
||||
cmd_item_t *it, *tmp;
|
||||
SLIST_FOREACH_SAFE(it, &s_cmd_list, next, tmp) {
|
||||
free(it->hint);
|
||||
free(it);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (strchr(cmd->command, ' ') != NULL) {
|
||||
free(item);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
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.
|
||||
*/
|
||||
int unused __attribute__((unused));
|
||||
unused = asprintf(&item->hint, " %s", cmd->hint);
|
||||
} else if (cmd->argtable) {
|
||||
/* Generate hint based on cmd->argtable */
|
||||
char *buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
FILE *f = open_memstream(&buf, &buf_size);
|
||||
if (f != NULL) {
|
||||
arg_print_syntax(f, cmd->argtable, NULL);
|
||||
fclose(f);
|
||||
}
|
||||
item->hint = buf;
|
||||
}
|
||||
item->argtable = cmd->argtable;
|
||||
item->func = cmd->func;
|
||||
cmd_item_t *last = SLIST_FIRST(&s_cmd_list);
|
||||
if (last == NULL) {
|
||||
SLIST_INSERT_HEAD(&s_cmd_list, item, next);
|
||||
} else {
|
||||
cmd_item_t *it;
|
||||
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;
|
||||
}
|
||||
cmd_item_t *it;
|
||||
SLIST_FOREACH(it, &s_cmd_list, next) {
|
||||
/* Check if command starts with buf */
|
||||
if (strncmp(buf, it->command, len) == 0) {
|
||||
linenoiseAddCompletion(lc, it->command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char *esp_console_get_hint(const char *buf, int *color, int *bold)
|
||||
{
|
||||
int len = strlen(buf);
|
||||
cmd_item_t *it;
|
||||
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;
|
||||
}
|
||||
|
||||
static const cmd_item_t *find_command_by_name(const char *name)
|
||||
{
|
||||
const cmd_item_t *cmd = NULL;
|
||||
cmd_item_t *it;
|
||||
SLIST_FOREACH(it, &s_cmd_list, next) {
|
||||
if (strcmp(name, it->command) == 0) {
|
||||
cmd = it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
esp_err_t esp_console_run(const char *cmdline, int *cmd_ret)
|
||||
{
|
||||
if (s_tmp_line_buf == NULL) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
char **argv = (char **) calloc(s_config.max_cmdline_args, sizeof(char *));
|
||||
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,
|
||||
s_config.max_cmdline_args);
|
||||
if (argc == 0) {
|
||||
free(argv);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
const cmd_item_t *cmd = find_command_by_name(argv[0]);
|
||||
if (cmd == NULL) {
|
||||
free(argv);
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
*cmd_ret = (*cmd->func)(argc, argv);
|
||||
free(argv);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static int help_command(int argc, char **argv)
|
||||
{
|
||||
cmd_item_t *it;
|
||||
|
||||
/* 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
|
||||
*/
|
||||
const char *hint = (it->hint) ? it->hint : "";
|
||||
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) {
|
||||
arg_print_glossary(stdout, (void **) it->argtable, " %12s %s\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
esp_err_t esp_console_register_help_command()
|
||||
{
|
||||
esp_console_cmd_t command = {
|
||||
.command = "help",
|
||||
.help = "Print the list of registered commands",
|
||||
.func = &help_command
|
||||
};
|
||||
return esp_console_cmd_register(&command);
|
||||
}
|
2
components/console/component.mk
Normal file
2
components/console/component.mk
Normal file
@ -0,0 +1,2 @@
|
||||
COMPONENT_ADD_INCLUDEDIRS := .
|
||||
COMPONENT_SRCDIRS := linenoise argtable3 .
|
192
components/console/esp_console.h
Normal file
192
components/console/esp_console.h
Normal file
@ -0,0 +1,192 @@
|
||||
// Copyright 2016-2017 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.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include "esp_err.h"
|
||||
|
||||
// Forward declaration. Definition in linenoise/linenoise.h.
|
||||
typedef struct linenoiseCompletions linenoiseCompletions;
|
||||
|
||||
/**
|
||||
* @brief Parameters for console initialization
|
||||
*/
|
||||
typedef struct {
|
||||
size_t max_cmdline_length; //!< length of command line buffer, in bytes
|
||||
size_t max_cmdline_args; //!< maximum number of command line arguments to parse
|
||||
int hint_color; //!< ASCII color code of hint text
|
||||
int hint_bold; //!< Set to 1 to print hint text in bold
|
||||
} esp_console_config_t;
|
||||
|
||||
/**
|
||||
* @brief initialize console module
|
||||
* Call this once before using other console module features
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_NO_MEM if out of memory
|
||||
* - ESP_ERR_INVALID_STATE if already initialized
|
||||
*/
|
||||
esp_err_t esp_console_init(const esp_console_config_t* config);
|
||||
|
||||
|
||||
/**
|
||||
* @brief de-initialize console module
|
||||
* Call this once when done using console module functions
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_INVALID_STATE if not initialized yet
|
||||
*/
|
||||
esp_err_t esp_console_deinit();
|
||||
|
||||
|
||||
/**
|
||||
* @brief Console command main function
|
||||
* @param argc number of arguments
|
||||
* @param argv array with argc entries, each pointing to a zero-terminated string argument
|
||||
* @return console command return code, 0 indicates "success"
|
||||
*/
|
||||
typedef int (*esp_console_cmd_func_t)(int argc, char** argv);
|
||||
|
||||
/**
|
||||
* @brief Console command description
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* Command name. Must not be NULL, must not contain spaces.
|
||||
* The pointer must be valid until the call to esp_console_deinit.
|
||||
*/
|
||||
const char* command; //!< command name
|
||||
/**
|
||||
* Help text for the command, shown by help command.
|
||||
* If set, the pointer must be valid until the call to esp_console_deinit.
|
||||
* If not set, the command will not be listed in 'help' output.
|
||||
*/
|
||||
const char* help;
|
||||
/**
|
||||
* Hint text, usually lists possible arguments.
|
||||
* If set to NULL, and 'argtable' field is non-NULL, hint will be generated
|
||||
* automatically
|
||||
*/
|
||||
const char* hint;
|
||||
/**
|
||||
* Pointer to a function which implements the command.
|
||||
*/
|
||||
esp_console_cmd_func_t func;
|
||||
/**
|
||||
* Array or structure of pointers to arg_xxx structures, may be NULL.
|
||||
* Used to generate hint text if 'hint' is set to NULL.
|
||||
* Array/structure which this field points to must end with an arg_end.
|
||||
* Only used for the duration of esp_console_cmd_register call.
|
||||
*/
|
||||
void* argtable;
|
||||
} esp_console_cmd_t;
|
||||
|
||||
/**
|
||||
* @brief Register console command
|
||||
* @param cmd pointer to the command description; can point to a temporary value
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_NO_MEM if out of memory
|
||||
*/
|
||||
esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd);
|
||||
|
||||
/**
|
||||
* @brief Run command line
|
||||
* @param cmdline command line (command name followed by a number of arguments)
|
||||
* @param[out] cmd_ret return code from the command (set if command was run)
|
||||
* @return
|
||||
* - ESP_OK, if command was run
|
||||
* - ESP_ERR_INVALID_ARG, if the command line is empty, or only contained
|
||||
* whitespace
|
||||
* - ESP_ERR_NOT_FOUND, if command with given name wasn't registered
|
||||
* - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
|
||||
*/
|
||||
esp_err_t esp_console_run(const char* cmdline, int* cmd_ret);
|
||||
|
||||
/**
|
||||
* @brief Split command line into arguments in place
|
||||
*
|
||||
* - This function finds whitespace-separated arguments in the given input line.
|
||||
*
|
||||
* 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]
|
||||
*
|
||||
* - Argument which include spaces may be surrounded with quotes. In this case
|
||||
* spaces are preserved and quotes are stripped.
|
||||
*
|
||||
* 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]
|
||||
*
|
||||
* - Escape sequences may be used to produce backslash, double quote, and space:
|
||||
*
|
||||
* 'a\ b\\c\"' -> [ 'a b\c"' ]
|
||||
*
|
||||
* Pointers to at most argv_size - 1 arguments are returned in argv array.
|
||||
* The pointer after the last one (i.e. argv[argc]) is set to NULL.
|
||||
*
|
||||
* @param line pointer to buffer to parse; it is modified in place
|
||||
* @param argv array where the pointers to arguments are written
|
||||
* @param argv_size number of elements in argv_array (max. number of arguments)
|
||||
* @return number of arguments found (argc)
|
||||
*/
|
||||
size_t esp_console_split_argv(char *line, char **argv, size_t argv_size);
|
||||
|
||||
/**
|
||||
* @brief Callback which provides command completion for linenoise library
|
||||
*
|
||||
* When using linenoise for line editing, command completion support
|
||||
* can be enabled like this:
|
||||
*
|
||||
* linenoiseSetCompletionCallback(&esp_console_get_completion);
|
||||
*
|
||||
* @param buf the string typed by the user
|
||||
* @param lc linenoiseCompletions to be filled in
|
||||
*/
|
||||
void esp_console_get_completion(const char *buf, linenoiseCompletions *lc);
|
||||
|
||||
/**
|
||||
* @brief Callback which provides command hints for linenoise library
|
||||
*
|
||||
* When using linenoise for line editing, hints support can be enabled as
|
||||
* follows:
|
||||
*
|
||||
* linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
|
||||
*
|
||||
* The extra cast is needed because linenoiseHintsCallback is defined as
|
||||
* returning a char* instead of const char*.
|
||||
*
|
||||
* @param buf line typed by the user
|
||||
* @param[out] color ANSI color code to be used when displaying the hint
|
||||
* @param[out] bold set to 1 if hint has to be displayed in bold
|
||||
* @return string containing the hint text. This string is persistent and should
|
||||
* not be freed (i.e. linenoiseSetFreeHintsCallback should not be used).
|
||||
*/
|
||||
const char *esp_console_get_hint(const char *buf, int *color, int *bold);
|
||||
|
||||
/**
|
||||
* @brief Register a 'help' command
|
||||
* Default 'help' command prints the list of registered commands along with
|
||||
* hints and help strings.
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
|
||||
*/
|
||||
esp_err_t esp_console_register_help_command();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
25
components/console/linenoise/LICENSE
Normal file
25
components/console/linenoise/LICENSE
Normal file
@ -0,0 +1,25 @@
|
||||
Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1116
components/console/linenoise/linenoise.c
Normal file
1116
components/console/linenoise/linenoise.c
Normal file
File diff suppressed because it is too large
Load Diff
76
components/console/linenoise/linenoise.h
Normal file
76
components/console/linenoise/linenoise.h
Normal file
@ -0,0 +1,76 @@
|
||||
/* linenoise.h -- VERSION 1.0
|
||||
*
|
||||
* Guerrilla line editing library against the idea that a line editing lib
|
||||
* needs to be 20,000 lines of C code.
|
||||
*
|
||||
* See linenoise.c for more information.
|
||||
*
|
||||
* ------------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __LINENOISE_H
|
||||
#define __LINENOISE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct linenoiseCompletions {
|
||||
size_t len;
|
||||
char **cvec;
|
||||
} linenoiseCompletions;
|
||||
|
||||
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
|
||||
typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold);
|
||||
typedef void(linenoiseFreeHintsCallback)(void *);
|
||||
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
|
||||
void linenoiseSetHintsCallback(linenoiseHintsCallback *);
|
||||
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
|
||||
void linenoiseAddCompletion(linenoiseCompletions *, const char *);
|
||||
|
||||
int linenoiseProbe(void);
|
||||
char *linenoise(const char *prompt);
|
||||
void linenoiseFree(void *ptr);
|
||||
int linenoiseHistoryAdd(const char *line);
|
||||
int linenoiseHistorySetMaxLen(int len);
|
||||
int linenoiseHistorySave(const char *filename);
|
||||
int linenoiseHistoryLoad(const char *filename);
|
||||
void linenoiseHistoryFree();
|
||||
void linenoiseClearScreen(void);
|
||||
void linenoiseSetMultiLine(int ml);
|
||||
void linenoiseSetDumbMode(int set);
|
||||
void linenoisePrintKeyCodes(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __LINENOISE_H */
|
120
components/console/split_argv.c
Normal file
120
components/console/split_argv.c
Normal file
@ -0,0 +1,120 @@
|
||||
// Copyright 2016-2017 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.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SS_FLAG_ESCAPE 0x8
|
||||
|
||||
typedef enum {
|
||||
/* parsing the space between arguments */
|
||||
SS_SPACE = 0x0,
|
||||
/* parsing an argument which isn't quoted */
|
||||
SS_ARG = 0x1,
|
||||
/* parsing a quoted argument */
|
||||
SS_QUOTED_ARG = 0x2,
|
||||
/* parsing an escape sequence within unquoted argument */
|
||||
SS_ARG_ESCAPED = SS_ARG | SS_FLAG_ESCAPE,
|
||||
/* parsing an escape sequence within a quoted argument */
|
||||
SS_QUOTED_ARG_ESCAPED = SS_QUOTED_ARG | SS_FLAG_ESCAPE,
|
||||
} split_state_t;
|
||||
|
||||
size_t esp_console_split_argv(char *line, char **argv, size_t argv_size)
|
||||
{
|
||||
const int QUOTE = '"';
|
||||
const int ESCAPE = '\\';
|
||||
const int SPACE = ' ';
|
||||
split_state_t state = SS_SPACE;
|
||||
int argc = 0;
|
||||
char *next_arg_start = line;
|
||||
char *out_ptr = line;
|
||||
for (char *in_ptr = line; argc < argv_size - 1; ++in_ptr) {
|
||||
int char_in = (unsigned char) *in_ptr;
|
||||
if (char_in == 0) {
|
||||
break;
|
||||
}
|
||||
int char_out = -1;
|
||||
|
||||
/* helper function, called when done with an argument */
|
||||
void end_arg() {
|
||||
char_out = 0;
|
||||
argv[argc++] = next_arg_start;
|
||||
state = SS_SPACE;
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case SS_SPACE:
|
||||
if (char_in == SPACE) {
|
||||
/* skip space */
|
||||
} else if (char_in == QUOTE) {
|
||||
next_arg_start = out_ptr;
|
||||
state = SS_QUOTED_ARG;
|
||||
} else if (char_in == ESCAPE) {
|
||||
next_arg_start = out_ptr;
|
||||
state = SS_ARG_ESCAPED;
|
||||
} else {
|
||||
next_arg_start = out_ptr;
|
||||
state = SS_ARG;
|
||||
char_out = char_in;
|
||||
}
|
||||
break;
|
||||
|
||||
case SS_QUOTED_ARG:
|
||||
if (char_in == QUOTE) {
|
||||
end_arg();
|
||||
} else if (char_in == ESCAPE) {
|
||||
state = SS_QUOTED_ARG_ESCAPED;
|
||||
} else {
|
||||
char_out = char_in;
|
||||
}
|
||||
break;
|
||||
|
||||
case SS_ARG_ESCAPED:
|
||||
case SS_QUOTED_ARG_ESCAPED:
|
||||
if (char_in == ESCAPE || char_in == QUOTE || char_in == SPACE) {
|
||||
char_out = char_in;
|
||||
} else {
|
||||
/* unrecognized escape character, skip */
|
||||
}
|
||||
state = (split_state_t) (state & (~SS_FLAG_ESCAPE));
|
||||
break;
|
||||
|
||||
case SS_ARG:
|
||||
if (char_in == SPACE) {
|
||||
end_arg();
|
||||
} else if (char_in == ESCAPE) {
|
||||
state = SS_ARG_ESCAPED;
|
||||
} else {
|
||||
char_out = char_in;
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* need to output anything? */
|
||||
if (char_out >= 0) {
|
||||
*out_ptr = char_out;
|
||||
++out_ptr;
|
||||
}
|
||||
}
|
||||
/* make sure the final argument is terminated */
|
||||
*out_ptr = 0;
|
||||
/* finalize the last argument */
|
||||
if (state != SS_SPACE && argc < argv_size - 1) {
|
||||
argv[argc++] = next_arg_start;
|
||||
}
|
||||
/* add a NULL at the end of argv */
|
||||
argv[argc] = NULL;
|
||||
|
||||
return argc;
|
||||
}
|
Reference in New Issue
Block a user