2 * Copyright (C) 2012, 2013
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
27 * isatty/STDERR_FILENO/STDOUT_FILNO
28 * + some other things likewise.
35 * Windows and it's posix underscore bullshit. We simply fix this
36 * with yay, another macro :P
38 # define isatty _isatty
41 #define GMQCC_IS_STDOUT(X) ((FILE*)((void*)X) == stdout)
42 #define GMQCC_IS_STDERR(X) ((FILE*)((void*)X) == stderr)
43 #define GMQCC_IS_DEFINE(X) (GMQCC_IS_STDERR(X) || GMQCC_IS_STDOUT(X))
54 * Doing colored output on windows is fucking stupid. The linux way is
55 * the real way. So we emulate it on windows :)
58 #define WIN32_LEAN_AND_MEAN
62 * Windows doesn't have constants for FILENO, sadly but the docs tell
63 * use the constant values.
67 #define STDERR_FILENO 2
68 #define STDOUT_FILENO 1
90 WCYAN = WBLUE | WGREEN,
91 WMAGENTA = WBLUE | WRED,
92 WYELLOW = WGREEN | WRED,
93 WWHITE = WBLUE | WGREEN | WRED
96 static const int ansi2win[] = {
107 static int win_fputs(FILE *h, const char *str) {
108 /* state for translate */
117 int colors[] = {-1, -1 };
120 CONSOLE_SCREEN_BUFFER_INFO cinfo;
121 GetConsoleScreenBufferInfo (
122 (GMQCC_IS_STDOUT(h)) ?
123 GetStdHandle(STD_OUTPUT_HANDLE) :
124 GetStdHandle(STD_ERROR_HANDLE), &cinfo
126 icolor = cinfo.wAttributes;
131 else if (state == '\x1B' && *str == '[')
133 else if (state == '[') {
135 colors[colorpos] = *str;
140 for (find = colorpos + 1, acolor = 0, mult = 1; find < 2; find++) {
141 acolor += (colors[find] - 48) * mult;
145 /* convert to windows color */
148 else if (acolor == RESET) {
152 else if (BLACK <= acolor && acolor <= WHITE)
153 wcolor = ansi2win[acolor - 30];
154 else if (acolor == 90) {
155 /* special gray really white man */
160 SetConsoleTextAttribute (
161 (GMQCC_IS_STDOUT(h)) ?
162 GetStdHandle(STD_OUTPUT_HANDLE) :
163 GetStdHandle(STD_ERROR_HANDLE),
165 wcolor | intense | (icolor & 0xF0)
177 SetConsoleTextAttribute(
178 (GMQCC_IS_STDOUT(h)) ?
179 GetStdHandle(STD_OUTPUT_HANDLE) :
180 GetStdHandle(STD_ERROR_HANDLE),
188 * We use standard files as default. These can be changed at any time
189 * with con_change(F, F)
191 static con_t console;
194 * Enables color on output if supported.
195 * NOTE: The support for checking colors is NULL. On windows this will
196 * always work, on *nix it depends if the term has colors.
198 * NOTE: This prevents colored output to piped stdout/err via isatty
201 static void con_enablecolor() {
202 if (console.handle_err == stderr || console.handle_err == stdout)
203 console.color_err = !!(isatty(STDERR_FILENO));
204 if (console.handle_out == stderr || console.handle_out == stdout)
205 console.color_out = !!(isatty(STDOUT_FILENO));
209 * Does a write to the handle with the format string and list of
210 * arguments. This colorizes for windows as well via translate
213 static int con_write(FILE *handle, const char *fmt, va_list va) {
216 ln = vfprintf(handle, fmt, va);
220 memset(data, 0, sizeof(data));
221 vsnprintf(data, sizeof(data), fmt, va);
222 ln = (GMQCC_IS_DEFINE(handle)) ? win_fputs(data, handle) : file_puts(data, handle);
228 /**********************************************************************
229 * EXPOSED INTERFACE BEGINS
230 *********************************************************************/
233 if (!GMQCC_IS_DEFINE(console.handle_err))
234 file_close(console.handle_err);
235 if (!GMQCC_IS_DEFINE(console.handle_out))
236 file_close(console.handle_out);
239 void con_color(int state) {
243 console.color_err = 0;
244 console.color_out = 0;
249 console.handle_err = stderr;
250 console.handle_out = stdout;
260 * This is clever, say you want to change the console to use two
261 * files for out/err. You pass in two strings, it will properly
262 * close the existing handles (if they're not std* handles) and
263 * open them. Now say you want TO use stdout and stderr, this
264 * allows you to do that so long as you cast them to (char*).
265 * Say you need stdout for out, but want a file for error, you can
266 * do this too, just cast the stdout for (char*) and stick to a
267 * string for the error file.
269 int con_change(const char *out, const char *err) {
272 if (!out) out = (const char *)((!console.handle_out) ? stdout : console.handle_out);
273 if (!err) err = (const char *)((!console.handle_err) ? stderr : console.handle_err);
275 if (GMQCC_IS_DEFINE(out)) {
276 console.handle_out = GMQCC_IS_STDOUT(out) ? stdout : stderr;
278 } else if (!(console.handle_out = file_open(out, "w"))) return 0;
280 if (GMQCC_IS_DEFINE(err)) {
281 console.handle_err = GMQCC_IS_STDOUT(err) ? stdout : stderr;
283 } else if (!(console.handle_err = file_open(err, "w"))) return 0;
286 setvbuf(console.handle_out, NULL, _IONBF, 0);
287 setvbuf(console.handle_err, NULL, _IONBF, 0);
293 * Defaultizer because stdio.h shouldn't be used anywhere except here
294 * and inside file.c To prevent mis-match of wrapper-interfaces.
296 FILE *con_default_out() {
297 return (console.handle_out = stdout);
299 FILE *con_default_err() {
300 return (console.handle_err = stderr);
303 int con_verr(const char *fmt, va_list va) {
304 return con_write(console.handle_err, fmt, va);
306 int con_vout(const char *fmt, va_list va) {
307 return con_write(console.handle_out, fmt, va);
311 * Standard stdout/stderr printf functions used generally where they need
314 int con_err(const char *fmt, ...) {
322 int con_out(const char *fmt, ...) {
332 * Utility console message writes for lexer contexts. These will allow
333 * for reporting of file:line based on lexer context, These are used
334 * heavily in the parser/ir/ast.
336 void con_vprintmsg_c(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap, const char *condname) {
337 /* color selection table */
344 int err = !!(level == LVL_ERROR);
345 int color = (err) ? console.color_err : console.color_out;
346 int (*print) (const char *, ...) = (err) ? &con_err : &con_out;
347 int (*vprint)(const char *, va_list) = (err) ? &con_verr : &con_vout;
350 print("\033[0;%dm%s:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, sel[level], msgtype);
352 print("%s:%d: %s: ", name, (int)line, msgtype);
356 print(" [%s]\n", condname);
361 void con_vprintmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap) {
362 con_vprintmsg_c(level, name, line, msgtype, msg, ap, NULL);
365 void con_printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) {
368 con_vprintmsg(level, name, line, msgtype, msg, va);
372 void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
373 con_vprintmsg(lvl, ((lex_ctx*)ctx)->file, ((lex_ctx*)ctx)->line, msgtype, msg, ap);
376 void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...) {
379 con_cvprintmsg(ctx, lvl, msgtype, msg, va);
383 /* General error interface */
384 size_t compile_errors = 0;
385 size_t compile_warnings = 0;
387 size_t compile_Werrors = 0;
388 static lex_ctx first_werror;
390 void compile_show_werrors()
392 con_cprintmsg((void*)&first_werror, LVL_ERROR, "first warning", "was here");
395 void vcompile_error(lex_ctx ctx, const char *msg, va_list ap)
398 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap);
401 void compile_error(lex_ctx ctx, const char *msg, ...)
405 vcompile_error(ctx, msg, ap);
409 bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_list ap)
411 const char *msgtype = "warning";
412 int lvl = LVL_WARNING;
413 char warn_name[1024];
415 if (!OPTS_WARN(warntype))
420 (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
423 if (OPTS_WERROR(warntype)) {
424 if (!compile_Werrors)
428 if (OPTS_FLAG(BAIL_ON_WERROR)) {
435 con_vprintmsg_c(lvl, ctx.file, ctx.line, msgtype, fmt, ap, warn_name);
437 return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR);
440 bool GMQCC_WARN compile_warning(lex_ctx ctx, int warntype, const char *fmt, ...)
445 r = vcompile_warning(ctx, warntype, fmt, ap);