[PATCH 5/5] console: keep the keys typed ahead when checking for Ctrl-C
Ahmad Fatoum
a.fatoum at barebox.org
Sun Nov 8 01:32:07 PST 2026
ctrlc() reads a character to see whether it is a Ctrl-C and throws it
away if it isn't. Commands like sleep call it in a loop, so whatever is
typed while they run is lost.
For the full console, move the pending input into the input FIFO
instead and look for a Ctrl-C there, leaving everything else for the
next reader. As on a tty, a Ctrl-C discards all of the queued input.
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
common/console.c | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/common/console.c b/common/console.c
index c57bb87b0114..b4ff7ca30f29 100644
--- a/common/console.c
+++ b/common/console.c
@@ -20,6 +20,7 @@
#include <module.h>
#include <sched.h>
#include <ratp_bb.h>
+#include <readkey.h>
#include <magicvar.h>
#include <globalvar.h>
#include <linux/list.h>
@@ -608,11 +609,35 @@ EXPORT_SYMBOL(pollchar);
/**
* console_ctrlc - check the console input for a Ctrl-C
*
+ * Unlike reading the input with getchar(), this keeps all other characters
+ * queued, so that keys typed while a command runs aren't lost. As on a tty,
+ * a Ctrl-C discards all queued input.
+ *
* Return: true if Ctrl-C was pressed
*/
bool console_ctrlc(void)
{
- return tstc() && getchar() == 3;
+ struct kfifo *fifo = console_input_fifo;
+ unsigned int i;
+
+ while (tstc_raw()) {
+ int c = getc_raw();
+
+ if (c < 0)
+ break;
+
+ kfifo_putc(fifo, c);
+ }
+
+ /* Check full FIFO: earlier getchar() may also have queued a Ctrl-C */
+ for (i = fifo->out; i != fifo->in; i++) {
+ if (fifo->buffer[i & (fifo->size - 1)] == CTL_CH('c')) {
+ kfifo_reset(fifo);
+ return true;
+ }
+ }
+
+ return false;
}
int console_putc(struct console_device *con, char c)
--
2.47.3
More information about the barebox
mailing list