0875b58f |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: exc.c,v 1.1 1998/06/17 23:44:42 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 General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (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 General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with mLib; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: exc.c,v $ |
32 | * Revision 1.1 1998/06/17 23:44:42 mdw |
33 | * Initial revision |
34 | * |
35 | */ |
36 | |
37 | #include <stdarg.h> |
38 | #include <stdlib.h> |
39 | #include <stdio.h> |
40 | |
41 | #include "exc.h" |
42 | |
43 | /*----- Global variables --------------------------------------------------*/ |
44 | |
45 | __exc_hnd *__exc_list = 0; |
46 | |
47 | /*----- Functions ---------------------------------------------------------*/ |
48 | |
49 | /* --- @exc__duff@ --- * |
50 | * |
51 | * Arguments: @exc_extype type@ = type of duff exception |
52 | * @exc_exval val@ = extra data supplied |
53 | * |
54 | * Returns: Doesn't |
55 | * |
56 | * Use: Default handler when everything goes wrong. |
57 | */ |
58 | |
59 | static void exc__duff(exc_extype type, exc_exval val) |
60 | { |
61 | fprintf(stderr, "fatal error: uncaught exception (type = %lu)\n", type); |
62 | abort(); |
63 | } |
64 | |
65 | /* --- @exc__duffproc@ --- * |
66 | * |
67 | * Current handler when there are no more exceptions left. |
68 | */ |
69 | |
70 | static exc__uncaught exc__duffproc = exc__duff; |
71 | |
72 | /* --- @exc_uncaught@ --- * |
73 | * |
74 | * Arguments: @void (*proc)(exc_extype type, exc_exval val) = new handler |
75 | * |
76 | * Returns: Pointer to the old handler value. |
77 | * |
78 | * Use: Sets the handler for uncaught exceptions. |
79 | */ |
80 | |
81 | exc__uncaught exc_uncaught(exc__uncaught proc) |
82 | { |
83 | exc__uncaught p = exc__duffproc; |
84 | if (proc) |
85 | exc__duffproc = proc; |
86 | return (p); |
87 | } |
88 | |
89 | /* --- @__exc_throw@ --- * |
90 | * |
91 | * Arguments: @exc_extype type@ = type of exception to throw |
92 | * |
93 | * Returns: Doesn't |
94 | * |
95 | * Use: NOT FOR USER CONSUMPTION. Reads an appropriate exception |
96 | * value and throws an exception. |
97 | */ |
98 | |
99 | void __exc_throw(exc_extype type, ...) |
100 | { |
101 | va_list ap; |
102 | exc_exval v; |
103 | |
104 | va_start(ap, type); |
105 | switch (type & 0xC0) { |
106 | case EXC_NOVAL: |
107 | v.i = 0; |
108 | break; |
109 | case EXC_INTVAL: |
110 | v.i = va_arg(ap, int); |
111 | break; |
112 | case EXC_PTRVAL: |
113 | v.p = va_arg(ap, void *); |
114 | break; |
115 | case EXC_STRVAL: |
116 | v.s = va_arg(ap, char *); |
117 | break; |
118 | } |
119 | va_end(ap); |
120 | __exc_rethrow(type, v); |
121 | } |
122 | |
123 | /* --- @__exc_rethrow@ --- * |
124 | * |
125 | * Arguments: @exc_extype type@ = type of exception to throw |
126 | * @exc_exval val@ = value of exception to throw |
127 | * |
128 | * Returns: Doesn't |
129 | * |
130 | * Use: NOT FOR USER CONSUMPTION. Does the donkey-work of raising |
131 | * an exception. |
132 | */ |
133 | |
134 | void __exc_rethrow(exc_extype type, exc_exval val) |
135 | { |
136 | __exc_hnd *p = __exc_list; |
137 | if (!p) |
138 | exc__duffproc(type, val); |
139 | p->type = type; |
140 | p->val = val; |
141 | __exc_list = p->next; |
142 | longjmp(p->buf, type); |
143 | } |
144 | |
145 | /*----- That's all, folks -------------------------------------------------*/ |