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>
This commit is contained in:
parent
176478a8bd
commit
9df92e6369
@ -14,6 +14,7 @@ static void restore_term(void)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
tcsetattr(term_fd, TCSAFLUSH, &old_term);
|
tcsetattr(term_fd, TCSAFLUSH, &old_term);
|
||||||
|
close(term_fd);
|
||||||
term_fd = -1;
|
term_fd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,6 +25,27 @@ static void restore_term_on_signal(int sig)
|
|||||||
raise(sig);
|
raise(sig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int disable_echo(void)
|
||||||
|
{
|
||||||
|
struct termios t;
|
||||||
|
|
||||||
|
term_fd = open("/dev/tty", O_RDWR);
|
||||||
|
if (tcgetattr(term_fd, &t) < 0)
|
||||||
|
goto error;
|
||||||
|
|
||||||
|
old_term = t;
|
||||||
|
sigchain_push_common(restore_term_on_signal);
|
||||||
|
|
||||||
|
t.c_lflag &= ~ECHO;
|
||||||
|
if (!tcsetattr(term_fd, TCSAFLUSH, &t))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
error:
|
||||||
|
close(term_fd);
|
||||||
|
term_fd = -1;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
char *git_terminal_prompt(const char *prompt, int echo)
|
char *git_terminal_prompt(const char *prompt, int echo)
|
||||||
{
|
{
|
||||||
static struct strbuf buf = STRBUF_INIT;
|
static struct strbuf buf = STRBUF_INIT;
|
||||||
@ -34,24 +56,9 @@ char *git_terminal_prompt(const char *prompt, int echo)
|
|||||||
if (!fh)
|
if (!fh)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!echo) {
|
if (!echo && disable_echo()) {
|
||||||
struct termios t;
|
fclose(fh);
|
||||||
|
return NULL;
|
||||||
if (tcgetattr(fileno(fh), &t) < 0) {
|
|
||||||
fclose(fh);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
old_term = t;
|
|
||||||
term_fd = fileno(fh);
|
|
||||||
sigchain_push_common(restore_term_on_signal);
|
|
||||||
|
|
||||||
t.c_lflag &= ~ECHO;
|
|
||||||
if (tcsetattr(fileno(fh), TCSAFLUSH, &t) < 0) {
|
|
||||||
term_fd = -1;
|
|
||||||
fclose(fh);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fputs(prompt, fh);
|
fputs(prompt, fh);
|
||||||
|
Loading…
Reference in New Issue
Block a user