xref: /netbsd-src/usr.bin/mail/tty.c (revision 1ad9454efb13a65cd7535ccf867508cb14d9d30e)
1 /*	$NetBSD: tty.c,v 1.21 2006/09/18 19:46:21 christos 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. 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 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)tty.c	8.2 (Berkeley) 6/6/93";
36 #else
37 __RCSID("$NetBSD: tty.c,v 1.21 2006/09/18 19:46:21 christos Exp $");
38 #endif
39 #endif /* not lint */
40 
41 /*
42  * Mail -- a mail program
43  *
44  * Generally useful tty stuff.
45  */
46 
47 #include "rcv.h"
48 #include "extern.h"
49 #ifdef USE_READLINE
50 #include "complete.h"
51 #endif
52 
53 static	cc_t	c_erase;		/* Current erase char */
54 static	cc_t	c_kill;			/* Current kill char */
55 static	jmp_buf	rewrite;		/* Place to go when continued */
56 static	jmp_buf	intjmp;			/* Place to go when interrupted */
57 #ifndef TIOCSTI
58 static	int	ttyset;			/* We must now do erase/kill */
59 #endif
60 
61 /*
62  * Read all relevant header fields.
63  */
64 
65 int
66 grabh(struct header *hp, int gflags)
67 {
68 	struct termios ttybuf;
69 	sig_t saveint;
70 	sig_t savetstp;
71 	sig_t savettou;
72 	sig_t savettin;
73 	int errs;
74 #ifndef TIOCSTI
75 	sig_t savequit;
76 #else
77 # ifdef TIOCEXT
78 	int extproc, flag;
79 # endif /* TIOCEXT */
80 #endif /* TIOCSTI */
81 
82 #ifdef __GNUC__
83 	/* Avoid longjmp clobbering */
84 # if defined(TIOCSTI) && defined(TIOCEXT)
85 	(void)&extproc;
86 # endif
87 	(void)&saveint;
88 	saveint = 0;	/* XXX gcc */
89 #endif /* __GNUC__ */
90 
91 	savetstp = signal(SIGTSTP, SIG_DFL);
92 	savettou = signal(SIGTTOU, SIG_DFL);
93 	savettin = signal(SIGTTIN, SIG_DFL);
94 	errs = 0;
95 #ifndef TIOCSTI
96 	ttyset = 0;
97 #endif
98 	if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
99 		warn("tcgetattr");
100 		return(-1);
101 	}
102 	c_erase = ttybuf.c_cc[VERASE];
103 	c_kill = ttybuf.c_cc[VKILL];
104 #ifndef TIOCSTI
105 	ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
106 	ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
107 	if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
108 		(void)signal(SIGINT, SIG_DFL);
109 	if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
110 		(void)signal(SIGQUIT, SIG_DFL);
111 #else
112 # ifdef		TIOCEXT
113 	extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
114 	if (extproc) {
115 		flag = 0;
116 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
117 			warn("TIOCEXT: off");
118 	}
119 # endif	/* TIOCEXT */
120 	if (setjmp(intjmp))
121 		goto out;
122 	saveint = signal(SIGINT, ttyint);
123 #endif
124 	if (gflags & GTO) {
125 #ifndef TIOCSTI
126 		if (!ttyset && hp->h_to != NULL)
127 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
128 #endif
129 		hp->h_to =
130 			extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
131 	}
132 	if (gflags & GSUBJECT) {
133 #ifndef TIOCSTI
134 		if (!ttyset && hp->h_subject != NULL)
135 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
136 #endif
137 		hp->h_subject = readtty("Subject: ", hp->h_subject);
138 	}
139 	if (gflags & GCC) {
140 #ifndef TIOCSTI
141 		if (!ttyset && hp->h_cc != NULL)
142 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
143 #endif
144 		hp->h_cc =
145 			extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
146 	}
147 	if (gflags & GBCC) {
148 #ifndef TIOCSTI
149 		if (!ttyset && hp->h_bcc != NULL)
150 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
151 #endif
152 		hp->h_bcc =
153 			extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
154 	}
155 	if (gflags & GSMOPTS) {
156 		char *smopts;
157 		char *argv[MAXARGC];
158 		int argc, i;
159 		struct name *np, *t;
160 
161 #ifndef TIOCSTI
162 		if (!ttyset && hp->h_smopts != NULL)
163 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
164 #endif
165 		smopts = readtty("Smopts: ", detract(hp->h_smopts, GSMOPTS));
166 
167 		/* Parse smopts with getrawlist() rather than expand()
168 		 * to get a shell-like expansion.
169 		 */
170 		hp->h_smopts = NULL;
171 		if (smopts) {
172 			np = NULL;
173 			argc = getrawlist(smopts, argv, sizeof(argv)/sizeof(*argv));
174 			for (i = 0 ; i < argc ; i++) {
175 				t = nalloc(argv[i], GSMOPTS);
176 				if (hp->h_smopts == NULL)
177 			hp->h_smopts = t;
178 				else
179 			np->n_flink = t;
180 				t->n_blink = np;
181 				np = t;
182 			}
183 		}
184 	}
185 out:
186 	(void)signal(SIGTSTP, savetstp);
187 	(void)signal(SIGTTOU, savettou);
188 	(void)signal(SIGTTIN, savettin);
189 #ifndef TIOCSTI
190 	ttybuf.c_cc[VERASE] = c_erase;
191 	ttybuf.c_cc[VKILL] = c_kill;
192 	if (ttyset)
193 		tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
194 	(void)signal(SIGQUIT, savequit);
195 #else
196 # ifdef		TIOCEXT
197 	if (extproc) {
198 		flag = 1;
199 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
200 			warn("TIOCEXT: on");
201 	}
202 # endif	/* TIOCEXT */
203 #endif
204 	(void)signal(SIGINT, saveint);
205 	return(errs);
206 }
207 
208 /*
209  * Read up a header from standard input.
210  * The source string has the preliminary contents to
211  * be read.
212  *
213  */
214 
215 #ifdef USE_READLINE
216 char *
217 readtty(const char pr[], char src[])
218 {
219   return savestr(rl_getline(pr, src));
220 }
221 #else
222 char *
223 readtty(const char pr[], char src[])
224 {
225 	char ch, canonb[BUFSIZ];
226 	int c;
227 	char *cp, *cp2;
228 	static char empty[] = "";
229 #if __GNUC__
230 	/* Avoid longjmp clobbering */
231 	(void)&c;
232 	(void)&cp2;
233 #endif
234 
235 	(void)fputs(pr, stdout);
236 	(void)fflush(stdout);
237 	if (src != NULL && strlen(src) > BUFSIZ - 2) {
238 		(void)printf("too long to edit\n");
239 		return(src);
240 	}
241 #ifndef TIOCSTI
242 	if (src != NULL)
243 		cp = copy(src, canonb);
244 	else
245 		cp = copy("", canonb);
246 	(void)fputs(canonb, stdout);
247 	(void)fflush(stdout);
248 #else
249 	cp = src == NULL ? empty : src;
250 	while ((c = *cp++) != '\0') {
251 		if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
252 		    (c_kill != _POSIX_VDISABLE && c == c_kill)) {
253 			ch = '\\';
254 			(void)ioctl(0, TIOCSTI, &ch);
255 		}
256 		ch = c;
257 		(void)ioctl(0, TIOCSTI, &ch);
258 	}
259 	cp = canonb;
260 	*cp = 0;
261 #endif
262 	cp2 = cp;
263 	while (cp2 < canonb + BUFSIZ)
264 		*cp2++ = 0;
265 	cp2 = cp;
266 	if (setjmp(rewrite))
267 		goto redo;
268 	(void)signal(SIGTSTP, ttystop);
269 	(void)signal(SIGTTOU, ttystop);
270 	(void)signal(SIGTTIN, ttystop);
271 	clearerr(stdin);
272 	while (cp2 < canonb + BUFSIZ) {
273 		c = getc(stdin);
274 		if (c == EOF || c == '\n')
275 			break;
276 		*cp2++ = c;
277 	}
278 	*cp2 = 0;
279 	(void)signal(SIGTSTP, SIG_DFL);
280 	(void)signal(SIGTTOU, SIG_DFL);
281 	(void)signal(SIGTTIN, SIG_DFL);
282 	if (c == EOF && ferror(stdin)) {
283 redo:
284 		cp = strlen(canonb) > 0 ? canonb : NULL;
285 		clearerr(stdin);
286 		return(readtty(pr, cp));
287 	}
288 #ifndef TIOCSTI
289 	if (cp == NULL || *cp == '\0')
290 		return(src);
291 	cp2 = cp;
292 	if (!ttyset)
293 		return(strlen(canonb) > 0 ? savestr(canonb) : NULL);
294 	while (*cp != '\0') {
295 		c = *cp++;
296 		if (c_erase != _POSIX_VDISABLE && c == c_erase) {
297 			if (cp2 == canonb)
298 				continue;
299 			if (cp2[-1] == '\\') {
300 				cp2[-1] = c;
301 				continue;
302 			}
303 			cp2--;
304 			continue;
305 		}
306 		if (c_kill != _POSIX_VDISABLE && c == c_kill) {
307 			if (cp2 == canonb)
308 				continue;
309 			if (cp2[-1] == '\\') {
310 				cp2[-1] = c;
311 				continue;
312 			}
313 			cp2 = canonb;
314 			continue;
315 		}
316 		*cp2++ = c;
317 	}
318 	*cp2 = '\0';
319 #endif
320 	if (equal("", canonb))
321 		return(NULL);
322 	return(savestr(canonb));
323 }
324 #endif
325 
326 /*
327  * Receipt continuation.
328  */
329 void
330 ttystop(int s)
331 {
332 	sig_t old_action = signal(s, SIG_DFL);
333 	sigset_t nset;
334 
335 	(void)sigemptyset(&nset);
336 	(void)sigaddset(&nset, s);
337 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
338 	(void)kill(0, s);
339 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
340 	(void)signal(s, old_action);
341 	longjmp(rewrite, 1);
342 }
343 
344 /*ARGSUSED*/
345 void
346 ttyint(int s)
347 {
348 	longjmp(intjmp, 1);
349 }
350