OSDN Git Service

mingw: fix colourization on Cygwin pseudo terminals
[git-core/git.git] / compat / winansi.c
1 /*
2  * Copyright 2008 Peter Harris <git@peter.is-a-geek.org>
3  */
4
5 #undef NOGDI
6 #include "../git-compat-util.h"
7 #include <wingdi.h>
8 #include <winreg.h>
9
10 /* In this file, we actually want to use Windows' own isatty(). */
11 #undef isatty
12
13 /*
14  ANSI codes used by git: m, K
15
16  This file is git-specific. Therefore, this file does not attempt
17  to implement any codes that are not used by git.
18 */
19
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;
27
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
40
41 typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
42                 PCONSOLE_FONT_INFOEX);
43
44 static void warn_if_raster_font(void)
45 {
46         DWORD fontFamily = 0;
47         PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
48
49         /* don't bother if output was ascii only */
50         if (!non_ascii_used)
51                 return;
52
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);
71                 }
72         }
73
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);
81         }
82 }
83
84 static int is_console(int fd)
85 {
86         CONSOLE_SCREEN_BUFFER_INFO sbi;
87         DWORD mode;
88         HANDLE hcon;
89
90         static int initialized = 0;
91
92         /* get OS handle of the file descriptor */
93         hcon = (HANDLE) _get_osfhandle(fd);
94         if (hcon == INVALID_HANDLE_VALUE)
95                 return 0;
96
97         /* check if its a device (i.e. console, printer, serial port) */
98         if (GetFileType(hcon) != FILE_TYPE_CHAR)
99                 return 0;
100
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;
107
108         /* initialize attributes */
109         if (!initialized) {
110                 console = hcon;
111                 attr = plain_attr = sbi.wAttributes;
112                 negative = 0;
113                 initialized = 1;
114         }
115
116         return 1;
117 }
118
119 #define BUFFER_SIZE 4096
120 #define MAX_PARAMS 16
121
122 static void write_console(unsigned char *str, size_t len)
123 {
124         /* only called from console_thread, so a static buffer will do */
125         static wchar_t wbuf[2 * BUFFER_SIZE + 1];
126         DWORD dummy;
127
128         /* convert utf-8 to utf-16 */
129         int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
130
131         /* write directly to console */
132         WriteConsoleW(console, wbuf, wlen, &dummy, NULL);
133
134         /* remember if non-ascii characters are printed */
135         if (wlen != len)
136                 non_ascii_used = 1;
137 }
138
139 #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
140 #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
141
142 static void set_console_attr(void)
143 {
144         WORD attributes = attr;
145         if (negative) {
146                 attributes &= ~FOREGROUND_ALL;
147                 attributes &= ~BACKGROUND_ALL;
148
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;
157
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;
164         }
165         SetConsoleTextAttribute(console, attributes);
166 }
167
168 static void erase_in_line(void)
169 {
170         CONSOLE_SCREEN_BUFFER_INFO sbi;
171         DWORD dummy; /* Needed for Windows 7 (or Vista) regression */
172
173         if (!console)
174                 return;
175
176         GetConsoleScreenBufferInfo(console, &sbi);
177         FillConsoleOutputCharacterA(console, ' ',
178                 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition,
179                 &dummy);
180 }
181
182 static void set_attr(char func, const int *params, int paramlen)
183 {
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;
310                         }
311                 }
312                 set_console_attr();
313                 break;
314         case 'K':
315                 erase_in_line();
316                 break;
317         default:
318                 /* Unsupported code */
319                 break;
320         }
321 }
322
323 enum {
324         TEXT = 0, ESCAPE = 033, BRACKET = '['
325 };
326
327 static DWORD WINAPI console_thread(LPVOID unused)
328 {
329         unsigned char buffer[BUFFER_SIZE];
330         DWORD bytes;
331         int start, end = 0, c, parampos = 0, state = TEXT;
332         int params[MAX_PARAMS];
333
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;
344                 }
345
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);
358
359                                         /* then start parsing escape sequence */
360                                         start = end - 1;
361                                         memset(params, 0, sizeof(params));
362                                         parampos = 0;
363                                         state = ESCAPE;
364                                 }
365                                 break;
366
367                         case ESCAPE:
368                                 /* continue if "\033[", otherwise bail out */
369                                 state = (c == BRACKET) ? BRACKET : TEXT;
370                                 break;
371
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 == ';') {
378                                         /*
379                                          * next parameter, bail out if out of
380                                          * bounds
381                                          */
382                                         parampos++;
383                                         if (parampos >= MAX_PARAMS)
384                                                 state = TEXT;
385                                 } else {
386                                         /*
387                                          * end of escape sequence, change
388                                          * console attributes
389                                          */
390                                         set_attr(c, params, parampos + 1);
391                                         start = end;
392                                         state = TEXT;
393                                 }
394                                 break;
395                         }
396                 }
397
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;
410                         }
411
412                         /* print remaining complete UTF-8 sequences */
413                         if (end > start)
414                                 write_console(buffer + start, end - start);
415
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;
423                 }
424         }
425
426         /* check if the console font supports unicode */
427         warn_if_raster_font();
428
429         CloseHandle(hread);
430         return 0;
431 }
432
433 static void winansi_exit(void)
434 {
435         /* flush all streams */
436         _flushall();
437
438         /* signal console thread to exit */
439         FlushFileBuffers(hwrite);
440         DisconnectNamedPipe(hwrite);
441
442         /* wait for console thread to copy remaining data */
443         WaitForSingleObject(hthread, INFINITE);
444
445         /* cleanup handles... */
446         CloseHandle(hwrite);
447         CloseHandle(hthread);
448 }
449
450 static void die_lasterr(const char *fmt, ...)
451 {
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);
457 }
458
459 static HANDLE duplicate_handle(HANDLE hnd)
460 {
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;
467 }
468
469
470 /*
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).
474  *
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).
480  */
481 typedef struct {
482         HANDLE osfhnd;
483         char osflags;
484 } ioinfo;
485
486 extern __declspec(dllimport) ioinfo *__pioinfo[];
487
488 static size_t sizeof_ioinfo = 0;
489
490 #define IOINFO_L2E 5
491 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
492
493 #define FPIPE 0x08
494 #define FDEV  0x40
495
496 static inline ioinfo* _pioinfo(int fd)
497 {
498         return (ioinfo*)((char*)__pioinfo[fd >> IOINFO_L2E] +
499                         (fd & (IOINFO_ARRAY_ELTS - 1)) * sizeof_ioinfo);
500 }
501
502 static int init_sizeof_ioinfo(void)
503 {
504         int istty, wastty;
505         /* don't init twice */
506         if (sizeof_ioinfo)
507                 return sizeof_ioinfo >= 256;
508
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*);
520         }
521         error("Tweaking file descriptors doesn't work with this MSVCRT.dll");
522         return 1;
523 }
524
525 static HANDLE swap_osfhnd(int fd, HANDLE new_handle)
526 {
527         ioinfo *pioinfo;
528         HANDLE old_handle;
529
530         /* init ioinfo size if we haven't done so */
531         if (init_sizeof_ioinfo())
532                 return INVALID_HANDLE_VALUE;
533
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;
539 }
540
541 #ifdef DETECT_MSYS_TTY
542
543 #include <winternl.h>
544 #include <ntstatus.h>
545
546 static void detect_msys_tty(int fd)
547 {
548         ULONG result;
549         BYTE buffer[1024];
550         POBJECT_NAME_INFORMATION nameinfo = (POBJECT_NAME_INFORMATION) buffer;
551         PWSTR name;
552
553         /* check if fd is a pipe */
554         HANDLE h = (HANDLE) _get_osfhandle(fd);
555         if (GetFileType(h) != FILE_TYPE_PIPE)
556                 return;
557
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;
564
565         /*
566          * Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX')
567          * or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX')
568          */
569         if ((!wcsstr(name, L"msys-") && !wcsstr(name, L"cygwin-")) ||
570                         !wcsstr(name, L"-pty"))
571                 return;
572
573         /* init ioinfo size if we haven't done so */
574         if (init_sizeof_ioinfo())
575                 return;
576
577         /* set FDEV flag, reset FPIPE flag */
578         _pioinfo(fd)->osflags &= ~FPIPE;
579         _pioinfo(fd)->osflags |= FDEV;
580 }
581
582 #endif
583
584 int winansi_isatty(int fd)
585 {
586         int res = isatty(fd);
587
588         if (res) {
589                 /*
590                  * Make sure that /dev/null is not fooling Git into believing
591                  * that we are connected to a terminal, as "_isatty() returns a
592                  * nonzero value if the descriptor is associated with a
593                  * character device."; for more information, see
594                  *
595                  * https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
596                  */
597                 HANDLE handle = (HANDLE)_get_osfhandle(fd);
598                 if (fd == STDIN_FILENO) {
599                         DWORD dummy;
600
601                         if (!GetConsoleMode(handle, &dummy))
602                                 res = 0;
603                 } else if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
604                         CONSOLE_SCREEN_BUFFER_INFO dummy;
605
606                         if (!GetConsoleScreenBufferInfo(handle, &dummy))
607                                 res = 0;
608                 }
609         }
610
611         return res;
612 }
613
614 void winansi_init(void)
615 {
616         int con1, con2;
617         char name[32];
618
619         /* check if either stdout or stderr is a console output screen buffer */
620         con1 = is_console(1);
621         con2 = is_console(2);
622         if (!con1 && !con2) {
623 #ifdef DETECT_MSYS_TTY
624                 /* check if stdin / stdout / stderr are MSYS2 pty pipes */
625                 detect_msys_tty(0);
626                 detect_msys_tty(1);
627                 detect_msys_tty(2);
628 #endif
629                 return;
630         }
631
632         /* create a named pipe to communicate with the console thread */
633         xsnprintf(name, sizeof(name), "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
634         hwrite = CreateNamedPipe(name, PIPE_ACCESS_OUTBOUND,
635                 PIPE_TYPE_BYTE | PIPE_WAIT, 1, BUFFER_SIZE, 0, 0, NULL);
636         if (hwrite == INVALID_HANDLE_VALUE)
637                 die_lasterr("CreateNamedPipe failed");
638
639         hread = CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
640         if (hread == INVALID_HANDLE_VALUE)
641                 die_lasterr("CreateFile for named pipe failed");
642
643         /* start console spool thread on the pipe's read end */
644         hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);
645         if (hthread == INVALID_HANDLE_VALUE)
646                 die_lasterr("CreateThread(console_thread) failed");
647
648         /* schedule cleanup routine */
649         if (atexit(winansi_exit))
650                 die_errno("atexit(winansi_exit) failed");
651
652         /* redirect stdout / stderr to the pipe */
653         if (con1)
654                 hconsole1 = swap_osfhnd(1, duplicate_handle(hwrite));
655         if (con2)
656                 hconsole2 = swap_osfhnd(2, duplicate_handle(hwrite));
657 }
658
659 /*
660  * Returns the real console handle if stdout / stderr is a pipe redirecting
661  * to the console. Allows spawn / exec to pass the console to the next process.
662  */
663 HANDLE winansi_get_osfhandle(int fd)
664 {
665         HANDLE hnd = (HANDLE) _get_osfhandle(fd);
666         if (isatty(fd) && GetFileType(hnd) == FILE_TYPE_PIPE) {
667                 if (fd == 1 && hconsole1)
668                         return hconsole1;
669                 else if (fd == 2 && hconsole2)
670                         return hconsole2;
671         }
672         return hnd;
673 }