Reverted changed from 2005/01/24 19:14:54, which was a commit to the
[bpt/guile.git] / libguile / error.c
CommitLineData
4af3c6f1 1/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2004 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 28#include "libguile/_scm.h"
4af3c6f1 29#include "libguile/dynwind.h"
a0599745
MD
30#include "libguile/pairs.h"
31#include "libguile/strings.h"
32#include "libguile/throw.h"
20e6290e 33
a0599745
MD
34#include "libguile/validate.h"
35#include "libguile/error.h"
0f2d19dd 36
bd9e24b3
GH
37#ifdef HAVE_STRING_H
38#include <string.h>
39#endif
95b88819
GH
40#ifdef HAVE_UNISTD_H
41#include <unistd.h>
42#endif
7beabedb
MG
43
44/* For Windows... */
45#ifdef HAVE_IO_H
46#include <io.h>
47#endif
0f2d19dd
JB
48\f
49
0f2d19dd
JB
50/* {Errors and Exceptional Conditions}
51 */
52
0f2d19dd 53
24d1f171 54/* Scheme interface to scm_error_scm. */
7cb1d4d3 55void
1bbd0b84 56scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
7cb1d4d3 57{
24d1f171
MV
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);
7cb1d4d3 63}
0f2d19dd 64
24d1f171 65/* All errors should pass through here. */
3b3b36dd 66SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0,
1e6808ea
MG
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"
cdd8c091
MV
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"
1e6808ea 82 "it will usually be @code{#f}.")
1bbd0b84 83#define FUNC_NAME s_scm_error_scm
c37e0e55 84{
24d1f171 85 if (scm_gc_running_p)
a6d9e5ab 86 {
24d1f171
MV
87 /* The error occured during GC --- abort */
88 fprintf (stderr, "Guile: error during GC.\n"),
89 abort ();
a6d9e5ab 90 }
89958ad0 91
24d1f171
MV
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);
c37e0e55 97}
1bbd0b84 98#undef FUNC_NAME
c37e0e55 99
b4e15479
SJ
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
4af3c6f1
KR
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
a1ec6916 126SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
1bbd0b84 127 (SCM err),
1e6808ea
MG
128 "Return the Unix error message corresponding to @var{err}, which\n"
129 "must be an integer value.")
1bbd0b84 130#define FUNC_NAME s_scm_strerror
efb997f5 131{
4af3c6f1
KR
132 SCM ret;
133 scm_frame_begin (0);
76da80e7
MV
134 scm_frame_unwind_handler ((void(*)(void*)) scm_mutex_unlock,
135 &scm_i_misc_mutex,
136 SCM_F_WIND_EXPLICITLY);
137 scm_mutex_lock (&scm_i_misc_mutex);
4af3c6f1 138
468e87a7 139 ret = scm_from_locale_string (SCM_I_STRERROR (scm_to_int (err)));
4af3c6f1
KR
140
141 scm_frame_end ();
142 return ret;
efb997f5 143}
1bbd0b84 144#undef FUNC_NAME
efb997f5 145
a7f54aed 146SCM_GLOBAL_SYMBOL (scm_system_error_key, "system-error");
52859adf 147void
1bbd0b84 148scm_syserror (const char *subr)
52859adf 149{
4af3c6f1 150 SCM err = scm_from_int (SCM_I_ERRNO ());
01f61221 151 scm_error (scm_system_error_key,
52859adf 152 subr,
70d63753 153 "~A",
4af3c6f1
KR
154 scm_cons (scm_strerror (err), SCM_EOL),
155 scm_cons (err, SCM_EOL));
52859adf
GH
156}
157
158void
1bbd0b84 159scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
52859adf 160{
01f61221 161 scm_error (scm_system_error_key,
52859adf
GH
162 subr,
163 message,
164 args,
7888309b 165 scm_cons (scm_from_int (eno), SCM_EOL));
52859adf
GH
166}
167
a7f54aed 168SCM_GLOBAL_SYMBOL (scm_num_overflow_key, "numerical-overflow");
52859adf 169void
1bbd0b84 170scm_num_overflow (const char *subr)
52859adf 171{
01f61221 172 scm_error (scm_num_overflow_key,
52859adf
GH
173 subr,
174 "Numerical overflow",
175 SCM_BOOL_F,
176 SCM_BOOL_F);
177}
178
a7f54aed 179SCM_GLOBAL_SYMBOL (scm_out_of_range_key, "out-of-range");
52859adf 180void
1bbd0b84 181scm_out_of_range (const char *subr, SCM bad_value)
52859adf 182{
01f61221 183 scm_error (scm_out_of_range_key,
52859adf 184 subr,
cdd8c091 185 "Value out of range: ~S",
1afff620 186 scm_list_1 (bad_value),
cdd8c091 187 scm_list_1 (bad_value));
52859adf 188}
f5bf2977 189
1e76143f
GB
190void
191scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos)
192{
193 scm_error (scm_out_of_range_key,
194 subr,
cdd8c091 195 "Argument ~A out of range: ~S",
34d19ef6 196 scm_list_2 (pos, bad_value),
cdd8c091 197 scm_list_1 (bad_value));
1e76143f
GB
198}
199
200
a7f54aed 201SCM_GLOBAL_SYMBOL (scm_args_number_key, "wrong-number-of-args");
0f2d19dd 202void
1bbd0b84 203scm_wrong_num_args (SCM proc)
f5bf2977 204{
01f61221 205 scm_error (scm_args_number_key,
f5bf2977 206 NULL,
70d63753 207 "Wrong number of arguments to ~A",
1afff620 208 scm_list_1 (proc),
f5bf2977
GH
209 SCM_BOOL_F);
210}
211
9f40cd87
DH
212
213void
214scm_error_num_args_subr (const char *subr)
215{
216 scm_error (scm_args_number_key,
217 NULL,
218 "Wrong number of arguments to ~A",
468e87a7 219 scm_list_1 (scm_from_locale_string (subr)),
9f40cd87
DH
220 SCM_BOOL_F);
221}
222
223
a7f54aed 224SCM_GLOBAL_SYMBOL (scm_arg_type_key, "wrong-type-arg");
f5bf2977 225void
1bbd0b84 226scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
f5bf2977 227{
01f61221 228 scm_error (scm_arg_type_key,
f5bf2977 229 subr,
83e1ab6d 230 (pos == 0) ? "Wrong type: ~S"
70d63753 231 : "Wrong type argument in position ~A: ~S",
1afff620 232 (pos == 0) ? scm_list_1 (bad_value)
e11e83f3 233 : scm_list_2 (scm_from_int (pos), bad_value),
cdd8c091 234 scm_list_1 (bad_value));
f5bf2977
GH
235}
236
b6791b2e
GB
237void
238scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage)
239{
468e87a7 240 SCM msg = scm_from_locale_string (szMessage);
cdd8c091
MV
241 if (pos == 0)
242 {
243 scm_error (scm_arg_type_key,
244 subr, "Wrong type (expecting ~A): ~S",
245 scm_list_2 (msg, bad_value),
246 scm_list_1 (bad_value));
247 }
248 else
249 {
250 scm_error (scm_arg_type_key,
251 subr,
252 "Wrong type argument in position ~A (expecting ~A): ~S",
253 scm_list_3 (scm_from_int (pos), msg, bad_value),
254 scm_list_1 (bad_value));
255 }
b6791b2e
GB
256}
257
258
a7f54aed 259SCM_GLOBAL_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
f5bf2977 260void
1bbd0b84 261scm_memory_error (const char *subr)
f5bf2977 262{
468e87a7
MV
263 fprintf (stderr, "FATAL: memory error in %s\n", subr);
264 abort ();
f5bf2977
GH
265}
266
a7f54aed 267SCM_GLOBAL_SYMBOL (scm_misc_error_key, "misc-error");
523f5266 268void
1bbd0b84 269scm_misc_error (const char *subr, const char *message, SCM args)
523f5266
GH
270{
271 scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
272}
273
0f2d19dd
JB
274void
275scm_init_error ()
0f2d19dd 276{
a0599745
MD
277#include "libguile/cpp_err_symbols.c"
278#include "libguile/error.x"
0f2d19dd
JB
279}
280
89e00824
ML
281
282/*
283 Local Variables:
284 c-file-style: "gnu"
285 End:
286*/