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