3 * $Id: exc.h,v 1.3 1999/05/06 19:51:35 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
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 1999/05/06 19:51:35 mdw
34 * Reformatted the LGPL notice a little bit.
36 * Revision 1.2 1999/05/05 18:50:31 mdw
37 * Change licensing conditions to LGPL.
39 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
40 * Initial version of mLib
53 /*----- Quick documentation -----------------------------------------------*
55 * This header file provides some exception handling facilities in C
56 * programs. It modifies the syntax of the language slightly, using the
59 * The `throw' expression returns no value. It has the syntax:
61 * THROW ( expr , expr )
63 * The first expression must have type compatible with unsigned integer; it
64 * identifies an `exception type'. The second must have type compatible
65 * with pointer to void; it contains the `exception data'. Control is
66 * passed to the current exception handler.
68 * The `RETHROW' expression, valid only within an exception handler, causes
69 * the current exception to be thrown again.
71 * A `try' statement has the syntax:
73 * TRY stat CATCH stat END_TRY;
75 * The first statement is called the `test'; the second is the `handler'.
76 * During execution of the test, the handler is added to a stack of
77 * active exception handlers; the topmost handler on this stack is called
78 * the `current' handler. When execution of the test completes, the
79 * corresponding handler is removed from the stack.
81 * The test statement may complete in one of these ways:
83 * * Normal completion -- control reaches the end of the statement
86 * * Throwing an exception -- an exception is thrown when the handler is
87 * the current exception handler.
89 * * By executing a `break' statement.
91 * * By executing the expression `EXIT_TRY' and transferring control to
92 * a point outside the entire `try' statement (e.g., executing a `goto'
93 * or `return' statement).
95 * Any other attempt to leave the test causes undefined behaviour.
97 * If an exception is thrown while the handler is the current exception
98 * handler, it is given control. The variables `exc_type' and `exc_val'
99 * denote the exception type and value respectively -- they are passed
100 * unchanged from the `throw' expression which caused the exception.
101 * A handler is deactivated before it is invoked; if it causes an
102 * exception to be thrown (and does not contain a nested `try' statement)
103 * control will be passed to an earlier active handler.
105 * Control is passed to handlers using the `longjmp' function.
110 * ... something dangerous ...
111 * } CATCH switch (exc_type) {
112 * case EXC_INTERESTING:
113 * ... handle exception ...
116 * ... do tidying up ...
121 /*----- Exception type allocation -----------------------------------------*
123 * Nobody allocates exception types, so we'll just have to try to get along
124 * without too many collisions. An exception type is an unsigned long,
125 * which gives us four bytes. The top two bytes identify the library which
126 * `owns' the exception, with special values zero meaning `defined as part
127 * of the system' and 0xFFFF providing a shared space of types which can
128 * be used by anyone as long as they don't get seen by anyone else.
130 * The lower byte pair encodes a type number, and a value which defines
131 * the type of the value field (see below).
134 /* --- Type type of an exception --- */
136 typedef unsigned long exc_extype;
138 /* --- Build a byte pair from two characters --- *
140 * Note the icky casting to handle signed chars.
143 #define EXC_PAIR(x, y) (((unsigned long)(unsigned char)(x) << 8) | \
144 (unsigned long)(unsigned char)(y))
146 /* --- Allocate an exception number --- */
148 #define EXC_ALLOC(owner, type) (((unsigned long)(owner) << 16) | \
149 (unsigned long)(type))
151 /* --- Special owner codes --- */
153 #define EXC_GLOBAL 0u /* The global space defined here */
154 #define EXC_SHARED 0xFFFFu /* The shared space for everyone */
156 /*----- Exception values --------------------------------------------------*
158 * Exception values can have several different types. This is a mess, and
159 * C doesn't handle it too well, but we can try. I'll encode the value type
160 * as part of the exception type, in the top bits of the bottom byte. Messy?
164 /* --- Encoding a value type in an extype --- */
166 #define EXC_TYPECODE(t, w) (((w) & ~0xC0u) | ((t) & 0xC0u))
168 /* --- The various value types --- */
170 #define EXC_NOVAL 0x00u /* No interesting value */
171 #define EXC_INTVAL 0x40u /* Integer value */
172 #define EXC_PTRVAL 0x80u /* Arbitrary pointer value */
173 #define EXC_STRVAL 0xC0u /* Pointer to character string */
175 /* --- Allocating exceptions with appropriate types --- */
177 #define EXC_ALLOCN(o, t) EXC_TYPECODE(EXC_NOVAL, EXC_ALLOC(o, t))
178 #define EXC_ALLOCI(o, t) EXC_TYPECODE(EXC_INTVAL, EXC_ALLOC(o, t))
179 #define EXC_ALLOCP(o, t) EXC_TYPECODE(EXC_PTRVAL, EXC_ALLOC(o, t))
180 #define EXC_ALLOCS(o, t) EXC_TYPECODE(EXC_STRVAL, EXC_ALLOC(o, t))
182 /* --- A union representing the type --- */
184 typedef union exc_exval {
190 /*----- Predefined exceptions ---------------------------------------------*/
192 /* --- @EXC_NOMEM@ --- *
196 * Meaning: An attempt to allocate memory failed.
199 #define EXC_NOMEM EXC_ALLOCN(EXC_GLOBAL, 0u)
201 /* --- @EXC_ERRNO@ --- *
203 * Value: @int errno@ = the error raised
205 * Meaning: Some kind of OS error occurred.
208 #define EXC_ERRNO EXC_ALLOCI(EXC_GLOBAL, 1u)
210 /* --- @EXC_OSERROR@ --- *
212 * Value: @os_error *e@ = pointer to error block
214 * Meaning: For RISC OS programmers only: alternative way of propagating
218 #define EXC_OSERROR EXC_ALLOCP(EXC_GLOBAL, 1u)
220 /* --- @EXC_SIGNAL@ --- *
222 * Value: @int sig@ = signal number
224 * Meaning: Report the raising of a signal.
227 #define EXC_SIGNAL EXC_ALLOCI(EXC_GLOBAL, 2u)
229 /* --- @EXC_FAIL@ --- *
231 * Value: @const char *p@ = pointer to expanatory string
233 * Meaning: Miscellaneous error.
236 #define EXC_FAIL EXC_ALLOCS(EXC_GLOBAL, 0xFFu)
238 /*----- An exception handler block ----------------------------------------*/
240 /* --- Try to think of this as being opaque --- */
242 typedef struct __exc_hnd {
243 struct __exc_hnd *next; /* Pointer to next record down */
244 exc_extype type; /* Type of this exception */
245 exc_exval val; /* Value of this exception */
246 jmp_buf buf; /* Jump buffer when exceptions hit */
249 /*----- Global variables --------------------------------------------------*/
251 extern __exc_hnd *__exc_list; /* List of active handlers */
253 /*----- Macros ------------------------------------------------------------*/
255 /* --- References to current exception type and value --- */
257 #define exc_type (__exc_ec.type)
258 #define exc_val (__exc_ec.val)
259 #define exc_i (__exc_ec.val.i)
260 #define exc_p (__exc_ec.val.p)
261 #define exc_s (__exc_ec.val.s)
263 /* --- How it actually works --- *
265 * A `try' block is contained within a block which provides an exception
266 * handler buffer in automatic storage. This block is a loop, to allow
267 * `break' to escape from it. It adds the handler buffer to the top of a
268 * list, and does a `setjmp' to allow a return here following an exception.
269 * The `setjmp' returns zero for the `try' section, and nonzero if there's
270 * an exception to `catch'. It looks a little like this:
275 * if (!setjmp(h.buf)) {
276 * do <try code> while (0);
277 * remove_handler(&h);
282 * Everything else is ugly hacking to make things work.
285 /* --- Trying things which may cause exceptions --- */
288 volatile __exc_hnd __exc_ec; \
289 __exc_ec.next = __exc_list; \
290 __exc_list = (__exc_hnd *)&__exc_ec; \
291 if (!setjmp(*(jmp_buf *)&__exc_ec.buf /* very nasty! */ )) { do
293 #define EXIT_TRY do __exc_list = __exc_ec.next; while (0)
294 #define CATCH while (0); EXIT_TRY; } else
296 #define END_TRY } while (0)
298 /* --- Raising exceptions --- */
300 #define THROW __exc_throw
301 #define RETHROW __exc_rethrow(__exc_ec.type, __exc_ec.val)
303 /*----- Functions ---------------------------------------------------------*/
305 /* --- @exc_uncaught@ --- *
307 * Arguments: @void (*proc)(exc_extype type, exc_exval val) = new handler
309 * Returns: Pointer to the old handler value.
311 * Use: Sets the handler for uncaught exceptions.
314 typedef void (*exc__uncaught)(exc_extype /*type*/, exc_exval /*val*/);
315 extern exc__uncaught exc_uncaught(exc__uncaught /*proc*/);
317 /* --- @__exc_throw@ --- *
319 * Arguments: @exc_extype type@ = type of exception to throw
323 * Use: NOT FOR USER CONSUMPTION. Reads an appropriate exception
324 * value and throws an exception.
327 extern void __exc_throw(exc_extype /*type*/, ...);
329 /* --- @__exc_rethrow@ --- *
331 * Arguments: @exc_extype type@ = type of exception to throw
332 * @exc_exval val@ = value of exception to throw
336 * Use: NOT FOR USER CONSUMPTION. Does the donkey-work of raising
340 extern void __exc_rethrow(exc_extype /*type*/, exc_exval /*val*/);
342 /*----- That's all, folks -------------------------------------------------*/