e18db9e82fce06d8036758a2d7606d3b54ca0e2b
[bpt/guile.git] / libguile / error.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2004, 2006 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18
19 \f
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <errno.h>
27
28 #include "libguile/_scm.h"
29 #include "libguile/dynwind.h"
30 #include "libguile/pairs.h"
31 #include "libguile/strings.h"
32 #include "libguile/throw.h"
33
34 #include "libguile/validate.h"
35 #include "libguile/error.h"
36
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
44 /* For Windows... */
45 #ifdef HAVE_IO_H
46 #include <io.h>
47 #endif
48 \f
49
50 /* {Errors and Exceptional Conditions}
51 */
52
53
54 /* Scheme interface to scm_error_scm. */
55 void
56 scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
57 {
58 scm_error_scm
59 (key,
60 (subr == NULL) ? SCM_BOOL_F : scm_from_locale_string (subr),
61 (message == NULL) ? SCM_BOOL_F : scm_from_locale_string (message),
62 args, rest);
63 }
64
65 /* All errors should pass through here. */
66 SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0,
67 (SCM key, SCM subr, SCM message, SCM args, SCM data),
68 "Raise an error with key @var{key}. @var{subr} can be a string\n"
69 "naming the procedure associated with the error, or @code{#f}.\n"
70 "@var{message} is the error message string, possibly containing\n"
71 "@code{~S} and @code{~A} escapes. When an error is reported,\n"
72 "these are replaced by formatting the corresponding members of\n"
73 "@var{args}: @code{~A} (was @code{%s} in older versions of\n"
74 "Guile) formats using @code{display} and @code{~S} (was\n"
75 "@code{%S}) formats using @code{write}. @var{data} is a list or\n"
76 "@code{#f} depending on @var{key}: if @var{key} is\n"
77 "@code{system-error} then it should be a list containing the\n"
78 "Unix @code{errno} value; If @var{key} is @code{signal} then it\n"
79 "should be a list containing the Unix signal number; If\n"
80 "@var{key} is @code{out-of-range} or @code{wrong-type-arg},\n"
81 "it is a list containing the bad value; otherwise\n"
82 "it will usually be @code{#f}.")
83 #define FUNC_NAME s_scm_error_scm
84 {
85 if (scm_gc_running_p)
86 {
87 /* The error occured during GC --- abort */
88 fprintf (stderr, "Guile: error during GC.\n"),
89 abort ();
90 }
91
92 scm_ithrow (key, scm_list_4 (subr, message, args, data), 1);
93
94 /* No return, but just in case: */
95 fprintf (stderr, "Guile scm_ithrow returned!\n");
96 exit (1);
97 }
98 #undef FUNC_NAME
99
100 #ifdef __MINGW32__
101 # include "win32-socket.h"
102 # define SCM_I_STRERROR(err) \
103 ((err >= WSABASEERR) ? scm_i_socket_strerror (err) : strerror (err))
104 # define SCM_I_ERRNO() \
105 (errno ? errno : scm_i_socket_errno ())
106 #else
107 # define SCM_I_STRERROR(err) strerror (err)
108 # define SCM_I_ERRNO() errno
109 #endif /* __MINGW32__ */
110
111 /* strerror may not be thread safe, for instance in glibc (version 2.3.2) an
112 error number not among the known values results in a string like "Unknown
113 error 9999" formed in a static buffer, which will be overwritten by a
114 similar call in another thread. A test program running two threads with
115 different unknown error numbers can trip this fairly quickly.
116
117 Some systems don't do what glibc does, instead just giving a single
118 "Unknown error" for unrecognised numbers. It doesn't seem worth trying
119 to tell if that's the case, a mutex is reasonably fast, and strerror
120 isn't needed very often.
121
122 strerror_r (when available) could be used, it might be a touch faster
123 than a frame and a mutex, though there's probably not much
124 difference. */
125
126 SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
127 (SCM err),
128 "Return the Unix error message corresponding to @var{err}, which\n"
129 "must be an integer value.")
130 #define FUNC_NAME s_scm_strerror
131 {
132 SCM ret;
133 scm_dynwind_begin (0);
134 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
135
136 ret = scm_from_locale_string (SCM_I_STRERROR (scm_to_int (err)));
137
138 scm_dynwind_end ();
139 return ret;
140 }
141 #undef FUNC_NAME
142
143 SCM_GLOBAL_SYMBOL (scm_system_error_key, "system-error");
144 void
145 scm_syserror (const char *subr)
146 {
147 SCM err = scm_from_int (SCM_I_ERRNO ());
148 scm_error (scm_system_error_key,
149 subr,
150 "~A",
151 scm_cons (scm_strerror (err), SCM_EOL),
152 scm_cons (err, SCM_EOL));
153 }
154
155 void
156 scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
157 {
158 scm_error (scm_system_error_key,
159 subr,
160 message,
161 args,
162 scm_cons (scm_from_int (eno), SCM_EOL));
163 }
164
165 SCM_GLOBAL_SYMBOL (scm_num_overflow_key, "numerical-overflow");
166 void
167 scm_num_overflow (const char *subr)
168 {
169 scm_error (scm_num_overflow_key,
170 subr,
171 "Numerical overflow",
172 SCM_BOOL_F,
173 SCM_BOOL_F);
174 }
175
176 SCM_GLOBAL_SYMBOL (scm_out_of_range_key, "out-of-range");
177 void
178 scm_out_of_range (const char *subr, SCM bad_value)
179 {
180 scm_error (scm_out_of_range_key,
181 subr,
182 "Value out of range: ~S",
183 scm_list_1 (bad_value),
184 scm_list_1 (bad_value));
185 }
186
187 void
188 scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos)
189 {
190 scm_error (scm_out_of_range_key,
191 subr,
192 "Argument ~A out of range: ~S",
193 scm_list_2 (pos, bad_value),
194 scm_list_1 (bad_value));
195 }
196
197
198 SCM_GLOBAL_SYMBOL (scm_args_number_key, "wrong-number-of-args");
199 void
200 scm_wrong_num_args (SCM proc)
201 {
202 scm_error (scm_args_number_key,
203 NULL,
204 "Wrong number of arguments to ~A",
205 scm_list_1 (proc),
206 SCM_BOOL_F);
207 }
208
209
210 void
211 scm_error_num_args_subr (const char *subr)
212 {
213 scm_error (scm_args_number_key,
214 NULL,
215 "Wrong number of arguments to ~A",
216 scm_list_1 (scm_from_locale_string (subr)),
217 SCM_BOOL_F);
218 }
219
220
221 SCM_GLOBAL_SYMBOL (scm_arg_type_key, "wrong-type-arg");
222 void
223 scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
224 {
225 scm_error (scm_arg_type_key,
226 subr,
227 (pos == 0) ? "Wrong type: ~S"
228 : "Wrong type argument in position ~A: ~S",
229 (pos == 0) ? scm_list_1 (bad_value)
230 : scm_list_2 (scm_from_int (pos), bad_value),
231 scm_list_1 (bad_value));
232 }
233
234 void
235 scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage)
236 {
237 SCM msg = scm_from_locale_string (szMessage);
238 if (pos == 0)
239 {
240 scm_error (scm_arg_type_key,
241 subr, "Wrong type (expecting ~A): ~S",
242 scm_list_2 (msg, bad_value),
243 scm_list_1 (bad_value));
244 }
245 else
246 {
247 scm_error (scm_arg_type_key,
248 subr,
249 "Wrong type argument in position ~A (expecting ~A): ~S",
250 scm_list_3 (scm_from_int (pos), msg, bad_value),
251 scm_list_1 (bad_value));
252 }
253 }
254
255
256 SCM_GLOBAL_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
257 void
258 scm_memory_error (const char *subr)
259 {
260 fprintf (stderr, "FATAL: memory error in %s\n", subr);
261 abort ();
262 }
263
264 SCM_GLOBAL_SYMBOL (scm_misc_error_key, "misc-error");
265 void
266 scm_misc_error (const char *subr, const char *message, SCM args)
267 {
268 scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
269 }
270
271 void
272 scm_init_error ()
273 {
274 #include "libguile/cpp_err_symbols.c"
275 #include "libguile/error.x"
276 }
277
278
279 /*
280 Local Variables:
281 c-file-style: "gnu"
282 End:
283 */