]> git.xonotic.org Git - xonotic/gmqcc.git/blob - error.c
initial commit
[xonotic/gmqcc.git] / error.c
1 /*
2  * Copyright (C) 2012 
3  *      Dale Weiler
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * SOFTWARE.
22  */
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <limits.h>
27
28 /*
29  * Compiler error system, this handles the error printing, and managing
30  * such as after so many errors just stop the compilation, and other
31  * intereting like colors for the console.
32  */
33 #ifndef WIN32
34 #       define CON_BLACK   30
35 #       define CON_RED     31
36 #       define CON_GREEN   32
37 #       define CON_BROWN   33
38 #       define CON_BLUE    34
39 #       define CON_MAGENTA 35
40 #       define CON_CYAN    36
41 #       define CON_WHITE   37
42 static const int error_color[] = {
43         CON_RED,
44         CON_CYAN,
45         CON_MAGENTA
46 };
47 #endif
48 int error_total = 0;
49 int error_max   = 10;
50
51 static const char *const error_list[] = {
52         "Parsing Error:",
53         "Lexing Error:",
54         "Internal Error:"
55 };
56
57 int error(int status, const char *msg, ...) {
58         char      bu[1024*4]; /* enough? */
59         char      fu[1024*4]; /* enough? */
60         va_list   va;
61         
62         if (error_total + 1 > error_max) {
63                 fprintf(stderr, "%d errors and more following, bailing\n", error_total);
64                 exit (-1);
65         }
66         error_total ++;
67 /* color */
68 #       ifndef WIN32
69         sprintf  (bu, "\033[0;%dm%s \033[0;%dm", error_color[status-SHRT_MAX], error_list[status-SHRT_MAX], error_color[(status-1)-SHRT_MAX]);
70 #else
71         sprintf  (bu, "%s ", error_list[status-SHRT_MAX]);
72 #endif
73         va_start (va, msg);
74         vsprintf (fu, msg, va);
75         va_end   (va);
76         fputs    (bu, stderr);
77         fputs    (fu, stderr);
78
79 /* color */
80 #       ifndef WIN32
81         fputs    ("\033[0m", stderr);
82 #       endif
83         
84         fflush   (stderr);
85         
86         return status;
87 }