| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#ifndef LER_INCLUDED |
| 59 |
#define LER_INCLUDED |
| 60 |
|
| 61 |
#include "os.h" |
| 62 |
#ifndef LIB_INCLUDED |
| 63 |
#include "lib.h" |
| 64 |
#endif |
| 65 |
#include <setjmp.h> |
| 66 |
#include <assert.h> |
| 67 |
|
| 68 |
#ifdef __cplusplus |
| 69 |
extern "C" { |
| 70 |
#endif |
| 71 |
|
| 72 |
|
| 73 |
#define ERRFACMAX 6 |
| 74 |
|
| 75 |
union erradef |
| 76 |
{ |
| 77 |
int erraint; |
| 78 |
char *errastr; |
| 79 |
}; |
| 80 |
typedef union erradef erradef; |
| 81 |
|
| 82 |
struct errdef |
| 83 |
{ |
| 84 |
struct errdef *errprv; |
| 85 |
int errcode; |
| 86 |
char errfac[ERRFACMAX+1]; |
| 87 |
erradef erraav[10]; |
| 88 |
int erraac; |
| 89 |
jmp_buf errbuf; |
| 90 |
}; |
| 91 |
typedef struct errdef errdef; |
| 92 |
|
| 93 |
#define ERRBUFSIZ 512 |
| 94 |
|
| 95 |
|
| 96 |
struct errmfdef |
| 97 |
{ |
| 98 |
uint errmfnum; |
| 99 |
ulong errmfseek; |
| 100 |
}; |
| 101 |
typedef struct errmfdef errmfdef; |
| 102 |
|
| 103 |
struct errcxdef |
| 104 |
{ |
| 105 |
errdef *errcxptr; |
| 106 |
void (*errcxlog)(void *, char *fac, int err, int argc, erradef *); |
| 107 |
|
| 108 |
void *errcxlgc; |
| 109 |
int errcxofs; |
| 110 |
char errcxbuf[ERRBUFSIZ]; |
| 111 |
osfildef *errcxfp; |
| 112 |
errmfdef *errcxseek; |
| 113 |
uint errcxsksz; |
| 114 |
ulong errcxbase; |
| 115 |
struct appctxdef *errcxappctx; |
| 116 |
}; |
| 117 |
typedef struct errcxdef errcxdef; |
| 118 |
|
| 119 |
|
| 120 |
#define ERRBEGIN(ctx) \ |
| 121 |
{ \ |
| 122 |
errdef fr_; \ |
| 123 |
if ((fr_.errcode = setjmp(fr_.errbuf)) == 0) \ |
| 124 |
{ \ |
| 125 |
fr_.errprv = (ctx)->errcxptr; \ |
| 126 |
(ctx)->errcxptr = &fr_; |
| 127 |
|
| 128 |
|
| 129 |
#define ERRCATCH(ctx, e) \ |
| 130 |
assert(1==1 && (ctx)->errcxptr != fr_.errprv); \ |
| 131 |
(ctx)->errcxptr = fr_.errprv; \ |
| 132 |
} \ |
| 133 |
else \ |
| 134 |
{ \ |
| 135 |
assert(2==2 && (ctx)->errcxptr != fr_.errprv); \ |
| 136 |
(e) = fr_.errcode; \ |
| 137 |
(ctx)->errcxptr = fr_.errprv; |
| 138 |
|
| 139 |
|
| 140 |
#define errargint(argnum) (fr_.erraav[argnum].erraint) |
| 141 |
#define errargstr(argnum) (fr_.erraav[argnum].errastr) |
| 142 |
|
| 143 |
|
| 144 |
#define ERREND(ctx) \ |
| 145 |
} \ |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
#define ERRCLEAN(ctx) \ |
| 150 |
assert((ctx)->errcxptr != fr_.errprv); \ |
| 151 |
(ctx)->errcxptr = fr_.errprv; \ |
| 152 |
} \ |
| 153 |
else \ |
| 154 |
{ \ |
| 155 |
assert((ctx)->errcxptr != fr_.errprv); \ |
| 156 |
(ctx)->errcxptr = fr_.errprv; |
| 157 |
|
| 158 |
#define ERRENDCLN(ctx) \ |
| 159 |
errrse(ctx); \ |
| 160 |
} \ |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
#define ERRTINT erraint |
| 167 |
#define ERRTSTR errastr |
| 168 |
|
| 169 |
|
| 170 |
#define errargc(ctx,cnt) ((ctx)->errcxptr->erraac=(cnt)) |
| 171 |
|
| 172 |
|
| 173 |
#ifdef ERR_NO_MACRO |
| 174 |
char *errstr(errcxdef *ctx, const char *str, int len); |
| 175 |
#else |
| 176 |
|
| 177 |
#define errstr(ctx,str,len) \ |
| 178 |
((memcpy(&(ctx)->errcxbuf[(ctx)->errcxofs],str,(size_t)len), \ |
| 179 |
(ctx)->errcxofs += (len), \ |
| 180 |
(ctx)->errcxbuf[(ctx)->errcxofs++] = '\0'), \ |
| 181 |
&(ctx)->errcxbuf[(ctx)->errcxofs-(len)-1]) |
| 182 |
|
| 183 |
#endif |
| 184 |
|
| 185 |
|
| 186 |
#define errargv(ctx,index,typ,arg) \ |
| 187 |
((ctx)->errcxptr->erraav[index].typ=(arg)) |
| 188 |
|
| 189 |
|
| 190 |
#ifdef ERR_NO_MACRO |
| 191 |
void errsign(errcxdef *ctx, int e, char *facility); |
| 192 |
#else |
| 193 |
# ifdef DEBUG |
| 194 |
void errjmp(jmp_buf buf, int e); |
| 195 |
# define errsign(ctx, e, fac) \ |
| 196 |
(strncpy((ctx)->errcxptr->errfac, fac, ERRFACMAX),\ |
| 197 |
(ctx)->errcxptr->errfac[ERRFACMAX]='\0',\ |
| 198 |
(ctx)->errcxofs=0, errjmp((ctx)->errcxptr->errbuf, e)) |
| 199 |
# else |
| 200 |
# define errsign(ctx, e, fac) \ |
| 201 |
(strncpy((ctx)->errcxptr->errfac, fac, ERRFACMAX),\ |
| 202 |
(ctx)->errcxptr->errfac[ERRFACMAX]='\0',\ |
| 203 |
(ctx)->errcxofs=0, longjmp((ctx)->errcxptr->errbuf, e)) |
| 204 |
# endif |
| 205 |
#endif |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
#ifdef ERR_NO_MACRO |
| 210 |
void errsigf(errcxdef *ctx, char *facility, int err); |
| 211 |
#else |
| 212 |
#define errsigf(ctx, fac, e) (errargc(ctx,0),errsign(ctx,e,fac)) |
| 213 |
#endif |
| 214 |
|
| 215 |
|
| 216 |
#define errsigf1(ctx, fac, e, typ1, arg1) \ |
| 217 |
(errargv(ctx,0,typ1,arg1),errargc(ctx,1),errsign(ctx,e,fac)) |
| 218 |
|
| 219 |
|
| 220 |
#define errsigf2(ctx, fac, e, typ1, arg1, typ2, arg2) \ |
| 221 |
(errargv(ctx,0,typ1,arg1), errargv(ctx,1,typ2,arg2), \ |
| 222 |
errargc(ctx,2), errsign(ctx,e,fac)) |
| 223 |
|
| 224 |
|
| 225 |
#ifdef ERR_NO_MACRO |
| 226 |
void errrse1(errcxdef *ctx, errdef *fr); |
| 227 |
# define errrse(ctx) errrse1(ctx, &fr_) |
| 228 |
#else |
| 229 |
|
| 230 |
|
| 231 |
# define errrse(ctx) \ |
| 232 |
(errargc(ctx, fr_.erraac),\ |
| 233 |
memcpy((ctx)->errcxptr->erraav, fr_.erraav, \ |
| 234 |
(size_t)(fr_.erraac*sizeof(erradef))),\ |
| 235 |
errsign(ctx, fr_.errcode, fr_.errfac)) |
| 236 |
|
| 237 |
#endif |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
#define errkeepargs(ctx) errcopyargs(ctx, &fr_) |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
#define errcopyargs(ctx, fr) \ |
| 257 |
(errargc((ctx), (fr)->erraac), \ |
| 258 |
memcpy((ctx)->errcxptr->erraav, (fr)->erraav, \ |
| 259 |
(size_t)((fr)->erraac*sizeof(erradef)))) |
| 260 |
|
| 261 |
|
| 262 |
#define errclog(ctx) \ |
| 263 |
((*(ctx)->errcxlog)((ctx)->errcxlgc,fr_.errfac,fr_.errcode,\ |
| 264 |
fr_.erraac,fr_.erraav)) |
| 265 |
|
| 266 |
|
| 267 |
#define errprelog(ctx, err) \ |
| 268 |
((*(ctx)->errcxlog)((ctx)->errcxlgc,(ctx)->errcxptr->errfac,\ |
| 269 |
err,(ctx)->errcxptr->erraac,\ |
| 270 |
(ctx)->errcxptr->erraav)) |
| 271 |
|
| 272 |
|
| 273 |
#ifdef ERR_NO_MACRO |
| 274 |
void errlogn(errcxdef *ctx, int err, char *facility); |
| 275 |
#else |
| 276 |
|
| 277 |
#define errlogn(ctx,err,fac) \ |
| 278 |
((ctx)->errcxofs=0,\ |
| 279 |
(*(ctx)->errcxlog)((ctx)->errcxlgc,fac,err,(ctx)->errcxptr->erraac,\ |
| 280 |
(ctx)->errcxptr->erraav)) |
| 281 |
|
| 282 |
#endif |
| 283 |
|
| 284 |
|
| 285 |
#ifdef ERR_NO_MACRO |
| 286 |
void errlogf(errcxdef *ctx, char *facility, int err); |
| 287 |
#else |
| 288 |
|
| 289 |
|
| 290 |
#define errlogf(ctx,fac,err) (errargc(ctx,0),errlogn(ctx,err,fac)) |
| 291 |
|
| 292 |
#endif |
| 293 |
|
| 294 |
|
| 295 |
#define errlogf1(ctx, fac, e, typ1, arg1) \ |
| 296 |
(errargv(ctx,0,typ1,arg1),errargc(ctx,1),errlogn(ctx,e,fac)) |
| 297 |
|
| 298 |
|
| 299 |
#define errlogf2(ctx, fac, e, typ1, arg1, typ2, arg2) \ |
| 300 |
(errargv(ctx,0,typ1,arg1),errargv(ctx,1,typ2,arg2),\ |
| 301 |
errargc(ctx,2),errlogn(ctx,e,fac)) |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
int errfmt(char *outbuf, int outbufl, char *fmt, int argc, |
| 314 |
erradef *argv); |
| 315 |
|
| 316 |
|
| 317 |
void errmsg(errcxdef *ctx, char *outbuf, uint outbufl, uint err); |
| 318 |
|
| 319 |
|
| 320 |
void errini(errcxdef *ctx, osfildef *fp); |
| 321 |
|
| 322 |
|
| 323 |
errcxdef *lerini(); |
| 324 |
void lerfre(errcxdef *ctx); |
| 325 |
|
| 326 |
|
| 327 |
typedef struct errmdef errmdef; |
| 328 |
struct errmdef |
| 329 |
{ |
| 330 |
uint errmerr; |
| 331 |
char *errmtxt; |
| 332 |
}; |
| 333 |
|
| 334 |
#ifdef __cplusplus |
| 335 |
} |
| 336 |
#endif |
| 337 |
|
| 338 |
#endif |