]> git.xonotic.org Git - xonotic/gmqcc.git/blob - conout.c
5f8454d910027ca2f74e4a3e79e3afa39f5707e7
[xonotic/gmqcc.git] / conout.c
1 /*
2  * Copyright (C) 2012, 2013
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 #include "platform.h"
25
26 #define GMQCC_IS_STDOUT(X) ((fs_file_t*)((void*)X) == (fs_file_t*)stdout)
27 #define GMQCC_IS_STDERR(X) ((fs_file_t*)((void*)X) == (fs_file_t*)stderr)
28 #define GMQCC_IS_DEFINE(X) (GMQCC_IS_STDERR(X) || GMQCC_IS_STDOUT(X))
29
30 typedef struct {
31     fs_file_t *handle_err;
32     fs_file_t *handle_out;
33     int        color_err;
34     int        color_out;
35 } con_t;
36
37 /*
38  * We use standard files as default. These can be changed at any time
39  * with con_change(F, F)
40  */
41 static con_t console;
42
43 /*
44  * Enables color on output if supported.
45  * NOTE: The support for checking colors is NULL.  On windows this will
46  * always work, on *nix it depends if the term has colors.
47  *
48  * NOTE: This prevents colored output to piped stdout/err via isatty
49  * checks.
50  */
51 static void con_enablecolor(void) {
52     if (console.handle_err == (fs_file_t*)stderr || console.handle_err == (fs_file_t*)stdout)
53         console.color_err = !!(platform_isatty(STDERR_FILENO));
54     if (console.handle_out == (fs_file_t*)stderr || console.handle_out == (fs_file_t*)stdout)
55         console.color_out = !!(platform_isatty(STDOUT_FILENO));
56 }
57
58 /*
59  * Does a write to the handle with the format string and list of
60  * arguments.  This colorizes for windows as well via translate
61  * step.
62  */
63 static int con_write(fs_file_t *handle, const char *fmt, va_list va) {
64     return vfprintf((FILE*)handle, fmt, va);
65 }
66
67 /**********************************************************************
68  * EXPOSED INTERFACE BEGINS
69  *********************************************************************/
70
71 void con_close() {
72     if (!GMQCC_IS_DEFINE(console.handle_err))
73         fs_file_close(console.handle_err);
74     if (!GMQCC_IS_DEFINE(console.handle_out))
75         fs_file_close(console.handle_out);
76 }
77
78 void con_color(int state) {
79     if (state)
80         con_enablecolor();
81     else {
82         console.color_err = 0;
83         console.color_out = 0;
84     }
85 }
86
87 void con_init() {
88     console.handle_err = (fs_file_t*)stderr;
89     console.handle_out = (fs_file_t*)stdout;
90     con_enablecolor();
91 }
92
93 void con_reset() {
94     con_close();
95     con_init ();
96 }
97
98 /*
99  * This is clever, say you want to change the console to use two
100  * files for out/err.  You pass in two strings, it will properly
101  * close the existing handles (if they're not std* handles) and
102  * open them.  Now say you want TO use stdout and stderr, this
103  * allows you to do that so long as you cast them to (char*).
104  * Say you need stdout for out, but want a file for error, you can
105  * do this too, just cast the stdout for (char*) and stick to a
106  * string for the error file.
107  */
108 int con_change(const char *out, const char *err) {
109     con_close();
110
111     if (!out) out = (const char *)((!console.handle_out) ? (fs_file_t*)stdout : console.handle_out);
112     if (!err) err = (const char *)((!console.handle_err) ? (fs_file_t*)stderr : console.handle_err);
113
114     if (GMQCC_IS_DEFINE(out)) {
115         console.handle_out = (fs_file_t*)(GMQCC_IS_STDOUT(out) ? stdout : stderr);
116         con_enablecolor();
117     } else if (!(console.handle_out = fs_file_open(out, "w"))) return 0;
118
119     if (GMQCC_IS_DEFINE(err)) {
120         console.handle_err = (fs_file_t*)(GMQCC_IS_STDOUT(err) ? stdout : stderr);
121         con_enablecolor();
122     } else if (!(console.handle_err = fs_file_open(err, "w"))) return 0;
123
124     return 1;
125 }
126
127 /*
128  * Defaultizer because stdio.h shouldn't be used anywhere except here
129  * and inside file.c To prevent mis-match of wrapper-interfaces.
130  */
131 fs_file_t *con_default_out() {
132     return (fs_file_t*)(console.handle_out = (fs_file_t*)stdout);
133 }
134 fs_file_t *con_default_err() {
135     return (fs_file_t*)(console.handle_err = (fs_file_t*)stderr);
136 }
137
138 int con_verr(const char *fmt, va_list va) {
139     return con_write(console.handle_err, fmt, va);
140 }
141 int con_vout(const char *fmt, va_list va) {
142     return con_write(console.handle_out, fmt, va);
143 }
144
145 /*
146  * Standard stdout/stderr printf functions used generally where they need
147  * to be used.
148  */
149 int con_err(const char *fmt, ...) {
150     va_list  va;
151     int      ln = 0;
152     va_start(va, fmt);
153     con_verr(fmt, va);
154     va_end  (va);
155     return   ln;
156 }
157 int con_out(const char *fmt, ...) {
158     va_list  va;
159     int      ln = 0;
160     va_start(va, fmt);
161     con_vout(fmt, va);
162     va_end  (va);
163     return   ln;
164 }
165
166 /*
167  * Utility console message writes for lexer contexts.  These will allow
168  * for reporting of file:line based on lexer context, These are used
169  * heavily in the parser/ir/ast.
170  */
171 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) {
172     /* color selection table */
173     static int sel[] = {
174         CON_WHITE,
175         CON_CYAN,
176         CON_RED
177     };
178
179     int  err                             = !!(level == LVL_ERROR);
180     int  color                           = (err) ? console.color_err : console.color_out;
181     int (*print) (const char *, ...)     = (err) ? &con_err          : &con_out;
182     int (*vprint)(const char *, va_list) = (err) ? &con_verr         : &con_vout;
183
184     if (color)
185         print("\033[0;%dm%s:%d:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, (int)column, sel[level], msgtype);
186     else
187         print("%s:%d:%d: %s: ", name, (int)line, (int)column, msgtype);
188
189     vprint(msg, ap);
190     if (condname)
191         print(" [%s]\n", condname);
192     else
193         print("\n");
194 }
195
196 void con_vprintmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap) {
197     con_vprintmsg_c(level, name, line, column, msgtype, msg, ap, NULL);
198 }
199
200 void con_printmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...) {
201     va_list   va;
202     va_start(va, msg);
203     con_vprintmsg(level, name, line, column, msgtype, msg, va);
204     va_end  (va);
205 }
206
207 void con_cvprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
208     con_vprintmsg(lvl, ctx.file, ctx.line, ctx.column, msgtype, msg, ap);
209 }
210
211 void con_cprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, ...) {
212     va_list   va;
213     va_start(va, msg);
214     con_cvprintmsg(ctx, lvl, msgtype, msg, va);
215     va_end  (va);
216 }
217
218 #ifndef QCVM_EXECUTOR
219 /* General error interface */
220 size_t compile_errors   = 0;
221 size_t compile_warnings = 0;
222 size_t compile_Werrors  = 0;
223 static lex_ctx_t first_werror;
224
225 void compile_show_werrors()
226 {
227     con_cprintmsg(first_werror, LVL_ERROR, "first warning", "was here");
228 }
229
230 void vcompile_error(lex_ctx_t ctx, const char *msg, va_list ap)
231 {
232     ++compile_errors;
233     con_cvprintmsg(ctx, LVL_ERROR, "error", msg, ap);
234 }
235
236 void compile_error(lex_ctx_t ctx, const char *msg, ...)
237 {
238     va_list ap;
239     va_start(ap, msg);
240     vcompile_error(ctx, msg, ap);
241     va_end(ap);
242 }
243
244 bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap)
245 {
246     const char *msgtype = "warning";
247     int         lvl     = LVL_WARNING;
248     char        warn_name[1024];
249
250     if (!OPTS_WARN(warntype))
251         return false;
252
253     warn_name[0] = '-';
254     warn_name[1] = 'W';
255     (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
256
257     ++compile_warnings;
258     if (OPTS_WERROR(warntype)) {
259         if (!compile_Werrors)
260             first_werror = ctx;
261         ++compile_Werrors;
262         msgtype = "Werror";
263         if (OPTS_FLAG(BAIL_ON_WERROR)) {
264             msgtype = "error";
265             ++compile_errors;
266         }
267         lvl = LVL_ERROR;
268     }
269
270     con_vprintmsg_c(lvl, ctx.file, ctx.line, ctx.column, msgtype, fmt, ap, warn_name);
271
272     return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR);
273 }
274
275 bool GMQCC_WARN compile_warning(lex_ctx_t ctx, int warntype, const char *fmt, ...)
276 {
277     bool r;
278     va_list ap;
279     va_start(ap, fmt);
280     r = vcompile_warning(ctx, warntype, fmt, ap);
281     va_end(ap);
282     return r;
283 }
284 #endif