diff --git a/subsys/shell/Kconfig b/subsys/shell/Kconfig index fcc2e3f89aa..ca1a6aa2370 100644 --- a/subsys/shell/Kconfig +++ b/subsys/shell/Kconfig @@ -203,6 +203,12 @@ config SHELL_CMDS_SELECT This option enables select command. It can be used to set new root 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 bool "Enable shell log backend" depends on LOG && !LOG_MINIMAL diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index e58f5bd0098..c71f54e6844 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -107,8 +107,9 @@ static inline enum shell_state state_get(const struct shell *shell) static inline const struct shell_static_entry * selected_cmd_get(const struct shell *shell) { - if (IS_ENABLED(CONFIG_SHELL_CMDS_SELECT)) { - return shell->ctx->selected_cmd; + if (IS_ENABLED(CONFIG_SHELL_CMDS_SELECT) + || (CONFIG_SHELL_CMD_ROOT[0] != 0)) { + return shell->ctx->selected_cmd; } return NULL; @@ -267,7 +268,8 @@ static bool tab_prepare(const struct shell *shell, /* terminate arguments with 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) && !z_shell_in_select_mode(shell)) { *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_fprintf(shell, SHELL_WARNING, "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); } } @@ -1149,6 +1155,9 @@ static int instance_init(const struct shell *shell, const void *p_config, memset(shell->ctx, 0, sizeof(*shell->ctx)); 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); @@ -1258,7 +1267,8 @@ void shell_thread(void *shell_handle, void *arg_log_backend, 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, log_level); } diff --git a/subsys/shell/shell_utils.c b/subsys/shell/shell_utils.c index 8939ee4aa2b..132dc6e6970 100644 --- a/subsys/shell/shell_utils.c +++ b/subsys/shell/shell_utils.c @@ -229,7 +229,7 @@ static inline uint32_t shell_root_cmd_count(void) } /* 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 struct shell_cmd_entry *cmd; diff --git a/subsys/shell/shell_utils.h b/subsys/shell/shell_utils.h index b64e6cd6bb4..866ff870860 100644 --- a/subsys/shell/shell_utils.h +++ b/subsys/shell/shell_utils.h @@ -81,6 +81,8 @@ const struct shell_static_entry *z_shell_get_last_command( void z_shell_spaces_trim(char *str); 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) { z_shell_fprintf_buffer_flush(shell->fprintf_ctx);