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