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