1 /* exception.c
2 *
3 * $Id$
4 *
5 * Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
6 * Copyright 2002, 2003 Sam Hocevar <sam@hocevar.net>, Paris
7 *
8 * This software was derived from Elk 1.2, which was Copyright 1987, 1988,
9 * 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
10 * by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
11 * between TELES and Nixdorf Microprocessor Engineering, Berlin).
12 *
13 * Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
14 * owners or individual owners of copyright in this software, grant to any
15 * person or company a worldwide, royalty free, license to
16 *
17 * i) copy this software,
18 * ii) prepare derivative works based on this software,
19 * iii) distribute copies of this software or derivative works,
20 * iv) perform this software, or
21 * v) display this software,
22 *
23 * provided that this notice is not removed and that neither Oliver Laumann
24 * nor Teles nor Nixdorf are deemed to have made any representations as to
25 * the suitability of this software for any purpose nor are held responsible
26 * for any defects of this software.
27 *
28 * THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
29 */
30
31 #include "config.h"
32
33 #include <stdlib.h>
34
35 #include "kernel.h"
36
37 extern void Reset () elk_attribute(__noreturn__);
38
39 int Intr_Was_Ignored;
40 unsigned long int Intr_Level;
41
42 #if defined(HAVE_SIGPROCMASK)
43 sigset_t Sigset_Old, Sigset_Block;
44 #elif defined(HAVE_SIGBLOCK)
45 int Sigmask_Old, Sigmask_Block;
46 #endif
47
48 static Object V_Interrupt_Handler;
49
50 /* Make sure temp files are removed on hangup and broken pipe.
51 */
52 /*ARGSUSED*/
Signal_Exit(int sig)53 void Signal_Exit (int sig) {
54 Exit_Handler ();
55 exit (1);
56 }
57
Init_Exception()58 void Init_Exception () {
59 Define_Variable (&V_Interrupt_Handler, "interrupt-handler", Null);
60 #if defined(HAVE_SIGPROCMASK)
61 sigemptyset (&Sigset_Block);
62 sigaddset (&Sigset_Block, SIGINT);
63 (void)sigprocmask (0, (sigset_t *)0, &Sigset_Old);
64 #elif defined(HAVE_SIGBLOCK)
65 Sigmask_Block = sigmask (SIGINT);
66 Sigmask_Old = sigblock (0);
67 #endif
68 #ifdef SIGHUP
69 (void)signal (SIGHUP, Signal_Exit);
70 #endif
71 #ifdef SIGPIPE
72 (void)signal (SIGPIPE, Signal_Exit);
73 #endif
74 }
75
76 /*ARGSUSED*/
Intr_Handler(int sig)77 void Intr_Handler (int sig) {
78 Object fun;
79
80 #if defined(HAVE_SIGPROCMASK) || ! defined(HAVE_SIGBLOCK)
81 (void)signal (SIGINT, Intr_Handler);
82 #endif
83 Set_Error_Tag ("interrupt-handler");
84 Reset_IO (1);
85 fun = Var_Get (V_Interrupt_Handler);
86 if (TYPE(fun) == T_Compound && COMPOUND(fun)->min_args == 0)
87 (void)Funcall (fun, Null, 0);
88 Format (Curr_Output_Port, "~%\7Interrupt!~%", 15, 0, (Object *)0);
89 Reset ();
90 /*NOTREACHED*/
91 }
92
Install_Intr_Handler()93 void Install_Intr_Handler () {
94 if (signal (SIGINT, SIG_IGN) == SIG_IGN)
95 Intr_Was_Ignored = 1;
96 else
97 (void)signal (SIGINT, Intr_Handler);
98 }
99
P_Disable_Interrupts()100 Object P_Disable_Interrupts () {
101 Disable_Interrupts;
102 return Make_Unsigned_Long (Intr_Level);
103 }
104
P_Enable_Interrupts()105 Object P_Enable_Interrupts () {
106 Enable_Interrupts;
107 return Make_Unsigned_Long (Intr_Level);
108 }
109