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