]> git.xonotic.org Git - xonotic/gmqcc.git/blob - conout.c
-Werror-<warning>, -Wno-error-<warning>, manpage updated
[xonotic/gmqcc.git] / conout.c
1 /*
2  * Copyright (C) 2012
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 "gmqcc.h"
24
25 /*
26  * isatty/STDERR_FILENO/STDOUT_FILNO
27  * + some other things likewise.
28  */
29 #ifndef _WIN32
30 #   include <unistd.h>
31 #else
32 #   include <io.h>
33     /*
34      * Windows and it's posix underscore bullshit.  We simply fix this
35      * with yay, another macro :P
36      */
37 #   define isatty _isatty
38 #endif
39
40 #define GMQCC_IS_STDOUT(X) ((FILE*)((void*)X) == stdout)
41 #define GMQCC_IS_STDERR(X) ((FILE*)((void*)X) == stderr)
42 #define GMQCC_IS_DEFINE(X) (GMQCC_IS_STDERR(X) || GMQCC_IS_STDOUT(X))
43
44 typedef struct {
45     FILE *handle_err;
46     FILE *handle_out;
47
48     int   color_err;
49     int   color_out;
50 } con_t;
51
52 /*
53  * Doing colored output on windows is fucking stupid.  The linux way is
54  * the real way. So we emulate it on windows :)
55  */
56 #ifdef _MSC_VER
57 #define WIN32_LEAN_AND_MEAN
58 #include <windows.h>
59
60 /*
61  * Windows doesn't have constants for FILENO, sadly but the docs tell
62  * use the constant values.
63  */
64 #undef  STDERR_FILENO
65 #undef  STDOUT_FILENO
66 #define STDERR_FILENO 2
67 #define STDOUT_FILENO 1
68
69 enum {
70     RESET = 0,
71     BOLD  = 1,
72     BLACK = 30,
73     RED,
74     GREEN,
75     YELLOW,
76     BLUE,
77     MAGENTA,
78     CYAN,
79     GRAY,
80     WHITE
81 };
82
83 enum {
84     WBLACK,
85     WBLUE,
86     WGREEN   = 2,
87     WRED     = 4,
88     WINTENSE = 8,
89     WCYAN    = WBLUE  | WGREEN,
90     WMAGENTA = WBLUE  | WRED,
91     WYELLOW  = WGREEN | WRED,
92     WWHITE   = WBLUE  | WGREEN | WRED
93 }
94
95 static const ansi2win[] = {
96     WBLACK,
97     WRED,
98     WGREEN,
99     WYELLOW,
100     WBLUE,
101     WMAGENTA,
102     WCYAN,
103     WWHITE
104 };
105
106 static void win_fputs(char *str, FILE *h) {
107     /* state for translate */
108     int acolor;
109     int wcolor;
110     int icolor;
111
112     int state;
113     int place;
114
115     /* attributes */
116     int intense  =  -1;
117     int colors[] = {-1, -1 };
118     int colorpos = 1;
119
120     CONSOLE_SCREEN_BUFFER_INFO cinfo;
121     GetConsoleScreenBufferInfo (
122         (GMQCC_IS_STDOUT(h)) ?
123             GetStdHandle(STD_OUTPUT_HANDLE) :
124             GetStdHandle(STD_ERROR_HANDLE), &cinfo
125     );
126     icolor = cinfo.wAttributes;
127
128     while (*str) {
129         if (*str == '\e')
130             state = '\e';
131         else if (state == '\e' && *str == '[')
132             state = '[';
133         else if (state == '[') {
134             if (*str != 'm') {
135                 colors[colorpos] = *str;
136                 colorpos--;
137             } else {
138                 int find;
139                 int mult;
140                 for (find = colorpos + 1, acolor = 0, mult = 1; find < 2; find++) {
141                     acolor += (colors[find] - 48) * mult;
142                     mult   *= 10;
143                 }
144
145                 /* convert to windows color */
146                 if (acolor == BOLD)
147                     intense = WINTENSE;
148                 else if (acolor == RESET) {
149                     intense = WBLACK;
150                     wcolor  = icolor;
151                 }
152                 else if (BLACK < acolor && acolor <= WHITE)
153                     wcolor = ansi2win[acolor - 30];
154                 else if (acolor == 90) {
155                     /* special gray really white man */
156                     wcolor  = WWHITE;
157                     intense = WBLACK;
158                 }
159
160                 SetConsoleTextAttribute (
161                     (h == stdout) ?
162                     GetStdHandle(STD_OUTPUT_HANDLE) :
163                     GetStdHandle(STD_ERROR_HANDLE),
164
165                     wcolor | intense | (icolor & 0xF0)
166                 );
167                 colorpos =  1;
168                 state    = -1;
169             }
170         } else {
171             fputc(*str, h);
172         }
173     }
174     /* restore */
175     SetConsoleTextAttribute(
176         (GMQCC_IS_STDOUT(h)) ?
177         GetStdHandle(STD_OUTPUT_HANDLE) :
178         GetStdHandle(STD_ERROR_HANDLE),
179         icolor
180     );
181 }
182 #endif
183
184 /*
185  * We use standard files as default. These can be changed at any time
186  * with con_change(F, F)
187  */
188 static con_t console;
189
190 /*
191  * Enables color on output if supported.
192  * NOTE: The support for checking colors is NULL.  On windows this will
193  * always work, on *nix it depends if the term has colors.
194  *
195  * NOTE: This prevents colored output to piped stdout/err via isatty
196  * checks.
197  */
198 static void con_enablecolor() {
199     if (console.handle_err == stderr || console.handle_err == stdout)
200         console.color_err = !!(isatty(STDERR_FILENO));
201     if (console.handle_out == stderr || console.handle_out == stdout)
202         console.color_out = !!(isatty(STDOUT_FILENO));
203 }
204
205 /*
206  * Does a write to the handle with the format string and list of
207  * arguments.  This colorizes for windows as well via translate
208  * step.
209  */
210 static int con_write(FILE *handle, const char *fmt, va_list va) {
211     int      ln;
212     #ifndef _MSC_VER
213     ln = vfprintf(handle, fmt, va);
214     #else
215     {
216         char *data = NULL;
217         ln   = _vscprintf(fmt, va);
218         data = malloc(ln + 1);
219         data[ln] = 0;
220         vsprintf(data, fmt, va);
221         if (GMQCC_IS_DEFINE(handle))
222             ln = win_fputs(data, handle);
223         else
224             ln = fputs(data, handle);
225         free(data);
226     }
227     #endif
228     return ln;
229 }
230
231 /**********************************************************************
232  * EXPOSED INTERFACE BEGINS
233  *********************************************************************/
234
235 void con_close() {
236     if (!GMQCC_IS_DEFINE(console.handle_err))
237         fclose(console.handle_err);
238     if (!GMQCC_IS_DEFINE(console.handle_out))
239         fclose(console.handle_out);
240 }
241
242 void con_color(int state) {
243     if (state)
244         con_enablecolor();
245     else {
246         console.color_err = 0;
247         console.color_out = 0;
248     }
249 }
250
251 void con_init() {
252     console.handle_err = stderr;
253     console.handle_out = stdout;
254     con_enablecolor();
255 }
256
257 void con_reset() {
258     con_close();
259     con_init ();
260 }
261
262 /*
263  * This is clever, say you want to change the console to use two
264  * files for out/err.  You pass in two strings, it will properly
265  * close the existing handles (if they're not std* handles) and
266  * open them.  Now say you want TO use stdout and stderr, this
267  * allows you to do that so long as you cast them to (char*).
268  * Say you need stdout for out, but want a file for error, you can
269  * do this too, just cast the stdout for (char*) and stick to a
270  * string for the error file.
271  */
272 int con_change(const char *out, const char *err) {
273     con_close();
274
275     if (GMQCC_IS_DEFINE(out)) {
276         console.handle_out = GMQCC_IS_STDOUT(out) ? stdout : stderr;
277         con_enablecolor();
278     } else if (!(console.handle_out = fopen(out, "w"))) return 0;
279
280     if (GMQCC_IS_DEFINE(err)) {
281         console.handle_err = GMQCC_IS_STDOUT(err) ? stdout : stderr;
282         con_enablecolor();
283     } else if (!(console.handle_err = fopen(err, "w"))) return 0;
284
285     /* no buffering */
286     setvbuf(console.handle_out, NULL, _IONBF, 0);
287     setvbuf(console.handle_err, NULL, _IONBF, 0);
288
289     return 1;
290 }
291
292 int con_verr(const char *fmt, va_list va) {
293     return con_write(console.handle_err, fmt, va);
294 }
295 int con_vout(const char *fmt, va_list va) {
296     return con_write(console.handle_out, fmt, va);
297 }
298
299 /*
300  * Standard stdout/stderr printf functions used generally where they need
301  * to be used.
302  */
303 int con_err(const char *fmt, ...) {
304     va_list  va;
305     int      ln = 0;
306     va_start(va, fmt);
307     con_verr(fmt, va);
308     va_end  (va);
309     return   ln;
310 }
311 int con_out(const char *fmt, ...) {
312     va_list  va;
313     int      ln = 0;
314     va_start(va, fmt);
315     con_vout(fmt, va);
316     va_end  (va);
317     return   ln;
318 }
319
320 /*
321  * Utility console message writes for lexer contexts.  These will allow
322  * for reporting of file:line based on lexer context, These are used
323  * heavily in the parser/ir/ast.
324  */
325 void con_vprintmsg_c(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap, const char *condname) {
326     /* color selection table */
327     static int sel[] = {
328         CON_WHITE,
329         CON_CYAN,
330         CON_RED
331     };
332
333     int  err                         = !!(level == LVL_ERROR);
334     int  color                       = (err) ? console.color_err : console.color_out;
335     int (*print) (const char *, ...)  = (err) ? &con_err          : &con_out;
336     int (*vprint)(const char *, va_list) = (err) ? &con_verr : &con_vout;
337
338     if (color)
339         print("\033[0;%dm%s:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, sel[level], msgtype);
340     else
341         print("%s:%d: %s: ", name, (int)line, msgtype);
342
343     vprint(msg, ap);
344     if (condname)
345         print(" [%s]\n", condname);
346     else
347         print("\n");
348 }
349
350 void con_vprintmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap) {
351     con_vprintmsg_c(level, name, line, msgtype, msg, ap, NULL);
352 }
353
354 void con_printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) {
355     va_list   va;
356     va_start(va, msg);
357     con_vprintmsg(level, name, line, msgtype, msg, va);
358     va_end  (va);
359 }
360
361 void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
362     con_vprintmsg(lvl, ((lex_ctx*)ctx)->file, ((lex_ctx*)ctx)->line, msgtype, msg, ap);
363 }
364
365 void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...) {
366     va_list   va;
367     va_start(va, msg);
368     con_cvprintmsg(ctx, lvl, msgtype, msg, va);
369     va_end  (va);
370 }
371
372 /* General error interface */
373 size_t compile_errors = 0;
374 size_t compile_warnings = 0;
375
376 void vcompile_error(lex_ctx ctx, const char *msg, va_list ap)
377 {
378     ++compile_errors;
379     con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap);
380 }
381
382 void compile_error(lex_ctx ctx, const char *msg, ...)
383 {
384     va_list ap;
385     va_start(ap, msg);
386     vcompile_error(ctx, msg, ap);
387     va_end(ap);
388 }
389
390 bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_list ap)
391 {
392     int lvl = LVL_WARNING;
393     char warn_name[1024];
394
395     if (!OPTS_WARN(warntype))
396         return false;
397
398     warn_name[0] = '-';
399     warn_name[1] = 'W';
400     (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
401
402     if (OPTS_WERROR(warntype)) {
403         ++compile_errors;
404         lvl = LVL_ERROR;
405     }
406     else
407         ++compile_warnings;
408
409     con_vprintmsg_c(lvl, ctx.file, ctx.line, (opts.werror ? "error" : "warning"), fmt, ap, warn_name);
410
411     return OPTS_WERROR(warntype);
412 }
413
414 bool GMQCC_WARN compile_warning(lex_ctx ctx, int warntype, const char *fmt, ...)
415 {
416     bool r;
417     va_list ap;
418     va_start(ap, fmt);
419     r = vcompile_warning(ctx, warntype, fmt, ap);
420     va_end(ap);
421     return r;
422 }