5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * isatty/STDERR_FILENO/STDOUT_FILNO
27 * + some other things likewise.
33 #define GMQCC_IS_STDOUT(X) ((X) == stdout)
34 #define GMQCC_IS_STDERR(X) ((X) == stderr)
35 #define GMQCC_IS_DEFINE(X) (GMQCC_IS_STDERR(X) || GMQCC_IS_STDOUT(X))
46 * Doing colored output on windows is fucking stupid. The linux way is
47 * the real way. So we emulate it on windows :)
50 #define WIN32_LEAN_AND_MEAN
54 * Windows doesn't have constants for FILENO, sadly but the docs tell
55 * use the constant values.
59 #define STDERR_FILENO 2
60 #define STDOUT_FILENO 1
63 * Windows and it's posix underscore bullshit. We simply fix this
64 * with yay, another macro :P
66 #define isatty _isatty
88 WCYAN = WBLUE | WGREEN,
89 WMAGENTA = WBLUE | WRED,
90 WYELLOW = WGREEN | WRED,
91 WWHITE = WBLUE | WGREEN | WRED
94 static const ansi2win[] = {
105 static void win_fputs(char *str, FILE *f) {
106 /* state for translate */
116 int colors[] = {-1, -1 };
119 CONSOLE_SCREEN_BUFFER_INFO cinfo;
120 GetConsoleScreenBufferInfo(
122 GetStdHandle(STD_OUTPUT_HANDLE) :
123 GetStdHandle(STD_ERROR_HANDLE), &cinfo
125 icolor = cinfo.wAttributes;
130 else if (state == '\e' && *str == '[')
132 else if (state == '[') {
134 colors[colorpos] = *str;
139 for (find = colorpos + 1, acolor = 0, mult = 1; find < 2; find++) {
140 acolor += (colors[find] - 48) * mult;
144 /* convert to windows color */
147 else if (acolor == RESET) {
151 else if (BLACK < acolor && acolor <= WHITE)
152 wcolor = ansi2win[acolor - 30];
153 else if (acolor == 90) {
154 /* special gray really white man */
159 SetConsoleTextattribute(
161 GetStdHandle(STD_OUTPUT_HANDLE) :
162 GetStdHandle(STD_ERROR_HANDLE),
164 wcolor | intense | (icolor & 0xF0)
174 SetConsoleTextAttribute(
176 GetStdHandle(STD_OUTPUT_HANDLE) :
177 GetStdHandle(STD_ERROR_HANDLE),
184 * We use standard files as default. These can be changed at any time
185 * with con_change(F, F)
187 static con_t console;
190 * Enables color on output if supported.
191 * NOTE: The support for checking colors is NULL. On windows this will
192 * always work, on *nix it depends if the term has colors.
194 * NOTE: This prevents colored output to piped stdout/err via isatty
197 static void con_enablecolor() {
198 if (console.handle_err == stderr || console.handle_err == stdout)
199 console.color_err = !!(isatty(STDERR_FILENO));
200 if (console.handle_out == stderr || console.handle_out == stdout)
201 console.color_out = !!(isatty(STDOUT_FILENO));
205 char buf[4] = {0, 0, 0, 0};
208 * This is such a hack. But I'm not linking in any libraries to
209 * do this stupidity. It's insane there is simply not a ttyhascolor
212 FILE *tput = popen("tput colors", "r");
215 * disable colors since we can't determine without tput
216 * which should be guranteed on all *nix OSes
218 console.color_err = 0;
219 console.color_out = 0;
225 * Handle to tput was a success, lets read in the amount of
226 * color support. It should be at minimal 8.
228 fread(buf, 1, sizeof(buf)-1, tput);
231 console.color_err = 0;
232 console.color_out = 0;
236 * We made it this far, which means we support colors in the
245 * Does a write to the handle with the format string and list of
246 * arguments. This colorizes for windows as well via translate
249 static int con_write(FILE *handle, const char *fmt, va_list va) {
252 ln = vfprintf(handle, fmt, va);
256 ln = _vscprintf(fmt, va);
257 data = malloc(ln + 1);
259 vsprintf(data, fmt, va);
260 if (GMQCC_IS_DEFINE(handle))
261 ln = win_fputs(data, handle);
263 ln = fputs(data, handle);
270 /**********************************************************************
271 * EXPOSED INTERFACE BEGINS
272 *********************************************************************/
275 if (!GMQCC_IS_DEFINE(console.handle_err))
276 fclose(console.handle_err);
277 if (!GMQCC_IS_DEFINE(console.handle_out))
278 fclose(console.handle_out);
281 void con_color(int state) {
285 console.color_err = 0;
286 console.color_out = 0;
291 console.handle_err = stderr;
292 console.handle_out = stdout;
302 * This is clever, say you want to change the console to use two
303 * files for out/err. You pass in two strings, it will properly
304 * close the existing handles (if they're not std* handles) and
305 * open them. Now say you want TO use stdout and stderr, this
306 * allows you to do that so long as you cast them to (char*).
307 * Say you need stdout for out, but want a file for error, you can
308 * do this too, just cast the stdout for (char*) and stick to a
309 * string for the error file.
311 int con_change(const char *out, const char *err) {
314 if (GMQCC_IS_DEFINE((FILE*)out)) {
315 console.handle_out = (((FILE*)err) == stdout) ? stdout : stderr;
317 } else if (!(console.handle_out = fopen(out, "w"))) return 0;
319 if (GMQCC_IS_DEFINE((FILE*)err)) {
320 console.handle_err = (((FILE*)err) == stdout) ? stdout : stderr;
322 } else if (!(console.handle_err = fopen(err, "w"))) return 0;
327 int con_verr(const char *fmt, va_list va) {
328 return con_write(console.handle_err, fmt, va);
330 int con_vout(const char *fmt, va_list va) {
331 return con_write(console.handle_out, fmt, va);
334 int con_err(const char *fmt, ...) {
342 int con_out(const char *fmt, ...) {