3 * Structured exception handling in C
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 /*----- Global variables --------------------------------------------------*/
36 __exc_hnd
*__exc_list
= 0;
38 /*----- Functions ---------------------------------------------------------*/
42 * Arguments: @exc_extype type@ = type of duff exception
43 * @exc_exval val@ = extra data supplied
47 * Use: Default handler when everything goes wrong.
50 static void duff(exc_extype type
, exc_exval val
)
52 fprintf(stderr
, "fatal error: uncaught exception (type = %lx)\n", type
);
56 /* --- @duffproc@ --- *
58 * Current handler when there are no more exceptions left.
61 static exc__uncaught duffproc
= duff
;
63 /* --- @exc_uncaught@ --- *
65 * Arguments: @void (*proc)(exc_extype type, exc_exval val) = new handler
67 * Returns: Pointer to the old handler value.
69 * Use: Sets the handler for uncaught exceptions.
72 exc__uncaught
exc_uncaught(exc__uncaught proc
)
74 exc__uncaught p
= duffproc
;
80 /* --- @__exc_throw@ --- *
82 * Arguments: @exc_extype type@ = type of exception to throw
86 * Use: NOT FOR USER CONSUMPTION. Reads an appropriate exception
87 * value and throws an exception.
90 void __exc_throw(exc_extype type
, ...)
96 switch (type
& 0xC0) {
101 v
.i
= va_arg(ap
, int);
104 v
.p
= va_arg(ap
, void *);
107 v
.s
= va_arg(ap
, char *);
111 __exc_rethrow(type
, v
);
114 /* --- @__exc_rethrow@ --- *
116 * Arguments: @exc_extype type@ = type of exception to throw
117 * @exc_exval val@ = value of exception to throw
121 * Use: NOT FOR USER CONSUMPTION. Does the donkey-work of raising
125 void __exc_rethrow(exc_extype type
, exc_exval val
)
127 __exc_hnd
*p
= __exc_list
;
132 __exc_list
= p
->next
;
133 longjmp(p
->buf
, type
);
136 /*----- That's all, folks -------------------------------------------------*/