[PATCH 5/9] lib: term: fix term_getsize cursor restore

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


term_getsize() sends \e7 (DECSC) per-cdev to Save Cursor position, but
fails to use \e8 (DECRC) to Restore Cursor on each console afterwards.

Instead, it attempts to restore with term_setpos(0, 0) which uses printf()
internally and thus hits all stdout consoles, including ones that were
never queried like fbconsole.

Fix this by making cursor saving/restoring per-console by means of
DECSC/DECRC and thereby removing the term_setpos(0, 0) call entirely.

Both existent callers are safe without the term_setpos(0, 0):

- edit.c immediately clears the screen with \e[2J and positions its own
  cursor
- watch.c sends \e[H\e[J (home + clear) before calling term_getsize

Also send DECRC on parse failure to restore cursor even when the terminal
didn't respond to the position query.

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

diff --git a/lib/term.c b/lib/term.c
index 3949aeb45136..0a656d79e725 100644
--- a/lib/term.c
+++ b/lib/term.c
@@ -76,6 +76,7 @@ int term_cdev_get_size(struct console_device *cdev,
 		       int *screenwidth, int *screenheight)
 {
 	const char esc[] = "\e7" "\e[r" "\e[999;999H" "\e[6n";
+	const char restore[] = "\e8";
 	int ret, params[2];
 
 	if (!(cdev->f_active & CONSOLE_STDIN))
@@ -88,6 +89,9 @@ int term_cdev_get_size(struct console_device *cdev,
 	console_puts(cdev, esc);
 
 	ret = term_cdev_read_reply(cdev, params, 2, 'R');
+
+	console_puts(cdev, restore);
+
 	if (ret)
 		return ret;
 
@@ -113,8 +117,6 @@ int term_getsize(int *screenwidth, int *screenheight)
 		found = true;
 	}
 
-	term_setpos(0, 0);
-
 	if (!found)
 		return -ENOENT;
 
-- 
2.47.3




More information about the barebox mailing list