X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=con.c;h=8073a5723d71286568e5d0c164d3e34059af924f;hb=d1fd7d044ba1470dd6c66b1f265ec3aafb829b07;hp=1da837fdb7496c3502caabfca904aedd63c21b14;hpb=f0678cfa5a4ec33feee9135fa154eaa17d14bac0;p=xonotic%2Fgmqcc.git diff --git a/con.c b/con.c index 1da837f..8073a57 100644 --- a/con.c +++ b/con.c @@ -118,7 +118,7 @@ static void win_fputs(char *str, FILE *f) { CONSOLE_SCREEN_BUFFER_INFO cinfo; GetConsoleScreenBufferInfo( - (h == stdout) ? + (GMQCC_IS_STDOUT(h)) ? GetStdHandle(STD_OUTPUT_HANDLE) : GetStdHandle(STD_ERROR_HANDLE), &cinfo ); @@ -172,7 +172,7 @@ static void win_fputs(char *str, FILE *f) { } /* restore */ SetConsoleTextAttribute( - (h == stdout) ? + (GMQCC_IS_STDOUT(h)) ? GetStdHandle(STD_OUTPUT_HANDLE) : GetStdHandle(STD_ERROR_HANDLE), icolor @@ -199,46 +199,6 @@ static void con_enablecolor() { console.color_err = !!(isatty(STDERR_FILENO)); if (console.handle_out == stderr || console.handle_out == stdout) console.color_out = !!(isatty(STDOUT_FILENO)); - - #ifndef _WIN32 - { - char buf[4] = {0, 0, 0, 0}; - - /* - * This is such a hack. But I'm not linking in any libraries to - * do this stupidity. It's insane there is simply not a ttyhascolor - * in unistd.h - */ - FILE *tput = popen("tput colors", "r"); - if (!tput) { - /* - * disable colors since we can't determine without tput - * which should be guranteed on all *nix OSes - */ - console.color_err = 0; - console.color_out = 0; - - return; - } - - /* - * Handle to tput was a success, lets read in the amount of - * color support. It should be at minimal 8. - */ - fread(buf, 1, sizeof(buf)-1, tput); - - if (atoi(buf) < 8) { - console.color_err = 0; - console.color_out = 0; - } - - /* - * We made it this far, which means we support colors in the - * terminal. - */ - fclose(tput); - } - #endif } /* @@ -312,7 +272,7 @@ int con_change(const char *out, const char *err) { con_close(); if (GMQCC_IS_DEFINE((FILE*)out)) { - console.handle_out = (((FILE*)err) == stdout) ? stdout : stderr; + console.handle_out = (((FILE*)out) == stdout) ? stdout : stderr; con_enablecolor(); } else if (!(console.handle_out = fopen(out, "w"))) return 0; @@ -321,6 +281,10 @@ int con_change(const char *out, const char *err) { con_enablecolor(); } else if (!(console.handle_err = fopen(err, "w"))) return 0; + // no buffering + setvbuf(console.handle_out, NULL, _IONBF, 0); + setvbuf(console.handle_err, NULL, _IONBF, 0); + return 1; } @@ -331,6 +295,10 @@ int con_vout(const char *fmt, va_list va) { return con_write(console.handle_out, fmt, va); } +/* + * Standard stdout/stderr printf functions used generally where they need + * to be used. + */ int con_err(const char *fmt, ...) { va_list va; int ln = 0; @@ -347,3 +315,47 @@ int con_out(const char *fmt, ...) { va_end (va); return ln; } + +/* + * Utility console message writes for lexer contexts. These will allow + * for reporting of file:line based on lexer context, These are used + * heavily in the parser/ir/ast. + */ +void con_vprintmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap) { + /* color selection table */ + static int sel[] = { + CON_WHITE, + CON_CYAN, + CON_RED + }; + + int err = !!(level == LVL_ERROR); + int color = (err) ? console.color_err : console.color_out; + int (*print)(const char *, ...) = (err) ? &con_err : &con_out; + + if (color) + print("\033[0;%dm%s:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, sel[level], msgtype); + else + print("%s:%d: %s: ", name, (int)line, msgtype); + + con_verr(msg, ap); + fprintf (stderr, "\n"); +} + +void con_printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) { + va_list va; + va_start(va, msg); + con_vprintmsg(level, name, line, msgtype, msg, va); + va_end (va); +} + +void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap) { + con_vprintmsg(lvl, ((lex_ctx*)ctx)->file, ((lex_ctx*)ctx)->line, msgtype, msg, ap); +} + +void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...) { + va_list va; + va_start(va, msg); + con_cvprintmsg(ctx, lvl, msgtype, msg, va); + va_end (va); +}