From fa9337d73cc27ae35639cab6fbb82aa2f918d3ca Mon Sep 17 00:00:00 2001 From: Juan Carrano Date: Thu, 7 Feb 2019 12:39:11 +0100 Subject: [PATCH] sys/shell: cancel current line on CTRL-C. CTRL-C cancels the current line, similar to how getty works. This is useful if one is using a dumb terminal to communicate with a node, as it saves having to repeatedly type backspace to discard the current line. It also helps when connecting to an already running node, as one does not know what is on the line buffer, the safest thing to do is to begin by sending a ctrl-C. This is a suggestion of @benemorius. --- sys/shell/shell.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sys/shell/shell.c b/sys/shell/shell.c index 543069fdc5..f791525394 100644 --- a/sys/shell/shell.c +++ b/sys/shell/shell.c @@ -32,6 +32,7 @@ #include "shell.h" #include "shell_commands.h" +#define ETX '\x03' /** ASCII "End-of-Text", or ctrl-C */ #if !defined(SHELL_NO_ECHO) || !defined(SHELL_NO_PROMPT) #ifdef MODULE_NEWLIB /* use local copy of putchar, as it seems to be inlined, @@ -243,7 +244,8 @@ static int readline(char *buf, size_t size) /* 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 == ETX) { *line_buf_ptr = '\0'; #ifndef SHELL_NO_ECHO _putchar('\r'); @@ -251,7 +253,7 @@ static int readline(char *buf, size_t size) #endif /* return 1 if line is empty, 0 otherwise */ - return line_buf_ptr == buf; + return c == ETX || line_buf_ptr == buf; } /* QEMU uses 0x7f (DEL) as backspace, while 0x08 (BS) is for most terminals */ else if (c == 0x08 || c == 0x7f) {