Skip to content

Commit

Permalink
sys/shell: simplify array traversal code
Browse files Browse the repository at this point in the history
The code for traversing arrays of shell commands (used to print help messages
and to search for commmand handlers) was needlessly complex.

Co-authored-by: Juan Carrano <[email protected]>
  • Loading branch information
HendrikVE and jcarrano committed May 18, 2020
1 parent 62701c6 commit ee1e1ad
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 deletions sys/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@
#define BS '\x08' /** ASCII "Backspace" */
#define DEL '\x7f' /** ASCII "Delete" */

#ifdef MODULE_SHELL_COMMANDS
#define MORE_COMMANDS _shell_command_list
#else
#define MORE_COMMANDS
#endif /* MODULE_SHELL_COMMANDS */

#ifdef MODULE_NEWLIB
#define flush_if_needed() fflush(stdout)
#else
Expand All @@ -66,6 +60,12 @@
#define PROMPT_ON 0
#endif /* SHELL_NO_PROMPT */

#ifdef MODULE_SHELL_COMMANDS
#define _builtin_cmds _shell_command_list
#else
#define _builtin_cmds NULL
#endif

#define SQUOTE '\''
#define DQUOTE '"'
#define ESCAPECHAR '\\'
Expand All @@ -87,56 +87,49 @@ static enum PARSE_STATE escape_toggle(enum PARSE_STATE s)
return s ^ PARSE_ESCAPE_MASK;
}

static shell_command_handler_t find_handler(const shell_command_t *command_list, char *command)
static shell_command_handler_t search_commands(const shell_command_t *entry,
char *command)
{
const shell_command_t *command_lists[] = {
command_list,
MORE_COMMANDS
};

/* iterating over command_lists */
for (unsigned int i = 0; i < ARRAY_SIZE(command_lists); i++) {

const shell_command_t *entry;

if ((entry = command_lists[i])) {
/* iterating over commands in command_lists entry */
while (entry->name != NULL) {
if (strcmp(entry->name, command) == 0) {
return entry->handler;
}
else {
entry++;
}
}
for (; entry->name != NULL; entry++) {
if (strcmp(entry->name, command) == 0) {
return entry->handler;
}
}

return NULL;
}

static void print_help(const shell_command_t *command_list)
static shell_command_handler_t find_handler(
const shell_command_t *command_list, char *command)
{
printf("%-20s %s\n", "Command", "Description");
puts("---------------------------------------");
shell_command_handler_t handler = NULL;
if (command_list != NULL) {
handler = search_commands(command_list, command);
}

const shell_command_t *command_lists[] = {
command_list,
MORE_COMMANDS
};
if (handler == NULL && _builtin_cmds != NULL) {
handler = search_commands(_builtin_cmds, command);
}

/* iterating over command_lists */
for (unsigned int i = 0; i < ARRAY_SIZE(command_lists); i++) {
return handler;
}

const shell_command_t *entry;
static void print_commands(const shell_command_t *entry)
{
for (; entry->name != NULL; entry++) {
printf("%-20s %s\n", entry->name, entry->desc);
}
}

if ((entry = command_lists[i])) {
/* iterating over commands in command_lists entry */
while (entry->name != NULL) {
printf("%-20s %s\n", entry->name, entry->desc);
entry++;
}
}
static void print_help(const shell_command_t *command_list)
{
puts("Command Description"
"\n---------------------------------------");
if (command_list != NULL) {
print_commands(command_list);
}

if (_builtin_cmds != NULL) {
print_commands(_builtin_cmds);
}
}

Expand Down

0 comments on commit ee1e1ad

Please sign in to comment.