19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1980, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 350c3a8314SMike Heffner #if 0 369b50d902SRodney W. Grimes static char sccsid[] = "@(#)tty.c 8.1 (Berkeley) 6/6/93"; 370c3a8314SMike Heffner #endif 380c3a8314SMike Heffner static const char rcsid[] = 390c3a8314SMike Heffner "$FreeBSD$"; 409b50d902SRodney W. Grimes #endif /* not lint */ 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes /* 439b50d902SRodney W. Grimes * Mail -- a mail program 449b50d902SRodney W. Grimes * 459b50d902SRodney W. Grimes * Generally useful tty stuff. 469b50d902SRodney W. Grimes */ 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include "rcv.h" 499b50d902SRodney W. Grimes #include "extern.h" 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes static int c_erase; /* Current erase char */ 529b50d902SRodney W. Grimes static int c_kill; /* Current kill char */ 539b50d902SRodney W. Grimes static jmp_buf rewrite; /* Place to go when continued */ 549b50d902SRodney W. Grimes static jmp_buf intjmp; /* Place to go when interrupted */ 559b50d902SRodney W. Grimes #ifndef TIOCSTI 569b50d902SRodney W. Grimes static int ttyset; /* We must now do erase/kill */ 579b50d902SRodney W. Grimes #endif 589b50d902SRodney W. Grimes 599b50d902SRodney W. Grimes /* 609b50d902SRodney W. Grimes * Read all relevant header fields. 619b50d902SRodney W. Grimes */ 629b50d902SRodney W. Grimes 639b50d902SRodney W. Grimes int 649b50d902SRodney W. Grimes grabh(hp, gflags) 659b50d902SRodney W. Grimes struct header *hp; 669b50d902SRodney W. Grimes int gflags; 679b50d902SRodney W. Grimes { 687c6d202aSPeter Wemm struct termios tio; 699b50d902SRodney W. Grimes sig_t saveint; 709b50d902SRodney W. Grimes #ifndef TIOCSTI 719b50d902SRodney W. Grimes sig_t savequit; 729b50d902SRodney W. Grimes #endif 739b50d902SRodney W. Grimes sig_t savetstp; 749b50d902SRodney W. Grimes sig_t savettou; 759b50d902SRodney W. Grimes sig_t savettin; 769b50d902SRodney W. Grimes int errs; 779b50d902SRodney W. Grimes 789b50d902SRodney W. Grimes savetstp = signal(SIGTSTP, SIG_DFL); 799b50d902SRodney W. Grimes savettou = signal(SIGTTOU, SIG_DFL); 809b50d902SRodney W. Grimes savettin = signal(SIGTTIN, SIG_DFL); 819b50d902SRodney W. Grimes errs = 0; 829b50d902SRodney W. Grimes #ifndef TIOCSTI 839b50d902SRodney W. Grimes ttyset = 0; 849b50d902SRodney W. Grimes #endif 857c6d202aSPeter Wemm if (tcgetattr(fileno(stdin), &tio) < 0) { 860c3a8314SMike Heffner warn("tcgetattr(stdin)"); 879b50d902SRodney W. Grimes return (-1); 889b50d902SRodney W. Grimes } 897c6d202aSPeter Wemm c_erase = tio.c_cc[VERASE]; 907c6d202aSPeter Wemm c_kill = tio.c_cc[VKILL]; 919b50d902SRodney W. Grimes #ifndef TIOCSTI 927c6d202aSPeter Wemm tio.c_cc[VERASE] = 0; 937c6d202aSPeter Wemm tio.c_cc[VKILL] = 0; 949b50d902SRodney W. Grimes if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL) 959ce73e90SMike Heffner (void)signal(SIGINT, SIG_DFL); 969b50d902SRodney W. Grimes if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL) 979ce73e90SMike Heffner (void)signal(SIGQUIT, SIG_DFL); 989b50d902SRodney W. Grimes #else 999b50d902SRodney W. Grimes if (setjmp(intjmp)) 1009b50d902SRodney W. Grimes goto out; 1019b50d902SRodney W. Grimes saveint = signal(SIGINT, ttyint); 1029b50d902SRodney W. Grimes #endif 1039b50d902SRodney W. Grimes if (gflags & GTO) { 1049b50d902SRodney W. Grimes #ifndef TIOCSTI 1059ce73e90SMike Heffner if (!ttyset && hp->h_to != NULL) 1067c6d202aSPeter Wemm ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &tio); 1079b50d902SRodney W. Grimes #endif 1089b50d902SRodney W. Grimes hp->h_to = 1099b50d902SRodney W. Grimes extract(readtty("To: ", detract(hp->h_to, 0)), GTO); 1109b50d902SRodney W. Grimes } 1119b50d902SRodney W. Grimes if (gflags & GSUBJECT) { 1129b50d902SRodney W. Grimes #ifndef TIOCSTI 1139ce73e90SMike Heffner if (!ttyset && hp->h_subject != NULL) 1147c6d202aSPeter Wemm ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &tio); 1159b50d902SRodney W. Grimes #endif 1169b50d902SRodney W. Grimes hp->h_subject = readtty("Subject: ", hp->h_subject); 1179b50d902SRodney W. Grimes } 1189b50d902SRodney W. Grimes if (gflags & GCC) { 1199b50d902SRodney W. Grimes #ifndef TIOCSTI 1209ce73e90SMike Heffner if (!ttyset && hp->h_cc != NULL) 1217c6d202aSPeter Wemm ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &tio); 1229b50d902SRodney W. Grimes #endif 1239b50d902SRodney W. Grimes hp->h_cc = 1249b50d902SRodney W. Grimes extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC); 1259b50d902SRodney W. Grimes } 1269b50d902SRodney W. Grimes if (gflags & GBCC) { 1279b50d902SRodney W. Grimes #ifndef TIOCSTI 1289ce73e90SMike Heffner if (!ttyset && hp->h_bcc != NULL) 1297c6d202aSPeter Wemm ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &tio); 1309b50d902SRodney W. Grimes #endif 1319b50d902SRodney W. Grimes hp->h_bcc = 1329b50d902SRodney W. Grimes extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC); 1339b50d902SRodney W. Grimes } 1349b50d902SRodney W. Grimes out: 1359ce73e90SMike Heffner (void)signal(SIGTSTP, savetstp); 1369ce73e90SMike Heffner (void)signal(SIGTTOU, savettou); 1379ce73e90SMike Heffner (void)signal(SIGTTIN, savettin); 1389b50d902SRodney W. Grimes #ifndef TIOCSTI 1397c6d202aSPeter Wemm tio.c_cc[VERASE] = c_erase; 1407c6d202aSPeter Wemm tio.c_cc[VKILL] = c_kill; 1419b50d902SRodney W. Grimes if (ttyset) 1427c6d202aSPeter Wemm tcsetattr(fileno(stdin), TCSADRAIN, &tio); 1439ce73e90SMike Heffner (void)signal(SIGQUIT, savequit); 1449b50d902SRodney W. Grimes #endif 1459ce73e90SMike Heffner (void)signal(SIGINT, saveint); 1469b50d902SRodney W. Grimes return (errs); 1479b50d902SRodney W. Grimes } 1489b50d902SRodney W. Grimes 1499b50d902SRodney W. Grimes /* 1509b50d902SRodney W. Grimes * Read up a header from standard input. 1519b50d902SRodney W. Grimes * The source string has the preliminary contents to 1529b50d902SRodney W. Grimes * be read. 1539b50d902SRodney W. Grimes * 1549b50d902SRodney W. Grimes */ 1559b50d902SRodney W. Grimes 1569b50d902SRodney W. Grimes char * 1579b50d902SRodney W. Grimes readtty(pr, src) 1589ce73e90SMike Heffner const char *pr; 1599ce73e90SMike Heffner char src[]; 1609b50d902SRodney W. Grimes { 1619b50d902SRodney W. Grimes char ch, canonb[BUFSIZ]; 1629b50d902SRodney W. Grimes int c; 1639ce73e90SMike Heffner char *cp, *cp2; 1649b50d902SRodney W. Grimes 1659b50d902SRodney W. Grimes fputs(pr, stdout); 1669ce73e90SMike Heffner (void)fflush(stdout); 1679ce73e90SMike Heffner if (src != NULL && strlen(src) > BUFSIZ - 2) { 1689b50d902SRodney W. Grimes printf("too long to edit\n"); 1699b50d902SRodney W. Grimes return (src); 1709b50d902SRodney W. Grimes } 1719b50d902SRodney W. Grimes #ifndef TIOCSTI 1729ce73e90SMike Heffner if (src != NULL) 1730c3a8314SMike Heffner strlcpy(canonb, src, sizeof(canonb)); 1749b50d902SRodney W. Grimes else 1750c3a8314SMike Heffner *canonb = '\0'; 1769b50d902SRodney W. Grimes fputs(canonb, stdout); 1779ce73e90SMike Heffner (void)fflush(stdout); 1789b50d902SRodney W. Grimes #else 1799ce73e90SMike Heffner cp = src == NULL ? "" : src; 1809ce73e90SMike Heffner while ((c = *cp++) != '\0') { 1819b50d902SRodney W. Grimes if (c == c_erase || c == c_kill) { 1829b50d902SRodney W. Grimes ch = '\\'; 1839b50d902SRodney W. Grimes ioctl(0, TIOCSTI, &ch); 1849b50d902SRodney W. Grimes } 1859b50d902SRodney W. Grimes ch = c; 1869b50d902SRodney W. Grimes ioctl(0, TIOCSTI, &ch); 1879b50d902SRodney W. Grimes } 1889b50d902SRodney W. Grimes cp = canonb; 1890c3a8314SMike Heffner *cp = '\0'; 1909b50d902SRodney W. Grimes #endif 1919b50d902SRodney W. Grimes cp2 = cp; 1929b50d902SRodney W. Grimes while (cp2 < canonb + BUFSIZ) 1930c3a8314SMike Heffner *cp2++ = '\0'; 1949b50d902SRodney W. Grimes cp2 = cp; 1959b50d902SRodney W. Grimes if (setjmp(rewrite)) 1969b50d902SRodney W. Grimes goto redo; 1979ce73e90SMike Heffner (void)signal(SIGTSTP, ttystop); 1989ce73e90SMike Heffner (void)signal(SIGTTOU, ttystop); 1999ce73e90SMike Heffner (void)signal(SIGTTIN, ttystop); 2009b50d902SRodney W. Grimes clearerr(stdin); 2019b50d902SRodney W. Grimes while (cp2 < canonb + BUFSIZ) { 2029b50d902SRodney W. Grimes c = getc(stdin); 2039b50d902SRodney W. Grimes if (c == EOF || c == '\n') 2049b50d902SRodney W. Grimes break; 2059b50d902SRodney W. Grimes *cp2++ = c; 2069b50d902SRodney W. Grimes } 2079ce73e90SMike Heffner *cp2 = '\0'; 2089ce73e90SMike Heffner (void)signal(SIGTSTP, SIG_DFL); 2099ce73e90SMike Heffner (void)signal(SIGTTOU, SIG_DFL); 2109ce73e90SMike Heffner (void)signal(SIGTTIN, SIG_DFL); 2119b50d902SRodney W. Grimes if (c == EOF && ferror(stdin)) { 2129b50d902SRodney W. Grimes redo: 2139ce73e90SMike Heffner cp = strlen(canonb) > 0 ? canonb : NULL; 2149b50d902SRodney W. Grimes clearerr(stdin); 2159b50d902SRodney W. Grimes return (readtty(pr, cp)); 2169b50d902SRodney W. Grimes } 2179b50d902SRodney W. Grimes #ifndef TIOCSTI 2189ce73e90SMike Heffner if (cp == NULL || *cp == '\0') 2199b50d902SRodney W. Grimes return (src); 2209b50d902SRodney W. Grimes cp2 = cp; 2219b50d902SRodney W. Grimes if (!ttyset) 2229ce73e90SMike Heffner return (strlen(canonb) > 0 ? savestr(canonb) : NULL); 2239b50d902SRodney W. Grimes while (*cp != '\0') { 2249b50d902SRodney W. Grimes c = *cp++; 2259b50d902SRodney W. Grimes if (c == c_erase) { 2269b50d902SRodney W. Grimes if (cp2 == canonb) 2279b50d902SRodney W. Grimes continue; 2289b50d902SRodney W. Grimes if (cp2[-1] == '\\') { 2299b50d902SRodney W. Grimes cp2[-1] = c; 2309b50d902SRodney W. Grimes continue; 2319b50d902SRodney W. Grimes } 2329b50d902SRodney W. Grimes cp2--; 2339b50d902SRodney W. Grimes continue; 2349b50d902SRodney W. Grimes } 2359b50d902SRodney W. Grimes if (c == c_kill) { 2369b50d902SRodney W. Grimes if (cp2 == canonb) 2379b50d902SRodney W. Grimes continue; 2389b50d902SRodney W. Grimes if (cp2[-1] == '\\') { 2399b50d902SRodney W. Grimes cp2[-1] = c; 2409b50d902SRodney W. Grimes continue; 2419b50d902SRodney W. Grimes } 2429b50d902SRodney W. Grimes cp2 = canonb; 2439b50d902SRodney W. Grimes continue; 2449b50d902SRodney W. Grimes } 2459b50d902SRodney W. Grimes *cp2++ = c; 2469b50d902SRodney W. Grimes } 2479b50d902SRodney W. Grimes *cp2 = '\0'; 2489b50d902SRodney W. Grimes #endif 2499b50d902SRodney W. Grimes if (equal("", canonb)) 2509ce73e90SMike Heffner return (NULL); 2519b50d902SRodney W. Grimes return (savestr(canonb)); 2529b50d902SRodney W. Grimes } 2539b50d902SRodney W. Grimes 2549b50d902SRodney W. Grimes /* 2559b50d902SRodney W. Grimes * Receipt continuation. 2569b50d902SRodney W. Grimes */ 2579b50d902SRodney W. Grimes void 2589b50d902SRodney W. Grimes ttystop(s) 2599b50d902SRodney W. Grimes int s; 2609b50d902SRodney W. Grimes { 2619b50d902SRodney W. Grimes sig_t old_action = signal(s, SIG_DFL); 2629b50d902SRodney W. Grimes 2639ce73e90SMike Heffner (void)sigsetmask(sigblock(0) & ~sigmask(s)); 2649ce73e90SMike Heffner (void)kill(0, s); 2659ce73e90SMike Heffner (void)sigblock(sigmask(s)); 2669ce73e90SMike Heffner (void)signal(s, old_action); 2679b50d902SRodney W. Grimes longjmp(rewrite, 1); 2689b50d902SRodney W. Grimes } 2699b50d902SRodney W. Grimes 2709b50d902SRodney W. Grimes /*ARGSUSED*/ 2719b50d902SRodney W. Grimes void 2729b50d902SRodney W. Grimes ttyint(s) 2739b50d902SRodney W. Grimes int s; 2749b50d902SRodney W. Grimes { 2759b50d902SRodney W. Grimes longjmp(intjmp, 1); 2769b50d902SRodney W. Grimes } 277