tty.c revision 1.27 1 1.24 christos /* $NetBSD: tty.c,v 1.27 2006/11/28 18:45:32 christos Exp $ */
2 1.5 christos
3 1.1 cgd /*
4 1.3 deraadt * Copyright (c) 1980, 1993
5 1.3 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.17 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.8 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.5 christos #if 0
35 1.6 tls static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 6/6/93";
36 1.5 christos #else
37 1.24 christos __RCSID("$NetBSD: tty.c,v 1.27 2006/11/28 18:45:32 christos Exp $");
38 1.5 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * Mail -- a mail program
43 1.1 cgd *
44 1.1 cgd * Generally useful tty stuff.
45 1.1 cgd */
46 1.1 cgd
47 1.1 cgd #include "rcv.h"
48 1.3 deraadt #include "extern.h"
49 1.25 christos #ifdef USE_EDITLINE
50 1.21 christos #include "complete.h"
51 1.21 christos #endif
52 1.1 cgd
53 1.25 christos #if !defined(USE_EDITLINE) || !defined(TIOCSTI)
54 1.4 mycroft static cc_t c_erase; /* Current erase char */
55 1.4 mycroft static cc_t c_kill; /* Current kill char */
56 1.25 christos #endif
57 1.25 christos #ifndef USE_EDITLINE
58 1.1 cgd static jmp_buf rewrite; /* Place to go when continued */
59 1.25 christos #endif
60 1.1 cgd static jmp_buf intjmp; /* Place to go when interrupted */
61 1.1 cgd #ifndef TIOCSTI
62 1.1 cgd static int ttyset; /* We must now do erase/kill */
63 1.1 cgd #endif
64 1.1 cgd
65 1.24 christos
66 1.1 cgd /*
67 1.27 christos * Read up a header from standard input.
68 1.27 christos * The source string has the preliminary contents to
69 1.27 christos * be read.
70 1.27 christos *
71 1.27 christos */
72 1.27 christos #ifdef USE_EDITLINE
73 1.27 christos static char *
74 1.27 christos readtty(const char pr[], char src[])
75 1.27 christos {
76 1.27 christos char *line;
77 1.27 christos line = my_gets(&elm.string, pr, src);
78 1.27 christos #if 0
79 1.27 christos return line ? savestr(line) : __UNCONST("");
80 1.27 christos #else
81 1.27 christos if (line)
82 1.27 christos return savestr(line);
83 1.27 christos else
84 1.27 christos return __UNCONST("");
85 1.27 christos #endif
86 1.27 christos }
87 1.27 christos
88 1.27 christos #else /* USE_EDITLINE */
89 1.27 christos
90 1.27 christos /*
91 1.27 christos * Receipt continuation.
92 1.27 christos */
93 1.27 christos static void
94 1.27 christos ttystop(int s)
95 1.27 christos {
96 1.27 christos sig_t old_action = signal(s, SIG_DFL);
97 1.27 christos sigset_t nset;
98 1.27 christos
99 1.27 christos (void)sigemptyset(&nset);
100 1.27 christos (void)sigaddset(&nset, s);
101 1.27 christos (void)sigprocmask(SIG_BLOCK, &nset, NULL);
102 1.27 christos (void)kill(0, s);
103 1.27 christos (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
104 1.27 christos (void)signal(s, old_action);
105 1.27 christos longjmp(rewrite, 1);
106 1.27 christos }
107 1.27 christos
108 1.27 christos static char *
109 1.27 christos readtty(const char pr[], char src[])
110 1.27 christos {
111 1.27 christos /* XXX - watch for potential setjmp/longjmp clobbering!
112 1.27 christos * Currently there appear to be none.
113 1.27 christos */
114 1.27 christos char canonb[LINESIZE];
115 1.27 christos int c;
116 1.27 christos char *cp, *cp2;
117 1.27 christos #ifdef TIOCSTI
118 1.27 christos char ch;
119 1.27 christos static char empty[] = "";
120 1.27 christos #endif
121 1.27 christos (void)fputs(pr, stdout);
122 1.27 christos (void)fflush(stdout);
123 1.27 christos if (src != NULL && strlen(src) > sizeof(canonb) - 2) {
124 1.27 christos (void)printf("too long to edit\n");
125 1.27 christos return src;
126 1.27 christos }
127 1.27 christos #ifndef TIOCSTI
128 1.27 christos if (src != NULL)
129 1.27 christos cp = copy(src, canonb);
130 1.27 christos else
131 1.27 christos cp = copy("", canonb);
132 1.27 christos (void)fputs(canonb, stdout);
133 1.27 christos (void)fflush(stdout);
134 1.27 christos #else
135 1.27 christos cp = src == NULL ? empty : src;
136 1.27 christos while ((c = *cp++) != '\0') {
137 1.27 christos if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
138 1.27 christos (c_kill != _POSIX_VDISABLE && c == c_kill)) {
139 1.27 christos ch = '\\';
140 1.27 christos (void)ioctl(0, TIOCSTI, &ch);
141 1.27 christos }
142 1.27 christos ch = c;
143 1.27 christos (void)ioctl(0, TIOCSTI, &ch);
144 1.27 christos }
145 1.27 christos cp = canonb;
146 1.27 christos *cp = 0;
147 1.27 christos #endif
148 1.27 christos cp2 = cp;
149 1.27 christos while (cp2 < canonb + sizeof(canonb))
150 1.27 christos *cp2++ = 0;
151 1.27 christos cp2 = cp;
152 1.27 christos if (setjmp(rewrite))
153 1.27 christos goto redo;
154 1.27 christos (void)signal(SIGTSTP, ttystop);
155 1.27 christos (void)signal(SIGTTOU, ttystop);
156 1.27 christos (void)signal(SIGTTIN, ttystop);
157 1.27 christos clearerr(stdin);
158 1.27 christos while (cp2 < canonb + sizeof(canonb)) {
159 1.27 christos c = getc(stdin);
160 1.27 christos if (c == EOF || c == '\n')
161 1.27 christos break;
162 1.27 christos *cp2++ = c;
163 1.27 christos }
164 1.27 christos *cp2 = 0;
165 1.27 christos (void)signal(SIGTSTP, SIG_DFL);
166 1.27 christos (void)signal(SIGTTOU, SIG_DFL);
167 1.27 christos (void)signal(SIGTTIN, SIG_DFL);
168 1.27 christos if (c == EOF && ferror(stdin)) {
169 1.27 christos redo:
170 1.27 christos cp = strlen(canonb) > 0 ? canonb : NULL;
171 1.27 christos clearerr(stdin);
172 1.27 christos return readtty(pr, cp);
173 1.27 christos }
174 1.27 christos #ifndef TIOCSTI
175 1.27 christos if (cp == NULL || *cp == '\0')
176 1.27 christos return src;
177 1.27 christos cp2 = cp;
178 1.27 christos if (!ttyset)
179 1.27 christos return strlen(canonb) > 0 ? savestr(canonb) : NULL;
180 1.27 christos while (*cp != '\0') {
181 1.27 christos c = *cp++;
182 1.27 christos if (c_erase != _POSIX_VDISABLE && c == c_erase) {
183 1.27 christos if (cp2 == canonb)
184 1.27 christos continue;
185 1.27 christos if (cp2[-1] == '\\') {
186 1.27 christos cp2[-1] = c;
187 1.27 christos continue;
188 1.27 christos }
189 1.27 christos cp2--;
190 1.27 christos continue;
191 1.27 christos }
192 1.27 christos if (c_kill != _POSIX_VDISABLE && c == c_kill) {
193 1.27 christos if (cp2 == canonb)
194 1.27 christos continue;
195 1.27 christos if (cp2[-1] == '\\') {
196 1.27 christos cp2[-1] = c;
197 1.27 christos continue;
198 1.27 christos }
199 1.27 christos cp2 = canonb;
200 1.27 christos continue;
201 1.27 christos }
202 1.27 christos *cp2++ = c;
203 1.27 christos }
204 1.27 christos *cp2 = '\0';
205 1.27 christos #endif
206 1.27 christos if (equal("", canonb))
207 1.27 christos return NULL;
208 1.27 christos return savestr(canonb);
209 1.27 christos }
210 1.27 christos #endif /* USE_EDITLINE */
211 1.27 christos
212 1.27 christos
213 1.27 christos /*ARGSUSED*/
214 1.27 christos static void
215 1.27 christos ttyint(int s __unused)
216 1.27 christos {
217 1.27 christos longjmp(intjmp, 1);
218 1.27 christos }
219 1.27 christos
220 1.27 christos /*
221 1.1 cgd * Read all relevant header fields.
222 1.1 cgd */
223 1.27 christos PUBLIC int
224 1.12 wiz grabh(struct header *hp, int gflags)
225 1.1 cgd {
226 1.4 mycroft struct termios ttybuf;
227 1.24 christos
228 1.25 christos /* The following are declared volatile to avoid longjmp
229 1.25 christos * clobbering, though they seem safe without it! */
230 1.25 christos sig_t volatile saveint;
231 1.25 christos sig_t volatile savetstp;
232 1.25 christos sig_t volatile savettou;
233 1.25 christos sig_t volatile savettin;
234 1.1 cgd #ifndef TIOCSTI
235 1.25 christos sig_t volatile savequit;
236 1.6 tls #else
237 1.10 christos # ifdef TIOCEXT
238 1.25 christos int volatile extproc;
239 1.10 christos # endif /* TIOCEXT */
240 1.10 christos #endif /* TIOCSTI */
241 1.26 christos int retval;
242 1.10 christos
243 1.1 cgd savetstp = signal(SIGTSTP, SIG_DFL);
244 1.1 cgd savettou = signal(SIGTTOU, SIG_DFL);
245 1.1 cgd savettin = signal(SIGTTIN, SIG_DFL);
246 1.1 cgd #ifndef TIOCSTI
247 1.1 cgd ttyset = 0;
248 1.1 cgd #endif
249 1.4 mycroft if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
250 1.16 wiz warn("tcgetattr");
251 1.27 christos return -1;
252 1.1 cgd }
253 1.25 christos #if !defined(USE_EDITLINE) || !defined(TIOCSTI)
254 1.4 mycroft c_erase = ttybuf.c_cc[VERASE];
255 1.4 mycroft c_kill = ttybuf.c_cc[VKILL];
256 1.25 christos #endif
257 1.1 cgd #ifndef TIOCSTI
258 1.9 mycroft ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
259 1.9 mycroft ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
260 1.1 cgd if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
261 1.19 christos (void)signal(SIGINT, SIG_DFL);
262 1.1 cgd if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
263 1.19 christos (void)signal(SIGQUIT, SIG_DFL);
264 1.1 cgd #else
265 1.6 tls # ifdef TIOCEXT
266 1.6 tls extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
267 1.6 tls if (extproc) {
268 1.25 christos int flag;
269 1.6 tls flag = 0;
270 1.6 tls if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
271 1.16 wiz warn("TIOCEXT: off");
272 1.6 tls }
273 1.6 tls # endif /* TIOCEXT */
274 1.25 christos saveint = signal(SIGINT, ttyint); /* must precede setjmp to be saved */
275 1.26 christos if ((retval = setjmp(intjmp)) != 0) {
276 1.24 christos (void)fputc('\n', stdout);
277 1.1 cgd goto out;
278 1.22 christos }
279 1.1 cgd #endif
280 1.1 cgd if (gflags & GTO) {
281 1.1 cgd #ifndef TIOCSTI
282 1.14 wiz if (!ttyset && hp->h_to != NULL)
283 1.4 mycroft ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
284 1.1 cgd #endif
285 1.1 cgd hp->h_to =
286 1.1 cgd extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
287 1.1 cgd }
288 1.1 cgd if (gflags & GSUBJECT) {
289 1.1 cgd #ifndef TIOCSTI
290 1.13 wiz if (!ttyset && hp->h_subject != NULL)
291 1.4 mycroft ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
292 1.1 cgd #endif
293 1.1 cgd hp->h_subject = readtty("Subject: ", hp->h_subject);
294 1.1 cgd }
295 1.1 cgd if (gflags & GCC) {
296 1.1 cgd #ifndef TIOCSTI
297 1.14 wiz if (!ttyset && hp->h_cc != NULL)
298 1.4 mycroft ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
299 1.1 cgd #endif
300 1.1 cgd hp->h_cc =
301 1.1 cgd extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
302 1.1 cgd }
303 1.1 cgd if (gflags & GBCC) {
304 1.1 cgd #ifndef TIOCSTI
305 1.14 wiz if (!ttyset && hp->h_bcc != NULL)
306 1.4 mycroft ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
307 1.1 cgd #endif
308 1.1 cgd hp->h_bcc =
309 1.1 cgd extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
310 1.1 cgd }
311 1.21 christos if (gflags & GSMOPTS) {
312 1.21 christos char *smopts;
313 1.21 christos #ifndef TIOCSTI
314 1.21 christos if (!ttyset && hp->h_smopts != NULL)
315 1.21 christos ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
316 1.21 christos #endif
317 1.21 christos smopts = readtty("Smopts: ", detract(hp->h_smopts, GSMOPTS));
318 1.21 christos
319 1.21 christos /* Parse smopts with getrawlist() rather than expand()
320 1.21 christos * to get a shell-like expansion.
321 1.21 christos */
322 1.21 christos hp->h_smopts = NULL;
323 1.21 christos if (smopts) {
324 1.25 christos struct name *np, *t;
325 1.25 christos char *argv[MAXARGC];
326 1.25 christos int argc, i;
327 1.25 christos
328 1.21 christos np = NULL;
329 1.21 christos argc = getrawlist(smopts, argv, sizeof(argv)/sizeof(*argv));
330 1.25 christos for (i = 0; i < argc; i++) {
331 1.21 christos t = nalloc(argv[i], GSMOPTS);
332 1.21 christos if (hp->h_smopts == NULL)
333 1.21 christos hp->h_smopts = t;
334 1.21 christos else
335 1.21 christos np->n_flink = t;
336 1.21 christos t->n_blink = np;
337 1.21 christos np = t;
338 1.21 christos }
339 1.21 christos }
340 1.25 christos #ifdef MIME_SUPPORT
341 1.25 christos if (hp->h_attach) {
342 1.25 christos struct attachment *ap;
343 1.25 christos int i;
344 1.25 christos i = 0;
345 1.25 christos for (ap = hp->h_attach; ap; ap = ap->a_flink)
346 1.25 christos i++;
347 1.25 christos (void)printf("Attachment%s: %d\n", i > 1 ? "s" : "", i);
348 1.25 christos }
349 1.25 christos #endif
350 1.21 christos }
351 1.25 christos #ifdef TIOCSTI
352 1.1 cgd out:
353 1.25 christos #endif
354 1.19 christos (void)signal(SIGTSTP, savetstp);
355 1.19 christos (void)signal(SIGTTOU, savettou);
356 1.19 christos (void)signal(SIGTTIN, savettin);
357 1.1 cgd #ifndef TIOCSTI
358 1.4 mycroft ttybuf.c_cc[VERASE] = c_erase;
359 1.4 mycroft ttybuf.c_cc[VKILL] = c_kill;
360 1.1 cgd if (ttyset)
361 1.4 mycroft tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
362 1.19 christos (void)signal(SIGQUIT, savequit);
363 1.6 tls #else
364 1.6 tls # ifdef TIOCEXT
365 1.6 tls if (extproc) {
366 1.25 christos int flag;
367 1.6 tls flag = 1;
368 1.6 tls if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
369 1.16 wiz warn("TIOCEXT: on");
370 1.6 tls }
371 1.6 tls # endif /* TIOCEXT */
372 1.1 cgd #endif
373 1.19 christos (void)signal(SIGINT, saveint);
374 1.26 christos return retval;
375 1.1 cgd }
376