* error.c (scm_wta): Added support for SCM_ARG6 and SCM_ARG7.
[bpt/guile.git] / libguile / error.c
1 /* Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41 \f
42
43 #include <stdio.h>
44 #include "_scm.h"
45 #include "pairs.h"
46 #include "genio.h"
47 #include "throw.h"
48
49 #include "error.h"
50
51 #ifdef HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54 \f
55
56 \f
57 /* {Errors and Exceptional Conditions}
58 */
59
60
61 extern int errno;
62
63 /* All errors should pass through here. */
64 void
65 scm_error (key, subr, message, args, rest)
66 SCM key;
67 char *subr;
68 char *message;
69 SCM args;
70 SCM rest;
71 {
72 SCM arg_list;
73 arg_list = scm_listify (subr ? scm_makfrom0str (subr) : SCM_BOOL_F,
74 message ? scm_makfrom0str (message) : SCM_BOOL_F,
75 args,
76 rest,
77 SCM_UNDEFINED);
78 scm_ithrow (key, arg_list, 1);
79
80 /* No return, but just in case: */
81
82 write (2, "unhandled system error\n",
83 sizeof ("unhandled system error\n") - 1);
84 exit (1);
85 }
86
87 /* Scheme interface to scm_error. */
88 SCM_PROC(s_error_scm, "scm-error", 5, 0, 0, scm_error_scm);
89 SCM
90 scm_error_scm (key, subr, message, args, rest)
91 SCM key;
92 SCM subr;
93 SCM message;
94 SCM args;
95 SCM rest;
96 {
97 SCM_ASSERT (SCM_NIMP (key) && SCM_SYMBOLP (key), key, SCM_ARG1, s_error_scm);
98 SCM_ASSERT (SCM_FALSEP (subr) || (SCM_NIMP (subr) && SCM_ROSTRINGP (subr)),
99 subr, SCM_ARG2, s_error_scm);
100 SCM_ASSERT (SCM_FALSEP (message)
101 || (SCM_NIMP (message) && SCM_ROSTRINGP (message)),
102 message, SCM_ARG3, s_error_scm);
103
104 SCM_COERCE_SUBSTR (message);
105
106 scm_error (key,
107 (SCM_FALSEP (subr)) ? NULL : SCM_ROCHARS (subr),
108 (SCM_FALSEP (message)) ? NULL : SCM_ROCHARS (message),
109 args,
110 rest);
111 /* not reached. */
112 }
113
114 SCM_PROC (s_strerror, "strerror", 1, 0, 0, scm_strerror);
115 SCM
116 scm_strerror (SCM err)
117 {
118 SCM_ASSERT (SCM_INUMP (err), err, SCM_ARG1, s_strerror);
119 return scm_makfrom0str (strerror (SCM_INUM (err)));
120 }
121
122 SCM_SYMBOL (scm_system_error_key, "system-error");
123 void
124 scm_syserror (subr)
125 char *subr;
126 {
127 scm_error (scm_system_error_key,
128 subr,
129 "%s",
130 scm_listify (scm_makfrom0str (strerror (errno)),
131 SCM_UNDEFINED),
132 scm_listify (SCM_MAKINUM (errno), SCM_UNDEFINED));
133 }
134
135 void
136 scm_syserror_msg (subr, message, args, eno)
137 char *subr;
138 char *message;
139 SCM args;
140 int eno;
141 {
142 scm_error (scm_system_error_key,
143 subr,
144 message,
145 args,
146 scm_listify (SCM_MAKINUM (eno), SCM_UNDEFINED));
147 }
148
149 void
150 scm_sysmissing (subr)
151 char *subr;
152 {
153 #ifdef ENOSYS
154 scm_error (scm_system_error_key,
155 subr,
156 "%s",
157 scm_listify (scm_makfrom0str (strerror (ENOSYS)), SCM_UNDEFINED),
158 scm_listify (SCM_MAKINUM (ENOSYS), SCM_UNDEFINED));
159 #else
160 scm_error (scm_system_error_key,
161 subr,
162 "Missing function",
163 SCM_BOOL_F,
164 scm_listify (SCM_MAKINUM (0), SCM_UNDEFINED));
165 #endif
166 }
167
168 SCM_SYMBOL (scm_num_overflow_key, "numerical-overflow");
169 void
170 scm_num_overflow (subr)
171 char *subr;
172 {
173 scm_error (scm_num_overflow_key,
174 subr,
175 "Numerical overflow",
176 SCM_BOOL_F,
177 SCM_BOOL_F);
178 }
179
180 SCM_SYMBOL (scm_out_of_range_key, "out-of-range");
181 void
182 scm_out_of_range (subr, bad_value)
183 char *subr;
184 SCM bad_value;
185 {
186 scm_error (scm_out_of_range_key,
187 subr,
188 "Argument out of range: %S",
189 scm_listify (bad_value, SCM_UNDEFINED),
190 SCM_BOOL_F);
191 }
192
193 SCM_SYMBOL (scm_args_number_key, "wrong-number-of-args");
194 void
195 scm_wrong_num_args (proc)
196 SCM proc;
197 {
198 scm_error (scm_args_number_key,
199 NULL,
200 "Wrong number of arguments to %s",
201 scm_listify (proc, SCM_UNDEFINED),
202 SCM_BOOL_F);
203 }
204
205 SCM_SYMBOL (scm_arg_type_key, "wrong-type-arg");
206 void
207 scm_wrong_type_arg (subr, pos, bad_value)
208 char *subr;
209 int pos;
210 SCM bad_value;
211 {
212 scm_error (scm_arg_type_key,
213 subr,
214 (pos == 0) ? "Wrong type argument: %S"
215 : "Wrong type argument in position %s: %S",
216 (pos == 0) ? scm_listify (bad_value, SCM_UNDEFINED)
217 : scm_listify (SCM_MAKINUM (pos), bad_value, SCM_UNDEFINED),
218 SCM_BOOL_F);
219 }
220
221 SCM_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
222 void
223 scm_memory_error (subr)
224 char *subr;
225 {
226 scm_error (scm_memory_alloc_key,
227 subr,
228 "Memory allocation error",
229 SCM_BOOL_F,
230 SCM_BOOL_F);
231 }
232
233 SCM_SYMBOL (scm_misc_error_key, "misc-error");
234 void
235 scm_misc_error (subr, message, args)
236 char *subr;
237 char *message;
238 SCM args;
239 {
240 scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
241 }
242
243 /* implements the SCM_ASSERT interface. */
244 SCM
245 scm_wta (arg, pos, s_subr)
246 SCM arg;
247 char *pos;
248 char *s_subr;
249 {
250 if (!s_subr || !*s_subr)
251 s_subr = NULL;
252 if ((~0x1fL) & (long) pos)
253 {
254 /* error string supplied. */
255 scm_misc_error (s_subr, pos, SCM_EOL);
256 }
257 else
258 {
259 /* numerical error code. */
260 int error = (long) pos;
261
262 switch (error)
263 {
264 case SCM_ARGn:
265 scm_wrong_type_arg (s_subr, 0, arg);
266 case SCM_ARG1:
267 scm_wrong_type_arg (s_subr, 1, arg);
268 case SCM_ARG2:
269 scm_wrong_type_arg (s_subr, 2, arg);
270 case SCM_ARG3:
271 scm_wrong_type_arg (s_subr, 3, arg);
272 case SCM_ARG4:
273 scm_wrong_type_arg (s_subr, 4, arg);
274 case SCM_ARG5:
275 scm_wrong_type_arg (s_subr, 5, arg);
276 case SCM_ARG6:
277 scm_wrong_type_arg (s_subr, 6, arg);
278 case SCM_ARG7:
279 scm_wrong_type_arg (s_subr, 7, arg);
280 case SCM_WNA:
281 scm_wrong_num_args (arg);
282 case SCM_OUTOFRANGE:
283 scm_out_of_range (s_subr, arg);
284 case SCM_NALLOC:
285 scm_memory_error (s_subr);
286 default:
287 /* this shouldn't happen. */
288 scm_misc_error (s_subr, "Unknown error", SCM_EOL);
289 }
290 }
291 return SCM_UNSPECIFIED;
292 }
293
294 /* obsolete interface: scm_everr (exp, env, arg, pos, s_subr)
295 was equivalent to scm_wta (arg, pos, s_subr) */
296
297 void
298 scm_init_error ()
299 {
300 #include "cpp_err_symbols.c"
301 #include "error.x"
302 }
303