console: Fix formatting
This commit is contained in:
parent
b3894dba97
commit
87adbd3229
1 changed files with 32 additions and 32 deletions
|
@ -28,18 +28,18 @@ typedef struct cmd_item_ {
|
||||||
/**
|
/**
|
||||||
* Command name (statically allocated by application)
|
* Command name (statically allocated by application)
|
||||||
*/
|
*/
|
||||||
const char* command;
|
const char *command;
|
||||||
/**
|
/**
|
||||||
* Help text (statically allocated by application), may be NULL.
|
* Help text (statically allocated by application), may be NULL.
|
||||||
*/
|
*/
|
||||||
const char* help;
|
const char *help;
|
||||||
/**
|
/**
|
||||||
* Hint text, usually lists possible arguments, dynamically allocated.
|
* Hint text, usually lists possible arguments, dynamically allocated.
|
||||||
* May be NULL.
|
* May be NULL.
|
||||||
*/
|
*/
|
||||||
char* hint;
|
char *hint;
|
||||||
esp_console_cmd_func_t func; //!< pointer to the command handler
|
esp_console_cmd_func_t func; //!< pointer to the command handler
|
||||||
void* argtable; //!< optional pointer to arg table
|
void *argtable; //!< optional pointer to arg table
|
||||||
SLIST_ENTRY(cmd_item_) next; //!< next command in the list
|
SLIST_ENTRY(cmd_item_) next; //!< next command in the list
|
||||||
} cmd_item_t;
|
} cmd_item_t;
|
||||||
|
|
||||||
|
@ -50,11 +50,11 @@ static SLIST_HEAD(cmd_list_, cmd_item_) s_cmd_list;
|
||||||
static esp_console_config_t s_config;
|
static esp_console_config_t s_config;
|
||||||
|
|
||||||
/** temporary buffer used for command line parsing */
|
/** temporary buffer used for command line parsing */
|
||||||
static char* s_tmp_line_buf;
|
static char *s_tmp_line_buf;
|
||||||
|
|
||||||
static const cmd_item_t* find_command_by_name(const char* name);
|
static const cmd_item_t *find_command_by_name(const char *name);
|
||||||
|
|
||||||
esp_err_t esp_console_init(const esp_console_config_t* config)
|
esp_err_t esp_console_init(const esp_console_config_t *config)
|
||||||
{
|
{
|
||||||
if (s_tmp_line_buf) {
|
if (s_tmp_line_buf) {
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_ERR_INVALID_STATE;
|
||||||
|
@ -91,11 +91,11 @@ esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
if (cmd->command == NULL) {
|
if (cmd->command == NULL) {
|
||||||
free(item);
|
free(item);
|
||||||
return ESP_ERR_INVALID_ARG;
|
return ESP_ERR_INVALID_ARG;
|
||||||
}
|
}
|
||||||
if (strchr(cmd->command, ' ') != NULL) {
|
if (strchr(cmd->command, ' ') != NULL) {
|
||||||
free(item);
|
free(item);
|
||||||
return ESP_ERR_INVALID_ARG;
|
return ESP_ERR_INVALID_ARG;
|
||||||
}
|
}
|
||||||
item->command = cmd->command;
|
item->command = cmd->command;
|
||||||
|
@ -107,9 +107,9 @@ esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
|
||||||
asprintf(&item->hint, " %s", cmd->hint);
|
asprintf(&item->hint, " %s", cmd->hint);
|
||||||
} else if (cmd->argtable) {
|
} else if (cmd->argtable) {
|
||||||
/* Generate hint based on cmd->argtable */
|
/* Generate hint based on cmd->argtable */
|
||||||
char* buf = NULL;
|
char *buf = NULL;
|
||||||
size_t buf_size = 0;
|
size_t buf_size = 0;
|
||||||
FILE* f = open_memstream(&buf, &buf_size);
|
FILE *f = open_memstream(&buf, &buf_size);
|
||||||
if (f != NULL) {
|
if (f != NULL) {
|
||||||
arg_print_syntax(f, cmd->argtable, NULL);
|
arg_print_syntax(f, cmd->argtable, NULL);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
@ -118,11 +118,11 @@ esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
|
||||||
}
|
}
|
||||||
item->argtable = cmd->argtable;
|
item->argtable = cmd->argtable;
|
||||||
item->func = cmd->func;
|
item->func = cmd->func;
|
||||||
cmd_item_t* last = SLIST_FIRST(&s_cmd_list);
|
cmd_item_t *last = SLIST_FIRST(&s_cmd_list);
|
||||||
if (last == NULL) {
|
if (last == NULL) {
|
||||||
SLIST_INSERT_HEAD(&s_cmd_list, item, next);
|
SLIST_INSERT_HEAD(&s_cmd_list, item, next);
|
||||||
} else {
|
} else {
|
||||||
cmd_item_t* it;
|
cmd_item_t *it;
|
||||||
while ((it = SLIST_NEXT(last, next)) != NULL) {
|
while ((it = SLIST_NEXT(last, next)) != NULL) {
|
||||||
last = it;
|
last = it;
|
||||||
}
|
}
|
||||||
|
@ -137,7 +137,7 @@ void esp_console_get_completion(const char *buf, linenoiseCompletions *lc)
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cmd_item_t* it;
|
cmd_item_t *it;
|
||||||
SLIST_FOREACH(it, &s_cmd_list, next) {
|
SLIST_FOREACH(it, &s_cmd_list, next) {
|
||||||
/* Check if command starts with buf */
|
/* Check if command starts with buf */
|
||||||
if (strncmp(buf, it->command, len) == 0) {
|
if (strncmp(buf, it->command, len) == 0) {
|
||||||
|
@ -146,10 +146,10 @@ void esp_console_get_completion(const char *buf, linenoiseCompletions *lc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* esp_console_get_hint(const char *buf, int *color, int *bold)
|
const char *esp_console_get_hint(const char *buf, int *color, int *bold)
|
||||||
{
|
{
|
||||||
int len = strlen(buf);
|
int len = strlen(buf);
|
||||||
cmd_item_t* it;
|
cmd_item_t *it;
|
||||||
SLIST_FOREACH(it, &s_cmd_list, next) {
|
SLIST_FOREACH(it, &s_cmd_list, next) {
|
||||||
if (strlen(it->command) == len &&
|
if (strlen(it->command) == len &&
|
||||||
strncmp(buf, it->command, len) == 0) {
|
strncmp(buf, it->command, len) == 0) {
|
||||||
|
@ -161,10 +161,10 @@ const char* esp_console_get_hint(const char *buf, int *color, int *bold)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const cmd_item_t* find_command_by_name(const char* name)
|
static const cmd_item_t *find_command_by_name(const char *name)
|
||||||
{
|
{
|
||||||
const cmd_item_t* cmd = NULL;
|
const cmd_item_t *cmd = NULL;
|
||||||
cmd_item_t* it;
|
cmd_item_t *it;
|
||||||
SLIST_FOREACH(it, &s_cmd_list, next) {
|
SLIST_FOREACH(it, &s_cmd_list, next) {
|
||||||
if (strcmp(name, it->command) == 0) {
|
if (strcmp(name, it->command) == 0) {
|
||||||
cmd = it;
|
cmd = it;
|
||||||
|
@ -174,26 +174,26 @@ static const cmd_item_t* find_command_by_name(const char* name)
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t esp_console_run(const char* cmdline, int* cmd_ret)
|
esp_err_t esp_console_run(const char *cmdline, int *cmd_ret)
|
||||||
{
|
{
|
||||||
if (s_tmp_line_buf == NULL) {
|
if (s_tmp_line_buf == NULL) {
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_ERR_INVALID_STATE;
|
||||||
}
|
}
|
||||||
char** argv = (char**) calloc(s_config.max_cmdline_args, sizeof(char*));
|
char **argv = (char **) calloc(s_config.max_cmdline_args, sizeof(char *));
|
||||||
if (argv == NULL) {
|
if (argv == NULL) {
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
strlcpy(s_tmp_line_buf, cmdline, s_config.max_cmdline_length);
|
strlcpy(s_tmp_line_buf, cmdline, s_config.max_cmdline_length);
|
||||||
|
|
||||||
size_t argc = esp_console_split_argv(s_tmp_line_buf, argv,
|
size_t argc = esp_console_split_argv(s_tmp_line_buf, argv,
|
||||||
s_config.max_cmdline_args);
|
s_config.max_cmdline_args);
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
free(argv);
|
free(argv);
|
||||||
return ESP_ERR_INVALID_ARG;
|
return ESP_ERR_INVALID_ARG;
|
||||||
}
|
}
|
||||||
const cmd_item_t* cmd = find_command_by_name(argv[0]);
|
const cmd_item_t *cmd = find_command_by_name(argv[0]);
|
||||||
if (cmd == NULL) {
|
if (cmd == NULL) {
|
||||||
free(argv);
|
free(argv);
|
||||||
return ESP_ERR_NOT_FOUND;
|
return ESP_ERR_NOT_FOUND;
|
||||||
}
|
}
|
||||||
*cmd_ret = (*cmd->func)(argc, argv);
|
*cmd_ret = (*cmd->func)(argc, argv);
|
||||||
|
@ -201,9 +201,9 @@ esp_err_t esp_console_run(const char* cmdline, int* cmd_ret)
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int help_command(int argc, char** argv)
|
static int help_command(int argc, char **argv)
|
||||||
{
|
{
|
||||||
cmd_item_t* it;
|
cmd_item_t *it;
|
||||||
|
|
||||||
/* Print summary of each command */
|
/* Print summary of each command */
|
||||||
SLIST_FOREACH(it, &s_cmd_list, next) {
|
SLIST_FOREACH(it, &s_cmd_list, next) {
|
||||||
|
@ -213,7 +213,7 @@ static int help_command(int argc, char** argv)
|
||||||
/* First line: command name and hint
|
/* First line: command name and hint
|
||||||
* Pad all the hints to the same column
|
* Pad all the hints to the same column
|
||||||
*/
|
*/
|
||||||
const char* hint = (it->hint) ? it->hint : "";
|
const char *hint = (it->hint) ? it->hint : "";
|
||||||
printf("%-s %s\n", it->command, hint);
|
printf("%-s %s\n", it->command, hint);
|
||||||
/* Second line: print help.
|
/* Second line: print help.
|
||||||
* Argtable has a nice helper function for this which does line
|
* Argtable has a nice helper function for this which does line
|
||||||
|
@ -223,7 +223,7 @@ static int help_command(int argc, char** argv)
|
||||||
arg_print_formatted(stdout, 2, 78, it->help);
|
arg_print_formatted(stdout, 2, 78, it->help);
|
||||||
/* Finally, print the list of arguments */
|
/* Finally, print the list of arguments */
|
||||||
if (it->argtable) {
|
if (it->argtable) {
|
||||||
arg_print_glossary(stdout, (void**) it->argtable, " %12s %s\n");
|
arg_print_glossary(stdout, (void **) it->argtable, " %12s %s\n");
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
@ -234,9 +234,9 @@ static int help_command(int argc, char** argv)
|
||||||
esp_err_t esp_console_register_help_command()
|
esp_err_t esp_console_register_help_command()
|
||||||
{
|
{
|
||||||
esp_console_cmd_t command = {
|
esp_console_cmd_t command = {
|
||||||
.command = "help",
|
.command = "help",
|
||||||
.help = "Print the list of registered commands",
|
.help = "Print the list of registered commands",
|
||||||
.func = &help_command
|
.func = &help_command
|
||||||
};
|
};
|
||||||
return esp_console_cmd_register(&command);
|
return esp_console_cmd_register(&command);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue