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
)
14 /* restore_term calls sigchain_pop_common */
20 #define INPUT_PATH "/dev/tty"
21 #define OUTPUT_PATH "/dev/tty"
23 static volatile sig_atomic_t term_fd_needs_closing
;
24 static int term_fd
= -1;
25 static struct termios old_term
;
27 static const char *background_resume_msg
;
28 static const char *restore_error_msg
;
29 static volatile sig_atomic_t ttou_received
;
31 /* async safe error function for use by signal handlers. */
32 static void write_err(const char *msg
)
34 write_in_full(2, "error: ", strlen("error: "));
35 write_in_full(2, msg
, strlen(msg
));
36 write_in_full(2, "\n", 1);
39 static void print_background_resume_msg(int signo
)
41 int saved_errno
= errno
;
43 struct sigaction old_sa
;
44 struct sigaction sa
= { .sa_handler
= SIG_DFL
};
47 write_err(background_resume_msg
);
48 sigaction(signo
, &sa
, &old_sa
);
51 sigaddset(&mask
, signo
);
52 sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
54 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
55 sigaction(signo
, &old_sa
, NULL
);
59 static void restore_terminal_on_suspend(int signo
)
61 int saved_errno
= errno
;
65 struct sigaction old_sa
;
66 struct sigaction sa
= { .sa_handler
= SIG_DFL
};
69 if (tcgetattr(term_fd
, &t
) < 0)
72 if (tcsetattr(term_fd
, TCSAFLUSH
, &old_term
) < 0)
73 write_err(restore_error_msg
);
75 sigaction(signo
, &sa
, &old_sa
);
78 sigaddset(&mask
, signo
);
79 sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
81 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
82 sigaction(signo
, &old_sa
, NULL
);
84 write_err(restore_error_msg
);
88 * If we resume in the background then we receive SIGTTOU when calling
89 * tcsetattr() below. Set up a handler to print an error message in that
93 sigaddset(&mask
, SIGTTOU
);
94 sa
.sa_mask
= old_sa
.sa_mask
;
95 sa
.sa_handler
= print_background_resume_msg
;
96 sa
.sa_flags
= SA_RESTART
;
97 sigaction(SIGTTOU
, &sa
, &old_sa
);
100 sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
101 res
= tcsetattr(term_fd
, TCSAFLUSH
, &t
);
102 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
106 write_err(restore_error_msg
);
107 sigaction(SIGTTOU
, &old_sa
, NULL
);
112 static void reset_job_signals(void)
114 if (restore_error_msg
) {
115 signal(SIGTTIN
, SIG_DFL
);
116 signal(SIGTTOU
, SIG_DFL
);
117 signal(SIGTSTP
, SIG_DFL
);
118 restore_error_msg
= NULL
;
119 background_resume_msg
= NULL
;
123 static void close_term_fd(void)
125 if (term_fd_needs_closing
)
127 term_fd_needs_closing
= 0;
131 void restore_term(void)
136 tcsetattr(term_fd
, TCSAFLUSH
, &old_term
);
138 sigchain_pop_common();
142 int save_term(enum save_term_flags flags
)
147 term_fd
= ((flags
& SAVE_TERM_STDIN
)
149 : open("/dev/tty", O_RDWR
));
152 term_fd_needs_closing
= !(flags
& SAVE_TERM_STDIN
);
153 if (tcgetattr(term_fd
, &old_term
) < 0) {
157 sigchain_push_common(restore_term_on_signal
);
159 * If job control is disabled then the shell will have set the
160 * disposition of SIGTSTP to SIG_IGN.
162 sigaction(SIGTSTP
, NULL
, &sa
);
163 if (sa
.sa_handler
== SIG_IGN
)
166 /* avoid calling gettext() from signal handler */
167 background_resume_msg
= _("cannot resume in the background, please use 'fg' to resume");
168 restore_error_msg
= _("cannot restore terminal settings");
169 sa
.sa_handler
= restore_terminal_on_suspend
;
170 sa
.sa_flags
= SA_RESTART
;
171 sigemptyset(&sa
.sa_mask
);
172 sigaddset(&sa
.sa_mask
, SIGTSTP
);
173 sigaddset(&sa
.sa_mask
, SIGTTIN
);
174 sigaddset(&sa
.sa_mask
, SIGTTOU
);
175 sigaction(SIGTSTP
, &sa
, NULL
);
176 sigaction(SIGTTIN
, &sa
, NULL
);
177 sigaction(SIGTTOU
, &sa
, NULL
);
182 static int disable_bits(enum save_term_flags flags
, tcflag_t bits
)
186 if (save_term(flags
) < 0)
196 if (!tcsetattr(term_fd
, TCSAFLUSH
, &t
))
199 sigchain_pop_common();
205 static int disable_echo(enum save_term_flags flags
)
207 return disable_bits(flags
, ECHO
);
210 static int enable_non_canonical(enum save_term_flags flags
)
212 return disable_bits(flags
, ICANON
| ECHO
);
216 * On macos it is not possible to use poll() with a terminal so use select
219 static int getchar_with_timeout(int timeout
)
221 struct timeval tv
, *tvp
= NULL
;
227 tv
.tv_sec
= timeout
/ 1000;
228 tv
.tv_usec
= (timeout
% 1000) * 1000;
234 res
= select(1, &readfds
, NULL
, NULL
, tvp
);
246 #elif defined(GIT_WINDOWS_NATIVE)
248 #define INPUT_PATH "CONIN$"
249 #define OUTPUT_PATH "CONOUT$"
250 #define FORCE_TEXT "t"
252 static int use_stty
= 1;
253 static struct string_list stty_restore
= STRING_LIST_INIT_DUP
;
254 static HANDLE hconin
= INVALID_HANDLE_VALUE
;
255 static HANDLE hconout
= INVALID_HANDLE_VALUE
;
256 static DWORD cmode_in
, cmode_out
;
258 void restore_term(void)
262 struct child_process cp
= CHILD_PROCESS_INIT
;
264 if (stty_restore
.nr
== 0)
267 strvec_push(&cp
.args
, "stty");
268 for (i
= 0; i
< stty_restore
.nr
; i
++)
269 strvec_push(&cp
.args
, stty_restore
.items
[i
].string
);
271 string_list_clear(&stty_restore
, 0);
275 sigchain_pop_common();
277 if (hconin
== INVALID_HANDLE_VALUE
)
280 SetConsoleMode(hconin
, cmode_in
);
283 assert(hconout
!= INVALID_HANDLE_VALUE
);
284 SetConsoleMode(hconout
, cmode_out
);
285 CloseHandle(hconout
);
288 hconin
= hconout
= INVALID_HANDLE_VALUE
;
291 int save_term(enum save_term_flags flags
)
293 hconin
= CreateFileA("CONIN$", GENERIC_READ
| GENERIC_WRITE
,
294 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
295 FILE_ATTRIBUTE_NORMAL
, NULL
);
296 if (hconin
== INVALID_HANDLE_VALUE
)
299 if (flags
& SAVE_TERM_DUPLEX
) {
300 hconout
= CreateFileA("CONOUT$", GENERIC_READ
| GENERIC_WRITE
,
301 FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
,
302 FILE_ATTRIBUTE_NORMAL
, NULL
);
303 if (hconout
== INVALID_HANDLE_VALUE
)
306 GetConsoleMode(hconout
, &cmode_out
);
309 GetConsoleMode(hconin
, &cmode_in
);
311 sigchain_push_common(restore_term_on_signal
);
315 hconin
= INVALID_HANDLE_VALUE
;
319 static int disable_bits(enum save_term_flags flags
, DWORD bits
)
322 struct child_process cp
= CHILD_PROCESS_INIT
;
324 strvec_push(&cp
.args
, "stty");
326 if (bits
& ENABLE_LINE_INPUT
) {
327 string_list_append(&stty_restore
, "icanon");
329 * POSIX allows VMIN and VTIME to overlap with VEOF and
330 * VEOL - let's hope that is not the case on windows.
332 strvec_pushl(&cp
.args
, "-icanon", "min", "1", "time", "0", NULL
);
335 if (bits
& ENABLE_ECHO_INPUT
) {
336 string_list_append(&stty_restore
, "echo");
337 strvec_push(&cp
.args
, "-echo");
340 if (bits
& ENABLE_PROCESSED_INPUT
) {
341 string_list_append(&stty_restore
, "-ignbrk");
342 string_list_append(&stty_restore
, "intr");
343 string_list_append(&stty_restore
, "^c");
344 strvec_push(&cp
.args
, "ignbrk");
345 strvec_push(&cp
.args
, "intr");
346 strvec_push(&cp
.args
, "");
349 if (run_command(&cp
) == 0)
352 /* `stty` could not be executed; access the Console directly */
356 if (save_term(flags
) < 0)
359 if (!SetConsoleMode(hconin
, cmode_in
& ~bits
)) {
361 hconin
= INVALID_HANDLE_VALUE
;
362 sigchain_pop_common();
369 static int disable_echo(enum save_term_flags flags
)
371 return disable_bits(flags
, ENABLE_ECHO_INPUT
);
374 static int enable_non_canonical(enum save_term_flags flags
)
376 return disable_bits(flags
,
377 ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
| ENABLE_PROCESSED_INPUT
);
381 * Override `getchar()`, as the default implementation does not use
384 * This poses a problem when we want to see whether the standard
385 * input has more characters, as the default of Git for Windows is to start the
386 * Bash in a MinTTY, which uses a named pipe to emulate a pty, in which case
387 * our `poll()` emulation calls `PeekNamedPipe()`, which seems to require
388 * `ReadFile()` to be called first to work properly (it only reports 0
389 * available bytes, otherwise).
391 * So let's just override `getchar()` with a version backed by `ReadFile()` and
392 * go our merry ways from here.
394 static int mingw_getchar(void)
399 if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE
), &ch
, 1, &read
, NULL
))
403 error("Unexpected 0 read");
409 #define getchar mingw_getchar
411 static int getchar_with_timeout(int timeout
)
413 struct pollfd pfd
= { .fd
= 0, .events
= POLLIN
};
415 if (poll(&pfd
, 1, timeout
) < 1)
427 char *git_terminal_prompt(const char *prompt
, int echo
)
429 static struct strbuf buf
= STRBUF_INIT
;
431 FILE *input_fh
, *output_fh
;
433 input_fh
= fopen(INPUT_PATH
, "r" FORCE_TEXT
);
437 output_fh
= fopen(OUTPUT_PATH
, "w" FORCE_TEXT
);
443 if (!echo
&& disable_echo(0)) {
449 fputs(prompt
, output_fh
);
452 r
= strbuf_getline_lf(&buf
, input_fh
);
454 putc('\n', output_fh
);
468 * The `is_known_escape_sequence()` function returns 1 if the passed string
469 * corresponds to an Escape sequence that the terminal capabilities contains.
471 * To avoid depending on ncurses or other platform-specific libraries, we rely
472 * on the presence of the `infocmp` executable to do the job for us (failing
473 * silently if the program is not available or refused to run).
475 struct escape_sequence_entry
{
476 struct hashmap_entry entry
;
477 char sequence
[FLEX_ARRAY
];
480 static int sequence_entry_cmp(const void *hashmap_cmp_fn_data UNUSED
,
481 const struct escape_sequence_entry
*e1
,
482 const struct escape_sequence_entry
*e2
,
485 return strcmp(e1
->sequence
, keydata
? keydata
: e2
->sequence
);
488 static int is_known_escape_sequence(const char *sequence
)
490 static struct hashmap sequences
;
491 static int initialized
;
494 struct child_process cp
= CHILD_PROCESS_INIT
;
495 struct strbuf buf
= STRBUF_INIT
;
498 hashmap_init(&sequences
, (hashmap_cmp_fn
)sequence_entry_cmp
,
501 strvec_pushl(&cp
.args
, "infocmp", "-L", "-1", NULL
);
502 if (pipe_command(&cp
, NULL
, 0, &buf
, 0, NULL
, 0))
503 strbuf_setlen(&buf
, 0);
505 for (eol
= p
= buf
.buf
; *p
; p
= eol
+ 1) {
510 eol
= strchrnul(p
, '\n');
512 if (starts_with(p
, "\\E")) {
513 char *comma
= memchr(p
, ',', eol
- p
);
514 struct escape_sequence_entry
*e
;
518 FLEX_ALLOC_MEM(e
, sequence
, p
, comma
- p
);
519 hashmap_entry_init(&e
->entry
,
520 strhash(e
->sequence
));
521 hashmap_add(&sequences
, &e
->entry
);
529 return !!hashmap_get_from_hash(&sequences
, strhash(sequence
), sequence
);
532 int read_key_without_echo(struct strbuf
*buf
)
534 static int warning_displayed
;
537 if (warning_displayed
|| enable_non_canonical(SAVE_TERM_STDIN
) < 0) {
538 if (!warning_displayed
) {
539 warning("reading single keystrokes not supported on "
540 "this platform; reading line instead");
541 warning_displayed
= 1;
544 return strbuf_getline(buf
, stdin
);
553 strbuf_addch(buf
, ch
);
555 if (ch
== '\033' /* ESC */) {
557 * We are most likely looking at an Escape sequence. Let's try
558 * to read more bytes, waiting at most half a second, assuming
559 * that the sequence is complete if we did not receive any byte
562 * Start by replacing the Escape byte with ^[ */
563 strbuf_splice(buf
, buf
->len
- 1, 1, "^[", 2);
566 * Query the terminal capabilities once about all the Escape
567 * sequences it knows about, so that we can avoid waiting for
568 * half a second when we know that the sequence is complete.
570 while (!is_known_escape_sequence(buf
->buf
)) {
571 ch
= getchar_with_timeout(500);
574 strbuf_addch(buf
, ch
);
584 int save_term(enum save_term_flags flags
)
586 /* no duplex support available */
587 return -!!(flags
& SAVE_TERM_DUPLEX
);
590 void restore_term(void)
594 char *git_terminal_prompt(const char *prompt
, int echo
)
596 return getpass(prompt
);
599 int read_key_without_echo(struct strbuf
*buf
)
601 static int warning_displayed
;
604 if (!warning_displayed
) {
605 warning("reading single keystrokes not supported on this "
606 "platform; reading line instead");
607 warning_displayed
= 1;
614 strbuf_addstr(buf
, res
);