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