1/* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35#if 0 36static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 6/6/93"; 37#endif 38static const char rcsid[] = 39 "$FreeBSD: src/usr.bin/mail/tty.c,v 1.6 2002/06/30 05:25:06 obrien Exp $"; 40#endif /* not lint */ 41 42#include <sys/cdefs.h> 43#include <sys/ioctl.h> 44 45/* 46 * Mail -- a mail program 47 * 48 * Generally useful tty stuff. 49 */ 50 51#include "rcv.h" 52#include "extern.h" 53 54static cc_t c_erase; /* Current erase char */ 55static cc_t c_kill; /* Current kill char */ 56static jmp_buf rewrite; /* Place to go when continued */ 57static jmp_buf intjmp; /* Place to go when interrupted */ 58#ifndef TIOCSTI 59static int ttyset; /* We must now do erase/kill */ 60#endif 61 62/* 63 * Read all relevant header fields. 64 */ 65 66int 67grabh(hp, gflags) 68 struct header *hp; 69 int gflags; 70{ 71 struct termios ttybuf; 72 sig_t saveint; 73 sig_t savetstp; 74 sig_t savettou; 75 sig_t savettin; 76 int errs; 77#ifndef TIOCSTI 78 sig_t savequit; 79#else 80# ifdef TIOCEXT 81 int extproc, flag; 82# endif /* TIOCEXT */ 83#endif /* TIOCSTI */ 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 warn("tcgetattr(stdin)"); 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] = _POSIX_VDISABLE; 100 ttybuf.c_cc[VKILL] = _POSIX_VDISABLE; 101 if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL) 102 (void)signal(SIGINT, SIG_DFL); 103 if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL) 104 (void)signal(SIGQUIT, SIG_DFL); 105#else 106# ifdef TIOCEXT 107 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0); 108 if (extproc) { 109 flag = 0; 110 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0) 111 warn("TIOCEXT: off"); 112 } 113# endif /* TIOCEXT */ 114 saveint = signal(SIGINT, ttyint); 115 if (setjmp(intjmp)) { 116 /* Interrupt from headers needs to be told to caller */ 117 errs++; 118 goto out; 119 } 120#endif 121 if (gflags & GTO) { 122#ifndef TIOCSTI 123 if (!ttyset && hp->h_to != NULL) 124 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 125#endif 126 hp->h_to = 127 extract(readtty("To: ", detract(hp->h_to, 0)), GTO); 128 } 129 if (gflags & GSUBJECT) { 130#ifndef TIOCSTI 131 if (!ttyset && hp->h_subject != NULL) 132 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 133#endif 134 hp->h_subject = readtty("Subject: ", hp->h_subject); 135 } 136 if (gflags & GCC) { 137#ifndef TIOCSTI 138 if (!ttyset && hp->h_cc != NULL) 139 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 140#endif 141 hp->h_cc = 142 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC); 143 } 144 if (gflags & GBCC) { 145#ifndef TIOCSTI 146 if (!ttyset && hp->h_bcc != NULL) 147 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 148#endif 149 hp->h_bcc = 150 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC); 151 } 152out: 153 (void)signal(SIGTSTP, savetstp); 154 (void)signal(SIGTTOU, savettou); 155 (void)signal(SIGTTIN, savettin); 156#ifndef TIOCSTI 157 ttybuf.c_cc[VERASE] = c_erase; 158 ttybuf.c_cc[VKILL] = c_kill; 159 if (ttyset) 160 tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf); 161 (void)signal(SIGQUIT, savequit); 162#else 163# ifdef TIOCEXT 164 if (extproc) { 165 flag = 1; 166 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0) 167 warn("TIOCEXT: on"); 168 } 169# endif /* TIOCEXT */ 170#endif 171 (void)signal(SIGINT, saveint); 172 return (errs); 173} 174 175/* 176 * Read up a header from standard input. 177 * The source string has the preliminary contents to 178 * be read. 179 * 180 */ 181 182char * 183readtty(pr, src) 184 const char *pr; 185 char src[]; 186{ 187 char ch, canonb[BUFSIZ]; 188 int c; 189 char *cp, *cp2; 190 191 fputs(pr, stdout); 192 (void)fflush(stdout); 193 if (src != NULL && strlen(src) > BUFSIZ - 2) { 194 printf("too long to edit\n"); 195 return (src); 196 } 197#ifndef TIOCSTI 198 if (src != NULL) 199 strlcpy(canonb, src, sizeof(canonb)); 200 else 201 *canonb = '\0'; 202 fputs(canonb, stdout); 203 (void)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 (setjmp(rewrite)) 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)) { 239redo: 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 */ 284void 285ttystop(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_BLOCK, &nset, NULL); 294 kill(0, s); 295 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 296 (void)signal(s, old_action); 297 longjmp(rewrite, 1); 298} 299 300/*ARGSUSED*/ 301void 302ttyint(s) 303 int s; 304{ 305 longjmp(intjmp, 1); 306} 307