[PATCH 3/9] lib: term: add per-console terminal response parser

Ahmad Fatoum a.fatoum at barebox.org
Thu Apr 30 23:53:57 PDT 2026


Add term_cdev_read_reply() to parse CSI responses from the specified
console device, replacing the open-coded parsing in term_getsize().

The function collects the response into a buffer with a single 100ms
timeout, then validates and parses it using simple_strtoul, keeping
the same timeout and parsing semantics as the original code.

Also add term_cdev_drain() to discard pending input before sending
a query, preventing stale data from being parsed as a response.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 lib/term.c | 96 +++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 66 insertions(+), 30 deletions(-)

diff --git a/lib/term.c b/lib/term.c
index 78158a3036c3..96fdb0737d31 100644
--- a/lib/term.c
+++ b/lib/term.c
@@ -3,6 +3,8 @@
 
 #include <linux/kstrtox.h>
 #include <linux/minmax.h>
+#include <errno.h>
+#include <clock.h>
 #include <console.h>
 #include <term.h>
 
@@ -11,54 +13,88 @@ void term_setpos(int x, int y)
 	printf("\x1b[%d;%dH", y + 2, x + 1);
 }
 
+static void term_cdev_drain(struct console_device *cdev)
+{
+	while (cdev->tstc(cdev))
+		cdev->getc(cdev);
+}
+
+/**
+ * term_cdev_read_reply - read and parse a CSI response from a console
+ * @cdev:	console device to read from
+ * @params:	output array of parsed integer parameters
+ * @num:	number of expected parameters
+ * @end_char:	character that terminates the response (e.g. 'R')
+ *
+ * Reads a CSI response of the form ESC [ Pn ; Pn ; ... <end_char>
+ * with a single 100ms timeout for the entire response.
+ *
+ * Return: 0 on success, -ETIMEDOUT or -EINVAL on failure.
+ */
+static int term_cdev_read_reply(struct console_device *cdev,
+				int *params, int num, char end_char)
+{
+	char buf[64];
+	uint64_t start;
+	char *endp;
+	int n = 0, i;
+
+	start = get_time_ns();
+
+	while (1) {
+		if (is_timeout(start, 100 * MSECOND))
+			return -ETIMEDOUT;
+
+		if (!cdev->tstc(cdev))
+			continue;
+
+		buf[n] = cdev->getc(cdev);
+		if (buf[n] == end_char)
+			break;
+
+		if (++n >= sizeof(buf) - 1)
+			return -EINVAL;
+	}
+
+	if (n < 2 || buf[0] != '\e' || buf[1] != '[')
+		return -EINVAL;
+
+	endp = buf + 2;
+	for (i = 0; i < num; i++) {
+		params[i] = simple_strtoul(endp, &endp, 10);
+		if (i < num - 1) {
+			if (*endp != ';')
+				return -EINVAL;
+			endp++;
+		}
+	}
+
+	return 0;
+}
+
 int term_getsize(int *screenwidth, int *screenheight)
 {
-	int n;
 	int width = INT_MAX, height = INT_MAX;
 	bool found = false;
-	char *endp;
 	const char esc[] = "\e7" "\e[r" "\e[999;999H" "\e[6n";
-	char buf[64];
 
 	for_each_console(cdev) {
-		int w, h;
-		uint64_t start;
+		int w, h, params[2];
 
 		if (!(cdev->f_active & CONSOLE_STDIN))
 			continue;
 		if (!(cdev->f_active & CONSOLE_STDOUT))
 			continue;
 
-		memset(buf, 0, sizeof(buf));
+		term_cdev_drain(cdev);
 
 		console_puts(cdev, esc);
 
-		n = 0;
-
-		start = get_time_ns();
-
-		while (1) {
-			if (is_timeout(start, 100 * MSECOND))
-				break;
-
-			if (!cdev->tstc(cdev))
-				continue;
-
-			buf[n] = cdev->getc(cdev);
-
-			if (buf[n] == 'R')
-				break;
-
-			n++;
-		}
-
-		if (buf[0] != 27)
-			continue;
-		if (buf[1] != '[')
+		if (term_cdev_read_reply(cdev, params, 2, 'R'))
 			continue;
 
-		h = simple_strtoul(buf + 2, &endp, 10);
-		w = simple_strtoul(endp + 1, NULL, 10);
+		h = params[0];
+		w = params[1];
 
 		width = min(w, width);
 		height = min(h, height);
-- 
2.47.3




More information about the barebox mailing list