2 * Copyright 2008 Peter Harris <git@peter.is-a-geek.org>
6 #include "../git-compat-util.h"
10 #include "win32/lazyload.h"
12 static int fd_is_interactive[3] = { 0, 0, 0 };
13 #define FD_CONSOLE 0x1
14 #define FD_SWAPPED 0x2
18 ANSI codes used by git: m, K
20 This file is git-specific. Therefore, this file does not attempt
21 to implement any codes that are not used by git.
24 static HANDLE console;
25 static WORD plain_attr;
28 static int non_ascii_used = 0;
29 static HANDLE hthread, hread, hwrite;
30 static HANDLE hconsole1, hconsole2;
33 #if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 5
34 typedef struct _CONSOLE_FONT_INFOEX {
40 WCHAR FaceName[LF_FACESIZE];
41 } CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
45 static void warn_if_raster_font(void)
48 DECLARE_PROC_ADDR(kernel32.dll, BOOL, GetCurrentConsoleFontEx,
49 HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
51 /* don't bother if output was ascii only */
55 /* GetCurrentConsoleFontEx is available since Vista */
56 if (INIT_PROC_ADDR(GetCurrentConsoleFontEx)) {
57 CONSOLE_FONT_INFOEX cfi;
58 cfi.cbSize = sizeof(cfi);
59 if (GetCurrentConsoleFontEx(console, 0, &cfi))
60 fontFamily = cfi.FontFamily;
62 /* pre-Vista: check default console font in registry */
64 if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CURRENT_USER, "Console",
65 0, KEY_READ, &hkey)) {
66 DWORD size = sizeof(fontFamily);
67 RegQueryValueExA(hkey, "FontFamily", NULL, NULL,
68 (LPVOID) &fontFamily, &size);
73 if (!(fontFamily & TMPF_TRUETYPE)) {
74 const wchar_t *msg = L"\nWarning: Your console font probably "
75 L"doesn\'t support Unicode. If you experience strange "
76 L"characters in the output, consider switching to a "
77 L"TrueType font such as Consolas!\n";
79 WriteConsoleW(console, msg, wcslen(msg), &dummy, NULL);
83 static int is_console(int fd)
85 CONSOLE_SCREEN_BUFFER_INFO sbi;
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)
96 /* check if its a device (i.e. console, printer, serial port) */
97 if (GetFileType(hcon) != FILE_TYPE_CHAR)
100 /* check if its a handle to a console output screen buffer */
102 if (!GetConsoleMode(hcon, &mode))
105 * This code path is only reached if there is no console
106 * attached to stdout/stderr, i.e. we will not need to output
107 * any text to any console, therefore we might just as well
108 * use black as foreground color.
111 } else if (!GetConsoleScreenBufferInfo(hcon, &sbi))
114 if (fd >= 0 && fd <= 2)
115 fd_is_interactive[fd] |= FD_CONSOLE;
117 /* initialize attributes */
120 attr = plain_attr = sbi.wAttributes;
128 #define BUFFER_SIZE 4096
129 #define MAX_PARAMS 16
131 static void write_console(unsigned char *str, size_t len)
133 /* only called from console_thread, so a static buffer will do */
134 static wchar_t wbuf[2 * BUFFER_SIZE + 1];
137 /* convert utf-8 to utf-16 */
138 int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
140 wchar_t *err = L"[invalid]";
141 WriteConsoleW(console, err, wcslen(err), &dummy, NULL);
145 /* write directly to console */
146 WriteConsoleW(console, wbuf, wlen, &dummy, NULL);
148 /* remember if non-ascii characters are printed */
153 #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
154 #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
156 static void set_console_attr(void)
158 WORD attributes = attr;
160 attributes &= ~FOREGROUND_ALL;
161 attributes &= ~BACKGROUND_ALL;
163 /* This could probably use a bitmask
164 instead of a series of ifs */
165 if (attr & FOREGROUND_RED)
166 attributes |= BACKGROUND_RED;
167 if (attr & FOREGROUND_GREEN)
168 attributes |= BACKGROUND_GREEN;
169 if (attr & FOREGROUND_BLUE)
170 attributes |= BACKGROUND_BLUE;
172 if (attr & BACKGROUND_RED)
173 attributes |= FOREGROUND_RED;
174 if (attr & BACKGROUND_GREEN)
175 attributes |= FOREGROUND_GREEN;
176 if (attr & BACKGROUND_BLUE)
177 attributes |= FOREGROUND_BLUE;
179 SetConsoleTextAttribute(console, attributes);
182 static void erase_in_line(void)
184 CONSOLE_SCREEN_BUFFER_INFO sbi;
185 DWORD dummy; /* Needed for Windows 7 (or Vista) regression */
190 GetConsoleScreenBufferInfo(console, &sbi);
191 FillConsoleOutputCharacterA(console, ' ',
192 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition,
196 static void set_attr(char func, const int *params, int paramlen)
201 for (i = 0; i < paramlen; i++) {
208 attr |= FOREGROUND_INTENSITY;
211 case 22: /* normal */
212 attr &= ~FOREGROUND_INTENSITY;
217 case 4: /* underline */
218 case 21: /* double underline */
219 /* Wikipedia says this flag does nothing */
220 /* Furthermore, mingw doesn't define this flag
221 attr |= COMMON_LVB_UNDERSCORE; */
223 case 24: /* no underline */
224 /* attr &= ~COMMON_LVB_UNDERSCORE; */
226 case 5: /* slow blink */
227 case 6: /* fast blink */
228 /* We don't have blink, but we do have
229 background intensity */
230 attr |= BACKGROUND_INTENSITY;
232 case 25: /* no blink */
233 attr &= ~BACKGROUND_INTENSITY;
235 case 7: /* negative */
238 case 27: /* positive */
241 case 8: /* conceal */
242 case 28: /* reveal */
246 attr &= ~FOREGROUND_ALL;
249 attr &= ~FOREGROUND_ALL;
250 attr |= FOREGROUND_RED;
253 attr &= ~FOREGROUND_ALL;
254 attr |= FOREGROUND_GREEN;
256 case 33: /* Yellow */
257 attr &= ~FOREGROUND_ALL;
258 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
261 attr &= ~FOREGROUND_ALL;
262 attr |= FOREGROUND_BLUE;
264 case 35: /* Magenta */
265 attr &= ~FOREGROUND_ALL;
266 attr |= FOREGROUND_RED | FOREGROUND_BLUE;
269 attr &= ~FOREGROUND_ALL;
270 attr |= FOREGROUND_GREEN | FOREGROUND_BLUE;
273 attr |= FOREGROUND_RED |
277 case 38: /* Unknown */
280 attr &= ~FOREGROUND_ALL;
281 attr |= (plain_attr & FOREGROUND_ALL);
284 attr &= ~BACKGROUND_ALL;
287 attr &= ~BACKGROUND_ALL;
288 attr |= BACKGROUND_RED;
291 attr &= ~BACKGROUND_ALL;
292 attr |= BACKGROUND_GREEN;
294 case 43: /* Yellow */
295 attr &= ~BACKGROUND_ALL;
296 attr |= BACKGROUND_RED | BACKGROUND_GREEN;
299 attr &= ~BACKGROUND_ALL;
300 attr |= BACKGROUND_BLUE;
302 case 45: /* Magenta */
303 attr &= ~BACKGROUND_ALL;
304 attr |= BACKGROUND_RED | BACKGROUND_BLUE;
307 attr &= ~BACKGROUND_ALL;
308 attr |= BACKGROUND_GREEN | BACKGROUND_BLUE;
311 attr |= BACKGROUND_RED |
315 case 48: /* Unknown */
318 attr &= ~BACKGROUND_ALL;
319 attr |= (plain_attr & BACKGROUND_ALL);
322 /* Unsupported code */
332 /* Unsupported code */
338 TEXT = 0, ESCAPE = 033, BRACKET = '['
341 static DWORD WINAPI console_thread(LPVOID unused)
343 unsigned char buffer[BUFFER_SIZE];
345 int start, end = 0, c, parampos = 0, state = TEXT;
346 int params[MAX_PARAMS];
349 /* read next chunk of bytes from the pipe */
350 if (!ReadFile(hread, buffer + end, BUFFER_SIZE - end, &bytes,
352 /* exit if pipe has been closed or disconnected */
353 if (GetLastError() == ERROR_PIPE_NOT_CONNECTED ||
354 GetLastError() == ERROR_BROKEN_PIPE)
356 /* ignore other errors */
360 /* scan the bytes and handle ANSI control codes */
363 while (end < bytes) {
368 /* print text seen so far */
370 write_console(buffer + start,
373 /* then start parsing escape sequence */
375 memset(params, 0, sizeof(params));
382 /* continue if "\033[", otherwise bail out */
383 state = (c == BRACKET) ? BRACKET : TEXT;
387 /* parse [0-9;]* into array of parameters */
388 if (c >= '0' && c <= '9') {
389 params[parampos] *= 10;
390 params[parampos] += c - '0';
391 } else if (c == ';') {
393 * next parameter, bail out if out of
397 if (parampos >= MAX_PARAMS)
401 * end of escape sequence, change
404 set_attr(c, params, parampos + 1);
412 /* print remaining text unless parsing an escape sequence */
413 if (state == TEXT && end > start) {
414 /* check for incomplete UTF-8 sequences and fix end */
415 if (buffer[end - 1] >= 0x80) {
416 if (buffer[end -1] >= 0xc0)
418 else if (end - 1 > start &&
419 buffer[end - 2] >= 0xe0)
421 else if (end - 2 > start &&
422 buffer[end - 3] >= 0xf0)
426 /* print remaining complete UTF-8 sequences */
428 write_console(buffer + start, end - start);
430 /* move remaining bytes to the front */
432 memmove(buffer, buffer + end, bytes - end);
435 /* all data has been consumed, mark buffer empty */
440 /* check if the console font supports unicode */
441 warn_if_raster_font();
447 static void winansi_exit(void)
449 /* flush all streams */
452 /* signal console thread to exit */
453 FlushFileBuffers(hwrite);
454 DisconnectNamedPipe(hwrite);
456 /* wait for console thread to copy remaining data */
457 WaitForSingleObject(hthread, INFINITE);
459 /* cleanup handles... */
461 CloseHandle(hthread);
464 static void die_lasterr(const char *fmt, ...)
467 va_start(params, fmt);
468 errno = err_win_to_posix(GetLastError());
469 die_errno(fmt, params);
474 int winansi_dup2(int oldfd, int newfd)
476 int ret = dup2(oldfd, newfd);
478 if (!ret && newfd >= 0 && newfd <= 2)
479 fd_is_interactive[newfd] = oldfd < 0 || oldfd > 2 ?
480 0 : fd_is_interactive[oldfd];
485 static HANDLE duplicate_handle(HANDLE hnd)
487 HANDLE hresult, hproc = GetCurrentProcess();
488 if (!DuplicateHandle(hproc, hnd, hproc, &hresult, 0, TRUE,
489 DUPLICATE_SAME_ACCESS))
490 die_lasterr("DuplicateHandle(%li) failed",
491 (long) (intptr_t) hnd);
495 static HANDLE swap_osfhnd(int fd, HANDLE new_handle)
498 * Create a copy of the original handle associated with fd
499 * because the original will get closed when we dup2().
501 HANDLE handle = (HANDLE)_get_osfhandle(fd);
502 HANDLE duplicate = duplicate_handle(handle);
504 /* Create a temp fd associated with the already open "new_handle". */
505 int new_fd = _open_osfhandle((intptr_t)new_handle, O_BINARY);
507 assert((fd == 1) || (fd == 2));
510 * Use stock dup2() to re-bind fd to the new handle. Note that
511 * this will implicitly close(1) and close both fd=1 and the
512 * originally associated handle. It will open a new fd=1 and
513 * call DuplicateHandle() on the handle associated with new_fd.
514 * It is because of this implicit close() that we created the
515 * copy of the original.
517 * Note that we need to update the cached console handle to the
518 * duplicated one because the dup2() call will implicitly close
521 * Note that dup2() when given target := {0,1,2} will also
522 * call SetStdHandle(), so we don't need to worry about that.
524 if (console == handle)
528 /* Close the temp fd. This explicitly closes "new_handle"
529 * (because it has been associated with it).
534 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
535 fd_is_interactive[fd] |= FD_SWAPPED;
540 #ifdef DETECT_MSYS_TTY
542 #include <winternl.h>
544 #if defined(_MSC_VER)
546 typedef struct _OBJECT_NAME_INFORMATION
550 } OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION;
552 #define ObjectNameInformation 1
555 #include <ntstatus.h>
558 static void detect_msys_tty(int fd)
562 POBJECT_NAME_INFORMATION nameinfo = (POBJECT_NAME_INFORMATION) buffer;
565 /* check if fd is a pipe */
566 HANDLE h = (HANDLE) _get_osfhandle(fd);
567 if (GetFileType(h) != FILE_TYPE_PIPE)
571 if (!NT_SUCCESS(NtQueryObject(h, ObjectNameInformation,
572 buffer, sizeof(buffer) - 2, &result)))
574 name = nameinfo->Name.Buffer;
575 name[nameinfo->Name.Length / sizeof(*name)] = 0;
578 * Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX')
579 * or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX')
581 if ((!wcsstr(name, L"msys-") && !wcsstr(name, L"cygwin-")) ||
582 !wcsstr(name, L"-pty"))
586 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
587 fd_is_interactive[fd] |= FD_MSYS;
593 * Wrapper for isatty(). Most calls in the main git code
594 * call isatty(1 or 2) to see if the instance is interactive
595 * and should: be colored, show progress, paginate output.
596 * We lie and give results for what the descriptor WAS at
597 * startup (and ignore any pipe redirection we internally
601 int winansi_isatty(int fd)
603 if (fd >= 0 && fd <= 2)
604 return fd_is_interactive[fd] != 0;
608 void winansi_init(void)
613 /* check if either stdout or stderr is a console output screen buffer */
614 con1 = is_console(1);
615 con2 = is_console(2);
617 /* Also compute console bit for fd 0 even though we don't need the result here. */
620 if (!con1 && !con2) {
621 #ifdef DETECT_MSYS_TTY
622 /* check if stdin / stdout / stderr are MSYS2 pty pipes */
630 /* create a named pipe to communicate with the console thread */
631 xsnprintf(name, sizeof(name), "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
632 hwrite = CreateNamedPipe(name, PIPE_ACCESS_OUTBOUND,
633 PIPE_TYPE_BYTE | PIPE_WAIT, 1, BUFFER_SIZE, 0, 0, NULL);
634 if (hwrite == INVALID_HANDLE_VALUE)
635 die_lasterr("CreateNamedPipe failed");
637 hread = CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
638 if (hread == INVALID_HANDLE_VALUE)
639 die_lasterr("CreateFile for named pipe failed");
641 /* start console spool thread on the pipe's read end */
642 hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);
643 if (hthread == INVALID_HANDLE_VALUE)
644 die_lasterr("CreateThread(console_thread) failed");
646 /* schedule cleanup routine */
647 if (atexit(winansi_exit))
648 die_errno("atexit(winansi_exit) failed");
650 /* redirect stdout / stderr to the pipe */
652 hconsole1 = swap_osfhnd(1, duplicate_handle(hwrite));
654 hconsole2 = swap_osfhnd(2, duplicate_handle(hwrite));
658 * Returns the real console handle if stdout / stderr is a pipe redirecting
659 * to the console. Allows spawn / exec to pass the console to the next process.
661 HANDLE winansi_get_osfhandle(int fd)
663 if (fd == 1 && (fd_is_interactive[1] & FD_SWAPPED))
665 if (fd == 2 && (fd_is_interactive[2] & FD_SWAPPED))
668 return (HANDLE)_get_osfhandle(fd);