a49332e48f7cfb4fa9b83294723a4859f7417534
[projects/chimara/chimara.git] / interpreters / nitfol / errmesg.c
1 /*  Nitfol - z-machine interpreter using Glk for output.
2     Copyright (C) 1999  Evin Robertson
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
17
18     The author can be reached at nitfol@deja.com
19 */
20 #include "nitfol.h"
21
22 #ifdef HEADER
23 typedef enum { E_INSTR, E_OBJECT, E_STACK,  E_MEMORY,  E_MATH,    E_STRING,
24                E_OUTPUT, E_SOUND, E_SYSTEM, E_VERSION, E_CORRUPT, E_SAVE,
25                E_DEBUG } errortypes;
26 #endif
27
28 static const char *errortypenames[] = {
29   "instruc",
30   "object",
31   "stack",
32   "memory",
33   "math",
34   "string",
35   "output",
36   "sound",
37   "system",
38   "version",
39   "corrupt",
40   "save",
41   "debug"
42 };
43
44
45 /* These all feature loop detection, so if error reporting spawns another
46    error, we won't infinite loop.  Hopefully.
47 */
48
49 void n_show_warn(errortypes type, const char *message, offset number)
50 {
51   if(!ignore_errors && allow_output) {
52     showstuff("WARN", errortypenames[type], message, number);
53   }
54 }
55
56 void n_show_port(errortypes type, const char *message, offset number)
57 {
58   if(!ignore_errors && allow_output) {
59     showstuff("PORT", errortypenames[type], message, number);
60   }
61 }
62
63 void n_show_error(errortypes type, const char *message, offset number)
64 {
65   if(!ignore_errors && allow_output) {
66     showstuff("ERROR", errortypenames[type], message, number);
67   }
68 }
69
70 /* showstuff calls n_show_fatal if it's looping uncontrollably, but it
71    disables its loopiness detector so it can show this fatal error.  So
72    n_show_fatal needs its own loop detection. */
73 void n_show_fatal(errortypes type, const char *message, offset number)
74 {
75   static BOOL loopy = FALSE;
76   if(loopy) {
77     /* puts("loopy"); */
78     glk_exit();
79   }
80   loopy = TRUE;
81   showstuff("FATAL", errortypenames[type], message, number);
82   loopy = FALSE;
83
84   glk_exit();
85 }
86
87 void n_show_debug(errortypes type, const char *message, offset number)
88 {
89 #ifndef HIDE_DEBUG
90   static BOOL loopy = FALSE;
91   if(loopy)
92     n_show_fatal(E_SYSTEM, "loopy debug error", 0);
93   loopy = TRUE;
94   showstuff("E_DEBUG", errortypenames[type], message, number);
95   loopy = FALSE;
96 #endif
97 }
98
99 zword z_range_error(offset p)
100 {
101   if(!ignore_errors) {
102     static BOOL loopy = FALSE;
103     if(loopy)
104       n_show_fatal(E_SYSTEM, "loopy range error", 0);
105     loopy = TRUE;
106     showstuff("RANGE", errortypenames[E_MEMORY], "invalid memory access", p);
107     loopy = FALSE;
108   }
109   return 0;
110 }