xref: /netbsd-src/lib/libcurses/tstp.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: tstp.c,v 1.44 2018/10/18 07:53:13 roy 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.44 2018/10/18 07:53:13 roy Exp $");
38 #endif
39 #endif				/* not lint */
40 
41 #include <sys/ioctl.h>
42 #include <sys/param.h>
43 
44 #include <errno.h>
45 #include <signal.h>
46 #include <termios.h>
47 #include <unistd.h>
48 
49 #include "curses.h"
50 #include "curses_private.h"
51 
52 static int tstp_set;
53 static int winch_set;
54 
55 static void (*otstpfn)(int) = SIG_DFL;
56 
57 static struct sigaction	owsa;
58 #ifndef TCSASOFT
59 #define TCSASOFT 0
60 #endif
61 
62 /*
63  * stop_signal_handler --
64  *	Handle stop signals.
65  */
66 void
67 __stop_signal_handler(/*ARGSUSED*/int signo)
68 {
69 	sigset_t oset, set;
70 
71 	/*
72 	 * Block window change and timer signals.  The latter is because
73 	 * applications use timers to decide when to repaint the screen.
74 	 */
75 	(void)sigemptyset(&set);
76 	(void)sigaddset(&set, SIGALRM);
77 	(void)sigaddset(&set, SIGWINCH);
78 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
79 
80 	/*
81 	 * End the window, which also resets the terminal state to the
82 	 * original modes.
83 	 */
84 	__stopwin();
85 
86 	/* Unblock SIGTSTP. */
87 	(void)sigemptyset(&set);
88 	(void)sigaddset(&set, SIGTSTP);
89 	(void)sigprocmask(SIG_UNBLOCK, &set, NULL);
90 
91 	/* Stop ourselves. */
92 	(void)kill(0, SIGTSTP);
93 
94 	/* Time passes ... */
95 
96 	/* restart things */
97 	__restartwin();
98 
99 	/* Reset the signals. */
100 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
101 }
102 
103 /*
104  * Set the TSTP handler.
105  */
106 void
107 __set_stophandler(void)
108 {
109 
110 #ifdef DEBUG
111 	__CTRACE(__CTRACE_MISC, "__set_stophandler: %d\n", tstp_set);
112 #endif
113 	if (!tstp_set) {
114 		otstpfn = signal(SIGTSTP, __stop_signal_handler);
115 		tstp_set = 1;
116 	}
117 }
118 
119 /*
120  * Restore the TSTP handler.
121  */
122 void
123 __restore_stophandler(void)
124 {
125 
126 #ifdef DEBUG
127 	__CTRACE(__CTRACE_MISC, "__restore_stophandler: %d\n", tstp_set);
128 #endif
129 	if (tstp_set) {
130 		(void)signal(SIGTSTP, otstpfn);
131 		tstp_set = 0;
132 	}
133 }
134 
135 /*
136  * winch_signal_handler --
137  *	Handle winch signals by pushing KEY_RESIZE into the input stream.
138  */
139 void
140 __winch_signal_handler(/*ARGSUSED*/int signo)
141 {
142 	struct winsize win;
143 
144 	if (ioctl(fileno(_cursesi_screen->outfd), TIOCGWINSZ, &win) != -1 &&
145 	    win.ws_row != 0 && win.ws_col != 0)
146 	{
147 		LINES = win.ws_row;
148 		COLS = win.ws_col;
149 	}
150 	/*
151 	 * If there was a previous handler, call that,
152 	 * otherwise tell getch() to send KEY_RESIZE.
153 	 */
154 	if (owsa.sa_handler != SIG_DFL &&
155 	    owsa.sa_handler != SIG_IGN &&
156 	    owsa.sa_handler != SIG_ERR &&
157 	    owsa.sa_handler != SIG_HOLD)
158 		owsa.sa_handler(signo);
159 	else
160 		_cursesi_screen->resized = 1;
161 }
162 
163 /*
164  * Set the WINCH handler.
165  */
166 void
167 __set_winchhandler(void)
168 {
169 
170 #ifdef DEBUG
171 	__CTRACE(__CTRACE_MISC, "__set_winchhandler: %d\n", winch_set);
172 #endif
173 	if (!winch_set) {
174 		struct sigaction sa;
175 
176 		sa.sa_handler = __winch_signal_handler;
177 		sa.sa_flags = 0;
178 		sigemptyset(&sa.sa_mask);
179 		sigaction(SIGWINCH, &sa, &owsa);
180 		winch_set = 1;
181 #ifdef DEBUG
182 		__CTRACE(__CTRACE_MISC,
183 		    "__set_winchhandler: owsa.sa_handler=%p\n",
184 		    owsa.sa_handler);
185 #endif
186 	}
187 }
188 
189 /*
190  * Restore the WINCH handler.
191  */
192 void
193 __restore_winchhandler(void)
194 {
195 
196 #ifdef DEBUG
197 	__CTRACE(__CTRACE_MISC, "__restore_winchhandler: %d\n", winch_set);
198 #endif
199 	if (winch_set > 0) {
200 		struct sigaction cwsa;
201 
202 		sigaction(SIGWINCH, NULL, &cwsa);
203 		if (cwsa.sa_handler == owsa.sa_handler) {
204 			sigaction(SIGWINCH, &owsa, NULL);
205 			winch_set = 0;
206 		} else {
207 			/*
208 			 * We're now using the programs WINCH handler,
209 			 * so don't restore the previous one.
210 			 */
211 			winch_set = -1;
212 #ifdef DEBUG
213 			__CTRACE(__CTRACE_MISC, "cwsa.sa_handler = %p\n",
214 			    cwsa.sa_handler);
215 #endif
216 		}
217 	}
218 }
219 
220 /* To allow both SIGTSTP and endwin() to come back nicely, we provide
221    the following routines. */
222 
223 int
224 __stopwin(void)
225 {
226 
227 #ifdef DEBUG
228 	__CTRACE(__CTRACE_MISC, "__stopwin\n");
229 #endif
230 	if (_cursesi_screen == NULL)
231 		return ERR;
232 	if (_cursesi_screen->endwin)
233 		return OK;
234 
235 	/* Get the current terminal state (which the user may have changed). */
236 	(void)tcgetattr(fileno(_cursesi_screen->infd),
237 			&_cursesi_screen->save_termios);
238 
239 	__restore_stophandler();
240 	__restore_winchhandler();
241 
242 	if (curscr != NULL) {
243 		__unsetattr(0);
244 		__mvcur((int)curscr->cury, (int)curscr->curx,
245 		    (int)curscr->maxy - 1, 0, 0);
246 	}
247 
248 	if (meta_on != NULL)
249 		(void)tputs(meta_on, 0, __cputchar);
250 
251 	if ((curscr != NULL) && (curscr->flags & __KEYPAD))
252 		(void)tputs(keypad_local, 0, __cputchar);
253 	(void)tputs(cursor_normal, 0, __cputchar);
254 	(void)tputs(exit_ca_mode, 0, __cputchar);
255 	(void)fflush(_cursesi_screen->outfd);
256 #ifdef BSD
257 	(void)setvbuf(_cursesi_screen->outfd, NULL, _IOLBF, 0);
258 #endif
259 
260 	_cursesi_screen->endwin = 1;
261 
262 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
263 	    &_cursesi_screen->orig_termios) ? ERR : OK;
264 }
265 
266 void
267 __restartwin(void)
268 {
269 	struct winsize win;
270 	int nlines, ncols;
271 
272 #ifdef DEBUG
273 	__CTRACE(__CTRACE_MISC, "__restartwin\n");
274 #endif
275 	if (!_cursesi_screen->endwin)
276 		return;
277 
278 	/* Reset the curses SIGTSTP and SIGWINCH signal handlers. */
279 	__set_stophandler();
280 	__set_winchhandler();
281 
282 	/*
283 	 * Check to see if the window size has changed.
284 	 * If the application didn't update LINES and COLS,
285 	 * set the * resized flag to tell getch() to push KEY_RESIZE.
286 	 * Update curscr (which also updates __virtscr) and stdscr
287 	 * to match the new size.
288 	 */
289 	if (ioctl(fileno(_cursesi_screen->outfd), TIOCGWINSZ, &win) != -1 &&
290 	    win.ws_row != 0 && win.ws_col != 0)
291 	{
292 		if (win.ws_row != LINES) {
293 			LINES = win.ws_row;
294 			_cursesi_screen->resized = 1;
295 		}
296 		if (win.ws_col != COLS) {
297 			COLS = win.ws_col;
298 			_cursesi_screen->resized = 1;
299 		}
300 	}
301 	/*
302 	 * We need to make local copies of LINES and COLS, otherwise we
303 	 * could lose if they are changed between wresize() calls.
304 	 */
305 	nlines = LINES;
306 	ncols = COLS;
307 	if (curscr->maxy != nlines || curscr->maxx != ncols)
308 		wresize(curscr, nlines, ncols);
309 	if (stdscr->maxy != nlines || stdscr->maxx != ncols)
310 		wresize(stdscr, nlines, ncols);
311 
312 	/* save the new "default" terminal state */
313 	(void)tcgetattr(fileno(_cursesi_screen->infd),
314 			&_cursesi_screen->orig_termios);
315 
316 	/* Reset the terminal state to the mode just before we stopped. */
317 	(void)tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
318 			&_cursesi_screen->save_termios);
319 
320 	/* Restore colours */
321 	__restore_colors();
322 
323 	/* Reset meta */
324 	__restore_meta_state();
325 
326 	/* Restart the screen. */
327 	__startwin(_cursesi_screen);
328 
329 	/* Reset cursor visibility */
330 	__restore_cursor_vis();
331 
332 	/* Repaint the screen. */
333 	wrefresh(curscr);
334 }
335 
336 int
337 def_prog_mode(void)
338 {
339 
340 	if (_cursesi_screen->endwin)
341 		return ERR;
342 
343 	return tcgetattr(fileno(_cursesi_screen->infd),
344 			 &_cursesi_screen->save_termios) ? ERR : OK;
345 }
346 
347 int
348 reset_prog_mode(void)
349 {
350 
351 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
352 			 &_cursesi_screen->save_termios) ? ERR : OK;
353 }
354 
355 int
356 def_shell_mode(void)
357 {
358 
359 	return tcgetattr(fileno(_cursesi_screen->infd),
360 			 &_cursesi_screen->orig_termios) ? ERR : OK;
361 }
362 
363 int
364 reset_shell_mode(void)
365 {
366 
367 	return __stopwin();
368 }
369