The sixth batch for 2.26
[git.git] / compat / terminal.c
blob35bca03d1470601568d17ed3cf71c5c622d43d87
1 #include "git-compat-util.h"
2 #include "compat/terminal.h"
3 #include "sigchain.h"
4 #include "strbuf.h"
5 #include "run-command.h"
6 #include "string-list.h"
7 #include "hashmap.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)
15 restore_term();
16 sigchain_pop(sig);
17 raise(sig);
20 #ifdef HAVE_DEV_TTY
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)
30 if (term_fd < 0)
31 return;
33 tcsetattr(term_fd, TCSAFLUSH, &old_term);
34 close(term_fd);
35 term_fd = -1;
38 static int disable_bits(tcflag_t bits)
40 struct termios t;
42 term_fd = open("/dev/tty", O_RDWR);
43 if (tcgetattr(term_fd, &t) < 0)
44 goto error;
46 old_term = t;
47 sigchain_push_common(restore_term_on_signal);
49 t.c_lflag &= ~bits;
50 if (!tcsetattr(term_fd, TCSAFLUSH, &t))
51 return 0;
53 error:
54 close(term_fd);
55 term_fd = -1;
56 return -1;
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;
78 static DWORD cmode;
80 static void restore_term(void)
82 if (use_stty) {
83 int i;
84 struct child_process cp = CHILD_PROCESS_INIT;
86 if (stty_restore.nr == 0)
87 return;
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);
92 run_command(&cp);
93 string_list_clear(&stty_restore, 0);
94 return;
97 if (hconin == INVALID_HANDLE_VALUE)
98 return;
100 SetConsoleMode(hconin, cmode);
101 CloseHandle(hconin);
102 hconin = INVALID_HANDLE_VALUE;
105 static int disable_bits(DWORD bits)
107 if (use_stty) {
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)
132 return 0;
134 /* `stty` could not be executed; access the Console directly */
135 use_stty = 0;
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)
142 return -1;
144 GetConsoleMode(hconin, &cmode);
145 sigchain_push_common(restore_term_on_signal);
146 if (!SetConsoleMode(hconin, cmode & ~bits)) {
147 CloseHandle(hconin);
148 hconin = INVALID_HANDLE_VALUE;
149 return -1;
152 return 0;
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
167 * `ReadFile()`.
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)
181 DWORD read = 0;
182 unsigned char ch;
184 if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE), &ch, 1, &read, NULL))
185 return EOF;
187 if (!read) {
188 error("Unexpected 0 read");
189 return EOF;
192 return ch;
194 #define getchar mingw_getchar
196 #endif
198 #ifndef FORCE_TEXT
199 #define FORCE_TEXT
200 #endif
202 char *git_terminal_prompt(const char *prompt, int echo)
204 static struct strbuf buf = STRBUF_INIT;
205 int r;
206 FILE *input_fh, *output_fh;
208 input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT);
209 if (!input_fh)
210 return NULL;
212 output_fh = fopen(OUTPUT_PATH, "w" FORCE_TEXT);
213 if (!output_fh) {
214 fclose(input_fh);
215 return NULL;
218 if (!echo && disable_echo()) {
219 fclose(input_fh);
220 fclose(output_fh);
221 return NULL;
224 fputs(prompt, output_fh);
225 fflush(output_fh);
227 r = strbuf_getline_lf(&buf, input_fh);
228 if (!echo) {
229 putc('\n', output_fh);
230 fflush(output_fh);
233 restore_term();
234 fclose(input_fh);
235 fclose(output_fh);
237 if (r == EOF)
238 return NULL;
239 return buf.buf;
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,
258 const void *keydata)
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;
268 if (!initialized) {
269 struct child_process cp = CHILD_PROCESS_INIT;
270 struct strbuf buf = STRBUF_INIT;
271 char *p, *eol;
273 hashmap_init(&sequences, (hashmap_cmp_fn)sequence_entry_cmp,
274 NULL, 0);
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) {
281 p = strchr(p, '=');
282 if (!p)
283 break;
284 p++;
285 eol = strchrnul(p, '\n');
287 if (starts_with(p, "\\E")) {
288 char *comma = memchr(p, ',', eol - p);
289 struct escape_sequence_entry *e;
291 p[0] = '^';
292 p[1] = '[';
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);
298 if (!*eol)
299 break;
301 initialized = 1;
304 return !!hashmap_get_from_hash(&sequences, strhash(sequence), sequence);
307 int read_key_without_echo(struct strbuf *buf)
309 static int warning_displayed;
310 int ch;
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);
322 strbuf_reset(buf);
323 ch = getchar();
324 if (ch == EOF) {
325 restore_term();
326 return EOF;
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
335 * within that time.
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)
349 break;
351 ch = getchar();
352 if (ch == EOF)
353 return 0;
354 strbuf_addch(buf, ch);
358 restore_term();
359 return 0;
362 #else
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;
372 const char *res;
374 if (!warning_displayed) {
375 warning("reading single keystrokes not supported on this "
376 "platform; reading line instead");
377 warning_displayed = 1;
380 res = getpass("");
381 strbuf_reset(buf);
382 if (!res)
383 return EOF;
384 strbuf_addstr(buf, res);
385 return 0;
388 #endif