From eba6c5420aca15f048d3698367c6fa9b8d34fa25 Mon Sep 17 00:00:00 2001 From: Juan Carrano Date: Wed, 16 Jan 2019 15:45:21 +0100 Subject: [PATCH 1/2] sys/shell: Exit the shell on ctrl-D Right now the only way to exit the shell is if stdin is closed. This works on native, but on an embedded platform stdin is the uart and thus is never closed. This patch causes the shell loop to exit on EOT (ASCII 0x04 / ctrl-D), also called "End-of-Transmission". --- sys/shell/shell.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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; } From f56eb5bdb58aa18ac03a5336f95370bb2ed3dad7 Mon Sep 17 00:00:00 2001 From: Juan Carrano Date: Tue, 12 Feb 2019 15:08:23 +0100 Subject: [PATCH 2/2] tests/shell: Test exit with control-D. Test that the shell exits on ctrl-D and that it exits only once. --- tests/shell/main.c | 8 ++++++++ tests/shell/tests/01-run.py | 2 ++ 2 files changed, 10 insertions(+) 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.'))