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 <jakub.rzeszutko@nordicsemi.no>
This commit is contained in:
Jakub Rzeszutko 2019-02-08 15:37:40 +01:00 committed by Anas Nashif
commit 090ef041e8
6 changed files with 22 additions and 35 deletions

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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);
}
}