On 12 Feb 2023, at 11:47, Alessio Stalla <alessiostalla@gmail.com> wrote:

Greetings everyone,

is there a well-known way to do the thing in the subject, i.e. ask the user for a password in a console application, reading from the keyboard without echoing anything to the terminal? Using a library is fine, SBCL only is fine, limited to some OS is not so fine but better than nothing. Any idea?


Here’s something that I’ve used for Lispworks and SBCL:


#+sbcl
(require :sb-posix)

#+sbcl
(defun echo-off ()
  (let ((tm (sb-posix:tcgetattr sb-sys:*tty*)))
    (setf (sb-posix:termios-lflag tm)
      (logandc2 (sb-posix:termios-lflag tm) sb-posix:echo))
    (sb-posix:tcsetattr sb-sys:*tty* sb-posix:tcsanow tm)))

#+sbcl
(defun echo-on ()
  (let ((tm (sb-posix:tcgetattr sb-sys:*tty*)))
    (setf (sb-posix:termios-lflag tm)
      (logior (sb-posix:termios-lflag tm) sb-posix:echo))
    (sb-posix:tcsetattr sb-sys:*tty* sb-posix:tcsanow tm)))

#+sbcl
(defun prompt-for-value (prompt)
  (format t "~&~a " prompt)
  (force-output)
  (read-line))

#+sbcl
(defun prompt-for-value/no-echo (prompt)
  (format t "~&~a " prompt)
  (force-output)
  (echo-off)
  (unwind-protect
       (read-line)
    (echo-on)))

#+capi
(defun prompt-for-value (prompt)
  (capi:prompt-for-string prompt))

#+capi
(defun prompt-for-value/no-echo (prompt)
  (capi:prompt-for-string prompt :pane-class 'capi:password-pane))

(defun get-passphrase-from-user ()
  (prompt-for-value/no-echo "Passphrase?"))