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?
Alessio,
I think you should be able to reliably disable local echo in a terminal using a specific ANSI sequence. Sadly, my terminal foo is a bit rusty these days, so I don’t remember the specifics, but following this idea should help.
Antoni
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?
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?"))
Thank you very much, works like a charm!
On Sun, 12 Feb 2023 at 16:16, Raymond Wiker rwiker@gmail.com wrote:
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?"))
Raymond Wiker wrote:
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?
Just tested this for Allegro CL:
(eval-when (compile eval load) (require :osi))
(defun echo-off () (let ((tm (excl.osi:tcgetattr *terminal-io*))) (setf (excl.osi:termios-lflag tm) (logandc2 (excl.osi:termios-lflag tm) excl.osi:*echo*)) (excl.osi:tcsetattr *terminal-io* tm)))
(defun echo-on () (let ((tm (excl.osi:tcgetattr *terminal-io*))) (setf (excl.osi:termios-lflag tm) (logior (excl.osi:termios-lflag tm) excl.osi:*echo*)) (excl.osi:tcsetattr *terminal-io* tm)))
(defun prompt-for-value (prompt) (format t "~&~a " prompt) (force-output) (read-line))
(defun prompt-for-value/no-echo (prompt) (format t "~&~a " prompt) (force-output) (echo-off) (unwind-protect (read-line) (echo-on)))
(defun get-passphrase-from-user () (prompt-for-value/no-echo "Passphrase?"))