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