* *.c: Pervasive software-engineering-motivated rewrite of
[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>
48#include "_scm.h"
20e6290e
JB
49#include "pairs.h"
50#include "genio.h"
51#include "throw.h"
52
1bbd0b84 53#include "scm_validate.h"
20e6290e 54#include "error.h"
0f2d19dd 55
95b88819
GH
56#ifdef HAVE_UNISTD_H
57#include <unistd.h>
58#endif
0f2d19dd
JB
59\f
60
61\f
62/* {Errors and Exceptional Conditions}
63 */
64
0f2d19dd 65
0f2d19dd 66extern int errno;
f5bf2977 67
c37e0e55 68/* All errors should pass through here. */
7cb1d4d3 69void
1bbd0b84 70scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
7cb1d4d3
GH
71{
72 SCM arg_list;
f5bf2977 73 arg_list = scm_listify (subr ? scm_makfrom0str (subr) : SCM_BOOL_F,
b59b97ba 74 message ? scm_makfrom0str (message) : SCM_BOOL_F,
7cb1d4d3
GH
75 args,
76 rest,
77 SCM_UNDEFINED);
78 scm_ithrow (key, arg_list, 1);
79
80 /* No return, but just in case: */
5c11cc9d
GH
81 {
82 const char msg[] = "guile:scm_error:scm_ithrow returned!\n";
7cb1d4d3 83
5c11cc9d
GH
84 write (2, msg, (sizeof msg) - 1);
85 }
7cb1d4d3
GH
86 exit (1);
87}
0f2d19dd 88
c37e0e55 89/* Scheme interface to scm_error. */
1bbd0b84
GB
90GUILE_PROC(scm_error_scm, "scm-error", 5, 0, 0,
91 (SCM key, SCM subr, SCM message, SCM args, SCM rest),
92"")
93#define FUNC_NAME s_scm_error_scm
c37e0e55 94{
1bbd0b84
GB
95 char *szSubr;
96 char *szMessage;
97 SCM_VALIDATE_SYMBOL(1,key);
98 SCM_VALIDATE_NULLORROSTRING_COPY(2,subr,szSubr);
99 SCM_VALIDATE_NULLORROSTRING_COPY(3,message,szMessage);
89958ad0
JB
100 SCM_COERCE_SUBSTR (message);
101
1bbd0b84 102 scm_error (key, szSubr, szMessage, args, rest);
c37e0e55
GH
103 /* not reached. */
104}
1bbd0b84 105#undef FUNC_NAME
c37e0e55 106
1bbd0b84
GB
107GUILE_PROC (scm_strerror, "strerror", 1, 0, 0,
108 (SCM err),
109"")
110#define FUNC_NAME s_scm_strerror
efb997f5 111{
1bbd0b84 112 SCM_VALIDATE_INT(1,err);
efb997f5
GH
113 return scm_makfrom0str (strerror (SCM_INUM (err)));
114}
1bbd0b84 115#undef FUNC_NAME
efb997f5 116
523f5266 117SCM_SYMBOL (scm_system_error_key, "system-error");
52859adf 118void
1bbd0b84 119scm_syserror (const char *subr)
52859adf 120{
01f61221 121 scm_error (scm_system_error_key,
52859adf 122 subr,
f5bf2977 123 "%s",
5c11cc9d
GH
124 scm_cons (scm_makfrom0str (strerror (errno)), SCM_EOL),
125 scm_cons (SCM_MAKINUM (errno), SCM_EOL));
52859adf
GH
126}
127
128void
1bbd0b84 129scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
52859adf 130{
01f61221 131 scm_error (scm_system_error_key,
52859adf
GH
132 subr,
133 message,
134 args,
5c11cc9d 135 scm_cons (SCM_MAKINUM (eno), SCM_EOL));
52859adf
GH
136}
137
138void
1bbd0b84 139scm_sysmissing (const char *subr)
52859adf
GH
140{
141#ifdef ENOSYS
01f61221 142 scm_error (scm_system_error_key,
52859adf 143 subr,
f5bf2977 144 "%s",
5c11cc9d
GH
145 scm_cons (scm_makfrom0str (strerror (ENOSYS)), SCM_EOL),
146 scm_cons (SCM_MAKINUM (ENOSYS), SCM_EOL));
52859adf 147#else
01f61221 148 scm_error (scm_system_error_key,
52859adf
GH
149 subr,
150 "Missing function",
151 SCM_BOOL_F,
5c11cc9d 152 scm_cons (SCM_MAKINUM (0), SCM_EOL));
52859adf
GH
153#endif
154}
155
523f5266 156SCM_SYMBOL (scm_num_overflow_key, "numerical-overflow");
52859adf 157void
1bbd0b84 158scm_num_overflow (const char *subr)
52859adf 159{
01f61221 160 scm_error (scm_num_overflow_key,
52859adf
GH
161 subr,
162 "Numerical overflow",
163 SCM_BOOL_F,
164 SCM_BOOL_F);
165}
166
523f5266 167SCM_SYMBOL (scm_out_of_range_key, "out-of-range");
52859adf 168void
1bbd0b84 169scm_out_of_range (const char *subr, SCM bad_value)
52859adf 170{
01f61221 171 scm_error (scm_out_of_range_key,
52859adf
GH
172 subr,
173 "Argument out of range: %S",
5c11cc9d 174 scm_cons (bad_value, SCM_EOL),
52859adf
GH
175 SCM_BOOL_F);
176}
f5bf2977 177
523f5266 178SCM_SYMBOL (scm_args_number_key, "wrong-number-of-args");
0f2d19dd 179void
1bbd0b84 180scm_wrong_num_args (SCM proc)
f5bf2977 181{
01f61221 182 scm_error (scm_args_number_key,
f5bf2977
GH
183 NULL,
184 "Wrong number of arguments to %s",
5c11cc9d 185 scm_cons (proc, SCM_EOL),
f5bf2977
GH
186 SCM_BOOL_F);
187}
188
523f5266 189SCM_SYMBOL (scm_arg_type_key, "wrong-type-arg");
f5bf2977 190void
1bbd0b84 191scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
f5bf2977 192{
01f61221 193 scm_error (scm_arg_type_key,
f5bf2977
GH
194 subr,
195 (pos == 0) ? "Wrong type argument: %S"
196 : "Wrong type argument in position %s: %S",
5c11cc9d
GH
197 (pos == 0) ? scm_cons (bad_value, SCM_EOL)
198 : scm_cons (SCM_MAKINUM (pos), scm_cons (bad_value, SCM_EOL)),
f5bf2977
GH
199 SCM_BOOL_F);
200}
201
523f5266 202SCM_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
f5bf2977 203void
1bbd0b84 204scm_memory_error (const char *subr)
f5bf2977 205{
01f61221 206 scm_error (scm_memory_alloc_key,
f5bf2977
GH
207 subr,
208 "Memory allocation error",
209 SCM_BOOL_F,
210 SCM_BOOL_F);
211}
212
523f5266
GH
213SCM_SYMBOL (scm_misc_error_key, "misc-error");
214void
1bbd0b84 215scm_misc_error (const char *subr, const char *message, SCM args)
523f5266
GH
216{
217 scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
218}
219
f5bf2977
GH
220/* implements the SCM_ASSERT interface. */
221SCM
1bbd0b84 222scm_wta (SCM arg, const char *pos, const char *s_subr)
f5bf2977
GH
223{
224 if (!s_subr || !*s_subr)
225 s_subr = NULL;
226 if ((~0x1fL) & (long) pos)
227 {
228 /* error string supplied. */
26a4f923 229 scm_misc_error (s_subr, pos, SCM_LIST1 (arg));
f5bf2977
GH
230 }
231 else
232 {
233 /* numerical error code. */
234 int error = (long) pos;
235
236 switch (error)
237 {
238 case SCM_ARGn:
239 scm_wrong_type_arg (s_subr, 0, arg);
240 case SCM_ARG1:
241 scm_wrong_type_arg (s_subr, 1, arg);
242 case SCM_ARG2:
243 scm_wrong_type_arg (s_subr, 2, arg);
244 case SCM_ARG3:
245 scm_wrong_type_arg (s_subr, 3, arg);
246 case SCM_ARG4:
247 scm_wrong_type_arg (s_subr, 4, arg);
248 case SCM_ARG5:
249 scm_wrong_type_arg (s_subr, 5, arg);
3d9352fb
MD
250 case SCM_ARG6:
251 scm_wrong_type_arg (s_subr, 6, arg);
252 case SCM_ARG7:
253 scm_wrong_type_arg (s_subr, 7, arg);
f5bf2977
GH
254 case SCM_WNA:
255 scm_wrong_num_args (arg);
256 case SCM_OUTOFRANGE:
257 scm_out_of_range (s_subr, arg);
258 case SCM_NALLOC:
259 scm_memory_error (s_subr);
260 default:
261 /* this shouldn't happen. */
b1edcd36 262 scm_misc_error (s_subr, "Unknown error", SCM_EOL);
f5bf2977
GH
263 }
264 }
265 return SCM_UNSPECIFIED;
266}
267
0f2d19dd
JB
268void
269scm_init_error ()
0f2d19dd 270{
67ec3667 271#include "cpp_err_symbols.c"
0f2d19dd
JB
272#include "error.x"
273}
274