Win32: Thread-safe windows console output
[git/dscho.git] / compat / winansi.c
bloba3e4d8829536679f4d7e5fe926ea36d5dccf5ff6
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 Functions to be wrapped:
13 #undef isatty
16 ANSI codes used by git: m, K
18 This file is git-specific. Therefore, this file does not attempt
19 to implement any codes that are not used by git.
22 static HANDLE console;
23 static WORD plain_attr;
24 static WORD attr;
25 static int negative;
26 static int non_ascii_used = 0;
27 static HANDLE hthread, hread, hwrite;
28 static HANDLE hwrite1 = INVALID_HANDLE_VALUE, hwrite2 = INVALID_HANDLE_VALUE;
29 static HANDLE hconsole1, hconsole2;
31 #ifdef __MINGW32__
32 typedef struct _CONSOLE_FONT_INFOEX {
33 ULONG cbSize;
34 DWORD nFont;
35 COORD dwFontSize;
36 UINT FontFamily;
37 UINT FontWeight;
38 WCHAR FaceName[LF_FACESIZE];
39 } CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
40 #endif
42 typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
43 PCONSOLE_FONT_INFOEX);
45 static void warn_if_raster_font(void)
47 DWORD fontFamily = 0;
48 PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
50 /* don't bother if output was ascii only */
51 if (!non_ascii_used)
52 return;
54 /* GetCurrentConsoleFontEx is available since Vista */
55 pGetCurrentConsoleFontEx = (PGETCURRENTCONSOLEFONTEX) GetProcAddress(
56 GetModuleHandle("kernel32.dll"),
57 "GetCurrentConsoleFontEx");
58 if (pGetCurrentConsoleFontEx) {
59 CONSOLE_FONT_INFOEX cfi;
60 cfi.cbSize = sizeof(cfi);
61 if (pGetCurrentConsoleFontEx(console, 0, &cfi))
62 fontFamily = cfi.FontFamily;
63 } else {
64 /* pre-Vista: check default console font in registry */
65 HKEY hkey;
66 if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CURRENT_USER, "Console",
67 0, KEY_READ, &hkey)) {
68 DWORD size = sizeof(fontFamily);
69 RegQueryValueExA(hkey, "FontFamily", NULL, NULL,
70 (LPVOID) &fontFamily, &size);
71 RegCloseKey(hkey);
75 if (!(fontFamily & TMPF_TRUETYPE)) {
76 const wchar_t *msg = L"\nWarning: Your console font probably "
77 L"doesn\'t support Unicode. If you experience strange "
78 L"characters in the output, consider switching to a "
79 L"TrueType font such as Lucida Console!\n";
80 WriteConsoleW(console, msg, wcslen(msg), NULL, NULL);
84 static int is_console(int fd)
86 CONSOLE_SCREEN_BUFFER_INFO sbi;
87 HANDLE hcon;
89 static int initialized = 0;
91 /* get OS handle of the file descriptor */
92 hcon = (HANDLE) _get_osfhandle(fd);
93 if (hcon == INVALID_HANDLE_VALUE)
94 return 0;
96 /* check if its a device (i.e. console, printer, serial port) */
97 if (GetFileType(hcon) != FILE_TYPE_CHAR)
98 return 0;
100 /* check if its a handle to a console output screen buffer */
101 if (!GetConsoleScreenBufferInfo(hcon, &sbi))
102 return 0;
104 /* initialize attributes */
105 if (!initialized) {
106 attr = plain_attr = sbi.wAttributes;
107 negative = 0;
108 initialized = 1;
111 return 1;
114 #define BUFFER_SIZE 4096
115 #define MAX_PARAMS 16
117 static void write_console(unsigned char *str, size_t len)
119 /* only called from console_thread, so a static buffer will do */
120 static wchar_t wbuf[2 * BUFFER_SIZE + 1];
122 /* convert utf-8 to utf-16 */
123 int wlen = MultiByteToWideChar(CP_UTF8, 0, (char*) str, len, wbuf,
124 ARRAY_SIZE(wbuf));
126 /* write directly to console */
127 WriteConsoleW(console, wbuf, wlen, NULL, NULL);
129 /* remember if non-ascii characters are printed */
130 if (wlen != len)
131 non_ascii_used = 1;
134 #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
135 #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
137 static void set_console_attr(void)
139 WORD attributes = attr;
140 if (negative) {
141 attributes &= ~FOREGROUND_ALL;
142 attributes &= ~BACKGROUND_ALL;
144 /* This could probably use a bitmask
145 instead of a series of ifs */
146 if (attr & FOREGROUND_RED)
147 attributes |= BACKGROUND_RED;
148 if (attr & FOREGROUND_GREEN)
149 attributes |= BACKGROUND_GREEN;
150 if (attr & FOREGROUND_BLUE)
151 attributes |= BACKGROUND_BLUE;
153 if (attr & BACKGROUND_RED)
154 attributes |= FOREGROUND_RED;
155 if (attr & BACKGROUND_GREEN)
156 attributes |= FOREGROUND_GREEN;
157 if (attr & BACKGROUND_BLUE)
158 attributes |= FOREGROUND_BLUE;
160 SetConsoleTextAttribute(console, attributes);
163 static void erase_in_line(void)
165 CONSOLE_SCREEN_BUFFER_INFO sbi;
166 DWORD dummy; /* Needed for Windows 7 (or Vista) regression */
168 if (!console)
169 return;
171 GetConsoleScreenBufferInfo(console, &sbi);
172 FillConsoleOutputCharacterA(console, ' ',
173 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition,
174 &dummy);
177 static void set_attr(char func, const int *params, int paramlen)
179 int i;
180 switch (func) {
181 case 'm':
182 for (i = 0; i < paramlen; i++) {
183 switch (params[i]) {
184 case 0: /* reset */
185 attr = plain_attr;
186 negative = 0;
187 break;
188 case 1: /* bold */
189 attr |= FOREGROUND_INTENSITY;
190 break;
191 case 2: /* faint */
192 case 22: /* normal */
193 attr &= ~FOREGROUND_INTENSITY;
194 break;
195 case 3: /* italic */
196 /* Unsupported */
197 break;
198 case 4: /* underline */
199 case 21: /* double underline */
200 /* Wikipedia says this flag does nothing */
201 /* Furthermore, mingw doesn't define this flag
202 attr |= COMMON_LVB_UNDERSCORE; */
203 break;
204 case 24: /* no underline */
205 /* attr &= ~COMMON_LVB_UNDERSCORE; */
206 break;
207 case 5: /* slow blink */
208 case 6: /* fast blink */
209 /* We don't have blink, but we do have
210 background intensity */
211 attr |= BACKGROUND_INTENSITY;
212 break;
213 case 25: /* no blink */
214 attr &= ~BACKGROUND_INTENSITY;
215 break;
216 case 7: /* negative */
217 negative = 1;
218 break;
219 case 27: /* positive */
220 negative = 0;
221 break;
222 case 8: /* conceal */
223 case 28: /* reveal */
224 /* Unsupported */
225 break;
226 case 30: /* Black */
227 attr &= ~FOREGROUND_ALL;
228 break;
229 case 31: /* Red */
230 attr &= ~FOREGROUND_ALL;
231 attr |= FOREGROUND_RED;
232 break;
233 case 32: /* Green */
234 attr &= ~FOREGROUND_ALL;
235 attr |= FOREGROUND_GREEN;
236 break;
237 case 33: /* Yellow */
238 attr &= ~FOREGROUND_ALL;
239 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
240 break;
241 case 34: /* Blue */
242 attr &= ~FOREGROUND_ALL;
243 attr |= FOREGROUND_BLUE;
244 break;
245 case 35: /* Magenta */
246 attr &= ~FOREGROUND_ALL;
247 attr |= FOREGROUND_RED | FOREGROUND_BLUE;
248 break;
249 case 36: /* Cyan */
250 attr &= ~FOREGROUND_ALL;
251 attr |= FOREGROUND_GREEN | FOREGROUND_BLUE;
252 break;
253 case 37: /* White */
254 attr |= FOREGROUND_RED |
255 FOREGROUND_GREEN |
256 FOREGROUND_BLUE;
257 break;
258 case 38: /* Unknown */
259 break;
260 case 39: /* reset */
261 attr &= ~FOREGROUND_ALL;
262 attr |= (plain_attr & FOREGROUND_ALL);
263 break;
264 case 40: /* Black */
265 attr &= ~BACKGROUND_ALL;
266 break;
267 case 41: /* Red */
268 attr &= ~BACKGROUND_ALL;
269 attr |= BACKGROUND_RED;
270 break;
271 case 42: /* Green */
272 attr &= ~BACKGROUND_ALL;
273 attr |= BACKGROUND_GREEN;
274 break;
275 case 43: /* Yellow */
276 attr &= ~BACKGROUND_ALL;
277 attr |= BACKGROUND_RED | BACKGROUND_GREEN;
278 break;
279 case 44: /* Blue */
280 attr &= ~BACKGROUND_ALL;
281 attr |= BACKGROUND_BLUE;
282 break;
283 case 45: /* Magenta */
284 attr &= ~BACKGROUND_ALL;
285 attr |= BACKGROUND_RED | BACKGROUND_BLUE;
286 break;
287 case 46: /* Cyan */
288 attr &= ~BACKGROUND_ALL;
289 attr |= BACKGROUND_GREEN | BACKGROUND_BLUE;
290 break;
291 case 47: /* White */
292 attr |= BACKGROUND_RED |
293 BACKGROUND_GREEN |
294 BACKGROUND_BLUE;
295 break;
296 case 48: /* Unknown */
297 break;
298 case 49: /* reset */
299 attr &= ~BACKGROUND_ALL;
300 attr |= (plain_attr & BACKGROUND_ALL);
301 break;
302 default:
303 /* Unsupported code */
304 break;
307 set_console_attr();
308 break;
309 case 'K':
310 erase_in_line();
311 break;
312 default:
313 /* Unsupported code */
314 break;
318 enum {
319 TEXT = 0, ESCAPE = 033, BRACKET = '['
322 static DWORD WINAPI console_thread(LPVOID unused)
324 unsigned char buffer[BUFFER_SIZE];
325 DWORD bytes;
326 int start, end = 0, c, parampos = 0, state = TEXT;
327 int params[MAX_PARAMS];
329 while (1) {
330 /* read next chunk of bytes from the pipe */
331 if (!ReadFile(hread, buffer + end, BUFFER_SIZE - end, &bytes,
332 NULL)) {
333 /* exit if pipe has been closed or disconnected */
334 if (GetLastError() == ERROR_PIPE_NOT_CONNECTED ||
335 GetLastError() == ERROR_BROKEN_PIPE)
336 break;
337 /* ignore other errors */
338 continue;
341 /* scan the bytes and handle ANSI control codes */
342 bytes += end;
343 start = end = 0;
344 while (end < bytes) {
345 c = buffer[end++];
346 switch (state) {
347 case TEXT:
348 if (c == ESCAPE) {
349 /* print text seen so far */
350 if (end - 1 > start)
351 write_console(buffer + start,
352 end - 1 - start);
354 /* then start parsing escape sequence */
355 start = end - 1;
356 memset(params, 0, sizeof(params));
357 parampos = 0;
358 state = ESCAPE;
360 break;
362 case ESCAPE:
363 /* continue if "\033[", otherwise bail out */
364 state = (c == BRACKET) ? BRACKET : TEXT;
365 break;
367 case BRACKET:
368 /* parse [0-9;]* into array of parameters */
369 if (c >= '0' && c <= '9') {
370 params[parampos] *= 10;
371 params[parampos] += c - '0';
372 } else if (c == ';') {
374 * next parameter, bail out if out of
375 * bounds
377 parampos++;
378 if (parampos >= MAX_PARAMS)
379 state = TEXT;
380 } else {
382 * end of escape sequence, change
383 * console attributes
385 set_attr(c, params, parampos + 1);
386 start = end;
387 state = TEXT;
389 break;
393 /* print remaining text unless parsing an escape sequence */
394 if (state == TEXT && end > start) {
395 /* check for incomplete UTF-8 sequences and fix end */
396 if (buffer[end - 1] >= 0x80) {
397 if (buffer[end -1] >= 0xc0)
398 end--;
399 else if (end - 1 > start &&
400 buffer[end - 2] >= 0xe0)
401 end -= 2;
402 else if (end - 2 > start &&
403 buffer[end - 3] >= 0xf0)
404 end -= 3;
407 /* print remaining complete UTF-8 sequences */
408 if (end > start)
409 write_console(buffer + start, end - start);
411 /* move remaining bytes to the front */
412 if (end < bytes)
413 memmove(buffer, buffer + end, bytes - end);
414 end = bytes - end;
415 } else {
416 /* all data has been consumed, mark buffer empty */
417 end = 0;
421 /* check if the console font supports unicode */
422 warn_if_raster_font();
424 CloseHandle(hread);
425 return 0;
428 static void winansi_exit(void)
430 /* flush all streams */
431 _flushall();
433 /* signal console thread to exit */
434 FlushFileBuffers(hwrite);
435 DisconnectNamedPipe(hwrite);
437 /* wait for console thread to copy remaining data */
438 WaitForSingleObject(hthread, INFINITE);
440 /* cleanup handles... */
441 if (hwrite1 != INVALID_HANDLE_VALUE)
442 CloseHandle(hwrite1);
443 if (hwrite2 != INVALID_HANDLE_VALUE)
444 CloseHandle(hwrite2);
445 CloseHandle(hwrite);
446 CloseHandle(hthread);
449 static void die_lasterr(const char *fmt, ...)
451 va_list params;
452 va_start(params, fmt);
453 errno = err_win_to_posix(GetLastError());
454 die_errno(fmt, params);
455 va_end(params);
458 static HANDLE duplicate_handle(HANDLE hnd)
460 HANDLE hresult, hproc = GetCurrentProcess();
461 if (!DuplicateHandle(hproc, hnd, hproc, &hresult, 0, TRUE,
462 DUPLICATE_SAME_ACCESS))
463 die_lasterr("DuplicateHandle(%li) failed", (long) hnd);
464 return hresult;
467 static HANDLE redirect_console(FILE *stream, HANDLE *phcon, int new_fd)
469 /* get original console handle */
470 int fd = _fileno(stream);
471 HANDLE hcon = (HANDLE) _get_osfhandle(fd);
472 if (hcon == INVALID_HANDLE_VALUE)
473 die_errno("_get_osfhandle(%i) failed", fd);
475 /* save a copy to phcon and console (used by the background thread) */
476 console = *phcon = duplicate_handle(hcon);
478 /* duplicate new_fd over fd (closes fd and associated handle (hcon)) */
479 if (_dup2(new_fd, fd))
480 die_errno("_dup2(%i, %i) failed", new_fd, fd);
482 /* no buffering, or stdout / stderr will be out of sync */
483 setbuf(stream, NULL);
484 return (HANDLE) _get_osfhandle(fd);
487 void winansi_init(void)
489 int con1, con2, hwrite_fd;
490 char name[32];
492 /* check if either stdout or stderr is a console output screen buffer */
493 con1 = is_console(1);
494 con2 = is_console(2);
495 if (!con1 && !con2)
496 return;
498 /* create a named pipe to communicate with the console thread */
499 sprintf(name, "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
500 hwrite = CreateNamedPipe(name, PIPE_ACCESS_OUTBOUND,
501 PIPE_TYPE_BYTE | PIPE_WAIT, 1, BUFFER_SIZE, 0, 0, NULL);
502 if (hwrite == INVALID_HANDLE_VALUE)
503 die_lasterr("CreateNamedPipe failed");
505 hread = CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
506 if (hread == INVALID_HANDLE_VALUE)
507 die_lasterr("CreateFile for named pipe failed");
509 /* start console spool thread on the pipe's read end */
510 hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);
511 if (hthread == INVALID_HANDLE_VALUE)
512 die_lasterr("CreateThread(console_thread) failed");
514 /* schedule cleanup routine */
515 if (atexit(winansi_exit))
516 die_errno("atexit(winansi_exit) failed");
518 /* create a file descriptor for the write end of the pipe */
519 hwrite_fd = _open_osfhandle((long) duplicate_handle(hwrite), _O_BINARY);
520 if (hwrite_fd == -1)
521 die_errno("_open_osfhandle(%li) failed", (long) hwrite);
523 /* redirect stdout / stderr to the pipe */
524 if (con1)
525 hwrite1 = redirect_console(stdout, &hconsole1, hwrite_fd);
526 if (con2)
527 hwrite2 = redirect_console(stderr, &hconsole2, hwrite_fd);
529 /* close pipe file descriptor (also closes the duped hwrite) */
530 close(hwrite_fd);
533 static int is_same_handle(HANDLE hnd, int fd)
535 return hnd != INVALID_HANDLE_VALUE && hnd == (HANDLE) _get_osfhandle(fd);
539 * Return true if stdout / stderr is a pipe redirecting to the console.
541 int winansi_isatty(int fd)
543 if (fd == 1 && is_same_handle(hwrite1, 1))
544 return 1;
545 else if (fd == 2 && is_same_handle(hwrite2, 2))
546 return 1;
547 else
548 return isatty(fd);
552 * Returns the real console handle if stdout / stderr is a pipe redirecting
553 * to the console. Allows spawn / exec to pass the console to the next process.
555 HANDLE winansi_get_osfhandle(int fd)
557 if (fd == 1 && is_same_handle(hwrite1, 1))
558 return hconsole1;
559 else if (fd == 2 && is_same_handle(hwrite2, 2))
560 return hconsole2;
561 else
562 return (HANDLE) _get_osfhandle(fd);