Skip to content

Instantly share code, notes, and snippets.

@kisp
Created August 14, 2017 16:31
Show Gist options
  • Save kisp/5fb672b56a9200d79dd8afe49e66b1c6 to your computer and use it in GitHub Desktop.
Save kisp/5fb672b56a9200d79dd8afe49e66b1c6 to your computer and use it in GitHub Desktop.
Disable terminal echoing in SBCL (e.g. for password prompts)
(defun prompt-for-password ()
(format t "Password: ")
(force-output)
(with-echoing-disabled
(prog1
(read-line)
(terpri))))
(format t "got ~S~%" (prompt-for-password))
sbcl --noinform --load with-echoing-disabled.lisp --load example.lisp --quit
(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