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