terminal: don't assume stdin is /dev/tty

read_key_without_echo() reads from stdin but uses /dev/tty when it disables echo. This is unfortunate as there no guarantee that stdin is the same device as /dev/tty. The perl version of "add -p" uses stdin when it sets the terminal mode, this commit does the same for the builtin version. There is still a difference between the perl and builtin versions though - the perl version will ignore any errors when setting the terminal mode[1] and will still read single bytes when stdin is not a terminal. The builtin version displays a warning if setting the terminal mode fails and switches to reading a line at a time. [1] https://github.com/jonathanstowe/TermReadKey/blob/b061c913bbf7ff9bad9b4eea6caae189eacd6063/ReadKey.xs#L1090 Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Mar 16, 2022 at 18:54 UTC e4938ce3cc5967c2366db289ca854b0b796a5afd
2 files changed +22 -9
compat/terminal.c
+20 -9
@@ -20,28 +20,41 @@ static void restore_term_on_signal(int sig)
20 #define INPUT_PATH "/dev/tty"
21 #define OUTPUT_PATH "/dev/tty"
22
23 +static volatile sig_atomic_t term_fd_needs_closing;
24 static int term_fd = -1;
25 static struct termios old_term;
26
27 +static void close_term_fd(void)
28 +{
29 + if (term_fd_needs_closing)
30 + close(term_fd);
31 + term_fd_needs_closing = 0;
32 + term_fd = -1;
33 +}
34 +
35 void restore_term(void)
36 {
37 if (term_fd < 0)
38 return;
39
40 tcsetattr(term_fd, TCSAFLUSH, &old_term);
32 - close(term_fd);
33 - term_fd = -1;
41 + close_term_fd();
42 sigchain_pop_common();
43 }
44
45 int save_term(enum save_term_flags flags)
46 {
47 if (term_fd < 0)
40 - term_fd = open("/dev/tty", O_RDWR);
48 + term_fd = ((flags & SAVE_TERM_STDIN)
49 + ? 0
50 + : open("/dev/tty", O_RDWR));
51 if (term_fd < 0)
52 return -1;
43 - if (tcgetattr(term_fd, &old_term) < 0)
53 + term_fd_needs_closing = !(flags & SAVE_TERM_STDIN);
54 + if (tcgetattr(term_fd, &old_term) < 0) {
55 + close_term_fd();
56 return -1;
57 + }
58 sigchain_push_common(restore_term_on_signal);
59
60 return 0;
@@ -52,7 +65,7 @@ static int disable_bits(enum save_term_flags flags, tcflag_t bits)
65 struct termios t;
66
67 if (save_term(flags) < 0)
55 - goto error;
68 + return -1;
69
70 t = old_term;
71
@@ -65,9 +78,7 @@ static int disable_bits(enum save_term_flags flags, tcflag_t bits)
78 return 0;
79
80 sigchain_pop_common();
68 -error:
69 - close(term_fd);
70 - term_fd = -1;
81 + close_term_fd();
82 return -1;
83 }
84
@@ -362,7 +373,7 @@ int read_key_without_echo(struct strbuf *buf)
373 static int warning_displayed;
374 int ch;
375
365 - if (warning_displayed || enable_non_canonical(0) < 0) {
376 + if (warning_displayed || enable_non_canonical(SAVE_TERM_STDIN) < 0) {
377 if (!warning_displayed) {
378 warning("reading single keystrokes not supported on "
379 "this platform; reading line instead");
compat/terminal.h
+2
@@ -4,6 +4,8 @@
4 enum save_term_flags {
5 /* Save input and output settings */
6 SAVE_TERM_DUPLEX = 1 << 0,
7 + /* Save stdin rather than /dev/tty (fails if stdin is not a terminal) */
8 + SAVE_TERM_STDIN = 1 << 1,
9 };
10
11 /*