]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - con.c
Visual studio __forceinline can be used for GMQCC_INLINE
[xonotic/gmqcc.git] / con.c
diff --git a/con.c b/con.c
index 7ab5fcbe4caa398dd9a81ab9d5ed389e32f6032e..2d18d6941992bdcc75d23915b289f3491048a5ba 100644 (file)
--- a/con.c
+++ b/con.c
@@ -30,8 +30,8 @@
 #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 {
@@ -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
@@ -271,16 +271,20 @@ 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,7 +316,6 @@ 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
@@ -325,25 +328,19 @@ void con_vprintmsg(int level, const char *name, size_t line, const char *msgtype
         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);
+    print("\n");
 }
 
 void con_printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) {