1 #include "git-compat-util.h"
2 #include "compat/terminal.h"
5 #include "run-command.h"
6 #include "string-list.h"
9 #if defined(HAVE_DEV_TTY) || defined(GIT_WINDOWS_NATIVE)
11 static void restore_term(void);
13 static void restore_term_on_signal(int sig)
22 #define INPUT_PATH "/dev/tty"
23 #define OUTPUT_PATH "/dev/tty"
25 static int term_fd = -1;
26 static struct termios old_term;
28 static void restore_term(void)
33 tcsetattr(term_fd, TCSAFLUSH, &old_term);
38 static int disable_bits(tcflag_t bits)
42 term_fd = open("/dev/tty", O_RDWR);
43 if (tcgetattr(term_fd, &t) < 0)
47 sigchain_push_common(restore_term_on_signal);
50 if (!tcsetattr(term_fd, TCSAFLUSH, &t))
59 static int disable_echo(void)
61 return disable_bits(ECHO);
64 static int enable_non_canonical(void)
66 return disable_bits(ICANON | ECHO);
69 #elif defined(GIT_WINDOWS_NATIVE)
71 #define INPUT_PATH "CONIN$"
72 #define OUTPUT_PATH "CONOUT$"
73 #define FORCE_TEXT "t"
75 static int use_stty = 1;
76 static struct string_list stty_restore = STRING_LIST_INIT_DUP;
77 static HANDLE hconin = INVALID_HANDLE_VALUE;
80 static void restore_term(void)
84 struct child_process cp = CHILD_PROCESS_INIT;
86 if (stty_restore.nr == 0)
89 argv_array_push(&cp.args, "stty");
90 for (i = 0; i < stty_restore.nr; i++)
91 argv_array_push(&cp.args, stty_restore.items[i].string);
93 string_list_clear(&stty_restore, 0);
97 if (hconin == INVALID_HANDLE_VALUE)
100 SetConsoleMode(hconin, cmode);
102 hconin = INVALID_HANDLE_VALUE;
105 static int disable_bits(DWORD bits)
108 struct child_process cp = CHILD_PROCESS_INIT;
110 argv_array_push(&cp.args, "stty");
112 if (bits & ENABLE_LINE_INPUT) {
113 string_list_append(&stty_restore, "icanon");
114 argv_array_push(&cp.args, "-icanon");
117 if (bits & ENABLE_ECHO_INPUT) {
118 string_list_append(&stty_restore, "echo");
119 argv_array_push(&cp.args, "-echo");
122 if (bits & ENABLE_PROCESSED_INPUT) {
123 string_list_append(&stty_restore, "-ignbrk");
124 string_list_append(&stty_restore, "intr");
125 string_list_append(&stty_restore, "^c");
126 argv_array_push(&cp.args, "ignbrk");
127 argv_array_push(&cp.args, "intr");
128 argv_array_push(&cp.args, "");
131 if (run_command(&cp) == 0)
134 /* `stty` could not be executed; access the Console directly */
138 hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
139 FILE_SHARE_READ, NULL, OPEN_EXISTING,
140 FILE_ATTRIBUTE_NORMAL, NULL);
141 if (hconin == INVALID_HANDLE_VALUE)
144 GetConsoleMode(hconin, &cmode);
145 sigchain_push_common(restore_term_on_signal);
146 if (!SetConsoleMode(hconin, cmode & ~bits)) {
148 hconin = INVALID_HANDLE_VALUE;
155 static int disable_echo(void)
157 return disable_bits(ENABLE_ECHO_INPUT);
160 static int enable_non_canonical(void)
162 return disable_bits(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
166 * Override `getchar()`, as the default implementation does not use
169 * This poses a problem when we want to see whether the standard
170 * input has more characters, as the default of Git for Windows is to start the
171 * Bash in a MinTTY, which uses a named pipe to emulate a pty, in which case
172 * our `poll()` emulation calls `PeekNamedPipe()`, which seems to require
173 * `ReadFile()` to be called first to work properly (it only reports 0
174 * available bytes, otherwise).
176 * So let's just override `getchar()` with a version backed by `ReadFile()` and
177 * go our merry ways from here.
179 static int mingw_getchar(void)
184 if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE), &ch, 1, &read, NULL))
188 error("Unexpected 0 read");
194 #define getchar mingw_getchar
202 char *git_terminal_prompt(const char *prompt, int echo)
204 static struct strbuf buf = STRBUF_INIT;
206 FILE *input_fh, *output_fh;
208 input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT);
212 output_fh = fopen(OUTPUT_PATH, "w" FORCE_TEXT);
218 if (!echo && disable_echo()) {
224 fputs(prompt, output_fh);
227 r = strbuf_getline_lf(&buf, input_fh);
229 putc('\n', output_fh);
243 * The `is_known_escape_sequence()` function returns 1 if the passed string
244 * corresponds to an Escape sequence that the terminal capabilities contains.
246 * To avoid depending on ncurses or other platform-specific libraries, we rely
247 * on the presence of the `infocmp` executable to do the job for us (failing
248 * silently if the program is not available or refused to run).
250 struct escape_sequence_entry {
251 struct hashmap_entry entry;
252 char sequence[FLEX_ARRAY];
255 static int sequence_entry_cmp(const void *hashmap_cmp_fn_data,
256 const struct escape_sequence_entry *e1,
257 const struct escape_sequence_entry *e2,
260 return strcmp(e1->sequence, keydata ? keydata : e2->sequence);
263 static int is_known_escape_sequence(const char *sequence)
265 static struct hashmap sequences;
266 static int initialized;
269 struct child_process cp = CHILD_PROCESS_INIT;
270 struct strbuf buf = STRBUF_INIT;
273 hashmap_init(&sequences, (hashmap_cmp_fn)sequence_entry_cmp,
276 argv_array_pushl(&cp.args, "infocmp", "-L", "-1", NULL);
277 if (pipe_command(&cp, NULL, 0, &buf, 0, NULL, 0))
278 strbuf_setlen(&buf, 0);
280 for (eol = p = buf.buf; *p; p = eol + 1) {
285 eol = strchrnul(p, '\n');
287 if (starts_with(p, "\\E")) {
288 char *comma = memchr(p, ',', eol - p);
289 struct escape_sequence_entry *e;
293 FLEX_ALLOC_MEM(e, sequence, p, comma - p);
294 hashmap_entry_init(&e->entry,
295 strhash(e->sequence));
296 hashmap_add(&sequences, &e->entry);
304 return !!hashmap_get_from_hash(&sequences, strhash(sequence), sequence);
307 int read_key_without_echo(struct strbuf *buf)
309 static int warning_displayed;
312 if (warning_displayed || enable_non_canonical() < 0) {
313 if (!warning_displayed) {
314 warning("reading single keystrokes not supported on "
315 "this platform; reading line instead");
316 warning_displayed = 1;
319 return strbuf_getline(buf, stdin);
328 strbuf_addch(buf, ch);
330 if (ch == '\033' /* ESC */) {
332 * We are most likely looking at an Escape sequence. Let's try
333 * to read more bytes, waiting at most half a second, assuming
334 * that the sequence is complete if we did not receive any byte
337 * Start by replacing the Escape byte with ^[ */
338 strbuf_splice(buf, buf->len - 1, 1, "^[", 2);
341 * Query the terminal capabilities once about all the Escape
342 * sequences it knows about, so that we can avoid waiting for
343 * half a second when we know that the sequence is complete.
345 while (!is_known_escape_sequence(buf->buf)) {
346 struct pollfd pfd = { .fd = 0, .events = POLLIN };
348 if (poll(&pfd, 1, 500) < 1)
354 strbuf_addch(buf, ch);
364 char *git_terminal_prompt(const char *prompt, int echo)
366 return getpass(prompt);
369 int read_key_without_echo(struct strbuf *buf)
371 static int warning_displayed;
374 if (!warning_displayed) {
375 warning("reading single keystrokes not supported on this "
376 "platform; reading line instead");
377 warning_displayed = 1;
384 strbuf_addstr(buf, res);