shell: add select command

The "select" command has been implemented, which allows user to
narrow down the command tree.

This implementation differs from the "select" command available
in the legacy shell. In a new implementation, if the selected
command has a handler and if the user has not entered the
registered subcommand, the shell will call the handler of selected
command and pass the text as arguments.

This may be useful, for example, if someone wants to use the
shell as an interface to a modem that supports AT commands.
Instead of each time you write e.g:
	at at+command1
	at at+command2
	at at+command3
user can execute following commands:
	select at
	at+command1
	at+command2
	at+command3

where:
at - root command for passing at commands to the modem
at+commandX - at command passed to the modem.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
This commit is contained in:
Jakub Rzeszutko 2019-05-16 11:02:53 +02:00 committed by Carles Cufí
commit b633e431a4
8 changed files with 227 additions and 96 deletions

View file

@ -40,6 +40,12 @@
#define SHELL_HELP_ECHO_OFF \
"Disable shell echo. Editing keys and meta-keys are not handled"
#define SHELL_HELP_SELECT "Selects new root command. In order for the " \
"command to be selected, it must meet the criteria:\n" \
" - it is a static command\n" \
" - it is not preceded by a dynamic command\n" \
"Return to the main command tree is done by pressing alt+r."
#define SHELL_HELP_SHELL "Useful, not Unix-like shell commands."
#define SHELL_HELP_HELP "Prints help message."
@ -374,6 +380,27 @@ static int cmd_resize(const struct shell *shell, size_t argc, char **argv)
return 0;
}
static int cmd_select(const struct shell *shell, size_t argc, char **argv)
{
const struct shell_static_entry *candidate = NULL;
struct shell_static_entry entry;
size_t matching_argc;
argc--;
argv = argv + 1;
candidate = shell_get_last_command(shell, argc, argv, &matching_argc,
&entry, true);
if ((candidate != NULL) && (argc == matching_argc)) {
shell->ctx->selected_cmd = candidate;
return 0;
}
shell_error(shell, "Cannot select command");
return -EINVAL;
}
SHELL_STATIC_SUBCMD_SET_CREATE(m_sub_colors,
SHELL_CMD_ARG(off, NULL, SHELL_HELP_COLORS_OFF, cmd_colors_off, 1, 0),
SHELL_CMD_ARG(on, NULL, SHELL_HELP_COLORS_ON, cmd_colors_on, 1, 0),
@ -425,3 +452,5 @@ SHELL_COND_CMD_ARG_REGISTER(CONFIG_SHELL_HISTORY, history, NULL,
SHELL_HELP_HISTORY, cmd_history, 1, 0);
SHELL_COND_CMD_ARG_REGISTER(CONFIG_SHELL_CMDS_RESIZE, resize, &m_sub_resize,
SHELL_HELP_RESIZE, cmd_resize, 1, 1);
SHELL_COND_CMD_ARG_REGISTER(CONFIG_SHELL_CMDS_SELECT, select, NULL,
SHELL_HELP_SELECT, cmd_select, 2, 255);