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