From 090ef041e87b697d4b759f7dc850ff649b482db6 Mon Sep 17 00:00:00 2001 From: Jakub Rzeszutko Date: Fri, 8 Feb 2019 15:37:40 +0100 Subject: [PATCH] shell: improved shell_prompt_change function Shell will store only pointer to the prompt string instead of copying it to the RAM buffer. It will save RAM memory and it will simplify implementation of a new feature: "select" command. When a command will be selected than shell will display command syntax as a prompt. Removed obsolete ASSERT check in a static function. Signed-off-by: Jakub Rzeszutko --- include/shell/shell.h | 13 ++++----- .../shell/shell_module/prj_minimal.conf | 2 -- .../shell/shell_module/prj_minimal_rtt.conf | 1 - subsys/shell/Kconfig | 6 ----- subsys/shell/shell.c | 27 ++++++++----------- subsys/shell/shell_ops.c | 8 +++--- 6 files changed, 22 insertions(+), 35 deletions(-) diff --git a/include/shell/shell.h b/include/shell/shell.h index 67126fbf603..5edf2229945 100644 --- a/include/shell/shell.h +++ b/include/shell/shell.h @@ -401,6 +401,8 @@ enum shell_signal { * @brief Shell instance context. */ struct shell_ctx { + const char *prompt; /*!< shell current prompt. */ + enum shell_state state; /*!< Internal module state.*/ enum shell_receive_state receive_state;/*!< Escape sequence indicator.*/ @@ -447,7 +449,7 @@ enum shell_flag { * @brief Shell instance internals. */ struct shell { - char *const prompt; /*!< shell prompt. */ + const char *default_prompt; /*!< shell default prompt. */ const struct shell_transport *iface; /*!< Transport interface.*/ struct shell_ctx *ctx; /*!< Internal context.*/ @@ -475,7 +477,7 @@ extern void shell_print_stream(const void *user_ctx, const char *data, * @brief Macro for defining a shell instance. * * @param[in] _name Instance name. - * @param[in] _prompt Shell prompt string. + * @param[in] _prompt Shell default prompt string. * @param[in] _transport_iface Pointer to the transport interface. * @param[in] _log_queue_size Logger processing queue size. * @param[in] _log_timeout Logger thread timeout in milliseconds on full @@ -488,7 +490,6 @@ extern void shell_print_stream(const void *user_ctx, const char *data, _log_queue_size, _log_timeout, _shell_flag) \ static const struct shell _name; \ static struct shell_ctx UTIL_CAT(_name, _ctx); \ - static char _name##prompt[CONFIG_SHELL_PROMPT_LENGTH + 1] = _prompt; \ static u8_t _name##_out_buffer[CONFIG_SHELL_PRINTF_BUFF_SIZE]; \ SHELL_LOG_BACKEND_DEFINE(_name, _name##_out_buffer, \ CONFIG_SHELL_PRINTF_BUFF_SIZE, \ @@ -502,7 +503,7 @@ extern void shell_print_stream(const void *user_ctx, const char *data, static K_THREAD_STACK_DEFINE(_name##_stack, CONFIG_SHELL_STACK_SIZE); \ static struct k_thread _name##_thread; \ static const struct shell _name = { \ - .prompt = _name##prompt, \ + .default_prompt = _prompt, \ .iface = _transport_iface, \ .ctx = &UTIL_CAT(_name, _ctx), \ .history = SHELL_HISTORY_PTR(_name), \ @@ -660,9 +661,9 @@ void shell_process(const struct shell *shell); * @param[in] prompt New shell prompt. * * @return 0 Success. - * @return -ENOMEM New prompt is too long. + * @return -EINVAL Pointer to new prompt is not correct. */ -int shell_prompt_change(const struct shell *shell, char *prompt); +int shell_prompt_change(const struct shell *shell, const char *prompt); /** * @brief Prints the current command help. diff --git a/samples/subsys/shell/shell_module/prj_minimal.conf b/samples/subsys/shell/shell_module/prj_minimal.conf index 3c2a91c1741..3fe1e3fc790 100644 --- a/samples/subsys/shell/shell_module/prj_minimal.conf +++ b/samples/subsys/shell/shell_module/prj_minimal.conf @@ -13,9 +13,7 @@ CONFIG_SHELL_STACK_SIZE=1024 CONFIG_SHELL_CMD_BUFF_SIZE=128 CONFIG_SHELL_WILDCARD=n CONFIG_SHELL_DYNAMIC_CMDS=n -CONFIG_SHELL_PROMPT_LENGTH=10 CONFIG_SHELL_VT100_COLORS=n CONFIG_SHELL_HELP_ON_WRONG_ARGUMENT_COUNT=n CONFIG_SHELL_STATS=n CONFIG_SHELL_CMDS=n - diff --git a/samples/subsys/shell/shell_module/prj_minimal_rtt.conf b/samples/subsys/shell/shell_module/prj_minimal_rtt.conf index 5ca29d0d3cb..9ff4f02b15e 100644 --- a/samples/subsys/shell/shell_module/prj_minimal_rtt.conf +++ b/samples/subsys/shell/shell_module/prj_minimal_rtt.conf @@ -13,7 +13,6 @@ CONFIG_SHELL_STACK_SIZE=1024 CONFIG_SHELL_CMD_BUFF_SIZE=128 CONFIG_SHELL_WILDCARD=n CONFIG_SHELL_DYNAMIC_CMDS=n -CONFIG_SHELL_PROMPT_LENGTH=10 CONFIG_SHELL_VT100_COLORS=n CONFIG_SHELL_HELP_ON_WRONG_ARGUMENT_COUNT=n CONFIG_SHELL_STATS=n diff --git a/subsys/shell/Kconfig b/subsys/shell/Kconfig index 25452df5912..53327560adb 100644 --- a/subsys/shell/Kconfig +++ b/subsys/shell/Kconfig @@ -37,12 +37,6 @@ config SHELL_BACKSPACE_MODE_DELETE Some terminals send code: 0x08 (backspace) other 0x7F (delete). When this option is set shell will expect 0x7F for backspace key. -config SHELL_PROMPT_LENGTH - int "Maximum prompt length" - default 16 - help - Maximum length of prompt name in bytes. - config SHELL_CMD_BUFF_SIZE int "Shell command buffer size" default 256 diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index 7f5e9780543..604c9942d75 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -1069,8 +1069,6 @@ static void shell_log_process(const struct shell *shell) static int instance_init(const struct shell *shell, const void *p_config, bool use_colors) { - __ASSERT_NO_MSG(shell); - __ASSERT_NO_MSG(shell->ctx && shell->iface && shell->prompt); __ASSERT_NO_MSG((shell->shell_flag == SHELL_FLAG_CRLF_DEFAULT) || (shell->shell_flag == SHELL_FLAG_OLF_CRLF)); @@ -1082,6 +1080,7 @@ 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; history_init(shell); @@ -1098,7 +1097,7 @@ static int instance_init(const struct shell *shell, const void *p_config, shell->ctx->state = SHELL_STATE_INITIALIZED; shell->ctx->vt100_ctx.cons.terminal_wid = SHELL_DEFAULT_TERMINAL_WIDTH; shell->ctx->vt100_ctx.cons.terminal_hei = SHELL_DEFAULT_TERMINAL_HEIGHT; - shell->ctx->vt100_ctx.cons.name_len = shell_strlen(shell->prompt); + shell->ctx->vt100_ctx.cons.name_len = shell_strlen(shell->ctx->prompt); flag_use_colors_set(shell, IS_ENABLED(CONFIG_SHELL_VT100_COLORS)); return 0; @@ -1107,7 +1106,7 @@ static int instance_init(const struct shell *shell, const void *p_config, static int instance_uninit(const struct shell *shell) { __ASSERT_NO_MSG(shell); - __ASSERT_NO_MSG(shell->ctx && shell->iface && shell->prompt); + __ASSERT_NO_MSG(shell->ctx && shell->iface); int err; @@ -1219,7 +1218,7 @@ int shell_init(const struct shell *shell, const void *transport_config, bool use_colors, bool log_backend, u32_t init_log_level) { __ASSERT_NO_MSG(shell); - __ASSERT_NO_MSG(shell->ctx && shell->iface && shell->prompt); + __ASSERT_NO_MSG(shell->ctx && shell->iface && shell->default_prompt); int err = instance_init(shell, transport_config, use_colors); @@ -1259,7 +1258,7 @@ int shell_uninit(const struct shell *shell) int shell_start(const struct shell *shell) { __ASSERT_NO_MSG(shell); - __ASSERT_NO_MSG(shell->ctx && shell->iface && shell->prompt); + __ASSERT_NO_MSG(shell->ctx && shell->iface && shell->default_prompt); if (shell->ctx->state != SHELL_STATE_INITIALIZED) { return -ENOTSUP; @@ -1367,20 +1366,16 @@ void shell_fprintf(const struct shell *shell, enum shell_vt100_color color, } } -int shell_prompt_change(const struct shell *shell, char *prompt) +int shell_prompt_change(const struct shell *shell, const char *prompt) { - u16_t len; - __ASSERT_NO_MSG(shell); - __ASSERT_NO_MSG(prompt); - len = shell_strlen(prompt); - - if (len <= CONFIG_SHELL_PROMPT_LENGTH) { - memcpy(shell->prompt, prompt, len + 1); /* +1 for '\0' */ - return 0; + if (prompt == NULL) { + return -EINVAL; } - return -ENOMEM; + shell->ctx->prompt = prompt; + + return 0; } void shell_help(const struct shell *shell) diff --git a/subsys/shell/shell_ops.c b/subsys/shell/shell_ops.c index f8743d55372..181bc7af03d 100644 --- a/subsys/shell/shell_ops.c +++ b/subsys/shell/shell_ops.c @@ -30,14 +30,14 @@ void shell_op_cursor_horiz_move(const struct shell *shell, s32_t delta) */ static inline bool full_line_cmd(const struct shell *shell) { - return ((shell->ctx->cmd_buff_len + shell_strlen(shell->prompt)) + return ((shell->ctx->cmd_buff_len + shell_strlen(shell->ctx->prompt)) % shell->ctx->vt100_ctx.cons.terminal_wid == 0); } /* Function returns true if cursor is at beginning of an empty line. */ bool shell_cursor_in_empty_line(const struct shell *shell) { - return ((shell->ctx->cmd_buff_pos + shell_strlen(shell->prompt)) + return ((shell->ctx->cmd_buff_pos + shell_strlen(shell->ctx->prompt)) % shell->ctx->vt100_ctx.cons.terminal_wid == 0); } @@ -346,10 +346,10 @@ static void print_prompt(const struct shell *shell) shell_vt100_colors_store(shell, &col); shell_vt100_color_set(shell, SHELL_INFO); - shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->prompt); + shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->ctx->prompt); shell_vt100_colors_restore(shell, &col); } else { - shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->prompt); + shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->ctx->prompt); } }