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