1 /* $NetBSD: tty.c,v 1.17 2003/08/07 11:14:42 agc Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 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[] = "@(#)tty.c 8.2 (Berkeley) 6/6/93"; 36 #else 37 __RCSID("$NetBSD: tty.c,v 1.17 2003/08/07 11:14:42 agc Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 /* 42 * Mail -- a mail program 43 * 44 * Generally useful tty stuff. 45 */ 46 47 #include "rcv.h" 48 #include "extern.h" 49 50 static cc_t c_erase; /* Current erase char */ 51 static cc_t c_kill; /* Current kill char */ 52 static jmp_buf rewrite; /* Place to go when continued */ 53 static jmp_buf intjmp; /* Place to go when interrupted */ 54 #ifndef TIOCSTI 55 static int ttyset; /* We must now do erase/kill */ 56 #endif 57 58 /* 59 * Read all relevant header fields. 60 */ 61 62 int 63 grabh(struct header *hp, int gflags) 64 { 65 struct termios ttybuf; 66 sig_t saveint; 67 sig_t savetstp; 68 sig_t savettou; 69 sig_t savettin; 70 int errs; 71 #ifndef TIOCSTI 72 sig_t savequit; 73 #else 74 # ifdef TIOCEXT 75 int extproc, flag; 76 # endif /* TIOCEXT */ 77 #endif /* TIOCSTI */ 78 79 #ifdef __GNUC__ 80 /* Avoid longjmp clobbering */ 81 # if defined(TIOCSTI) && defined(TIOCEXT) 82 (void)&extproc; 83 # endif 84 (void)&saveint; 85 #endif /* __GNUC__ */ 86 87 savetstp = signal(SIGTSTP, SIG_DFL); 88 savettou = signal(SIGTTOU, SIG_DFL); 89 savettin = signal(SIGTTIN, SIG_DFL); 90 errs = 0; 91 #ifndef TIOCSTI 92 ttyset = 0; 93 #endif 94 if (tcgetattr(fileno(stdin), &ttybuf) < 0) { 95 warn("tcgetattr"); 96 return(-1); 97 } 98 c_erase = ttybuf.c_cc[VERASE]; 99 c_kill = ttybuf.c_cc[VKILL]; 100 #ifndef TIOCSTI 101 ttybuf.c_cc[VERASE] = _POSIX_VDISABLE; 102 ttybuf.c_cc[VKILL] = _POSIX_VDISABLE; 103 if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL) 104 signal(SIGINT, SIG_DFL); 105 if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL) 106 signal(SIGQUIT, SIG_DFL); 107 #else 108 # ifdef TIOCEXT 109 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0); 110 if (extproc) { 111 flag = 0; 112 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0) 113 warn("TIOCEXT: off"); 114 } 115 # endif /* TIOCEXT */ 116 if (setjmp(intjmp)) 117 goto out; 118 saveint = signal(SIGINT, ttyint); 119 #endif 120 if (gflags & GTO) { 121 #ifndef TIOCSTI 122 if (!ttyset && hp->h_to != NULL) 123 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 124 #endif 125 hp->h_to = 126 extract(readtty("To: ", detract(hp->h_to, 0)), GTO); 127 } 128 if (gflags & GSUBJECT) { 129 #ifndef TIOCSTI 130 if (!ttyset && hp->h_subject != NULL) 131 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 132 #endif 133 hp->h_subject = readtty("Subject: ", hp->h_subject); 134 } 135 if (gflags & GCC) { 136 #ifndef TIOCSTI 137 if (!ttyset && hp->h_cc != NULL) 138 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 139 #endif 140 hp->h_cc = 141 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC); 142 } 143 if (gflags & GBCC) { 144 #ifndef TIOCSTI 145 if (!ttyset && hp->h_bcc != NULL) 146 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 147 #endif 148 hp->h_bcc = 149 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC); 150 } 151 out: 152 signal(SIGTSTP, savetstp); 153 signal(SIGTTOU, savettou); 154 signal(SIGTTIN, savettin); 155 #ifndef TIOCSTI 156 ttybuf.c_cc[VERASE] = c_erase; 157 ttybuf.c_cc[VKILL] = c_kill; 158 if (ttyset) 159 tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 160 signal(SIGQUIT, savequit); 161 #else 162 # ifdef TIOCEXT 163 if (extproc) { 164 flag = 1; 165 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0) 166 warn("TIOCEXT: on"); 167 } 168 # endif /* TIOCEXT */ 169 #endif 170 signal(SIGINT, saveint); 171 return(errs); 172 } 173 174 /* 175 * Read up a header from standard input. 176 * The source string has the preliminary contents to 177 * be read. 178 * 179 */ 180 181 char * 182 readtty(char pr[], char src[]) 183 { 184 char ch, canonb[BUFSIZ]; 185 int c; 186 char *cp, *cp2; 187 #if __GNUC__ 188 /* Avoid longjmp clobbering */ 189 (void)&c; 190 (void)&cp2; 191 #endif 192 193 fputs(pr, stdout); 194 fflush(stdout); 195 if (src != NULL && strlen(src) > BUFSIZ - 2) { 196 printf("too long to edit\n"); 197 return(src); 198 } 199 #ifndef TIOCSTI 200 if (src != NULL) 201 cp = copy(src, canonb); 202 else 203 cp = copy("", canonb); 204 fputs(canonb, stdout); 205 fflush(stdout); 206 #else 207 cp = src == NULL ? "" : src; 208 while ((c = *cp++) != '\0') { 209 if ((c_erase != _POSIX_VDISABLE && c == c_erase) || 210 (c_kill != _POSIX_VDISABLE && c == c_kill)) { 211 ch = '\\'; 212 ioctl(0, TIOCSTI, &ch); 213 } 214 ch = c; 215 ioctl(0, TIOCSTI, &ch); 216 } 217 cp = canonb; 218 *cp = 0; 219 #endif 220 cp2 = cp; 221 while (cp2 < canonb + BUFSIZ) 222 *cp2++ = 0; 223 cp2 = cp; 224 if (setjmp(rewrite)) 225 goto redo; 226 signal(SIGTSTP, ttystop); 227 signal(SIGTTOU, ttystop); 228 signal(SIGTTIN, ttystop); 229 clearerr(stdin); 230 while (cp2 < canonb + BUFSIZ) { 231 c = getc(stdin); 232 if (c == EOF || c == '\n') 233 break; 234 *cp2++ = c; 235 } 236 *cp2 = 0; 237 signal(SIGTSTP, SIG_DFL); 238 signal(SIGTTOU, SIG_DFL); 239 signal(SIGTTIN, SIG_DFL); 240 if (c == EOF && ferror(stdin)) { 241 redo: 242 cp = strlen(canonb) > 0 ? canonb : NULL; 243 clearerr(stdin); 244 return(readtty(pr, cp)); 245 } 246 #ifndef TIOCSTI 247 if (cp == NULL || *cp == '\0') 248 return(src); 249 cp2 = cp; 250 if (!ttyset) 251 return(strlen(canonb) > 0 ? savestr(canonb) : NULL); 252 while (*cp != '\0') { 253 c = *cp++; 254 if (c_erase != _POSIX_VDISABLE && c == c_erase) { 255 if (cp2 == canonb) 256 continue; 257 if (cp2[-1] == '\\') { 258 cp2[-1] = c; 259 continue; 260 } 261 cp2--; 262 continue; 263 } 264 if (c_kill != _POSIX_VDISABLE && c == c_kill) { 265 if (cp2 == canonb) 266 continue; 267 if (cp2[-1] == '\\') { 268 cp2[-1] = c; 269 continue; 270 } 271 cp2 = canonb; 272 continue; 273 } 274 *cp2++ = c; 275 } 276 *cp2 = '\0'; 277 #endif 278 if (equal("", canonb)) 279 return(NULL); 280 return(savestr(canonb)); 281 } 282 283 /* 284 * Receipt continuation. 285 */ 286 void 287 ttystop(int s) 288 { 289 sig_t old_action = signal(s, SIG_DFL); 290 sigset_t nset; 291 292 sigemptyset(&nset); 293 sigaddset(&nset, s); 294 sigprocmask(SIG_BLOCK, &nset, NULL); 295 kill(0, s); 296 sigprocmask(SIG_UNBLOCK, &nset, NULL); 297 signal(s, old_action); 298 longjmp(rewrite, 1); 299 } 300 301 /*ARGSUSED*/ 302 void 303 ttyint(int s) 304 { 305 longjmp(intjmp, 1); 306 } 307