fix: use return-from for env var fallback (or discards secondary values)

or in Common Lisp only preserves the primary value — secondary
values from the truthy branch are lost. return-from preserves
all values, so both cols and rows are returned correctly.
This commit is contained in:
2026-05-14 15:03:00 -04:00
parent 36fbe81441
commit b44b4b6aa0
2 changed files with 21 additions and 35 deletions

View File

@@ -162,23 +162,17 @@ as a fallback when a keyword is not in *named-colors*.")
(values))
(defmethod backend-size ((b modern-backend))
(or ;; ioctl on fd 0 (stdin) — the parent's own terminal, which IS
;; the real controlling terminal when running from a shell.
(multiple-value-bind (cols rows)
(ignore-errors
(let ((winsize (sb-alien:make-alien sb-alien:unsigned-short 4)))
(unwind-protect
(let ((ok (sb-unix:unix-ioctl 0 21523
(sb-alien:alien-sap winsize))))
(when ok
(let ((c (sb-alien:deref winsize 1))
(r (sb-alien:deref winsize 0)))
(when (and c r (> c 0) (> r 0))
(values c r)))))
(sb-alien:free-alien winsize))))
;; MY_TERM_COLS/MY_TERM_ROWS — set by the passepartout script
;; before exec sbcl. Check FIRST with return-from so both
;; values (cols and rows) are preserved (or discards secondaries).
(let ((cstr (sb-ext:posix-getenv "MY_TERM_COLS"))
(rstr (sb-ext:posix-getenv "MY_TERM_ROWS")))
(when (and cstr rstr)
(let ((cols (parse-integer cstr :junk-allowed t))
(rows (parse-integer rstr :junk-allowed t)))
(when (and cols rows (> cols 0) (> rows 0))
(values cols rows)))
;; ioctl on stdout fd — fast, correct after SIGWINCH at runtime.
(return-from backend-size (values cols rows))))))
(or
(multiple-value-bind (cols rows)
(ignore-errors
(let* ((winsize (sb-alien:make-alien sb-alien:unsigned-short 4)))
@@ -204,16 +198,7 @@ as a fallback when a keyword is not in *named-colors*.")
(let ((cols (sb-alien:deref winsize 1))
(rows (sb-alien:deref winsize 0)))
(values cols rows)))))
(sb-unix:unix-close tty-fd)))))
;; MY_TERM_COLS/MY_TERM_LINES — set by the passepartout script
;; before exec sbcl. SBCL strips COLUMNS/LINES but leaves these.
(ignore-errors
(let* ((cstr (sb-ext:posix-getenv "MY_TERM_COLS"))
(rstr (sb-ext:posix-getenv "MY_TERM_LINES"))
(cols (when cstr (parse-integer cstr :junk-allowed t)))
(rows (when rstr (parse-integer rstr :junk-allowed t))))
(when (and cols rows (> cols 0) (> rows 0))
(values cols rows))))
(sb-unix:unix-close tty-fd)))))
(values 80 24)))
(defmethod backend-write ((b modern-backend) string)