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