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