| 1 | /* |
| 2 | * Copyright (c) 1988 Mark Nudleman |
| 3 | * Copyright (c) 1988 Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms are permitted |
| 7 | * provided that the above copyright notice and this paragraph are |
| 8 | * duplicated in all such forms and that any documentation, |
| 9 | * advertising materials, and other materials related to such |
| 10 | * distribution and use acknowledge that the software was developed |
| 11 | * by Mark Nudleman and the University of California, Berkeley. The |
| 12 | * name of Mark Nudleman or the |
| 13 | * University may not be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 17 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 18 | */ |
| 19 | |
| 20 | #ifndef lint |
| 21 | static char sccsid[] = "@(#)signal.c 5.5 (Berkeley) %G%"; |
| 22 | #endif /* not lint */ |
| 23 | |
| 24 | /* |
| 25 | * Routines dealing with signals. |
| 26 | * |
| 27 | * A signal usually merely causes a bit to be set in the "signals" word. |
| 28 | * At some convenient time, the mainline code checks to see if any |
| 29 | * signals need processing by calling psignal(). |
| 30 | * If we happen to be reading from a file [in iread()] at the time |
| 31 | * the signal is received, we call intread to interrupt the iread. |
| 32 | */ |
| 33 | |
| 34 | #include <less.h> |
| 35 | #include <signal.h> |
| 36 | |
| 37 | /* |
| 38 | * "sigs" contains bits indicating signals which need to be processed. |
| 39 | */ |
| 40 | int sigs; |
| 41 | |
| 42 | #ifdef SIGTSTP |
| 43 | #define S_STOP 02 |
| 44 | #endif |
| 45 | #if defined(SIGWINCH) || defined(SIGWIND) |
| 46 | #define S_WINCH 04 |
| 47 | #endif |
| 48 | |
| 49 | extern int sc_width, sc_height; |
| 50 | extern int screen_trashed; |
| 51 | extern int lnloop; |
| 52 | extern int linenums; |
| 53 | extern int scroll; |
| 54 | extern int reading; |
| 55 | |
| 56 | #ifdef SIGTSTP |
| 57 | /* |
| 58 | * "Stop" (^Z) signal handler. |
| 59 | */ |
| 60 | static |
| 61 | stop() |
| 62 | { |
| 63 | (void)signal(SIGTSTP, stop); |
| 64 | sigs |= S_STOP; |
| 65 | if (reading) |
| 66 | intread(); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | #ifdef SIGWINCH |
| 71 | /* |
| 72 | * "Window" change handler |
| 73 | */ |
| 74 | winch() |
| 75 | { |
| 76 | (void)signal(SIGWINCH, winch); |
| 77 | sigs |= S_WINCH; |
| 78 | if (reading) |
| 79 | intread(); |
| 80 | } |
| 81 | #else |
| 82 | #ifdef SIGWIND |
| 83 | /* |
| 84 | * "Window" change handler |
| 85 | */ |
| 86 | winch() |
| 87 | { |
| 88 | (void)signal(SIGWIND, winch); |
| 89 | sigs |= S_WINCH; |
| 90 | if (reading) |
| 91 | intread(); |
| 92 | } |
| 93 | #endif |
| 94 | #endif |
| 95 | |
| 96 | /* |
| 97 | * Set up the signal handlers. |
| 98 | */ |
| 99 | init_signals(on) |
| 100 | int on; |
| 101 | { |
| 102 | int quit(); |
| 103 | |
| 104 | if (on) |
| 105 | { |
| 106 | /* |
| 107 | * Set signal handlers. |
| 108 | */ |
| 109 | (void)signal(SIGINT, quit); |
| 110 | #ifdef SIGTSTP |
| 111 | (void)signal(SIGTSTP, stop); |
| 112 | #endif |
| 113 | #ifdef SIGWINCH |
| 114 | (void)signal(SIGWINCH, winch); |
| 115 | #else |
| 116 | #ifdef SIGWIND |
| 117 | (void)signal(SIGWIND, winch); |
| 118 | #endif |
| 119 | #endif |
| 120 | } else |
| 121 | { |
| 122 | /* |
| 123 | * Restore signals to defaults. |
| 124 | */ |
| 125 | (void)signal(SIGINT, SIG_DFL); |
| 126 | #ifdef SIGTSTP |
| 127 | (void)signal(SIGTSTP, SIG_DFL); |
| 128 | #endif |
| 129 | #ifdef SIGWINCH |
| 130 | (void)signal(SIGWINCH, SIG_IGN); |
| 131 | #endif |
| 132 | #ifdef SIGWIND |
| 133 | (void)signal(SIGWIND, SIG_IGN); |
| 134 | #endif |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Process any signals we have received. |
| 140 | * A received signal cause a bit to be set in "sigs". |
| 141 | */ |
| 142 | psignals() |
| 143 | { |
| 144 | register int tsignals; |
| 145 | |
| 146 | if ((tsignals = sigs) == 0) |
| 147 | return; |
| 148 | sigs = 0; |
| 149 | |
| 150 | #ifdef S_WINCH |
| 151 | if (tsignals & S_WINCH) |
| 152 | { |
| 153 | int old_width, old_height; |
| 154 | /* |
| 155 | * Re-execute get_term() to read the new window size. |
| 156 | */ |
| 157 | old_width = sc_width; |
| 158 | old_height = sc_height; |
| 159 | get_term(); |
| 160 | if (sc_width != old_width || sc_height != old_height) |
| 161 | { |
| 162 | scroll = (sc_height + 1) / 2; |
| 163 | screen_trashed = 1; |
| 164 | } |
| 165 | } |
| 166 | #endif |
| 167 | #ifdef SIGTSTP |
| 168 | if (tsignals & S_STOP) |
| 169 | { |
| 170 | /* |
| 171 | * Clean up the terminal. |
| 172 | */ |
| 173 | #ifdef SIGTTOU |
| 174 | (void)signal(SIGTTOU, SIG_IGN); |
| 175 | #endif |
| 176 | lower_left(); |
| 177 | clear_eol(); |
| 178 | deinit(); |
| 179 | (void)flush(); |
| 180 | raw_mode(0); |
| 181 | #ifdef SIGTTOU |
| 182 | (void)signal(SIGTTOU, SIG_DFL); |
| 183 | #endif |
| 184 | (void)signal(SIGTSTP, SIG_DFL); |
| 185 | (void)kill(getpid(), SIGTSTP); |
| 186 | /* |
| 187 | * ... Bye bye. ... |
| 188 | * Hopefully we'll be back later and resume here... |
| 189 | * Reset the terminal and arrange to repaint the |
| 190 | * screen when we get back to the main command loop. |
| 191 | */ |
| 192 | (void)signal(SIGTSTP, stop); |
| 193 | raw_mode(1); |
| 194 | init(); |
| 195 | screen_trashed = 1; |
| 196 | } |
| 197 | #endif |
| 198 | } |