xref: /dragonfly/usr.bin/mail/tty.c (revision c37c9ab3)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)tty.c	8.2 (Berkeley) 6/6/93
30  * $FreeBSD: src/usr.bin/mail/tty.c,v 1.2.8.3 2003/01/06 05:46:04 mikeh Exp $
31  * $DragonFly: src/usr.bin/mail/tty.c,v 1.5 2004/09/08 03:01:11 joerg Exp $
32  */
33 
34 /*
35  * Mail -- a mail program
36  *
37  * Generally useful tty stuff.
38  */
39 
40 #include "rcv.h"
41 #include "extern.h"
42 
43 static cc_t	c_erase;		/* Current erase char */
44 static cc_t	c_kill;			/* Current kill char */
45 static jmp_buf	rewrite;		/* Place to go when continued */
46 static jmp_buf	intjmp;			/* Place to go when interrupted */
47 
48 /*
49  * Read all relevant header fields.
50  */
51 
52 int
53 grabh(struct header *hp, int gflags)
54 {
55 	struct termios ttybuf;
56 	sig_t saveint;
57 	sig_t savetstp;
58 	sig_t savettou;
59 	sig_t savettin;
60 	int errs;
61 	int extproc, flag;
62 
63 	savetstp = signal(SIGTSTP, SIG_DFL);
64 	savettou = signal(SIGTTOU, SIG_DFL);
65 	savettin = signal(SIGTTIN, SIG_DFL);
66 	errs = 0;
67 	if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
68 		warn("tcgetattr(stdin)");
69 		return (-1);
70 	}
71 	c_erase = ttybuf.c_cc[VERASE];
72 	c_kill = ttybuf.c_cc[VKILL];
73 	extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
74 	if (extproc) {
75 		flag = 0;
76 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
77 			warn("TIOCEXT: off");
78 	}
79 	if (setjmp(intjmp))
80 		goto out;
81 	saveint = signal(SIGINT, ttyint);
82 	if (gflags & GTO) {
83 		hp->h_to =
84 			extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
85 	}
86 	if (gflags & GSUBJECT) {
87 		hp->h_subject = readtty("Subject: ", hp->h_subject);
88 	}
89 	if (gflags & GCC) {
90 		hp->h_cc =
91 			extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
92 	}
93 	if (gflags & GBCC) {
94 		hp->h_bcc =
95 			extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
96 	}
97 out:
98 	signal(SIGTSTP, savetstp);
99 	signal(SIGTTOU, savettou);
100 	signal(SIGTTIN, savettin);
101 	if (extproc) {
102 		flag = 1;
103 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
104 			warn("TIOCEXT: on");
105 	}
106 	signal(SIGINT, saveint);
107 	return (errs);
108 }
109 
110 /*
111  * Read up a header from standard input.
112  * The source string has the preliminary contents to
113  * be read.
114  *
115  */
116 
117 char *
118 readtty(const char *pr, char *src)
119 {
120 	char ch, canonb[BUFSIZ];
121 	int c;
122 	char *cp, *cp2;
123 
124 	fputs(pr, stdout);
125 	fflush(stdout);
126 	if (src != NULL && strlen(src) > BUFSIZ - 2) {
127 		printf("too long to edit\n");
128 		return (src);
129 	}
130 	cp = src == NULL ? "" : src;
131 	while ((c = *cp++) != '\0') {
132 		if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
133 		    (c_kill != _POSIX_VDISABLE && c == c_kill)) {
134 			ch = '\\';
135 			ioctl(0, TIOCSTI, &ch);
136 		}
137 		ch = c;
138 		ioctl(0, TIOCSTI, &ch);
139 	}
140 	cp = canonb;
141 	*cp = '\0';
142 	cp2 = cp;
143 	while (cp2 < canonb + BUFSIZ)
144 		*cp2++ = '\0';
145 	cp2 = cp;
146 	if (setjmp(rewrite))
147 		goto redo;
148 	signal(SIGTSTP, ttystop);
149 	signal(SIGTTOU, ttystop);
150 	signal(SIGTTIN, ttystop);
151 	clearerr(stdin);
152 	while (cp2 < canonb + BUFSIZ) {
153 		c = getc(stdin);
154 		if (c == EOF || c == '\n')
155 			break;
156 		*cp2++ = c;
157 	}
158 	*cp2 = '\0';
159 	signal(SIGTSTP, SIG_DFL);
160 	signal(SIGTTOU, SIG_DFL);
161 	signal(SIGTTIN, SIG_DFL);
162 	if (c == EOF && ferror(stdin)) {
163 redo:
164 		cp = strlen(canonb) > 0 ? canonb : NULL;
165 		clearerr(stdin);
166 		return (readtty(pr, cp));
167 	}
168 	if (equal("", canonb))
169 		return (NULL);
170 	return (savestr(canonb));
171 }
172 
173 /*
174  * Receipt continuation.
175  */
176 void
177 ttystop(int s)
178 {
179 	sig_t old_action = signal(s, SIG_DFL);
180 	sigset_t nset;
181 
182 	sigemptyset(&nset);
183 	sigaddset(&nset, s);
184 	sigprocmask(SIG_BLOCK, &nset, NULL);
185 	kill(0, s);
186 	sigprocmask(SIG_UNBLOCK, &nset, NULL);
187 	signal(s, old_action);
188 	longjmp(rewrite, 1);
189 }
190 
191 /*ARGSUSED*/
192 void
193 ttyint(int s)
194 {
195 	longjmp(intjmp, 1);
196 }
197