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