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