* socket.c: Added declaration of inet_aton to avoid compiler
[bpt/guile.git] / libguile / scmsigs.c
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 <signal.h>
45 #include "_scm.h"
46
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51
52 \f
53
54 #if (__TURBOC__==1)
55 #define signal ssignal /* Needed for TURBOC V1.0 */
56 #endif
57
58 \f
59
60 /* SIGRETTYPE is the type that signal handlers return. See <signal.h>*/
61
62 #ifdef RETSIGTYPE
63 #define SIGRETTYPE RETSIGTYPE
64 #else
65 #ifdef STDC_HEADERS
66 #if (__TURBOC__==1)
67 #define SIGRETTYPE int
68 #else
69 #define SIGRETTYPE void
70 #endif
71 #else
72 #ifdef linux
73 #define SIGRETTYPE void
74 #else
75 #define SIGRETTYPE int
76 #endif
77 #endif
78 #endif
79
80 #ifdef vms
81 #ifdef __GNUC__
82 #define SIGRETTYPE int
83 #endif
84 #endif
85
86 \f
87
88 #define SIGFN(NAME, SCM_NAME, SIGNAL) \
89 static SIGRETTYPE \
90 NAME (sig) \
91 int sig; \
92 { \
93 signal (SIGNAL, NAME); \
94 scm_take_signal (SCM_NAME); \
95 }
96
97 #ifdef SIGHUP
98 SIGFN(scm_hup_signal, SCM_HUP_SIGNAL, SIGHUP)
99 #endif
100
101 #ifdef SIGINT
102 SIGFN(scm_int_signal, SCM_INT_SIGNAL, SIGINT)
103 #endif
104
105 #ifdef SIGFPE
106 SIGFN(scm_fpe_signal, SCM_FPE_SIGNAL, SIGFPE)
107 #endif
108
109 #ifdef SIGBUS
110 SIGFN(scm_bus_signal, SCM_BUS_SIGNAL, SIGBUS)
111 #endif
112
113 #ifdef SIGSEGV
114 SIGFN(scm_segv_signal, SCM_SEGV_SIGNAL, SIGSEGV)
115 #endif
116
117 #ifdef SIGALRM
118 SIGFN(scm_alrm_signal, SCM_ALRM_SIGNAL, SIGALRM)
119 #endif
120
121 #define FAKESIGFN(NAME, SCM_NAME) \
122 static SIGRETTYPE \
123 NAME (sig) \
124 int sig; \
125 { \
126 scm_take_signal (SCM_NAME); \
127 }
128
129 #if 0
130 /* !!! */
131 FAKESIGFN(scm_gc_signal, SCM_GC_SIGNAL)
132 FAKESIGFN(scm_tick_signal, SCM_TICK_SIGNAL)
133 #endif
134 \f
135
136 SCM_PROC(s_alarm, "alarm", 1, 0, 0, scm_alarm);
137 #ifdef __STDC__
138 SCM
139 scm_alarm (SCM i)
140 #else
141 SCM
142 scm_alarm (i)
143 SCM i;
144 #endif
145 {
146 unsigned int j;
147 SCM_ASSERT (SCM_INUMP (i) && (SCM_INUM (i) >= 0), i, SCM_ARG1, s_alarm);
148 SCM_SYSCALL (j = alarm (SCM_INUM (i)));
149 return SCM_MAKINUM (j);
150 }
151
152
153 SCM_PROC(s_pause, "pause", 0, 0, 0, scm_pause);
154 #ifdef __STDC__
155 SCM
156 scm_pause (void)
157 #else
158 SCM
159 scm_pause ()
160 #endif
161 {
162 pause ();
163 return SCM_UNSPECIFIED;
164 }
165
166 SCM_PROC(s_sleep, "sleep", 1, 0, 0, scm_sleep);
167 #ifdef __STDC__
168 SCM
169 scm_sleep (SCM i)
170 #else
171 SCM
172 scm_sleep (i)
173 SCM i;
174 #endif
175 {
176 unsigned int j;
177 SCM_ASSERT (SCM_INUMP (i) && (SCM_INUM (i) >= 0), i, SCM_ARG1, s_sleep);
178 #ifdef __HIGHC__
179 SCM_SYSCALL(j = 0; sleep(SCM_INUM(i)););
180 #else
181 SCM_SYSCALL(j = sleep(SCM_INUM(i)););
182 #endif
183 return SCM_MAKINUM (j);
184 }
185
186 SCM_PROC(s_raise, "raise", 1, 0, 0, scm_raise);
187 #ifdef __STDC__
188 SCM
189 scm_raise(SCM sig)
190 #else
191 SCM
192 scm_raise(sig)
193 SCM sig;
194 #endif
195 {
196 SCM_ASSERT(SCM_INUMP(sig), sig, SCM_ARG1, s_raise);
197 # ifdef vms
198 return SCM_MAKINUM(gsignal((int)SCM_INUM(sig)));
199 # else
200 return kill (getpid(), (int)SCM_INUM(sig)) ? SCM_BOOL_F : SCM_BOOL_T;
201 # endif
202 }
203
204 \f
205 #ifdef SIGHUP
206 static SIGRETTYPE (*oldhup) ();
207 #endif
208
209 #ifdef SIGINT
210 static SIGRETTYPE (*oldint) ();
211 #endif
212
213 #ifdef SIGFPE
214 static SIGRETTYPE (*oldfpe) ();
215 #endif
216
217 #ifdef SIGBUS
218 static SIGRETTYPE (*oldbus) ();
219 #endif
220
221 #ifdef SIGSEGV /* AMIGA lacks! */
222 static SIGRETTYPE (*oldsegv) ();
223 #endif
224
225 #ifdef SIGALRM
226 static SIGRETTYPE (*oldalrm) ();
227 #endif
228
229 #ifdef SIGPIPE
230 static SIGRETTYPE (*oldpipe) ();
231 #endif
232
233
234 #ifdef __STDC__
235 void
236 scm_init_signals (void)
237 #else
238 void
239 scm_init_signals ()
240 #endif
241 {
242 #ifdef SIGINT
243 oldint = signal (SIGINT, scm_int_signal);
244 #endif
245 #ifdef SIGHUP
246 oldhup = signal (SIGHUP, scm_hup_signal);
247 #endif
248 #ifdef SIGFPE
249 oldfpe = signal (SIGFPE, scm_fpe_signal);
250 #endif
251 #ifdef SIGBUS
252 oldbus = signal (SIGBUS, scm_bus_signal);
253 #endif
254 #ifdef SIGSEGV /* AMIGA lacks! */
255 oldsegv = signal (SIGSEGV, scm_segv_signal);
256 #endif
257 #ifdef SIGALRM
258 alarm (0); /* kill any pending ALRM interrupts */
259 oldalrm = signal (SIGALRM, scm_alrm_signal);
260 #endif
261 #ifdef SIGPIPE
262 oldpipe = signal (SIGPIPE, SIG_IGN);
263 #endif
264 #ifdef ultrix
265 siginterrupt (SIGINT, 1);
266 siginterrupt (SIGALRM, 1);
267 siginterrupt (SIGHUP, 1);
268 siginterrupt (SIGPIPE, 1);
269 #endif /* ultrix */
270 }
271
272 /* This is used in preparation for a possible fork(). Ignore all
273 signals before the fork so that child will catch only if it
274 establishes a handler */
275 #ifdef __STDC__
276 void
277 scm_ignore_signals (void)
278 #else
279 void
280 scm_ignore_signals ()
281 #endif
282 {
283 #ifdef ultrix
284 siginterrupt (SIGINT, 0);
285 siginterrupt (SIGALRM, 0);
286 siginterrupt (SIGHUP, 0);
287 siginterrupt (SIGPIPE, 0);
288 #endif /* ultrix */
289 signal (SIGINT, SIG_IGN);
290 #ifdef SIGHUP
291 signal (SIGHUP, SIG_DFL);
292 #endif
293 #ifdef SCM_FLOATS
294 signal (SIGFPE, SIG_DFL);
295 #endif
296 #ifdef SIGBUS
297 signal (SIGBUS, SIG_DFL);
298 #endif
299 #ifdef SIGSEGV /* AMIGA lacks! */
300 signal (SIGSEGV, SIG_DFL);
301 #endif
302 /* Some documentation claims that ALRMs are cleared accross forks.
303 If this is not always true then the value returned by alarm(0)
304 will have to be saved and scm_unignore_signals() will have to
305 reinstate it. */
306 /* This code should be neccessary only if the forked process calls
307 alarm() without establishing a handler:
308 #ifdef SIGALRM
309 oldalrm = signal(SIGALRM, SIG_DFL);
310 #endif */
311 /* These flushes are per warning in man page on fork(). */
312 fflush (stdout);
313 fflush (stderr);
314 }
315
316 #ifdef __STDC__
317 void
318 scm_unignore_signals (void)
319 #else
320 void
321 scm_unignore_signals ()
322 #endif
323 {
324 signal (SIGINT, scm_int_signal);
325 #ifdef SIGHUP
326 signal (SIGHUP, scm_hup_signal);
327 #endif
328 #ifdef SCM_FLOATS
329 signal (SIGFPE, scm_fpe_signal);
330 #endif
331 #ifdef SIGBUS
332 signal (SIGBUS, scm_bus_signal);
333 #endif
334 #ifdef SIGSEGV /* AMIGA lacks! */
335 signal (SIGSEGV, scm_segv_signal);
336 #endif
337 #ifdef SIGALRM
338 signal (SIGALRM, scm_alrm_signal);
339 #endif
340 #ifdef ultrix
341 siginterrupt (SIGINT, 1);
342 siginterrupt (SIGALRM, 1);
343 siginterrupt (SIGHUP, 1);
344 siginterrupt (SIGPIPE, 1);
345 #endif /* ultrix */
346 }
347
348 SCM_PROC (s_restore_signals, "restore-signals", 0, 0, 0, scm_restore_signals);
349 #ifdef __STDC__
350 SCM
351 scm_restore_signals (void)
352 #else
353 SCM
354 scm_restore_signals ()
355 #endif
356 {
357 #ifdef ultrix
358 siginterrupt (SIGINT, 0);
359 siginterrupt (SIGALRM, 0);
360 siginterrupt (SIGHUP, 0);
361 siginterrupt (SIGPIPE, 0);
362 #endif /* ultrix */
363 signal (SIGINT, oldint);
364 #ifdef SIGHUP
365 signal (SIGHUP, oldhup);
366 #endif
367 #ifdef SCM_FLOATS
368 signal (SIGFPE, oldfpe);
369 #endif
370 #ifdef SIGBUS
371 signal (SIGBUS, oldbus);
372 #endif
373 #ifdef SIGSEGV /* AMIGA lacks! */
374 signal (SIGSEGV, oldsegv);
375 #endif
376 #ifdef SIGPIPE
377 signal (SIGPIPE, oldpipe);
378 #endif
379 #ifdef SIGALRM
380 alarm (0); /* kill any pending ALRM interrupts */
381 signal (SIGALRM, oldalrm);
382 #endif
383 return SCM_UNSPECIFIED;
384 }
385
386
387 #ifdef __STDC__
388 void
389 scm_init_scmsigs (void)
390 #else
391 void
392 scm_init_scmsigs ()
393 #endif
394 {
395 #include "scmsigs.x"
396 }
397