docs: sync .org with implementation for backend-size, read-raw-byte, SIGWINCH
backend-protocol.org / simple.lisp: - Replace hard-coded 80x24 prose with full 5-step fallback chain (MY_TERM env vars → ioctl fd 0 → ioctl stdout → /dev/tty → 80x24) - Document return-from pattern (or discards secondary values) modern-backend.org / modern.lisp: - Replace simple ioctl-only prose with 4-step fallback chain - Document env-var pre-check and /dev/tty fallback text-input.org / input.lisp: - Update read-raw-byte prose: with-pinned-objects/vector-sap instead of alien buffer (code was already correct, prose stale) - Add missing (require :sb-posix) to SIGWINCH handler code block - Document :sb-posix requirement in prose
This commit is contained in:
@@ -817,14 +817,71 @@ No-op — simple backend has no terminal state to restore.
|
||||
|
||||
*** Backend Size (Simple)
|
||||
|
||||
Returns hard-coded 80x24 dimensions. A real implementation would use
|
||||
ioctl or TIOCGWINSZ, but the simple backend avoids OS-specific calls
|
||||
for maximum portability.
|
||||
Queries actual terminal dimensions through a fallback chain, with
|
||||
a hard-coded 80x24 at the end:
|
||||
|
||||
1. **Env var pre-check** — ~MY_TERM_COLS~ / ~MY_TERM_ROWS~, set by the
|
||||
calling script before ~exec sbcl~. Checked with ~return-from~ so that
|
||||
/both/ values are preserved (Common Lisp's ~or~ discards secondary
|
||||
values).
|
||||
2. **ioctl on fd 0 (stdin)** — the parent's real terminal fd.
|
||||
3. **ioctl on stdout** — fast and correct after SIGWINCH at runtime.
|
||||
4. **ioctl on ~/dev/tty~** — fallback when stdin/stdout are pipes.
|
||||
5. **~(values 80 24)~** — last resort.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/backend/simple.lisp
|
||||
(defmethod backend-size ((b simple-backend))
|
||||
;; Try ioctl, fall back to 80x24
|
||||
(values 80 24))
|
||||
;; MY_TERM_COLS/MY_TERM_ROWS — set by the calling script.
|
||||
;; Check with return-from to preserve both values.
|
||||
(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))
|
||||
(return-from backend-size (values cols rows))))))
|
||||
(or ;; ioctl on fd 0 (stdin) — the parent's own terminal.
|
||||
(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))))
|
||||
(when (and cols rows (> cols 0) (> rows 0))
|
||||
(values cols rows)))
|
||||
;; ioctl on stdout fd — fast, correct after SIGWINCH at runtime.
|
||||
(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
|
||||
(sb-sys:fd-stream-fd (backend-output-stream b))
|
||||
21523 (sb-alien:alien-sap winsize))))
|
||||
(when ok
|
||||
(values (sb-alien:deref winsize 1)
|
||||
(sb-alien:deref winsize 0))))
|
||||
(sb-alien:free-alien winsize))))
|
||||
(when (and cols rows (> cols 0) (> rows 0))
|
||||
(values cols rows)))
|
||||
(ignore-errors
|
||||
(let ((tty-fd (sb-unix:unix-open "/dev/tty" 0 0)))
|
||||
(when (and tty-fd (numberp tty-fd) (> tty-fd 0))
|
||||
(unwind-protect
|
||||
(let* ((winsize (sb-alien:make-alien sb-alien:unsigned-short 4)))
|
||||
(let ((ok (sb-unix:unix-ioctl tty-fd 21523
|
||||
(sb-alien:alien-sap winsize))))
|
||||
(when ok
|
||||
(values (sb-alien:deref winsize 1)
|
||||
(sb-alien:deref winsize 0))))
|
||||
(sb-alien:free-alien winsize))
|
||||
(sb-unix:unix-close tty-fd)))))
|
||||
(values 80 24)))
|
||||
#+END_SRC
|
||||
|
||||
*** Backend Write (Simple)
|
||||
|
||||
Reference in New Issue
Block a user