Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / vi / intr.c
blob08419c3f200c56d1ff66d59cad7af3b085b99f07
1 /*-
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
7 * are met:
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
31 * SUCH DAMAGE.
34 #ifndef lint
35 static char sccsid[] = "@(#)intr.c 8.1 (Berkeley) 3/23/94";
36 #endif /* not lint */
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <sys/time.h>
42 #include <bitstring.h>
43 #include <errno.h>
44 #include <limits.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <termios.h>
50 #include <unistd.h>
52 #include "compat.h"
53 #include <db.h>
54 #include <regex.h>
56 #include "vi.h"
58 /*
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
68 * level is 0).
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
72 * stroke.
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
79 * which one to stop.
82 static void intr_def __P((int));
85 * intr_init --
86 * Set up a interrupts.
88 int
89 intr_init(sp)
90 SCR *sp;
92 struct sigaction act;
93 struct termios nterm;
95 /* You can never interrupt sessions not using tty's. */
96 if (!F_ISSET(sp->gp, G_STDIN_TTY))
97 return (1);
99 /* If interrupts already set up, just increase the level. */
100 if (sp->intr_level++)
101 return (0);
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);
109 act.sa_flags = 0;
110 if (sigaction(SIGINT, &act, &sp->intr_act)) {
111 msgq(sp, M_SYSERR, "sigaction");
112 goto err1;
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");
123 goto err2;
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);
136 return (1);
138 return (0);
142 * intr_end --
143 * Tear down interrupts.
145 void
146 intr_end(sp)
147 SCR *sp;
149 /* If not the bottom level of interrupts, just return. */
150 if (--sp->intr_level)
151 return;
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);
166 * intr_def --
167 * Default interrupt handler.
169 static void
170 intr_def(signo)
171 int signo;
173 SCR *sp;
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);