xref: /freebsd/usr.bin/mail/tty.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
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.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #ifndef lint
310c3a8314SMike Heffner #if 0
32856f23edSMike Heffner static char sccsid[] = "@(#)tty.c	8.2 (Berkeley) 6/6/93";
330c3a8314SMike Heffner #endif
349b50d902SRodney W. Grimes #endif /* not lint */
35e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
36e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
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 
47856f23edSMike Heffner static	cc_t	c_erase;		/* Current erase char */
48856f23edSMike Heffner static	cc_t	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
606d8484b0SPhilippe Charnier grabh(struct header *hp, int gflags)
619b50d902SRodney W. Grimes {
62856f23edSMike Heffner 	struct termios ttybuf;
639b50d902SRodney W. Grimes 	sig_t saveint;
649b50d902SRodney W. Grimes 	sig_t savetstp;
659b50d902SRodney W. Grimes 	sig_t savettou;
669b50d902SRodney W. Grimes 	sig_t savettin;
679b50d902SRodney W. Grimes 	int errs;
68856f23edSMike Heffner #ifndef TIOCSTI
69856f23edSMike Heffner 	sig_t savequit;
70856f23edSMike Heffner #else
71856f23edSMike Heffner # ifdef TIOCEXT
72856f23edSMike Heffner 	int extproc, flag;
73856f23edSMike Heffner # endif /* TIOCEXT */
74856f23edSMike Heffner #endif /* TIOCSTI */
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes 	savetstp = signal(SIGTSTP, SIG_DFL);
779b50d902SRodney W. Grimes 	savettou = signal(SIGTTOU, SIG_DFL);
789b50d902SRodney W. Grimes 	savettin = signal(SIGTTIN, SIG_DFL);
799b50d902SRodney W. Grimes 	errs = 0;
809b50d902SRodney W. Grimes #ifndef TIOCSTI
819b50d902SRodney W. Grimes 	ttyset = 0;
829b50d902SRodney W. Grimes #endif
83856f23edSMike Heffner 	if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
840c3a8314SMike Heffner 		warn("tcgetattr(stdin)");
859b50d902SRodney W. Grimes 		return (-1);
869b50d902SRodney W. Grimes 	}
87856f23edSMike Heffner 	c_erase = ttybuf.c_cc[VERASE];
88856f23edSMike Heffner 	c_kill = ttybuf.c_cc[VKILL];
899b50d902SRodney W. Grimes #ifndef TIOCSTI
90856f23edSMike Heffner 	ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
91856f23edSMike Heffner 	ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
929b50d902SRodney W. Grimes 	if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
939ce73e90SMike Heffner 		(void)signal(SIGINT, SIG_DFL);
949b50d902SRodney W. Grimes 	if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
959ce73e90SMike Heffner 		(void)signal(SIGQUIT, SIG_DFL);
969b50d902SRodney W. Grimes #else
97856f23edSMike Heffner # ifdef		TIOCEXT
98856f23edSMike Heffner 	extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
99856f23edSMike Heffner 	if (extproc) {
100856f23edSMike Heffner 		flag = 0;
101856f23edSMike Heffner 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
102856f23edSMike Heffner 			warn("TIOCEXT: off");
103856f23edSMike Heffner 	}
104856f23edSMike Heffner # endif	/* TIOCEXT */
1059b50d902SRodney W. Grimes 	if (setjmp(intjmp))
1069b50d902SRodney W. Grimes 		goto out;
1079b50d902SRodney W. Grimes 	saveint = signal(SIGINT, ttyint);
1089b50d902SRodney W. Grimes #endif
1099b50d902SRodney W. Grimes 	if (gflags & GTO) {
1109b50d902SRodney W. Grimes #ifndef TIOCSTI
1119ce73e90SMike Heffner 		if (!ttyset && hp->h_to != NULL)
112856f23edSMike Heffner 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1139b50d902SRodney W. Grimes #endif
1149b50d902SRodney W. Grimes 		hp->h_to =
1159b50d902SRodney W. Grimes 			extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
1169b50d902SRodney W. Grimes 	}
1179b50d902SRodney W. Grimes 	if (gflags & GSUBJECT) {
1189b50d902SRodney W. Grimes #ifndef TIOCSTI
1199ce73e90SMike Heffner 		if (!ttyset && hp->h_subject != NULL)
120856f23edSMike Heffner 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1219b50d902SRodney W. Grimes #endif
1229b50d902SRodney W. Grimes 		hp->h_subject = readtty("Subject: ", hp->h_subject);
1239b50d902SRodney W. Grimes 	}
1249b50d902SRodney W. Grimes 	if (gflags & GCC) {
1259b50d902SRodney W. Grimes #ifndef TIOCSTI
1269ce73e90SMike Heffner 		if (!ttyset && hp->h_cc != NULL)
127856f23edSMike Heffner 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1289b50d902SRodney W. Grimes #endif
1299b50d902SRodney W. Grimes 		hp->h_cc =
1309b50d902SRodney W. Grimes 			extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
1319b50d902SRodney W. Grimes 	}
1329b50d902SRodney W. Grimes 	if (gflags & GBCC) {
1339b50d902SRodney W. Grimes #ifndef TIOCSTI
1349ce73e90SMike Heffner 		if (!ttyset && hp->h_bcc != NULL)
135856f23edSMike Heffner 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1369b50d902SRodney W. Grimes #endif
1379b50d902SRodney W. Grimes 		hp->h_bcc =
1389b50d902SRodney W. Grimes 			extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
1399b50d902SRodney W. Grimes 	}
1409b50d902SRodney W. Grimes out:
1419ce73e90SMike Heffner 	(void)signal(SIGTSTP, savetstp);
1429ce73e90SMike Heffner 	(void)signal(SIGTTOU, savettou);
1439ce73e90SMike Heffner 	(void)signal(SIGTTIN, savettin);
1449b50d902SRodney W. Grimes #ifndef TIOCSTI
145856f23edSMike Heffner 	ttybuf.c_cc[VERASE] = c_erase;
146856f23edSMike Heffner 	ttybuf.c_cc[VKILL] = c_kill;
1479b50d902SRodney W. Grimes 	if (ttyset)
148856f23edSMike Heffner 		tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1499ce73e90SMike Heffner 	(void)signal(SIGQUIT, savequit);
150856f23edSMike Heffner #else
151856f23edSMike Heffner # ifdef		TIOCEXT
152856f23edSMike Heffner 	if (extproc) {
153856f23edSMike Heffner 		flag = 1;
154856f23edSMike Heffner 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
155856f23edSMike Heffner 			warn("TIOCEXT: on");
156856f23edSMike Heffner 	}
157856f23edSMike Heffner # endif	/* TIOCEXT */
1589b50d902SRodney W. Grimes #endif
1599ce73e90SMike Heffner 	(void)signal(SIGINT, saveint);
1609b50d902SRodney W. Grimes 	return (errs);
1619b50d902SRodney W. Grimes }
1629b50d902SRodney W. Grimes 
1639b50d902SRodney W. Grimes /*
1649b50d902SRodney W. Grimes  * Read up a header from standard input.
1659b50d902SRodney W. Grimes  * The source string has the preliminary contents to
1669b50d902SRodney W. Grimes  * be read.
1679b50d902SRodney W. Grimes  *
1689b50d902SRodney W. Grimes  */
1699b50d902SRodney W. Grimes 
1709b50d902SRodney W. Grimes char *
1716d8484b0SPhilippe Charnier readtty(const char *pr, char src[])
1729b50d902SRodney W. Grimes {
1739b50d902SRodney W. Grimes 	char ch, canonb[BUFSIZ];
1749b50d902SRodney W. Grimes 	int c;
1759ce73e90SMike Heffner 	char *cp, *cp2;
1769b50d902SRodney W. Grimes 
1779b50d902SRodney W. Grimes 	fputs(pr, stdout);
1789ce73e90SMike Heffner 	(void)fflush(stdout);
1799ce73e90SMike Heffner 	if (src != NULL && strlen(src) > BUFSIZ - 2) {
1809b50d902SRodney W. Grimes 		printf("too long to edit\n");
1819b50d902SRodney W. Grimes 		return (src);
1829b50d902SRodney W. Grimes 	}
1839b50d902SRodney W. Grimes #ifndef TIOCSTI
1849ce73e90SMike Heffner 	if (src != NULL)
1850c3a8314SMike Heffner 		strlcpy(canonb, src, sizeof(canonb));
1869b50d902SRodney W. Grimes 	else
1870c3a8314SMike Heffner 		*canonb = '\0';
1889b50d902SRodney W. Grimes 	fputs(canonb, stdout);
1899ce73e90SMike Heffner 	(void)fflush(stdout);
1909b50d902SRodney W. Grimes #else
1919ce73e90SMike Heffner 	cp = src == NULL ? "" : src;
1929ce73e90SMike Heffner 	while ((c = *cp++) != '\0') {
193856f23edSMike Heffner 		if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
194856f23edSMike Heffner 		    (c_kill != _POSIX_VDISABLE && c == c_kill)) {
1959b50d902SRodney W. Grimes 			ch = '\\';
1969b50d902SRodney W. Grimes 			ioctl(0, TIOCSTI, &ch);
1979b50d902SRodney W. Grimes 		}
1989b50d902SRodney W. Grimes 		ch = c;
1999b50d902SRodney W. Grimes 		ioctl(0, TIOCSTI, &ch);
2009b50d902SRodney W. Grimes 	}
2019b50d902SRodney W. Grimes 	cp = canonb;
2020c3a8314SMike Heffner 	*cp = '\0';
2039b50d902SRodney W. Grimes #endif
2049b50d902SRodney W. Grimes 	cp2 = cp;
2059b50d902SRodney W. Grimes 	while (cp2 < canonb + BUFSIZ)
2060c3a8314SMike Heffner 		*cp2++ = '\0';
2079b50d902SRodney W. Grimes 	cp2 = cp;
2089b50d902SRodney W. Grimes 	if (setjmp(rewrite))
2099b50d902SRodney W. Grimes 		goto redo;
2109ce73e90SMike Heffner 	(void)signal(SIGTSTP, ttystop);
2119ce73e90SMike Heffner 	(void)signal(SIGTTOU, ttystop);
2129ce73e90SMike Heffner 	(void)signal(SIGTTIN, ttystop);
2139b50d902SRodney W. Grimes 	clearerr(stdin);
2149b50d902SRodney W. Grimes 	while (cp2 < canonb + BUFSIZ) {
2159b50d902SRodney W. Grimes 		c = getc(stdin);
2169b50d902SRodney W. Grimes 		if (c == EOF || c == '\n')
2179b50d902SRodney W. Grimes 			break;
2189b50d902SRodney W. Grimes 		*cp2++ = c;
2199b50d902SRodney W. Grimes 	}
2209ce73e90SMike Heffner 	*cp2 = '\0';
2219ce73e90SMike Heffner 	(void)signal(SIGTSTP, SIG_DFL);
2229ce73e90SMike Heffner 	(void)signal(SIGTTOU, SIG_DFL);
2239ce73e90SMike Heffner 	(void)signal(SIGTTIN, SIG_DFL);
2249b50d902SRodney W. Grimes 	if (c == EOF && ferror(stdin)) {
2259b50d902SRodney W. Grimes redo:
2269ce73e90SMike Heffner 		cp = strlen(canonb) > 0 ? canonb : NULL;
2279b50d902SRodney W. Grimes 		clearerr(stdin);
2289b50d902SRodney W. Grimes 		return (readtty(pr, cp));
2299b50d902SRodney W. Grimes 	}
2309b50d902SRodney W. Grimes #ifndef TIOCSTI
2319ce73e90SMike Heffner 	if (cp == NULL || *cp == '\0')
2329b50d902SRodney W. Grimes 		return (src);
2339b50d902SRodney W. Grimes 	cp2 = cp;
2349b50d902SRodney W. Grimes 	if (!ttyset)
2359ce73e90SMike Heffner 		return (strlen(canonb) > 0 ? savestr(canonb) : NULL);
2369b50d902SRodney W. Grimes 	while (*cp != '\0') {
2379b50d902SRodney W. Grimes 		c = *cp++;
238856f23edSMike Heffner 		if (c_erase != _POSIX_VDISABLE && c == c_erase) {
2399b50d902SRodney W. Grimes 			if (cp2 == canonb)
2409b50d902SRodney W. Grimes 				continue;
2419b50d902SRodney W. Grimes 			if (cp2[-1] == '\\') {
2429b50d902SRodney W. Grimes 				cp2[-1] = c;
2439b50d902SRodney W. Grimes 				continue;
2449b50d902SRodney W. Grimes 			}
2459b50d902SRodney W. Grimes 			cp2--;
2469b50d902SRodney W. Grimes 			continue;
2479b50d902SRodney W. Grimes 		}
248856f23edSMike Heffner 		if (c_kill != _POSIX_VDISABLE && c == c_kill) {
2499b50d902SRodney W. Grimes 			if (cp2 == canonb)
2509b50d902SRodney W. Grimes 				continue;
2519b50d902SRodney W. Grimes 			if (cp2[-1] == '\\') {
2529b50d902SRodney W. Grimes 				cp2[-1] = c;
2539b50d902SRodney W. Grimes 				continue;
2549b50d902SRodney W. Grimes 			}
2559b50d902SRodney W. Grimes 			cp2 = canonb;
2569b50d902SRodney W. Grimes 			continue;
2579b50d902SRodney W. Grimes 		}
2589b50d902SRodney W. Grimes 		*cp2++ = c;
2599b50d902SRodney W. Grimes 	}
2609b50d902SRodney W. Grimes 	*cp2 = '\0';
2619b50d902SRodney W. Grimes #endif
2629b50d902SRodney W. Grimes 	if (equal("", canonb))
2639ce73e90SMike Heffner 		return (NULL);
2649b50d902SRodney W. Grimes 	return (savestr(canonb));
2659b50d902SRodney W. Grimes }
2669b50d902SRodney W. Grimes 
2679b50d902SRodney W. Grimes /*
2689b50d902SRodney W. Grimes  * Receipt continuation.
2699b50d902SRodney W. Grimes  */
2709b50d902SRodney W. Grimes void
2716d8484b0SPhilippe Charnier ttystop(int s)
2729b50d902SRodney W. Grimes {
2739b50d902SRodney W. Grimes 	sig_t old_action = signal(s, SIG_DFL);
274856f23edSMike Heffner 	sigset_t nset;
2759b50d902SRodney W. Grimes 
276856f23edSMike Heffner 	(void)sigemptyset(&nset);
277856f23edSMike Heffner 	(void)sigaddset(&nset, s);
278856f23edSMike Heffner 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
279856f23edSMike Heffner 	kill(0, s);
280856f23edSMike Heffner 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
2819ce73e90SMike Heffner 	(void)signal(s, old_action);
2829b50d902SRodney W. Grimes 	longjmp(rewrite, 1);
2839b50d902SRodney W. Grimes }
2849b50d902SRodney W. Grimes 
2859b50d902SRodney W. Grimes void
2866d8484b0SPhilippe Charnier ttyint(int s __unused)
2879b50d902SRodney W. Grimes {
2889b50d902SRodney W. Grimes 	longjmp(intjmp, 1);
2899b50d902SRodney W. Grimes }
290