chiark / gitweb /
New version number.
[mLib] / exc.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
de907046 3 * $Id: exc.c,v 1.4 1999/05/17 20:35:30 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
0bd98442 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.
0875b58f 28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: exc.c,v $
de907046 33 * Revision 1.4 1999/05/17 20:35:30 mdw
34 * Output uncaught exception types in hex, because they're easier to
35 * translate that way.
36 *
0bd98442 37 * Revision 1.3 1999/05/06 19:51:35 mdw
38 * Reformatted the LGPL notice a little bit.
39 *
c846879c 40 * Revision 1.2 1999/05/05 18:50:31 mdw
41 * Change licensing conditions to LGPL.
42 *
43 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
44 * Initial version of mLib
0875b58f 45 *
46 */
47
48#include <stdarg.h>
49#include <stdlib.h>
50#include <stdio.h>
51
52#include "exc.h"
53
54/*----- Global variables --------------------------------------------------*/
55
56__exc_hnd *__exc_list = 0;
57
58/*----- Functions ---------------------------------------------------------*/
59
60/* --- @exc__duff@ --- *
61 *
62 * Arguments: @exc_extype type@ = type of duff exception
63 * @exc_exval val@ = extra data supplied
64 *
65 * Returns: Doesn't
66 *
67 * Use: Default handler when everything goes wrong.
68 */
69
70static void exc__duff(exc_extype type, exc_exval val)
71{
de907046 72 fprintf(stderr, "fatal error: uncaught exception (type = %lx)\n", type);
0875b58f 73 abort();
74}
75
76/* --- @exc__duffproc@ --- *
77 *
78 * Current handler when there are no more exceptions left.
79 */
80
81static exc__uncaught exc__duffproc = exc__duff;
82
83/* --- @exc_uncaught@ --- *
84 *
85 * Arguments: @void (*proc)(exc_extype type, exc_exval val) = new handler
86 *
87 * Returns: Pointer to the old handler value.
88 *
89 * Use: Sets the handler for uncaught exceptions.
90 */
91
92exc__uncaught exc_uncaught(exc__uncaught proc)
93{
94 exc__uncaught p = exc__duffproc;
95 if (proc)
96 exc__duffproc = proc;
97 return (p);
98}
99
100/* --- @__exc_throw@ --- *
101 *
102 * Arguments: @exc_extype type@ = type of exception to throw
103 *
104 * Returns: Doesn't
105 *
106 * Use: NOT FOR USER CONSUMPTION. Reads an appropriate exception
107 * value and throws an exception.
108 */
109
110void __exc_throw(exc_extype type, ...)
111{
112 va_list ap;
113 exc_exval v;
114
115 va_start(ap, type);
116 switch (type & 0xC0) {
117 case EXC_NOVAL:
118 v.i = 0;
119 break;
120 case EXC_INTVAL:
121 v.i = va_arg(ap, int);
122 break;
123 case EXC_PTRVAL:
124 v.p = va_arg(ap, void *);
125 break;
126 case EXC_STRVAL:
127 v.s = va_arg(ap, char *);
128 break;
129 }
130 va_end(ap);
131 __exc_rethrow(type, v);
132}
133
134/* --- @__exc_rethrow@ --- *
135 *
136 * Arguments: @exc_extype type@ = type of exception to throw
137 * @exc_exval val@ = value of exception to throw
138 *
139 * Returns: Doesn't
140 *
141 * Use: NOT FOR USER CONSUMPTION. Does the donkey-work of raising
142 * an exception.
143 */
144
145void __exc_rethrow(exc_extype type, exc_exval val)
146{
147 __exc_hnd *p = __exc_list;
148 if (!p)
149 exc__duffproc(type, val);
150 p->type = type;
151 p->val = val;
152 __exc_list = p->next;
153 longjmp(p->buf, type);
154}
155
156/*----- That's all, folks -------------------------------------------------*/