* errno.h: prototype for scm_strerror.
[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
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. */
0f2d19dd
JB
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
89958ad0
JB
87 write (2, "unhandled system error\n",
88 sizeof ("unhandled system error\n") - 1);
7cb1d4d3
GH
89 exit (1);
90}
0f2d19dd 91
c37e0e55
GH
92/* Scheme interface to scm_error. */
93SCM_PROC(s_error_scm, "scm-error", 5, 0, 0, scm_error_scm);
94SCM
95scm_error_scm (key, subr, message, args, rest)
96 SCM key;
97 SCM subr;
98 SCM message;
99 SCM args;
100 SCM rest;
101{
102 SCM_ASSERT (SCM_NIMP (key) && SCM_SYMBOLP (key), key, SCM_ARG1, s_error_scm);
103 SCM_ASSERT (SCM_FALSEP (subr) || (SCM_NIMP (subr) && SCM_ROSTRINGP (subr)),
104 subr, SCM_ARG2, s_error_scm);
105 SCM_ASSERT (SCM_FALSEP (message)
106 || (SCM_NIMP (message) && SCM_ROSTRINGP (message)),
107 message, SCM_ARG3, s_error_scm);
108
89958ad0
JB
109 SCM_COERCE_SUBSTR (message);
110
c37e0e55
GH
111 scm_error (key,
112 (SCM_FALSEP (subr)) ? NULL : SCM_ROCHARS (subr),
113 (SCM_FALSEP (message)) ? NULL : SCM_ROCHARS (message),
114 args,
115 rest);
116 /* not reached. */
117}
118
efb997f5
GH
119SCM_PROC (s_strerror, "strerror", 1, 0, 0, scm_strerror);
120SCM
121scm_strerror (SCM err)
122{
123 SCM_ASSERT (SCM_INUMP (err), err, SCM_ARG1, s_strerror);
124 return scm_makfrom0str (strerror (SCM_INUM (err)));
125}
126
523f5266 127SCM_SYMBOL (scm_system_error_key, "system-error");
52859adf
GH
128void
129scm_syserror (subr)
130 char *subr;
131{
01f61221 132 scm_error (scm_system_error_key,
52859adf 133 subr,
f5bf2977 134 "%s",
52859adf
GH
135 scm_listify (scm_makfrom0str (strerror (errno)),
136 SCM_UNDEFINED),
137 scm_listify (SCM_MAKINUM (errno), SCM_UNDEFINED));
138}
139
140void
3d8d56df 141scm_syserror_msg (subr, message, args, eno)
52859adf
GH
142 char *subr;
143 char *message;
144 SCM args;
3d8d56df 145 int eno;
52859adf 146{
01f61221 147 scm_error (scm_system_error_key,
52859adf
GH
148 subr,
149 message,
150 args,
3d8d56df 151 scm_listify (SCM_MAKINUM (eno), SCM_UNDEFINED));
52859adf
GH
152}
153
154void
155scm_sysmissing (subr)
156 char *subr;
157{
158#ifdef ENOSYS
01f61221 159 scm_error (scm_system_error_key,
52859adf 160 subr,
f5bf2977 161 "%s",
52859adf
GH
162 scm_listify (scm_makfrom0str (strerror (ENOSYS)), SCM_UNDEFINED),
163 scm_listify (SCM_MAKINUM (ENOSYS), SCM_UNDEFINED));
164#else
01f61221 165 scm_error (scm_system_error_key,
52859adf
GH
166 subr,
167 "Missing function",
168 SCM_BOOL_F,
169 scm_listify (SCM_MAKINUM (0), SCM_UNDEFINED));
170#endif
171}
172
523f5266 173SCM_SYMBOL (scm_num_overflow_key, "numerical-overflow");
52859adf
GH
174void
175scm_num_overflow (subr)
176 char *subr;
177{
01f61221 178 scm_error (scm_num_overflow_key,
52859adf
GH
179 subr,
180 "Numerical overflow",
181 SCM_BOOL_F,
182 SCM_BOOL_F);
183}
184
523f5266 185SCM_SYMBOL (scm_out_of_range_key, "out-of-range");
52859adf
GH
186void
187scm_out_of_range (subr, bad_value)
188 char *subr;
189 SCM bad_value;
190{
01f61221 191 scm_error (scm_out_of_range_key,
52859adf
GH
192 subr,
193 "Argument out of range: %S",
194 scm_listify (bad_value, SCM_UNDEFINED),
195 SCM_BOOL_F);
196}
f5bf2977 197
523f5266 198SCM_SYMBOL (scm_args_number_key, "wrong-number-of-args");
0f2d19dd 199void
f5bf2977
GH
200scm_wrong_num_args (proc)
201 SCM proc;
202{
01f61221 203 scm_error (scm_args_number_key,
f5bf2977
GH
204 NULL,
205 "Wrong number of arguments to %s",
206 scm_listify (proc, SCM_UNDEFINED),
207 SCM_BOOL_F);
208}
209
523f5266 210SCM_SYMBOL (scm_arg_type_key, "wrong-type-arg");
f5bf2977
GH
211void
212scm_wrong_type_arg (subr, pos, bad_value)
213 char *subr;
214 int pos;
215 SCM bad_value;
216{
01f61221 217 scm_error (scm_arg_type_key,
f5bf2977
GH
218 subr,
219 (pos == 0) ? "Wrong type argument: %S"
220 : "Wrong type argument in position %s: %S",
221 (pos == 0) ? scm_listify (bad_value, SCM_UNDEFINED)
222 : scm_listify (SCM_MAKINUM (pos), bad_value, SCM_UNDEFINED),
223 SCM_BOOL_F);
224}
225
523f5266 226SCM_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
f5bf2977
GH
227void
228scm_memory_error (subr)
229 char *subr;
230{
01f61221 231 scm_error (scm_memory_alloc_key,
f5bf2977
GH
232 subr,
233 "Memory allocation error",
234 SCM_BOOL_F,
235 SCM_BOOL_F);
236}
237
523f5266
GH
238SCM_SYMBOL (scm_misc_error_key, "misc-error");
239void
240scm_misc_error (subr, message, args)
241 char *subr;
242 char *message;
243 SCM args;
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
250scm_wta (arg, pos, s_subr)
251 SCM arg;
252 char *pos;
253 char *s_subr;
254{
255 if (!s_subr || !*s_subr)
256 s_subr = NULL;
257 if ((~0x1fL) & (long) pos)
258 {
259 /* error string supplied. */
b1edcd36 260 scm_misc_error (s_subr, pos, SCM_EOL);
f5bf2977
GH
261 }
262 else
263 {
264 /* numerical error code. */
265 int error = (long) pos;
266
267 switch (error)
268 {
269 case SCM_ARGn:
270 scm_wrong_type_arg (s_subr, 0, arg);
271 case SCM_ARG1:
272 scm_wrong_type_arg (s_subr, 1, arg);
273 case SCM_ARG2:
274 scm_wrong_type_arg (s_subr, 2, arg);
275 case SCM_ARG3:
276 scm_wrong_type_arg (s_subr, 3, arg);
277 case SCM_ARG4:
278 scm_wrong_type_arg (s_subr, 4, arg);
279 case SCM_ARG5:
280 scm_wrong_type_arg (s_subr, 5, arg);
281 case SCM_WNA:
282 scm_wrong_num_args (arg);
283 case SCM_OUTOFRANGE:
284 scm_out_of_range (s_subr, arg);
285 case SCM_NALLOC:
286 scm_memory_error (s_subr);
287 default:
288 /* this shouldn't happen. */
b1edcd36 289 scm_misc_error (s_subr, "Unknown error", SCM_EOL);
f5bf2977
GH
290 }
291 }
292 return SCM_UNSPECIFIED;
293}
294
295/* obsolete interface: scm_everr (exp, env, arg, pos, s_subr)
296 was equivalent to scm_wta (arg, pos, s_subr) */
297
0f2d19dd
JB
298void
299scm_init_error ()
0f2d19dd 300{
67ec3667 301#include "cpp_err_symbols.c"
0f2d19dd
JB
302#include "error.x"
303}
304