1 /* $NetBSD: tstp.c,v 1.45 2021/09/06 07:03:50 rin 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.45 2021/09/06 07:03:50 rin 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 __CTRACE(__CTRACE_MISC, "__set_stophandler: %d\n", tstp_set); 111 if (!tstp_set) { 112 otstpfn = signal(SIGTSTP, __stop_signal_handler); 113 tstp_set = 1; 114 } 115 } 116 117 /* 118 * Restore the TSTP handler. 119 */ 120 void 121 __restore_stophandler(void) 122 { 123 124 __CTRACE(__CTRACE_MISC, "__restore_stophandler: %d\n", tstp_set); 125 if (tstp_set) { 126 (void)signal(SIGTSTP, otstpfn); 127 tstp_set = 0; 128 } 129 } 130 131 /* 132 * winch_signal_handler -- 133 * Handle winch signals by pushing KEY_RESIZE into the input stream. 134 */ 135 void 136 __winch_signal_handler(/*ARGSUSED*/int signo) 137 { 138 struct winsize win; 139 140 if (ioctl(fileno(_cursesi_screen->outfd), TIOCGWINSZ, &win) != -1 && 141 win.ws_row != 0 && win.ws_col != 0) 142 { 143 LINES = win.ws_row; 144 COLS = win.ws_col; 145 } 146 /* 147 * If there was a previous handler, call that, 148 * otherwise tell getch() to send KEY_RESIZE. 149 */ 150 if (owsa.sa_handler != SIG_DFL && 151 owsa.sa_handler != SIG_IGN && 152 owsa.sa_handler != SIG_ERR && 153 owsa.sa_handler != SIG_HOLD) 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 __CTRACE(__CTRACE_MISC, "__set_winchhandler: %d\n", winch_set); 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 __CTRACE(__CTRACE_MISC, 176 "__set_winchhandler: owsa.sa_handler=%p\n", 177 owsa.sa_handler); 178 } 179 } 180 181 /* 182 * Restore the WINCH handler. 183 */ 184 void 185 __restore_winchhandler(void) 186 { 187 188 __CTRACE(__CTRACE_MISC, "__restore_winchhandler: %d\n", winch_set); 189 if (winch_set > 0) { 190 struct sigaction cwsa; 191 192 sigaction(SIGWINCH, NULL, &cwsa); 193 if (cwsa.sa_handler == owsa.sa_handler) { 194 sigaction(SIGWINCH, &owsa, NULL); 195 winch_set = 0; 196 } else { 197 /* 198 * We're now using the programs WINCH handler, 199 * so don't restore the previous one. 200 */ 201 winch_set = -1; 202 __CTRACE(__CTRACE_MISC, "cwsa.sa_handler = %p\n", 203 cwsa.sa_handler); 204 } 205 } 206 } 207 208 /* To allow both SIGTSTP and endwin() to come back nicely, we provide 209 the following routines. */ 210 211 int 212 __stopwin(void) 213 { 214 215 __CTRACE(__CTRACE_MISC, "__stopwin\n"); 216 if (_cursesi_screen == NULL) 217 return ERR; 218 if (_cursesi_screen->endwin) 219 return OK; 220 221 /* Get the current terminal state (which the user may have changed). */ 222 (void)tcgetattr(fileno(_cursesi_screen->infd), 223 &_cursesi_screen->save_termios); 224 225 __restore_stophandler(); 226 __restore_winchhandler(); 227 228 if (curscr != NULL) { 229 __unsetattr(0); 230 __mvcur((int)curscr->cury, (int)curscr->curx, 231 (int)curscr->maxy - 1, 0, 0); 232 } 233 234 if (meta_on != NULL) 235 (void)tputs(meta_on, 0, __cputchar); 236 237 if ((curscr != NULL) && (curscr->flags & __KEYPAD)) 238 (void)tputs(keypad_local, 0, __cputchar); 239 (void)tputs(cursor_normal, 0, __cputchar); 240 (void)tputs(exit_ca_mode, 0, __cputchar); 241 (void)fflush(_cursesi_screen->outfd); 242 #ifdef BSD 243 (void)setvbuf(_cursesi_screen->outfd, NULL, _IOLBF, 0); 244 #endif 245 246 _cursesi_screen->endwin = 1; 247 248 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 249 &_cursesi_screen->orig_termios) ? ERR : OK; 250 } 251 252 void 253 __restartwin(void) 254 { 255 struct winsize win; 256 int nlines, ncols; 257 258 __CTRACE(__CTRACE_MISC, "__restartwin\n"); 259 if (!_cursesi_screen->endwin) 260 return; 261 262 /* Reset the curses SIGTSTP and SIGWINCH signal handlers. */ 263 __set_stophandler(); 264 __set_winchhandler(); 265 266 /* 267 * Check to see if the window size has changed. 268 * If the application didn't update LINES and COLS, 269 * set the * resized flag to tell getch() to push KEY_RESIZE. 270 * Update curscr (which also updates __virtscr) and stdscr 271 * to match the new size. 272 */ 273 if (ioctl(fileno(_cursesi_screen->outfd), TIOCGWINSZ, &win) != -1 && 274 win.ws_row != 0 && win.ws_col != 0) 275 { 276 if (win.ws_row != LINES) { 277 LINES = win.ws_row; 278 _cursesi_screen->resized = 1; 279 } 280 if (win.ws_col != COLS) { 281 COLS = win.ws_col; 282 _cursesi_screen->resized = 1; 283 } 284 } 285 /* 286 * We need to make local copies of LINES and COLS, otherwise we 287 * could lose if they are changed between wresize() calls. 288 */ 289 nlines = LINES; 290 ncols = COLS; 291 if (curscr->maxy != nlines || curscr->maxx != ncols) 292 wresize(curscr, nlines, ncols); 293 if (stdscr->maxy != nlines || stdscr->maxx != ncols) 294 wresize(stdscr, nlines, ncols); 295 296 /* save the new "default" terminal state */ 297 (void)tcgetattr(fileno(_cursesi_screen->infd), 298 &_cursesi_screen->orig_termios); 299 300 /* Reset the terminal state to the mode just before we stopped. */ 301 (void)tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 302 &_cursesi_screen->save_termios); 303 304 /* Restore colours */ 305 __restore_colors(); 306 307 /* Reset meta */ 308 __restore_meta_state(); 309 310 /* Restart the screen. */ 311 __startwin(_cursesi_screen); 312 313 /* Reset cursor visibility */ 314 __restore_cursor_vis(); 315 316 /* Repaint the screen. */ 317 wrefresh(curscr); 318 } 319 320 int 321 def_prog_mode(void) 322 { 323 324 if (_cursesi_screen->endwin) 325 return ERR; 326 327 return tcgetattr(fileno(_cursesi_screen->infd), 328 &_cursesi_screen->save_termios) ? ERR : OK; 329 } 330 331 int 332 reset_prog_mode(void) 333 { 334 335 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 336 &_cursesi_screen->save_termios) ? ERR : OK; 337 } 338 339 int 340 def_shell_mode(void) 341 { 342 343 return tcgetattr(fileno(_cursesi_screen->infd), 344 &_cursesi_screen->orig_termios) ? ERR : OK; 345 } 346 347 int 348 reset_shell_mode(void) 349 { 350 351 return __stopwin(); 352 } 353