chiark / gitweb /
Whoops. I'd left the type of the jump buffer as `sigjmp_buf'.
[mLib] / exc.h
1 /* -*-c-*-
2  *
3  * $Id: exc.h,v 1.3 1999/05/06 19:51:35 mdw Exp $
4  *
5  * Structured exception handling in C
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------*
31  *
32  * $Log: exc.h,v $
33  * Revision 1.3  1999/05/06 19:51:35  mdw
34  * Reformatted the LGPL notice a little bit.
35  *
36  * Revision 1.2  1999/05/05 18:50:31  mdw
37  * Change licensing conditions to LGPL.
38  *
39  * Revision 1.1.1.1  1998/06/17 23:44:42  mdw
40  * Initial version of mLib
41  *
42  */
43
44 #ifndef EXC_H
45 #define EXC_H
46
47 #ifdef __cplusplus
48   extern "C" {
49 #endif
50
51 #include <setjmp.h>
52
53 /*----- Quick documentation -----------------------------------------------*
54  *
55  * This header file provides some exception handling facilities in C
56  * programs.  It modifies the syntax of the language slightly, using the
57  * preprocessor.
58  *
59  * The `throw' expression returns no value.  It has the syntax:
60  *
61  *   THROW ( expr , expr )
62  *
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.
67  *
68  * The `RETHROW' expression, valid only within an exception handler, causes
69  * the current exception to be thrown again.
70  *
71  * A `try' statement has the syntax:
72  *
73  *   TRY stat CATCH stat END_TRY;
74  *
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.
80  *
81  * The test statement may complete in one of these ways:
82  *
83  *   * Normal completion -- control reaches the end of the statement
84  *     normally.
85  *
86  *   * Throwing an exception -- an exception is thrown when the handler is
87  *     the current exception handler.
88  *
89  *   * By executing a `break' statement.
90  *
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).
94  *
95  * Any other attempt to leave the test causes undefined behaviour.
96  *
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.
104  *
105  * Control is passed to handlers using the `longjmp' function.
106  *
107  * Example:
108  *
109  *   TRY {
110  *     ... something dangerous ...
111  *   } CATCH switch (exc_type) {
112  *     case EXC_INTERESTING:
113  *       ... handle exception ...
114  *       break;
115  *     default:
116  *       ... do tidying up ...
117  *       RETHROW;
118  *   } END_TRY;
119  */
120
121 /*----- Exception type allocation -----------------------------------------*
122  *
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.
129  *
130  * The lower byte pair encodes a type number, and a value which defines
131  * the type of the value field (see below).
132  */
133
134 /* --- Type type of an exception --- */
135
136 typedef unsigned long exc_extype;
137
138 /* --- Build a byte pair from two characters --- *
139  *
140  * Note the icky casting to handle signed chars.
141  */
142
143 #define EXC_PAIR(x, y) (((unsigned long)(unsigned char)(x) << 8) |      \
144                         (unsigned long)(unsigned char)(y))
145
146 /* --- Allocate an exception number --- */
147
148 #define EXC_ALLOC(owner, type) (((unsigned long)(owner) << 16) |        \
149                                 (unsigned long)(type))
150
151 /* --- Special owner codes --- */
152
153 #define EXC_GLOBAL 0u                   /* The global space defined here */
154 #define EXC_SHARED 0xFFFFu              /* The shared space for everyone */
155
156 /*----- Exception values --------------------------------------------------*
157  *
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?
161  * You betcha.
162  */
163
164 /* --- Encoding a value type in an extype --- */
165
166 #define EXC_TYPECODE(t, w) (((w) & ~0xC0u) | ((t) & 0xC0u))
167
168 /* --- The various value types --- */
169
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 */
174
175 /* --- Allocating exceptions with appropriate types --- */
176
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))
181
182 /* --- A union representing the type --- */
183
184 typedef union exc_exval {
185   int i;
186   void *p;
187   char *s;
188 } exc_exval;
189
190 /*----- Predefined exceptions ---------------------------------------------*/
191
192 /* --- @EXC_NOMEM@ --- *
193  *
194  * Value:       ---
195  *
196  * Meaning:     An attempt to allocate memory failed.
197  */
198
199 #define EXC_NOMEM EXC_ALLOCN(EXC_GLOBAL, 0u)
200
201 /* --- @EXC_ERRNO@ --- *
202  *
203  * Value:       @int errno@ = the error raised
204  *
205  * Meaning:     Some kind of OS error occurred.
206  */
207
208 #define EXC_ERRNO EXC_ALLOCI(EXC_GLOBAL, 1u)
209
210 /* --- @EXC_OSERROR@ --- *
211  *
212  * Value:       @os_error *e@ = pointer to error block
213  *
214  * Meaning:     For RISC OS programmers only: alternative way of propagating
215  *              errors.
216  */
217
218 #define EXC_OSERROR EXC_ALLOCP(EXC_GLOBAL, 1u)
219
220 /* --- @EXC_SIGNAL@ --- *
221  *
222  * Value:       @int sig@ = signal number
223  *
224  * Meaning:     Report the raising of a signal.
225  */
226
227 #define EXC_SIGNAL EXC_ALLOCI(EXC_GLOBAL, 2u)
228
229 /* --- @EXC_FAIL@ --- *
230  *
231  * Value:       @const char *p@ = pointer to expanatory string
232  *
233  * Meaning:     Miscellaneous error.
234  */
235
236 #define EXC_FAIL EXC_ALLOCS(EXC_GLOBAL, 0xFFu)
237
238 /*----- An exception handler block ----------------------------------------*/
239
240 /* --- Try to think of this as being opaque --- */
241
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 */
247 } __exc_hnd;
248
249 /*----- Global variables --------------------------------------------------*/
250
251 extern __exc_hnd *__exc_list;           /* List of active handlers */
252
253 /*----- Macros ------------------------------------------------------------*/
254
255 /* --- References to current exception type and value --- */
256
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)
262
263 /* --- How it actually works --- *
264  *
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:
271  *
272  *   do {
273  *     __exc_hnd h;
274  *     add_handler(&h);
275  *     if (!setjmp(h.buf)) {
276  *       do <try code> while (0);
277  *       remove_handler(&h);
278  *     } else
279  *       <catch code>
280  *   } while (0)
281  *
282  * Everything else is ugly hacking to make things work.
283  */
284
285 /* --- Trying things which may cause exceptions --- */
286
287 #define TRY do {                                                        \
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
292
293 #define EXIT_TRY do __exc_list = __exc_ec.next; while (0)
294 #define CATCH while (0); EXIT_TRY; } else
295
296 #define END_TRY } while (0)
297
298 /* --- Raising exceptions --- */
299
300 #define THROW __exc_throw
301 #define RETHROW __exc_rethrow(__exc_ec.type, __exc_ec.val)
302
303 /*----- Functions ---------------------------------------------------------*/
304
305 /* --- @exc_uncaught@ --- *
306  *
307  * Arguments:   @void (*proc)(exc_extype type, exc_exval val) = new handler
308  *
309  * Returns:     Pointer to the old handler value.
310  *
311  * Use:         Sets the handler for uncaught exceptions.
312  */
313
314 typedef void (*exc__uncaught)(exc_extype /*type*/, exc_exval /*val*/);
315 extern exc__uncaught exc_uncaught(exc__uncaught /*proc*/);
316
317 /* --- @__exc_throw@ --- *
318  *
319  * Arguments:   @exc_extype type@ = type of exception to throw
320  *
321  * Returns:     Doesn't
322  *
323  * Use:         NOT FOR USER CONSUMPTION.  Reads an appropriate exception
324  *              value and throws an exception.
325  */
326
327 extern void __exc_throw(exc_extype /*type*/, ...);
328
329 /* --- @__exc_rethrow@ --- *
330  *
331  * Arguments:   @exc_extype type@ = type of exception to throw
332  *              @exc_exval val@ = value of exception to throw
333  *
334  * Returns:     Doesn't
335  *
336  * Use:         NOT FOR USER CONSUMPTION.  Does the donkey-work of raising
337  *              an exception.
338  */
339
340 extern void __exc_rethrow(exc_extype /*type*/, exc_exval /*val*/);
341
342 /*----- That's all, folks -------------------------------------------------*/
343
344 #ifdef __cplusplus
345   }
346 #endif
347
348 #endif