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