mingw: adjust is_console() to work with stdin
[git/raj.git] / compat / winansi.c
blob590d61cb1bc77a31a4fcecf7c405c74822c9d6fe
1 /*
2 * Copyright 2008 Peter Harris <git@peter.is-a-geek.org>
3 */
5 #undef NOGDI
6 #include "../git-compat-util.h"
7 #include <wingdi.h>
8 #include <winreg.h>
10 /* In this file, we actually want to use Windows' own isatty(). */
11 #undef isatty
14 ANSI codes used by git: m, K
16 This file is git-specific. Therefore, this file does not attempt
17 to implement any codes that are not used by git.
20 static HANDLE console;
21 static WORD plain_attr;
22 static WORD attr;
23 static int negative;
24 static int non_ascii_used = 0;
25 static HANDLE hthread, hread, hwrite;
26 static HANDLE hconsole1, hconsole2;
28 #ifdef __MINGW32__
29 #if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 5
30 typedef struct _CONSOLE_FONT_INFOEX {
31 ULONG cbSize;
32 DWORD nFont;
33 COORD dwFontSize;
34 UINT FontFamily;
35 UINT FontWeight;
36 WCHAR FaceName[LF_FACESIZE];
37 } CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
38 #endif
39 #endif
41 typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
42 PCONSOLE_FONT_INFOEX);
44 static void warn_if_raster_font(void)
46 DWORD fontFamily = 0;
47 PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
49 /* don't bother if output was ascii only */
50 if (!non_ascii_used)
51 return;
53 /* GetCurrentConsoleFontEx is available since Vista */
54 pGetCurrentConsoleFontEx = (PGETCURRENTCONSOLEFONTEX) GetProcAddress(
55 GetModuleHandle("kernel32.dll"),
56 "GetCurrentConsoleFontEx");
57 if (pGetCurrentConsoleFontEx) {
58 CONSOLE_FONT_INFOEX cfi;
59 cfi.cbSize = sizeof(cfi);
60 if (pGetCurrentConsoleFontEx(console, 0, &cfi))
61 fontFamily = cfi.FontFamily;
62 } else {
63 /* pre-Vista: check default console font in registry */
64 HKEY hkey;
65 if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CURRENT_USER, "Console",
66 0, KEY_READ, &hkey)) {
67 DWORD size = sizeof(fontFamily);
68 RegQueryValueExA(hkey, "FontFamily", NULL, NULL,
69 (LPVOID) &fontFamily, &size);
70 RegCloseKey(hkey);
74 if (!(fontFamily & TMPF_TRUETYPE)) {
75 const wchar_t *msg = L"\nWarning: Your console font probably "
76 L"doesn\'t support Unicode. If you experience strange "
77 L"characters in the output, consider switching to a "
78 L"TrueType font such as Consolas!\n";
79 DWORD dummy;
80 WriteConsoleW(console, msg, wcslen(msg), &dummy, NULL);
84 static int is_console(int fd)
86 CONSOLE_SCREEN_BUFFER_INFO sbi;
87 DWORD mode;
88 HANDLE hcon;
90 static int initialized = 0;
92 /* get OS handle of the file descriptor */
93 hcon = (HANDLE) _get_osfhandle(fd);
94 if (hcon == INVALID_HANDLE_VALUE)
95 return 0;
97 /* check if its a device (i.e. console, printer, serial port) */
98 if (GetFileType(hcon) != FILE_TYPE_CHAR)
99 return 0;
101 /* check if its a handle to a console output screen buffer */
102 if (!fd) {
103 if (!GetConsoleMode(hcon, &mode))
104 return 0;
105 } else if (!GetConsoleScreenBufferInfo(hcon, &sbi))
106 return 0;
108 /* initialize attributes */
109 if (!initialized) {
110 console = hcon;
111 attr = plain_attr = sbi.wAttributes;
112 negative = 0;
113 initialized = 1;
116 return 1;
119 #define BUFFER_SIZE 4096
120 #define MAX_PARAMS 16
122 static void write_console(unsigned char *str, size_t len)
124 /* only called from console_thread, so a static buffer will do */
125 static wchar_t wbuf[2 * BUFFER_SIZE + 1];
126 DWORD dummy;
128 /* convert utf-8 to utf-16 */
129 int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
131 /* write directly to console */
132 WriteConsoleW(console, wbuf, wlen, &dummy, NULL);
134 /* remember if non-ascii characters are printed */
135 if (wlen != len)
136 non_ascii_used = 1;
139 #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
140 #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
142 static void set_console_attr(void)
144 WORD attributes = attr;
145 if (negative) {
146 attributes &= ~FOREGROUND_ALL;
147 attributes &= ~BACKGROUND_ALL;
149 /* This could probably use a bitmask
150 instead of a series of ifs */
151 if (attr & FOREGROUND_RED)
152 attributes |= BACKGROUND_RED;
153 if (attr & FOREGROUND_GREEN)
154 attributes |= BACKGROUND_GREEN;
155 if (attr & FOREGROUND_BLUE)
156 attributes |= BACKGROUND_BLUE;
158 if (attr & BACKGROUND_RED)
159 attributes |= FOREGROUND_RED;
160 if (attr & BACKGROUND_GREEN)
161 attributes |= FOREGROUND_GREEN;
162 if (attr & BACKGROUND_BLUE)
163 attributes |= FOREGROUND_BLUE;
165 SetConsoleTextAttribute(console, attributes);
168 static void erase_in_line(void)
170 CONSOLE_SCREEN_BUFFER_INFO sbi;
171 DWORD dummy; /* Needed for Windows 7 (or Vista) regression */
173 if (!console)
174 return;
176 GetConsoleScreenBufferInfo(console, &sbi);
177 FillConsoleOutputCharacterA(console, ' ',
178 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition,
179 &dummy);
182 static void set_attr(char func, const int *params, int paramlen)
184 int i;
185 switch (func) {
186 case 'm':
187 for (i = 0; i < paramlen; i++) {
188 switch (params[i]) {
189 case 0: /* reset */
190 attr = plain_attr;
191 negative = 0;
192 break;
193 case 1: /* bold */
194 attr |= FOREGROUND_INTENSITY;
195 break;
196 case 2: /* faint */
197 case 22: /* normal */
198 attr &= ~FOREGROUND_INTENSITY;
199 break;
200 case 3: /* italic */
201 /* Unsupported */
202 break;
203 case 4: /* underline */
204 case 21: /* double underline */
205 /* Wikipedia says this flag does nothing */
206 /* Furthermore, mingw doesn't define this flag
207 attr |= COMMON_LVB_UNDERSCORE; */
208 break;
209 case 24: /* no underline */
210 /* attr &= ~COMMON_LVB_UNDERSCORE; */
211 break;
212 case 5: /* slow blink */
213 case 6: /* fast blink */
214 /* We don't have blink, but we do have
215 background intensity */
216 attr |= BACKGROUND_INTENSITY;
217 break;
218 case 25: /* no blink */
219 attr &= ~BACKGROUND_INTENSITY;
220 break;
221 case 7: /* negative */
222 negative = 1;
223 break;
224 case 27: /* positive */
225 negative = 0;
226 break;
227 case 8: /* conceal */
228 case 28: /* reveal */
229 /* Unsupported */
230 break;
231 case 30: /* Black */
232 attr &= ~FOREGROUND_ALL;
233 break;
234 case 31: /* Red */
235 attr &= ~FOREGROUND_ALL;
236 attr |= FOREGROUND_RED;
237 break;
238 case 32: /* Green */
239 attr &= ~FOREGROUND_ALL;
240 attr |= FOREGROUND_GREEN;
241 break;
242 case 33: /* Yellow */
243 attr &= ~FOREGROUND_ALL;
244 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
245 break;
246 case 34: /* Blue */
247 attr &= ~FOREGROUND_ALL;
248 attr |= FOREGROUND_BLUE;
249 break;
250 case 35: /* Magenta */
251 attr &= ~FOREGROUND_ALL;
252 attr |= FOREGROUND_RED | FOREGROUND_BLUE;
253 break;
254 case 36: /* Cyan */
255 attr &= ~FOREGROUND_ALL;
256 attr |= FOREGROUND_GREEN | FOREGROUND_BLUE;
257 break;
258 case 37: /* White */
259 attr |= FOREGROUND_RED |
260 FOREGROUND_GREEN |
261 FOREGROUND_BLUE;
262 break;
263 case 38: /* Unknown */
264 break;
265 case 39: /* reset */
266 attr &= ~FOREGROUND_ALL;
267 attr |= (plain_attr & FOREGROUND_ALL);
268 break;
269 case 40: /* Black */
270 attr &= ~BACKGROUND_ALL;
271 break;
272 case 41: /* Red */
273 attr &= ~BACKGROUND_ALL;
274 attr |= BACKGROUND_RED;
275 break;
276 case 42: /* Green */
277 attr &= ~BACKGROUND_ALL;
278 attr |= BACKGROUND_GREEN;
279 break;
280 case 43: /* Yellow */
281 attr &= ~BACKGROUND_ALL;
282 attr |= BACKGROUND_RED | BACKGROUND_GREEN;
283 break;
284 case 44: /* Blue */
285 attr &= ~BACKGROUND_ALL;
286 attr |= BACKGROUND_BLUE;
287 break;
288 case 45: /* Magenta */
289 attr &= ~BACKGROUND_ALL;
290 attr |= BACKGROUND_RED | BACKGROUND_BLUE;
291 break;
292 case 46: /* Cyan */
293 attr &= ~BACKGROUND_ALL;
294 attr |= BACKGROUND_GREEN | BACKGROUND_BLUE;
295 break;
296 case 47: /* White */
297 attr |= BACKGROUND_RED |
298 BACKGROUND_GREEN |
299 BACKGROUND_BLUE;
300 break;
301 case 48: /* Unknown */
302 break;
303 case 49: /* reset */
304 attr &= ~BACKGROUND_ALL;
305 attr |= (plain_attr & BACKGROUND_ALL);
306 break;
307 default:
308 /* Unsupported code */
309 break;
312 set_console_attr();
313 break;
314 case 'K':
315 erase_in_line();
316 break;
317 default:
318 /* Unsupported code */
319 break;
323 enum {
324 TEXT = 0, ESCAPE = 033, BRACKET = '['
327 static DWORD WINAPI console_thread(LPVOID unused)
329 unsigned char buffer[BUFFER_SIZE];
330 DWORD bytes;
331 int start, end = 0, c, parampos = 0, state = TEXT;
332 int params[MAX_PARAMS];
334 while (1) {
335 /* read next chunk of bytes from the pipe */
336 if (!ReadFile(hread, buffer + end, BUFFER_SIZE - end, &bytes,
337 NULL)) {
338 /* exit if pipe has been closed or disconnected */
339 if (GetLastError() == ERROR_PIPE_NOT_CONNECTED ||
340 GetLastError() == ERROR_BROKEN_PIPE)
341 break;
342 /* ignore other errors */
343 continue;
346 /* scan the bytes and handle ANSI control codes */
347 bytes += end;
348 start = end = 0;
349 while (end < bytes) {
350 c = buffer[end++];
351 switch (state) {
352 case TEXT:
353 if (c == ESCAPE) {
354 /* print text seen so far */
355 if (end - 1 > start)
356 write_console(buffer + start,
357 end - 1 - start);
359 /* then start parsing escape sequence */
360 start = end - 1;
361 memset(params, 0, sizeof(params));
362 parampos = 0;
363 state = ESCAPE;
365 break;
367 case ESCAPE:
368 /* continue if "\033[", otherwise bail out */
369 state = (c == BRACKET) ? BRACKET : TEXT;
370 break;
372 case BRACKET:
373 /* parse [0-9;]* into array of parameters */
374 if (c >= '0' && c <= '9') {
375 params[parampos] *= 10;
376 params[parampos] += c - '0';
377 } else if (c == ';') {
379 * next parameter, bail out if out of
380 * bounds
382 parampos++;
383 if (parampos >= MAX_PARAMS)
384 state = TEXT;
385 } else {
387 * end of escape sequence, change
388 * console attributes
390 set_attr(c, params, parampos + 1);
391 start = end;
392 state = TEXT;
394 break;
398 /* print remaining text unless parsing an escape sequence */
399 if (state == TEXT && end > start) {
400 /* check for incomplete UTF-8 sequences and fix end */
401 if (buffer[end - 1] >= 0x80) {
402 if (buffer[end -1] >= 0xc0)
403 end--;
404 else if (end - 1 > start &&
405 buffer[end - 2] >= 0xe0)
406 end -= 2;
407 else if (end - 2 > start &&
408 buffer[end - 3] >= 0xf0)
409 end -= 3;
412 /* print remaining complete UTF-8 sequences */
413 if (end > start)
414 write_console(buffer + start, end - start);
416 /* move remaining bytes to the front */
417 if (end < bytes)
418 memmove(buffer, buffer + end, bytes - end);
419 end = bytes - end;
420 } else {
421 /* all data has been consumed, mark buffer empty */
422 end = 0;
426 /* check if the console font supports unicode */
427 warn_if_raster_font();
429 CloseHandle(hread);
430 return 0;
433 static void winansi_exit(void)
435 /* flush all streams */
436 _flushall();
438 /* signal console thread to exit */
439 FlushFileBuffers(hwrite);
440 DisconnectNamedPipe(hwrite);
442 /* wait for console thread to copy remaining data */
443 WaitForSingleObject(hthread, INFINITE);
445 /* cleanup handles... */
446 CloseHandle(hwrite);
447 CloseHandle(hthread);
450 static void die_lasterr(const char *fmt, ...)
452 va_list params;
453 va_start(params, fmt);
454 errno = err_win_to_posix(GetLastError());
455 die_errno(fmt, params);
456 va_end(params);
459 static HANDLE duplicate_handle(HANDLE hnd)
461 HANDLE hresult, hproc = GetCurrentProcess();
462 if (!DuplicateHandle(hproc, hnd, hproc, &hresult, 0, TRUE,
463 DUPLICATE_SAME_ACCESS))
464 die_lasterr("DuplicateHandle(%li) failed",
465 (long) (intptr_t) hnd);
466 return hresult;
471 * Make MSVCRT's internal file descriptor control structure accessible
472 * so that we can tweak OS handles and flags directly (we need MSVCRT
473 * to treat our pipe handle as if it were a console).
475 * We assume that the ioinfo structure (exposed by MSVCRT.dll via
476 * __pioinfo) starts with the OS handle and the flags. The exact size
477 * varies between MSVCRT versions, so we try different sizes until
478 * toggling the FDEV bit of _pioinfo(1)->osflags is reflected in
479 * isatty(1).
481 typedef struct {
482 HANDLE osfhnd;
483 char osflags;
484 } ioinfo;
486 extern __declspec(dllimport) ioinfo *__pioinfo[];
488 static size_t sizeof_ioinfo = 0;
490 #define IOINFO_L2E 5
491 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
493 #define FPIPE 0x08
494 #define FDEV 0x40
496 static inline ioinfo* _pioinfo(int fd)
498 return (ioinfo*)((char*)__pioinfo[fd >> IOINFO_L2E] +
499 (fd & (IOINFO_ARRAY_ELTS - 1)) * sizeof_ioinfo);
502 static int init_sizeof_ioinfo(void)
504 int istty, wastty;
505 /* don't init twice */
506 if (sizeof_ioinfo)
507 return sizeof_ioinfo >= 256;
509 sizeof_ioinfo = sizeof(ioinfo);
510 wastty = isatty(1);
511 while (sizeof_ioinfo < 256) {
512 /* toggle FDEV flag, check isatty, then toggle back */
513 _pioinfo(1)->osflags ^= FDEV;
514 istty = isatty(1);
515 _pioinfo(1)->osflags ^= FDEV;
516 /* return if we found the correct size */
517 if (istty != wastty)
518 return 0;
519 sizeof_ioinfo += sizeof(void*);
521 error("Tweaking file descriptors doesn't work with this MSVCRT.dll");
522 return 1;
525 static HANDLE swap_osfhnd(int fd, HANDLE new_handle)
527 ioinfo *pioinfo;
528 HANDLE old_handle;
530 /* init ioinfo size if we haven't done so */
531 if (init_sizeof_ioinfo())
532 return INVALID_HANDLE_VALUE;
534 /* get ioinfo pointer and change the handles */
535 pioinfo = _pioinfo(fd);
536 old_handle = pioinfo->osfhnd;
537 pioinfo->osfhnd = new_handle;
538 return old_handle;
541 #ifdef DETECT_MSYS_TTY
543 #include <winternl.h>
544 #include <ntstatus.h>
546 static void detect_msys_tty(int fd)
548 ULONG result;
549 BYTE buffer[1024];
550 POBJECT_NAME_INFORMATION nameinfo = (POBJECT_NAME_INFORMATION) buffer;
551 PWSTR name;
553 /* check if fd is a pipe */
554 HANDLE h = (HANDLE) _get_osfhandle(fd);
555 if (GetFileType(h) != FILE_TYPE_PIPE)
556 return;
558 /* get pipe name */
559 if (!NT_SUCCESS(NtQueryObject(h, ObjectNameInformation,
560 buffer, sizeof(buffer) - 2, &result)))
561 return;
562 name = nameinfo->Name.Buffer;
563 name[nameinfo->Name.Length] = 0;
565 /* check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX') */
566 if (!wcsstr(name, L"msys-") || !wcsstr(name, L"-pty"))
567 return;
569 /* init ioinfo size if we haven't done so */
570 if (init_sizeof_ioinfo())
571 return;
573 /* set FDEV flag, reset FPIPE flag */
574 _pioinfo(fd)->osflags &= ~FPIPE;
575 _pioinfo(fd)->osflags |= FDEV;
578 #endif
580 int winansi_isatty(int fd)
582 int res = isatty(fd);
584 if (res) {
586 * Make sure that /dev/null is not fooling Git into believing
587 * that we are connected to a terminal, as "_isatty() returns a
588 * nonzero value if the descriptor is associated with a
589 * character device."; for more information, see
591 * https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
593 HANDLE handle = (HANDLE)_get_osfhandle(fd);
594 if (fd == STDIN_FILENO) {
595 DWORD dummy;
597 if (!GetConsoleMode(handle, &dummy))
598 res = 0;
599 } else if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
600 CONSOLE_SCREEN_BUFFER_INFO dummy;
602 if (!GetConsoleScreenBufferInfo(handle, &dummy))
603 res = 0;
607 return res;
610 void winansi_init(void)
612 int con1, con2;
613 char name[32];
615 /* check if either stdout or stderr is a console output screen buffer */
616 con1 = is_console(1);
617 con2 = is_console(2);
618 if (!con1 && !con2) {
619 #ifdef DETECT_MSYS_TTY
620 /* check if stdin / stdout / stderr are MSYS2 pty pipes */
621 detect_msys_tty(0);
622 detect_msys_tty(1);
623 detect_msys_tty(2);
624 #endif
625 return;
628 /* create a named pipe to communicate with the console thread */
629 xsnprintf(name, sizeof(name), "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
630 hwrite = CreateNamedPipe(name, PIPE_ACCESS_OUTBOUND,
631 PIPE_TYPE_BYTE | PIPE_WAIT, 1, BUFFER_SIZE, 0, 0, NULL);
632 if (hwrite == INVALID_HANDLE_VALUE)
633 die_lasterr("CreateNamedPipe failed");
635 hread = CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
636 if (hread == INVALID_HANDLE_VALUE)
637 die_lasterr("CreateFile for named pipe failed");
639 /* start console spool thread on the pipe's read end */
640 hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);
641 if (hthread == INVALID_HANDLE_VALUE)
642 die_lasterr("CreateThread(console_thread) failed");
644 /* schedule cleanup routine */
645 if (atexit(winansi_exit))
646 die_errno("atexit(winansi_exit) failed");
648 /* redirect stdout / stderr to the pipe */
649 if (con1)
650 hconsole1 = swap_osfhnd(1, duplicate_handle(hwrite));
651 if (con2)
652 hconsole2 = swap_osfhnd(2, duplicate_handle(hwrite));
656 * Returns the real console handle if stdout / stderr is a pipe redirecting
657 * to the console. Allows spawn / exec to pass the console to the next process.
659 HANDLE winansi_get_osfhandle(int fd)
661 HANDLE hnd = (HANDLE) _get_osfhandle(fd);
662 if (isatty(fd) && GetFileType(hnd) == FILE_TYPE_PIPE) {
663 if (fd == 1 && hconsole1)
664 return hconsole1;
665 else if (fd == 2 && hconsole2)
666 return hconsole2;
668 return hnd;