Skip to content

Commit

Permalink
cli: Consume invalid escape sequences early
Browse files Browse the repository at this point in the history
Unexpected 'Esc' key presses are accumulated internally, even if it is
already clear that the current escape sequence is invalid. This results
in weird behaviour. For example, the next character after 'Esc' key
simply disappears from input and 'Unknown command' is printed
after 'Enter'.

This commit fixes some issues with extra 'Esc' keys entered by user:

1. Sequence <Esc><Esc><Enter> right after autoboot stop gives:
=>
nknown command 'ry 'help'
=>
2. Sequence <Esc><p><r><i><Enter> gives:
=> ri
Unknown command 'ri' - try 'help'
=>
3. Extra 'Esc' key presses break backspace functionality.

Signed-off-by: Yurii Monakov <[email protected]>
Reviewed-by: Simon Glass <[email protected]>
  • Loading branch information
monakov-y authored and trini committed Oct 24, 2023
1 parent 5cab351 commit 2dd86b9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/cli_getch.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ static int cli_ch_esc(struct cli_ch_state *cch, int ichar,
case 1:
if (ichar == '[' || ichar == 'O')
act = ESC_SAVE;
else
act = ESC_CONVERTED;
break;
case 2:
switch (ichar) {
Expand Down
12 changes: 12 additions & 0 deletions test/common/cread.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ static int cli_ch_test(struct unit_test_state *uts)
ut_asserteq('a', cli_ch_process(cch, 'a'));
ut_asserteq(0, cli_ch_process(cch, 0));

/* unexpected 'Esc' */
ut_asserteq('a', cli_ch_process(cch, 'a'));
ut_asserteq(0, cli_ch_process(cch, '\e'));
ut_asserteq('b', cli_ch_process(cch, 'b'));
ut_asserteq(0, cli_ch_process(cch, 0));

return 0;
}
COMMON_TEST(cli_ch_test, 0);
Expand Down Expand Up @@ -80,6 +86,12 @@ static int cread_test(struct unit_test_state *uts)
ut_asserteq(7, cli_readline_into_buffer("-> ", buf, 1));
ut_asserteq_str("abc\e[Xx", buf);

/* unexpected 'Esc' */
*buf = '\0';
ut_asserteq(7, console_in_puts("abc\eXx\n"));
ut_asserteq(5, cli_readline_into_buffer("-> ", buf, 1));
ut_asserteq_str("abcXx", buf);

/* check timeout, should be between 1000 and 1050ms */
start = get_timer(0);
*buf = '\0';
Expand Down

0 comments on commit 2dd86b9

Please sign in to comment.