Merge 'home' into HEAD
[git/mingw/4msysgit.git] / compat / winansi.c
blob13bc28dd336de7bf0e63226008c3a303381dc7fc
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>
11 ANSI codes used by git: m, K
13 This file is git-specific. Therefore, this file does not attempt
14 to implement any codes that are not used by git.
17 static HANDLE console;
18 static WORD plain_attr;
19 static WORD attr;
20 static int negative;
21 static int non_ascii_used = 0;
22 static HANDLE hthread, hread, hwrite;
23 static HANDLE hwrite1 = INVALID_HANDLE_VALUE, hwrite2 = INVALID_HANDLE_VALUE;
24 static HANDLE hconsole1, hconsole2;
26 #ifdef __MINGW32__
27 typedef struct _CONSOLE_FONT_INFOEX {
28 ULONG cbSize;
29 DWORD nFont;
30 COORD dwFontSize;
31 UINT FontFamily;
32 UINT FontWeight;
33 WCHAR FaceName[LF_FACESIZE];
34 } CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
35 #endif
37 typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
38 PCONSOLE_FONT_INFOEX);
40 static void warn_if_raster_font(void)
42 DWORD fontFamily = 0;
43 PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
45 /* don't bother if output was ascii only */
46 if (!non_ascii_used)
47 return;
49 /* GetCurrentConsoleFontEx is available since Vista */
50 pGetCurrentConsoleFontEx = (PGETCURRENTCONSOLEFONTEX) GetProcAddress(
51 GetModuleHandle("kernel32.dll"),
52 "GetCurrentConsoleFontEx");
53 if (pGetCurrentConsoleFontEx) {
54 CONSOLE_FONT_INFOEX cfi;
55 cfi.cbSize = sizeof(cfi);
56 if (pGetCurrentConsoleFontEx(console, 0, &cfi))
57 fontFamily = cfi.FontFamily;
58 } else {
59 /* pre-Vista: check default console font in registry */
60 HKEY hkey;
61 if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CURRENT_USER, "Console",
62 0, KEY_READ, &hkey)) {
63 DWORD size = sizeof(fontFamily);
64 RegQueryValueExA(hkey, "FontFamily", NULL, NULL,
65 (LPVOID) &fontFamily, &size);
66 RegCloseKey(hkey);
70 if (!(fontFamily & TMPF_TRUETYPE)) {
71 const wchar_t *msg = L"\nWarning: Your console font probably "
72 L"doesn\'t support Unicode. If you experience strange "
73 L"characters in the output, consider switching to a "
74 L"TrueType font such as Lucida Console!\n";
75 DWORD dummy;
76 WriteConsoleW(console, msg, wcslen(msg), &dummy, NULL);
80 static int is_console(int fd)
82 CONSOLE_SCREEN_BUFFER_INFO sbi;
83 HANDLE hcon;
85 static int initialized = 0;
87 /* get OS handle of the file descriptor */
88 hcon = (HANDLE) _get_osfhandle(fd);
89 if (hcon == INVALID_HANDLE_VALUE)
90 return 0;
92 /* check if its a device (i.e. console, printer, serial port) */
93 if (GetFileType(hcon) != FILE_TYPE_CHAR)
94 return 0;
96 /* check if its a handle to a console output screen buffer */
97 if (!GetConsoleScreenBufferInfo(hcon, &sbi))
98 return 0;
100 /* initialize attributes */
101 if (!initialized) {
102 console = hcon;
103 attr = plain_attr = sbi.wAttributes;
104 negative = 0;
105 initialized = 1;
108 return 1;
111 #define BUFFER_SIZE 4096
112 #define MAX_PARAMS 16
114 static void write_console(unsigned char *str, size_t len)
116 /* only called from console_thread, so a static buffer will do */
117 static wchar_t wbuf[2 * BUFFER_SIZE + 1];
118 DWORD dummy;
120 /* convert utf-8 to utf-16 */
121 int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
123 /* write directly to console */
124 WriteConsoleW(console, wbuf, wlen, &dummy, NULL);
126 /* remember if non-ascii characters are printed */
127 if (wlen != len)
128 non_ascii_used = 1;
131 #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
132 #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
134 static void set_console_attr(void)
136 WORD attributes = attr;
137 if (negative) {
138 attributes &= ~FOREGROUND_ALL;
139 attributes &= ~BACKGROUND_ALL;
141 /* This could probably use a bitmask
142 instead of a series of ifs */
143 if (attr & FOREGROUND_RED)
144 attributes |= BACKGROUND_RED;
145 if (attr & FOREGROUND_GREEN)
146 attributes |= BACKGROUND_GREEN;
147 if (attr & FOREGROUND_BLUE)
148 attributes |= BACKGROUND_BLUE;
150 if (attr & BACKGROUND_RED)
151 attributes |= FOREGROUND_RED;
152 if (attr & BACKGROUND_GREEN)
153 attributes |= FOREGROUND_GREEN;
154 if (attr & BACKGROUND_BLUE)
155 attributes |= FOREGROUND_BLUE;
157 SetConsoleTextAttribute(console, attributes);
160 static void erase_in_line(void)
162 CONSOLE_SCREEN_BUFFER_INFO sbi;
163 DWORD dummy; /* Needed for Windows 7 (or Vista) regression */
165 if (!console)
166 return;
168 GetConsoleScreenBufferInfo(console, &sbi);
169 FillConsoleOutputCharacterA(console, ' ',
170 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition,
171 &dummy);
174 static void set_attr(char func, const int *params, int paramlen)
176 int i;
177 switch (func) {
178 case 'm':
179 for (i = 0; i < paramlen; i++) {
180 switch (params[i]) {
181 case 0: /* reset */
182 attr = plain_attr;
183 negative = 0;
184 break;
185 case 1: /* bold */
186 attr |= FOREGROUND_INTENSITY;
187 break;
188 case 2: /* faint */
189 case 22: /* normal */
190 attr &= ~FOREGROUND_INTENSITY;
191 break;
192 case 3: /* italic */
193 /* Unsupported */
194 break;
195 case 4: /* underline */
196 case 21: /* double underline */
197 /* Wikipedia says this flag does nothing */
198 /* Furthermore, mingw doesn't define this flag
199 attr |= COMMON_LVB_UNDERSCORE; */
200 break;
201 case 24: /* no underline */
202 /* attr &= ~COMMON_LVB_UNDERSCORE; */
203 break;
204 case 5: /* slow blink */
205 case 6: /* fast blink */
206 /* We don't have blink, but we do have
207 background intensity */
208 attr |= BACKGROUND_INTENSITY;
209 break;
210 case 25: /* no blink */
211 attr &= ~BACKGROUND_INTENSITY;
212 break;
213 case 7: /* negative */
214 negative = 1;
215 break;
216 case 27: /* positive */
217 negative = 0;
218 break;
219 case 8: /* conceal */
220 case 28: /* reveal */
221 /* Unsupported */
222 break;
223 case 30: /* Black */
224 attr &= ~FOREGROUND_ALL;
225 break;
226 case 31: /* Red */
227 attr &= ~FOREGROUND_ALL;
228 attr |= FOREGROUND_RED;
229 break;
230 case 32: /* Green */
231 attr &= ~FOREGROUND_ALL;
232 attr |= FOREGROUND_GREEN;
233 break;
234 case 33: /* Yellow */
235 attr &= ~FOREGROUND_ALL;
236 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
237 break;
238 case 34: /* Blue */
239 attr &= ~FOREGROUND_ALL;
240 attr |= FOREGROUND_BLUE;
241 break;
242 case 35: /* Magenta */
243 attr &= ~FOREGROUND_ALL;
244 attr |= FOREGROUND_RED | FOREGROUND_BLUE;
245 break;
246 case 36: /* Cyan */
247 attr &= ~FOREGROUND_ALL;
248 attr |= FOREGROUND_GREEN | FOREGROUND_BLUE;
249 break;
250 case 37: /* White */
251 attr |= FOREGROUND_RED |
252 FOREGROUND_GREEN |
253 FOREGROUND_BLUE;
254 break;
255 case 38: /* Unknown */
256 break;
257 case 39: /* reset */
258 attr &= ~FOREGROUND_ALL;
259 attr |= (plain_attr & FOREGROUND_ALL);
260 break;
261 case 40: /* Black */
262 attr &= ~BACKGROUND_ALL;
263 break;
264 case 41: /* Red */
265 attr &= ~BACKGROUND_ALL;
266 attr |= BACKGROUND_RED;
267 break;
268 case 42: /* Green */
269 attr &= ~BACKGROUND_ALL;
270 attr |= BACKGROUND_GREEN;
271 break;
272 case 43: /* Yellow */
273 attr &= ~BACKGROUND_ALL;
274 attr |= BACKGROUND_RED | BACKGROUND_GREEN;
275 break;
276 case 44: /* Blue */
277 attr &= ~BACKGROUND_ALL;
278 attr |= BACKGROUND_BLUE;
279 break;
280 case 45: /* Magenta */
281 attr &= ~BACKGROUND_ALL;
282 attr |= BACKGROUND_RED | BACKGROUND_BLUE;
283 break;
284 case 46: /* Cyan */
285 attr &= ~BACKGROUND_ALL;
286 attr |= BACKGROUND_GREEN | BACKGROUND_BLUE;
287 break;
288 case 47: /* White */
289 attr |= BACKGROUND_RED |
290 BACKGROUND_GREEN |
291 BACKGROUND_BLUE;
292 break;
293 case 48: /* Unknown */
294 break;
295 case 49: /* reset */
296 attr &= ~BACKGROUND_ALL;
297 attr |= (plain_attr & BACKGROUND_ALL);
298 break;
299 default:
300 /* Unsupported code */
301 break;
304 set_console_attr();
305 break;
306 case 'K':
307 erase_in_line();
308 break;
309 default:
310 /* Unsupported code */
311 break;
315 enum {
316 TEXT = 0, ESCAPE = 033, BRACKET = '['
319 static DWORD WINAPI console_thread(LPVOID unused)
321 unsigned char buffer[BUFFER_SIZE];
322 DWORD bytes;
323 int start, end = 0, c, parampos = 0, state = TEXT;
324 int params[MAX_PARAMS];
326 while (1) {
327 /* read next chunk of bytes from the pipe */
328 if (!ReadFile(hread, buffer + end, BUFFER_SIZE - end, &bytes,
329 NULL)) {
330 /* exit if pipe has been closed or disconnected */
331 if (GetLastError() == ERROR_PIPE_NOT_CONNECTED ||
332 GetLastError() == ERROR_BROKEN_PIPE)
333 break;
334 /* ignore other errors */
335 continue;
338 /* scan the bytes and handle ANSI control codes */
339 bytes += end;
340 start = end = 0;
341 while (end < bytes) {
342 c = buffer[end++];
343 switch (state) {
344 case TEXT:
345 if (c == ESCAPE) {
346 /* print text seen so far */
347 if (end - 1 > start)
348 write_console(buffer + start,
349 end - 1 - start);
351 /* then start parsing escape sequence */
352 start = end - 1;
353 memset(params, 0, sizeof(params));
354 parampos = 0;
355 state = ESCAPE;
357 break;
359 case ESCAPE:
360 /* continue if "\033[", otherwise bail out */
361 state = (c == BRACKET) ? BRACKET : TEXT;
362 break;
364 case BRACKET:
365 /* parse [0-9;]* into array of parameters */
366 if (c >= '0' && c <= '9') {
367 params[parampos] *= 10;
368 params[parampos] += c - '0';
369 } else if (c == ';') {
371 * next parameter, bail out if out of
372 * bounds
374 parampos++;
375 if (parampos >= MAX_PARAMS)
376 state = TEXT;
377 } else {
379 * end of escape sequence, change
380 * console attributes
382 set_attr(c, params, parampos + 1);
383 start = end;
384 state = TEXT;
386 break;
390 /* print remaining text unless parsing an escape sequence */
391 if (state == TEXT && end > start) {
392 /* check for incomplete UTF-8 sequences and fix end */
393 if (buffer[end - 1] >= 0x80) {
394 if (buffer[end -1] >= 0xc0)
395 end--;
396 else if (end - 1 > start &&
397 buffer[end - 2] >= 0xe0)
398 end -= 2;
399 else if (end - 2 > start &&
400 buffer[end - 3] >= 0xf0)
401 end -= 3;
404 /* print remaining complete UTF-8 sequences */
405 if (end > start)
406 write_console(buffer + start, end - start);
408 /* move remaining bytes to the front */
409 if (end < bytes)
410 memmove(buffer, buffer + end, bytes - end);
411 end = bytes - end;
412 } else {
413 /* all data has been consumed, mark buffer empty */
414 end = 0;
418 /* check if the console font supports unicode */
419 warn_if_raster_font();
421 CloseHandle(hread);
422 return 0;
425 static void winansi_exit(void)
427 /* flush all streams */
428 _flushall();
430 /* signal console thread to exit */
431 FlushFileBuffers(hwrite);
432 DisconnectNamedPipe(hwrite);
434 /* wait for console thread to copy remaining data */
435 WaitForSingleObject(hthread, INFINITE);
437 /* cleanup handles... */
438 if (hwrite1 != INVALID_HANDLE_VALUE)
439 CloseHandle(hwrite1);
440 if (hwrite2 != INVALID_HANDLE_VALUE)
441 CloseHandle(hwrite2);
442 CloseHandle(hwrite);
443 CloseHandle(hthread);
446 static void die_lasterr(const char *fmt, ...)
448 va_list params;
449 va_start(params, fmt);
450 errno = err_win_to_posix(GetLastError());
451 die_errno(fmt, params);
452 va_end(params);
455 static HANDLE duplicate_handle(HANDLE hnd)
457 HANDLE hresult, hproc = GetCurrentProcess();
458 if (!DuplicateHandle(hproc, hnd, hproc, &hresult, 0, TRUE,
459 DUPLICATE_SAME_ACCESS))
460 die_lasterr("DuplicateHandle(%li) failed", (long) hnd);
461 return hresult;
466 * Make MSVCRT's internal file descriptor control structure accessible
467 * so that we can tweak OS handles and flags directly (we need MSVCRT
468 * to treat our pipe handle as if it were a console).
470 * We assume that the ioinfo structure (exposed by MSVCRT.dll via
471 * __pioinfo) starts with the OS handle and the flags. The exact size
472 * varies between MSVCRT versions, so we try different sizes until
473 * toggling the FDEV bit of _pioinfo(1)->osflags is reflected in
474 * isatty(1).
476 typedef struct {
477 HANDLE osfhnd;
478 char osflags;
479 } ioinfo;
481 extern __declspec(dllimport) ioinfo *__pioinfo[];
483 static size_t sizeof_ioinfo = 0;
485 #define IOINFO_L2E 5
486 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
488 #define FDEV 0x40
490 static inline ioinfo* _pioinfo(int fd)
492 return (ioinfo*)((char*)__pioinfo[fd >> IOINFO_L2E] +
493 (fd & (IOINFO_ARRAY_ELTS - 1)) * sizeof_ioinfo);
496 static int init_sizeof_ioinfo()
498 int istty, wastty;
499 /* don't init twice */
500 if (sizeof_ioinfo)
501 return sizeof_ioinfo >= 256;
503 sizeof_ioinfo = sizeof(ioinfo);
504 wastty = isatty(1);
505 while (sizeof_ioinfo < 256) {
506 /* toggle FDEV flag, check isatty, then toggle back */
507 _pioinfo(1)->osflags ^= FDEV;
508 istty = isatty(1);
509 _pioinfo(1)->osflags ^= FDEV;
510 /* return if we found the correct size */
511 if (istty != wastty)
512 return 0;
513 sizeof_ioinfo += sizeof(void*);
515 error("Tweaking file descriptors doesn't work with this MSVCRT.dll");
516 return 1;
519 static HANDLE swap_osfhnd(int fd, HANDLE new_handle)
521 ioinfo *pioinfo;
522 HANDLE old_handle;
524 /* init ioinfo size if we haven't done so */
525 if (init_sizeof_ioinfo())
526 return INVALID_HANDLE_VALUE;
528 /* get ioinfo pointer and change the handles */
529 pioinfo = _pioinfo(fd);
530 old_handle = pioinfo->osfhnd;
531 pioinfo->osfhnd = new_handle;
532 return old_handle;
535 void winansi_init(void)
537 int con1, con2;
538 char name[32];
540 /* check if either stdout or stderr is a console output screen buffer */
541 con1 = is_console(1);
542 con2 = is_console(2);
543 if (!con1 && !con2)
544 return;
546 /* create a named pipe to communicate with the console thread */
547 sprintf(name, "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
548 hwrite = CreateNamedPipe(name, PIPE_ACCESS_OUTBOUND,
549 PIPE_TYPE_BYTE | PIPE_WAIT, 1, BUFFER_SIZE, 0, 0, NULL);
550 if (hwrite == INVALID_HANDLE_VALUE)
551 die_lasterr("CreateNamedPipe failed");
553 hread = CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
554 if (hread == INVALID_HANDLE_VALUE)
555 die_lasterr("CreateFile for named pipe failed");
557 /* start console spool thread on the pipe's read end */
558 hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);
559 if (hthread == INVALID_HANDLE_VALUE)
560 die_lasterr("CreateThread(console_thread) failed");
562 /* schedule cleanup routine */
563 if (atexit(winansi_exit))
564 die_errno("atexit(winansi_exit) failed");
566 /* redirect stdout / stderr to the pipe */
567 if (con1)
568 hconsole1 = swap_osfhnd(1, hwrite1 = duplicate_handle(hwrite));
569 if (con2)
570 hconsole2 = swap_osfhnd(2, hwrite2 = duplicate_handle(hwrite));
573 static int is_same_handle(HANDLE hnd, int fd)
575 return hnd != INVALID_HANDLE_VALUE && hnd == (HANDLE) _get_osfhandle(fd);
579 * Returns the real console handle if stdout / stderr is a pipe redirecting
580 * to the console. Allows spawn / exec to pass the console to the next process.
582 HANDLE winansi_get_osfhandle(int fd)
584 if (fd == 1 && is_same_handle(hwrite1, 1))
585 return hconsole1;
586 else if (fd == 2 && is_same_handle(hwrite2, 2))
587 return hconsole2;
588 else
589 return (HANDLE) _get_osfhandle(fd);