Skip to content

Commit 9df92e6

Browse files
kusmagitster
authored andcommitted
compat/terminal: factor out echo-disabling
By moving the echo-disabling code to a separate function, we can implement OS-specific versions of it for non-POSIX platforms. Signed-off-by: Erik Faye-Lund <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 176478a commit 9df92e6

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

compat/terminal.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static void restore_term(void)
1414
return;
1515

1616
tcsetattr(term_fd, TCSAFLUSH, &old_term);
17+
close(term_fd);
1718
term_fd = -1;
1819
}
1920

@@ -24,6 +25,27 @@ static void restore_term_on_signal(int sig)
2425
raise(sig);
2526
}
2627

28+
static int disable_echo(void)
29+
{
30+
struct termios t;
31+
32+
term_fd = open("/dev/tty", O_RDWR);
33+
if (tcgetattr(term_fd, &t) < 0)
34+
goto error;
35+
36+
old_term = t;
37+
sigchain_push_common(restore_term_on_signal);
38+
39+
t.c_lflag &= ~ECHO;
40+
if (!tcsetattr(term_fd, TCSAFLUSH, &t))
41+
return 0;
42+
43+
error:
44+
close(term_fd);
45+
term_fd = -1;
46+
return -1;
47+
}
48+
2749
char *git_terminal_prompt(const char *prompt, int echo)
2850
{
2951
static struct strbuf buf = STRBUF_INIT;
@@ -34,24 +56,9 @@ char *git_terminal_prompt(const char *prompt, int echo)
3456
if (!fh)
3557
return NULL;
3658

37-
if (!echo) {
38-
struct termios t;
39-
40-
if (tcgetattr(fileno(fh), &t) < 0) {
41-
fclose(fh);
42-
return NULL;
43-
}
44-
45-
old_term = t;
46-
term_fd = fileno(fh);
47-
sigchain_push_common(restore_term_on_signal);
48-
49-
t.c_lflag &= ~ECHO;
50-
if (tcsetattr(fileno(fh), TCSAFLUSH, &t) < 0) {
51-
term_fd = -1;
52-
fclose(fh);
53-
return NULL;
54-
}
59+
if (!echo && disable_echo()) {
60+
fclose(fh);
61+
return NULL;
5562
}
5663

5764
fputs(prompt, fh);

0 commit comments

Comments
 (0)