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 359b50d902SRodney W. Grimes static char sccsid[] = "@(#)tty.c 8.1 (Berkeley) 6/6/93"; 369b50d902SRodney W. Grimes #endif /* not lint */ 379b50d902SRodney W. Grimes 389b50d902SRodney W. Grimes /* 399b50d902SRodney W. Grimes * Mail -- a mail program 409b50d902SRodney W. Grimes * 419b50d902SRodney W. Grimes * Generally useful tty stuff. 429b50d902SRodney W. Grimes */ 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #include "rcv.h" 459b50d902SRodney W. Grimes #include "extern.h" 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes static int c_erase; /* Current erase char */ 489b50d902SRodney W. Grimes static int c_kill; /* Current kill char */ 499b50d902SRodney W. Grimes static jmp_buf rewrite; /* Place to go when continued */ 509b50d902SRodney W. Grimes static jmp_buf intjmp; /* Place to go when interrupted */ 519b50d902SRodney W. Grimes #ifndef TIOCSTI 529b50d902SRodney W. Grimes static int ttyset; /* We must now do erase/kill */ 539b50d902SRodney W. Grimes #endif 549b50d902SRodney W. Grimes 559b50d902SRodney W. Grimes /* 569b50d902SRodney W. Grimes * Read all relevant header fields. 579b50d902SRodney W. Grimes */ 589b50d902SRodney W. Grimes 599b50d902SRodney W. Grimes int 609b50d902SRodney W. Grimes grabh(hp, gflags) 619b50d902SRodney W. Grimes struct header *hp; 629b50d902SRodney W. Grimes int gflags; 639b50d902SRodney W. Grimes { 649b50d902SRodney W. Grimes struct sgttyb ttybuf; 659b50d902SRodney W. Grimes sig_t saveint; 669b50d902SRodney W. Grimes #ifndef TIOCSTI 679b50d902SRodney W. Grimes sig_t savequit; 689b50d902SRodney W. Grimes #endif 699b50d902SRodney W. Grimes sig_t savetstp; 709b50d902SRodney W. Grimes sig_t savettou; 719b50d902SRodney W. Grimes sig_t savettin; 729b50d902SRodney W. Grimes int errs; 739b50d902SRodney W. Grimes void ttyint(); 749b50d902SRodney W. Grimes 759b50d902SRodney W. Grimes savetstp = signal(SIGTSTP, SIG_DFL); 769b50d902SRodney W. Grimes savettou = signal(SIGTTOU, SIG_DFL); 779b50d902SRodney W. Grimes savettin = signal(SIGTTIN, SIG_DFL); 789b50d902SRodney W. Grimes errs = 0; 799b50d902SRodney W. Grimes #ifndef TIOCSTI 809b50d902SRodney W. Grimes ttyset = 0; 819b50d902SRodney W. Grimes #endif 829b50d902SRodney W. Grimes if (ioctl(fileno(stdin), TIOCGETP, &ttybuf) < 0) { 839b50d902SRodney W. Grimes perror("gtty"); 849b50d902SRodney W. Grimes return(-1); 859b50d902SRodney W. Grimes } 869b50d902SRodney W. Grimes c_erase = ttybuf.sg_erase; 879b50d902SRodney W. Grimes c_kill = ttybuf.sg_kill; 889b50d902SRodney W. Grimes #ifndef TIOCSTI 899b50d902SRodney W. Grimes ttybuf.sg_erase = 0; 909b50d902SRodney W. Grimes ttybuf.sg_kill = 0; 919b50d902SRodney W. Grimes if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL) 929b50d902SRodney W. Grimes signal(SIGINT, SIG_DFL); 939b50d902SRodney W. Grimes if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL) 949b50d902SRodney W. Grimes signal(SIGQUIT, SIG_DFL); 959b50d902SRodney W. Grimes #else 969b50d902SRodney W. Grimes if (setjmp(intjmp)) 979b50d902SRodney W. Grimes goto out; 989b50d902SRodney W. Grimes saveint = signal(SIGINT, ttyint); 999b50d902SRodney W. Grimes #endif 1009b50d902SRodney W. Grimes if (gflags & GTO) { 1019b50d902SRodney W. Grimes #ifndef TIOCSTI 1029b50d902SRodney W. Grimes if (!ttyset && hp->h_to != NIL) 1039b50d902SRodney W. Grimes ttyset++, stty(fileno(stdin), &ttybuf); 1049b50d902SRodney W. Grimes #endif 1059b50d902SRodney W. Grimes hp->h_to = 1069b50d902SRodney W. Grimes extract(readtty("To: ", detract(hp->h_to, 0)), GTO); 1079b50d902SRodney W. Grimes } 1089b50d902SRodney W. Grimes if (gflags & GSUBJECT) { 1099b50d902SRodney W. Grimes #ifndef TIOCSTI 1109b50d902SRodney W. Grimes if (!ttyset && hp->h_subject != NOSTR) 1119b50d902SRodney W. Grimes ttyset++, stty(fileno(stdin), &ttybuf); 1129b50d902SRodney W. Grimes #endif 1139b50d902SRodney W. Grimes hp->h_subject = readtty("Subject: ", hp->h_subject); 1149b50d902SRodney W. Grimes } 1159b50d902SRodney W. Grimes if (gflags & GCC) { 1169b50d902SRodney W. Grimes #ifndef TIOCSTI 1179b50d902SRodney W. Grimes if (!ttyset && hp->h_cc != NIL) 1189b50d902SRodney W. Grimes ttyset++, stty(fileno(stdin), &ttybuf); 1199b50d902SRodney W. Grimes #endif 1209b50d902SRodney W. Grimes hp->h_cc = 1219b50d902SRodney W. Grimes extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC); 1229b50d902SRodney W. Grimes } 1239b50d902SRodney W. Grimes if (gflags & GBCC) { 1249b50d902SRodney W. Grimes #ifndef TIOCSTI 1259b50d902SRodney W. Grimes if (!ttyset && hp->h_bcc != NIL) 1269b50d902SRodney W. Grimes ttyset++, stty(fileno(stdin), &ttybuf); 1279b50d902SRodney W. Grimes #endif 1289b50d902SRodney W. Grimes hp->h_bcc = 1299b50d902SRodney W. Grimes extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC); 1309b50d902SRodney W. Grimes } 1319b50d902SRodney W. Grimes out: 1329b50d902SRodney W. Grimes signal(SIGTSTP, savetstp); 1339b50d902SRodney W. Grimes signal(SIGTTOU, savettou); 1349b50d902SRodney W. Grimes signal(SIGTTIN, savettin); 1359b50d902SRodney W. Grimes #ifndef TIOCSTI 1369b50d902SRodney W. Grimes ttybuf.sg_erase = c_erase; 1379b50d902SRodney W. Grimes ttybuf.sg_kill = c_kill; 1389b50d902SRodney W. Grimes if (ttyset) 1399b50d902SRodney W. Grimes stty(fileno(stdin), &ttybuf); 1409b50d902SRodney W. Grimes signal(SIGQUIT, savequit); 1419b50d902SRodney W. Grimes #endif 1429b50d902SRodney W. Grimes signal(SIGINT, saveint); 1439b50d902SRodney W. Grimes return(errs); 1449b50d902SRodney W. Grimes } 1459b50d902SRodney W. Grimes 1469b50d902SRodney W. Grimes /* 1479b50d902SRodney W. Grimes * Read up a header from standard input. 1489b50d902SRodney W. Grimes * The source string has the preliminary contents to 1499b50d902SRodney W. Grimes * be read. 1509b50d902SRodney W. Grimes * 1519b50d902SRodney W. Grimes */ 1529b50d902SRodney W. Grimes 1539b50d902SRodney W. Grimes char * 1549b50d902SRodney W. Grimes readtty(pr, src) 1559b50d902SRodney W. Grimes char pr[], src[]; 1569b50d902SRodney W. Grimes { 1579b50d902SRodney W. Grimes char ch, canonb[BUFSIZ]; 1589b50d902SRodney W. Grimes int c; 1599b50d902SRodney W. Grimes register char *cp, *cp2; 1609b50d902SRodney W. Grimes void ttystop(); 1619b50d902SRodney W. Grimes 1629b50d902SRodney W. Grimes fputs(pr, stdout); 1639b50d902SRodney W. Grimes fflush(stdout); 1649b50d902SRodney W. Grimes if (src != NOSTR && strlen(src) > BUFSIZ - 2) { 1659b50d902SRodney W. Grimes printf("too long to edit\n"); 1669b50d902SRodney W. Grimes return(src); 1679b50d902SRodney W. Grimes } 1689b50d902SRodney W. Grimes #ifndef TIOCSTI 1699b50d902SRodney W. Grimes if (src != NOSTR) 1709b50d902SRodney W. Grimes cp = copy(src, canonb); 1719b50d902SRodney W. Grimes else 1729b50d902SRodney W. Grimes cp = copy("", canonb); 1739b50d902SRodney W. Grimes fputs(canonb, stdout); 1749b50d902SRodney W. Grimes fflush(stdout); 1759b50d902SRodney W. Grimes #else 1769b50d902SRodney W. Grimes cp = src == NOSTR ? "" : src; 1779b50d902SRodney W. Grimes while (c = *cp++) { 1789b50d902SRodney W. Grimes if (c == c_erase || c == c_kill) { 1799b50d902SRodney W. Grimes ch = '\\'; 1809b50d902SRodney W. Grimes ioctl(0, TIOCSTI, &ch); 1819b50d902SRodney W. Grimes } 1829b50d902SRodney W. Grimes ch = c; 1839b50d902SRodney W. Grimes ioctl(0, TIOCSTI, &ch); 1849b50d902SRodney W. Grimes } 1859b50d902SRodney W. Grimes cp = canonb; 1869b50d902SRodney W. Grimes *cp = 0; 1879b50d902SRodney W. Grimes #endif 1889b50d902SRodney W. Grimes cp2 = cp; 1899b50d902SRodney W. Grimes while (cp2 < canonb + BUFSIZ) 1909b50d902SRodney W. Grimes *cp2++ = 0; 1919b50d902SRodney W. Grimes cp2 = cp; 1929b50d902SRodney W. Grimes if (setjmp(rewrite)) 1939b50d902SRodney W. Grimes goto redo; 1949b50d902SRodney W. Grimes signal(SIGTSTP, ttystop); 1959b50d902SRodney W. Grimes signal(SIGTTOU, ttystop); 1969b50d902SRodney W. Grimes signal(SIGTTIN, ttystop); 1979b50d902SRodney W. Grimes clearerr(stdin); 1989b50d902SRodney W. Grimes while (cp2 < canonb + BUFSIZ) { 1999b50d902SRodney W. Grimes c = getc(stdin); 2009b50d902SRodney W. Grimes if (c == EOF || c == '\n') 2019b50d902SRodney W. Grimes break; 2029b50d902SRodney W. Grimes *cp2++ = c; 2039b50d902SRodney W. Grimes } 2049b50d902SRodney W. Grimes *cp2 = 0; 2059b50d902SRodney W. Grimes signal(SIGTSTP, SIG_DFL); 2069b50d902SRodney W. Grimes signal(SIGTTOU, SIG_DFL); 2079b50d902SRodney W. Grimes signal(SIGTTIN, SIG_DFL); 2089b50d902SRodney W. Grimes if (c == EOF && ferror(stdin)) { 2099b50d902SRodney W. Grimes redo: 2109b50d902SRodney W. Grimes cp = strlen(canonb) > 0 ? canonb : NOSTR; 2119b50d902SRodney W. Grimes clearerr(stdin); 2129b50d902SRodney W. Grimes return(readtty(pr, cp)); 2139b50d902SRodney W. Grimes } 2149b50d902SRodney W. Grimes #ifndef TIOCSTI 2159b50d902SRodney W. Grimes if (cp == NOSTR || *cp == '\0') 2169b50d902SRodney W. Grimes return(src); 2179b50d902SRodney W. Grimes cp2 = cp; 2189b50d902SRodney W. Grimes if (!ttyset) 2199b50d902SRodney W. Grimes return(strlen(canonb) > 0 ? savestr(canonb) : NOSTR); 2209b50d902SRodney W. Grimes while (*cp != '\0') { 2219b50d902SRodney W. Grimes c = *cp++; 2229b50d902SRodney W. Grimes if (c == c_erase) { 2239b50d902SRodney W. Grimes if (cp2 == canonb) 2249b50d902SRodney W. Grimes continue; 2259b50d902SRodney W. Grimes if (cp2[-1] == '\\') { 2269b50d902SRodney W. Grimes cp2[-1] = c; 2279b50d902SRodney W. Grimes continue; 2289b50d902SRodney W. Grimes } 2299b50d902SRodney W. Grimes cp2--; 2309b50d902SRodney W. Grimes continue; 2319b50d902SRodney W. Grimes } 2329b50d902SRodney W. Grimes if (c == c_kill) { 2339b50d902SRodney W. Grimes if (cp2 == canonb) 2349b50d902SRodney W. Grimes continue; 2359b50d902SRodney W. Grimes if (cp2[-1] == '\\') { 2369b50d902SRodney W. Grimes cp2[-1] = c; 2379b50d902SRodney W. Grimes continue; 2389b50d902SRodney W. Grimes } 2399b50d902SRodney W. Grimes cp2 = canonb; 2409b50d902SRodney W. Grimes continue; 2419b50d902SRodney W. Grimes } 2429b50d902SRodney W. Grimes *cp2++ = c; 2439b50d902SRodney W. Grimes } 2449b50d902SRodney W. Grimes *cp2 = '\0'; 2459b50d902SRodney W. Grimes #endif 2469b50d902SRodney W. Grimes if (equal("", canonb)) 2479b50d902SRodney W. Grimes return(NOSTR); 2489b50d902SRodney W. Grimes return(savestr(canonb)); 2499b50d902SRodney W. Grimes } 2509b50d902SRodney W. Grimes 2519b50d902SRodney W. Grimes /* 2529b50d902SRodney W. Grimes * Receipt continuation. 2539b50d902SRodney W. Grimes */ 2549b50d902SRodney W. Grimes void 2559b50d902SRodney W. Grimes ttystop(s) 2569b50d902SRodney W. Grimes int s; 2579b50d902SRodney W. Grimes { 2589b50d902SRodney W. Grimes sig_t old_action = signal(s, SIG_DFL); 2599b50d902SRodney W. Grimes 2609b50d902SRodney W. Grimes sigsetmask(sigblock(0) & ~sigmask(s)); 2619b50d902SRodney W. Grimes kill(0, s); 2629b50d902SRodney W. Grimes sigblock(sigmask(s)); 2639b50d902SRodney W. Grimes signal(s, old_action); 2649b50d902SRodney W. Grimes longjmp(rewrite, 1); 2659b50d902SRodney W. Grimes } 2669b50d902SRodney W. Grimes 2679b50d902SRodney W. Grimes /*ARGSUSED*/ 2689b50d902SRodney W. Grimes void 2699b50d902SRodney W. Grimes ttyint(s) 2709b50d902SRodney W. Grimes int s; 2719b50d902SRodney W. Grimes { 2729b50d902SRodney W. Grimes longjmp(intjmp, 1); 2739b50d902SRodney W. Grimes } 274