ffb5168aea400d58e1b200824b1499655e460a81
[projects/chimara/chimara.git] / interpreters / frotz / err.c
1 /* err.c - Runtime error reporting functions
2  *      Written by Jim Dunleavy <jim.dunleavy@erha.ie>
3  *
4  * This file is part of Frotz.
5  *
6  * Frotz is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Frotz is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include "frotz.h"
22
23 /* Define stuff for stricter Z-code error checking, for the generic
24    Unix/DOS/etc terminal-window interface. Feel free to change the way
25    player prefs are specified, or replace report_zstrict_error() 
26    completely if you want to change the way errors are reported. */
27
28 /* int err_report_mode = ERR_DEFAULT_REPORT_MODE; */
29
30 static int error_count[ERR_NUM_ERRORS];
31
32 static char *err_messages[] = {
33     "Text buffer overflow",
34     "Store out of dynamic memory",
35     "Division by zero",
36     "Illegal object",
37     "Illegal attribute",
38     "No such property",
39     "Stack overflow",
40     "Call to illegal address",
41     "Call to non-routine",
42     "Stack underflow",
43     "Illegal opcode",
44     "Bad stack frame",
45     "Jump to illegal address",
46     "Can't save while in interrupt",
47     "Nesting stream #3 too deep",
48     "Illegal window",
49     "Illegal window property",
50     "Print at illegal address",
51     "Illegal dictionary word length",
52     "@jin called with object 0",
53     "@get_child called with object 0",
54     "@get_parent called with object 0",
55     "@get_sibling called with object 0",
56     "@get_prop_addr called with object 0",
57     "@get_prop called with object 0",
58     "@put_prop called with object 0",
59     "@clear_attr called with object 0",
60     "@set_attr called with object 0",
61     "@test_attr called with object 0",
62     "@move_object called moving object 0",
63     "@move_object called moving into object 0",
64     "@remove_object called with object 0",
65     "@get_next_prop called with object 0"
66 };
67
68 static void print_long (unsigned long value, int base);
69
70 /*
71  * init_err
72  *
73  * Initialise error reporting.
74  *
75  */
76
77 void init_err (void)
78 {
79     int i;
80
81     /* Initialize the counters. */
82     
83     for (i = 0; i < ERR_NUM_ERRORS; i++)
84         error_count[i] = 0;
85 }
86
87 /*
88  * runtime_error
89  *
90  * An error has occurred. Ignore it, pass it to os_fatal or report
91  * it according to err_report_mode.
92  *
93  * errnum : Numeric code for error (1 to ERR_NUM_ERRORS)
94  *
95  */
96
97 void runtime_error (int errnum)
98 {
99     int wasfirst;
100     
101     if (errnum <= 0 || errnum > ERR_NUM_ERRORS)
102         return;
103
104     if (option_err_report_mode == ERR_REPORT_FATAL
105         || (!option_ignore_errors && errnum <= ERR_MAX_FATAL)) {
106         flush_buffer ();
107         os_fatal (err_messages[errnum - 1]);
108         return;
109     }
110
111     wasfirst = (error_count[errnum - 1] == 0);
112     error_count[errnum - 1]++;
113     
114     if ((option_err_report_mode == ERR_REPORT_ALWAYS)
115         || (option_err_report_mode == ERR_REPORT_ONCE && wasfirst)) {
116         long pc;
117
118         GET_PC (pc);
119         print_string ("Warning: ");
120         print_string (err_messages[errnum - 1]);
121         print_string (" (PC = ");
122         print_long (pc, 16);
123         print_char (')');
124         
125         if (option_err_report_mode == ERR_REPORT_ONCE) {
126             print_string (" (will ignore further occurrences)");
127         } else {
128             print_string (" (occurence ");
129             print_long (error_count[errnum - 1], 10);
130             print_char (')');
131         }
132         new_line ();
133     }
134
135 } /* report_error */
136
137 /*
138  * print_long
139  *
140  * Print an unsigned 32bit number in decimal or hex.
141  *
142  */
143
144 static void print_long (unsigned long value, int base)
145 {
146     unsigned long i;
147     char c;
148
149     for (i = (base == 10 ? 1000000000 : 0x10000000); i != 0; i /= base)
150         if (value >= i || i == 1) {
151             c = (value / i) % base;
152             print_char (c + (c <= 9 ? '0' : 'a' - 10));
153         }
154
155 }/* print_long */