1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-07 09:16:13 +02:00

add generic terminal prompt function

When we need to prompt the user for input interactively, we
want to access their terminal directly. We can't rely on
stdio because it may be connected to pipes or files, rather
than the terminal. Instead, we use "getpass()", because it
abstracts the idea of prompting and reading from the
terminal.  However, it has some problems:

  1. It never echoes the typed characters, which makes it OK
     for passwords but annoying for other input (like usernames).

  2. Some implementations of getpass() have an extremely
     small input buffer (e.g., Solaris 8 is reported to
     support only 8 characters).

  3. Some implementations of getpass() will fall back to
     reading from stdin (e.g., glibc). We explicitly don't
     want this, because our stdin may be connected to a pipe
     speaking a particular protocol, and reading will
     disrupt the protocol flow (e.g., the remote-curl
     helper).

  4. Some implementations of getpass() turn off signals, so
     that hitting "^C" on the terminal does not break out of
     the password prompt. This can be a mild annoyance.

Instead, let's provide an abstract "git_terminal_prompt"
function that addresses these concerns. This patch includes
an implementation based on /dev/tty, enabled by setting
HAVE_DEV_TTY. The fallback is to use getpass() as before.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2011-12-10 05:41:01 -05:00 committed by Junio C Hamano
parent 1cb0134f34
commit 21aeafceda
3 changed files with 96 additions and 0 deletions

View File

@ -229,6 +229,9 @@ all::
#
# Define NO_REGEX if you have no or inferior regex support in your C library.
#
# Define HAVE_DEV_TTY if your system can open /dev/tty to interact with the
# user.
#
# Define GETTEXT_POISON if you are debugging the choice of strings marked
# for translation. In a GETTEXT_POISON build, you can turn all strings marked
# for translation into gibberish by setting the GIT_GETTEXT_POISON variable
@ -523,6 +526,7 @@ LIB_H += compat/bswap.h
LIB_H += compat/cygwin.h
LIB_H += compat/mingw.h
LIB_H += compat/obstack.h
LIB_H += compat/terminal.h
LIB_H += compat/win32/pthread.h
LIB_H += compat/win32/syslog.h
LIB_H += compat/win32/poll.h
@ -610,6 +614,7 @@ LIB_OBJS += color.o
LIB_OBJS += combine-diff.o
LIB_OBJS += commit.o
LIB_OBJS += compat/obstack.o
LIB_OBJS += compat/terminal.o
LIB_OBJS += config.o
LIB_OBJS += connect.o
LIB_OBJS += connected.o
@ -1646,6 +1651,10 @@ ifdef HAVE_PATHS_H
BASIC_CFLAGS += -DHAVE_PATHS_H
endif
ifdef HAVE_DEV_TTY
BASIC_CFLAGS += -DHAVE_DEV_TTY
endif
ifdef DIR_HAS_BSD_GROUP_SEMANTICS
COMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS
endif

81
compat/terminal.c Normal file
View File

@ -0,0 +1,81 @@
#include "git-compat-util.h"
#include "compat/terminal.h"
#include "sigchain.h"
#include "strbuf.h"
#ifdef HAVE_DEV_TTY
static int term_fd = -1;
static struct termios old_term;
static void restore_term(void)
{
if (term_fd < 0)
return;
tcsetattr(term_fd, TCSAFLUSH, &old_term);
term_fd = -1;
}
static void restore_term_on_signal(int sig)
{
restore_term();
sigchain_pop(sig);
raise(sig);
}
char *git_terminal_prompt(const char *prompt, int echo)
{
static struct strbuf buf = STRBUF_INIT;
int r;
FILE *fh;
fh = fopen("/dev/tty", "w+");
if (!fh)
return NULL;
if (!echo) {
struct termios t;
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);
fflush(fh);
r = strbuf_getline(&buf, fh, '\n');
if (!echo) {
putc('\n', fh);
fflush(fh);
}
restore_term();
fclose(fh);
if (r == EOF)
return NULL;
return buf.buf;
}
#else
char *git_terminal_prompt(const char *prompt, int echo)
{
return getpass(prompt);
}
#endif

6
compat/terminal.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef COMPAT_TERMINAL_H
#define COMPAT_TERMINAL_H
char *git_terminal_prompt(const char *prompt, int echo);
#endif /* COMPAT_TERMINAL_H */