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 <jakub.rzeszutko@nordicsemi.no>
This commit is contained in:
Jakub Rzeszutko 2018-11-29 09:08:20 +01:00 committed by Carles Cufí
commit 9643ed6d27
2 changed files with 29 additions and 8 deletions

View file

@ -349,6 +349,7 @@ struct shell_flags {
u32_t tx_rdy :1; u32_t tx_rdy :1;
u32_t mode_delete :1; /*!< Operation mode of backspace key */ u32_t mode_delete :1; /*!< Operation mode of backspace key */
u32_t history_exit:1; /*!< Request to exit history mode */ 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)), BUILD_ASSERT_MSG((sizeof(struct shell_flags) == sizeof(u32_t)),

View file

@ -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) static void metakeys_handle(const struct shell *shell, char data)
{ {
/* Optional feature */ /* 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) static void shell_state_collect(const struct shell *shell)
{ {
size_t count = 0; size_t count = 0;
@ -777,7 +794,7 @@ static void shell_state_collect(const struct shell *shell)
switch (shell->ctx->receive_state) { switch (shell->ctx->receive_state) {
case SHELL_RECEIVE_DEFAULT: case SHELL_RECEIVE_DEFAULT:
if ((data == '\r') || (data == '\n')) { if (process_nl(shell, data)) {
if (!shell->ctx->cmd_buff_len) { if (!shell->ctx->cmd_buff_len) {
history_mode_exit(shell); history_mode_exit(shell);
cursor_next_line_move(shell); cursor_next_line_move(shell);
@ -785,10 +802,13 @@ static void shell_state_collect(const struct shell *shell)
/* Command execution */ /* Command execution */
(void)shell_execute(shell); (void)shell_execute(shell);
} }
/* Function responsible for printing prompt
* on received NL.
*/
shell_state_set(shell, SHELL_STATE_ACTIVE); shell_state_set(shell, SHELL_STATE_ACTIVE);
return; return;
} }
switch (data) { switch (data) {
case SHELL_VT100_ASCII_ESC: /* ESCAPE */ case SHELL_VT100_ASCII_ESC: /* ESCAPE */
receive_state_change(shell, SHELL_RECEIVE_ESC); receive_state_change(shell, SHELL_RECEIVE_ESC);