1 /* $NetBSD: tstp.c,v 1.30 2003/08/07 16:44:25 agc 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.30 2003/08/07 16:44:25 agc Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 #include <errno.h> 42 #include <signal.h> 43 #include <termios.h> 44 #include <unistd.h> 45 46 #include "curses.h" 47 #include "curses_private.h" 48 49 /* 50 * stop_signal_handler -- 51 * Handle stop signals. 52 */ 53 void 54 __stop_signal_handler(/*ARGSUSED*/int signo) 55 { 56 sigset_t oset, set; 57 58 /* 59 * Block window change and timer signals. The latter is because 60 * applications use timers to decide when to repaint the screen. 61 */ 62 (void) sigemptyset(&set); 63 (void) sigaddset(&set, SIGALRM); 64 (void) sigaddset(&set, SIGWINCH); 65 (void) sigprocmask(SIG_BLOCK, &set, &oset); 66 67 /* 68 * End the window, which also resets the terminal state to the 69 * original modes. 70 */ 71 __stopwin(); 72 73 /* Unblock SIGTSTP. */ 74 (void) sigemptyset(&set); 75 (void) sigaddset(&set, SIGTSTP); 76 (void) sigprocmask(SIG_UNBLOCK, &set, NULL); 77 78 /* Stop ourselves. */ 79 (void) kill(0, SIGTSTP); 80 81 /* Time passes ... */ 82 83 /* restart things */ 84 __restartwin(); 85 86 /* Reset the signals. */ 87 (void) sigprocmask(SIG_SETMASK, &oset, NULL); 88 } 89 90 static void (*otstpfn) 91 __P((int)) = SIG_DFL; 92 93 /* 94 * Set the TSTP handler. 95 */ 96 void 97 __set_stophandler(void) 98 { 99 otstpfn = signal(SIGTSTP, __stop_signal_handler); 100 } 101 102 /* 103 * Restore the TSTP handler. 104 */ 105 void 106 __restore_stophandler(void) 107 { 108 (void) signal(SIGTSTP, otstpfn); 109 } 110 111 112 /* To allow both SIGTSTP and endwin() to come back nicely, we provide 113 the following routines. */ 114 115 int 116 __stopwin(void) 117 { 118 if (_cursesi_screen->endwin) 119 return OK; 120 121 /* Get the current terminal state (which the user may have changed). */ 122 (void) tcgetattr(fileno(_cursesi_screen->infd), 123 &_cursesi_screen->save_termios); 124 125 __restore_stophandler(); 126 127 if (curscr != NULL) { 128 __unsetattr(0); 129 __mvcur((int) curscr->cury, (int) curscr->curx, (int) curscr->maxy - 1, 0, 0); 130 } 131 132 if (__tc_mo != NULL) 133 (void) tputs(__tc_mo, 0, __cputchar); 134 135 if ((curscr != NULL) && (curscr->flags & __KEYPAD)) 136 (void) tputs(__tc_ke, 0, __cputchar); 137 (void) tputs(__tc_ve, 0, __cputchar); 138 (void) tputs(__tc_te, 0, __cputchar); 139 (void) fflush(_cursesi_screen->outfd); 140 (void) setvbuf(_cursesi_screen->outfd, NULL, _IOLBF, (size_t) 0); 141 142 _cursesi_screen->endwin = 1; 143 144 return (tcsetattr(fileno(_cursesi_screen->infd), 145 __tcaction ? TCSASOFT | TCSADRAIN : TCSADRAIN, 146 &_cursesi_screen->orig_termios) ? ERR : OK); 147 } 148 149 150 void 151 __restartwin(void) 152 { 153 if (!_cursesi_screen->endwin) 154 return; 155 156 /* Reset the curses SIGTSTP signal handler. */ 157 __set_stophandler(); 158 159 /* save the new "default" terminal state */ 160 (void) tcgetattr(fileno(_cursesi_screen->infd), 161 &_cursesi_screen->orig_termios); 162 163 /* Reset the terminal state to the mode just before we stopped. */ 164 (void) tcsetattr(fileno(_cursesi_screen->infd), 165 __tcaction ? TCSASOFT | TCSADRAIN : TCSADRAIN, 166 &_cursesi_screen->save_termios); 167 168 /* Restore colours */ 169 __restore_colors(); 170 171 /* Reset meta */ 172 __restore_meta_state(); 173 174 /* Restart the screen. */ 175 __startwin(_cursesi_screen); 176 177 /* Reset cursor visibility */ 178 __restore_cursor_vis(); 179 180 /* Repaint the screen. */ 181 wrefresh(curscr); 182 } 183 184 int 185 def_prog_mode(void) 186 { 187 if (_cursesi_screen->endwin) 188 return ERR; 189 190 return (tcgetattr(fileno(_cursesi_screen->infd), 191 &_cursesi_screen->save_termios) ? ERR : OK); 192 } 193 194 int 195 reset_prog_mode(void) 196 { 197 198 return tcsetattr(fileno(_cursesi_screen->infd), 199 __tcaction ? TCSASOFT | TCSADRAIN : TCSADRAIN, 200 &_cursesi_screen->save_termios) ? ERR : 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