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