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