3 * $Id: exc.h,v 1.2 1999/05/05 18:50:31 mdw Exp $
5 * Structured exception handling in C
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/05/05 18:50:31 mdw
33 * Change licensing conditions to LGPL.
35 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
36 * Initial version of mLib
49 /*----- Quick documentation -----------------------------------------------*
51 * This header file provides some exception handling facilities in C
52 * programs. It modifies the syntax of the language slightly, using the
55 * The `throw' expression returns no value. It has the syntax:
57 * THROW ( expr , expr )
59 * The first expression must have type compatible with unsigned integer; it
60 * identifies an `exception type'. The second must have type compatible
61 * with pointer to void; it contains the `exception data'. Control is
62 * passed to the current exception handler.
64 * The `RETHROW' expression, valid only within an exception handler, causes
65 * the current exception to be thrown again.
67 * A `try' statement has the syntax:
69 * TRY stat CATCH stat END_TRY;
71 * The first statement is called the `test'; the second is the `handler'.
72 * During execution of the test, the handler is added to a stack of
73 * active exception handlers; the topmost handler on this stack is called
74 * the `current' handler. When execution of the test completes, the
75 * corresponding handler is removed from the stack.
77 * The test statement may complete in one of these ways:
79 * * Normal completion -- control reaches the end of the statement
82 * * Throwing an exception -- an exception is thrown when the handler is
83 * the current exception handler.
85 * * By executing a `break' statement.
87 * * By executing the expression `EXIT_TRY' and transferring control to
88 * a point outside the entire `try' statement (e.g., executing a `goto'
89 * or `return' statement).
91 * Any other attempt to leave the test causes undefined behaviour.
93 * If an exception is thrown while the handler is the current exception
94 * handler, it is given control. The variables `exc_type' and `exc_val'
95 * denote the exception type and value respectively -- they are passed
96 * unchanged from the `throw' expression which caused the exception.
97 * A handler is deactivated before it is invoked; if it causes an
98 * exception to be thrown (and does not contain a nested `try' statement)
99 * control will be passed to an earlier active handler.
101 * Control is passed to handlers using the `longjmp' function.
106 * ... something dangerous ...
107 * } CATCH switch (exc_type) {
108 * case EXC_INTERESTING:
109 * ... handle exception ...
112 * ... do tidying up ...
117 /*----- Exception type allocation -----------------------------------------*
119 * Nobody allocates exception types, so we'll just have to try to get along
120 * without too many collisions. An exception type is an unsigned long,
121 * which gives us four bytes. The top two bytes identify the library which
122 * `owns' the exception, with special values zero meaning `defined as part
123 * of the system' and 0xFFFF providing a shared space of types which can
124 * be used by anyone as long as they don't get seen by anyone else.
126 * The lower byte pair encodes a type number, and a value which defines
127 * the type of the value field (see below).
130 /* --- Type type of an exception --- */
132 typedef unsigned long exc_extype;
134 /* --- Build a byte pair from two characters --- *
136 * Note the icky casting to handle signed chars.
139 #define EXC_PAIR(x, y) (((unsigned long)(unsigned char)(x) << 8) | \
140 (unsigned long)(unsigned char)(y))
142 /* --- Allocate an exception number --- */
144 #define EXC_ALLOC(owner, type) (((unsigned long)(owner) << 16) | \
145 (unsigned long)(type))
147 /* --- Special owner codes --- */
149 #define EXC_GLOBAL 0u /* The global space defined here */
150 #define EXC_SHARED 0xFFFFu /* The shared space for everyone */
152 /*----- Exception values --------------------------------------------------*
154 * Exception values can have several different types. This is a mess, and
155 * C doesn't handle it too well, but we can try. I'll encode the value type
156 * as part of the exception type, in the top bits of the bottom byte. Messy?
160 /* --- Encoding a value type in an extype --- */
162 #define EXC_TYPECODE(t, w) (((w) & ~0xC0u) | ((t) & 0xC0u))
164 /* --- The various value types --- */
166 #define EXC_NOVAL 0x00u /* No interesting value */
167 #define EXC_INTVAL 0x40u /* Integer value */
168 #define EXC_PTRVAL 0x80u /* Arbitrary pointer value */
169 #define EXC_STRVAL 0xC0u /* Pointer to character string */
171 /* --- Allocating exceptions with appropriate types --- */
173 #define EXC_ALLOCN(o, t) EXC_TYPECODE(EXC_NOVAL, EXC_ALLOC(o, t))
174 #define EXC_ALLOCI(o, t) EXC_TYPECODE(EXC_INTVAL, EXC_ALLOC(o, t))
175 #define EXC_ALLOCP(o, t) EXC_TYPECODE(EXC_PTRVAL, EXC_ALLOC(o, t))
176 #define EXC_ALLOCS(o, t) EXC_TYPECODE(EXC_STRVAL, EXC_ALLOC(o, t))
178 /* --- A union representing the type --- */
180 typedef union exc_exval {
186 /*----- Predefined exceptions ---------------------------------------------*/
188 /* --- @EXC_NOMEM@ --- *
192 * Meaning: An attempt to allocate memory failed.
195 #define EXC_NOMEM EXC_ALLOCN(EXC_GLOBAL, 0u)
197 /* --- @EXC_ERRNO@ --- *
199 * Value: @int errno@ = the error raised
201 * Meaning: Some kind of OS error occurred.
204 #define EXC_ERRNO EXC_ALLOCI(EXC_GLOBAL, 1u)
206 /* --- @EXC_OSERROR@ --- *
208 * Value: @os_error *e@ = pointer to error block
210 * Meaning: For RISC OS programmers only: alternative way of propagating
214 #define EXC_OSERROR EXC_ALLOCP(EXC_GLOBAL, 1u)
216 /* --- @EXC_SIGNAL@ --- *
218 * Value: @int sig@ = signal number
220 * Meaning: Report the raising of a signal.
223 #define EXC_SIGNAL EXC_ALLOCI(EXC_GLOBAL, 2u)
225 /* --- @EXC_FAIL@ --- *
227 * Value: @const char *p@ = pointer to expanatory string
229 * Meaning: Miscellaneous error.
232 #define EXC_FAIL EXC_ALLOCS(EXC_GLOBAL, 0xFFu)
234 /*----- An exception handler block ----------------------------------------*/
236 /* --- Try to think of this as being opaque --- */
238 typedef struct __exc_hnd {
239 struct __exc_hnd *next; /* Pointer to next record down */
240 exc_extype type; /* Type of this exception */
241 exc_exval val; /* Value of this exception */
242 jmp_buf buf; /* Jump buffer when exceptions hit */
245 /*----- Global variables --------------------------------------------------*/
247 extern __exc_hnd *__exc_list; /* List of active handlers */
249 /*----- Macros ------------------------------------------------------------*/
251 /* --- References to current exception type and value --- */
253 #define exc_type (__exc_ec.type)
254 #define exc_val (__exc_ec.val)
255 #define exc_i (__exc_ec.val.i)
256 #define exc_p (__exc_ec.val.p)
257 #define exc_s (__exc_ec.val.s)
259 /* --- How it actually works --- *
261 * A `try' block is contained within a block which provides an exception
262 * handler buffer in automatic storage. This block is a loop, to allow
263 * `break' to escape from it. It adds the handler buffer to the top of a
264 * list, and does a `setjmp' to allow a return here following an exception.
265 * The `setjmp' returns zero for the `try' section, and nonzero if there's
266 * an exception to `catch'. It looks a little like this:
271 * if (!setjmp(h.buf)) {
272 * do <try code> while (0);
273 * remove_handler(&h);
278 * Everything else is ugly hacking to make things work.
281 /* --- Trying things which may cause exceptions --- */
284 volatile __exc_hnd __exc_ec; \
285 __exc_ec.next = __exc_list; \
286 __exc_list = (__exc_hnd *)&__exc_ec; \
287 if (!setjmp(*(jmp_buf *)&__exc_ec.buf /* very nasty! */ )) { do
289 #define EXIT_TRY do __exc_list = __exc_ec.next; while (0)
290 #define CATCH while (0); EXIT_TRY; } else
292 #define END_TRY } while (0)
294 /* --- Raising exceptions --- */
296 #define THROW __exc_throw
297 #define RETHROW __exc_rethrow(__exc_ec.type, __exc_ec.val)
299 /*----- Functions ---------------------------------------------------------*/
301 /* --- @exc_uncaught@ --- *
303 * Arguments: @void (*proc)(exc_extype type, exc_exval val) = new handler
305 * Returns: Pointer to the old handler value.
307 * Use: Sets the handler for uncaught exceptions.
310 typedef void (*exc__uncaught)(exc_extype /*type*/, exc_exval /*val*/);
311 extern exc__uncaught exc_uncaught(exc__uncaught /*proc*/);
313 /* --- @__exc_throw@ --- *
315 * Arguments: @exc_extype type@ = type of exception to throw
319 * Use: NOT FOR USER CONSUMPTION. Reads an appropriate exception
320 * value and throws an exception.
323 extern void __exc_throw(exc_extype /*type*/, ...);
325 /* --- @__exc_rethrow@ --- *
327 * Arguments: @exc_extype type@ = type of exception to throw
328 * @exc_exval val@ = value of exception to throw
332 * Use: NOT FOR USER CONSUMPTION. Does the donkey-work of raising
336 extern void __exc_rethrow(exc_extype /*type*/, exc_exval /*val*/);
338 /*----- That's all, folks -------------------------------------------------*/