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