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