| 1 |
/* Copyright (c) 1987, 1989, 2012 University of Maryland Department of |
| 2 |
Computer Science. |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining |
| 5 |
a copy of this software and associated documentation files (the |
| 6 |
"Software"), to deal in the Software without restriction, including |
| 7 |
without limitation, the rights to use, copy, modify, merge, publish, |
| 8 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 9 |
permit persons to whom the Software is furnished to do so, subject to |
| 10 |
the following conditions: The above copyright notice, this permission |
| 11 |
notice and the disclaimer statement shall be included in all copies |
| 12 |
or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 19 |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 20 |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* Print an error message with an optional system error number, and |
| 25 |
* optionally quit. |
| 26 |
* |
| 27 |
*/ |
| 28 |
#ifdef HAVE_CONFIG_H |
| 29 |
#include <config.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
|
| 34 |
#include "types.h" |
| 35 |
#include "error.h" |
| 36 |
#include "tempfile.h" |
| 37 |
|
| 38 |
extern char *ProgName; /* program name from argv[0] */ |
| 39 |
|
| 40 |
#include <errno.h> |
| 41 |
#ifndef errno |
| 42 |
extern int errno; |
| 43 |
#endif |
| 44 |
|
| 45 |
static FILE *trap_file; /* error diversion file, if any */ |
| 46 |
static void (*trap_fn)(int, const char *); /* trap function */ |
| 47 |
static char *trap_buf; /* buffer for trapped error strings */ |
| 48 |
static int trap_size; /* size of trap_buf */ |
| 49 |
|
| 50 |
#ifndef KPATHSEA |
| 51 |
extern char *malloc(), *realloc(); |
| 52 |
#endif |
| 53 |
|
| 54 |
#if !defined (HAVE_STRERROR) && !defined (strerror) |
| 55 |
static char * |
| 56 |
strerror (int errnum) |
| 57 |
{ |
| 58 |
extern char *sys_errlist[]; |
| 59 |
extern int sys_nerr; |
| 60 |
|
| 61 |
return 0 < errnum && errnum <= sys_nerr |
| 62 |
? sys_errlist[errnum] : "Unknown system error"; |
| 63 |
} |
| 64 |
#endif /* not HAVE_STRERROR && not strerror */ |
| 65 |
|
| 66 |
/* |
| 67 |
* Read a trapped error into trap_buf. |
| 68 |
* Return a pointer to the (NUL-terminated) text. |
| 69 |
* If something goes wrong, return something else printable. |
| 70 |
*/ |
| 71 |
static const char * |
| 72 |
readback(void) |
| 73 |
{ |
| 74 |
int nbytes = ftell(trap_file) + 1; |
| 75 |
|
| 76 |
if (nbytes > trap_size) { |
| 77 |
if (trap_buf == NULL) |
| 78 |
trap_buf = malloc((unsigned)nbytes); |
| 79 |
else |
| 80 |
trap_buf = realloc(trap_buf, (unsigned)nbytes); |
| 81 |
if (trap_buf == NULL) { |
| 82 |
trap_size = 0; |
| 83 |
return ("Ouch! Lost error text: out of memory?"); |
| 84 |
} |
| 85 |
} |
| 86 |
rewind(trap_file); /* now can read */ |
| 87 |
nbytes = fread(trap_buf, 1, nbytes, trap_file); |
| 88 |
if (nbytes < 0) |
| 89 |
return ("Ouch! Trouble reading error text!"); |
| 90 |
trap_buf[nbytes] = 0; |
| 91 |
return (trap_buf); |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
* Print an error message to the error output (either stderr, or |
| 96 |
* if trapping errors, to the error trap file). We precede this |
| 97 |
* with the program's name and an optional string (a0), then use |
| 98 |
* the format and variable argument list, then append an optional |
| 99 |
* Unix error string. Finally, if errors are being trapped, we |
| 100 |
* pass the error text and the quit flag to the trap function. |
| 101 |
* |
| 102 |
* In the interest of `look and feel', if errors are being trapped, |
| 103 |
* the program name is omitted. |
| 104 |
*/ |
| 105 |
static void |
| 106 |
verror(int quit, const char *a0, const char *fmt, va_list l, int e) |
| 107 |
{ |
| 108 |
register FILE *fp = trap_file; |
| 109 |
|
| 110 |
/* print to the trap file, if any, else stderr */ |
| 111 |
if ((fp = trap_file) != NULL) |
| 112 |
rewind(fp); /* now can write */ |
| 113 |
else { |
| 114 |
fp = stderr; |
| 115 |
(void) fflush(fp); |
| 116 |
} |
| 117 |
if (trap_file == NULL) |
| 118 |
(void) fprintf(fp, "%s: ", ProgName); |
| 119 |
if (a0) |
| 120 |
(void) fprintf(fp, "%s", a0); |
| 121 |
(void) vfprintf(fp, fmt, l); |
| 122 |
if (e) |
| 123 |
(void) fprintf(fp, ": %s", strerror(e)); |
| 124 |
(void) putc('\n', fp); |
| 125 |
(void) fflush(fp); |
| 126 |
if (trap_file != NULL) |
| 127 |
(*trap_fn)(quit, readback()); |
| 128 |
if (quit) |
| 129 |
exit(quit); |
| 130 |
} |
| 131 |
|
| 132 |
/* |
| 133 |
* Print an error message and optionally quit. |
| 134 |
*/ |
| 135 |
void |
| 136 |
error(int quit, int e, const char *fmt, ...) |
| 137 |
{ |
| 138 |
va_list l; |
| 139 |
va_start(l, fmt); |
| 140 |
if (e < 0) |
| 141 |
e = errno; |
| 142 |
verror(quit, NULL, fmt, l, e); |
| 143 |
va_end(l); |
| 144 |
} |
| 145 |
/* |
| 146 |
* Panic (print to stderr and abort). |
| 147 |
*/ |
| 148 |
void |
| 149 |
panic(const char *fmt, ...) |
| 150 |
{ |
| 151 |
va_list l; |
| 152 |
va_start(l, fmt); |
| 153 |
verror(0, "panic: ", fmt, l, 0); |
| 154 |
va_end(l); |
| 155 |
abort(); |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Enable error trapping: register the function fn as the trapper. |
| 160 |
* If fn is NULL, stop trapping. |
| 161 |
*/ |
| 162 |
void |
| 163 |
SetErrorTrap(void (*fn)(int, const char *)) |
| 164 |
{ |
| 165 |
int tempfd; |
| 166 |
char fname[BUFSIZ]; |
| 167 |
|
| 168 |
/* shut down any existing error trap */ |
| 169 |
if (trap_file) { |
| 170 |
(void) fclose(trap_file); |
| 171 |
trap_file = NULL; |
| 172 |
} |
| 173 |
if ((trap_fn = fn) == NULL) |
| 174 |
return; |
| 175 |
/* begin trapping */ |
| 176 |
if ((tempfd = MakeRWTempFile(fname)) < 0) |
| 177 |
error(1, -1, "cannot create temporary file %s", fname); |
| 178 |
if (trap_size == 0) { |
| 179 |
trap_buf = malloc((unsigned)(trap_size = 1000)); |
| 180 |
if (trap_buf == 0) |
| 181 |
error(1, -1, |
| 182 |
"cannot get space for error buffer"); |
| 183 |
} |
| 184 |
if ((trap_file = fdopen(tempfd, "r+")) == NULL) |
| 185 |
error(1, -1, "cannot get stdio file for error trap"); |
| 186 |
} |