0

I have a test program that produces a lengthy output, so I ran it in "Shell". However, at some point the test program prompts for a password, and when entering it, the characters are displayed, even if the program has set "echo off" (it's a Perl program that uses ReadMode('noecho');).

Is there a way to prevent keyboard echo during password input?

Code sample

As the solution seems to depend on the actual code being used, here is a routine to "get a PIN":

# read PIN from STDIN use Term::ReadKey; sub read_PIN(;$) { my $PIN; if (-t STDIN) { my $prompt = 'PIN:'; $prompt = $_[0] . ' ' . $prompt if ($_[0]); ReadMode('noecho'); # disable echo print $prompt if (-t STDOUT); # use a prompt $PIN = <STDIN>; print "\n" if (-t STDOUT); ReadMode('normal'); # re-enable echo } else { $PIN = <STDIN>; } chomp($PIN); return $PIN; } # use example: $PIN = read_PIN('some detail'); 
6
  • 2
    Does your program's prompt match comint-password-prompt-regexp? Commented Feb 1, 2024 at 11:06
  • Never heard about that ;-) Well, it's even worse: 1) comint-password-prompt-regexplacks PIN: and 2) (even more worse) my program suppresses the prompt unless (-t STDIN) (which seems true for Emacs' shell mode). Commented Feb 1, 2024 at 11:22
  • 1
    If you want a tty, use ansi-term, but it's probably easier to change your code to always prompt with something like "Password:" Commented Feb 1, 2024 at 13:03
  • I also didn't know about ansi-term; when I tried it, it worked! Commented Feb 1, 2024 at 13:36
  • ...but ansi-term seems to have a limited or no scroll-back buffer, so I see no advantage compared to using the terminal directly. Commented Feb 1, 2024 at 13:55

2 Answers 2

1

If you want Emacs to behave like a terminal, you need to use one of the terminal emulation packages, such as: term, ansi-term, or vterm (and maybe eat, but I've never tried it).

0

The problem seems to be that Emacs' Shell mode does not obey the "stty echo" setting:

The Shell echos keyboard input independent of the state of stty echo (at least in 24.3.1).

> stty speed 38400 baud; line = 0; -icrnl -imaxbel -onlcr -echo > stty -echo > stty speed 38400 baud; line = 0; -icrnl -imaxbel -onlcr -echo > stty echo > stty stty speed 38400 baud; line = 0; -icrnl -imaxbel -onlcr > ddddd > stty -echo stty -echo > stty speed 38400 baud; line = 0; -icrnl -imaxbel -onlcr -echo 

So the echo flag can be changed, but it has no effect. Without a change in the code this won't work in Shell mode.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.