4 #define GMQCC_IS_STDOUT(X) ((X) == stdout)
5 #define GMQCC_IS_STDERR(X) ((X) == stderr)
6 #define GMQCC_IS_DEFINE(X) (GMQCC_IS_STDERR(X) || GMQCC_IS_STDOUT(X))
18 * Enables color on output if supported.
19 * NOTE: The support for checking colors is nullptr. On windows this will
20 * always work, on *nix it depends if the term has colors.
22 * NOTE: This prevents colored output to piped stdout/err via isatty
25 static void con_enablecolor(void) {
26 console.color_err = util_isatty(console.handle_err);
27 console.color_out = util_isatty(console.handle_out);
31 * Does a write to the handle with the format string and list of
32 * arguments. This colorizes for windows as well via translate
35 static int con_write(FILE *handle, const char *fmt, va_list va) {
36 return vfprintf(handle, fmt, va);
39 /**********************************************************************
40 * EXPOSED INTERFACE BEGINS
41 *********************************************************************/
44 if (!GMQCC_IS_DEFINE(console.handle_err))
45 fclose(console.handle_err);
46 if (!GMQCC_IS_DEFINE(console.handle_out))
47 fclose(console.handle_out);
50 void con_color(int state) {
54 console.color_err = 0;
55 console.color_out = 0;
60 console.handle_err = stderr;
61 console.handle_out = stdout;
71 * Defaultizer because stdio.h shouldn't be used anywhere except here
72 * and inside file.c To prevent mis-match of wrapper-interfaces.
74 FILE *con_default_out() {
75 return console.handle_out = stdout;
78 FILE *con_default_err() {
79 return console.handle_err = stderr;
82 int con_verr(const char *fmt, va_list va) {
83 return con_write(console.handle_err, fmt, va);
85 int con_vout(const char *fmt, va_list va) {
86 return con_write(console.handle_out, fmt, va);
90 * Standard stdout/stderr printf functions used generally where they need
93 int con_err(const char *fmt, ...) {
101 int con_out(const char *fmt, ...) {
111 * Utility console message writes for lexer contexts. These will allow
112 * for reporting of file:line based on lexer context, These are used
113 * heavily in the parser/ir/ast.
115 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) {
116 /* color selection table */
123 int err = !!(level == LVL_ERROR);
124 int color = (err) ? console.color_err : console.color_out;
125 int (*print) (const char *, ...) = (err) ? &con_err : &con_out;
126 int (*vprint)(const char *, va_list) = (err) ? &con_verr : &con_vout;
129 print("\033[0;%dm%s:%d:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, (int)column, sel[level], msgtype);
131 print("%s:%d:%d: %s: ", name, (int)line, (int)column, msgtype);
135 print(" [%s]\n", condname);
140 void con_vprintmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap) {
141 con_vprintmsg_c(level, name, line, column, msgtype, msg, ap, nullptr);
144 void con_printmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...) {
147 con_vprintmsg(level, name, line, column, msgtype, msg, va);
151 void con_cvprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
152 con_vprintmsg(lvl, ctx.file, ctx.line, ctx.column, msgtype, msg, ap);
155 void con_cprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, ...) {
158 con_cvprintmsg(ctx, lvl, msgtype, msg, va);
162 /* General error interface: TODO seperate as part of the compiler front-end */
163 size_t compile_errors = 0;
164 size_t compile_warnings = 0;
165 size_t compile_Werrors = 0;
166 static lex_ctx_t first_werror;
168 void compile_show_werrors()
170 con_cprintmsg(first_werror, LVL_ERROR, "first warning", "was here");
173 void vcompile_error(lex_ctx_t ctx, const char *msg, va_list ap)
176 con_cvprintmsg(ctx, LVL_ERROR, "error", msg, ap);
179 void compile_error_(lex_ctx_t ctx, const char *msg, ...)
183 vcompile_error(ctx, msg, ap);
187 bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap)
189 const char *msgtype = "warning";
190 int lvl = LVL_WARNING;
191 char warn_name[1024];
193 if (!OPTS_WARN(warntype))
198 (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
201 if (OPTS_WERROR(warntype)) {
202 if (!compile_Werrors)
206 if (OPTS_FLAG(BAIL_ON_WERROR)) {
213 con_vprintmsg_c(lvl, ctx.file, ctx.line, ctx.column, msgtype, fmt, ap, warn_name);
215 return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR);
218 bool GMQCC_WARN compile_warning_(lex_ctx_t ctx, int warntype, const char *fmt, ...)
223 r = vcompile_warning(ctx, warntype, fmt, ap);