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_on_signal(int sig
)
20 #define INPUT_PATH "/dev/tty"
21 #define OUTPUT_PATH "/dev/tty"
23 static int term_fd
= -1;
24 static struct termios old_term
;
26 void restore_term(void)
31 tcsetattr(term_fd
, TCSAFLUSH
, &old_term
);
36 int save_term(int full_duplex
)
39 term_fd
= open("/dev/tty", O_RDWR
);
41 return (term_fd
< 0) ? -1 : tcgetattr(term_fd
, &old_term
);
44 static int disable_bits(tcflag_t bits
)
52 sigchain_push_common(restore_term_on_signal
);
55 if (!tcsetattr(term_fd
, TCSAFLUSH
, &t
))
64 static int disable_echo(void)
66 return disable_bits(ECHO
);
69 static int enable_non_canonical(void)
71 return disable_bits(ICANON
| ECHO
);
74 #elif defined(GIT_WINDOWS_NATIVE)
76 #define INPUT_PATH "CONIN$"
77 #define OUTPUT_PATH "CONOUT$"
78 #define FORCE_TEXT "t"
80 static int use_stty
= 1;
81 static struct string_list stty_restore
= STRING_LIST_INIT_DUP
;
82 static HANDLE hconin
= INVALID_HANDLE_VALUE
;
83 static HANDLE hconout
= INVALID_HANDLE_VALUE
;
84 static DWORD cmode_in
, cmode_out
;
86 void restore_term(void)
90 struct child_process cp
= CHILD_PROCESS_INIT
;
92 if (stty_restore
.nr
== 0)
95 strvec_push(&cp
.args
, "stty");
96 for (i
= 0; i
< stty_restore
.nr
; i
++)
97 strvec_push(&cp
.args
, stty_restore
.items
[i
].string
);
99 string_list_clear(&stty_restore
, 0);
103 if (hconin
== INVALID_HANDLE_VALUE
)
106 SetConsoleMode(hconin
, cmode_in
);
109 assert(hconout
!= INVALID_HANDLE_VALUE
);
110 SetConsoleMode(hconout
, cmode_out
);
111 CloseHandle(hconout
);
114 hconin
= hconout
= INVALID_HANDLE_VALUE
;
117 int save_term(int full_duplex
)
119 hconin
= CreateFileA("CONIN$", GENERIC_READ
| GENERIC_WRITE
,
120 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
121 FILE_ATTRIBUTE_NORMAL
, NULL
);
122 if (hconin
== INVALID_HANDLE_VALUE
)
126 hconout
= CreateFileA("CONOUT$", GENERIC_READ
| GENERIC_WRITE
,
127 FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
,
128 FILE_ATTRIBUTE_NORMAL
, NULL
);
129 if (hconout
== INVALID_HANDLE_VALUE
)
132 GetConsoleMode(hconout
, &cmode_out
);
135 GetConsoleMode(hconin
, &cmode_in
);
140 hconin
= INVALID_HANDLE_VALUE
;
144 static int disable_bits(DWORD bits
)
147 struct child_process cp
= CHILD_PROCESS_INIT
;
149 strvec_push(&cp
.args
, "stty");
151 if (bits
& ENABLE_LINE_INPUT
) {
152 string_list_append(&stty_restore
, "icanon");
153 strvec_push(&cp
.args
, "-icanon");
156 if (bits
& ENABLE_ECHO_INPUT
) {
157 string_list_append(&stty_restore
, "echo");
158 strvec_push(&cp
.args
, "-echo");
161 if (bits
& ENABLE_PROCESSED_INPUT
) {
162 string_list_append(&stty_restore
, "-ignbrk");
163 string_list_append(&stty_restore
, "intr");
164 string_list_append(&stty_restore
, "^c");
165 strvec_push(&cp
.args
, "ignbrk");
166 strvec_push(&cp
.args
, "intr");
167 strvec_push(&cp
.args
, "");
170 if (run_command(&cp
) == 0)
173 /* `stty` could not be executed; access the Console directly */
177 if (save_term(0) < 0)
180 sigchain_push_common(restore_term_on_signal
);
181 if (!SetConsoleMode(hconin
, cmode_in
& ~bits
)) {
183 hconin
= INVALID_HANDLE_VALUE
;
190 static int disable_echo(void)
192 return disable_bits(ENABLE_ECHO_INPUT
);
195 static int enable_non_canonical(void)
197 return disable_bits(ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
| ENABLE_PROCESSED_INPUT
);
201 * Override `getchar()`, as the default implementation does not use
204 * This poses a problem when we want to see whether the standard
205 * input has more characters, as the default of Git for Windows is to start the
206 * Bash in a MinTTY, which uses a named pipe to emulate a pty, in which case
207 * our `poll()` emulation calls `PeekNamedPipe()`, which seems to require
208 * `ReadFile()` to be called first to work properly (it only reports 0
209 * available bytes, otherwise).
211 * So let's just override `getchar()` with a version backed by `ReadFile()` and
212 * go our merry ways from here.
214 static int mingw_getchar(void)
219 if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE
), &ch
, 1, &read
, NULL
))
223 error("Unexpected 0 read");
229 #define getchar mingw_getchar
237 char *git_terminal_prompt(const char *prompt
, int echo
)
239 static struct strbuf buf
= STRBUF_INIT
;
241 FILE *input_fh
, *output_fh
;
243 input_fh
= fopen(INPUT_PATH
, "r" FORCE_TEXT
);
247 output_fh
= fopen(OUTPUT_PATH
, "w" FORCE_TEXT
);
253 if (!echo
&& disable_echo()) {
259 fputs(prompt
, output_fh
);
262 r
= strbuf_getline_lf(&buf
, input_fh
);
264 putc('\n', output_fh
);
278 * The `is_known_escape_sequence()` function returns 1 if the passed string
279 * corresponds to an Escape sequence that the terminal capabilities contains.
281 * To avoid depending on ncurses or other platform-specific libraries, we rely
282 * on the presence of the `infocmp` executable to do the job for us (failing
283 * silently if the program is not available or refused to run).
285 struct escape_sequence_entry
{
286 struct hashmap_entry entry
;
287 char sequence
[FLEX_ARRAY
];
290 static int sequence_entry_cmp(const void *hashmap_cmp_fn_data
,
291 const struct escape_sequence_entry
*e1
,
292 const struct escape_sequence_entry
*e2
,
295 return strcmp(e1
->sequence
, keydata
? keydata
: e2
->sequence
);
298 static int is_known_escape_sequence(const char *sequence
)
300 static struct hashmap sequences
;
301 static int initialized
;
304 struct child_process cp
= CHILD_PROCESS_INIT
;
305 struct strbuf buf
= STRBUF_INIT
;
308 hashmap_init(&sequences
, (hashmap_cmp_fn
)sequence_entry_cmp
,
311 strvec_pushl(&cp
.args
, "infocmp", "-L", "-1", NULL
);
312 if (pipe_command(&cp
, NULL
, 0, &buf
, 0, NULL
, 0))
313 strbuf_setlen(&buf
, 0);
315 for (eol
= p
= buf
.buf
; *p
; p
= eol
+ 1) {
320 eol
= strchrnul(p
, '\n');
322 if (starts_with(p
, "\\E")) {
323 char *comma
= memchr(p
, ',', eol
- p
);
324 struct escape_sequence_entry
*e
;
328 FLEX_ALLOC_MEM(e
, sequence
, p
, comma
- p
);
329 hashmap_entry_init(&e
->entry
,
330 strhash(e
->sequence
));
331 hashmap_add(&sequences
, &e
->entry
);
339 return !!hashmap_get_from_hash(&sequences
, strhash(sequence
), sequence
);
342 int read_key_without_echo(struct strbuf
*buf
)
344 static int warning_displayed
;
347 if (warning_displayed
|| enable_non_canonical() < 0) {
348 if (!warning_displayed
) {
349 warning("reading single keystrokes not supported on "
350 "this platform; reading line instead");
351 warning_displayed
= 1;
354 return strbuf_getline(buf
, stdin
);
363 strbuf_addch(buf
, ch
);
365 if (ch
== '\033' /* ESC */) {
367 * We are most likely looking at an Escape sequence. Let's try
368 * to read more bytes, waiting at most half a second, assuming
369 * that the sequence is complete if we did not receive any byte
372 * Start by replacing the Escape byte with ^[ */
373 strbuf_splice(buf
, buf
->len
- 1, 1, "^[", 2);
376 * Query the terminal capabilities once about all the Escape
377 * sequences it knows about, so that we can avoid waiting for
378 * half a second when we know that the sequence is complete.
380 while (!is_known_escape_sequence(buf
->buf
)) {
381 struct pollfd pfd
= { .fd
= 0, .events
= POLLIN
};
383 if (poll(&pfd
, 1, 500) < 1)
389 strbuf_addch(buf
, ch
);
399 int save_term(int full_duplex
)
401 /* full_duplex == 1, but no support available */
405 void restore_term(void)
409 char *git_terminal_prompt(const char *prompt
, int echo
)
411 return getpass(prompt
);
414 int read_key_without_echo(struct strbuf
*buf
)
416 static int warning_displayed
;
419 if (!warning_displayed
) {
420 warning("reading single keystrokes not supported on this "
421 "platform; reading line instead");
422 warning_displayed
= 1;
429 strbuf_addstr(buf
, res
);