MSVC: fix winansi.c compile errors
[git/dscho.git] / compat / winansi.c
blobbf514f9de599cd96ea02ed1afddde5e157960b8a
1 /*
2 * Copyright 2008 Peter Harris <git@peter.is-a-geek.org>
3 */
5 #undef NOGDI
6 #include "../git-compat-util.h"
7 #include <malloc.h>
8 #include <wingdi.h>
9 #include <winreg.h>
12 Functions to be wrapped:
14 #undef printf
15 #undef fprintf
16 #undef fputs
17 #undef vfprintf
18 /* TODO: write */
21 ANSI codes used by git: m, K
23 This file is git-specific. Therefore, this file does not attempt
24 to implement any codes that are not used by git.
27 static HANDLE console;
28 static WORD plain_attr;
29 static WORD attr;
30 static int negative;
31 static FILE *last_stream = NULL;
32 static int non_ascii_used = 0;
34 #ifdef __MINGW32__
35 typedef struct _CONSOLE_FONT_INFOEX {
36 ULONG cbSize;
37 DWORD nFont;
38 COORD dwFontSize;
39 UINT FontFamily;
40 UINT FontWeight;
41 WCHAR FaceName[LF_FACESIZE];
42 } CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
43 #endif
45 typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
46 PCONSOLE_FONT_INFOEX);
48 static void warn_if_raster_font(void)
50 DWORD fontFamily = 0;
51 PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
53 /* don't bother if output was ascii only */
54 if (!non_ascii_used)
55 return;
57 /* GetCurrentConsoleFontEx is available since Vista */
58 pGetCurrentConsoleFontEx = (PGETCURRENTCONSOLEFONTEX) GetProcAddress(
59 GetModuleHandle("kernel32.dll"), "GetCurrentConsoleFontEx");
60 if (pGetCurrentConsoleFontEx) {
61 CONSOLE_FONT_INFOEX cfi;
62 cfi.cbSize = sizeof(cfi);
63 if (pGetCurrentConsoleFontEx(console, 0, &cfi))
64 fontFamily = cfi.FontFamily;
65 } else {
66 /* pre-Vista: check default console font in registry */
67 HKEY hkey;
68 if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CURRENT_USER, "Console", 0,
69 KEY_READ, &hkey)) {
70 DWORD size = sizeof(fontFamily);
71 RegQueryValueExA(hkey, "FontFamily", NULL, NULL,
72 (LPVOID) &fontFamily, &size);
73 RegCloseKey(hkey);
77 if (!(fontFamily & TMPF_TRUETYPE))
78 warning("Your console font probably doesn\'t support "
79 "Unicode. If you experience strange characters in the output, "
80 "consider switching to a TrueType font such as Lucida Console!");
83 static int is_console(FILE *stream)
85 CONSOLE_SCREEN_BUFFER_INFO sbi;
86 HANDLE hcon;
88 static int initialized = 0;
90 /* use cached value if stream hasn't changed */
91 if (stream == last_stream)
92 return console != NULL;
94 last_stream = stream;
95 console = NULL;
97 /* get OS handle of the stream */
98 hcon = (HANDLE) _get_osfhandle(_fileno(stream));
99 if (hcon == INVALID_HANDLE_VALUE)
100 return 0;
102 /* check if its a handle to a console output screen buffer */
103 if (!GetConsoleScreenBufferInfo(hcon, &sbi))
104 return 0;
106 if (!initialized) {
107 attr = plain_attr = sbi.wAttributes;
108 negative = 0;
109 initialized = 1;
110 /* check console font on exit */
111 atexit(warn_if_raster_font);
114 console = hcon;
115 return 1;
118 static int write_console(const char *str, size_t len)
120 /* convert utf-8 to utf-16, write directly to console */
121 int wlen = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0);
122 wchar_t *wbuf = (wchar_t *) alloca(wlen * sizeof(wchar_t));
123 MultiByteToWideChar(CP_UTF8, 0, str, len, wbuf, wlen);
125 WriteConsoleW(console, wbuf, wlen, NULL, NULL);
127 /* remember if non-ascii characters are printed */
128 if (wlen != len)
129 non_ascii_used = 1;
131 /* return original (utf-8 encoded) length */
132 return len;
135 #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
136 #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
138 static void set_console_attr(void)
140 WORD attributes = attr;
141 if (negative) {
142 attributes &= ~FOREGROUND_ALL;
143 attributes &= ~BACKGROUND_ALL;
145 /* This could probably use a bitmask
146 instead of a series of ifs */
147 if (attr & FOREGROUND_RED)
148 attributes |= BACKGROUND_RED;
149 if (attr & FOREGROUND_GREEN)
150 attributes |= BACKGROUND_GREEN;
151 if (attr & FOREGROUND_BLUE)
152 attributes |= BACKGROUND_BLUE;
154 if (attr & BACKGROUND_RED)
155 attributes |= FOREGROUND_RED;
156 if (attr & BACKGROUND_GREEN)
157 attributes |= FOREGROUND_GREEN;
158 if (attr & BACKGROUND_BLUE)
159 attributes |= FOREGROUND_BLUE;
161 SetConsoleTextAttribute(console, attributes);
164 static void erase_in_line(void)
166 CONSOLE_SCREEN_BUFFER_INFO sbi;
167 DWORD dummy; /* Needed for Windows 7 (or Vista) regression */
169 if (!console)
170 return;
172 GetConsoleScreenBufferInfo(console, &sbi);
173 FillConsoleOutputCharacterA(console, ' ',
174 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition,
175 &dummy);
179 static const char *set_attr(const char *str)
181 const char *func;
182 size_t len = strspn(str, "0123456789;");
183 func = str + len;
185 switch (*func) {
186 case 'm':
187 do {
188 long val = strtol(str, (char **)&str, 10);
189 switch (val) {
190 case 0: /* reset */
191 attr = plain_attr;
192 negative = 0;
193 break;
194 case 1: /* bold */
195 attr |= FOREGROUND_INTENSITY;
196 break;
197 case 2: /* faint */
198 case 22: /* normal */
199 attr &= ~FOREGROUND_INTENSITY;
200 break;
201 case 3: /* italic */
202 /* Unsupported */
203 break;
204 case 4: /* underline */
205 case 21: /* double underline */
206 /* Wikipedia says this flag does nothing */
207 /* Furthermore, mingw doesn't define this flag
208 attr |= COMMON_LVB_UNDERSCORE; */
209 break;
210 case 24: /* no underline */
211 /* attr &= ~COMMON_LVB_UNDERSCORE; */
212 break;
213 case 5: /* slow blink */
214 case 6: /* fast blink */
215 /* We don't have blink, but we do have
216 background intensity */
217 attr |= BACKGROUND_INTENSITY;
218 break;
219 case 25: /* no blink */
220 attr &= ~BACKGROUND_INTENSITY;
221 break;
222 case 7: /* negative */
223 negative = 1;
224 break;
225 case 27: /* positive */
226 negative = 0;
227 break;
228 case 8: /* conceal */
229 case 28: /* reveal */
230 /* Unsupported */
231 break;
232 case 30: /* Black */
233 attr &= ~FOREGROUND_ALL;
234 break;
235 case 31: /* Red */
236 attr &= ~FOREGROUND_ALL;
237 attr |= FOREGROUND_RED;
238 break;
239 case 32: /* Green */
240 attr &= ~FOREGROUND_ALL;
241 attr |= FOREGROUND_GREEN;
242 break;
243 case 33: /* Yellow */
244 attr &= ~FOREGROUND_ALL;
245 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
246 break;
247 case 34: /* Blue */
248 attr &= ~FOREGROUND_ALL;
249 attr |= FOREGROUND_BLUE;
250 break;
251 case 35: /* Magenta */
252 attr &= ~FOREGROUND_ALL;
253 attr |= FOREGROUND_RED | FOREGROUND_BLUE;
254 break;
255 case 36: /* Cyan */
256 attr &= ~FOREGROUND_ALL;
257 attr |= FOREGROUND_GREEN | FOREGROUND_BLUE;
258 break;
259 case 37: /* White */
260 attr |= FOREGROUND_RED |
261 FOREGROUND_GREEN |
262 FOREGROUND_BLUE;
263 break;
264 case 38: /* Unknown */
265 break;
266 case 39: /* reset */
267 attr &= ~FOREGROUND_ALL;
268 attr |= (plain_attr & FOREGROUND_ALL);
269 break;
270 case 40: /* Black */
271 attr &= ~BACKGROUND_ALL;
272 break;
273 case 41: /* Red */
274 attr &= ~BACKGROUND_ALL;
275 attr |= BACKGROUND_RED;
276 break;
277 case 42: /* Green */
278 attr &= ~BACKGROUND_ALL;
279 attr |= BACKGROUND_GREEN;
280 break;
281 case 43: /* Yellow */
282 attr &= ~BACKGROUND_ALL;
283 attr |= BACKGROUND_RED | BACKGROUND_GREEN;
284 break;
285 case 44: /* Blue */
286 attr &= ~BACKGROUND_ALL;
287 attr |= BACKGROUND_BLUE;
288 break;
289 case 45: /* Magenta */
290 attr &= ~BACKGROUND_ALL;
291 attr |= BACKGROUND_RED | BACKGROUND_BLUE;
292 break;
293 case 46: /* Cyan */
294 attr &= ~BACKGROUND_ALL;
295 attr |= BACKGROUND_GREEN | BACKGROUND_BLUE;
296 break;
297 case 47: /* White */
298 attr |= BACKGROUND_RED |
299 BACKGROUND_GREEN |
300 BACKGROUND_BLUE;
301 break;
302 case 48: /* Unknown */
303 break;
304 case 49: /* reset */
305 attr &= ~BACKGROUND_ALL;
306 attr |= (plain_attr & BACKGROUND_ALL);
307 break;
308 default:
309 /* Unsupported code */
310 break;
312 str++;
313 } while (*(str-1) == ';');
315 set_console_attr();
316 break;
317 case 'K':
318 erase_in_line();
319 break;
320 default:
321 /* Unsupported code */
322 break;
325 return func + 1;
328 static int ansi_emulate(const char *str, FILE *stream)
330 int rv = 0;
331 const char *pos = str;
333 fflush(stream);
335 while (*pos) {
336 pos = strstr(str, "\033[");
337 if (pos) {
338 size_t len = pos - str;
340 if (len) {
341 size_t out_len = write_console(str, len);
342 rv += out_len;
343 if (out_len < len)
344 return rv;
347 str = pos + 2;
348 rv += 2;
350 pos = set_attr(str);
351 rv += pos - str;
352 str = pos;
353 } else {
354 size_t len = strlen(str);
355 rv += write_console(str, len);
356 return rv;
359 return rv;
362 int winansi_fputs(const char *str, FILE *stream)
364 int rv;
366 if (!is_console(stream))
367 return fputs(str, stream);
369 rv = ansi_emulate(str, stream);
371 if (rv >= 0)
372 return 0;
373 else
374 return EOF;
377 int winansi_vfprintf(FILE *stream, const char *format, va_list list)
379 int len, rv;
380 char small_buf[256];
381 char *buf = small_buf;
382 va_list cp;
384 if (!is_console(stream))
385 goto abort;
387 va_copy(cp, list);
388 len = vsnprintf(small_buf, sizeof(small_buf), format, cp);
389 va_end(cp);
391 if (len > sizeof(small_buf) - 1) {
392 buf = malloc(len + 1);
393 if (!buf)
394 goto abort;
396 len = vsnprintf(buf, len + 1, format, list);
399 rv = ansi_emulate(buf, stream);
401 if (buf != small_buf)
402 free(buf);
403 return rv;
405 abort:
406 rv = vfprintf(stream, format, list);
407 return rv;
410 int winansi_fprintf(FILE *stream, const char *format, ...)
412 va_list list;
413 int rv;
415 va_start(list, format);
416 rv = winansi_vfprintf(stream, format, list);
417 va_end(list);
419 return rv;
422 int winansi_printf(const char *format, ...)
424 va_list list;
425 int rv;
427 va_start(list, format);
428 rv = winansi_vfprintf(stdout, format, list);
429 va_end(list);
431 return rv;