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>
2011-12-10 18:41:01 +08:00
|
|
|
#ifndef COMPAT_TERMINAL_H
|
|
|
|
#define COMPAT_TERMINAL_H
|
|
|
|
|
2022-03-17 02:54:02 +08:00
|
|
|
enum save_term_flags {
|
|
|
|
/* Save input and output settings */
|
|
|
|
SAVE_TERM_DUPLEX = 1 << 0,
|
2022-03-17 02:54:03 +08:00
|
|
|
/* Save stdin rather than /dev/tty (fails if stdin is not a terminal) */
|
|
|
|
SAVE_TERM_STDIN = 1 << 1,
|
2022-03-17 02:54:02 +08:00
|
|
|
};
|
|
|
|
|
2022-02-23 02:53:33 +08:00
|
|
|
/*
|
|
|
|
* Save the terminal attributes so they can be restored later by a
|
|
|
|
* call to restore_term(). Note that every successful call to
|
|
|
|
* save_term() must be matched by a call to restore_term() even if the
|
|
|
|
* attributes have not been changed. Returns 0 on success, -1 on
|
|
|
|
* failure.
|
|
|
|
*/
|
2022-03-17 02:54:02 +08:00
|
|
|
int save_term(enum save_term_flags flags);
|
2022-02-23 02:53:33 +08:00
|
|
|
/* Restore the terminal attributes that were saved with save_term() */
|
2021-10-05 15:46:47 +08:00
|
|
|
void restore_term(void);
|
|
|
|
|
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>
2011-12-10 18:41:01 +08:00
|
|
|
char *git_terminal_prompt(const char *prompt, int echo);
|
|
|
|
|
2020-01-15 02:43:49 +08:00
|
|
|
/* Read a single keystroke, without echoing it to the terminal */
|
|
|
|
int read_key_without_echo(struct strbuf *buf);
|
|
|
|
|
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>
2011-12-10 18:41:01 +08:00
|
|
|
#endif /* COMPAT_TERMINAL_H */
|