From 9643ed6d270c607a292b133adf62871058fec1a5 Mon Sep 17 00:00:00 2001 From: Jakub Rzeszutko Date: Thu, 29 Nov 2018 09:08:20 +0100 Subject: [PATCH] shell: fix double new line print for RTT backend In case terminal sends `\r\n` on the Enter button shell will go to the new line twice and it will print prompt twice. This patch fixes it. Signed-off-by: Jakub Rzeszutko --- include/shell/shell.h | 1 + subsys/shell/shell.c | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/include/shell/shell.h b/include/shell/shell.h index 8ca31d80c0b..57f6d5f3e8a 100644 --- a/include/shell/shell.h +++ b/include/shell/shell.h @@ -349,6 +349,7 @@ struct shell_flags { u32_t tx_rdy :1; u32_t mode_delete :1; /*!< Operation mode of backspace key */ u32_t history_exit:1; /*!< Request to exit history mode */ + u32_t last_nl :8; /*!< Last received new line character */ }; BUILD_ASSERT_MSG((sizeof(struct shell_flags) == sizeof(u32_t)), diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index dfa444752d9..d551ad51cf2 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -698,12 +698,6 @@ static void shell_tab_handle(const struct shell *shell) } } -#define SHELL_ASCII_MAX_CHAR (127u) -static inline int ascii_filter(const char data) -{ - return (u8_t) data > SHELL_ASCII_MAX_CHAR ? -EINVAL : 0; -} - static void metakeys_handle(const struct shell *shell, char data) { /* Optional feature */ @@ -757,6 +751,29 @@ static void metakeys_handle(const struct shell *shell, char data) } } +/* Functions returns true if new line character shall be processed */ +static bool process_nl(const struct shell *shell, u8_t data) +{ + if ((data != '\r') && (data != '\n')) { + shell->ctx->internal.flags.last_nl = 0; + return false; + } + + if ((shell->ctx->internal.flags.last_nl == 0) || + (data == shell->ctx->internal.flags.last_nl)) { + shell->ctx->internal.flags.last_nl = data; + return true; + } + + return false; +} + +#define SHELL_ASCII_MAX_CHAR (127u) +static inline int ascii_filter(const char data) +{ + return (u8_t) data > SHELL_ASCII_MAX_CHAR ? -EINVAL : 0; +} + static void shell_state_collect(const struct shell *shell) { size_t count = 0; @@ -777,7 +794,7 @@ static void shell_state_collect(const struct shell *shell) switch (shell->ctx->receive_state) { case SHELL_RECEIVE_DEFAULT: - if ((data == '\r') || (data == '\n')) { + if (process_nl(shell, data)) { if (!shell->ctx->cmd_buff_len) { history_mode_exit(shell); cursor_next_line_move(shell); @@ -785,10 +802,13 @@ static void shell_state_collect(const struct shell *shell) /* Command execution */ (void)shell_execute(shell); } - + /* Function responsible for printing prompt + * on received NL. + */ shell_state_set(shell, SHELL_STATE_ACTIVE); return; } + switch (data) { case SHELL_VT100_ASCII_ESC: /* ESCAPE */ receive_state_change(shell, SHELL_RECEIVE_ESC);