2 * Copyright 2008 Peter Harris <git@peter.is-a-geek.org>
6 #include "../git-compat-util.h"
11 Functions to be wrapped:
16 ANSI codes used by git: m, K
18 This file is git-specific. Therefore, this file does not attempt
19 to implement any codes that are not used by git.
22 static HANDLE console
;
23 static WORD plain_attr
;
26 static int non_ascii_used
= 0;
27 static HANDLE hthread
, hread
, hwrite
;
28 static HANDLE hwrite1
= INVALID_HANDLE_VALUE
, hwrite2
= INVALID_HANDLE_VALUE
;
29 static HANDLE hconsole1
, hconsole2
;
32 typedef struct _CONSOLE_FONT_INFOEX
{
38 WCHAR FaceName
[LF_FACESIZE
];
39 } CONSOLE_FONT_INFOEX
, *PCONSOLE_FONT_INFOEX
;
42 typedef BOOL (WINAPI
*PGETCURRENTCONSOLEFONTEX
)(HANDLE
, BOOL
,
43 PCONSOLE_FONT_INFOEX
);
45 static void warn_if_raster_font(void)
48 PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx
;
50 /* don't bother if output was ascii only */
54 /* GetCurrentConsoleFontEx is available since Vista */
55 pGetCurrentConsoleFontEx
= (PGETCURRENTCONSOLEFONTEX
) GetProcAddress(
56 GetModuleHandle("kernel32.dll"),
57 "GetCurrentConsoleFontEx");
58 if (pGetCurrentConsoleFontEx
) {
59 CONSOLE_FONT_INFOEX cfi
;
60 cfi
.cbSize
= sizeof(cfi
);
61 if (pGetCurrentConsoleFontEx(console
, 0, &cfi
))
62 fontFamily
= cfi
.FontFamily
;
64 /* pre-Vista: check default console font in registry */
66 if (ERROR_SUCCESS
== RegOpenKeyExA(HKEY_CURRENT_USER
, "Console",
67 0, KEY_READ
, &hkey
)) {
68 DWORD size
= sizeof(fontFamily
);
69 RegQueryValueExA(hkey
, "FontFamily", NULL
, NULL
,
70 (LPVOID
) &fontFamily
, &size
);
75 if (!(fontFamily
& TMPF_TRUETYPE
)) {
76 const wchar_t *msg
= L
"\nWarning: Your console font probably "
77 L
"doesn\'t support Unicode. If you experience strange "
78 L
"characters in the output, consider switching to a "
79 L
"TrueType font such as Lucida Console!\n";
80 WriteConsoleW(console
, msg
, wcslen(msg
), NULL
, NULL
);
84 static int is_console(int fd
)
86 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 */
101 if (!GetConsoleScreenBufferInfo(hcon
, &sbi
))
104 /* initialize attributes */
106 attr
= plain_attr
= sbi
.wAttributes
;
114 #define BUFFER_SIZE 4096
115 #define MAX_PARAMS 16
117 static void write_console(unsigned char *str
, size_t len
)
119 /* only called from console_thread, so a static buffer will do */
120 static wchar_t wbuf
[2 * BUFFER_SIZE
+ 1];
122 /* convert utf-8 to utf-16 */
123 int wlen
= MultiByteToWideChar(CP_UTF8
, 0, (char*) str
, len
, wbuf
,
126 /* write directly to console */
127 WriteConsoleW(console
, wbuf
, wlen
, NULL
, NULL
);
129 /* remember if non-ascii characters are printed */
134 #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
135 #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
137 static void set_console_attr(void)
139 WORD attributes
= attr
;
141 attributes
&= ~FOREGROUND_ALL
;
142 attributes
&= ~BACKGROUND_ALL
;
144 /* This could probably use a bitmask
145 instead of a series of ifs */
146 if (attr
& FOREGROUND_RED
)
147 attributes
|= BACKGROUND_RED
;
148 if (attr
& FOREGROUND_GREEN
)
149 attributes
|= BACKGROUND_GREEN
;
150 if (attr
& FOREGROUND_BLUE
)
151 attributes
|= BACKGROUND_BLUE
;
153 if (attr
& BACKGROUND_RED
)
154 attributes
|= FOREGROUND_RED
;
155 if (attr
& BACKGROUND_GREEN
)
156 attributes
|= FOREGROUND_GREEN
;
157 if (attr
& BACKGROUND_BLUE
)
158 attributes
|= FOREGROUND_BLUE
;
160 SetConsoleTextAttribute(console
, attributes
);
163 static void erase_in_line(void)
165 CONSOLE_SCREEN_BUFFER_INFO sbi
;
166 DWORD dummy
; /* Needed for Windows 7 (or Vista) regression */
171 GetConsoleScreenBufferInfo(console
, &sbi
);
172 FillConsoleOutputCharacterA(console
, ' ',
173 sbi
.dwSize
.X
- sbi
.dwCursorPosition
.X
, sbi
.dwCursorPosition
,
177 static void set_attr(char func
, const int *params
, int paramlen
)
182 for (i
= 0; i
< paramlen
; i
++) {
189 attr
|= FOREGROUND_INTENSITY
;
192 case 22: /* normal */
193 attr
&= ~FOREGROUND_INTENSITY
;
198 case 4: /* underline */
199 case 21: /* double underline */
200 /* Wikipedia says this flag does nothing */
201 /* Furthermore, mingw doesn't define this flag
202 attr |= COMMON_LVB_UNDERSCORE; */
204 case 24: /* no underline */
205 /* attr &= ~COMMON_LVB_UNDERSCORE; */
207 case 5: /* slow blink */
208 case 6: /* fast blink */
209 /* We don't have blink, but we do have
210 background intensity */
211 attr
|= BACKGROUND_INTENSITY
;
213 case 25: /* no blink */
214 attr
&= ~BACKGROUND_INTENSITY
;
216 case 7: /* negative */
219 case 27: /* positive */
222 case 8: /* conceal */
223 case 28: /* reveal */
227 attr
&= ~FOREGROUND_ALL
;
230 attr
&= ~FOREGROUND_ALL
;
231 attr
|= FOREGROUND_RED
;
234 attr
&= ~FOREGROUND_ALL
;
235 attr
|= FOREGROUND_GREEN
;
237 case 33: /* Yellow */
238 attr
&= ~FOREGROUND_ALL
;
239 attr
|= FOREGROUND_RED
| FOREGROUND_GREEN
;
242 attr
&= ~FOREGROUND_ALL
;
243 attr
|= FOREGROUND_BLUE
;
245 case 35: /* Magenta */
246 attr
&= ~FOREGROUND_ALL
;
247 attr
|= FOREGROUND_RED
| FOREGROUND_BLUE
;
250 attr
&= ~FOREGROUND_ALL
;
251 attr
|= FOREGROUND_GREEN
| FOREGROUND_BLUE
;
254 attr
|= FOREGROUND_RED
|
258 case 38: /* Unknown */
261 attr
&= ~FOREGROUND_ALL
;
262 attr
|= (plain_attr
& FOREGROUND_ALL
);
265 attr
&= ~BACKGROUND_ALL
;
268 attr
&= ~BACKGROUND_ALL
;
269 attr
|= BACKGROUND_RED
;
272 attr
&= ~BACKGROUND_ALL
;
273 attr
|= BACKGROUND_GREEN
;
275 case 43: /* Yellow */
276 attr
&= ~BACKGROUND_ALL
;
277 attr
|= BACKGROUND_RED
| BACKGROUND_GREEN
;
280 attr
&= ~BACKGROUND_ALL
;
281 attr
|= BACKGROUND_BLUE
;
283 case 45: /* Magenta */
284 attr
&= ~BACKGROUND_ALL
;
285 attr
|= BACKGROUND_RED
| BACKGROUND_BLUE
;
288 attr
&= ~BACKGROUND_ALL
;
289 attr
|= BACKGROUND_GREEN
| BACKGROUND_BLUE
;
292 attr
|= BACKGROUND_RED
|
296 case 48: /* Unknown */
299 attr
&= ~BACKGROUND_ALL
;
300 attr
|= (plain_attr
& BACKGROUND_ALL
);
303 /* Unsupported code */
313 /* Unsupported code */
319 TEXT
= 0, ESCAPE
= 033, BRACKET
= '['
322 static DWORD WINAPI
console_thread(LPVOID unused
)
324 unsigned char buffer
[BUFFER_SIZE
];
326 int start
, end
= 0, c
, parampos
= 0, state
= TEXT
;
327 int params
[MAX_PARAMS
];
330 /* read next chunk of bytes from the pipe */
331 if (!ReadFile(hread
, buffer
+ end
, BUFFER_SIZE
- end
, &bytes
,
333 /* exit if pipe has been closed or disconnected */
334 if (GetLastError() == ERROR_PIPE_NOT_CONNECTED
||
335 GetLastError() == ERROR_BROKEN_PIPE
)
337 /* ignore other errors */
341 /* scan the bytes and handle ANSI control codes */
344 while (end
< bytes
) {
349 /* print text seen so far */
351 write_console(buffer
+ start
,
354 /* then start parsing escape sequence */
356 memset(params
, 0, sizeof(params
));
363 /* continue if "\033[", otherwise bail out */
364 state
= (c
== BRACKET
) ? BRACKET
: TEXT
;
368 /* parse [0-9;]* into array of parameters */
369 if (c
>= '0' && c
<= '9') {
370 params
[parampos
] *= 10;
371 params
[parampos
] += c
- '0';
372 } else if (c
== ';') {
374 * next parameter, bail out if out of
378 if (parampos
>= MAX_PARAMS
)
382 * end of escape sequence, change
385 set_attr(c
, params
, parampos
+ 1);
393 /* print remaining text unless parsing an escape sequence */
394 if (state
== TEXT
&& end
> start
) {
395 /* check for incomplete UTF-8 sequences and fix end */
396 if (buffer
[end
- 1] >= 0x80) {
397 if (buffer
[end
-1] >= 0xc0)
399 else if (end
- 1 > start
&&
400 buffer
[end
- 2] >= 0xe0)
402 else if (end
- 2 > start
&&
403 buffer
[end
- 3] >= 0xf0)
407 /* print remaining complete UTF-8 sequences */
409 write_console(buffer
+ start
, end
- start
);
411 /* move remaining bytes to the front */
413 memmove(buffer
, buffer
+ end
, bytes
- end
);
416 /* all data has been consumed, mark buffer empty */
421 /* check if the console font supports unicode */
422 warn_if_raster_font();
428 static void winansi_exit(void)
430 /* flush all streams */
433 /* signal console thread to exit */
434 FlushFileBuffers(hwrite
);
435 DisconnectNamedPipe(hwrite
);
437 /* wait for console thread to copy remaining data */
438 WaitForSingleObject(hthread
, INFINITE
);
440 /* cleanup handles... */
441 if (hwrite1
!= INVALID_HANDLE_VALUE
)
442 CloseHandle(hwrite1
);
443 if (hwrite2
!= INVALID_HANDLE_VALUE
)
444 CloseHandle(hwrite2
);
446 CloseHandle(hthread
);
449 static void die_lasterr(const char *fmt
, ...)
452 va_start(params
, fmt
);
453 errno
= err_win_to_posix(GetLastError());
454 die_errno(fmt
, params
);
458 static HANDLE
duplicate_handle(HANDLE hnd
)
460 HANDLE hresult
, hproc
= GetCurrentProcess();
461 if (!DuplicateHandle(hproc
, hnd
, hproc
, &hresult
, 0, TRUE
,
462 DUPLICATE_SAME_ACCESS
))
463 die_lasterr("DuplicateHandle(%li) failed", (long) hnd
);
467 static HANDLE
redirect_console(FILE *stream
, HANDLE
*phcon
, int new_fd
)
469 /* get original console handle */
470 int fd
= _fileno(stream
);
471 HANDLE hcon
= (HANDLE
) _get_osfhandle(fd
);
472 if (hcon
== INVALID_HANDLE_VALUE
)
473 die_errno("_get_osfhandle(%i) failed", fd
);
475 /* save a copy to phcon and console (used by the background thread) */
476 console
= *phcon
= duplicate_handle(hcon
);
478 /* duplicate new_fd over fd (closes fd and associated handle (hcon)) */
479 if (_dup2(new_fd
, fd
))
480 die_errno("_dup2(%i, %i) failed", new_fd
, fd
);
482 /* no buffering, or stdout / stderr will be out of sync */
483 setbuf(stream
, NULL
);
484 return (HANDLE
) _get_osfhandle(fd
);
487 void winansi_init(void)
489 int con1
, con2
, hwrite_fd
;
492 /* check if either stdout or stderr is a console output screen buffer */
493 con1
= is_console(1);
494 con2
= is_console(2);
498 /* create a named pipe to communicate with the console thread */
499 sprintf(name
, "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
500 hwrite
= CreateNamedPipe(name
, PIPE_ACCESS_OUTBOUND
,
501 PIPE_TYPE_BYTE
| PIPE_WAIT
, 1, BUFFER_SIZE
, 0, 0, NULL
);
502 if (hwrite
== INVALID_HANDLE_VALUE
)
503 die_lasterr("CreateNamedPipe failed");
505 hread
= CreateFile(name
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
506 if (hread
== INVALID_HANDLE_VALUE
)
507 die_lasterr("CreateFile for named pipe failed");
509 /* start console spool thread on the pipe's read end */
510 hthread
= CreateThread(NULL
, 0, console_thread
, NULL
, 0, NULL
);
511 if (hthread
== INVALID_HANDLE_VALUE
)
512 die_lasterr("CreateThread(console_thread) failed");
514 /* schedule cleanup routine */
515 if (atexit(winansi_exit
))
516 die_errno("atexit(winansi_exit) failed");
518 /* create a file descriptor for the write end of the pipe */
519 hwrite_fd
= _open_osfhandle((long) duplicate_handle(hwrite
), _O_BINARY
);
521 die_errno("_open_osfhandle(%li) failed", (long) hwrite
);
523 /* redirect stdout / stderr to the pipe */
525 hwrite1
= redirect_console(stdout
, &hconsole1
, hwrite_fd
);
527 hwrite2
= redirect_console(stderr
, &hconsole2
, hwrite_fd
);
529 /* close pipe file descriptor (also closes the duped hwrite) */
533 static int is_same_handle(HANDLE hnd
, int fd
)
535 return hnd
!= INVALID_HANDLE_VALUE
&& hnd
== (HANDLE
) _get_osfhandle(fd
);
539 * Return true if stdout / stderr is a pipe redirecting to the console.
541 int winansi_isatty(int fd
)
543 if (fd
== 1 && is_same_handle(hwrite1
, 1))
545 else if (fd
== 2 && is_same_handle(hwrite2
, 2))
552 * Returns the real console handle if stdout / stderr is a pipe redirecting
553 * to the console. Allows spawn / exec to pass the console to the next process.
555 HANDLE
winansi_get_osfhandle(int fd
)
557 if (fd
== 1 && is_same_handle(hwrite1
, 1))
559 else if (fd
== 2 && is_same_handle(hwrite2
, 2))
562 return (HANDLE
) _get_osfhandle(fd
);