3 * $Id: exc.c,v 1.6 2004/04/08 01:36:11 mdw Exp $
5 * Structured exception handling in C
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
36 /*----- Global variables --------------------------------------------------*/
38 __exc_hnd
*__exc_list
= 0;
40 /*----- Functions ---------------------------------------------------------*/
44 * Arguments: @exc_extype type@ = type of duff exception
45 * @exc_exval val@ = extra data supplied
49 * Use: Default handler when everything goes wrong.
52 static void duff(exc_extype type
, exc_exval val
)
54 fprintf(stderr
, "fatal error: uncaught exception (type = %lx)\n", type
);
58 /* --- @duffproc@ --- *
60 * Current handler when there are no more exceptions left.
63 static exc__uncaught duffproc
= duff
;
65 /* --- @exc_uncaught@ --- *
67 * Arguments: @void (*proc)(exc_extype type, exc_exval val) = new handler
69 * Returns: Pointer to the old handler value.
71 * Use: Sets the handler for uncaught exceptions.
74 exc__uncaught
exc_uncaught(exc__uncaught proc
)
76 exc__uncaught p
= duffproc
;
82 /* --- @__exc_throw@ --- *
84 * Arguments: @exc_extype type@ = type of exception to throw
88 * Use: NOT FOR USER CONSUMPTION. Reads an appropriate exception
89 * value and throws an exception.
92 void __exc_throw(exc_extype type
, ...)
98 switch (type
& 0xC0) {
103 v
.i
= va_arg(ap
, int);
106 v
.p
= va_arg(ap
, void *);
109 v
.s
= va_arg(ap
, char *);
113 __exc_rethrow(type
, v
);
116 /* --- @__exc_rethrow@ --- *
118 * Arguments: @exc_extype type@ = type of exception to throw
119 * @exc_exval val@ = value of exception to throw
123 * Use: NOT FOR USER CONSUMPTION. Does the donkey-work of raising
127 void __exc_rethrow(exc_extype type
, exc_exval val
)
129 __exc_hnd
*p
= __exc_list
;
134 __exc_list
= p
->next
;
135 longjmp(p
->buf
, type
);
138 /*----- That's all, folks -------------------------------------------------*/