backward compatible processing for "+/pattern"
[unix-history] / usr / src / usr.bin / more / signal.c
CommitLineData
bfe13c81
KB
1/*
2 * Copyright (c) 1988 Mark Nudleman
3 * Copyright (c) 1988 Regents of the University of California.
4 * All rights reserved.
5 *
bfe13c81
KB
6 * Redistribution and use in source and binary forms are permitted
7 * provided that the above copyright notice and this paragraph are
8 * duplicated in all such forms and that any documentation,
9 * advertising materials, and other materials related to such
10 * distribution and use acknowledge that the software was developed
a942b40b
KB
11 * by Mark Nudleman and the University of California, Berkeley. The
12 * name of Mark Nudleman or the
bfe13c81
KB
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20#ifndef lint
966c6ec0 21static char sccsid[] = "@(#)signal.c 5.5 (Berkeley) %G%";
bfe13c81
KB
22#endif /* not lint */
23
24/*
25 * Routines dealing with signals.
26 *
27 * A signal usually merely causes a bit to be set in the "signals" word.
28 * At some convenient time, the mainline code checks to see if any
29 * signals need processing by calling psignal().
30 * If we happen to be reading from a file [in iread()] at the time
31 * the signal is received, we call intread to interrupt the iread.
32 */
33
966c6ec0 34#include <less.h>
bfe13c81
KB
35#include <signal.h>
36
37/*
38 * "sigs" contains bits indicating signals which need to be processed.
39 */
966c6ec0 40int sigs;
bfe13c81 41
bfe13c81
KB
42#ifdef SIGTSTP
43#define S_STOP 02
44#endif
45#if defined(SIGWINCH) || defined(SIGWIND)
46#define S_WINCH 04
47#endif
48
49extern int sc_width, sc_height;
50extern int screen_trashed;
51extern int lnloop;
52extern int linenums;
53extern int scroll;
54extern int reading;
55
bfe13c81
KB
56#ifdef SIGTSTP
57/*
58 * "Stop" (^Z) signal handler.
59 */
966c6ec0 60static
bfe13c81
KB
61stop()
62{
966c6ec0 63 (void)signal(SIGTSTP, stop);
bfe13c81
KB
64 sigs |= S_STOP;
65 if (reading)
66 intread();
67}
68#endif
69
70#ifdef SIGWINCH
71/*
72 * "Window" change handler
73 */
bfe13c81
KB
74winch()
75{
966c6ec0 76 (void)signal(SIGWINCH, winch);
bfe13c81
KB
77 sigs |= S_WINCH;
78 if (reading)
79 intread();
80}
81#else
82#ifdef SIGWIND
83/*
84 * "Window" change handler
85 */
bfe13c81
KB
86winch()
87{
966c6ec0 88 (void)signal(SIGWIND, winch);
bfe13c81
KB
89 sigs |= S_WINCH;
90 if (reading)
91 intread();
92}
93#endif
94#endif
95
96/*
97 * Set up the signal handlers.
98 */
bfe13c81
KB
99init_signals(on)
100 int on;
101{
966c6ec0
KB
102 int quit();
103
bfe13c81
KB
104 if (on)
105 {
106 /*
107 * Set signal handlers.
108 */
966c6ec0 109 (void)signal(SIGINT, quit);
bfe13c81 110#ifdef SIGTSTP
966c6ec0 111 (void)signal(SIGTSTP, stop);
bfe13c81
KB
112#endif
113#ifdef SIGWINCH
966c6ec0 114 (void)signal(SIGWINCH, winch);
bfe13c81
KB
115#else
116#ifdef SIGWIND
966c6ec0 117 (void)signal(SIGWIND, winch);
bfe13c81
KB
118#endif
119#endif
120 } else
121 {
122 /*
123 * Restore signals to defaults.
124 */
966c6ec0 125 (void)signal(SIGINT, SIG_DFL);
bfe13c81 126#ifdef SIGTSTP
966c6ec0 127 (void)signal(SIGTSTP, SIG_DFL);
bfe13c81
KB
128#endif
129#ifdef SIGWINCH
966c6ec0 130 (void)signal(SIGWINCH, SIG_IGN);
bfe13c81
KB
131#endif
132#ifdef SIGWIND
966c6ec0 133 (void)signal(SIGWIND, SIG_IGN);
bfe13c81
KB
134#endif
135 }
136}
137
138/*
139 * Process any signals we have received.
140 * A received signal cause a bit to be set in "sigs".
141 */
bfe13c81
KB
142psignals()
143{
144 register int tsignals;
145
146 if ((tsignals = sigs) == 0)
9bf656f5 147 return;
bfe13c81
KB
148 sigs = 0;
149
150#ifdef S_WINCH
151 if (tsignals & S_WINCH)
152 {
153 int old_width, old_height;
154 /*
155 * Re-execute get_term() to read the new window size.
156 */
157 old_width = sc_width;
158 old_height = sc_height;
159 get_term();
160 if (sc_width != old_width || sc_height != old_height)
161 {
162 scroll = (sc_height + 1) / 2;
163 screen_trashed = 1;
164 }
165 }
166#endif
167#ifdef SIGTSTP
168 if (tsignals & S_STOP)
169 {
170 /*
171 * Clean up the terminal.
172 */
173#ifdef SIGTTOU
966c6ec0 174 (void)signal(SIGTTOU, SIG_IGN);
bfe13c81
KB
175#endif
176 lower_left();
177 clear_eol();
178 deinit();
966c6ec0 179 (void)flush();
bfe13c81
KB
180 raw_mode(0);
181#ifdef SIGTTOU
966c6ec0 182 (void)signal(SIGTTOU, SIG_DFL);
bfe13c81 183#endif
966c6ec0
KB
184 (void)signal(SIGTSTP, SIG_DFL);
185 (void)kill(getpid(), SIGTSTP);
bfe13c81
KB
186 /*
187 * ... Bye bye. ...
188 * Hopefully we'll be back later and resume here...
189 * Reset the terminal and arrange to repaint the
190 * screen when we get back to the main command loop.
191 */
966c6ec0 192 (void)signal(SIGTSTP, stop);
bfe13c81
KB
193 raw_mode(1);
194 init();
195 screen_trashed = 1;
196 }
197#endif
bfe13c81 198}