diff --git a/sys/shell/shell.c b/sys/shell/shell.c index f7915253942d..6111a80ab1f4 100644 --- a/sys/shell/shell.c +++ b/sys/shell/shell.c @@ -33,6 +33,8 @@ #include "shell_commands.h" #define ETX '\x03' /** ASCII "End-of-Text", or ctrl-C */ +#define EOT '\x04' /** ASCII "End-of-Transmission", or ctrl-D */ + #if !defined(SHELL_NO_ECHO) || !defined(SHELL_NO_PROMPT) #ifdef MODULE_NEWLIB /* use local copy of putchar, as it seems to be inlined, @@ -237,7 +239,7 @@ static int readline(char *buf, size_t size) } int c = getchar(); - if (c < 0) { + if (c < 0 || c == EOT) { return EOF; } diff --git a/tests/shell/main.c b/tests/shell/main.c index 8f2cf7cc342c..2217fa45c0e1 100644 --- a/tests/shell/main.c +++ b/tests/shell/main.c @@ -78,6 +78,14 @@ int main(void) /* define own shell commands */ shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); + puts("shell exited (1)"); + + /* Restart the shell after the previous one exits, so that we can test + * ctrl-D exit */ + shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); + + puts("shell exited (2)"); + /* or use only system shell commands */ /* shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE); diff --git a/tests/shell/tests/01-run.py b/tests/shell/tests/01-run.py index be99c9bbab38..ea962a932b6e 100755 --- a/tests/shell/tests/01-run.py +++ b/tests/shell/tests/01-run.py @@ -50,6 +50,8 @@ ('help', EXPECTED_HELP), ('echo a string', ('\"echo\"\"a\"\"string\"')), ('ps', EXPECTED_PS), + ('reboot', ('test_shell.')), + (CONTROL_D, ('shell exited (1)')), ('garbage1234'+CONTROL_C, ('>')), # test cancelling a line ('help', EXPECTED_HELP), ('reboot', ('test_shell.'))