xref: /freebsd/usr.bin/mail/tty.c (revision 0c3a8314c00f58f16823e8cd6186757d5e8bcdcc)
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 	void ttyint();
789b50d902SRodney W. Grimes 
799b50d902SRodney W. Grimes 	savetstp = signal(SIGTSTP, SIG_DFL);
809b50d902SRodney W. Grimes 	savettou = signal(SIGTTOU, SIG_DFL);
819b50d902SRodney W. Grimes 	savettin = signal(SIGTTIN, SIG_DFL);
829b50d902SRodney W. Grimes 	errs = 0;
839b50d902SRodney W. Grimes #ifndef TIOCSTI
849b50d902SRodney W. Grimes 	ttyset = 0;
859b50d902SRodney W. Grimes #endif
867c6d202aSPeter Wemm 	if (tcgetattr(fileno(stdin), &tio) < 0) {
870c3a8314SMike Heffner 		warn("tcgetattr(stdin)");
889b50d902SRodney W. Grimes 		return(-1);
899b50d902SRodney W. Grimes 	}
907c6d202aSPeter Wemm 	c_erase = tio.c_cc[VERASE];
917c6d202aSPeter Wemm 	c_kill = tio.c_cc[VKILL];
929b50d902SRodney W. Grimes #ifndef TIOCSTI
937c6d202aSPeter Wemm 	tio.c_cc[VERASE] = 0;
947c6d202aSPeter Wemm 	tio.c_cc[VKILL] = 0;
959b50d902SRodney W. Grimes 	if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
969b50d902SRodney W. Grimes 		signal(SIGINT, SIG_DFL);
979b50d902SRodney W. Grimes 	if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
989b50d902SRodney W. Grimes 		signal(SIGQUIT, SIG_DFL);
999b50d902SRodney W. Grimes #else
1009b50d902SRodney W. Grimes 	if (setjmp(intjmp))
1019b50d902SRodney W. Grimes 		goto out;
1029b50d902SRodney W. Grimes 	saveint = signal(SIGINT, ttyint);
1039b50d902SRodney W. Grimes #endif
1049b50d902SRodney W. Grimes 	if (gflags & GTO) {
1059b50d902SRodney W. Grimes #ifndef TIOCSTI
1069b50d902SRodney W. Grimes 		if (!ttyset && hp->h_to != NIL)
1077c6d202aSPeter Wemm 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &tio);
1089b50d902SRodney W. Grimes #endif
1099b50d902SRodney W. Grimes 		hp->h_to =
1109b50d902SRodney W. Grimes 			extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
1119b50d902SRodney W. Grimes 	}
1129b50d902SRodney W. Grimes 	if (gflags & GSUBJECT) {
1139b50d902SRodney W. Grimes #ifndef TIOCSTI
1149b50d902SRodney W. Grimes 		if (!ttyset && hp->h_subject != NOSTR)
1157c6d202aSPeter Wemm 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &tio);
1169b50d902SRodney W. Grimes #endif
1179b50d902SRodney W. Grimes 		hp->h_subject = readtty("Subject: ", hp->h_subject);
1189b50d902SRodney W. Grimes 	}
1199b50d902SRodney W. Grimes 	if (gflags & GCC) {
1209b50d902SRodney W. Grimes #ifndef TIOCSTI
1219b50d902SRodney W. Grimes 		if (!ttyset && hp->h_cc != NIL)
1227c6d202aSPeter Wemm 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &tio);
1239b50d902SRodney W. Grimes #endif
1249b50d902SRodney W. Grimes 		hp->h_cc =
1259b50d902SRodney W. Grimes 			extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
1269b50d902SRodney W. Grimes 	}
1279b50d902SRodney W. Grimes 	if (gflags & GBCC) {
1289b50d902SRodney W. Grimes #ifndef TIOCSTI
1299b50d902SRodney W. Grimes 		if (!ttyset && hp->h_bcc != NIL)
1307c6d202aSPeter Wemm 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &tio);
1319b50d902SRodney W. Grimes #endif
1329b50d902SRodney W. Grimes 		hp->h_bcc =
1339b50d902SRodney W. Grimes 			extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
1349b50d902SRodney W. Grimes 	}
1359b50d902SRodney W. Grimes out:
1369b50d902SRodney W. Grimes 	signal(SIGTSTP, savetstp);
1379b50d902SRodney W. Grimes 	signal(SIGTTOU, savettou);
1389b50d902SRodney W. Grimes 	signal(SIGTTIN, savettin);
1399b50d902SRodney W. Grimes #ifndef TIOCSTI
1407c6d202aSPeter Wemm 	tio.c_cc[VERASE] = c_erase;
1417c6d202aSPeter Wemm 	tio.c_cc[VKILL] = c_kill;
1429b50d902SRodney W. Grimes 	if (ttyset)
1437c6d202aSPeter Wemm 		tcsetattr(fileno(stdin), TCSADRAIN, &tio);
1449b50d902SRodney W. Grimes 	signal(SIGQUIT, savequit);
1459b50d902SRodney W. Grimes #endif
1469b50d902SRodney W. Grimes 	signal(SIGINT, saveint);
1479b50d902SRodney W. Grimes 	return(errs);
1489b50d902SRodney W. Grimes }
1499b50d902SRodney W. Grimes 
1509b50d902SRodney W. Grimes /*
1519b50d902SRodney W. Grimes  * Read up a header from standard input.
1529b50d902SRodney W. Grimes  * The source string has the preliminary contents to
1539b50d902SRodney W. Grimes  * be read.
1549b50d902SRodney W. Grimes  *
1559b50d902SRodney W. Grimes  */
1569b50d902SRodney W. Grimes 
1579b50d902SRodney W. Grimes char *
1589b50d902SRodney W. Grimes readtty(pr, src)
1599b50d902SRodney W. Grimes 	char pr[], src[];
1609b50d902SRodney W. Grimes {
1619b50d902SRodney W. Grimes 	char ch, canonb[BUFSIZ];
1629b50d902SRodney W. Grimes 	int c;
1639b50d902SRodney W. Grimes 	register char *cp, *cp2;
1649b50d902SRodney W. Grimes 	void ttystop();
1659b50d902SRodney W. Grimes 
1669b50d902SRodney W. Grimes 	fputs(pr, stdout);
1679b50d902SRodney W. Grimes 	fflush(stdout);
1689b50d902SRodney W. Grimes 	if (src != NOSTR && strlen(src) > BUFSIZ - 2) {
1699b50d902SRodney W. Grimes 		printf("too long to edit\n");
1709b50d902SRodney W. Grimes 		return(src);
1719b50d902SRodney W. Grimes 	}
1729b50d902SRodney W. Grimes #ifndef TIOCSTI
1739b50d902SRodney W. Grimes 	if (src != NOSTR)
1740c3a8314SMike Heffner 		strlcpy(canonb, src, sizeof(canonb));
1759b50d902SRodney W. Grimes 	else
1760c3a8314SMike Heffner 		*canonb = '\0';
1779b50d902SRodney W. Grimes 	fputs(canonb, stdout);
1789b50d902SRodney W. Grimes 	fflush(stdout);
1799b50d902SRodney W. Grimes #else
1809b50d902SRodney W. Grimes 	cp = src == NOSTR ? "" : src;
1819b50d902SRodney W. Grimes 	while (c = *cp++) {
1829b50d902SRodney W. Grimes 		if (c == c_erase || c == c_kill) {
1839b50d902SRodney W. Grimes 			ch = '\\';
1849b50d902SRodney W. Grimes 			ioctl(0, TIOCSTI, &ch);
1859b50d902SRodney W. Grimes 		}
1869b50d902SRodney W. Grimes 		ch = c;
1879b50d902SRodney W. Grimes 		ioctl(0, TIOCSTI, &ch);
1889b50d902SRodney W. Grimes 	}
1899b50d902SRodney W. Grimes 	cp = canonb;
1900c3a8314SMike Heffner 	*cp = '\0';
1919b50d902SRodney W. Grimes #endif
1929b50d902SRodney W. Grimes 	cp2 = cp;
1939b50d902SRodney W. Grimes 	while (cp2 < canonb + BUFSIZ)
1940c3a8314SMike Heffner 		*cp2++ = '\0';
1959b50d902SRodney W. Grimes 	cp2 = cp;
1969b50d902SRodney W. Grimes 	if (setjmp(rewrite))
1979b50d902SRodney W. Grimes 		goto redo;
1989b50d902SRodney W. Grimes 	signal(SIGTSTP, ttystop);
1999b50d902SRodney W. Grimes 	signal(SIGTTOU, ttystop);
2009b50d902SRodney W. Grimes 	signal(SIGTTIN, ttystop);
2019b50d902SRodney W. Grimes 	clearerr(stdin);
2029b50d902SRodney W. Grimes 	while (cp2 < canonb + BUFSIZ) {
2039b50d902SRodney W. Grimes 		c = getc(stdin);
2049b50d902SRodney W. Grimes 		if (c == EOF || c == '\n')
2059b50d902SRodney W. Grimes 			break;
2069b50d902SRodney W. Grimes 		*cp2++ = c;
2079b50d902SRodney W. Grimes 	}
2089b50d902SRodney W. Grimes 	*cp2 = 0;
2099b50d902SRodney W. Grimes 	signal(SIGTSTP, SIG_DFL);
2109b50d902SRodney W. Grimes 	signal(SIGTTOU, SIG_DFL);
2119b50d902SRodney W. Grimes 	signal(SIGTTIN, SIG_DFL);
2129b50d902SRodney W. Grimes 	if (c == EOF && ferror(stdin)) {
2139b50d902SRodney W. Grimes redo:
2149b50d902SRodney W. Grimes 		cp = strlen(canonb) > 0 ? canonb : NOSTR;
2159b50d902SRodney W. Grimes 		clearerr(stdin);
2169b50d902SRodney W. Grimes 		return(readtty(pr, cp));
2179b50d902SRodney W. Grimes 	}
2189b50d902SRodney W. Grimes #ifndef TIOCSTI
2199b50d902SRodney W. Grimes 	if (cp == NOSTR || *cp == '\0')
2209b50d902SRodney W. Grimes 		return(src);
2219b50d902SRodney W. Grimes 	cp2 = cp;
2229b50d902SRodney W. Grimes 	if (!ttyset)
2239b50d902SRodney W. Grimes 		return(strlen(canonb) > 0 ? savestr(canonb) : NOSTR);
2249b50d902SRodney W. Grimes 	while (*cp != '\0') {
2259b50d902SRodney W. Grimes 		c = *cp++;
2269b50d902SRodney W. Grimes 		if (c == c_erase) {
2279b50d902SRodney W. Grimes 			if (cp2 == canonb)
2289b50d902SRodney W. Grimes 				continue;
2299b50d902SRodney W. Grimes 			if (cp2[-1] == '\\') {
2309b50d902SRodney W. Grimes 				cp2[-1] = c;
2319b50d902SRodney W. Grimes 				continue;
2329b50d902SRodney W. Grimes 			}
2339b50d902SRodney W. Grimes 			cp2--;
2349b50d902SRodney W. Grimes 			continue;
2359b50d902SRodney W. Grimes 		}
2369b50d902SRodney W. Grimes 		if (c == c_kill) {
2379b50d902SRodney W. Grimes 			if (cp2 == canonb)
2389b50d902SRodney W. Grimes 				continue;
2399b50d902SRodney W. Grimes 			if (cp2[-1] == '\\') {
2409b50d902SRodney W. Grimes 				cp2[-1] = c;
2419b50d902SRodney W. Grimes 				continue;
2429b50d902SRodney W. Grimes 			}
2439b50d902SRodney W. Grimes 			cp2 = canonb;
2449b50d902SRodney W. Grimes 			continue;
2459b50d902SRodney W. Grimes 		}
2469b50d902SRodney W. Grimes 		*cp2++ = c;
2479b50d902SRodney W. Grimes 	}
2489b50d902SRodney W. Grimes 	*cp2 = '\0';
2499b50d902SRodney W. Grimes #endif
2509b50d902SRodney W. Grimes 	if (equal("", canonb))
2519b50d902SRodney W. Grimes 		return(NOSTR);
2529b50d902SRodney W. Grimes 	return(savestr(canonb));
2539b50d902SRodney W. Grimes }
2549b50d902SRodney W. Grimes 
2559b50d902SRodney W. Grimes /*
2569b50d902SRodney W. Grimes  * Receipt continuation.
2579b50d902SRodney W. Grimes  */
2589b50d902SRodney W. Grimes void
2599b50d902SRodney W. Grimes ttystop(s)
2609b50d902SRodney W. Grimes 	int s;
2619b50d902SRodney W. Grimes {
2629b50d902SRodney W. Grimes 	sig_t old_action = signal(s, SIG_DFL);
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes 	sigsetmask(sigblock(0) & ~sigmask(s));
2659b50d902SRodney W. Grimes 	kill(0, s);
2669b50d902SRodney W. Grimes 	sigblock(sigmask(s));
2679b50d902SRodney W. Grimes 	signal(s, old_action);
2689b50d902SRodney W. Grimes 	longjmp(rewrite, 1);
2699b50d902SRodney W. Grimes }
2709b50d902SRodney W. Grimes 
2719b50d902SRodney W. Grimes /*ARGSUSED*/
2729b50d902SRodney W. Grimes void
2739b50d902SRodney W. Grimes ttyint(s)
2749b50d902SRodney W. Grimes 	int s;
2759b50d902SRodney W. Grimes {
2769b50d902SRodney W. Grimes 	longjmp(intjmp, 1);
2779b50d902SRodney W. Grimes }
278