xref: /netbsd-src/lib/libcurses/tstp.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: tstp.c,v 1.28 2003/05/28 20:19:42 dsl 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)tstp.c	8.3 (Berkeley) 5/4/94";
40 #else
41 __RCSID("$NetBSD: tstp.c,v 1.28 2003/05/28 20:19:42 dsl Exp $");
42 #endif
43 #endif				/* not lint */
44 
45 #include <errno.h>
46 #include <signal.h>
47 #include <termios.h>
48 #include <unistd.h>
49 
50 #include "curses.h"
51 #include "curses_private.h"
52 
53 /*
54  * stop_signal_handler --
55  *	Handle stop signals.
56  */
57 void
58 __stop_signal_handler(/*ARGSUSED*/int signo)
59 {
60 	sigset_t oset, set;
61 
62 	/*
63 	 * Block window change and timer signals.  The latter is because
64 	 * applications use timers to decide when to repaint the screen.
65 	 */
66 	(void) sigemptyset(&set);
67 	(void) sigaddset(&set, SIGALRM);
68 	(void) sigaddset(&set, SIGWINCH);
69 	(void) sigprocmask(SIG_BLOCK, &set, &oset);
70 
71 	/*
72 	 * End the window, which also resets the terminal state to the
73 	 * original modes.
74 	 */
75 	__stopwin();
76 
77 	/* Unblock SIGTSTP. */
78 	(void) sigemptyset(&set);
79 	(void) sigaddset(&set, SIGTSTP);
80 	(void) sigprocmask(SIG_UNBLOCK, &set, NULL);
81 
82 	/* Stop ourselves. */
83 	(void) kill(0, SIGTSTP);
84 
85 	/* Time passes ... */
86 
87 	/* restart things */
88 	__restartwin();
89 
90 	/* Reset the signals. */
91 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
92 }
93 
94 static void (*otstpfn)
95 __P((int)) = SIG_DFL;
96 
97 /*
98  * Set the TSTP handler.
99  */
100 void
101 __set_stophandler(void)
102 {
103 	otstpfn = signal(SIGTSTP, __stop_signal_handler);
104 }
105 
106 /*
107  * Restore the TSTP handler.
108  */
109 void
110 __restore_stophandler(void)
111 {
112 	(void) signal(SIGTSTP, otstpfn);
113 }
114 
115 
116 /* To allow both SIGTSTP and endwin() to come back nicely, we provide
117    the following routines. */
118 
119 int
120 __stopwin(void)
121 {
122 	if (_cursesi_screen->endwin)
123 		return OK;
124 
125 	/* Get the current terminal state (which the user may have changed). */
126 	(void) tcgetattr(fileno(_cursesi_screen->infd),
127 			 &_cursesi_screen->save_termios);
128 
129 	__restore_stophandler();
130 
131 	if (curscr != NULL) {
132 		__unsetattr(0);
133 		__mvcur((int) curscr->cury, (int) curscr->curx, (int) curscr->maxy - 1, 0, 0);
134 	}
135 
136 	if (__tc_mo != NULL)
137 		(void) tputs(__tc_mo, 0, __cputchar);
138 
139 	if ((curscr != NULL) && (curscr->flags & __KEYPAD))
140 		(void) tputs(__tc_ke, 0, __cputchar);
141 	(void) tputs(__tc_ve, 0, __cputchar);
142 	(void) tputs(__tc_te, 0, __cputchar);
143 	(void) fflush(_cursesi_screen->outfd);
144 	(void) setvbuf(_cursesi_screen->outfd, NULL, _IOLBF, (size_t) 0);
145 
146 	_cursesi_screen->endwin = 1;
147 
148 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
149 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
150 			  &_cursesi_screen->orig_termios) ? ERR : OK);
151 }
152 
153 
154 void
155 __restartwin(void)
156 {
157 	if (!_cursesi_screen->endwin)
158 		return;
159 
160 	/* Reset the curses SIGTSTP signal handler. */
161 	__set_stophandler();
162 
163 	/* save the new "default" terminal state */
164 	(void) tcgetattr(fileno(_cursesi_screen->infd),
165 			 &_cursesi_screen->orig_termios);
166 
167 	/* Reset the terminal state to the mode just before we stopped. */
168 	(void) tcsetattr(fileno(_cursesi_screen->infd),
169 			 __tcaction ?
170 			 TCSASOFT | TCSADRAIN : TCSADRAIN,
171 			 &_cursesi_screen->save_termios);
172 
173 	/* Restore colours */
174 	__restore_colors();
175 
176 	/* Reset meta */
177 	__restore_meta_state();
178 
179 	/* Restart the screen. */
180 	__startwin(_cursesi_screen);
181 
182 	/* Reset cursor visibility */
183 	__restore_cursor_vis();
184 
185 	/* Repaint the screen. */
186 	wrefresh(curscr);
187 }
188 
189 int
190 def_prog_mode(void)
191 {
192 	return (tcgetattr(fileno(_cursesi_screen->infd),
193 			  &_cursesi_screen->save_termios) ? ERR : OK);
194 }
195 
196 int
197 reset_prog_mode(void)
198 {
199 	__restartwin();
200 	return(OK);
201 }
202 
203 int
204 def_shell_mode(void)
205 {
206 	return (tcgetattr(fileno(_cursesi_screen->infd),
207 			  &_cursesi_screen->orig_termios) ? ERR : OK);
208 }
209 
210 int
211 reset_shell_mode(void)
212 {
213 	return (__stopwin());
214 }
215