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