]> git.xonotic.org Git - xonotic/gmqcc.git/blob - error.c
Merge branch 'master' into blub/bc3
[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 enum {
33     CON_BLACK   = 30,
34     CON_RED,
35     CON_GREEN,
36     CON_BROWN,
37     CON_BLUE,
38     CON_MAGENTA,
39     CON_CYAN ,
40     CON_WHITE
41 };
42 static const int error_color[] = {
43     CON_RED,
44     CON_CYAN,
45     CON_MAGENTA,
46     CON_BLUE,
47     CON_BROWN,
48     CON_WHITE
49 };
50 #endif
51 int error_total = 0;
52 int error_max   = 10;
53
54 static const char *const error_list[] = {
55     "Parsing Error:",
56     "Lexing Error:",
57     "Internal Error:",
58     "Compilation Error:",
59     "Preprocessor Error:"
60 };
61
62 int error(lex_file *file, int status, const char *msg, ...) {
63     char      bu[1024*4]; /* enough? */
64     char      fu[1024*4]; /* enough? */
65     va_list   va;
66
67     if (error_total + 1 > error_max) {
68         fprintf(stderr, "%d errors and more following, bailing\n", error_total);
69         exit (-1);
70     }
71     error_total ++;
72 /* color */
73 #    ifndef WIN32
74     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);
75 #else
76     sprintf  (bu, "%s ", error_list[status-SHRT_MAX]);
77 #endif
78     va_start (va, msg);
79     vsprintf (fu, msg, va);
80     va_end   (va);
81     fputs    (bu, stderr);
82     fputs    (fu, stderr);
83
84 /* color */
85 #    ifndef WIN32
86     fputs    ("\033[0m", stderr);
87 #    endif
88
89     fflush   (stderr);
90
91     return status;
92 }