shell: optionally set shell root command using Kconfig

If the shell root command shall be set in the application, e.g. for
implementing a login scheme, it is an advantage to set this already
during shell init.

This is now implemented using a new Kconfig variable SHELL_CMD_ROOT.

Signed-off-by: Hans Wilmers <hans@wilmers.no>
This commit is contained in:
Hans Wilmers 2021-06-11 16:19:03 +02:00 committed by Carles Cufí
commit 414603fc9c
4 changed files with 24 additions and 6 deletions

View file

@ -203,6 +203,12 @@ config SHELL_CMDS_SELECT
This option enables select command. It can be used to set new root This option enables select command. It can be used to set new root
command. Exit to main command tree is with alt+r. command. Exit to main command tree is with alt+r.
config SHELL_CMD_ROOT
string "Set a root command at init"
help
This option sets a root command at shell init,
and when exiting to main command tree with alt+r.
config SHELL_LOG_BACKEND config SHELL_LOG_BACKEND
bool "Enable shell log backend" bool "Enable shell log backend"
depends on LOG && !LOG_MINIMAL depends on LOG && !LOG_MINIMAL

View file

@ -107,8 +107,9 @@ static inline enum shell_state state_get(const struct shell *shell)
static inline const struct shell_static_entry * static inline const struct shell_static_entry *
selected_cmd_get(const struct shell *shell) selected_cmd_get(const struct shell *shell)
{ {
if (IS_ENABLED(CONFIG_SHELL_CMDS_SELECT)) { if (IS_ENABLED(CONFIG_SHELL_CMDS_SELECT)
return shell->ctx->selected_cmd; || (CONFIG_SHELL_CMD_ROOT[0] != 0)) {
return shell->ctx->selected_cmd;
} }
return NULL; return NULL;
@ -267,7 +268,8 @@ static bool tab_prepare(const struct shell *shell,
/* terminate arguments with NULL */ /* terminate arguments with NULL */
(*argv)[*argc] = NULL; (*argv)[*argc] = NULL;
if (IS_ENABLED(CONFIG_SHELL_CMDS_SELECT) && (*argc > 0) && if ((IS_ENABLED(CONFIG_SHELL_CMDS_SELECT) || (CONFIG_SHELL_CMD_ROOT[0] != 0))
&& (*argc > 0) &&
(strcmp("select", (*argv)[0]) == 0) && (strcmp("select", (*argv)[0]) == 0) &&
!z_shell_in_select_mode(shell)) { !z_shell_in_select_mode(shell)) {
*argv = *argv + 1; *argv = *argv + 1;
@ -827,7 +829,11 @@ static void alt_metakeys_handle(const struct shell *shell, char data)
z_shell_cmd_line_erase(shell); z_shell_cmd_line_erase(shell);
z_shell_fprintf(shell, SHELL_WARNING, z_shell_fprintf(shell, SHELL_WARNING,
"Restored default root commands\n"); "Restored default root commands\n");
shell->ctx->selected_cmd = NULL; if (CONFIG_SHELL_CMD_ROOT[0]) {
shell->ctx->selected_cmd = root_cmd_find(CONFIG_SHELL_CMD_ROOT);
} else {
shell->ctx->selected_cmd = NULL;
}
z_shell_print_prompt_and_cmd(shell); z_shell_print_prompt_and_cmd(shell);
} }
} }
@ -1149,6 +1155,9 @@ static int instance_init(const struct shell *shell, const void *p_config,
memset(shell->ctx, 0, sizeof(*shell->ctx)); memset(shell->ctx, 0, sizeof(*shell->ctx));
shell->ctx->prompt = shell->default_prompt; shell->ctx->prompt = shell->default_prompt;
if (CONFIG_SHELL_CMD_ROOT[0]) {
shell->ctx->selected_cmd = root_cmd_find(CONFIG_SHELL_CMD_ROOT);
}
history_init(shell); history_init(shell);
@ -1258,7 +1267,8 @@ void shell_thread(void *shell_handle, void *arg_log_backend,
return; return;
} }
if (IS_ENABLED(CONFIG_SHELL_LOG_BACKEND) && log_backend) { if (IS_ENABLED(CONFIG_SHELL_LOG_BACKEND) && log_backend
&& !IS_ENABLED(CONFIG_SHELL_START_OBSCURED)) {
z_shell_log_backend_enable(shell->log_backend, (void *)shell, z_shell_log_backend_enable(shell->log_backend, (void *)shell,
log_level); log_level);
} }

View file

@ -229,7 +229,7 @@ static inline uint32_t shell_root_cmd_count(void)
} }
/* Function returning pointer to parent command matching requested syntax. */ /* Function returning pointer to parent command matching requested syntax. */
static const struct shell_static_entry *root_cmd_find(const char *syntax) const struct shell_static_entry *root_cmd_find(const char *syntax)
{ {
const size_t cmd_count = shell_root_cmd_count(); const size_t cmd_count = shell_root_cmd_count();
const struct shell_cmd_entry *cmd; const struct shell_cmd_entry *cmd;

View file

@ -81,6 +81,8 @@ const struct shell_static_entry *z_shell_get_last_command(
void z_shell_spaces_trim(char *str); void z_shell_spaces_trim(char *str);
void z_shell_cmd_trim(const struct shell *shell); void z_shell_cmd_trim(const struct shell *shell);
const struct shell_static_entry *root_cmd_find(const char *syntax);
static inline void z_transport_buffer_flush(const struct shell *shell) static inline void z_transport_buffer_flush(const struct shell *shell)
{ {
z_shell_fprintf_buffer_flush(shell->fprintf_ctx); z_shell_fprintf_buffer_flush(shell->fprintf_ctx);