]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - con.c
lex_ctx of a return will be at the return keyword now
[xonotic/gmqcc.git] / con.c
diff --git a/con.c b/con.c
index a784ab7338cfe82be1b95628e1824b4f9b900b4e..f667e6db72fb1d31321b6eb88b6ad353d95fcdaf 100644 (file)
--- a/con.c
+++ b/con.c
@@ -22,6 +22,9 @@
  */
 #include "gmqcc.h"
 
+uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
+bool        opts_werror   = false;
+
 /*
  * isatty/STDERR_FILENO/STDOUT_FILNO
  * + some other things likewise.
@@ -37,7 +40,7 @@
 typedef struct {
     FILE *handle_err;
     FILE *handle_out;
-    
+
     int   color_err;
     int   color_out;
 } con_t;
@@ -64,7 +67,7 @@ typedef struct {
  * with yay, another macro :P
  */
 #define isatty _isatty
+
 enum {
     RESET = 0,
     BOLD  = 1,
@@ -107,15 +110,15 @@ static void win_fputs(char *str, FILE *f) {
     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(
         (GMQCC_IS_STDOUT(h)) ?
@@ -123,7 +126,7 @@ static void win_fputs(char *str, FILE *f) {
             GetStdHandle(STD_ERROR_HANDLE), &cinfo
     );
     icolor = cinfo.wAttributes;
-    
+
     while (*str) {
         if (*str == '\e')
             state = '\e';
@@ -140,7 +143,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 +158,12 @@ static void win_fputs(char *str, FILE *f) {
                     wcolor  = WWHITE;
                     intense = WBLACK;
                 }
-                
+
                 SetConsoleTextattribute(
                     (h == stdout) ?
                     GetStdHandle(STD_OUTPUT_HANDLE) :
                     GetStdHandle(STD_ERROR_HANDLE),
-                    
+
                     wcolor | intense | (icolor & 0xF0)
                 );
                 colorpos =  1;
@@ -190,7 +193,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.
  */
@@ -270,21 +273,21 @@ void con_reset() {
  */
 int con_change(const char *out, const char *err) {
     con_close();
-    
+
     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(err)) {
         console.handle_err = GMQCC_IS_STDOUT(err) ? stdout : stderr;
         con_enablecolor();
     } else if (!(console.handle_err = fopen(err, "w"))) return 0;
-    
-    // no buffering
+
+    /* no buffering */
     setvbuf(console.handle_out, NULL, _IONBF, 0);
     setvbuf(console.handle_err, NULL, _IONBF, 0);
-    
+
     return 1;
 }
 
@@ -360,3 +363,38 @@ 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 compile_error(lex_ctx ctx, const char *msg, ...)
+{
+    va_list ap;
+    ++compile_errors;
+    va_start(ap, msg);
+    con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap);
+    va_end(ap);
+}
+
+bool GMQCC_WARN compile_warning(lex_ctx ctx, int warntype, const char *fmt, ...)
+{
+       va_list ap;
+       int lvl = LVL_WARNING;
+
+    if (!OPTS_WARN(warntype))
+        return false;
+
+    if (opts_werror) {
+        ++compile_errors;
+           lvl = LVL_ERROR;
+       }
+       else
+        ++compile_warnings;
+
+       va_start(ap, fmt);
+    con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
+       va_end(ap);
+
+       return opts_werror;
+}