1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/shell: use flash_utils

This commit is contained in:
Marian Buschsieweke 2022-06-01 21:13:33 +02:00
parent c406f7ef42
commit 2825f5f2ae
No known key found for this signature in database
GPG Key ID: CB8E3238CE715A94
2 changed files with 44 additions and 14 deletions

View File

@ -26,6 +26,10 @@
#include "modules.h"
#include "xfa.h"
#ifndef __cplusplus
#include "flash_utils.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
@ -135,6 +139,20 @@ typedef struct shell_command_t {
shell_command_handler_t handler; /**< The callback function. */
} shell_command_t;
#ifndef __cplusplus
/**
* @brief A single command in the list of the supported commands.
*
* This type is used internally by the @ref SHELL_COMMAND macro.
*/
typedef struct {
FLASH_ATTR const char *name; /**< Name of the function */
FLASH_ATTR const char *desc; /**< Description to print in the "help"
* command. */
shell_command_handler_t handler; /**< The callback function. */
} shell_command_xfa_t;
#endif /* __cplusplus */
/**
* @brief Start a shell and exit once EOF is reached.
*
@ -179,9 +197,13 @@ static inline void shell_run(const shell_command_t *commands,
shell_run_forever(commands, line_buf, len);
}
#ifndef __cplusplus
/**
* @brief Define shell command
*
* @note This is not available from C++, but a trivial C file can easily
* hook up a `extern "C"` function implemented in C++.
*
* This macro is a helper for defining a shell command and adding it to the
* shell commands XFA (cross file array).
*
@ -205,10 +227,17 @@ static inline void shell_run(const shell_command_t *commands,
* SHELL_COMMAND(my_command, "my command help text", _my_command);
* ```
*/
#define SHELL_COMMAND(name, help, func) \
XFA_USE_CONST(shell_command_t*, shell_commands_xfa); \
static const shell_command_t _xfa_ ## name ## _cmd = { #name, help, &func }; \
XFA_ADD_PTR(shell_commands_xfa, name, name, &_xfa_ ## name ## _cmd)
#define SHELL_COMMAND(cmd, help, func) \
XFA_USE_CONST(shell_command_xfa_t*, shell_commands_xfa); \
static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_name[] = #cmd; \
static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_desc[] = help; \
static const shell_command_xfa_t _xfa_ ## cmd ## _cmd = { \
.name = _xfa_ ## cmd ## _cmd_name, \
.desc = _xfa_ ## cmd ## _cmd_desc, \
.handler = &func \
}; \
XFA_ADD_PTR(shell_commands_xfa, cmd, cmd, &_xfa_ ## cmd ## _cmd)
#endif /* __cplusplus */
#ifdef __cplusplus
}

View File

@ -41,7 +41,7 @@
#include "shell_lock.h"
/* define shell command cross file array */
XFA_INIT_CONST(shell_command_t*, shell_commands_xfa);
XFA_INIT_CONST(shell_command_xfa_t*, shell_commands_xfa);
#define ETX '\x03' /** ASCII "End-of-Text", or Ctrl-C */
#define EOT '\x04' /** ASCII "End-of-Transmission", or Ctrl-D */
@ -99,8 +99,8 @@ static shell_command_handler_t search_commands_xfa(char *command)
unsigned n = XFA_LEN(shell_command_t*, shell_commands_xfa);
for (unsigned i = 0; i < n; i++) {
const volatile shell_command_t *entry = shell_commands_xfa[i];
if (strcmp(entry->name, command) == 0) {
const volatile shell_command_xfa_t *entry = shell_commands_xfa[i];
if (flash_strcmp(command, entry->name) == 0) {
return entry->handler;
}
}
@ -132,17 +132,18 @@ static void print_commands(const shell_command_t *entry)
static void print_commands_xfa(void)
{
unsigned n = XFA_LEN(shell_command_t*, shell_commands_xfa);
unsigned n = XFA_LEN(shell_command_xfa_t*, shell_commands_xfa);
for (unsigned i = 0; i < n; i++) {
const volatile shell_command_t *entry = shell_commands_xfa[i];
printf("%-20s %s\n", entry->name, entry->desc);
const volatile shell_command_xfa_t *entry = shell_commands_xfa[i];
printf("%-20" PRIsflash " %" PRIsflash "\n",
entry->name, entry->desc);
}
}
static void print_help(const shell_command_t *command_list)
{
puts("Command Description"
"\n---------------------------------------");
printf("Command Description\n"
"---------------------------------------\n");
if (command_list != NULL) {
print_commands(command_list);
}
@ -294,7 +295,7 @@ static void handle_input_line(const shell_command_t *command_list, char *line)
*writepos = '\0';
if (pstate != PARSE_BLANK && pstate != PARSE_UNQUOTED) {
puts("shell: incorrect quoting");
printf("shell: incorrect quoting\n");
return;
}
@ -503,7 +504,7 @@ void shell_run_once(const shell_command_t *shell_commands,
return;
case -ENOBUFS:
puts("shell: maximum line length exceeded");
printf("shell: maximum line length exceeded\n");
break;
default: