* _scm.h (SCM_PROC): Extraneous semicolon (outside functions)
[bpt/guile.git] / libguile / error.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 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, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42
43#include <stdio.h>
44#include "_scm.h"
20e6290e
JB
45#include "pairs.h"
46#include "genio.h"
47#include "throw.h"
48
49#include "error.h"
0f2d19dd 50
95b88819
GH
51#ifdef HAVE_UNISTD_H
52#include <unistd.h>
53#endif
0f2d19dd
JB
54\f
55
56\f
57/* {Errors and Exceptional Conditions}
58 */
59
60SCM system_error_sym;
61
62/* True between SCM_DEFER_INTS and SCM_ALLOW_INTS, and
63 * when the interpreter is not running at all.
64 */
65int scm_ints_disabled = 1;
66
67
68extern int errno;
69#ifdef __STDC__
70static void
71err_head (char *str)
72#else
73static void
74err_head (str)
75 char *str;
76#endif
77{
78 int oerrno = errno;
79 if (SCM_NIMP (scm_cur_outp))
80 scm_fflush (scm_cur_outp);
81 scm_gen_putc ('\n', scm_cur_errp);
82#if 0
83 if (SCM_BOOL_F != *scm_loc_loadpath)
84 {
85 scm_iprin1 (*scm_loc_loadpath, scm_cur_errp, 1);
86 scm_gen_puts (scm_regular_string, ", line ", scm_cur_errp);
87 scm_intprint ((long) scm_linum, 10, scm_cur_errp);
88 scm_gen_puts (scm_regular_string, ": ", scm_cur_errp);
89 }
90#endif
91 scm_fflush (scm_cur_errp);
92 errno = oerrno;
93 if (scm_cur_errp == scm_def_errp)
94 {
95 if (errno > 0)
96 perror (str);
97 fflush (stderr);
98 return;
99 }
100}
101
102
103SCM_PROC(s_errno, "errno", 0, 1, 0, scm_errno);
104#ifdef __STDC__
105SCM
106scm_errno (SCM arg)
107#else
108SCM
109scm_errno (arg)
110 SCM arg;
111#endif
112{
113 int old = errno;
114 if (!SCM_UNBNDP (arg))
115 {
116 if (SCM_FALSEP (arg))
117 errno = 0;
118 else
119 errno = SCM_INUM (arg);
120 }
121 return SCM_MAKINUM (old);
122}
123
124SCM_PROC(s_perror, "perror", 1, 0, 0, scm_perror);
125#ifdef __STDC__
126SCM
127scm_perror (SCM arg)
128#else
129SCM
130scm_perror (arg)
131 SCM arg;
132#endif
133{
134 SCM_ASSERT (SCM_NIMP (arg) && SCM_STRINGP (arg), arg, SCM_ARG1, s_perror);
135 err_head (SCM_CHARS (arg));
136 return SCM_UNSPECIFIED;
137}
138
139
140#ifdef __STDC__
141void
142scm_everr (SCM exp, SCM env, SCM arg, char *pos, char *s_subr)
143#else
144void
145scm_everr (exp, env, arg, pos, s_subr)
146 SCM exp;
147 SCM env;
148 SCM arg;
149 char *pos;
150 char *s_subr;
151#endif
152{
153 SCM desc;
154 SCM args;
155
156 if ((~0x1fL) & (long) pos)
157 desc = scm_makfrom0str (pos);
158 else
159 desc = SCM_MAKINUM ((long)pos);
160
161 {
162 SCM sym;
163 if (!s_subr || !*s_subr)
164 sym = SCM_BOOL_F;
165 else
166 sym = SCM_CAR (scm_intern0 (s_subr));
167 args = scm_listify (desc, sym, arg, SCM_UNDEFINED);
168 }
169
170 /* (throw (quote %%system-error) <desc> <proc-name> arg)
171 *
172 * <desc> is a string or an integer (see %%system-errors).
173 * <proc-name> is a symbol or #f in some annoying cases (e.g. cddr).
174 */
175
176 scm_ithrow (system_error_sym, args, 1);
177
178 /* No return, but just in case: */
179
043045f8 180 write (2, "unhandled system error", sizeof ("unhandled system error") - 1);
0f2d19dd
JB
181 exit (1);
182}
183
184#ifdef __STDC__
185SCM
186scm_wta (SCM arg, char *pos, char *s_subr)
187#else
188SCM
189scm_wta (arg, pos, s_subr)
190 SCM arg;
191 char *pos;
192 char *s_subr;
193#endif
194{
195 scm_everr (SCM_UNDEFINED, SCM_EOL, arg, pos, s_subr);
196 return SCM_UNSPECIFIED;
197}
198
7cb1d4d3 199void (*scm_error_callback) () = 0;
0f2d19dd 200
7cb1d4d3
GH
201void
202scm_error (key, subr, message, args, rest)
203 SCM key;
204 char *subr;
205 char *message;
206 SCM args;
207 SCM rest;
208{
209 SCM arg_list;
210 if (scm_error_callback)
211 (*scm_error_callback) (key, subr, message, args, rest);
212
213 arg_list = scm_listify (scm_makfrom0str (subr),
214 scm_makfrom0str (message),
215 args,
216 rest,
217 SCM_UNDEFINED);
218 scm_ithrow (key, arg_list, 1);
219
220 /* No return, but just in case: */
221
222 write (2, "unhandled system error", sizeof ("unhandled system error") - 1);
223 exit (1);
224}
0f2d19dd
JB
225
226#ifdef __STDC__
227void
228scm_init_error (void)
229#else
230void
231scm_init_error ()
232#endif
233{
1efc5155 234 system_error_sym = scm_permanent_object (SCM_CAR (scm_intern0 ("%%system-error")));
0f2d19dd
JB
235#include "error.x"
236}
237