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
28 #define GMQCC_IS_STDOUT(X) ((FILE*)((void*)X) == stdout)
29 #define GMQCC_IS_STDERR(X) ((FILE*)((void*)X) == stderr)
30 #define GMQCC_IS_DEFINE(X) (GMQCC_IS_STDERR(X) || GMQCC_IS_STDOUT(X))
41 * Doing colored output on windows is fucking stupid. The linux way is
42 * the real way. So we emulate it on windows :)
45 #define WIN32_LEAN_AND_MEAN
49 * Windows doesn't have constants for FILENO, sadly but the docs tell
50 * use the constant values.
54 #define STDERR_FILENO 2
55 #define STDOUT_FILENO 1
77 WCYAN = WBLUE | WGREEN,
78 WMAGENTA = WBLUE | WRED,
79 WYELLOW = WGREEN | WRED,
80 WWHITE = WBLUE | WGREEN | WRED
83 static const int ansi2win[] = {
94 static int win_fputs(FILE *h, const char *str) {
95 /* state for translate */
103 int colors[] = {-1, -1 };
106 CONSOLE_SCREEN_BUFFER_INFO cinfo;
107 GetConsoleScreenBufferInfo (
108 (GMQCC_IS_STDOUT(h)) ?
109 GetStdHandle(STD_OUTPUT_HANDLE) :
110 GetStdHandle(STD_ERROR_HANDLE), &cinfo
112 icolor = cinfo.wAttributes;
117 else if (state == '\x1B' && *str == '[')
119 else if (state == '[') {
121 colors[colorpos] = *str;
126 for (find = colorpos + 1, acolor = 0, mult = 1; find < 2; find++) {
127 acolor += (colors[find] - 48) * mult;
131 /* convert to windows color */
134 else if (acolor == RESET) {
138 else if (BLACK <= acolor && acolor <= WHITE)
139 wcolor = ansi2win[acolor - 30];
140 else if (acolor == 90) {
141 /* special gray really white man */
146 SetConsoleTextAttribute (
147 (GMQCC_IS_STDOUT(h)) ?
148 GetStdHandle(STD_OUTPUT_HANDLE) :
149 GetStdHandle(STD_ERROR_HANDLE),
151 wcolor | intense | (icolor & 0xF0)
157 fs_file_write(str, 1, 1, stdout);
163 SetConsoleTextAttribute(
164 (GMQCC_IS_STDOUT(h)) ?
165 GetStdHandle(STD_OUTPUT_HANDLE) :
166 GetStdHandle(STD_ERROR_HANDLE),
174 * We use standard files as default. These can be changed at any time
175 * with con_change(F, F)
177 static con_t console;
180 * Enables color on output if supported.
181 * NOTE: The support for checking colors is NULL. On windows this will
182 * always work, on *nix it depends if the term has colors.
184 * NOTE: This prevents colored output to piped stdout/err via isatty
187 static void con_enablecolor(void) {
188 if (console.handle_err == stderr || console.handle_err == stdout)
189 console.color_err = !!(platform_isatty(STDERR_FILENO));
190 if (console.handle_out == stderr || console.handle_out == stdout)
191 console.color_out = !!(platform_isatty(STDOUT_FILENO));
195 * Does a write to the handle with the format string and list of
196 * arguments. This colorizes for windows as well via translate
199 static int con_write(FILE *handle, const char *fmt, va_list va) {
202 ln = vfprintf(handle, fmt, va);
206 memset(data, 0, sizeof(data));
207 platform_vsnprintf(data, sizeof(data), fmt, va);
208 ln = (GMQCC_IS_DEFINE(handle)) ? win_fputs(handle, data) : fs_file_puts(handle, data);
214 /**********************************************************************
215 * EXPOSED INTERFACE BEGINS
216 *********************************************************************/
219 if (!GMQCC_IS_DEFINE(console.handle_err))
220 fs_file_close(console.handle_err);
221 if (!GMQCC_IS_DEFINE(console.handle_out))
222 fs_file_close(console.handle_out);
225 void con_color(int state) {
229 console.color_err = 0;
230 console.color_out = 0;
235 console.handle_err = stderr;
236 console.handle_out = stdout;
246 * This is clever, say you want to change the console to use two
247 * files for out/err. You pass in two strings, it will properly
248 * close the existing handles (if they're not std* handles) and
249 * open them. Now say you want TO use stdout and stderr, this
250 * allows you to do that so long as you cast them to (char*).
251 * Say you need stdout for out, but want a file for error, you can
252 * do this too, just cast the stdout for (char*) and stick to a
253 * string for the error file.
255 int con_change(const char *out, const char *err) {
258 if (!out) out = (const char *)((!console.handle_out) ? stdout : console.handle_out);
259 if (!err) err = (const char *)((!console.handle_err) ? stderr : console.handle_err);
261 if (GMQCC_IS_DEFINE(out)) {
262 console.handle_out = GMQCC_IS_STDOUT(out) ? stdout : stderr;
264 } else if (!(console.handle_out = fs_file_open(out, "w"))) return 0;
266 if (GMQCC_IS_DEFINE(err)) {
267 console.handle_err = GMQCC_IS_STDOUT(err) ? stdout : stderr;
269 } else if (!(console.handle_err = fs_file_open(err, "w"))) return 0;
275 * Defaultizer because stdio.h shouldn't be used anywhere except here
276 * and inside file.c To prevent mis-match of wrapper-interfaces.
278 FILE *con_default_out() {
279 return (console.handle_out = stdout);
281 FILE *con_default_err() {
282 return (console.handle_err = stderr);
285 int con_verr(const char *fmt, va_list va) {
286 return con_write(console.handle_err, fmt, va);
288 int con_vout(const char *fmt, va_list va) {
289 return con_write(console.handle_out, fmt, va);
293 * Standard stdout/stderr printf functions used generally where they need
296 int con_err(const char *fmt, ...) {
304 int con_out(const char *fmt, ...) {
314 * Utility console message writes for lexer contexts. These will allow
315 * for reporting of file:line based on lexer context, These are used
316 * heavily in the parser/ir/ast.
318 static void con_vprintmsg_c(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap, const char *condname) {
319 /* color selection table */
326 int err = !!(level == LVL_ERROR);
327 int color = (err) ? console.color_err : console.color_out;
328 int (*print) (const char *, ...) = (err) ? &con_err : &con_out;
329 int (*vprint)(const char *, va_list) = (err) ? &con_verr : &con_vout;
332 print("\033[0;%dm%s:%d:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, (int)column, sel[level], msgtype);
334 print("%s:%d:%d: %s: ", name, (int)line, (int)column, msgtype);
338 print(" [%s]\n", condname);
343 void con_vprintmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap) {
344 con_vprintmsg_c(level, name, line, column, msgtype, msg, ap, NULL);
347 void con_printmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...) {
350 con_vprintmsg(level, name, line, column, msgtype, msg, va);
354 void con_cvprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
355 con_vprintmsg(lvl, ctx.file, ctx.line, ctx.column, msgtype, msg, ap);
358 void con_cprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, ...) {
361 con_cvprintmsg(ctx, lvl, msgtype, msg, va);
365 #ifndef QCVM_EXECUTOR
366 /* General error interface */
367 size_t compile_errors = 0;
368 size_t compile_warnings = 0;
369 size_t compile_Werrors = 0;
370 static lex_ctx_t first_werror;
372 void compile_show_werrors()
374 con_cprintmsg(first_werror, LVL_ERROR, "first warning", "was here");
377 void vcompile_error(lex_ctx_t ctx, const char *msg, va_list ap)
380 con_cvprintmsg(ctx, LVL_ERROR, "error", msg, ap);
383 void compile_error(lex_ctx_t ctx, const char *msg, ...)
387 vcompile_error(ctx, msg, ap);
391 bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap)
393 const char *msgtype = "warning";
394 int lvl = LVL_WARNING;
395 char warn_name[1024];
397 if (!OPTS_WARN(warntype))
402 (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
405 if (OPTS_WERROR(warntype)) {
406 if (!compile_Werrors)
410 if (OPTS_FLAG(BAIL_ON_WERROR)) {
417 con_vprintmsg_c(lvl, ctx.file, ctx.line, ctx.column, msgtype, fmt, ap, warn_name);
419 return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR);
422 bool GMQCC_WARN compile_warning(lex_ctx_t ctx, int warntype, const char *fmt, ...)
427 r = vcompile_warning(ctx, warntype, fmt, ap);