]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - con.c
fixes
[xonotic/gmqcc.git] / con.c
diff --git a/con.c b/con.c
index 7ab5fcbe4caa398dd9a81ab9d5ed389e32f6032e..7406caed3fc67ab25a01bdc476645cb9b395d253 100644 (file)
--- a/con.c
+++ b/con.c
 #include <unistd.h>
 #endif
 
-#define GMQCC_IS_STDOUT(X) ((X) == stdout)
-#define GMQCC_IS_STDERR(X) ((X) == stderr)
+#define GMQCC_IS_STDOUT(X) ((FILE*)((void*)X) == stdout)
+#define GMQCC_IS_STDERR(X) ((FILE*)((void*)X) == stderr)
 #define GMQCC_IS_DEFINE(X) (GMQCC_IS_STDERR(X) || GMQCC_IS_STDOUT(X))
 
 typedef struct {
     FILE *handle_err;
     FILE *handle_out;
-    
+
     int   color_err;
     int   color_out;
 } con_t;
@@ -46,7 +46,7 @@ typedef struct {
  * Doing colored output on windows is fucking stupid.  The linux way is
  * the real way. So we emulate it on windows :)
  */
-#ifdef _WIN32
+#ifdef _MSC_VER
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
@@ -64,7 +64,7 @@ typedef struct {
  * with yay, another macro :P
  */
 #define isatty _isatty
+
 enum {
     RESET = 0,
     BOLD  = 1,
@@ -102,28 +102,28 @@ static const ansi2win[] = {
     WWHITE
 };
 
-static void win_fputs(char *str, FILE *f) {
+static void win_fputs(char *str, FILE *h) {
     /* state for translate */
     int acolor;
     int wcolor;
     int icolor;
-    
+
     int state;
     int place;
-    
+
     /* attributes */
     int intense  =  -1;
     int colors[] = {-1, -1 };
     int colorpos = 1;
-    
+
     CONSOLE_SCREEN_BUFFER_INFO cinfo;
-    GetConsoleScreenBufferInfo(
-        (h == stdout) ?
+    GetConsoleScreenBufferInfo (
+        (GMQCC_IS_STDOUT(h)) ?
             GetStdHandle(STD_OUTPUT_HANDLE) :
             GetStdHandle(STD_ERROR_HANDLE), &cinfo
     );
     icolor = cinfo.wAttributes;
-    
+
     while (*str) {
         if (*str == '\e')
             state = '\e';
@@ -140,7 +140,7 @@ static void win_fputs(char *str, FILE *f) {
                     acolor += (colors[find] - 48) * mult;
                     mult   *= 10;
                 }
-                
+
                 /* convert to windows color */
                 if (acolor == BOLD)
                     intense = WINTENSE;
@@ -155,12 +155,12 @@ static void win_fputs(char *str, FILE *f) {
                     wcolor  = WWHITE;
                     intense = WBLACK;
                 }
-                
-                SetConsoleTextattribute(
+
+                SetConsoleTextAttribute (
                     (h == stdout) ?
                     GetStdHandle(STD_OUTPUT_HANDLE) :
                     GetStdHandle(STD_ERROR_HANDLE),
-                    
+
                     wcolor | intense | (icolor & 0xF0)
                 );
                 colorpos =  1;
@@ -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
@@ -190,7 +190,7 @@ static con_t console;
  * Enables color on output if supported.
  * NOTE: The support for checking colors is NULL.  On windows this will
  * always work, on *nix it depends if the term has colors.
- * 
+ *
  * NOTE: This prevents colored output to piped stdout/err via isatty
  * checks.
  */
@@ -208,7 +208,7 @@ static void con_enablecolor() {
  */
 static int con_write(FILE *handle, const char *fmt, va_list va) {
     int      ln;
-    #ifndef _WIN32
+    #ifndef _MSC_VER
     ln = vfprintf(handle, fmt, va);
     #else
     {
@@ -270,17 +270,21 @@ void con_reset() {
  */
 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;
+
+    if (GMQCC_IS_DEFINE(out)) {
+        console.handle_out = GMQCC_IS_STDOUT(out) ? stdout : stderr;
         con_enablecolor();
     } else if (!(console.handle_out = fopen(out, "w"))) return 0;
-    
-    if (GMQCC_IS_DEFINE((FILE*)err)) {
-        console.handle_err = (((FILE*)err) == stdout) ? stdout : stderr;
+
+    if (GMQCC_IS_DEFINE(err)) {
+        console.handle_err = GMQCC_IS_STDOUT(err) ? stdout : stderr;
         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;
 }
 
@@ -312,38 +316,38 @@ int con_out(const char *fmt, ...) {
     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) {
+void con_vprintmsg_c(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap, const char *condname) {
     /* 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;
-    
-    /* this might confuse you :P */
-    ((err) ? &con_err : &con_out)(
-        (color) ? 
-            "\033[0;%dm%s:%d: \033[0;%dm%s: \033[0m" : 
-            "%s:%d: %s: ",
-            
-        CON_CYAN,
-        name,
-        (int)line,
-        sel[level],
-        msgtype
-    );
-        
-    con_verr(msg, ap);
-    fprintf (stderr, "\n");
+
+    int  err                         = !!(level == LVL_ERROR);
+    int  color                       = (err) ? console.color_err : console.color_out;
+    int (*print) (const char *, ...)  = (err) ? &con_err          : &con_out;
+    int (*vprint)(const char *, va_list) = (err) ? &con_verr : &con_vout;
+
+    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);
+
+    vprint(msg, ap);
+    if (condname)
+        print(" [%s]\n", condname);
+    else
+        print("\n");
+}
+
+void con_vprintmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap) {
+    con_vprintmsg_c(level, name, line, msgtype, msg, ap, NULL);
 }
 
 void con_printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) {
@@ -363,3 +367,55 @@ void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ..
     con_cvprintmsg(ctx, lvl, msgtype, msg, va);
     va_end  (va);
 }
+
+/* General error interface */
+size_t compile_errors = 0;
+size_t compile_warnings = 0;
+
+void vcompile_error(lex_ctx ctx, const char *msg, va_list ap)
+{
+    ++compile_errors;
+    con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap);
+}
+
+void compile_error(lex_ctx ctx, const char *msg, ...)
+{
+    va_list ap;
+    va_start(ap, msg);
+    vcompile_error(ctx, msg, ap);
+    va_end(ap);
+}
+
+bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_list ap)
+{
+    int lvl = LVL_WARNING;
+    char warn_name[1024];
+
+    if (!OPTS_WARN(warntype))
+        return false;
+
+    warn_name[0] = '-';
+    warn_name[1] = 'W';
+    (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
+
+    if (opts.werror) {
+        ++compile_errors;
+        lvl = LVL_ERROR;
+    }
+    else
+        ++compile_warnings;
+
+    con_vprintmsg_c(lvl, ctx.file, ctx.line, (opts.werror ? "error" : "warning"), fmt, ap, warn_name);
+
+    return opts.werror;
+}
+
+bool GMQCC_WARN compile_warning(lex_ctx ctx, int warntype, const char *fmt, ...)
+{
+    bool r;
+    va_list ap;
+    va_start(ap, fmt);
+    r = vcompile_warning(ctx, warntype, fmt, ap);
+    va_end(ap);
+    return r;
+}