2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid
[] = "@(#)intr.c 8.1 (Berkeley) 3/23/94";
38 #include <sys/types.h>
39 #include <sys/queue.h>
42 #include <bitstring.h>
59 * There's a count of how deep the interrupt level in the SCR structure
60 * has gone. Each routine that wants to be interrupted increments this
61 * level, and decrements it when it tears the interrupts down. There are
62 * two simplifying assumptions:
64 * 1: All interruptible areas want the same handler (we don't have
65 * to save/restore the old handler).
66 * 2: This is the only way to turn on interrupts (we don't have to
67 * worry about them already being turned on if the interrupt
70 * We do it this way because interrupts have to be very fast -- if the
71 * O_REMAPMAX option is turned off, we are setting interrupts per key
74 * If an interrupt arrives, the S_INTERRUPTED bit is set in any SCR that
75 * has the S_INTERRUPTIBLE bit set. In the future this may be a problem.
76 * The user should be able to move to another screen and keep typing while
77 * another screen runs. Currently, if the user does this and the user has
78 * more than one interruptible thing running, there will be no way to know
82 static void intr_def
__P((int));
86 * Set up a interrupts.
95 /* You can never interrupt sessions not using tty's. */
96 if (!F_ISSET(sp
->gp
, G_STDIN_TTY
))
99 /* If interrupts already set up, just increase the level. */
100 if (sp
->intr_level
++)
103 /* Turn interrupts on in this screen. */
104 F_SET(sp
, S_INTERRUPTIBLE
);
106 /* Install a handler. */
107 act
.sa_handler
= intr_def
;
108 sigemptyset(&act
.sa_mask
);
110 if (sigaction(SIGINT
, &act
, &sp
->intr_act
)) {
111 msgq(sp
, M_SYSERR
, "sigaction");
116 * Turn on interrupts. ISIG turns on VINTR, VQUIT and VSUSP. We
117 * want VINTR to interrupt, so we install a handler. VQUIT is
118 * ignored by main() because nvi never wants to catch it. A handler
119 * for VSUSP should have been installed by the screen code.
121 if (tcgetattr(STDIN_FILENO
, &sp
->intr_term
)) {
122 msgq(sp
, M_SYSERR
, "tcgetattr");
125 nterm
= sp
->intr_term
;
126 nterm
.c_lflag
|= ISIG
;
127 if (tcsetattr(STDIN_FILENO
, TCSANOW
| TCSASOFT
, &nterm
)) {
128 msgq(sp
, M_SYSERR
, "tcsetattr");
130 * If an error occurs, back out the changes and run
131 * without interrupts.
133 err2
: (void)sigaction(SIGINT
, &sp
->intr_act
, NULL
);
134 err1
: sp
->intr_level
= 0;
135 F_CLR(sp
, S_INTERRUPTIBLE
);
143 * Tear down interrupts.
149 /* If not the bottom level of interrupts, just return. */
150 if (--sp
->intr_level
)
153 /* Turn off interrupts. */
154 if (tcsetattr(STDIN_FILENO
, TCSANOW
| TCSASOFT
, &sp
->intr_term
))
155 msgq(sp
, M_SYSERR
, "tcsetattr");
157 /* Reset the signal state. */
158 if (sigaction(SIGINT
, &sp
->intr_act
, NULL
))
159 msgq(sp
, M_SYSERR
, "sigaction");
161 /* Clear interrupt bits in this screen. */
162 F_CLR(sp
, S_INTERRUPTED
| S_INTERRUPTIBLE
);
167 * Default interrupt handler.
175 for (sp
= __global_list
->dq
.cqh_first
;
176 sp
!= (void *)&__global_list
->dq
; sp
= sp
->q
.cqe_next
)
177 if (F_ISSET(sp
, S_INTERRUPTIBLE
))
178 F_SET(sp
, S_INTERRUPTED
);