* configure.in: check for hstrerror.
[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. */
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
c37e0e55 63/* All errors should pass through here. */
7cb1d4d3
GH
64void
65scm_error (key, subr, message, args, rest)
66 SCM key;
3eeba8d4
JB
67 const char *subr;
68 const char *message;
7cb1d4d3
GH
69 SCM args;
70 SCM rest;
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
GH
89/* Scheme interface to scm_error. */
90SCM_PROC(s_error_scm, "scm-error", 5, 0, 0, scm_error_scm);
91SCM
92scm_error_scm (key, subr, message, args, rest)
93 SCM key;
94 SCM subr;
95 SCM message;
96 SCM args;
97 SCM rest;
98{
99 SCM_ASSERT (SCM_NIMP (key) && SCM_SYMBOLP (key), key, SCM_ARG1, s_error_scm);
100 SCM_ASSERT (SCM_FALSEP (subr) || (SCM_NIMP (subr) && SCM_ROSTRINGP (subr)),
101 subr, SCM_ARG2, s_error_scm);
102 SCM_ASSERT (SCM_FALSEP (message)
103 || (SCM_NIMP (message) && SCM_ROSTRINGP (message)),
104 message, SCM_ARG3, s_error_scm);
105
89958ad0
JB
106 SCM_COERCE_SUBSTR (message);
107
c37e0e55
GH
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
efb997f5
GH
116SCM_PROC (s_strerror, "strerror", 1, 0, 0, scm_strerror);
117SCM
118scm_strerror (SCM err)
119{
120 SCM_ASSERT (SCM_INUMP (err), err, SCM_ARG1, s_strerror);
121 return scm_makfrom0str (strerror (SCM_INUM (err)));
122}
123
523f5266 124SCM_SYMBOL (scm_system_error_key, "system-error");
52859adf
GH
125void
126scm_syserror (subr)
3eeba8d4 127 const char *subr;
52859adf 128{
01f61221 129 scm_error (scm_system_error_key,
52859adf 130 subr,
f5bf2977 131 "%s",
5c11cc9d
GH
132 scm_cons (scm_makfrom0str (strerror (errno)), SCM_EOL),
133 scm_cons (SCM_MAKINUM (errno), SCM_EOL));
52859adf
GH
134}
135
136void
3d8d56df 137scm_syserror_msg (subr, message, args, eno)
3eeba8d4
JB
138 const char *subr;
139 const char *message;
52859adf 140 SCM args;
3d8d56df 141 int eno;
52859adf 142{
01f61221 143 scm_error (scm_system_error_key,
52859adf
GH
144 subr,
145 message,
146 args,
5c11cc9d 147 scm_cons (SCM_MAKINUM (eno), SCM_EOL));
52859adf
GH
148}
149
150void
151scm_sysmissing (subr)
3eeba8d4 152 const char *subr;
52859adf
GH
153{
154#ifdef ENOSYS
01f61221 155 scm_error (scm_system_error_key,
52859adf 156 subr,
f5bf2977 157 "%s",
5c11cc9d
GH
158 scm_cons (scm_makfrom0str (strerror (ENOSYS)), SCM_EOL),
159 scm_cons (SCM_MAKINUM (ENOSYS), SCM_EOL));
52859adf 160#else
01f61221 161 scm_error (scm_system_error_key,
52859adf
GH
162 subr,
163 "Missing function",
164 SCM_BOOL_F,
5c11cc9d 165 scm_cons (SCM_MAKINUM (0), SCM_EOL));
52859adf
GH
166#endif
167}
168
523f5266 169SCM_SYMBOL (scm_num_overflow_key, "numerical-overflow");
52859adf
GH
170void
171scm_num_overflow (subr)
3eeba8d4 172 const char *subr;
52859adf 173{
01f61221 174 scm_error (scm_num_overflow_key,
52859adf
GH
175 subr,
176 "Numerical overflow",
177 SCM_BOOL_F,
178 SCM_BOOL_F);
179}
180
523f5266 181SCM_SYMBOL (scm_out_of_range_key, "out-of-range");
52859adf
GH
182void
183scm_out_of_range (subr, bad_value)
3eeba8d4 184 const char *subr;
52859adf
GH
185 SCM bad_value;
186{
01f61221 187 scm_error (scm_out_of_range_key,
52859adf
GH
188 subr,
189 "Argument out of range: %S",
5c11cc9d 190 scm_cons (bad_value, SCM_EOL),
52859adf
GH
191 SCM_BOOL_F);
192}
f5bf2977 193
523f5266 194SCM_SYMBOL (scm_args_number_key, "wrong-number-of-args");
0f2d19dd 195void
f5bf2977
GH
196scm_wrong_num_args (proc)
197 SCM proc;
198{
01f61221 199 scm_error (scm_args_number_key,
f5bf2977
GH
200 NULL,
201 "Wrong number of arguments to %s",
5c11cc9d 202 scm_cons (proc, SCM_EOL),
f5bf2977
GH
203 SCM_BOOL_F);
204}
205
523f5266 206SCM_SYMBOL (scm_arg_type_key, "wrong-type-arg");
f5bf2977
GH
207void
208scm_wrong_type_arg (subr, pos, bad_value)
3eeba8d4 209 const char *subr;
f5bf2977
GH
210 int pos;
211 SCM bad_value;
212{
01f61221 213 scm_error (scm_arg_type_key,
f5bf2977
GH
214 subr,
215 (pos == 0) ? "Wrong type argument: %S"
216 : "Wrong type argument in position %s: %S",
5c11cc9d
GH
217 (pos == 0) ? scm_cons (bad_value, SCM_EOL)
218 : scm_cons (SCM_MAKINUM (pos), scm_cons (bad_value, SCM_EOL)),
f5bf2977
GH
219 SCM_BOOL_F);
220}
221
523f5266 222SCM_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
f5bf2977
GH
223void
224scm_memory_error (subr)
3eeba8d4 225 const char *subr;
f5bf2977 226{
01f61221 227 scm_error (scm_memory_alloc_key,
f5bf2977
GH
228 subr,
229 "Memory allocation error",
230 SCM_BOOL_F,
231 SCM_BOOL_F);
232}
233
523f5266
GH
234SCM_SYMBOL (scm_misc_error_key, "misc-error");
235void
236scm_misc_error (subr, message, args)
3eeba8d4
JB
237 const char *subr;
238 const char *message;
523f5266
GH
239 SCM args;
240{
241 scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
242}
243
f5bf2977
GH
244/* implements the SCM_ASSERT interface. */
245SCM
246scm_wta (arg, pos, s_subr)
247 SCM arg;
3eeba8d4
JB
248 const char *pos;
249 const char *s_subr;
f5bf2977
GH
250{
251 if (!s_subr || !*s_subr)
252 s_subr = NULL;
253 if ((~0x1fL) & (long) pos)
254 {
255 /* error string supplied. */
26a4f923 256 scm_misc_error (s_subr, pos, SCM_LIST1 (arg));
f5bf2977
GH
257 }
258 else
259 {
260 /* numerical error code. */
261 int error = (long) pos;
262
263 switch (error)
264 {
265 case SCM_ARGn:
266 scm_wrong_type_arg (s_subr, 0, arg);
267 case SCM_ARG1:
268 scm_wrong_type_arg (s_subr, 1, arg);
269 case SCM_ARG2:
270 scm_wrong_type_arg (s_subr, 2, arg);
271 case SCM_ARG3:
272 scm_wrong_type_arg (s_subr, 3, arg);
273 case SCM_ARG4:
274 scm_wrong_type_arg (s_subr, 4, arg);
275 case SCM_ARG5:
276 scm_wrong_type_arg (s_subr, 5, arg);
3d9352fb
MD
277 case SCM_ARG6:
278 scm_wrong_type_arg (s_subr, 6, arg);
279 case SCM_ARG7:
280 scm_wrong_type_arg (s_subr, 7, arg);
f5bf2977
GH
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
0f2d19dd
JB
295void
296scm_init_error ()
0f2d19dd 297{
67ec3667 298#include "cpp_err_symbols.c"
0f2d19dd
JB
299#include "error.x"
300}
301