Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Peter Harris <git@peter.is-a-geek.org> |
| 3 | */ |
| 4 | |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 5 | #undef NOGDI |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 6 | #include "../git-compat-util.h" |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 7 | #include <wingdi.h> |
| 8 | #include <winreg.h> |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 9 | #include "win32.h" |
Johannes Schindelin | 4fa42df | 2019-06-13 04:49:46 -0700 | [diff] [blame] | 10 | #include "win32/lazyload.h" |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 11 | |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 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 |
Johannes Schindelin | cbb3f3c | 2016-12-11 12:16:57 +0100 | [diff] [blame] | 16 | |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 17 | /* |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 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. |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | static HANDLE console; |
| 25 | static WORD plain_attr; |
| 26 | static WORD attr; |
| 27 | static int negative; |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 28 | static int non_ascii_used = 0; |
| 29 | static HANDLE hthread, hread, hwrite; |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 30 | static HANDLE hconsole1, hconsole2; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 31 | |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 32 | #ifdef __MINGW32__ |
Johannes Schindelin | 466931d | 2016-01-14 17:52:02 +0100 | [diff] [blame] | 33 | #if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 5 |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 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 |
Johannes Schindelin | 466931d | 2016-01-14 17:52:02 +0100 | [diff] [blame] | 43 | #endif |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 44 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 45 | static void warn_if_raster_font(void) |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 46 | { |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 47 | DWORD fontFamily = 0; |
Johannes Schindelin | 4fa42df | 2019-06-13 04:49:46 -0700 | [diff] [blame] | 48 | DECLARE_PROC_ADDR(kernel32.dll, BOOL, GetCurrentConsoleFontEx, |
| 49 | HANDLE, BOOL, PCONSOLE_FONT_INFOEX); |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 50 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 51 | /* don't bother if output was ascii only */ |
| 52 | if (!non_ascii_used) |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 53 | return; |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 54 | |
| 55 | /* GetCurrentConsoleFontEx is available since Vista */ |
Johannes Schindelin | 4fa42df | 2019-06-13 04:49:46 -0700 | [diff] [blame] | 56 | if (INIT_PROC_ADDR(GetCurrentConsoleFontEx)) { |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 57 | CONSOLE_FONT_INFOEX cfi; |
| 58 | cfi.cbSize = sizeof(cfi); |
Johannes Schindelin | 4fa42df | 2019-06-13 04:49:46 -0700 | [diff] [blame] | 59 | if (GetCurrentConsoleFontEx(console, 0, &cfi)) |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 60 | fontFamily = cfi.FontFamily; |
| 61 | } else { |
| 62 | /* pre-Vista: check default console font in registry */ |
| 63 | HKEY hkey; |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 64 | if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CURRENT_USER, "Console", |
| 65 | 0, KEY_READ, &hkey)) { |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 66 | DWORD size = sizeof(fontFamily); |
| 67 | RegQueryValueExA(hkey, "FontFamily", NULL, NULL, |
| 68 | (LPVOID) &fontFamily, &size); |
| 69 | RegCloseKey(hkey); |
| 70 | } |
| 71 | } |
| 72 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 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"; |
| 78 | DWORD dummy; |
| 79 | WriteConsoleW(console, msg, wcslen(msg), &dummy, NULL); |
| 80 | } |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 83 | static int is_console(int fd) |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 84 | { |
| 85 | CONSOLE_SCREEN_BUFFER_INFO sbi; |
Johannes Schindelin | fee807c | 2016-12-22 18:08:57 +0100 | [diff] [blame] | 86 | DWORD mode; |
Karsten Blees | 143e615 | 2010-07-31 00:04:02 +0000 | [diff] [blame] | 87 | HANDLE hcon; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 88 | |
| 89 | static int initialized = 0; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 90 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 91 | /* get OS handle of the file descriptor */ |
| 92 | hcon = (HANDLE) _get_osfhandle(fd); |
Karsten Blees | 143e615 | 2010-07-31 00:04:02 +0000 | [diff] [blame] | 93 | if (hcon == INVALID_HANDLE_VALUE) |
| 94 | return 0; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 95 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 96 | /* check if its a device (i.e. console, printer, serial port) */ |
| 97 | if (GetFileType(hcon) != FILE_TYPE_CHAR) |
| 98 | return 0; |
| 99 | |
Karsten Blees | 143e615 | 2010-07-31 00:04:02 +0000 | [diff] [blame] | 100 | /* check if its a handle to a console output screen buffer */ |
Johannes Schindelin | fee807c | 2016-12-22 18:08:57 +0100 | [diff] [blame] | 101 | if (!fd) { |
| 102 | if (!GetConsoleMode(hcon, &mode)) |
| 103 | return 0; |
Johannes Schindelin | 3057da5 | 2017-05-04 15:55:29 +0200 | [diff] [blame] | 104 | /* |
| 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. |
| 109 | */ |
| 110 | sbi.wAttributes = 0; |
Johannes Schindelin | fee807c | 2016-12-22 18:08:57 +0100 | [diff] [blame] | 111 | } else if (!GetConsoleScreenBufferInfo(hcon, &sbi)) |
Karsten Blees | 143e615 | 2010-07-31 00:04:02 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 114 | if (fd >= 0 && fd <= 2) |
| 115 | fd_is_interactive[fd] |= FD_CONSOLE; |
| 116 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 117 | /* initialize attributes */ |
Karsten Blees | 143e615 | 2010-07-31 00:04:02 +0000 | [diff] [blame] | 118 | if (!initialized) { |
Karsten Blees | fcd428f | 2012-03-01 21:53:54 +0100 | [diff] [blame] | 119 | console = hcon; |
Karsten Blees | 143e615 | 2010-07-31 00:04:02 +0000 | [diff] [blame] | 120 | attr = plain_attr = sbi.wAttributes; |
| 121 | negative = 0; |
| 122 | initialized = 1; |
| 123 | } |
| 124 | |
Karsten Blees | 143e615 | 2010-07-31 00:04:02 +0000 | [diff] [blame] | 125 | return 1; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 128 | #define BUFFER_SIZE 4096 |
| 129 | #define MAX_PARAMS 16 |
| 130 | |
| 131 | static void write_console(unsigned char *str, size_t len) |
Karsten Blees | 617ce96 | 2010-07-31 00:04:01 +0000 | [diff] [blame] | 132 | { |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 133 | /* only called from console_thread, so a static buffer will do */ |
| 134 | static wchar_t wbuf[2 * BUFFER_SIZE + 1]; |
| 135 | DWORD dummy; |
Karsten Blees | 617ce96 | 2010-07-31 00:04:01 +0000 | [diff] [blame] | 136 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 137 | /* convert utf-8 to utf-16 */ |
| 138 | int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len); |
Johannes Schindelin | b6b066a | 2017-05-04 15:55:34 +0200 | [diff] [blame] | 139 | if (wlen < 0) { |
| 140 | wchar_t *err = L"[invalid]"; |
| 141 | WriteConsoleW(console, err, wcslen(err), &dummy, NULL); |
| 142 | return; |
| 143 | } |
Karsten Blees | 617ce96 | 2010-07-31 00:04:01 +0000 | [diff] [blame] | 144 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 145 | /* write directly to console */ |
| 146 | WriteConsoleW(console, wbuf, wlen, &dummy, NULL); |
| 147 | |
| 148 | /* remember if non-ascii characters are printed */ |
Karsten Blees | 1edeb9a | 2010-07-31 00:04:03 +0000 | [diff] [blame] | 149 | if (wlen != len) |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 150 | non_ascii_used = 1; |
Karsten Blees | 617ce96 | 2010-07-31 00:04:01 +0000 | [diff] [blame] | 151 | } |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 152 | |
| 153 | #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) |
| 154 | #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE) |
| 155 | |
| 156 | static void set_console_attr(void) |
| 157 | { |
| 158 | WORD attributes = attr; |
| 159 | if (negative) { |
| 160 | attributes &= ~FOREGROUND_ALL; |
| 161 | attributes &= ~BACKGROUND_ALL; |
| 162 | |
| 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; |
| 171 | |
| 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; |
| 178 | } |
| 179 | SetConsoleTextAttribute(console, attributes); |
| 180 | } |
| 181 | |
Johannes Schindelin | 1897713 | 2009-03-10 22:58:09 +0100 | [diff] [blame] | 182 | static void erase_in_line(void) |
| 183 | { |
| 184 | CONSOLE_SCREEN_BUFFER_INFO sbi; |
Johannes Schindelin | 492f709 | 2009-06-01 08:04:16 +0200 | [diff] [blame] | 185 | DWORD dummy; /* Needed for Windows 7 (or Vista) regression */ |
Johannes Schindelin | 1897713 | 2009-03-10 22:58:09 +0100 | [diff] [blame] | 186 | |
| 187 | if (!console) |
| 188 | return; |
| 189 | |
| 190 | GetConsoleScreenBufferInfo(console, &sbi); |
| 191 | FillConsoleOutputCharacterA(console, ' ', |
| 192 | sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition, |
Johannes Schindelin | 492f709 | 2009-06-01 08:04:16 +0200 | [diff] [blame] | 193 | &dummy); |
Johannes Schindelin | 1897713 | 2009-03-10 22:58:09 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 196 | static void set_attr(char func, const int *params, int paramlen) |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 197 | { |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 198 | int i; |
| 199 | switch (func) { |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 200 | case 'm': |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 201 | for (i = 0; i < paramlen; i++) { |
| 202 | switch (params[i]) { |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 203 | case 0: /* reset */ |
| 204 | attr = plain_attr; |
| 205 | negative = 0; |
| 206 | break; |
| 207 | case 1: /* bold */ |
| 208 | attr |= FOREGROUND_INTENSITY; |
| 209 | break; |
| 210 | case 2: /* faint */ |
| 211 | case 22: /* normal */ |
| 212 | attr &= ~FOREGROUND_INTENSITY; |
| 213 | break; |
| 214 | case 3: /* italic */ |
| 215 | /* Unsupported */ |
| 216 | break; |
| 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; */ |
| 222 | break; |
| 223 | case 24: /* no underline */ |
| 224 | /* attr &= ~COMMON_LVB_UNDERSCORE; */ |
| 225 | break; |
| 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; |
| 231 | break; |
| 232 | case 25: /* no blink */ |
| 233 | attr &= ~BACKGROUND_INTENSITY; |
| 234 | break; |
| 235 | case 7: /* negative */ |
| 236 | negative = 1; |
| 237 | break; |
| 238 | case 27: /* positive */ |
| 239 | negative = 0; |
| 240 | break; |
| 241 | case 8: /* conceal */ |
| 242 | case 28: /* reveal */ |
| 243 | /* Unsupported */ |
| 244 | break; |
| 245 | case 30: /* Black */ |
| 246 | attr &= ~FOREGROUND_ALL; |
| 247 | break; |
| 248 | case 31: /* Red */ |
| 249 | attr &= ~FOREGROUND_ALL; |
| 250 | attr |= FOREGROUND_RED; |
| 251 | break; |
| 252 | case 32: /* Green */ |
| 253 | attr &= ~FOREGROUND_ALL; |
| 254 | attr |= FOREGROUND_GREEN; |
| 255 | break; |
| 256 | case 33: /* Yellow */ |
| 257 | attr &= ~FOREGROUND_ALL; |
| 258 | attr |= FOREGROUND_RED | FOREGROUND_GREEN; |
| 259 | break; |
| 260 | case 34: /* Blue */ |
| 261 | attr &= ~FOREGROUND_ALL; |
| 262 | attr |= FOREGROUND_BLUE; |
| 263 | break; |
| 264 | case 35: /* Magenta */ |
| 265 | attr &= ~FOREGROUND_ALL; |
| 266 | attr |= FOREGROUND_RED | FOREGROUND_BLUE; |
| 267 | break; |
| 268 | case 36: /* Cyan */ |
| 269 | attr &= ~FOREGROUND_ALL; |
| 270 | attr |= FOREGROUND_GREEN | FOREGROUND_BLUE; |
| 271 | break; |
| 272 | case 37: /* White */ |
| 273 | attr |= FOREGROUND_RED | |
| 274 | FOREGROUND_GREEN | |
| 275 | FOREGROUND_BLUE; |
| 276 | break; |
| 277 | case 38: /* Unknown */ |
| 278 | break; |
| 279 | case 39: /* reset */ |
| 280 | attr &= ~FOREGROUND_ALL; |
| 281 | attr |= (plain_attr & FOREGROUND_ALL); |
| 282 | break; |
| 283 | case 40: /* Black */ |
| 284 | attr &= ~BACKGROUND_ALL; |
| 285 | break; |
| 286 | case 41: /* Red */ |
| 287 | attr &= ~BACKGROUND_ALL; |
| 288 | attr |= BACKGROUND_RED; |
| 289 | break; |
| 290 | case 42: /* Green */ |
| 291 | attr &= ~BACKGROUND_ALL; |
| 292 | attr |= BACKGROUND_GREEN; |
| 293 | break; |
| 294 | case 43: /* Yellow */ |
| 295 | attr &= ~BACKGROUND_ALL; |
| 296 | attr |= BACKGROUND_RED | BACKGROUND_GREEN; |
| 297 | break; |
| 298 | case 44: /* Blue */ |
| 299 | attr &= ~BACKGROUND_ALL; |
| 300 | attr |= BACKGROUND_BLUE; |
| 301 | break; |
| 302 | case 45: /* Magenta */ |
| 303 | attr &= ~BACKGROUND_ALL; |
| 304 | attr |= BACKGROUND_RED | BACKGROUND_BLUE; |
| 305 | break; |
| 306 | case 46: /* Cyan */ |
| 307 | attr &= ~BACKGROUND_ALL; |
| 308 | attr |= BACKGROUND_GREEN | BACKGROUND_BLUE; |
| 309 | break; |
| 310 | case 47: /* White */ |
| 311 | attr |= BACKGROUND_RED | |
| 312 | BACKGROUND_GREEN | |
| 313 | BACKGROUND_BLUE; |
| 314 | break; |
| 315 | case 48: /* Unknown */ |
| 316 | break; |
| 317 | case 49: /* reset */ |
| 318 | attr &= ~BACKGROUND_ALL; |
| 319 | attr |= (plain_attr & BACKGROUND_ALL); |
| 320 | break; |
| 321 | default: |
| 322 | /* Unsupported code */ |
| 323 | break; |
| 324 | } |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 325 | } |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 326 | set_console_attr(); |
| 327 | break; |
| 328 | case 'K': |
Johannes Schindelin | 1897713 | 2009-03-10 22:58:09 +0100 | [diff] [blame] | 329 | erase_in_line(); |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 330 | break; |
| 331 | default: |
| 332 | /* Unsupported code */ |
| 333 | break; |
| 334 | } |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 337 | enum { |
| 338 | TEXT = 0, ESCAPE = 033, BRACKET = '[' |
| 339 | }; |
| 340 | |
| 341 | static DWORD WINAPI console_thread(LPVOID unused) |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 342 | { |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 343 | unsigned char buffer[BUFFER_SIZE]; |
| 344 | DWORD bytes; |
| 345 | int start, end = 0, c, parampos = 0, state = TEXT; |
| 346 | int params[MAX_PARAMS]; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 347 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 348 | while (1) { |
| 349 | /* read next chunk of bytes from the pipe */ |
| 350 | if (!ReadFile(hread, buffer + end, BUFFER_SIZE - end, &bytes, |
| 351 | NULL)) { |
| 352 | /* exit if pipe has been closed or disconnected */ |
| 353 | if (GetLastError() == ERROR_PIPE_NOT_CONNECTED || |
| 354 | GetLastError() == ERROR_BROKEN_PIPE) |
| 355 | break; |
| 356 | /* ignore other errors */ |
| 357 | continue; |
| 358 | } |
Karsten Blees | 617ce96 | 2010-07-31 00:04:01 +0000 | [diff] [blame] | 359 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 360 | /* scan the bytes and handle ANSI control codes */ |
| 361 | bytes += end; |
| 362 | start = end = 0; |
| 363 | while (end < bytes) { |
| 364 | c = buffer[end++]; |
| 365 | switch (state) { |
| 366 | case TEXT: |
| 367 | if (c == ESCAPE) { |
| 368 | /* print text seen so far */ |
| 369 | if (end - 1 > start) |
| 370 | write_console(buffer + start, |
| 371 | end - 1 - start); |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 372 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 373 | /* then start parsing escape sequence */ |
| 374 | start = end - 1; |
| 375 | memset(params, 0, sizeof(params)); |
| 376 | parampos = 0; |
| 377 | state = ESCAPE; |
| 378 | } |
| 379 | break; |
| 380 | |
| 381 | case ESCAPE: |
| 382 | /* continue if "\033[", otherwise bail out */ |
| 383 | state = (c == BRACKET) ? BRACKET : TEXT; |
| 384 | break; |
| 385 | |
| 386 | case BRACKET: |
| 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 == ';') { |
| 392 | /* |
| 393 | * next parameter, bail out if out of |
| 394 | * bounds |
| 395 | */ |
| 396 | parampos++; |
| 397 | if (parampos >= MAX_PARAMS) |
| 398 | state = TEXT; |
| 399 | } else { |
| 400 | /* |
| 401 | * end of escape sequence, change |
| 402 | * console attributes |
| 403 | */ |
| 404 | set_attr(c, params, parampos + 1); |
| 405 | start = end; |
| 406 | state = TEXT; |
| 407 | } |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | |
| 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) |
| 417 | end--; |
| 418 | else if (end - 1 > start && |
| 419 | buffer[end - 2] >= 0xe0) |
| 420 | end -= 2; |
| 421 | else if (end - 2 > start && |
| 422 | buffer[end - 3] >= 0xf0) |
| 423 | end -= 3; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 426 | /* print remaining complete UTF-8 sequences */ |
| 427 | if (end > start) |
| 428 | write_console(buffer + start, end - start); |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 429 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 430 | /* move remaining bytes to the front */ |
| 431 | if (end < bytes) |
| 432 | memmove(buffer, buffer + end, bytes - end); |
| 433 | end = bytes - end; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 434 | } else { |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 435 | /* all data has been consumed, mark buffer empty */ |
| 436 | end = 0; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 437 | } |
| 438 | } |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 439 | |
| 440 | /* check if the console font supports unicode */ |
| 441 | warn_if_raster_font(); |
| 442 | |
| 443 | CloseHandle(hread); |
| 444 | return 0; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 447 | static void winansi_exit(void) |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 448 | { |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 449 | /* flush all streams */ |
| 450 | _flushall(); |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 451 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 452 | /* signal console thread to exit */ |
| 453 | FlushFileBuffers(hwrite); |
| 454 | DisconnectNamedPipe(hwrite); |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 455 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 456 | /* wait for console thread to copy remaining data */ |
| 457 | WaitForSingleObject(hthread, INFINITE); |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 458 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 459 | /* cleanup handles... */ |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 460 | CloseHandle(hwrite); |
| 461 | CloseHandle(hthread); |
| 462 | } |
| 463 | |
| 464 | static void die_lasterr(const char *fmt, ...) |
| 465 | { |
| 466 | va_list params; |
| 467 | va_start(params, fmt); |
| 468 | errno = err_win_to_posix(GetLastError()); |
| 469 | die_errno(fmt, params); |
| 470 | va_end(params); |
| 471 | } |
| 472 | |
Johannes Schindelin | ff8978d | 2018-10-30 11:44:40 -0700 | [diff] [blame] | 473 | #undef dup2 |
| 474 | int winansi_dup2(int oldfd, int newfd) |
| 475 | { |
| 476 | int ret = dup2(oldfd, newfd); |
| 477 | |
| 478 | if (!ret && newfd >= 0 && newfd <= 2) |
| 479 | fd_is_interactive[newfd] = oldfd < 0 || oldfd > 2 ? |
| 480 | 0 : fd_is_interactive[oldfd]; |
| 481 | |
| 482 | return ret; |
| 483 | } |
| 484 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 485 | static HANDLE duplicate_handle(HANDLE hnd) |
| 486 | { |
| 487 | HANDLE hresult, hproc = GetCurrentProcess(); |
| 488 | if (!DuplicateHandle(hproc, hnd, hproc, &hresult, 0, TRUE, |
| 489 | DUPLICATE_SAME_ACCESS)) |
Johannes Schindelin | 7c00bc3 | 2016-01-15 14:24:34 +0100 | [diff] [blame] | 490 | die_lasterr("DuplicateHandle(%li) failed", |
| 491 | (long) (intptr_t) hnd); |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 492 | return hresult; |
| 493 | } |
| 494 | |
Karsten Blees | fcd428f | 2012-03-01 21:53:54 +0100 | [diff] [blame] | 495 | static HANDLE swap_osfhnd(int fd, HANDLE new_handle) |
| 496 | { |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 497 | /* |
| 498 | * Create a copy of the original handle associated with fd |
| 499 | * because the original will get closed when we dup2(). |
| 500 | */ |
| 501 | HANDLE handle = (HANDLE)_get_osfhandle(fd); |
| 502 | HANDLE duplicate = duplicate_handle(handle); |
Karsten Blees | fcd428f | 2012-03-01 21:53:54 +0100 | [diff] [blame] | 503 | |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 504 | /* Create a temp fd associated with the already open "new_handle". */ |
| 505 | int new_fd = _open_osfhandle((intptr_t)new_handle, O_BINARY); |
Karsten Blees | fcd428f | 2012-03-01 21:53:54 +0100 | [diff] [blame] | 506 | |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 507 | assert((fd == 1) || (fd == 2)); |
| 508 | |
| 509 | /* |
| 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. |
| 516 | * |
Johannes Schindelin | 1d3f065 | 2017-01-18 13:14:35 +0100 | [diff] [blame] | 517 | * Note that we need to update the cached console handle to the |
| 518 | * duplicated one because the dup2() call will implicitly close |
| 519 | * the original one. |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 520 | * |
| 521 | * Note that dup2() when given target := {0,1,2} will also |
| 522 | * call SetStdHandle(), so we don't need to worry about that. |
| 523 | */ |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 524 | if (console == handle) |
| 525 | console = duplicate; |
Johannes Schindelin | 1d3f065 | 2017-01-18 13:14:35 +0100 | [diff] [blame] | 526 | dup2(new_fd, fd); |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 527 | |
| 528 | /* Close the temp fd. This explicitly closes "new_handle" |
| 529 | * (because it has been associated with it). |
| 530 | */ |
| 531 | close(new_fd); |
| 532 | |
Johannes Schindelin | a4d92d5 | 2017-02-13 23:34:06 +0100 | [diff] [blame] | 533 | if (fd == 2) |
| 534 | setvbuf(stderr, NULL, _IONBF, BUFSIZ); |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 535 | fd_is_interactive[fd] |= FD_SWAPPED; |
| 536 | |
| 537 | return duplicate; |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Karsten Blees | f7f90e0 | 2016-04-27 17:16:37 +0200 | [diff] [blame] | 540 | #ifdef DETECT_MSYS_TTY |
| 541 | |
| 542 | #include <winternl.h> |
Jeff Hostetler | 5f3ff78 | 2019-06-19 14:06:05 -0700 | [diff] [blame] | 543 | |
| 544 | #if defined(_MSC_VER) |
| 545 | |
| 546 | typedef struct _OBJECT_NAME_INFORMATION |
| 547 | { |
| 548 | UNICODE_STRING Name; |
Johannes Schindelin | 41616ef | 2019-10-04 08:09:27 -0700 | [diff] [blame] | 549 | WCHAR NameBuffer[FLEX_ARRAY]; |
Jeff Hostetler | 5f3ff78 | 2019-06-19 14:06:05 -0700 | [diff] [blame] | 550 | } OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION; |
| 551 | |
| 552 | #define ObjectNameInformation 1 |
| 553 | |
| 554 | #else |
Karsten Blees | f7f90e0 | 2016-04-27 17:16:37 +0200 | [diff] [blame] | 555 | #include <ntstatus.h> |
Jeff Hostetler | 5f3ff78 | 2019-06-19 14:06:05 -0700 | [diff] [blame] | 556 | #endif |
Karsten Blees | f7f90e0 | 2016-04-27 17:16:37 +0200 | [diff] [blame] | 557 | |
| 558 | static void detect_msys_tty(int fd) |
| 559 | { |
| 560 | ULONG result; |
| 561 | BYTE buffer[1024]; |
| 562 | POBJECT_NAME_INFORMATION nameinfo = (POBJECT_NAME_INFORMATION) buffer; |
| 563 | PWSTR name; |
| 564 | |
| 565 | /* check if fd is a pipe */ |
| 566 | HANDLE h = (HANDLE) _get_osfhandle(fd); |
| 567 | if (GetFileType(h) != FILE_TYPE_PIPE) |
| 568 | return; |
| 569 | |
| 570 | /* get pipe name */ |
| 571 | if (!NT_SUCCESS(NtQueryObject(h, ObjectNameInformation, |
| 572 | buffer, sizeof(buffer) - 2, &result))) |
| 573 | return; |
| 574 | name = nameinfo->Name.Buffer; |
Max Kirillov | c46458e | 2016-12-19 23:32:00 +0200 | [diff] [blame] | 575 | name[nameinfo->Name.Length / sizeof(*name)] = 0; |
Karsten Blees | f7f90e0 | 2016-04-27 17:16:37 +0200 | [diff] [blame] | 576 | |
Alan Davies | 8692483 | 2016-12-22 18:09:18 +0100 | [diff] [blame] | 577 | /* |
| 578 | * Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX') |
| 579 | * or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX') |
| 580 | */ |
| 581 | if ((!wcsstr(name, L"msys-") && !wcsstr(name, L"cygwin-")) || |
| 582 | !wcsstr(name, L"-pty")) |
Karsten Blees | f7f90e0 | 2016-04-27 17:16:37 +0200 | [diff] [blame] | 583 | return; |
| 584 | |
Johannes Schindelin | a4d92d5 | 2017-02-13 23:34:06 +0100 | [diff] [blame] | 585 | if (fd == 2) |
| 586 | setvbuf(stderr, NULL, _IONBF, BUFSIZ); |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 587 | fd_is_interactive[fd] |= FD_MSYS; |
Karsten Blees | f7f90e0 | 2016-04-27 17:16:37 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | #endif |
| 591 | |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 592 | /* |
| 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 |
| 598 | * do). |
| 599 | */ |
| 600 | #undef isatty |
Johannes Schindelin | cbb3f3c | 2016-12-11 12:16:57 +0100 | [diff] [blame] | 601 | int winansi_isatty(int fd) |
| 602 | { |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 603 | if (fd >= 0 && fd <= 2) |
| 604 | return fd_is_interactive[fd] != 0; |
| 605 | return isatty(fd); |
Johannes Schindelin | cbb3f3c | 2016-12-11 12:16:57 +0100 | [diff] [blame] | 606 | } |
| 607 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 608 | void winansi_init(void) |
| 609 | { |
Karsten Blees | fcd428f | 2012-03-01 21:53:54 +0100 | [diff] [blame] | 610 | int con1, con2; |
Johannes Schindelin | 9423885 | 2019-06-27 02:37:19 -0700 | [diff] [blame] | 611 | wchar_t name[32]; |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 612 | |
| 613 | /* check if either stdout or stderr is a console output screen buffer */ |
| 614 | con1 = is_console(1); |
| 615 | con2 = is_console(2); |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 616 | |
| 617 | /* Also compute console bit for fd 0 even though we don't need the result here. */ |
| 618 | is_console(0); |
| 619 | |
Karsten Blees | f7f90e0 | 2016-04-27 17:16:37 +0200 | [diff] [blame] | 620 | if (!con1 && !con2) { |
| 621 | #ifdef DETECT_MSYS_TTY |
| 622 | /* check if stdin / stdout / stderr are MSYS2 pty pipes */ |
| 623 | detect_msys_tty(0); |
| 624 | detect_msys_tty(1); |
| 625 | detect_msys_tty(2); |
| 626 | #endif |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 627 | return; |
Karsten Blees | f7f90e0 | 2016-04-27 17:16:37 +0200 | [diff] [blame] | 628 | } |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 629 | |
| 630 | /* create a named pipe to communicate with the console thread */ |
Johannes Schindelin | 9423885 | 2019-06-27 02:37:19 -0700 | [diff] [blame] | 631 | if (swprintf(name, ARRAY_SIZE(name) - 1, L"\\\\.\\pipe\\winansi%lu", |
| 632 | GetCurrentProcessId()) < 0) |
| 633 | die("Could not initialize winansi pipe name"); |
| 634 | hwrite = CreateNamedPipeW(name, PIPE_ACCESS_OUTBOUND, |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 635 | PIPE_TYPE_BYTE | PIPE_WAIT, 1, BUFFER_SIZE, 0, 0, NULL); |
| 636 | if (hwrite == INVALID_HANDLE_VALUE) |
| 637 | die_lasterr("CreateNamedPipe failed"); |
| 638 | |
Johannes Schindelin | 9423885 | 2019-06-27 02:37:19 -0700 | [diff] [blame] | 639 | hread = CreateFileW(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 640 | if (hread == INVALID_HANDLE_VALUE) |
| 641 | die_lasterr("CreateFile for named pipe failed"); |
| 642 | |
| 643 | /* start console spool thread on the pipe's read end */ |
| 644 | hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL); |
| 645 | if (hthread == INVALID_HANDLE_VALUE) |
| 646 | die_lasterr("CreateThread(console_thread) failed"); |
| 647 | |
| 648 | /* schedule cleanup routine */ |
| 649 | if (atexit(winansi_exit)) |
| 650 | die_errno("atexit(winansi_exit) failed"); |
| 651 | |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 652 | /* redirect stdout / stderr to the pipe */ |
| 653 | if (con1) |
Karsten Blees | 5182265 | 2014-06-14 00:09:06 +0200 | [diff] [blame] | 654 | hconsole1 = swap_osfhnd(1, duplicate_handle(hwrite)); |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 655 | if (con2) |
Karsten Blees | 5182265 | 2014-06-14 00:09:06 +0200 | [diff] [blame] | 656 | hconsole2 = swap_osfhnd(2, duplicate_handle(hwrite)); |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /* |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 660 | * Returns the real console handle if stdout / stderr is a pipe redirecting |
| 661 | * to the console. Allows spawn / exec to pass the console to the next process. |
| 662 | */ |
| 663 | HANDLE winansi_get_osfhandle(int fd) |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 664 | { |
Johannes Schindelin | c5a03b1 | 2019-11-22 14:41:03 +0000 | [diff] [blame] | 665 | HANDLE ret; |
| 666 | |
Jeff Hostetler | a9b8a09 | 2016-12-22 18:09:23 +0100 | [diff] [blame] | 667 | if (fd == 1 && (fd_is_interactive[1] & FD_SWAPPED)) |
| 668 | return hconsole1; |
| 669 | if (fd == 2 && (fd_is_interactive[2] & FD_SWAPPED)) |
| 670 | return hconsole2; |
| 671 | |
Johannes Schindelin | c5a03b1 | 2019-11-22 14:41:03 +0000 | [diff] [blame] | 672 | ret = (HANDLE)_get_osfhandle(fd); |
| 673 | |
| 674 | /* |
| 675 | * There are obviously circumstances under which _get_osfhandle() |
| 676 | * returns (HANDLE)-2. This is not documented anywhere, but that is so |
| 677 | * clearly an invalid handle value that we can just work around this |
| 678 | * and return the correct value for invalid handles. |
| 679 | */ |
| 680 | return ret == (HANDLE)-2 ? INVALID_HANDLE_VALUE : ret; |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 681 | } |