Created
August 14, 2017 16:31
-
-
Save kisp/5fb672b56a9200d79dd8afe49e66b1c6 to your computer and use it in GitHub Desktop.
Disable terminal echoing in SBCL (e.g. for password prompts)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun prompt-for-password () | |
(format t "Password: ") | |
(force-output) | |
(with-echoing-disabled | |
(prog1 | |
(read-line) | |
(terpri)))) | |
(format t "got ~S~%" (prompt-for-password)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sbcl --noinform --load with-echoing-disabled.lisp --load example.lisp --quit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'sb-posix) | |
(defun invoke-with-echoing-disabled (thunk) | |
(let ((fd (sb-sys:fd-stream-fd sb-sys:*stdout*))) | |
(if (zerop (sb-unix:unix-isatty fd)) | |
(funcall thunk) | |
(let ((termios (sb-posix:tcgetattr fd))) | |
(setf (sb-posix:termios-lflag termios) | |
(logand (sb-posix:termios-lflag termios) | |
(lognot sb-posix:echo))) | |
(sb-posix:tcsetattr fd sb-posix:tcsaflush termios) | |
(unwind-protect | |
(funcall thunk) | |
(setf (sb-posix:termios-lflag termios) | |
(logior (sb-posix:termios-lflag termios) | |
sb-posix:echo)) | |
(sb-posix:tcsetattr fd sb-posix:tcsaflush termios)))))) | |
(defmacro with-echoing-disabled (&body body) | |
`(invoke-with-echoing-disabled (lambda () ,@body))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment