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 <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Erik Faye-Lund committed
Dec 4, 2012 at 09:10 UTC
9df92e6369138764ce7bd44edbb307d98e9d72fd
1 file changed
+25
-18
compat/terminal.c
+25
-18
@@ -14,6 +14,7 @@ static void restore_term(void)
14
return;
15
16
tcsetattr(term_fd, TCSAFLUSH, &old_term);
17
+ close(term_fd);
18
term_fd = -1;
19
}
20
@@ -24,6 +25,27 @@ static void restore_term_on_signal(int sig)
25
raise(sig);
26
}
27
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
+
49
char *git_terminal_prompt(const char *prompt, int echo)
50
{
51
static struct strbuf buf = STRBUF_INIT;
@@ -34,24 +56,9 @@ char *git_terminal_prompt(const char *prompt, int echo)
56
if (!fh)
57
return NULL;
58
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;
62
}
63
64
fputs(prompt, fh);