2 * Copyright (C) 2012, 2013, 2014, 2015
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 #define GMQCC_IS_STDOUT(X) ((X) == stdout)
27 #define GMQCC_IS_STDERR(X) ((X) == stderr)
28 #define GMQCC_IS_DEFINE(X) (GMQCC_IS_STDERR(X) || GMQCC_IS_STDOUT(X))
40 * Enables color on output if supported.
41 * NOTE: The support for checking colors is NULL. On windows this will
42 * always work, on *nix it depends if the term has colors.
44 * NOTE: This prevents colored output to piped stdout/err via isatty
47 static void con_enablecolor(void) {
48 console.color_err = util_isatty(console.handle_err);
49 console.color_out = util_isatty(console.handle_out);
53 * Does a write to the handle with the format string and list of
54 * arguments. This colorizes for windows as well via translate
57 static int con_write(FILE *handle, const char *fmt, va_list va) {
58 return vfprintf(handle, fmt, va);
61 /**********************************************************************
62 * EXPOSED INTERFACE BEGINS
63 *********************************************************************/
66 if (!GMQCC_IS_DEFINE(console.handle_err))
67 fclose(console.handle_err);
68 if (!GMQCC_IS_DEFINE(console.handle_out))
69 fclose(console.handle_out);
72 void con_color(int state) {
76 console.color_err = 0;
77 console.color_out = 0;
82 console.handle_err = stderr;
83 console.handle_out = stdout;
93 * Defaultizer because stdio.h shouldn't be used anywhere except here
94 * and inside file.c To prevent mis-match of wrapper-interfaces.
96 FILE *con_default_out() {
97 return console.handle_out = stdout;
100 FILE *con_default_err() {
101 return console.handle_err = stderr;
104 int con_verr(const char *fmt, va_list va) {
105 return con_write(console.handle_err, fmt, va);
107 int con_vout(const char *fmt, va_list va) {
108 return con_write(console.handle_out, fmt, va);
112 * Standard stdout/stderr printf functions used generally where they need
115 int con_err(const char *fmt, ...) {
123 int con_out(const char *fmt, ...) {
133 * Utility console message writes for lexer contexts. These will allow
134 * for reporting of file:line based on lexer context, These are used
135 * heavily in the parser/ir/ast.
137 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) {
138 /* color selection table */
145 int err = !!(level == LVL_ERROR);
146 int color = (err) ? console.color_err : console.color_out;
147 int (*print) (const char *, ...) = (err) ? &con_err : &con_out;
148 int (*vprint)(const char *, va_list) = (err) ? &con_verr : &con_vout;
151 print("\033[0;%dm%s:%d:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, (int)column, sel[level], msgtype);
153 print("%s:%d:%d: %s: ", name, (int)line, (int)column, msgtype);
157 print(" [%s]\n", condname);
162 void con_vprintmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap) {
163 con_vprintmsg_c(level, name, line, column, msgtype, msg, ap, NULL);
166 void con_printmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...) {
169 con_vprintmsg(level, name, line, column, msgtype, msg, va);
173 void con_cvprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
174 con_vprintmsg(lvl, ctx.file, ctx.line, ctx.column, msgtype, msg, ap);
177 void con_cprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, ...) {
180 con_cvprintmsg(ctx, lvl, msgtype, msg, va);
184 /* General error interface: TODO seperate as part of the compiler front-end */
185 size_t compile_errors = 0;
186 size_t compile_warnings = 0;
187 size_t compile_Werrors = 0;
188 static lex_ctx_t first_werror;
190 void compile_show_werrors()
192 con_cprintmsg(first_werror, LVL_ERROR, "first warning", "was here");
195 void vcompile_error(lex_ctx_t ctx, const char *msg, va_list ap)
198 con_cvprintmsg(ctx, LVL_ERROR, "error", msg, ap);
201 void compile_error(lex_ctx_t ctx, const char *msg, ...)
205 vcompile_error(ctx, msg, ap);
209 bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap)
211 const char *msgtype = "warning";
212 int lvl = LVL_WARNING;
213 char warn_name[1024];
215 if (!OPTS_WARN(warntype))
220 (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
223 if (OPTS_WERROR(warntype)) {
224 if (!compile_Werrors)
228 if (OPTS_FLAG(BAIL_ON_WERROR)) {
235 con_vprintmsg_c(lvl, ctx.file, ctx.line, ctx.column, msgtype, fmt, ap, warn_name);
237 return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR);
240 bool GMQCC_WARN compile_warning(lex_ctx_t ctx, int warntype, const char *fmt, ...)
245 r = vcompile_warning(ctx, warntype, fmt, ap);