gmqcc/error.c

91 lines
2.7 KiB
C
Raw Normal View History

2012-04-09 10:42:06 +00:00
/*
* Copyright (C) 2012
2012-04-17 20:24:22 +00:00
* Dale Weiler
2012-04-09 10:42:06 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdarg.h>
2012-04-15 22:01:21 +00:00
#include "gmqcc.h"
2012-04-09 10:42:06 +00:00
/*
* Compiler error system, this handles the error printing, and managing
* such as after so many errors just stop the compilation, and other
* intereting like colors for the console.
*/
#ifndef WIN32
2012-04-17 20:24:22 +00:00
# define CON_BLACK 30
# define CON_RED 31
# define CON_GREEN 32
# define CON_BROWN 33
# define CON_BLUE 34
# define CON_MAGENTA 35
# define CON_CYAN 36
# define CON_WHITE 37
2012-04-09 10:42:06 +00:00
static const int error_color[] = {
2012-04-17 20:24:22 +00:00
CON_RED,
CON_CYAN,
CON_MAGENTA,
CON_BLUE,
CON_BROWN,
CON_WHITE
2012-04-09 10:42:06 +00:00
};
#endif
int error_total = 0;
int error_max = 10;
static const char *const error_list[] = {
2012-04-17 20:24:22 +00:00
"Parsing Error:",
"Lexing Error:",
"Internal Error:",
"Compilation Error:",
"Preprocessor Error:"
2012-04-09 10:42:06 +00:00
};
2012-04-28 09:25:19 +00:00
int error(lex_file *file, int status, const char *msg, ...) {
2012-04-17 20:24:22 +00:00
char bu[1024*4]; /* enough? */
char fu[1024*4]; /* enough? */
va_list va;
if (error_total + 1 > error_max) {
fprintf(stderr, "%d errors and more following, bailing\n", error_total);
exit (-1);
}
error_total ++;
2012-04-09 10:42:06 +00:00
/* color */
2012-04-17 20:24:22 +00:00
# ifndef WIN32
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);
2012-04-09 10:42:06 +00:00
#else
2012-04-17 20:24:22 +00:00
sprintf (bu, "%s ", error_list[status-SHRT_MAX]);
2012-04-09 10:42:06 +00:00
#endif
2012-04-17 20:24:22 +00:00
va_start (va, msg);
vsprintf (fu, msg, va);
va_end (va);
fputs (bu, stderr);
fputs (fu, stderr);
2012-04-09 10:42:06 +00:00
/* color */
2012-04-17 20:24:22 +00:00
# ifndef WIN32
fputs ("\033[0m", stderr);
# endif
fflush (stderr);
return status;
2012-04-09 10:42:06 +00:00
}