]> git.ipfire.org Git - thirdparty/git.git/blame - compat/winansi.c
Win32: fix broken pipe detection
[thirdparty/git.git] / compat / winansi.c
CommitLineData
c09df8a7
PH
1/*
2 * Copyright 2008 Peter Harris <git@peter.is-a-geek.org>
3 */
4
1edeb9ab 5#undef NOGDI
c09df8a7 6#include "../git-compat-util.h"
1edeb9ab
KB
7#include <wingdi.h>
8#include <winreg.h>
c09df8a7 9
c09df8a7
PH
10/*
11 ANSI codes used by git: m, K
12
13 This file is git-specific. Therefore, this file does not attempt
14 to implement any codes that are not used by git.
c09df8a7
PH
15*/
16
17static HANDLE console;
18static WORD plain_attr;
19static WORD attr;
20static int negative;
eac14f89
KB
21static int non_ascii_used = 0;
22static HANDLE hthread, hread, hwrite;
23static HANDLE hwrite1 = INVALID_HANDLE_VALUE, hwrite2 = INVALID_HANDLE_VALUE;
24static HANDLE hconsole1, hconsole2;
c09df8a7 25
1edeb9ab
KB
26#ifdef __MINGW32__
27typedef 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
36
37typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
38 PCONSOLE_FONT_INFOEX);
39
eac14f89 40static void warn_if_raster_font(void)
1edeb9ab 41{
1edeb9ab
KB
42 DWORD fontFamily = 0;
43 PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
44
eac14f89
KB
45 /* don't bother if output was ascii only */
46 if (!non_ascii_used)
1edeb9ab 47 return;
1edeb9ab
KB
48
49 /* GetCurrentConsoleFontEx is available since Vista */
50 pGetCurrentConsoleFontEx = (PGETCURRENTCONSOLEFONTEX) GetProcAddress(
eac14f89
KB
51 GetModuleHandle("kernel32.dll"),
52 "GetCurrentConsoleFontEx");
1edeb9ab
KB
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;
eac14f89
KB
61 if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CURRENT_USER, "Console",
62 0, KEY_READ, &hkey)) {
1edeb9ab
KB
63 DWORD size = sizeof(fontFamily);
64 RegQueryValueExA(hkey, "FontFamily", NULL, NULL,
65 (LPVOID) &fontFamily, &size);
66 RegCloseKey(hkey);
67 }
68 }
69
eac14f89
KB
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 Consolas!\n";
75 DWORD dummy;
76 WriteConsoleW(console, msg, wcslen(msg), &dummy, NULL);
77 }
1edeb9ab
KB
78}
79
eac14f89 80static int is_console(int fd)
c09df8a7
PH
81{
82 CONSOLE_SCREEN_BUFFER_INFO sbi;
143e6152 83 HANDLE hcon;
c09df8a7
PH
84
85 static int initialized = 0;
c09df8a7 86
eac14f89
KB
87 /* get OS handle of the file descriptor */
88 hcon = (HANDLE) _get_osfhandle(fd);
143e6152
KB
89 if (hcon == INVALID_HANDLE_VALUE)
90 return 0;
91
eac14f89
KB
92 /* check if its a device (i.e. console, printer, serial port) */
93 if (GetFileType(hcon) != FILE_TYPE_CHAR)
94 return 0;
95
143e6152
KB
96 /* check if its a handle to a console output screen buffer */
97 if (!GetConsoleScreenBufferInfo(hcon, &sbi))
98 return 0;
99
eac14f89 100 /* initialize attributes */
143e6152 101 if (!initialized) {
fcd428f4 102 console = hcon;
143e6152
KB
103 attr = plain_attr = sbi.wAttributes;
104 negative = 0;
105 initialized = 1;
106 }
c09df8a7 107
143e6152 108 return 1;
c09df8a7
PH
109}
110
eac14f89
KB
111#define BUFFER_SIZE 4096
112#define MAX_PARAMS 16
113
114static void write_console(unsigned char *str, size_t len)
617ce965 115{
eac14f89
KB
116 /* only called from console_thread, so a static buffer will do */
117 static wchar_t wbuf[2 * BUFFER_SIZE + 1];
118 DWORD dummy;
617ce965 119
eac14f89
KB
120 /* convert utf-8 to utf-16 */
121 int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
617ce965 122
eac14f89
KB
123 /* write directly to console */
124 WriteConsoleW(console, wbuf, wlen, &dummy, NULL);
1edeb9ab 125
eac14f89
KB
126 /* remember if non-ascii characters are printed */
127 if (wlen != len)
128 non_ascii_used = 1;
617ce965 129}
c09df8a7
PH
130
131#define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
132#define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
133
134static void set_console_attr(void)
135{
136 WORD attributes = attr;
137 if (negative) {
138 attributes &= ~FOREGROUND_ALL;
139 attributes &= ~BACKGROUND_ALL;
140
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;
149
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;
156 }
157 SetConsoleTextAttribute(console, attributes);
158}
159
1897713f
JS
160static void erase_in_line(void)
161{
162 CONSOLE_SCREEN_BUFFER_INFO sbi;
492f7091 163 DWORD dummy; /* Needed for Windows 7 (or Vista) regression */
1897713f
JS
164
165 if (!console)
166 return;
167
168 GetConsoleScreenBufferInfo(console, &sbi);
169 FillConsoleOutputCharacterA(console, ' ',
170 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition,
492f7091 171 &dummy);
1897713f
JS
172}
173
eac14f89 174static void set_attr(char func, const int *params, int paramlen)
c09df8a7 175{
eac14f89
KB
176 int i;
177 switch (func) {
c09df8a7 178 case 'm':
eac14f89
KB
179 for (i = 0; i < paramlen; i++) {
180 switch (params[i]) {
c09df8a7
PH
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;
302 }
eac14f89 303 }
c09df8a7
PH
304 set_console_attr();
305 break;
306 case 'K':
1897713f 307 erase_in_line();
c09df8a7
PH
308 break;
309 default:
310 /* Unsupported code */
311 break;
312 }
c09df8a7
PH
313}
314
eac14f89
KB
315enum {
316 TEXT = 0, ESCAPE = 033, BRACKET = '['
317};
c09df8a7 318
eac14f89
KB
319static DWORD WINAPI console_thread(LPVOID unused)
320{
321 unsigned char buffer[BUFFER_SIZE];
322 DWORD bytes;
323 int start, end = 0, c, parampos = 0, state = TEXT;
324 int params[MAX_PARAMS];
325
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;
336 }
617ce965 337
eac14f89
KB
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);
350
351 /* then start parsing escape sequence */
352 start = end - 1;
353 memset(params, 0, sizeof(params));
354 parampos = 0;
355 state = ESCAPE;
356 }
357 break;
358
359 case ESCAPE:
360 /* continue if "\033[", otherwise bail out */
361 state = (c == BRACKET) ? BRACKET : TEXT;
362 break;
363
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 == ';') {
370 /*
371 * next parameter, bail out if out of
372 * bounds
373 */
374 parampos++;
375 if (parampos >= MAX_PARAMS)
376 state = TEXT;
377 } else {
378 /*
379 * end of escape sequence, change
380 * console attributes
381 */
382 set_attr(c, params, parampos + 1);
383 start = end;
384 state = TEXT;
385 }
386 break;
387 }
388 }
c09df8a7 389
eac14f89
KB
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;
c09df8a7
PH
402 }
403
eac14f89
KB
404 /* print remaining complete UTF-8 sequences */
405 if (end > start)
406 write_console(buffer + start, end - start);
c09df8a7 407
eac14f89
KB
408 /* move remaining bytes to the front */
409 if (end < bytes)
410 memmove(buffer, buffer + end, bytes - end);
411 end = bytes - end;
c09df8a7 412 } else {
eac14f89
KB
413 /* all data has been consumed, mark buffer empty */
414 end = 0;
c09df8a7
PH
415 }
416 }
c09df8a7 417
eac14f89
KB
418 /* check if the console font supports unicode */
419 warn_if_raster_font();
c09df8a7 420
eac14f89
KB
421 CloseHandle(hread);
422 return 0;
c09df8a7
PH
423}
424
eac14f89 425static void winansi_exit(void)
c09df8a7 426{
eac14f89
KB
427 /* flush all streams */
428 _flushall();
429
430 /* signal console thread to exit */
431 FlushFileBuffers(hwrite);
432 DisconnectNamedPipe(hwrite);
433
434 /* wait for console thread to copy remaining data */
435 WaitForSingleObject(hthread, INFINITE);
436
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);
444}
c09df8a7 445
eac14f89
KB
446static void die_lasterr(const char *fmt, ...)
447{
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);
453}
c09df8a7 454
eac14f89
KB
455static HANDLE duplicate_handle(HANDLE hnd)
456{
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;
462}
c09df8a7 463
c09df8a7 464
fcd428f4
KB
465/*
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).
469 *
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).
475 */
476typedef struct {
477 HANDLE osfhnd;
478 char osflags;
479} ioinfo;
480
481extern __declspec(dllimport) ioinfo *__pioinfo[];
c09df8a7 482
fcd428f4 483static size_t sizeof_ioinfo = 0;
c09df8a7 484
fcd428f4
KB
485#define IOINFO_L2E 5
486#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
487
488#define FDEV 0x40
489
490static inline ioinfo* _pioinfo(int fd)
491{
492 return (ioinfo*)((char*)__pioinfo[fd >> IOINFO_L2E] +
493 (fd & (IOINFO_ARRAY_ELTS - 1)) * sizeof_ioinfo);
494}
495
496static int init_sizeof_ioinfo()
497{
498 int istty, wastty;
499 /* don't init twice */
500 if (sizeof_ioinfo)
501 return sizeof_ioinfo >= 256;
502
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*);
514 }
515 error("Tweaking file descriptors doesn't work with this MSVCRT.dll");
516 return 1;
517}
518
519static HANDLE swap_osfhnd(int fd, HANDLE new_handle)
520{
521 ioinfo *pioinfo;
522 HANDLE old_handle;
523
524 /* init ioinfo size if we haven't done so */
525 if (init_sizeof_ioinfo())
526 return INVALID_HANDLE_VALUE;
527
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;
c09df8a7
PH
533}
534
eac14f89 535void winansi_init(void)
c09df8a7 536{
fcd428f4 537 int con1, con2;
eac14f89 538 char name[32];
c09df8a7 539
eac14f89
KB
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;
c09df8a7 545
eac14f89
KB
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");
552
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");
556
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");
561
562 /* schedule cleanup routine */
563 if (atexit(winansi_exit))
564 die_errno("atexit(winansi_exit) failed");
565
eac14f89
KB
566 /* redirect stdout / stderr to the pipe */
567 if (con1)
fcd428f4 568 hconsole1 = swap_osfhnd(1, hwrite1 = duplicate_handle(hwrite));
eac14f89 569 if (con2)
fcd428f4 570 hconsole2 = swap_osfhnd(2, hwrite2 = duplicate_handle(hwrite));
c09df8a7
PH
571}
572
eac14f89 573static int is_same_handle(HANDLE hnd, int fd)
c09df8a7 574{
eac14f89
KB
575 return hnd != INVALID_HANDLE_VALUE && hnd == (HANDLE) _get_osfhandle(fd);
576}
c09df8a7 577
eac14f89
KB
578/*
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.
581 */
582HANDLE winansi_get_osfhandle(int fd)
583{
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);
c09df8a7 590}