* list.h (scm_list_1, scm_list_2, scm_list_3, scm_list_4, scm_list_5,
[bpt/guile.git] / libguile / error.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001 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
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include <stdio.h>
48 #include <errno.h>
49
50 #include "libguile/_scm.h"
51 #include "libguile/pairs.h"
52 #include "libguile/strings.h"
53 #include "libguile/throw.h"
54
55 #include "libguile/validate.h"
56 #include "libguile/error.h"
57
58 #ifdef HAVE_STRING_H
59 #include <string.h>
60 #endif
61 #ifdef HAVE_UNISTD_H
62 #include <unistd.h>
63 #endif
64 \f
65
66 \f
67 /* {Errors and Exceptional Conditions}
68 */
69
70
71 /* All errors should pass through here. */
72 void
73 scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
74 {
75 SCM arg_list;
76 if (scm_gc_running_p)
77 {
78 /* The error occured during GC --- abort */
79 fprintf (stderr, "Error in %s during GC: %s\n",
80 subr ? subr : "unknown function",
81 message ? message : "<empty message>");
82 abort ();
83 }
84 arg_list = scm_list_4 (subr ? scm_makfrom0str (subr) : SCM_BOOL_F,
85 message ? scm_makfrom0str (message) : SCM_BOOL_F,
86 args,
87 rest);
88 scm_ithrow (key, arg_list, 1);
89
90 /* No return, but just in case: */
91 {
92 const char msg[] = "guile:scm_error:scm_ithrow returned!\n";
93
94 write (2, msg, (sizeof msg) - 1);
95 }
96 exit (1);
97 }
98
99 /* Scheme interface to scm_error. */
100 SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0,
101 (SCM key, SCM subr, SCM message, SCM args, SCM data),
102 "Raise an error with key @var{key}. @var{subr} can be a string\n"
103 "naming the procedure associated with the error, or @code{#f}.\n"
104 "@var{message} is the error message string, possibly containing\n"
105 "@code{~S} and @code{~A} escapes. When an error is reported,\n"
106 "these are replaced by formatting the corresponding members of\n"
107 "@var{args}: @code{~A} (was @code{%s} in older versions of\n"
108 "Guile) formats using @code{display} and @code{~S} (was\n"
109 "@code{%S}) formats using @code{write}. @var{data} is a list or\n"
110 "@code{#f} depending on @var{key}: if @var{key} is\n"
111 "@code{system-error} then it should be a list containing the\n"
112 "Unix @code{errno} value; If @var{key} is @code{signal} then it\n"
113 "should be a list containing the Unix signal number; otherwise\n"
114 "it will usually be @code{#f}.")
115 #define FUNC_NAME s_scm_error_scm
116 {
117 char *szSubr;
118 char *szMessage;
119
120 SCM_VALIDATE_SYMBOL (1, key);
121
122 if (SCM_FALSEP (subr))
123 {
124 szSubr = NULL;
125 }
126 else if (SCM_SYMBOLP (subr))
127 {
128 szSubr = SCM_SYMBOL_CHARS (subr);
129 }
130 else
131 {
132 SCM_VALIDATE_STRING (2, subr);
133 SCM_STRING_COERCE_0TERMINATION_X (subr);
134 szSubr = SCM_STRING_CHARS (subr);
135 }
136
137 if (SCM_FALSEP (message))
138 {
139 szMessage = NULL;
140 }
141 else
142 {
143 SCM_VALIDATE_STRING (2, message);
144 SCM_STRING_COERCE_0TERMINATION_X (message);
145 szMessage = SCM_STRING_CHARS (message);
146 }
147
148 scm_error (key, szSubr, szMessage, args, data);
149 /* not reached. */
150 }
151 #undef FUNC_NAME
152
153 SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
154 (SCM err),
155 "Return the Unix error message corresponding to @var{err}, which\n"
156 "must be an integer value.")
157 #define FUNC_NAME s_scm_strerror
158 {
159 SCM_VALIDATE_INUM (1,err);
160 return scm_makfrom0str (strerror (SCM_INUM (err)));
161 }
162 #undef FUNC_NAME
163
164 SCM_SYMBOL (scm_system_error_key, "system-error");
165 void
166 scm_syserror (const char *subr)
167 {
168 int save_errno = errno;
169
170 scm_error (scm_system_error_key,
171 subr,
172 "~A",
173 scm_cons (scm_makfrom0str (strerror (save_errno)), SCM_EOL),
174 scm_cons (SCM_MAKINUM (save_errno), SCM_EOL));
175 }
176
177 void
178 scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
179 {
180 scm_error (scm_system_error_key,
181 subr,
182 message,
183 args,
184 scm_cons (SCM_MAKINUM (eno), SCM_EOL));
185 }
186
187 SCM_SYMBOL (scm_num_overflow_key, "numerical-overflow");
188 void
189 scm_num_overflow (const char *subr)
190 {
191 scm_error (scm_num_overflow_key,
192 subr,
193 "Numerical overflow",
194 SCM_BOOL_F,
195 SCM_BOOL_F);
196 }
197
198 SCM_SYMBOL (scm_out_of_range_key, "out-of-range");
199 void
200 scm_out_of_range (const char *subr, SCM bad_value)
201 {
202 scm_error (scm_out_of_range_key,
203 subr,
204 "Argument out of range: ~S",
205 scm_list_1 (bad_value),
206 SCM_BOOL_F);
207 }
208
209 void
210 scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos)
211 {
212 scm_error (scm_out_of_range_key,
213 subr,
214 "Argument ~S out of range: ~S",
215 scm_list_2 (pos,bad_value),
216 SCM_BOOL_F);
217 }
218
219
220 SCM_SYMBOL (scm_args_number_key, "wrong-number-of-args");
221 void
222 scm_wrong_num_args (SCM proc)
223 {
224 scm_error (scm_args_number_key,
225 NULL,
226 "Wrong number of arguments to ~A",
227 scm_list_1 (proc),
228 SCM_BOOL_F);
229 }
230
231
232 void
233 scm_error_num_args_subr (const char *subr)
234 {
235 scm_error (scm_args_number_key,
236 NULL,
237 "Wrong number of arguments to ~A",
238 scm_list_1 (scm_makfrom0str (subr)),
239 SCM_BOOL_F);
240 }
241
242
243 SCM_SYMBOL (scm_arg_type_key, "wrong-type-arg");
244 void
245 scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
246 {
247 scm_error (scm_arg_type_key,
248 subr,
249 (pos == 0) ? "Wrong type argument: ~S"
250 : "Wrong type argument in position ~A: ~S",
251 (pos == 0) ? scm_list_1 (bad_value)
252 : scm_list_2 (SCM_MAKINUM (pos), bad_value),
253 SCM_BOOL_F);
254 }
255
256 void
257 scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage)
258 {
259 SCM msg = scm_makfrom0str(szMessage);
260 if (pos == 0) {
261 scm_error (scm_arg_type_key,
262 subr, "Wrong type argument (expecting ~A): ~S",
263 scm_list_2 (msg, bad_value),
264 SCM_BOOL_F);
265 } else {
266 scm_error (scm_arg_type_key,
267 subr,
268 "Wrong type argument in position ~A (expecting ~A): ~S",
269 scm_list_3 (SCM_MAKINUM (pos), msg, bad_value),
270 SCM_BOOL_F);
271 }
272 }
273
274
275 SCM_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
276 void
277 scm_memory_error (const char *subr)
278 {
279 scm_error (scm_memory_alloc_key,
280 subr,
281 "Memory allocation error",
282 SCM_BOOL_F,
283 SCM_BOOL_F);
284 }
285
286 SCM_SYMBOL (scm_misc_error_key, "misc-error");
287 void
288 scm_misc_error (const char *subr, const char *message, SCM args)
289 {
290 scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
291 }
292
293 #if (SCM_DEBUG_DEPRECATED == 0)
294
295 SCM
296 scm_wta (SCM arg, const char *pos, const char *s_subr)
297 {
298 if (!s_subr || !*s_subr)
299 s_subr = NULL;
300 if ((~0x1fL) & (long) pos)
301 {
302 /* error string supplied. */
303 scm_misc_error (s_subr, pos, scm_list_1 (arg));
304 }
305 else
306 {
307 /* numerical error code. */
308 int error = (int) pos;
309
310 switch (error)
311 {
312 case SCM_ARGn:
313 scm_wrong_type_arg (s_subr, 0, arg);
314 case SCM_ARG1:
315 scm_wrong_type_arg (s_subr, 1, arg);
316 case SCM_ARG2:
317 scm_wrong_type_arg (s_subr, 2, arg);
318 case SCM_ARG3:
319 scm_wrong_type_arg (s_subr, 3, arg);
320 case SCM_ARG4:
321 scm_wrong_type_arg (s_subr, 4, arg);
322 case SCM_ARG5:
323 scm_wrong_type_arg (s_subr, 5, arg);
324 case SCM_ARG6:
325 scm_wrong_type_arg (s_subr, 6, arg);
326 case SCM_ARG7:
327 scm_wrong_type_arg (s_subr, 7, arg);
328 case SCM_WNA:
329 scm_wrong_num_args (arg);
330 case SCM_OUTOFRANGE:
331 scm_out_of_range (s_subr, arg);
332 case SCM_NALLOC:
333 scm_memory_error (s_subr);
334 default:
335 /* this shouldn't happen. */
336 scm_misc_error (s_subr, "Unknown error", SCM_EOL);
337 }
338 }
339 return SCM_UNSPECIFIED;
340 }
341
342 #endif /* SCM_DEBUG_DEPRECATED == 0 */
343
344 void
345 scm_init_error ()
346 {
347 #include "libguile/cpp_err_symbols.c"
348 #ifndef SCM_MAGIC_SNARFER
349 #include "libguile/error.x"
350 #endif
351 }
352
353
354 /*
355 Local Variables:
356 c-file-style: "gnu"
357 End:
358 */