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 * isatty/STDERR_FILENO/STDOUT_FILNO
27 * + some other things likewise.
34 * Windows and it's posix underscore bullshit. We simply fix this
35 * with yay, another macro :P
37 # define isatty _isatty
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))
53 * Doing colored output on windows is fucking stupid. The linux way is
54 * the real way. So we emulate it on windows :)
57 #define WIN32_LEAN_AND_MEAN
61 * Windows doesn't have constants for FILENO, sadly but the docs tell
62 * use the constant values.
66 #define STDERR_FILENO 2
67 #define STDOUT_FILENO 1
89 WCYAN = WBLUE | WGREEN,
90 WMAGENTA = WBLUE | WRED,
91 WYELLOW = WGREEN | WRED,
92 WWHITE = WBLUE | WGREEN | WRED
95 static const int ansi2win[] = {
106 static int win_fputs(const char *str, FILE *h) {
107 /* state for translate */
116 int colors[] = {-1, -1 };
119 CONSOLE_SCREEN_BUFFER_INFO cinfo;
120 GetConsoleScreenBufferInfo (
121 (GMQCC_IS_STDOUT(h)) ?
122 GetStdHandle(STD_OUTPUT_HANDLE) :
123 GetStdHandle(STD_ERROR_HANDLE), &cinfo
125 icolor = cinfo.wAttributes;
130 else if (state == '\x1B' && *str == '[')
132 else if (state == '[') {
134 colors[colorpos] = *str;
139 for (find = colorpos + 1, acolor = 0, mult = 1; find < 2; find++) {
140 acolor += (colors[find] - 48) * mult;
144 /* convert to windows color */
147 else if (acolor == RESET) {
151 else if (BLACK <= acolor && acolor <= WHITE)
152 wcolor = ansi2win[acolor - 30];
153 else if (acolor == 90) {
154 /* special gray really white man */
159 SetConsoleTextAttribute (
160 (GMQCC_IS_STDOUT(h)) ?
161 GetStdHandle(STD_OUTPUT_HANDLE) :
162 GetStdHandle(STD_ERROR_HANDLE),
164 wcolor | intense | (icolor & 0xF0)
176 SetConsoleTextAttribute(
177 (GMQCC_IS_STDOUT(h)) ?
178 GetStdHandle(STD_OUTPUT_HANDLE) :
179 GetStdHandle(STD_ERROR_HANDLE),
187 * We use standard files as default. These can be changed at any time
188 * with con_change(F, F)
190 static con_t console;
193 * Enables color on output if supported.
194 * NOTE: The support for checking colors is NULL. On windows this will
195 * always work, on *nix it depends if the term has colors.
197 * NOTE: This prevents colored output to piped stdout/err via isatty
200 static void con_enablecolor() {
201 if (console.handle_err == stderr || console.handle_err == stdout)
202 console.color_err = !!(isatty(STDERR_FILENO));
203 if (console.handle_out == stderr || console.handle_out == stdout)
204 console.color_out = !!(isatty(STDOUT_FILENO));
208 * Does a write to the handle with the format string and list of
209 * arguments. This colorizes for windows as well via translate
212 static int con_write(FILE *handle, const char *fmt, va_list va) {
215 ln = vfprintf(handle, fmt, va);
219 memset(data, 0, sizeof(data));
220 vsnprintf(data, sizeof(data), fmt, va);
221 ln = (GMQCC_IS_DEFINE(handle)) ? win_fputs(data, handle) : fputs(data, handle);
227 /**********************************************************************
228 * EXPOSED INTERFACE BEGINS
229 *********************************************************************/
232 if (!GMQCC_IS_DEFINE(console.handle_err))
233 fclose(console.handle_err);
234 if (!GMQCC_IS_DEFINE(console.handle_out))
235 fclose(console.handle_out);
238 void con_color(int state) {
242 console.color_err = 0;
243 console.color_out = 0;
248 console.handle_err = stderr;
249 console.handle_out = stdout;
259 * This is clever, say you want to change the console to use two
260 * files for out/err. You pass in two strings, it will properly
261 * close the existing handles (if they're not std* handles) and
262 * open them. Now say you want TO use stdout and stderr, this
263 * allows you to do that so long as you cast them to (char*).
264 * Say you need stdout for out, but want a file for error, you can
265 * do this too, just cast the stdout for (char*) and stick to a
266 * string for the error file.
268 int con_change(const char *out, const char *err) {
271 if (GMQCC_IS_DEFINE(out)) {
272 console.handle_out = GMQCC_IS_STDOUT(out) ? stdout : stderr;
274 } else if (!(console.handle_out = fopen(out, "w"))) return 0;
276 if (GMQCC_IS_DEFINE(err)) {
277 console.handle_err = GMQCC_IS_STDOUT(err) ? stdout : stderr;
279 } else if (!(console.handle_err = fopen(err, "w"))) return 0;
282 setvbuf(console.handle_out, NULL, _IONBF, 0);
283 setvbuf(console.handle_err, NULL, _IONBF, 0);
288 int con_verr(const char *fmt, va_list va) {
289 return con_write(console.handle_err, fmt, va);
291 int con_vout(const char *fmt, va_list va) {
292 return con_write(console.handle_out, fmt, va);
296 * Standard stdout/stderr printf functions used generally where they need
299 int con_err(const char *fmt, ...) {
307 int con_out(const char *fmt, ...) {
317 * Utility console message writes for lexer contexts. These will allow
318 * for reporting of file:line based on lexer context, These are used
319 * heavily in the parser/ir/ast.
321 void con_vprintmsg_c(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap, const char *condname) {
322 /* color selection table */
329 int err = !!(level == LVL_ERROR);
330 int color = (err) ? console.color_err : console.color_out;
331 int (*print) (const char *, ...) = (err) ? &con_err : &con_out;
332 int (*vprint)(const char *, va_list) = (err) ? &con_verr : &con_vout;
335 print("\033[0;%dm%s:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, sel[level], msgtype);
337 print("%s:%d: %s: ", name, (int)line, msgtype);
341 print(" [%s]\n", condname);
346 void con_vprintmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap) {
347 con_vprintmsg_c(level, name, line, msgtype, msg, ap, NULL);
350 void con_printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) {
353 con_vprintmsg(level, name, line, msgtype, msg, va);
357 void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
358 con_vprintmsg(lvl, ((lex_ctx*)ctx)->file, ((lex_ctx*)ctx)->line, msgtype, msg, ap);
361 void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...) {
364 con_cvprintmsg(ctx, lvl, msgtype, msg, va);
368 /* General error interface */
369 size_t compile_errors = 0;
370 size_t compile_warnings = 0;
372 void vcompile_error(lex_ctx ctx, const char *msg, va_list ap)
375 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap);
378 void compile_error(lex_ctx ctx, const char *msg, ...)
382 vcompile_error(ctx, msg, ap);
386 bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_list ap)
388 int lvl = LVL_WARNING;
389 char warn_name[1024];
391 if (!OPTS_WARN(warntype))
396 (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
398 if (OPTS_WERROR(warntype)) {
405 con_vprintmsg_c(lvl, ctx.file, ctx.line, ((lvl == LVL_ERROR) ? "error" : "warning"), fmt, ap, warn_name);
407 return OPTS_WERROR(warntype);
410 bool GMQCC_WARN compile_warning(lex_ctx ctx, int warntype, const char *fmt, ...)
415 r = vcompile_warning(ctx, warntype, fmt, ap);