tty.c revision 1.20 1 /* $NetBSD: tty.c,v 1.20 2006/05/10 21:53:48 mrg 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.20 2006/05/10 21:53:48 mrg 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
50 static cc_t c_erase; /* Current erase char */
51 static cc_t c_kill; /* Current kill char */
52 static jmp_buf rewrite; /* Place to go when continued */
53 static jmp_buf intjmp; /* Place to go when interrupted */
54 #ifndef TIOCSTI
55 static int ttyset; /* We must now do erase/kill */
56 #endif
57
58 /*
59 * Read all relevant header fields.
60 */
61
62 int
63 grabh(struct header *hp, int gflags)
64 {
65 struct termios ttybuf;
66 sig_t saveint;
67 sig_t savetstp;
68 sig_t savettou;
69 sig_t savettin;
70 int errs;
71 #ifndef TIOCSTI
72 sig_t savequit;
73 #else
74 # ifdef TIOCEXT
75 int extproc, flag;
76 # endif /* TIOCEXT */
77 #endif /* TIOCSTI */
78
79 #ifdef __GNUC__
80 /* Avoid longjmp clobbering */
81 # if defined(TIOCSTI) && defined(TIOCEXT)
82 (void)&extproc;
83 # endif
84 (void)&saveint;
85 saveint = 0; /* XXX gcc */
86 #endif /* __GNUC__ */
87
88 savetstp = signal(SIGTSTP, SIG_DFL);
89 savettou = signal(SIGTTOU, SIG_DFL);
90 savettin = signal(SIGTTIN, SIG_DFL);
91 errs = 0;
92 #ifndef TIOCSTI
93 ttyset = 0;
94 #endif
95 if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
96 warn("tcgetattr");
97 return(-1);
98 }
99 c_erase = ttybuf.c_cc[VERASE];
100 c_kill = ttybuf.c_cc[VKILL];
101 #ifndef TIOCSTI
102 ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
103 ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
104 if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
105 (void)signal(SIGINT, SIG_DFL);
106 if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
107 (void)signal(SIGQUIT, SIG_DFL);
108 #else
109 # ifdef TIOCEXT
110 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
111 if (extproc) {
112 flag = 0;
113 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
114 warn("TIOCEXT: off");
115 }
116 # endif /* TIOCEXT */
117 if (setjmp(intjmp))
118 goto out;
119 saveint = signal(SIGINT, ttyint);
120 #endif
121 if (gflags & GTO) {
122 #ifndef TIOCSTI
123 if (!ttyset && hp->h_to != NULL)
124 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
125 #endif
126 hp->h_to =
127 extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
128 }
129 if (gflags & GSUBJECT) {
130 #ifndef TIOCSTI
131 if (!ttyset && hp->h_subject != NULL)
132 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
133 #endif
134 hp->h_subject = readtty("Subject: ", hp->h_subject);
135 }
136 if (gflags & GCC) {
137 #ifndef TIOCSTI
138 if (!ttyset && hp->h_cc != NULL)
139 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
140 #endif
141 hp->h_cc =
142 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
143 }
144 if (gflags & GBCC) {
145 #ifndef TIOCSTI
146 if (!ttyset && hp->h_bcc != NULL)
147 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
148 #endif
149 hp->h_bcc =
150 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
151 }
152 out:
153 (void)signal(SIGTSTP, savetstp);
154 (void)signal(SIGTTOU, savettou);
155 (void)signal(SIGTTIN, savettin);
156 #ifndef TIOCSTI
157 ttybuf.c_cc[VERASE] = c_erase;
158 ttybuf.c_cc[VKILL] = c_kill;
159 if (ttyset)
160 tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
161 (void)signal(SIGQUIT, savequit);
162 #else
163 # ifdef TIOCEXT
164 if (extproc) {
165 flag = 1;
166 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
167 warn("TIOCEXT: on");
168 }
169 # endif /* TIOCEXT */
170 #endif
171 (void)signal(SIGINT, saveint);
172 return(errs);
173 }
174
175 /*
176 * Read up a header from standard input.
177 * The source string has the preliminary contents to
178 * be read.
179 *
180 */
181
182 char *
183 readtty(const char pr[], char src[])
184 {
185 char ch, canonb[BUFSIZ];
186 int c;
187 char *cp, *cp2;
188 static char empty[] = "";
189 #if __GNUC__
190 /* Avoid longjmp clobbering */
191 (void)&c;
192 (void)&cp2;
193 #endif
194
195 (void)fputs(pr, stdout);
196 (void)fflush(stdout);
197 if (src != NULL && strlen(src) > BUFSIZ - 2) {
198 (void)printf("too long to edit\n");
199 return(src);
200 }
201 #ifndef TIOCSTI
202 if (src != NULL)
203 cp = copy(src, canonb);
204 else
205 cp = copy("", canonb);
206 (void)fputs(canonb, stdout);
207 (void)fflush(stdout);
208 #else
209 cp = src == NULL ? empty : src;
210 while ((c = *cp++) != '\0') {
211 if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
212 (c_kill != _POSIX_VDISABLE && c == c_kill)) {
213 ch = '\\';
214 (void)ioctl(0, TIOCSTI, &ch);
215 }
216 ch = c;
217 (void)ioctl(0, TIOCSTI, &ch);
218 }
219 cp = canonb;
220 *cp = 0;
221 #endif
222 cp2 = cp;
223 while (cp2 < canonb + BUFSIZ)
224 *cp2++ = 0;
225 cp2 = cp;
226 if (setjmp(rewrite))
227 goto redo;
228 (void)signal(SIGTSTP, ttystop);
229 (void)signal(SIGTTOU, ttystop);
230 (void)signal(SIGTTIN, ttystop);
231 clearerr(stdin);
232 while (cp2 < canonb + BUFSIZ) {
233 c = getc(stdin);
234 if (c == EOF || c == '\n')
235 break;
236 *cp2++ = c;
237 }
238 *cp2 = 0;
239 (void)signal(SIGTSTP, SIG_DFL);
240 (void)signal(SIGTTOU, SIG_DFL);
241 (void)signal(SIGTTIN, SIG_DFL);
242 if (c == EOF && ferror(stdin)) {
243 redo:
244 cp = strlen(canonb) > 0 ? canonb : NULL;
245 clearerr(stdin);
246 return(readtty(pr, cp));
247 }
248 #ifndef TIOCSTI
249 if (cp == NULL || *cp == '\0')
250 return(src);
251 cp2 = cp;
252 if (!ttyset)
253 return(strlen(canonb) > 0 ? savestr(canonb) : NULL);
254 while (*cp != '\0') {
255 c = *cp++;
256 if (c_erase != _POSIX_VDISABLE && c == c_erase) {
257 if (cp2 == canonb)
258 continue;
259 if (cp2[-1] == '\\') {
260 cp2[-1] = c;
261 continue;
262 }
263 cp2--;
264 continue;
265 }
266 if (c_kill != _POSIX_VDISABLE && c == c_kill) {
267 if (cp2 == canonb)
268 continue;
269 if (cp2[-1] == '\\') {
270 cp2[-1] = c;
271 continue;
272 }
273 cp2 = canonb;
274 continue;
275 }
276 *cp2++ = c;
277 }
278 *cp2 = '\0';
279 #endif
280 if (equal("", canonb))
281 return(NULL);
282 return(savestr(canonb));
283 }
284
285 /*
286 * Receipt continuation.
287 */
288 void
289 ttystop(int s)
290 {
291 sig_t old_action = signal(s, SIG_DFL);
292 sigset_t nset;
293
294 (void)sigemptyset(&nset);
295 (void)sigaddset(&nset, s);
296 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
297 (void)kill(0, s);
298 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
299 (void)signal(s, old_action);
300 longjmp(rewrite, 1);
301 }
302
303 /*ARGSUSED*/
304 void
305 ttyint(int s)
306 {
307 longjmp(intjmp, 1);
308 }
309