mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 5/5] console: keep the keys typed ahead when checking for Ctrl-C
Date: Sun,  8 Nov 2026 10:32:07 +0100	[thread overview]
Message-ID: <20261108093354.1247607-6-a.fatoum@barebox.org> (raw)
In-Reply-To: <20261108093354.1247607-1-a.fatoum@barebox.org>

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@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




  parent reply	other threads:[~2026-11-08  9:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-08  9:32 [PATCH 0/5] console: escape sequence handling improvement Ahmad Fatoum
2026-11-08  9:32 ` [PATCH 1/5] readkey: wait for the rest of an escape sequence Ahmad Fatoum
2026-11-08  9:32 ` [PATCH 2/5] lib: term: ask the terminal when a console can't tell its size Ahmad Fatoum
2026-11-08  9:32 ` [PATCH 3/5] serial: virtio_console: report the console size Ahmad Fatoum
2026-11-08  9:32 ` [PATCH 4/5] console: give each console implementation its own console_ctrlc() Ahmad Fatoum
2026-11-08  9:32 ` Ahmad Fatoum [this message]
2026-11-09  9:50 ` [PATCH 0/5] console: escape sequence handling improvement Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261108093354.1247607-6-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox