xref: /netbsd-src/lib/libcurses/tstp.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: tstp.c,v 1.34 2006/08/25 17:04:32 jdc Exp $	*/
2 
3 /*
4  * Copyright (c) 1981, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)tstp.c	8.3 (Berkeley) 5/4/94";
36 #else
37 __RCSID("$NetBSD: tstp.c,v 1.34 2006/08/25 17:04:32 jdc Exp $");
38 #endif
39 #endif				/* not lint */
40 
41 #include <sys/ioctl.h>
42 
43 #include <errno.h>
44 #include <signal.h>
45 #include <termios.h>
46 #include <unistd.h>
47 
48 #include "curses.h"
49 #include "curses_private.h"
50 
51 static int tstp_set = 0;
52 static int winch_set = 0;
53 
54 static void (*otstpfn)
55 __P((int)) = SIG_DFL;
56 
57 static struct sigaction	owsa;
58 
59 /*
60  * stop_signal_handler --
61  *	Handle stop signals.
62  */
63 void
64 __stop_signal_handler(/*ARGSUSED*/int signo)
65 {
66 	sigset_t oset, set;
67 
68 	/*
69 	 * Block window change and timer signals.  The latter is because
70 	 * applications use timers to decide when to repaint the screen.
71 	 */
72 	(void) sigemptyset(&set);
73 	(void) sigaddset(&set, SIGALRM);
74 	(void) sigaddset(&set, SIGWINCH);
75 	(void) sigprocmask(SIG_BLOCK, &set, &oset);
76 
77 	/*
78 	 * End the window, which also resets the terminal state to the
79 	 * original modes.
80 	 */
81 	__stopwin();
82 
83 	/* Unblock SIGTSTP. */
84 	(void) sigemptyset(&set);
85 	(void) sigaddset(&set, SIGTSTP);
86 	(void) sigprocmask(SIG_UNBLOCK, &set, NULL);
87 
88 	/* Stop ourselves. */
89 	(void) kill(0, SIGTSTP);
90 
91 	/* Time passes ... */
92 
93 	/* restart things */
94 	__restartwin();
95 
96 	/* Reset the signals. */
97 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
98 }
99 
100 /*
101  * Set the TSTP handler.
102  */
103 void
104 __set_stophandler(void)
105 {
106 #ifdef DEBUG
107 	__CTRACE("__set_stophandler: %d\n", tstp_set);
108 #endif
109 	if (!tstp_set) {
110 		otstpfn = signal(SIGTSTP, __stop_signal_handler);
111 		tstp_set = 1;
112 	}
113 }
114 
115 /*
116  * Restore the TSTP handler.
117  */
118 void
119 __restore_stophandler(void)
120 {
121 #ifdef DEBUG
122 	__CTRACE("__restore_stophandler: %d\n", tstp_set);
123 #endif
124 	if (tstp_set) {
125 		(void) signal(SIGTSTP, otstpfn);
126 		tstp_set = 0;
127 	}
128 }
129 
130 /*
131  * winch_signal_handler --
132  *	Handle winch signals by pushing KEY_RESIZE into the input stream.
133  */
134 void
135 __winch_signal_handler(/*ARGSUSED*/int signo)
136 {
137 	struct winsize win;
138 
139 	if (ioctl(fileno(_cursesi_screen->outfd), TIOCGWINSZ, &win) != -1 &&
140 	    win.ws_row != 0 && win.ws_col != 0) {
141 		LINES = win.ws_row;
142 		COLS = win.ws_col;
143 	}
144 	/*
145 	 * If there was a previous handler, call that,
146 	 * otherwise tell getch() to send KEY_RESIZE.
147 	 */
148 	if (owsa.sa_handler !=  NULL)
149 		owsa.sa_handler(signo);
150 	else
151 		_cursesi_screen->resized = 1;
152 }
153 
154 /*
155  * Set the WINCH handler.
156  */
157 void
158 __set_winchhandler(void)
159 {
160 #ifdef DEBUG
161 	__CTRACE("__set_winchhandler: %d\n", winch_set);
162 #endif
163 	if (!winch_set) {
164 		struct sigaction sa;
165 
166 		sa.sa_handler = __winch_signal_handler;
167 		sa.sa_flags = 0;
168 		sigemptyset(&sa.sa_mask);
169 		sigaction(SIGWINCH, &sa, &owsa);
170 		winch_set = 1;
171 #ifdef DEBUG
172 		__CTRACE("__set_winchhandler: owsa.sa_handler=%p\n",
173 		    owsa.sa_handler);
174 #endif
175 	}
176 }
177 
178 /*
179  * Restore the WINCH handler.
180  */
181 void
182 __restore_winchhandler(void)
183 {
184 #ifdef DEBUG
185 	__CTRACE("__restore_winchhandler: %d\n", winch_set);
186 #endif
187 	if (winch_set > 0) {
188 		struct sigaction cwsa;
189 
190 		sigaction(SIGWINCH, NULL, &cwsa);
191 		if (cwsa.sa_handler == owsa.sa_handler) {
192 			sigaction(SIGWINCH, &owsa, NULL);
193 			winch_set = 0;
194 		} else {
195 			/*
196 			 * We're now using the programs WINCH handler,
197 			 * so don't restore the previous one.
198 			 */
199 			winch_set = -1;
200 #ifdef DEBUG
201 			__CTRACE("cwsa.sa_handler = %p\n", cwsa.sa_handler);
202 #endif
203 		}
204 	}
205 }
206 
207 /* To allow both SIGTSTP and endwin() to come back nicely, we provide
208    the following routines. */
209 
210 int
211 __stopwin(void)
212 {
213 #ifdef DEBUG
214 	__CTRACE("__stopwin\n");
215 #endif
216 	if (_cursesi_screen->endwin)
217 		return OK;
218 
219 	/* Get the current terminal state (which the user may have changed). */
220 	(void) tcgetattr(fileno(_cursesi_screen->infd),
221 			 &_cursesi_screen->save_termios);
222 
223 	__restore_stophandler();
224 	__restore_winchhandler();
225 
226 	if (curscr != NULL) {
227 		__unsetattr(0);
228 		__mvcur((int) curscr->cury, (int) curscr->curx,
229 		    (int) curscr->maxy - 1, 0, 0);
230 	}
231 
232 	if (__tc_mo != NULL)
233 		(void) tputs(__tc_mo, 0, __cputchar);
234 
235 	if ((curscr != NULL) && (curscr->flags & __KEYPAD))
236 		(void) tputs(__tc_ke, 0, __cputchar);
237 	(void) tputs(__tc_ve, 0, __cputchar);
238 	(void) tputs(__tc_te, 0, __cputchar);
239 	(void) fflush(_cursesi_screen->outfd);
240 	(void) setvbuf(_cursesi_screen->outfd, NULL, _IOLBF, (size_t) 0);
241 
242 	_cursesi_screen->endwin = 1;
243 
244 	return (tcsetattr(fileno(_cursesi_screen->infd),
245 			  __tcaction ? TCSASOFT | TCSADRAIN : TCSADRAIN,
246 			  &_cursesi_screen->orig_termios) ? ERR : OK);
247 }
248 
249 
250 void
251 __restartwin(void)
252 {
253 	struct winsize win;
254 
255 #ifdef DEBUG
256 	__CTRACE("__restartwin\n");
257 #endif
258 	if (!_cursesi_screen->endwin)
259 		return;
260 
261 	/* Reset the curses SIGTSTP and SIGWINCH signal handlers. */
262 	__set_stophandler();
263 	__set_winchhandler();
264 
265 	/*
266 	 * Check to see if the window size has changed.
267 	 * If the application didn't update LINES and COLS,
268 	 * set the * resized flag to tell getch() to push KEY_RESIZE.
269 	 * Update curscr, stdscr and __virtscr to match the new size.
270 	 */
271 	if (ioctl(fileno(_cursesi_screen->outfd), TIOCGWINSZ, &win) != -1 &&
272 	    win.ws_row != 0 && win.ws_col != 0) {
273 		if (win.ws_row != LINES) {
274 			LINES = win.ws_row;
275 			_cursesi_screen->resized = 1;
276 		}
277 		if (win.ws_col != COLS) {
278 			COLS = win.ws_col;
279 			_cursesi_screen->resized = 1;
280 		}
281 	}
282 	if (curscr->maxy != LINES || curscr->maxx != COLS)
283 		wresize(curscr, LINES, COLS);
284 	if (stdscr->maxy != LINES || stdscr->maxx != COLS)
285 		wresize(stdscr, LINES, COLS);
286 	if (__virtscr->maxy != LINES || __virtscr->maxx != COLS)
287 		wresize(__virtscr, LINES, COLS);
288 
289 	/* save the new "default" terminal state */
290 	(void) tcgetattr(fileno(_cursesi_screen->infd),
291 			 &_cursesi_screen->orig_termios);
292 
293 	/* Reset the terminal state to the mode just before we stopped. */
294 	(void) tcsetattr(fileno(_cursesi_screen->infd),
295 			 __tcaction ? TCSASOFT | TCSADRAIN : TCSADRAIN,
296 			 &_cursesi_screen->save_termios);
297 
298 	/* Restore colours */
299 	__restore_colors();
300 
301 	/* Reset meta */
302 	__restore_meta_state();
303 
304 	/* Restart the screen. */
305 	__startwin(_cursesi_screen);
306 
307 	/* Reset cursor visibility */
308 	__restore_cursor_vis();
309 
310 	/* Repaint the screen. */
311 	wrefresh(curscr);
312 }
313 
314 int
315 def_prog_mode(void)
316 {
317 	if (_cursesi_screen->endwin)
318 		return ERR;
319 
320 	return (tcgetattr(fileno(_cursesi_screen->infd),
321 			  &_cursesi_screen->save_termios) ? ERR : OK);
322 }
323 
324 int
325 reset_prog_mode(void)
326 {
327 
328 	return tcsetattr(fileno(_cursesi_screen->infd),
329 			 __tcaction ? TCSASOFT | TCSADRAIN : TCSADRAIN,
330 			 &_cursesi_screen->save_termios) ? ERR : OK;
331 }
332 
333 int
334 def_shell_mode(void)
335 {
336 	return (tcgetattr(fileno(_cursesi_screen->infd),
337 			  &_cursesi_screen->orig_termios) ? ERR : OK);
338 }
339 
340 int
341 reset_shell_mode(void)
342 {
343 	return (__stopwin());
344 }
345