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