fix: revert to simple ioctl-first with env var fallback

The previous logic (check ioctl result, prefer env when 80x24)
added complexity and crashes. Simple or with env vars after ioctl
is safe: ioctl returns 80x24 on stdout fd mismatch, env vars
(COLUMNS/LINES from shell) provide the correct initial size.
This commit is contained in:
2026-05-14 13:05:53 -04:00
parent abe4edaffc
commit 7813e27907
2 changed files with 42 additions and 68 deletions

View File

@@ -164,43 +164,27 @@ as a fallback when a keyword is not in *named-colors*.")
(values)) (values))
(defmethod backend-size ((b modern-backend)) (defmethod backend-size ((b modern-backend))
(multiple-value-bind (w h) (or (ignore-errors
(or (ignore-errors (let* ((+tiocgwinsz+ 21523) ; 0x5413 on Linux
(let* ((+tiocgwinsz+ 21523) ; 0x5413 on Linux (winsize (sb-alien:make-alien sb-alien:unsigned-short 4)))
(winsize (sb-alien:make-alien sb-alien:unsigned-short 4))) (unwind-protect
(unwind-protect (progn
(progn (sb-unix:unix-ioctl (sb-sys:fd-stream-fd (backend-output-stream b))
(sb-unix:unix-ioctl (sb-sys:fd-stream-fd (backend-output-stream b)) +tiocgwinsz+
+tiocgwinsz+ (sb-alien:alien-sap winsize))
(sb-alien:alien-sap winsize)) (values (sb-alien:deref winsize 1) ;; cols
(values (sb-alien:deref winsize 1) ;; cols (sb-alien:deref winsize 0))) ;; rows
(sb-alien:deref winsize 0))) ;; rows (sb-alien:free-alien winsize))))
(sb-alien:free-alien winsize)))) ;; $COLUMNS/$LINES fallback — some systems don't update the
(values 0 0)) ;; TTY size via ioctl on stdout's fd.
;; If ioctl returned a plausible size (not the 80x24 default), (ignore-errors
;; trust it — it will be correct at runtime after SIGWINCH. (let* ((cstr (sb-ext:posix-getenv "COLUMNS"))
(if (and (> w 80) (> h 24)) (rstr (sb-ext:posix-getenv "LINES"))
(values w h) (cols (when cstr (parse-integer cstr :junk-allowed t)))
;; ioctl returned 80x24 or less — suspicious. Try $COLUMNS/ (rows (when rstr (parse-integer rstr :junk-allowed t))))
;; $LINES from the shell, which reflect the true size at (when (and cols rows (> cols 0) (> rows 0))
;; process start even when ioctl on stdout's fd disagrees. (values cols rows))))
(or (ignore-errors (values 80 24)))
(let* ((cstr (sb-ext:posix-getenv "COLUMNS"))
(rstr (sb-ext:posix-getenv "LINES"))
(cols (when cstr (parse-integer cstr :junk-allowed t)))
(rows (when rstr (parse-integer rstr :junk-allowed t))))
;; Some environments set only COLUMNS or only LINES.
;; If one is missing, try the other from stty.
(when (and cols (null rows))
(let* ((out (uiop:run-program '("stty" "size")
:output :string
:ignore-error-status t))
(parts (when out (uiop:split-string (string-trim '(#\newline #\space) out)))))
(setf rows (when (and parts (= (length parts) 2))
(parse-integer (second parts) :junk-allowed t)))))
(when (and cols rows (> cols 0) (> rows 0))
(values cols rows))))
(values w h)))))
(defmethod backend-write ((b modern-backend) string) (defmethod backend-write ((b modern-backend) string)
(let ((stream (backend-output-stream b))) (let ((stream (backend-output-stream b)))

View File

@@ -27,37 +27,27 @@
(values)) (values))
(defmethod backend-size ((b simple-backend)) (defmethod backend-size ((b simple-backend))
(multiple-value-bind (w h) (or (ignore-errors
(or (ignore-errors (let* ((+tiocgwinsz+ 21523)
(let* ((+tiocgwinsz+ 21523) (winsize (sb-alien:make-alien sb-alien:unsigned-short 4)))
(winsize (sb-alien:make-alien sb-alien:unsigned-short 4))) (unwind-protect
(unwind-protect (progn
(progn (sb-unix:unix-ioctl (sb-sys:fd-stream-fd
(sb-unix:unix-ioctl (sb-sys:fd-stream-fd (backend-output-stream b))
(backend-output-stream b)) +tiocgwinsz+
+tiocgwinsz+ (sb-alien:alien-sap winsize))
(sb-alien:alien-sap winsize)) (values (sb-alien:deref winsize 1)
(values (sb-alien:deref winsize 1) (sb-alien:deref winsize 0)))
(sb-alien:deref winsize 0))) (sb-alien:free-alien winsize))))
(sb-alien:free-alien winsize)))) ;; $COLUMNS/$LINES fallback
(values 0 0)) (ignore-errors
(if (and (> w 80) (> h 24)) (let* ((cstr (sb-ext:posix-getenv "COLUMNS"))
(values w h) (rstr (sb-ext:posix-getenv "LINES"))
(or (ignore-errors (cols (when cstr (parse-integer cstr :junk-allowed t)))
(let* ((cstr (sb-ext:posix-getenv "COLUMNS")) (rows (when rstr (parse-integer rstr :junk-allowed t))))
(rstr (sb-ext:posix-getenv "LINES")) (when (and cols rows (> cols 0) (> rows 0))
(cols (when cstr (parse-integer cstr :junk-allowed t))) (values cols rows))))
(rows (when rstr (parse-integer rstr :junk-allowed t)))) (values 80 24)))
(when (and cols (null rows))
(let* ((out (uiop:run-program '("stty" "size")
:output :string
:ignore-error-status t))
(parts (when out (uiop:split-string (string-trim '(#\newline #\space) out)))))
(setf rows (when (and parts (= (length parts) 2))
(parse-integer (second parts) :junk-allowed t)))))
(when (and cols rows (> cols 0) (> rows 0))
(values cols rows))))
(values w h)))))
(defmethod backend-write ((b simple-backend) string) (defmethod backend-write ((b simple-backend) string)
(let ((stream (backend-output-stream b))) (let ((stream (backend-output-stream b)))