-/*
- * Doing colored output on windows is fucking stupid. The linux way is
- * the real way. So we emulate it on windows :)
- */
-#ifdef _WIN32
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-
-/*
- * Windows doesn't have constants for FILENO, sadly but the docs tell
- * use the constant values.
- */
-#undef STDERR_FILENO
-#undef STDOUT_FILENO
-#define STDERR_FILENO 2
-#define STDOUT_FILENO 1
-
-enum {
- RESET = 0,
- BOLD = 1,
- BLACK = 30,
- RED,
- GREEN,
- YELLOW,
- BLUE,
- MAGENTA,
- CYAN,
- GRAY,
- WHITE = GRAY
-};
-
-enum {
- WBLACK,
- WBLUE,
- WGREEN = 2,
- WRED = 4,
- WINTENSE = 8,
- WCYAN = WBLUE | WGREEN,
- WMAGENTA = WBLUE | WRED,
- WYELLOW = WGREEN | WRED,
- WWHITE = WBLUE | WGREEN | WRED
-};
-
-static const int ansi2win[] = {
- WBLACK,
- WRED,
- WGREEN,
- WYELLOW,
- WBLUE,
- WMAGENTA,
- WCYAN,
- WWHITE
-};
-
-static int win_fputs(FILE *h, const char *str) {
- /* state for translate */
- int acolor;
- int wcolor;
- int icolor;
-
- int state;
-
- /* attributes */
- int intense = -1;
- int colors[] = {-1, -1 };
- int colorpos = 1;
- int length = 0;
- CONSOLE_SCREEN_BUFFER_INFO cinfo;
- GetConsoleScreenBufferInfo (
- (GMQCC_IS_STDOUT(h)) ?
- GetStdHandle(STD_OUTPUT_HANDLE) :
- GetStdHandle(STD_ERROR_HANDLE), &cinfo
- );
- icolor = cinfo.wAttributes;
-
- while (*str) {
- if (*str == '\x1B')
- state = '\x1B';
- else if (state == '\x1B' && *str == '[')
- state = '[';
- else if (state == '[') {
- if (*str != 'm') {
- colors[colorpos] = *str;
- colorpos--;
- } else {
- int find;
- int mult;
- for (find = colorpos + 1, acolor = 0, mult = 1; find < 2; find++) {
- acolor += (colors[find] - 48) * mult;
- mult *= 10;
- }
-
- /* convert to windows color */
- if (acolor == BOLD)
- intense = WINTENSE;
- else if (acolor == RESET) {
- intense = WBLACK;
- wcolor = icolor;
- }
- else if (BLACK <= acolor && acolor <= WHITE)
- wcolor = ansi2win[acolor - 30];
- else if (acolor == 90) {
- /* special gray really white man */
- wcolor = WWHITE;
- intense = WBLACK;
- }
-
- SetConsoleTextAttribute (
- (GMQCC_IS_STDOUT(h)) ?
- GetStdHandle(STD_OUTPUT_HANDLE) :
- GetStdHandle(STD_ERROR_HANDLE),
-
- wcolor | intense | (icolor & 0xF0)
- );
- colorpos = 1;
- state = -1;
- }
- } else {
- fs_file_write(str, 1, 1, stdout);
- length ++;
- }
- str++;
- }
- /* restore */
- SetConsoleTextAttribute(
- (GMQCC_IS_STDOUT(h)) ?
- GetStdHandle(STD_OUTPUT_HANDLE) :
- GetStdHandle(STD_ERROR_HANDLE),
- icolor
- );
- return length;
-}
-#endif
-