2 * Copyright 2008 Peter Harris <git@peter.is-a-geek.org>
6 #include "../git-compat-util.h"
11 static int fd_is_interactive[3] = { 0, 0, 0 };
12 #define FD_CONSOLE 0x1
13 #define FD_SWAPPED 0x2
17 ANSI codes used by git: m, K
19 This file is git-specific. Therefore, this file does not attempt
20 to implement any codes that are not used by git.
23 static HANDLE console;
24 static WORD plain_attr;
27 static int non_ascii_used = 0;
28 static HANDLE hthread, hread, hwrite;
29 static HANDLE hconsole1, hconsole2;
32 #if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 5
33 typedef struct _CONSOLE_FONT_INFOEX {
39 WCHAR FaceName[LF_FACESIZE];
40 } CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
44 typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
45 PCONSOLE_FONT_INFOEX);
47 static void warn_if_raster_font(void)
50 PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
52 /* don't bother if output was ascii only */
56 /* GetCurrentConsoleFontEx is available since Vista */
57 pGetCurrentConsoleFontEx = (PGETCURRENTCONSOLEFONTEX) GetProcAddress(
58 GetModuleHandle("kernel32.dll"),
59 "GetCurrentConsoleFontEx");
60 if (pGetCurrentConsoleFontEx) {
61 CONSOLE_FONT_INFOEX cfi;
62 cfi.cbSize = sizeof(cfi);
63 if (pGetCurrentConsoleFontEx(console, 0, &cfi))
64 fontFamily = cfi.FontFamily;
66 /* pre-Vista: check default console font in registry */
68 if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CURRENT_USER, "Console",
69 0, KEY_READ, &hkey)) {
70 DWORD size = sizeof(fontFamily);
71 RegQueryValueExA(hkey, "FontFamily", NULL, NULL,
72 (LPVOID) &fontFamily, &size);
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";
83 WriteConsoleW(console, msg, wcslen(msg), &dummy, NULL);
87 static int is_console(int fd)
89 CONSOLE_SCREEN_BUFFER_INFO sbi;
93 static int initialized = 0;
95 /* get OS handle of the file descriptor */
96 hcon = (HANDLE) _get_osfhandle(fd);
97 if (hcon == INVALID_HANDLE_VALUE)
100 /* check if its a device (i.e. console, printer, serial port) */
101 if (GetFileType(hcon) != FILE_TYPE_CHAR)
104 /* check if its a handle to a console output screen buffer */
106 if (!GetConsoleMode(hcon, &mode))
109 * This code path is only reached if there is no console
110 * attached to stdout/stderr, i.e. we will not need to output
111 * any text to any console, therefore we might just as well
112 * use black as foreground color.
115 } else if (!GetConsoleScreenBufferInfo(hcon, &sbi))
118 if (fd >= 0 && fd <= 2)
119 fd_is_interactive[fd] |= FD_CONSOLE;
121 /* initialize attributes */
124 attr = plain_attr = sbi.wAttributes;
132 #define BUFFER_SIZE 4096
133 #define MAX_PARAMS 16
135 static void write_console(unsigned char *str, size_t len)
137 /* only called from console_thread, so a static buffer will do */
138 static wchar_t wbuf[2 * BUFFER_SIZE + 1];
141 /* convert utf-8 to utf-16 */
142 int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
144 wchar_t *err = L"[invalid]";
145 WriteConsoleW(console, err, wcslen(err), &dummy, NULL);
149 /* write directly to console */
150 WriteConsoleW(console, wbuf, wlen, &dummy, NULL);
152 /* remember if non-ascii characters are printed */
157 #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
158 #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
160 static void set_console_attr(void)
162 WORD attributes = attr;
164 attributes &= ~FOREGROUND_ALL;
165 attributes &= ~BACKGROUND_ALL;
167 /* This could probably use a bitmask
168 instead of a series of ifs */
169 if (attr & FOREGROUND_RED)
170 attributes |= BACKGROUND_RED;
171 if (attr & FOREGROUND_GREEN)
172 attributes |= BACKGROUND_GREEN;
173 if (attr & FOREGROUND_BLUE)
174 attributes |= BACKGROUND_BLUE;
176 if (attr & BACKGROUND_RED)
177 attributes |= FOREGROUND_RED;
178 if (attr & BACKGROUND_GREEN)
179 attributes |= FOREGROUND_GREEN;
180 if (attr & BACKGROUND_BLUE)
181 attributes |= FOREGROUND_BLUE;
183 SetConsoleTextAttribute(console, attributes);
186 static void erase_in_line(void)
188 CONSOLE_SCREEN_BUFFER_INFO sbi;
189 DWORD dummy; /* Needed for Windows 7 (or Vista) regression */
194 GetConsoleScreenBufferInfo(console, &sbi);
195 FillConsoleOutputCharacterA(console, ' ',
196 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition,
200 static void set_attr(char func, const int *params, int paramlen)
205 for (i = 0; i < paramlen; i++) {
212 attr |= FOREGROUND_INTENSITY;
215 case 22: /* normal */
216 attr &= ~FOREGROUND_INTENSITY;
221 case 4: /* underline */
222 case 21: /* double underline */
223 /* Wikipedia says this flag does nothing */
224 /* Furthermore, mingw doesn't define this flag
225 attr |= COMMON_LVB_UNDERSCORE; */
227 case 24: /* no underline */
228 /* attr &= ~COMMON_LVB_UNDERSCORE; */
230 case 5: /* slow blink */
231 case 6: /* fast blink */
232 /* We don't have blink, but we do have
233 background intensity */
234 attr |= BACKGROUND_INTENSITY;
236 case 25: /* no blink */
237 attr &= ~BACKGROUND_INTENSITY;
239 case 7: /* negative */
242 case 27: /* positive */
245 case 8: /* conceal */
246 case 28: /* reveal */
250 attr &= ~FOREGROUND_ALL;
253 attr &= ~FOREGROUND_ALL;
254 attr |= FOREGROUND_RED;
257 attr &= ~FOREGROUND_ALL;
258 attr |= FOREGROUND_GREEN;
260 case 33: /* Yellow */
261 attr &= ~FOREGROUND_ALL;
262 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
265 attr &= ~FOREGROUND_ALL;
266 attr |= FOREGROUND_BLUE;
268 case 35: /* Magenta */
269 attr &= ~FOREGROUND_ALL;
270 attr |= FOREGROUND_RED | FOREGROUND_BLUE;
273 attr &= ~FOREGROUND_ALL;
274 attr |= FOREGROUND_GREEN | FOREGROUND_BLUE;
277 attr |= FOREGROUND_RED |
281 case 38: /* Unknown */
284 attr &= ~FOREGROUND_ALL;
285 attr |= (plain_attr & FOREGROUND_ALL);
288 attr &= ~BACKGROUND_ALL;
291 attr &= ~BACKGROUND_ALL;
292 attr |= BACKGROUND_RED;
295 attr &= ~BACKGROUND_ALL;
296 attr |= BACKGROUND_GREEN;
298 case 43: /* Yellow */
299 attr &= ~BACKGROUND_ALL;
300 attr |= BACKGROUND_RED | BACKGROUND_GREEN;
303 attr &= ~BACKGROUND_ALL;
304 attr |= BACKGROUND_BLUE;
306 case 45: /* Magenta */
307 attr &= ~BACKGROUND_ALL;
308 attr |= BACKGROUND_RED | BACKGROUND_BLUE;
311 attr &= ~BACKGROUND_ALL;
312 attr |= BACKGROUND_GREEN | BACKGROUND_BLUE;
315 attr |= BACKGROUND_RED |
319 case 48: /* Unknown */
322 attr &= ~BACKGROUND_ALL;
323 attr |= (plain_attr & BACKGROUND_ALL);
326 /* Unsupported code */
336 /* Unsupported code */
342 TEXT = 0, ESCAPE = 033, BRACKET = '['
345 static DWORD WINAPI console_thread(LPVOID unused)
347 unsigned char buffer[BUFFER_SIZE];
349 int start, end = 0, c, parampos = 0, state = TEXT;
350 int params[MAX_PARAMS];
353 /* read next chunk of bytes from the pipe */
354 if (!ReadFile(hread, buffer + end, BUFFER_SIZE - end, &bytes,
356 /* exit if pipe has been closed or disconnected */
357 if (GetLastError() == ERROR_PIPE_NOT_CONNECTED ||
358 GetLastError() == ERROR_BROKEN_PIPE)
360 /* ignore other errors */
364 /* scan the bytes and handle ANSI control codes */
367 while (end < bytes) {
372 /* print text seen so far */
374 write_console(buffer + start,
377 /* then start parsing escape sequence */
379 memset(params, 0, sizeof(params));
386 /* continue if "\033[", otherwise bail out */
387 state = (c == BRACKET) ? BRACKET : TEXT;
391 /* parse [0-9;]* into array of parameters */
392 if (c >= '0' && c <= '9') {
393 params[parampos] *= 10;
394 params[parampos] += c - '0';
395 } else if (c == ';') {
397 * next parameter, bail out if out of
401 if (parampos >= MAX_PARAMS)
405 * end of escape sequence, change
408 set_attr(c, params, parampos + 1);
416 /* print remaining text unless parsing an escape sequence */
417 if (state == TEXT && end > start) {
418 /* check for incomplete UTF-8 sequences and fix end */
419 if (buffer[end - 1] >= 0x80) {
420 if (buffer[end -1] >= 0xc0)
422 else if (end - 1 > start &&
423 buffer[end - 2] >= 0xe0)
425 else if (end - 2 > start &&
426 buffer[end - 3] >= 0xf0)
430 /* print remaining complete UTF-8 sequences */
432 write_console(buffer + start, end - start);
434 /* move remaining bytes to the front */
436 memmove(buffer, buffer + end, bytes - end);
439 /* all data has been consumed, mark buffer empty */
444 /* check if the console font supports unicode */
445 warn_if_raster_font();
451 static void winansi_exit(void)
453 /* flush all streams */
456 /* signal console thread to exit */
457 FlushFileBuffers(hwrite);
458 DisconnectNamedPipe(hwrite);
460 /* wait for console thread to copy remaining data */
461 WaitForSingleObject(hthread, INFINITE);
463 /* cleanup handles... */
465 CloseHandle(hthread);
468 static void die_lasterr(const char *fmt, ...)
471 va_start(params, fmt);
472 errno = err_win_to_posix(GetLastError());
473 die_errno(fmt, params);
477 static HANDLE duplicate_handle(HANDLE hnd)
479 HANDLE hresult, hproc = GetCurrentProcess();
480 if (!DuplicateHandle(hproc, hnd, hproc, &hresult, 0, TRUE,
481 DUPLICATE_SAME_ACCESS))
482 die_lasterr("DuplicateHandle(%li) failed",
483 (long) (intptr_t) hnd);
487 static HANDLE swap_osfhnd(int fd, HANDLE new_handle)
490 * Create a copy of the original handle associated with fd
491 * because the original will get closed when we dup2().
493 HANDLE handle = (HANDLE)_get_osfhandle(fd);
494 HANDLE duplicate = duplicate_handle(handle);
496 /* Create a temp fd associated with the already open "new_handle". */
497 int new_fd = _open_osfhandle((intptr_t)new_handle, O_BINARY);
499 assert((fd == 1) || (fd == 2));
502 * Use stock dup2() to re-bind fd to the new handle. Note that
503 * this will implicitly close(1) and close both fd=1 and the
504 * originally associated handle. It will open a new fd=1 and
505 * call DuplicateHandle() on the handle associated with new_fd.
506 * It is because of this implicit close() that we created the
507 * copy of the original.
509 * Note that we need to update the cached console handle to the
510 * duplicated one because the dup2() call will implicitly close
513 * Note that dup2() when given target := {0,1,2} will also
514 * call SetStdHandle(), so we don't need to worry about that.
516 if (console == handle)
520 /* Close the temp fd. This explicitly closes "new_handle"
521 * (because it has been associated with it).
526 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
527 fd_is_interactive[fd] |= FD_SWAPPED;
532 #ifdef DETECT_MSYS_TTY
534 #include <winternl.h>
535 #include <ntstatus.h>
537 static void detect_msys_tty(int fd)
541 POBJECT_NAME_INFORMATION nameinfo = (POBJECT_NAME_INFORMATION) buffer;
544 /* check if fd is a pipe */
545 HANDLE h = (HANDLE) _get_osfhandle(fd);
546 if (GetFileType(h) != FILE_TYPE_PIPE)
550 if (!NT_SUCCESS(NtQueryObject(h, ObjectNameInformation,
551 buffer, sizeof(buffer) - 2, &result)))
553 name = nameinfo->Name.Buffer;
554 name[nameinfo->Name.Length / sizeof(*name)] = 0;
557 * Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX')
558 * or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX')
560 if ((!wcsstr(name, L"msys-") && !wcsstr(name, L"cygwin-")) ||
561 !wcsstr(name, L"-pty"))
565 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
566 fd_is_interactive[fd] |= FD_MSYS;
572 * Wrapper for isatty(). Most calls in the main git code
573 * call isatty(1 or 2) to see if the instance is interactive
574 * and should: be colored, show progress, paginate output.
575 * We lie and give results for what the descriptor WAS at
576 * startup (and ignore any pipe redirection we internally
580 int winansi_isatty(int fd)
582 if (fd >= 0 && fd <= 2)
583 return fd_is_interactive[fd] != 0;
587 void winansi_init(void)
592 /* check if either stdout or stderr is a console output screen buffer */
593 con1 = is_console(1);
594 con2 = is_console(2);
596 /* Also compute console bit for fd 0 even though we don't need the result here. */
599 if (!con1 && !con2) {
600 #ifdef DETECT_MSYS_TTY
601 /* check if stdin / stdout / stderr are MSYS2 pty pipes */
609 /* create a named pipe to communicate with the console thread */
610 xsnprintf(name, sizeof(name), "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
611 hwrite = CreateNamedPipe(name, PIPE_ACCESS_OUTBOUND,
612 PIPE_TYPE_BYTE | PIPE_WAIT, 1, BUFFER_SIZE, 0, 0, NULL);
613 if (hwrite == INVALID_HANDLE_VALUE)
614 die_lasterr("CreateNamedPipe failed");
616 hread = CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
617 if (hread == INVALID_HANDLE_VALUE)
618 die_lasterr("CreateFile for named pipe failed");
620 /* start console spool thread on the pipe's read end */
621 hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);
622 if (hthread == INVALID_HANDLE_VALUE)
623 die_lasterr("CreateThread(console_thread) failed");
625 /* schedule cleanup routine */
626 if (atexit(winansi_exit))
627 die_errno("atexit(winansi_exit) failed");
629 /* redirect stdout / stderr to the pipe */
631 hconsole1 = swap_osfhnd(1, duplicate_handle(hwrite));
633 hconsole2 = swap_osfhnd(2, duplicate_handle(hwrite));
637 * Returns the real console handle if stdout / stderr is a pipe redirecting
638 * to the console. Allows spawn / exec to pass the console to the next process.
640 HANDLE winansi_get_osfhandle(int fd)
642 if (fd == 1 && (fd_is_interactive[1] & FD_SWAPPED))
644 if (fd == 2 && (fd_is_interactive[2] & FD_SWAPPED))
647 return (HANDLE)_get_osfhandle(fd);