1 /* $OpenBSD: tty.c,v 1.12 2001/06/23 23:04:23 millert Exp $ */ 2 /* $NetBSD: tty.c,v 1.7 1997/07/09 05:25:46 mikel Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 4/20/95"; 40 #else 41 static char rcsid[] = "$OpenBSD: tty.c,v 1.12 2001/06/23 23:04:23 millert Exp $"; 42 #endif 43 #endif /* not lint */ 44 45 /* 46 * Mail -- a mail program 47 * 48 * Generally useful tty stuff. 49 */ 50 51 #include "rcv.h" 52 #include "extern.h" 53 #include <sys/ioctl.h> 54 55 static cc_t c_erase; /* Current erase char */ 56 static cc_t c_kill; /* Current kill char */ 57 static sigjmp_buf rewrite; /* Place to go when continued */ 58 static sigjmp_buf intjmp; /* Place to go when interrupted */ 59 #ifndef TIOCSTI 60 static int ttyset; /* We must now do erase/kill */ 61 #endif 62 63 /* 64 * Read all relevant header fields. 65 */ 66 67 int 68 grabh(hp, gflags) 69 struct header *hp; 70 int gflags; 71 { 72 struct termios ttybuf; 73 volatile sig_t saveint; 74 #ifndef TIOCSTI 75 sig_t savequit; 76 #else 77 # ifdef TIOCEXT 78 volatile int extproc; 79 int flag; 80 # endif /* TIOCEXT */ 81 #endif 82 sig_t savetstp; 83 sig_t savettou; 84 sig_t savettin; 85 volatile int errs = 0; 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] = 0; 102 ttybuf.c_cc[VKILL] = 0; 103 if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL) 104 (void)signal(SIGINT, SIG_DFL); 105 if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL) 106 (void)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 (sigsetjmp(intjmp, 1)) { 117 errs = SIGINT; 118 goto out; 119 } 120 saveint = signal(SIGINT, ttyint); 121 #endif 122 if (gflags & GTO) { 123 #ifndef TIOCSTI 124 if (!ttyset && hp->h_to != NIL) 125 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 126 #endif 127 hp->h_to = 128 extract(readtty("To: ", detract(hp->h_to, 0)), GTO); 129 } 130 if (gflags & GSUBJECT) { 131 #ifndef TIOCSTI 132 if (!ttyset && hp->h_subject != NULL) 133 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 134 #endif 135 hp->h_subject = readtty("Subject: ", hp->h_subject); 136 } 137 if (gflags & GCC) { 138 #ifndef TIOCSTI 139 if (!ttyset && hp->h_cc != NIL) 140 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 141 #endif 142 hp->h_cc = 143 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC); 144 } 145 if (gflags & GBCC) { 146 #ifndef TIOCSTI 147 if (!ttyset && hp->h_bcc != NIL) 148 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 149 #endif 150 hp->h_bcc = 151 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC); 152 } 153 out: 154 (void)signal(SIGTSTP, savetstp); 155 (void)signal(SIGTTOU, savettou); 156 (void)signal(SIGTTIN, savettin); 157 #ifndef TIOCSTI 158 ttybuf.c_cc[VERASE] = c_erase; 159 ttybuf.c_cc[VKILL] = c_kill; 160 if (ttyset) 161 tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 162 (void)signal(SIGQUIT, savequit); 163 #else 164 # ifdef TIOCEXT 165 if (extproc) { 166 flag = 1; 167 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0) 168 warn("TIOCEXT: on"); 169 } 170 # endif /* TIOCEXT */ 171 #endif 172 (void)signal(SIGINT, saveint); 173 return(errs); 174 } 175 176 /* 177 * Read up a header from standard input. 178 * The source string has the preliminary contents to 179 * be read. 180 * 181 */ 182 183 char * 184 readtty(pr, src) 185 char pr[], src[]; 186 { 187 char ch, canonb[BUFSIZ]; 188 volatile int c; 189 char *cp, * volatile cp2; 190 191 fputs(pr, stdout); 192 fflush(stdout); 193 if (src != NULL && strlen(src) > BUFSIZ - 2) { 194 puts("too long to edit"); 195 return(src); 196 } 197 #ifndef TIOCSTI 198 if (src != NULL) 199 cp = copy(src, canonb); 200 else 201 cp = copy("", canonb); 202 fputs(canonb, stdout); 203 fflush(stdout); 204 #else 205 cp = src == NULL ? "" : src; 206 while ((c = *cp++) != '\0') { 207 if ((c_erase != _POSIX_VDISABLE && c == c_erase) || 208 (c_kill != _POSIX_VDISABLE && c == c_kill)) { 209 ch = '\\'; 210 ioctl(0, TIOCSTI, &ch); 211 } 212 ch = c; 213 ioctl(0, TIOCSTI, &ch); 214 } 215 cp = canonb; 216 *cp = 0; 217 #endif 218 cp2 = cp; 219 while (cp2 < canonb + BUFSIZ) 220 *cp2++ = 0; 221 cp2 = cp; 222 if (sigsetjmp(rewrite, 1)) 223 goto redo; 224 (void)signal(SIGTSTP, ttystop); 225 (void)signal(SIGTTOU, ttystop); 226 (void)signal(SIGTTIN, ttystop); 227 clearerr(stdin); 228 while (cp2 < canonb + BUFSIZ) { 229 c = getc(stdin); 230 if (c == EOF || c == '\n') 231 break; 232 *cp2++ = c; 233 } 234 *cp2 = 0; 235 (void)signal(SIGTSTP, SIG_DFL); 236 (void)signal(SIGTTOU, SIG_DFL); 237 (void)signal(SIGTTIN, SIG_DFL); 238 if (c == EOF && ferror(stdin)) { 239 redo: 240 cp = strlen(canonb) > 0 ? canonb : NULL; 241 clearerr(stdin); 242 return(readtty(pr, cp)); 243 } 244 #ifndef TIOCSTI 245 if (cp == NULL || *cp == '\0') 246 return(src); 247 cp2 = cp; 248 if (!ttyset) 249 return(strlen(canonb) > 0 ? savestr(canonb) : NULL); 250 while (*cp != '\0') { 251 c = *cp++; 252 if (c_erase != _POSIX_VDISABLE && c == c_erase) { 253 if (cp2 == canonb) 254 continue; 255 if (cp2[-1] == '\\') { 256 cp2[-1] = c; 257 continue; 258 } 259 cp2--; 260 continue; 261 } 262 if (c_kill != _POSIX_VDISABLE && c == c_kill) { 263 if (cp2 == canonb) 264 continue; 265 if (cp2[-1] == '\\') { 266 cp2[-1] = c; 267 continue; 268 } 269 cp2 = canonb; 270 continue; 271 } 272 *cp2++ = c; 273 } 274 *cp2 = '\0'; 275 #endif 276 if (equal("", canonb)) 277 return(NULL); 278 return(savestr(canonb)); 279 } 280 281 /* 282 * Receipt continuation. 283 */ 284 void 285 ttystop(s) 286 int s; 287 { 288 sig_t old_action = signal(s, SIG_DFL); 289 sigset_t nset; 290 291 (void)sigemptyset(&nset); 292 (void)sigaddset(&nset, s); 293 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 294 (void)kill(0, s); 295 (void)sigprocmask(SIG_BLOCK, &nset, NULL); 296 (void)signal(s, old_action); 297 siglongjmp(rewrite, 1); 298 } 299 300 /*ARGSUSED*/ 301 void 302 ttyint(s) 303 int s; 304 { 305 siglongjmp(intjmp, 1); 306 } 307