From 4c2deebcdf7d668afa16f4d463453972bf7a4477 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Fri, 1 Feb 2019 14:15:44 +0100 Subject: [PATCH] shell: Move internal functions from shell.c to shell_ops.c Move function for printing prompt and command with prompt to shell_ops. Signed-off-by: Krzysztof Chruscinski --- subsys/shell/shell.c | 46 ++++++---------------------------------- subsys/shell/shell_ops.c | 34 +++++++++++++++++++++++++++++ subsys/shell/shell_ops.h | 14 ++++++++++++ 3 files changed, 54 insertions(+), 40 deletions(-) diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index d52fde54c14..1bb6b502b68 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -42,40 +42,6 @@ static void cmd_buffer_clear(const struct shell *shell) shell->ctx->cmd_buff_len = 0; } -static inline void prompt_print(const struct shell *shell) -{ - /* Below cannot be printed by shell_fprinf because it will cause - * interrupt spin - */ - if (IS_ENABLED(CONFIG_SHELL_VT100_COLORS) && - shell->ctx->internal.flags.use_colors && - (SHELL_INFO != shell->ctx->vt100_ctx.col.col)) { - struct shell_vt100_colors col; - - shell_vt100_colors_store(shell, &col); - shell_vt100_color_set(shell, SHELL_INFO); - shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->prompt); - shell_vt100_colors_restore(shell, &col); - } else { - shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->prompt); - } -} - -static inline void cmd_print(const struct shell *shell) -{ - shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->ctx->cmd_buff); -} - -static void cmd_line_print(const struct shell *shell) -{ - prompt_print(shell); - - if (flag_echo_get(shell)) { - cmd_print(shell); - shell_op_cursor_position_synchronize(shell); - } -} - /** * @brief Prints error message on wrong argument count. * Optionally, printing help on wrong argument count. @@ -110,7 +76,7 @@ static void state_set(const struct shell *shell, enum shell_state state) if (state == SHELL_STATE_ACTIVE) { cmd_buffer_clear(shell); - prompt_print(shell); + shell_print_prompt_and_cmd(shell); } } @@ -224,7 +190,7 @@ static void history_handle(const struct shell *shell, bool up) shell_op_cursor_home_move(shell); clear_eos(shell); - cmd_print(shell); + shell_print_cmd(shell); shell->ctx->cmd_buff_pos = len; shell->ctx->cmd_buff_len = len; shell_op_cond_next_line(shell); @@ -478,7 +444,7 @@ static void tab_options_print(const struct shell *shell, } cursor_next_line_move(shell); - cmd_line_print(shell); + shell_print_prompt_and_cmd(shell); } static u16_t common_beginning_find(const struct shell_static_entry *cmd, @@ -840,7 +806,7 @@ static void ctrl_metakeys_handle(const struct shell *shell, char data) case SHELL_VT100_ASCII_CTRL_L: /* CTRL + L */ SHELL_VT100_CMD(shell, SHELL_VT100_CURSORHOME); SHELL_VT100_CMD(shell, SHELL_VT100_CLEARSCREEN); - cmd_line_print(shell); + shell_print_prompt_and_cmd(shell); break; case SHELL_VT100_ASCII_CTRL_U: /* CTRL + U */ @@ -1081,7 +1047,7 @@ static void shell_log_process(const struct shell *shell) struct k_poll_signal *signal = &shell->ctx->signals[SHELL_SIGNAL_RXRDY]; - cmd_line_print(shell); + shell_print_prompt_and_cmd(shell); /* Arbitrary delay added to ensure that prompt is * readable and can be used to enter further commands. @@ -1391,7 +1357,7 @@ void shell_fprintf(const struct shell *shell, enum shell_vt100_color color, va_end(args); if (k_current_get() != shell->ctx->tid) { - cmd_line_print(shell); + shell_print_prompt_and_cmd(shell); transport_buffer_flush(shell); k_mutex_unlock(&shell->ctx->wr_mtx); } diff --git a/subsys/shell/shell_ops.c b/subsys/shell/shell_ops.c index 5122e7ce0ce..f8743d55372 100644 --- a/subsys/shell/shell_ops.c +++ b/subsys/shell/shell_ops.c @@ -334,6 +334,40 @@ void shell_cmd_line_erase(const struct shell *shell) clear_eos(shell); } +static void print_prompt(const struct shell *shell) +{ + /* Below cannot be printed by shell_fprinf because it will cause + * interrupt spin + */ + if (IS_ENABLED(CONFIG_SHELL_VT100_COLORS) && + shell->ctx->internal.flags.use_colors && + (SHELL_INFO != shell->ctx->vt100_ctx.col.col)) { + struct shell_vt100_colors col; + + shell_vt100_colors_store(shell, &col); + shell_vt100_color_set(shell, SHELL_INFO); + shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->prompt); + shell_vt100_colors_restore(shell, &col); + } else { + shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->prompt); + } +} + +void shell_print_cmd(const struct shell *shell) +{ + shell_raw_fprintf(shell->fprintf_ctx, "%s", shell->ctx->cmd_buff); +} + +void shell_print_prompt_and_cmd(const struct shell *shell) +{ + print_prompt(shell); + + if (flag_echo_get(shell)) { + shell_print_cmd(shell); + shell_op_cursor_position_synchronize(shell); + } +} + static void shell_pend_on_txdone(const struct shell *shell) { if (IS_ENABLED(CONFIG_MULTITHREADING) && diff --git a/subsys/shell/shell_ops.h b/subsys/shell/shell_ops.h index 4d1d29036b1..93baaa06123 100644 --- a/subsys/shell/shell_ops.h +++ b/subsys/shell/shell_ops.h @@ -197,6 +197,20 @@ bool shell_cursor_in_empty_line(const struct shell *shell); void shell_cmd_line_erase(const struct shell *shell); +/** + * @brief Print command buffer. + * + * @param shell Shell instance. + */ +void shell_print_cmd(const struct shell *shell); + +/** + * @brief Print prompt followed by command buffer. + * + * @param shell Shell instance. + */ +void shell_print_prompt_and_cmd(const struct shell *shell); + /* Function sends data stream to the shell instance. Each time before the * shell_write function is called, it must be ensured that IO buffer of fprintf * is flushed to avoid synchronization issues.