chiark / gitweb /
url: Allow `;' to separate key/value pairs in URL-encoded strings.
[mLib] / exc.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
8656dc50 3 * $Id: exc.c,v 1.6 2004/04/08 01:36: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
0875b58f 30#include <stdarg.h>
31#include <stdlib.h>
32#include <stdio.h>
33
34#include "exc.h"
35
36/*----- Global variables --------------------------------------------------*/
37
38__exc_hnd *__exc_list = 0;
39
40/*----- Functions ---------------------------------------------------------*/
41
ff0f0220 42/* --- @duff@ --- *
0875b58f 43 *
44 * Arguments: @exc_extype type@ = type of duff exception
45 * @exc_exval val@ = extra data supplied
46 *
47 * Returns: Doesn't
48 *
49 * Use: Default handler when everything goes wrong.
50 */
51
ff0f0220 52static void duff(exc_extype type, exc_exval val)
0875b58f 53{
de907046 54 fprintf(stderr, "fatal error: uncaught exception (type = %lx)\n", type);
0875b58f 55 abort();
56}
57
ff0f0220 58/* --- @duffproc@ --- *
0875b58f 59 *
60 * Current handler when there are no more exceptions left.
61 */
62
ff0f0220 63static exc__uncaught duffproc = duff;
0875b58f 64
65/* --- @exc_uncaught@ --- *
66 *
67 * Arguments: @void (*proc)(exc_extype type, exc_exval val) = new handler
68 *
69 * Returns: Pointer to the old handler value.
70 *
71 * Use: Sets the handler for uncaught exceptions.
72 */
73
74exc__uncaught exc_uncaught(exc__uncaught proc)
75{
ff0f0220 76 exc__uncaught p = duffproc;
0875b58f 77 if (proc)
ff0f0220 78 duffproc = proc;
0875b58f 79 return (p);
80}
81
82/* --- @__exc_throw@ --- *
83 *
84 * Arguments: @exc_extype type@ = type of exception to throw
85 *
86 * Returns: Doesn't
87 *
88 * Use: NOT FOR USER CONSUMPTION. Reads an appropriate exception
89 * value and throws an exception.
90 */
91
92void __exc_throw(exc_extype type, ...)
93{
94 va_list ap;
95 exc_exval v;
96
97 va_start(ap, type);
98 switch (type & 0xC0) {
99 case EXC_NOVAL:
100 v.i = 0;
101 break;
102 case EXC_INTVAL:
103 v.i = va_arg(ap, int);
104 break;
105 case EXC_PTRVAL:
106 v.p = va_arg(ap, void *);
107 break;
108 case EXC_STRVAL:
109 v.s = va_arg(ap, char *);
110 break;
111 }
112 va_end(ap);
113 __exc_rethrow(type, v);
114}
115
116/* --- @__exc_rethrow@ --- *
117 *
118 * Arguments: @exc_extype type@ = type of exception to throw
119 * @exc_exval val@ = value of exception to throw
120 *
121 * Returns: Doesn't
122 *
123 * Use: NOT FOR USER CONSUMPTION. Does the donkey-work of raising
124 * an exception.
125 */
126
127void __exc_rethrow(exc_extype type, exc_exval val)
128{
129 __exc_hnd *p = __exc_list;
130 if (!p)
ff0f0220 131 duffproc(type, val);
0875b58f 132 p->type = type;
133 p->val = val;
134 __exc_list = p->next;
135 longjmp(p->buf, type);
136}
137
138/*----- That's all, folks -------------------------------------------------*/