]> git.xonotic.org Git - xonotic/gmqcc.git/blob - conout.c
Remove these too
[xonotic/gmqcc.git] / conout.c
1 /*
2  * Copyright (C) 2012, 2013, 2014, 2015
3  *     Dale Weiler
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * SOFTWARE.
22  */
23 #include <stdio.h>
24 #include "gmqcc.h"
25
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))
29
30 typedef struct {
31     FILE *handle_err;
32     FILE *handle_out;
33     int color_err;
34     int color_out;
35 } con_t;
36
37 static con_t console;
38
39 /*
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.
43  *
44  * NOTE: This prevents colored output to piped stdout/err via isatty
45  * checks.
46  */
47 static void con_enablecolor(void) {
48     console.color_err = util_isatty(console.handle_err);
49     console.color_out = util_isatty(console.handle_out);
50 }
51
52 /*
53  * Does a write to the handle with the format string and list of
54  * arguments.  This colorizes for windows as well via translate
55  * step.
56  */
57 static int con_write(FILE *handle, const char *fmt, va_list va) {
58     return vfprintf(handle, fmt, va);
59 }
60
61 /**********************************************************************
62  * EXPOSED INTERFACE BEGINS
63  *********************************************************************/
64
65 void con_close() {
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);
70 }
71
72 void con_color(int state) {
73     if (state)
74         con_enablecolor();
75     else {
76         console.color_err = 0;
77         console.color_out = 0;
78     }
79 }
80
81 void con_init() {
82     console.handle_err = stderr;
83     console.handle_out = stdout;
84     con_enablecolor();
85 }
86
87 void con_reset() {
88     con_close();
89     con_init();
90 }
91
92 /*
93  * Defaultizer because stdio.h shouldn't be used anywhere except here
94  * and inside file.c To prevent mis-match of wrapper-interfaces.
95  */
96 FILE *con_default_out() {
97     return console.handle_out = stdout;
98 }
99
100 FILE *con_default_err() {
101     return console.handle_err = stderr;
102 }
103
104 int con_verr(const char *fmt, va_list va) {
105     return con_write(console.handle_err, fmt, va);
106 }
107 int con_vout(const char *fmt, va_list va) {
108     return con_write(console.handle_out, fmt, va);
109 }
110
111 /*
112  * Standard stdout/stderr printf functions used generally where they need
113  * to be used.
114  */
115 int con_err(const char *fmt, ...) {
116     va_list va;
117     int ln = 0;
118     va_start(va, fmt);
119     con_verr(fmt, va);
120     va_end(va);
121     return ln;
122 }
123 int con_out(const char *fmt, ...) {
124     va_list va;
125     int ln = 0;
126     va_start(va, fmt);
127     con_vout(fmt, va);
128     va_end (va);
129     return ln;
130 }
131
132 /*
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.
136  */
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 */
139     static int sel[] = {
140         CON_WHITE,
141         CON_CYAN,
142         CON_RED
143     };
144
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;
149
150     if (color)
151         print("\033[0;%dm%s:%d:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, (int)column, sel[level], msgtype);
152     else
153         print("%s:%d:%d: %s: ", name, (int)line, (int)column, msgtype);
154
155     vprint(msg, ap);
156     if (condname)
157         print(" [%s]\n", condname);
158     else
159         print("\n");
160 }
161
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);
164 }
165
166 void con_printmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...) {
167     va_list   va;
168     va_start(va, msg);
169     con_vprintmsg(level, name, line, column, msgtype, msg, va);
170     va_end  (va);
171 }
172
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);
175 }
176
177 void con_cprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, ...) {
178     va_list   va;
179     va_start(va, msg);
180     con_cvprintmsg(ctx, lvl, msgtype, msg, va);
181     va_end  (va);
182 }
183
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;
189
190 void compile_show_werrors()
191 {
192     con_cprintmsg(first_werror, LVL_ERROR, "first warning", "was here");
193 }
194
195 void vcompile_error(lex_ctx_t ctx, const char *msg, va_list ap)
196 {
197     ++compile_errors;
198     con_cvprintmsg(ctx, LVL_ERROR, "error", msg, ap);
199 }
200
201 void compile_error(lex_ctx_t ctx, const char *msg, ...)
202 {
203     va_list ap;
204     va_start(ap, msg);
205     vcompile_error(ctx, msg, ap);
206     va_end(ap);
207 }
208
209 bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap)
210 {
211     const char *msgtype = "warning";
212     int         lvl     = LVL_WARNING;
213     char        warn_name[1024];
214
215     if (!OPTS_WARN(warntype))
216         return false;
217
218     warn_name[0] = '-';
219     warn_name[1] = 'W';
220     (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
221
222     ++compile_warnings;
223     if (OPTS_WERROR(warntype)) {
224         if (!compile_Werrors)
225             first_werror = ctx;
226         ++compile_Werrors;
227         msgtype = "Werror";
228         if (OPTS_FLAG(BAIL_ON_WERROR)) {
229             msgtype = "error";
230             ++compile_errors;
231         }
232         lvl = LVL_ERROR;
233     }
234
235     con_vprintmsg_c(lvl, ctx.file, ctx.line, ctx.column, msgtype, fmt, ap, warn_name);
236
237     return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR);
238 }
239
240 bool GMQCC_WARN compile_warning(lex_ctx_t ctx, int warntype, const char *fmt, ...)
241 {
242     bool r;
243     va_list ap;
244     va_start(ap, fmt);
245     r = vcompile_warning(ctx, warntype, fmt, ap);
246     va_end(ap);
247     return r;
248 }