xref: /freebsd/usr.bin/mail/tty.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 #endif /* not lint */
34 #include <sys/cdefs.h>
35 /*
36  * Mail -- a mail program
37  *
38  * Generally useful tty stuff.
39  */
40 
41 #include "rcv.h"
42 #include "extern.h"
43 
44 static	cc_t	c_erase;		/* Current erase char */
45 static	cc_t	c_kill;			/* Current kill char */
46 static	jmp_buf	rewrite;		/* Place to go when continued */
47 static	jmp_buf	intjmp;			/* Place to go when interrupted */
48 #ifndef TIOCSTI
49 static	int	ttyset;			/* We must now do erase/kill */
50 #endif
51 
52 /*
53  * Read all relevant header fields.
54  */
55 
56 int
57 grabh(struct header *hp, int gflags)
58 {
59 	struct termios ttybuf;
60 	sig_t saveint;
61 	sig_t savetstp;
62 	sig_t savettou;
63 	sig_t savettin;
64 	int errs;
65 #ifndef TIOCSTI
66 	sig_t savequit;
67 #else
68 # ifdef TIOCEXT
69 	int extproc, flag;
70 # endif /* TIOCEXT */
71 #endif /* TIOCSTI */
72 
73 	savetstp = signal(SIGTSTP, SIG_DFL);
74 	savettou = signal(SIGTTOU, SIG_DFL);
75 	savettin = signal(SIGTTIN, SIG_DFL);
76 	errs = 0;
77 #ifndef TIOCSTI
78 	ttyset = 0;
79 #endif
80 	if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
81 		warn("tcgetattr(stdin)");
82 		return (-1);
83 	}
84 	c_erase = ttybuf.c_cc[VERASE];
85 	c_kill = ttybuf.c_cc[VKILL];
86 #ifndef TIOCSTI
87 	ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
88 	ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
89 	if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
90 		(void)signal(SIGINT, SIG_DFL);
91 	if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
92 		(void)signal(SIGQUIT, SIG_DFL);
93 #else
94 # ifdef		TIOCEXT
95 	extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
96 	if (extproc) {
97 		flag = 0;
98 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
99 			warn("TIOCEXT: off");
100 	}
101 # endif	/* TIOCEXT */
102 	if (setjmp(intjmp))
103 		goto out;
104 	saveint = signal(SIGINT, ttyint);
105 #endif
106 	if (gflags & GTO) {
107 #ifndef TIOCSTI
108 		if (!ttyset && hp->h_to != NULL)
109 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
110 #endif
111 		hp->h_to =
112 			extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
113 	}
114 	if (gflags & GSUBJECT) {
115 #ifndef TIOCSTI
116 		if (!ttyset && hp->h_subject != NULL)
117 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
118 #endif
119 		hp->h_subject = readtty("Subject: ", hp->h_subject);
120 	}
121 	if (gflags & GCC) {
122 #ifndef TIOCSTI
123 		if (!ttyset && hp->h_cc != NULL)
124 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
125 #endif
126 		hp->h_cc =
127 			extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
128 	}
129 	if (gflags & GBCC) {
130 #ifndef TIOCSTI
131 		if (!ttyset && hp->h_bcc != NULL)
132 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
133 #endif
134 		hp->h_bcc =
135 			extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
136 	}
137 #ifdef TIOCSTI
138 out:
139 #endif
140 	(void)signal(SIGTSTP, savetstp);
141 	(void)signal(SIGTTOU, savettou);
142 	(void)signal(SIGTTIN, savettin);
143 #ifndef TIOCSTI
144 	ttybuf.c_cc[VERASE] = c_erase;
145 	ttybuf.c_cc[VKILL] = c_kill;
146 	if (ttyset)
147 		tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
148 	(void)signal(SIGQUIT, savequit);
149 #else
150 # ifdef		TIOCEXT
151 	if (extproc) {
152 		flag = 1;
153 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
154 			warn("TIOCEXT: on");
155 	}
156 # endif	/* TIOCEXT */
157 #endif
158 	(void)signal(SIGINT, saveint);
159 	return (errs);
160 }
161 
162 /*
163  * Read up a header from standard input.
164  * The source string has the preliminary contents to
165  * be read.
166  *
167  */
168 
169 char *
170 readtty(const char *pr, char src[])
171 {
172 	char canonb[BUFSIZ];
173 #ifdef TIOCSTI
174 	char ch;
175 #endif
176 	int c;
177 	char *cp, *cp2;
178 
179 	fputs(pr, stdout);
180 	(void)fflush(stdout);
181 	if (src != NULL && strlen(src) > BUFSIZ - 2) {
182 		printf("too long to edit\n");
183 		return (src);
184 	}
185 #ifndef TIOCSTI
186 	if (src != NULL)
187 		strlcpy(canonb, src, sizeof(canonb));
188 	else
189 		*canonb = '\0';
190 	fputs(canonb, stdout);
191 	(void)fflush(stdout);
192 #else
193 	cp = src == NULL ? "" : src;
194 	while ((c = *cp++) != '\0') {
195 		if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
196 		    (c_kill != _POSIX_VDISABLE && c == c_kill)) {
197 			ch = '\\';
198 			ioctl(0, TIOCSTI, &ch);
199 		}
200 		ch = c;
201 		ioctl(0, TIOCSTI, &ch);
202 	}
203 	cp = canonb;
204 	*cp = '\0';
205 #endif
206 	cp2 = cp;
207 	while (cp2 < canonb + BUFSIZ)
208 		*cp2++ = '\0';
209 	cp2 = cp;
210 	if (setjmp(rewrite))
211 		goto redo;
212 	(void)signal(SIGTSTP, ttystop);
213 	(void)signal(SIGTTOU, ttystop);
214 	(void)signal(SIGTTIN, ttystop);
215 	clearerr(stdin);
216 	while (cp2 < canonb + BUFSIZ) {
217 		c = getc(stdin);
218 		if (c == EOF || c == '\n')
219 			break;
220 		*cp2++ = c;
221 	}
222 	*cp2 = '\0';
223 	(void)signal(SIGTSTP, SIG_DFL);
224 	(void)signal(SIGTTOU, SIG_DFL);
225 	(void)signal(SIGTTIN, SIG_DFL);
226 	if (c == EOF && ferror(stdin)) {
227 redo:
228 		cp = strlen(canonb) > 0 ? canonb : NULL;
229 		clearerr(stdin);
230 		return (readtty(pr, cp));
231 	}
232 #ifndef TIOCSTI
233 	if (cp == NULL || *cp == '\0')
234 		return (src);
235 	cp2 = cp;
236 	if (!ttyset)
237 		return (strlen(canonb) > 0 ? savestr(canonb) : NULL);
238 	while (*cp != '\0') {
239 		c = *cp++;
240 		if (c_erase != _POSIX_VDISABLE && c == c_erase) {
241 			if (cp2 == canonb)
242 				continue;
243 			if (cp2[-1] == '\\') {
244 				cp2[-1] = c;
245 				continue;
246 			}
247 			cp2--;
248 			continue;
249 		}
250 		if (c_kill != _POSIX_VDISABLE && c == c_kill) {
251 			if (cp2 == canonb)
252 				continue;
253 			if (cp2[-1] == '\\') {
254 				cp2[-1] = c;
255 				continue;
256 			}
257 			cp2 = canonb;
258 			continue;
259 		}
260 		*cp2++ = c;
261 	}
262 	*cp2 = '\0';
263 #endif
264 	if (equal("", canonb))
265 		return (NULL);
266 	return (savestr(canonb));
267 }
268 
269 /*
270  * Receipt continuation.
271  */
272 void
273 ttystop(int s)
274 {
275 	sig_t old_action = signal(s, SIG_DFL);
276 	sigset_t nset;
277 
278 	(void)sigemptyset(&nset);
279 	(void)sigaddset(&nset, s);
280 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
281 	kill(0, s);
282 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
283 	(void)signal(s, old_action);
284 	longjmp(rewrite, 1);
285 }
286 
287 void
288 ttyint(int s __unused)
289 {
290 	longjmp(intjmp, 1);
291 }
292