* validate.h, deprecated.h (SCM_VALIDATE_INUM, SCM_VALIDATE_INUM_COPY,
[bpt/guile.git] / libguile / error.c
CommitLineData
9f40cd87 1/* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
0f2d19dd 7 *
73be1d9e
MV
8 * This library 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 GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd
JB
19\f
20
4600886f
RB
21#if HAVE_CONFIG_H
22# include <config.h>
23#endif
24
0f2d19dd 25#include <stdio.h>
e6e2e95a 26#include <errno.h>
bd9e24b3 27
a0599745
MD
28#include "libguile/_scm.h"
29#include "libguile/pairs.h"
30#include "libguile/strings.h"
31#include "libguile/throw.h"
20e6290e 32
a0599745
MD
33#include "libguile/validate.h"
34#include "libguile/error.h"
0f2d19dd 35
bd9e24b3
GH
36#ifdef HAVE_STRING_H
37#include <string.h>
38#endif
95b88819
GH
39#ifdef HAVE_UNISTD_H
40#include <unistd.h>
41#endif
7beabedb
MG
42
43/* For Windows... */
44#ifdef HAVE_IO_H
45#include <io.h>
46#endif
0f2d19dd
JB
47\f
48
49\f
50/* {Errors and Exceptional Conditions}
51 */
52
0f2d19dd 53
c37e0e55 54/* All errors should pass through here. */
7cb1d4d3 55void
1bbd0b84 56scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
7cb1d4d3
GH
57{
58 SCM arg_list;
406c7d90 59 if (scm_gc_running_p)
ef18914a
MD
60 {
61 /* The error occured during GC --- abort */
62 fprintf (stderr, "Error in %s during GC: %s\n",
63 subr ? subr : "unknown function",
64 message ? message : "<empty message>");
65 abort ();
66 }
1afff620
KN
67 arg_list = scm_list_4 (subr ? scm_makfrom0str (subr) : SCM_BOOL_F,
68 message ? scm_makfrom0str (message) : SCM_BOOL_F,
69 args,
70 rest);
7cb1d4d3
GH
71 scm_ithrow (key, arg_list, 1);
72
73 /* No return, but just in case: */
5c11cc9d
GH
74 {
75 const char msg[] = "guile:scm_error:scm_ithrow returned!\n";
7cb1d4d3 76
5c11cc9d
GH
77 write (2, msg, (sizeof msg) - 1);
78 }
7cb1d4d3
GH
79 exit (1);
80}
0f2d19dd 81
c37e0e55 82/* Scheme interface to scm_error. */
3b3b36dd 83SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0,
1e6808ea
MG
84 (SCM key, SCM subr, SCM message, SCM args, SCM data),
85 "Raise an error with key @var{key}. @var{subr} can be a string\n"
86 "naming the procedure associated with the error, or @code{#f}.\n"
87 "@var{message} is the error message string, possibly containing\n"
88 "@code{~S} and @code{~A} escapes. When an error is reported,\n"
89 "these are replaced by formatting the corresponding members of\n"
90 "@var{args}: @code{~A} (was @code{%s} in older versions of\n"
91 "Guile) formats using @code{display} and @code{~S} (was\n"
92 "@code{%S}) formats using @code{write}. @var{data} is a list or\n"
93 "@code{#f} depending on @var{key}: if @var{key} is\n"
94 "@code{system-error} then it should be a list containing the\n"
95 "Unix @code{errno} value; If @var{key} is @code{signal} then it\n"
96 "should be a list containing the Unix signal number; otherwise\n"
97 "it will usually be @code{#f}.")
1bbd0b84 98#define FUNC_NAME s_scm_error_scm
c37e0e55 99{
1bbd0b84
GB
100 char *szSubr;
101 char *szMessage;
a6d9e5ab
DH
102
103 SCM_VALIDATE_SYMBOL (1, key);
104
7888309b 105 if (scm_is_false (subr))
a6d9e5ab
DH
106 {
107 szSubr = NULL;
108 }
109 else if (SCM_SYMBOLP (subr))
110 {
111 szSubr = SCM_SYMBOL_CHARS (subr);
112 }
113 else
114 {
115 SCM_VALIDATE_STRING (2, subr);
a6d9e5ab
DH
116 szSubr = SCM_STRING_CHARS (subr);
117 }
118
7888309b 119 if (scm_is_false (message))
a6d9e5ab
DH
120 {
121 szMessage = NULL;
122 }
123 else
124 {
125 SCM_VALIDATE_STRING (2, message);
a6d9e5ab
DH
126 szMessage = SCM_STRING_CHARS (message);
127 }
89958ad0 128
1e6808ea 129 scm_error (key, szSubr, szMessage, args, data);
c37e0e55
GH
130 /* not reached. */
131}
1bbd0b84 132#undef FUNC_NAME
c37e0e55 133
b4e15479
SJ
134#ifdef __MINGW32__
135# include "win32-socket.h"
136# define SCM_I_STRERROR(err) \
137 ((err >= WSABASEERR) ? scm_i_socket_strerror (err) : strerror (err))
138# define SCM_I_ERRNO() \
139 (errno ? errno : scm_i_socket_errno ())
140#else
141# define SCM_I_STRERROR(err) strerror (err)
142# define SCM_I_ERRNO() errno
143#endif /* __MINGW32__ */
144
a1ec6916 145SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
1bbd0b84 146 (SCM err),
1e6808ea
MG
147 "Return the Unix error message corresponding to @var{err}, which\n"
148 "must be an integer value.")
1bbd0b84 149#define FUNC_NAME s_scm_strerror
efb997f5 150{
a55c2b68 151 return scm_makfrom0str (SCM_I_STRERROR (scm_to_int (err)));
efb997f5 152}
1bbd0b84 153#undef FUNC_NAME
efb997f5 154
a7f54aed 155SCM_GLOBAL_SYMBOL (scm_system_error_key, "system-error");
52859adf 156void
1bbd0b84 157scm_syserror (const char *subr)
52859adf 158{
b4e15479 159 int save_errno = SCM_I_ERRNO ();
1de052a7 160
01f61221 161 scm_error (scm_system_error_key,
52859adf 162 subr,
70d63753 163 "~A",
b4e15479 164 scm_cons (scm_makfrom0str (SCM_I_STRERROR (save_errno)), SCM_EOL),
7888309b 165 scm_cons (scm_from_int (save_errno), SCM_EOL));
52859adf
GH
166}
167
168void
1bbd0b84 169scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
52859adf 170{
01f61221 171 scm_error (scm_system_error_key,
52859adf
GH
172 subr,
173 message,
174 args,
7888309b 175 scm_cons (scm_from_int (eno), SCM_EOL));
52859adf
GH
176}
177
a7f54aed 178SCM_GLOBAL_SYMBOL (scm_num_overflow_key, "numerical-overflow");
52859adf 179void
1bbd0b84 180scm_num_overflow (const char *subr)
52859adf 181{
01f61221 182 scm_error (scm_num_overflow_key,
52859adf
GH
183 subr,
184 "Numerical overflow",
185 SCM_BOOL_F,
186 SCM_BOOL_F);
187}
188
a7f54aed 189SCM_GLOBAL_SYMBOL (scm_out_of_range_key, "out-of-range");
52859adf 190void
1bbd0b84 191scm_out_of_range (const char *subr, SCM bad_value)
52859adf 192{
01f61221 193 scm_error (scm_out_of_range_key,
52859adf 194 subr,
70d63753 195 "Argument out of range: ~S",
1afff620 196 scm_list_1 (bad_value),
52859adf
GH
197 SCM_BOOL_F);
198}
f5bf2977 199
1e76143f
GB
200void
201scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos)
202{
203 scm_error (scm_out_of_range_key,
204 subr,
70d63753 205 "Argument ~S out of range: ~S",
34d19ef6 206 scm_list_2 (pos, bad_value),
1e76143f
GB
207 SCM_BOOL_F);
208}
209
210
a7f54aed 211SCM_GLOBAL_SYMBOL (scm_args_number_key, "wrong-number-of-args");
0f2d19dd 212void
1bbd0b84 213scm_wrong_num_args (SCM proc)
f5bf2977 214{
01f61221 215 scm_error (scm_args_number_key,
f5bf2977 216 NULL,
70d63753 217 "Wrong number of arguments to ~A",
1afff620 218 scm_list_1 (proc),
f5bf2977
GH
219 SCM_BOOL_F);
220}
221
9f40cd87
DH
222
223void
224scm_error_num_args_subr (const char *subr)
225{
226 scm_error (scm_args_number_key,
227 NULL,
228 "Wrong number of arguments to ~A",
1afff620 229 scm_list_1 (scm_makfrom0str (subr)),
9f40cd87
DH
230 SCM_BOOL_F);
231}
232
233
a7f54aed 234SCM_GLOBAL_SYMBOL (scm_arg_type_key, "wrong-type-arg");
f5bf2977 235void
1bbd0b84 236scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
f5bf2977 237{
01f61221 238 scm_error (scm_arg_type_key,
f5bf2977 239 subr,
70d63753
GB
240 (pos == 0) ? "Wrong type argument: ~S"
241 : "Wrong type argument in position ~A: ~S",
1afff620 242 (pos == 0) ? scm_list_1 (bad_value)
93ccaef0 243 : scm_list_2 (SCM_I_MAKINUM (pos), bad_value),
f5bf2977
GH
244 SCM_BOOL_F);
245}
246
b6791b2e
GB
247void
248scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage)
249{
250 SCM msg = scm_makfrom0str(szMessage);
251 if (pos == 0) {
252 scm_error (scm_arg_type_key,
253 subr, "Wrong type argument (expecting ~A): ~S",
1afff620 254 scm_list_2 (msg, bad_value),
b6791b2e
GB
255 SCM_BOOL_F);
256 } else {
257 scm_error (scm_arg_type_key,
258 subr,
259 "Wrong type argument in position ~A (expecting ~A): ~S",
93ccaef0 260 scm_list_3 (SCM_I_MAKINUM (pos), msg, bad_value),
b6791b2e
GB
261 SCM_BOOL_F);
262 }
263}
264
265
a7f54aed 266SCM_GLOBAL_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
f5bf2977 267void
1bbd0b84 268scm_memory_error (const char *subr)
f5bf2977 269{
01f61221 270 scm_error (scm_memory_alloc_key,
f5bf2977
GH
271 subr,
272 "Memory allocation error",
273 SCM_BOOL_F,
274 SCM_BOOL_F);
275}
276
a7f54aed 277SCM_GLOBAL_SYMBOL (scm_misc_error_key, "misc-error");
523f5266 278void
1bbd0b84 279scm_misc_error (const char *subr, const char *message, SCM args)
523f5266
GH
280{
281 scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
282}
283
0f2d19dd
JB
284void
285scm_init_error ()
0f2d19dd 286{
a0599745
MD
287#include "libguile/cpp_err_symbols.c"
288#include "libguile/error.x"
0f2d19dd
JB
289}
290
89e00824
ML
291
292/*
293 Local Variables:
294 c-file-style: "gnu"
295 End:
296*/