61ca78ce3bd13f8207740104f432f1cddc420107
[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     "@jin called with object 0",
52     "@get_child called with object 0",
53     "@get_parent called with object 0",
54     "@get_sibling called with object 0",
55     "@get_prop_addr called with object 0",
56     "@get_prop called with object 0",
57     "@put_prop called with object 0",
58     "@clear_attr called with object 0",
59     "@set_attr called with object 0",
60     "@test_attr called with object 0",
61     "@move_object called moving object 0",
62     "@move_object called moving into object 0",
63     "@remove_object called with object 0",
64     "@get_next_prop called with object 0"
65 };
66
67 static void print_long (unsigned long value, int base);
68
69 /*
70  * init_err
71  *
72  * Initialise error reporting.
73  *
74  */
75
76 void init_err (void)
77 {
78     int i;
79
80     /* Initialize the counters. */
81     
82     for (i = 0; i < ERR_NUM_ERRORS; i++)
83         error_count[i] = 0;
84 }
85
86 /*
87  * runtime_error
88  *
89  * An error has occurred. Ignore it, pass it to os_fatal or report
90  * it according to err_report_mode.
91  *
92  * errnum : Numeric code for error (1 to ERR_NUM_ERRORS)
93  *
94  */
95
96 void runtime_error (int errnum)
97 {
98     int wasfirst;
99     
100     if (errnum <= 0 || errnum > ERR_NUM_ERRORS)
101         return;
102
103     if (f_setup.err_report_mode == ERR_REPORT_FATAL
104         || (!f_setup.ignore_errors && errnum <= ERR_MAX_FATAL)) {
105         flush_buffer ();
106         os_fatal (err_messages[errnum - 1]);
107         return;
108     }
109
110     wasfirst = (error_count[errnum - 1] == 0);
111     error_count[errnum - 1]++;
112     
113     if ((f_setup.err_report_mode == ERR_REPORT_ALWAYS)
114         || (f_setup.err_report_mode == ERR_REPORT_ONCE && wasfirst)) {
115         long pc;
116
117         GET_PC (pc);
118         print_string ("Warning: ");
119         print_string (err_messages[errnum - 1]);
120         print_string (" (PC = ");
121         print_long (pc, 16);
122         print_char (')');
123         
124         if (f_setup.err_report_mode == ERR_REPORT_ONCE) {
125             print_string (" (will ignore further occurrences)");
126         } else {
127             print_string (" (occurence ");
128             print_long (error_count[errnum - 1], 10);
129             print_char (')');
130         }
131         new_line ();
132     }
133
134 } /* report_error */
135
136 /*
137  * print_long
138  *
139  * Print an unsigned 32bit number in decimal or hex.
140  *
141  */
142
143 static void print_long (unsigned long value, int base)
144 {
145     unsigned long i;
146     char c;
147
148     for (i = (base == 10 ? 1000000000 : 0x10000000); i != 0; i /= base)
149         if (value >= i || i == 1) {
150             c = (value / i) % base;
151             print_char (c + (c <= 9 ? '0' : 'a' - 10));
152         }
153
154 }/* print_long */