| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include "errors.h" |
| 29 |
|
| 30 |
#include "asm.h" |
| 31 |
#include "util.h" |
| 32 |
#include "version.h" |
| 33 |
|
| 34 |
#include <assert.h> |
| 35 |
#include <stdarg.h> |
| 36 |
#include <stdio.h> |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static error_format_t F_error_format = ERRORFORMAT_DEFAULT; |
| 55 |
static error_level_t F_error_level = ERRORLEVEL_DEFAULT; |
| 56 |
static size_t nof_fatals = 0; |
| 57 |
static size_t nof_errors = 0; |
| 58 |
static size_t nof_warnings = 0; |
| 59 |
char source_location_buffer[SOURCE_LOCATION_LENGTH]; |
| 60 |
|
| 61 |
static const char *level_names[] = |
| 62 |
{ |
| 63 |
[ERRORLEVEL_DEBUG] = "debug", |
| 64 |
[ERRORLEVEL_INFO] = "info", |
| 65 |
[ERRORLEVEL_NOTICE] = "notice", |
| 66 |
[ERRORLEVEL_WARNING] = "warning", |
| 67 |
[ERRORLEVEL_ERROR] = "error", |
| 68 |
[ERRORLEVEL_FATAL] = "fatal", |
| 69 |
[ERRORLEVEL_PANIC] = "***panic***", |
| 70 |
}; |
| 71 |
|
| 72 |
bool valid_error_format(int format) |
| 73 |
{ |
| 74 |
return (ERRORFORMAT_MIN <= format && format <= ERRORFORMAT_MAX); |
| 75 |
} |
| 76 |
|
| 77 |
void set_error_format(error_format_t format) |
| 78 |
{ |
| 79 |
assert(valid_error_format(format)); |
| 80 |
F_error_format = format; |
| 81 |
} |
| 82 |
|
| 83 |
bool valid_error_level(int level) |
| 84 |
{ |
| 85 |
return (ERRORLEVEL_MIN <= level && level <= ERRORLEVEL_MAX); |
| 86 |
} |
| 87 |
|
| 88 |
void set_error_level(error_level_t level) |
| 89 |
{ |
| 90 |
assert(valid_error_level(level)); |
| 91 |
F_error_level = level; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static bool visible_error_level(error_level_t level) |
| 98 |
{ |
| 99 |
return (level >= F_error_level); |
| 100 |
} |
| 101 |
|
| 102 |
size_t number_of_fatals(void) |
| 103 |
{ |
| 104 |
return nof_fatals; |
| 105 |
} |
| 106 |
|
| 107 |
size_t number_of_errors(void) |
| 108 |
{ |
| 109 |
return nof_errors; |
| 110 |
} |
| 111 |
|
| 112 |
size_t number_of_warnings(void) |
| 113 |
{ |
| 114 |
return nof_warnings; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
static void internal_panic(const char *message) |
| 119 |
{ |
| 120 |
fprintf( |
| 121 |
stderr, |
| 122 |
"\n%s: FATAL INTERNAL PANIC (errors.c): %s\n\n", |
| 123 |
getprogname(), |
| 124 |
message |
| 125 |
); |
| 126 |
exit(EXIT_FAILURE); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
static void print_error_message(const char *message) |
| 142 |
{ |
| 143 |
assert(message != NULL); |
| 144 |
assert(strlen(message) > 0); |
| 145 |
|
| 146 |
fprintf(stderr, "%s\n", message); |
| 147 |
|
| 148 |
if (FI_listfile != NULL) { |
| 149 |
fprintf(FI_listfile, "*%s\n", message); |
| 150 |
} |
| 151 |
else { |
| 152 |
|
| 153 |
assert(F_listfile == NULL); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static size_t |
| 162 |
sane_vsnprintf( char *restrict str, size_t size, const char *restrict fmt, va_list ap) |
| 163 |
{ |
| 164 |
int res; |
| 165 |
assert(str != NULL); |
| 166 |
assert(size > 0); |
| 167 |
assert(fmt != NULL); |
| 168 |
|
| 169 |
res = vsnprintf(str, size, fmt, ap); |
| 170 |
if (res < 0) { |
| 171 |
internal_panic("sane_vsnprintf() failed!"); |
| 172 |
} |
| 173 |
|
| 174 |
return (size_t) res; |
| 175 |
} |
| 176 |
|
| 177 |
static size_t |
| 178 |
sane_snprintf( char *restrict str, size_t size, const char *restrict fmt, ...) |
| 179 |
__attribute__((format(printf, 3, 4))); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
static size_t |
| 192 |
sane_snprintf( char *restrict str, size_t size, const char *restrict fmt, ...) |
| 193 |
{ |
| 194 |
size_t res; |
| 195 |
va_list ap; |
| 196 |
|
| 197 |
va_start(ap, fmt); |
| 198 |
|
| 199 |
res = sane_vsnprintf(str, size, fmt, ap); |
| 200 |
|
| 201 |
va_end(ap); |
| 202 |
return res; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
static size_t append_level(char *buffer, error_level_t level, size_t size) |
| 210 |
{ |
| 211 |
char name[1024]; |
| 212 |
size_t res = 0; |
| 213 |
assert(valid_error_level(level)); |
| 214 |
res = sane_snprintf(name, sizeof(name), "%s: ", level_names[level]); |
| 215 |
if (res >= sizeof(name)) { |
| 216 |
internal_panic("Buffer overflow in append_level()!"); |
| 217 |
} |
| 218 |
return strlcat(buffer, name, size); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
static size_t append_location(char *buffer, const INCFILE *file, size_t size) |
| 226 |
{ |
| 227 |
char location[1024]; |
| 228 |
size_t res = 0; |
| 229 |
|
| 230 |
|
| 231 |
location[0] = '\0'; |
| 232 |
|
| 233 |
switch (F_error_format) { |
| 234 |
case ERRORFORMAT_WOE: |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if (file != NULL) { |
| 240 |
res = sane_snprintf( |
| 241 |
location, sizeof(location), "%s (%lu): ", |
| 242 |
file->name, file->lineno |
| 243 |
); |
| 244 |
} |
| 245 |
break; |
| 246 |
case ERRORFORMAT_DILLON: |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if (file != NULL) { |
| 253 |
res = sane_snprintf( |
| 254 |
location, sizeof(location), "line %7lu %-10s ", |
| 255 |
file->lineno, file->name |
| 256 |
); |
| 257 |
} |
| 258 |
break; |
| 259 |
case ERRORFORMAT_GNU: |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if (file != NULL) { |
| 265 |
res = sane_snprintf( |
| 266 |
location, sizeof(location), "%s:%lu: ", |
| 267 |
file->name, file->lineno |
| 268 |
); |
| 269 |
} |
| 270 |
else { |
| 271 |
res = sane_snprintf( |
| 272 |
location, sizeof(location), "%s: ", |
| 273 |
getprogname() |
| 274 |
); |
| 275 |
} |
| 276 |
break; |
| 277 |
default: |
| 278 |
internal_panic("Invalid error format in append_location()!"); |
| 279 |
break; |
| 280 |
} |
| 281 |
if (res >= sizeof(location)) { |
| 282 |
internal_panic("Buffer overflow in append_location()!"); |
| 283 |
} |
| 284 |
return strlcat(buffer, location, size); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
static size_t |
| 292 |
append_information(char *buffer, const char *fmt, va_list ap, size_t size) |
| 293 |
{ |
| 294 |
char information[1024]; |
| 295 |
size_t res = 0; |
| 296 |
res = sane_vsnprintf(information, sizeof(information), fmt, ap); |
| 297 |
if (res >= sizeof(information)) { |
| 298 |
internal_panic("Buffer overflow in append_information()!"); |
| 299 |
} |
| 300 |
return strlcat(buffer, information, size); |
| 301 |
} |
| 302 |
|
| 303 |
static void vanotify(error_level_t level, const char *fmt, va_list ap) |
| 304 |
{ |
| 305 |
|
| 306 |
char buffer[1024]; |
| 307 |
|
| 308 |
INCFILE *file = pIncfile; |
| 309 |
|
| 310 |
size_t res; |
| 311 |
|
| 312 |
assert(valid_error_level(level)); |
| 313 |
|
| 314 |
if (!visible_error_level(level)) { |
| 315 |
|
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
while (file != NULL && (file->flags & INF_MACRO) != 0) { |
| 322 |
file = file->next; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
buffer[0] = '\0'; |
| 327 |
|
| 328 |
|
| 329 |
res = append_location(buffer, file, sizeof(buffer)); |
| 330 |
if (res > sizeof(buffer)) { |
| 331 |
internal_panic("Buffer overflow in vanotify()!"); |
| 332 |
} |
| 333 |
res = append_level(buffer, level, sizeof(buffer)); |
| 334 |
if (res > sizeof(buffer)) { |
| 335 |
internal_panic("Buffer overflow in vanotify()!"); |
| 336 |
} |
| 337 |
res = append_information(buffer, fmt, ap, sizeof(buffer)); |
| 338 |
if (res > sizeof(buffer)) { |
| 339 |
internal_panic("Buffer overflow in vanotify()!"); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
print_error_message(buffer); |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
if (level == ERRORLEVEL_WARNING) |
| 348 |
{ |
| 349 |
nof_warnings += 1; |
| 350 |
} |
| 351 |
if (level == ERRORLEVEL_ERROR) |
| 352 |
{ |
| 353 |
nof_errors += 1; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
if (level >= ERRORLEVEL_FATAL) |
| 358 |
{ |
| 359 |
nof_fatals += 1; |
| 360 |
} |
| 361 |
if (level == ERRORLEVEL_PANIC) |
| 362 |
{ |
| 363 |
exit(EXIT_FAILURE); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
#define IMPLEMENT_FMT(level) \ |
| 369 |
va_list ap; \ |
| 370 |
va_start(ap, fmt); \ |
| 371 |
vanotify(level, fmt, ap); \ |
| 372 |
va_end(ap) |
| 373 |
#define DEFINE_FMT(name, level) \ |
| 374 |
void name(const char *fmt, ...) \ |
| 375 |
{ \ |
| 376 |
IMPLEMENT_FMT(level); \ |
| 377 |
} |
| 378 |
|
| 379 |
void notify_fmt(error_level_t level, const char *fmt, ...) |
| 380 |
{ |
| 381 |
IMPLEMENT_FMT(level); |
| 382 |
} |
| 383 |
|
| 384 |
DEFINE_FMT(debug_fmt, ERRORLEVEL_DEBUG) |
| 385 |
DEFINE_FMT(info_fmt, ERRORLEVEL_INFO) |
| 386 |
DEFINE_FMT(notice_fmt, ERRORLEVEL_NOTICE) |
| 387 |
DEFINE_FMT(warning_fmt, ERRORLEVEL_WARNING) |
| 388 |
DEFINE_FMT(error_fmt, ERRORLEVEL_ERROR) |
| 389 |
DEFINE_FMT(fatal_fmt, ERRORLEVEL_FATAL) |
| 390 |
DEFINE_FMT(panic_fmt, ERRORLEVEL_PANIC) |
| 391 |
|
| 392 |
|