]> git.xonotic.org Git - xonotic/gmqcc.git/blob - error.c
ast and ir testers - to use: compile into gmqcc and execut the functions in main()
[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 "gmqcc.h"
25
26 /*
27  * Compiler error system, this handles the error printing, and managing
28  * such as after so many errors just stop the compilation, and other
29  * intereting like colors for the console.
30  */
31 #ifndef WIN32
32 #    define CON_BLACK   30
33 #    define CON_RED     31
34 #    define CON_GREEN   32
35 #    define CON_BROWN   33
36 #    define CON_BLUE    34
37 #    define CON_MAGENTA 35
38 #    define CON_CYAN    36
39 #    define CON_WHITE   37
40 static const int error_color[] = {
41     CON_RED,
42     CON_CYAN,
43     CON_MAGENTA,
44     CON_BLUE,
45     CON_BROWN,
46     CON_WHITE
47 };
48 #endif
49 int error_total = 0;
50 int error_max   = 10;
51
52 static const char *const error_list[] = {
53     "Parsing Error:",
54     "Lexing Error:",
55     "Internal Error:",
56     "Compilation Error:",
57     "Preprocessor Error:"
58 };
59
60 int error(lex_file *file, int status, const char *msg, ...) {
61     char      bu[1024*4]; /* enough? */
62     char      fu[1024*4]; /* enough? */
63     va_list   va;
64     
65     if (error_total + 1 > error_max) {
66         fprintf(stderr, "%d errors and more following, bailing\n", error_total);
67         exit (-1);
68     }
69     error_total ++;
70 /* color */
71 #    ifndef WIN32
72     sprintf  (bu, "\033[0;%dm%s \033[0;%dm %s:%d ", error_color[status-SHRT_MAX], error_list[status-SHRT_MAX], error_color[(status-1)-SHRT_MAX], file->name, file->line);
73 #else
74     sprintf  (bu, "%s ", error_list[status-SHRT_MAX]);
75 #endif
76     va_start (va, msg);
77     vsprintf (fu, msg, va);
78     va_end   (va);
79     fputs    (bu, stderr);
80     fputs    (fu, stderr);
81
82 /* color */
83 #    ifndef WIN32
84     fputs    ("\033[0m", stderr);
85 #    endif
86     
87     fflush   (stderr);
88     
89     return status;
90 }