*.[ch]: Replace GUILE_PROC w/ SCM_DEFINE.
[bpt/guile.git] / libguile / error.c
1 /* Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
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.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include <stdio.h>
48 #include "_scm.h"
49 #include "pairs.h"
50 #include "genio.h"
51 #include "throw.h"
52
53 #include "scm_validate.h"
54 #include "error.h"
55
56 #ifdef HAVE_UNISTD_H
57 #include <unistd.h>
58 #endif
59 \f
60
61 \f
62 /* {Errors and Exceptional Conditions}
63 */
64
65
66 extern int errno;
67
68 /* All errors should pass through here. */
69 void
70 scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
71 {
72 SCM arg_list;
73 arg_list = scm_listify (subr ? scm_makfrom0str (subr) : SCM_BOOL_F,
74 message ? scm_makfrom0str (message) : SCM_BOOL_F,
75 args,
76 rest,
77 SCM_UNDEFINED);
78 scm_ithrow (key, arg_list, 1);
79
80 /* No return, but just in case: */
81 {
82 const char msg[] = "guile:scm_error:scm_ithrow returned!\n";
83
84 write (2, msg, (sizeof msg) - 1);
85 }
86 exit (1);
87 }
88
89 /* Scheme interface to scm_error. */
90 SCM_DEFINE(scm_error_scm, "scm-error", 5, 0, 0,
91 (SCM key, SCM subr, SCM message, SCM args, SCM rest),
92 "Raise an error with key @var{key}. @var{subr} can be a string naming
93 the procedure associated with the error, or @code{#f}. @var{message}
94 is the error message string, possibly containing @code{%S} and @code{%s}
95 escapes. When an error is reported, these are replaced by formating the
96 corresponding members of @var{args}: @code{%s} formats using @code{display}
97 and @code{%S} formats using @code{write}. @var{data} is a
98 list or @code{#f} depending on @var{key}: if @var{key} is
99 @code{system-error} then it should be a list
100 containing the Unix @code{errno} value; If @var{key} is @code{signal} then
101 it should be a list containing the Unix signal number; otherwise it
102 will usually be @code{#f}.")
103 #define FUNC_NAME s_scm_error_scm
104 {
105 char *szSubr;
106 char *szMessage;
107 SCM_VALIDATE_SYMBOL(1,key);
108 SCM_VALIDATE_NULLORROSTRING_COPY(2,subr,szSubr);
109 SCM_VALIDATE_NULLORROSTRING_COPY(3,message,szMessage);
110 SCM_COERCE_SUBSTR (message);
111
112 scm_error (key, szSubr, szMessage, args, rest);
113 /* not reached. */
114 }
115 #undef FUNC_NAME
116
117 SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
118 (SCM err),
119 "Returns the Unix error message corresponding to @var{errno}, an integer.")
120 #define FUNC_NAME s_scm_strerror
121 {
122 SCM_VALIDATE_INUM(1,err);
123 return scm_makfrom0str (strerror (SCM_INUM (err)));
124 }
125 #undef FUNC_NAME
126
127 SCM_SYMBOL (scm_system_error_key, "system-error");
128 void
129 scm_syserror (const char *subr)
130 {
131 scm_error (scm_system_error_key,
132 subr,
133 "%s",
134 scm_cons (scm_makfrom0str (strerror (errno)), SCM_EOL),
135 scm_cons (SCM_MAKINUM (errno), SCM_EOL));
136 }
137
138 void
139 scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
140 {
141 scm_error (scm_system_error_key,
142 subr,
143 message,
144 args,
145 scm_cons (SCM_MAKINUM (eno), SCM_EOL));
146 }
147
148 void
149 scm_sysmissing (const char *subr)
150 {
151 #ifdef ENOSYS
152 scm_error (scm_system_error_key,
153 subr,
154 "%s",
155 scm_cons (scm_makfrom0str (strerror (ENOSYS)), SCM_EOL),
156 scm_cons (SCM_MAKINUM (ENOSYS), SCM_EOL));
157 #else
158 scm_error (scm_system_error_key,
159 subr,
160 "Missing function",
161 SCM_BOOL_F,
162 scm_cons (SCM_MAKINUM (0), SCM_EOL));
163 #endif
164 }
165
166 SCM_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_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 "Argument out of range: %S",
184 scm_cons (bad_value, SCM_EOL),
185 SCM_BOOL_F);
186 }
187
188 SCM_SYMBOL (scm_args_number_key, "wrong-number-of-args");
189 void
190 scm_wrong_num_args (SCM proc)
191 {
192 scm_error (scm_args_number_key,
193 NULL,
194 "Wrong number of arguments to %s",
195 scm_cons (proc, SCM_EOL),
196 SCM_BOOL_F);
197 }
198
199 SCM_SYMBOL (scm_arg_type_key, "wrong-type-arg");
200 void
201 scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
202 {
203 scm_error (scm_arg_type_key,
204 subr,
205 (pos == 0) ? "Wrong type argument: %S"
206 : "Wrong type argument in position %s: %S",
207 (pos == 0) ? scm_cons (bad_value, SCM_EOL)
208 : scm_cons (SCM_MAKINUM (pos), scm_cons (bad_value, SCM_EOL)),
209 SCM_BOOL_F);
210 }
211
212 SCM_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
213 void
214 scm_memory_error (const char *subr)
215 {
216 scm_error (scm_memory_alloc_key,
217 subr,
218 "Memory allocation error",
219 SCM_BOOL_F,
220 SCM_BOOL_F);
221 }
222
223 SCM_SYMBOL (scm_misc_error_key, "misc-error");
224 void
225 scm_misc_error (const char *subr, const char *message, SCM args)
226 {
227 scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
228 }
229
230 /* implements the SCM_ASSERT interface. */
231 SCM
232 scm_wta (SCM arg, const char *pos, const char *s_subr)
233 {
234 if (!s_subr || !*s_subr)
235 s_subr = NULL;
236 if ((~0x1fL) & (long) pos)
237 {
238 /* error string supplied. */
239 scm_misc_error (s_subr, pos, SCM_LIST1 (arg));
240 }
241 else
242 {
243 /* numerical error code. */
244 int error = (long) pos;
245
246 switch (error)
247 {
248 case SCM_ARGn:
249 scm_wrong_type_arg (s_subr, 0, arg);
250 case SCM_ARG1:
251 scm_wrong_type_arg (s_subr, 1, arg);
252 case SCM_ARG2:
253 scm_wrong_type_arg (s_subr, 2, arg);
254 case SCM_ARG3:
255 scm_wrong_type_arg (s_subr, 3, arg);
256 case SCM_ARG4:
257 scm_wrong_type_arg (s_subr, 4, arg);
258 case SCM_ARG5:
259 scm_wrong_type_arg (s_subr, 5, arg);
260 case SCM_ARG6:
261 scm_wrong_type_arg (s_subr, 6, arg);
262 case SCM_ARG7:
263 scm_wrong_type_arg (s_subr, 7, arg);
264 case SCM_WNA:
265 scm_wrong_num_args (arg);
266 case SCM_OUTOFRANGE:
267 scm_out_of_range (s_subr, arg);
268 case SCM_NALLOC:
269 scm_memory_error (s_subr);
270 default:
271 /* this shouldn't happen. */
272 scm_misc_error (s_subr, "Unknown error", SCM_EOL);
273 }
274 }
275 return SCM_UNSPECIFIED;
276 }
277
278 void
279 scm_init_error ()
280 {
281 #include "cpp_err_symbols.c"
282 #include "error.x"
283 }
284