Skip to content

Commit

Permalink
FIXUP! cancel current line on CTRL-C.
Browse files Browse the repository at this point in the history
CTRL-C now cancels the current line instead of exiting, similar to
how getty works.

This is a suggestion of @benemorius.
  • Loading branch information
jcarrano committed Feb 7, 2019
1 parent ced8302 commit c434864
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 4 additions & 3 deletions sys/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,23 @@ static int readline(char *buf, size_t size)
}

int c = getchar();
if (c < 0 || c == '\x03' || c == '\x04') {
if (c < 0 || c == '\x04') {
return EOF;
}

/* We allow Unix linebreaks (\n), DOS linebreaks (\r\n), and Mac linebreaks (\r). */
/* QEMU transmits only a single '\r' == 13 on hitting enter ("-serial stdio"). */
/* DOS newlines are handled like hitting enter twice, but empty lines are ignored. */
if (c == '\r' || c == '\n') {
/* Ctrl-C cancels the current line. */
if (c == '\r' || c == '\n' || c == '\x03') {
*line_buf_ptr = '\0';
#ifndef SHELL_NO_ECHO
_putchar('\r');
_putchar('\n');
#endif

/* return 1 if line is empty, 0 otherwise */
return line_buf_ptr == buf;
return c == '\x03' || line_buf_ptr == buf;
}
/* QEMU uses 0x7f (DEL) as backspace, while 0x08 (BS) is for most terminals */
else if (c == 0x08 || c == 0x7f) {
Expand Down
11 changes: 8 additions & 3 deletions tests/shell/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
# literal ETX instead of SIGINT.
# When using a board (with miniterm.py) it is not a problem.

DLE = '\x16'

if os.environ['BOARD'] == 'native':
CONTROL_C = '\x16\x03'
CONTROL_C = DLE+'\x03'
CONTROL_D = DLE+'\x04'
else:
CONTROL_C = '\x03'
CONTROL_D = '\x04'

CMDS = (
('start_test', ('[TEST_START]')),
Expand All @@ -49,9 +53,10 @@
('echo a string', ('\"echo\"\"a\"\"string\"')),
('ps', EXPECTED_PS),
('reboot', ('test_shell.')),
(CONTROL_C, ('shell exited (1)')),
(CONTROL_D, ('shell exited (1)')),
('echo', ('"echo"')),
('\x04', ('shell exited (2)'))
('garbage1234'+CONTROL_C, ('>')), # test cancelling a line
('help', EXPECTED_HELP),
)


Expand Down

0 comments on commit c434864

Please sign in to comment.