tty.c revision 1.23 1 /* $NetBSD: tty.c,v 1.23 2006/09/27 12:58:33 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.23 2006/09/27 12:58:33 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 static sig_t saveint;
70 static sig_t savetstp;
71 static sig_t savettou;
72 static sig_t savettin;
73 int errs;
74 #ifndef TIOCSTI
75 static sig_t savequit;
76 #else
77 # ifdef TIOCEXT
78 static int extproc;
79 int flag;
80 # endif /* TIOCEXT */
81 #endif /* TIOCSTI */
82
83 savetstp = signal(SIGTSTP, SIG_DFL);
84 savettou = signal(SIGTTOU, SIG_DFL);
85 savettin = signal(SIGTTIN, SIG_DFL);
86 errs = 0;
87 #ifndef TIOCSTI
88 ttyset = 0;
89 #endif
90 if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
91 warn("tcgetattr");
92 return(-1);
93 }
94 c_erase = ttybuf.c_cc[VERASE];
95 c_kill = ttybuf.c_cc[VKILL];
96 #ifndef TIOCSTI
97 ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
98 ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
99 if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
100 (void)signal(SIGINT, SIG_DFL);
101 if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
102 (void)signal(SIGQUIT, SIG_DFL);
103 #else
104 # ifdef TIOCEXT
105 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
106 if (extproc) {
107 flag = 0;
108 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
109 warn("TIOCEXT: off");
110 }
111 # endif /* TIOCEXT */
112 if (setjmp(intjmp)) {
113 #ifdef USE_READLINE
114 (void)fputc('\n',stdout);
115 errs = 1;
116 #endif /* USE_READLINE */
117 goto out;
118 }
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 if (gflags & GSMOPTS) {
153 char *smopts;
154 char *argv[MAXARGC];
155 int argc, i;
156 struct name *np, *t;
157
158 #ifndef TIOCSTI
159 if (!ttyset && hp->h_smopts != NULL)
160 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
161 #endif
162 smopts = readtty("Smopts: ", detract(hp->h_smopts, GSMOPTS));
163
164 /* Parse smopts with getrawlist() rather than expand()
165 * to get a shell-like expansion.
166 */
167 hp->h_smopts = NULL;
168 if (smopts) {
169 np = NULL;
170 argc = getrawlist(smopts, argv, sizeof(argv)/sizeof(*argv));
171 for (i = 0 ; i < argc ; i++) {
172 t = nalloc(argv[i], GSMOPTS);
173 if (hp->h_smopts == NULL)
174 hp->h_smopts = t;
175 else
176 np->n_flink = t;
177 t->n_blink = np;
178 np = t;
179 }
180 }
181 }
182 out:
183 (void)signal(SIGTSTP, savetstp);
184 (void)signal(SIGTTOU, savettou);
185 (void)signal(SIGTTIN, savettin);
186 #ifndef TIOCSTI
187 ttybuf.c_cc[VERASE] = c_erase;
188 ttybuf.c_cc[VKILL] = c_kill;
189 if (ttyset)
190 tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
191 (void)signal(SIGQUIT, savequit);
192 #else
193 # ifdef TIOCEXT
194 if (extproc) {
195 flag = 1;
196 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
197 warn("TIOCEXT: on");
198 }
199 # endif /* TIOCEXT */
200 #endif
201 (void)signal(SIGINT, saveint);
202 return(errs);
203 }
204
205 /*
206 * Read up a header from standard input.
207 * The source string has the preliminary contents to
208 * be read.
209 *
210 */
211
212 #ifdef USE_READLINE
213 char *
214 readtty(const char pr[], char src[])
215 {
216 return savestr(rl_getline(pr, src));
217 }
218 #else
219 char *
220 readtty(const char pr[], char src[])
221 {
222 char ch, canonb[BUFSIZ];
223 int c;
224 char *cp, *cp2;
225 static char empty[] = "";
226 #if __GNUC__
227 /* Avoid longjmp clobbering */
228 (void)&c;
229 (void)&cp2;
230 #endif
231
232 (void)fputs(pr, stdout);
233 (void)fflush(stdout);
234 if (src != NULL && strlen(src) > BUFSIZ - 2) {
235 (void)printf("too long to edit\n");
236 return(src);
237 }
238 #ifndef TIOCSTI
239 if (src != NULL)
240 cp = copy(src, canonb);
241 else
242 cp = copy("", canonb);
243 (void)fputs(canonb, stdout);
244 (void)fflush(stdout);
245 #else
246 cp = src == NULL ? empty : src;
247 while ((c = *cp++) != '\0') {
248 if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
249 (c_kill != _POSIX_VDISABLE && c == c_kill)) {
250 ch = '\\';
251 (void)ioctl(0, TIOCSTI, &ch);
252 }
253 ch = c;
254 (void)ioctl(0, TIOCSTI, &ch);
255 }
256 cp = canonb;
257 *cp = 0;
258 #endif
259 cp2 = cp;
260 while (cp2 < canonb + BUFSIZ)
261 *cp2++ = 0;
262 cp2 = cp;
263 if (setjmp(rewrite))
264 goto redo;
265 (void)signal(SIGTSTP, ttystop);
266 (void)signal(SIGTTOU, ttystop);
267 (void)signal(SIGTTIN, ttystop);
268 clearerr(stdin);
269 while (cp2 < canonb + BUFSIZ) {
270 c = getc(stdin);
271 if (c == EOF || c == '\n')
272 break;
273 *cp2++ = c;
274 }
275 *cp2 = 0;
276 (void)signal(SIGTSTP, SIG_DFL);
277 (void)signal(SIGTTOU, SIG_DFL);
278 (void)signal(SIGTTIN, SIG_DFL);
279 if (c == EOF && ferror(stdin)) {
280 redo:
281 cp = strlen(canonb) > 0 ? canonb : NULL;
282 clearerr(stdin);
283 return(readtty(pr, cp));
284 }
285 #ifndef TIOCSTI
286 if (cp == NULL || *cp == '\0')
287 return(src);
288 cp2 = cp;
289 if (!ttyset)
290 return(strlen(canonb) > 0 ? savestr(canonb) : NULL);
291 while (*cp != '\0') {
292 c = *cp++;
293 if (c_erase != _POSIX_VDISABLE && c == c_erase) {
294 if (cp2 == canonb)
295 continue;
296 if (cp2[-1] == '\\') {
297 cp2[-1] = c;
298 continue;
299 }
300 cp2--;
301 continue;
302 }
303 if (c_kill != _POSIX_VDISABLE && c == c_kill) {
304 if (cp2 == canonb)
305 continue;
306 if (cp2[-1] == '\\') {
307 cp2[-1] = c;
308 continue;
309 }
310 cp2 = canonb;
311 continue;
312 }
313 *cp2++ = c;
314 }
315 *cp2 = '\0';
316 #endif
317 if (equal("", canonb))
318 return(NULL);
319 return(savestr(canonb));
320 }
321 #endif
322
323 /*
324 * Receipt continuation.
325 */
326 void
327 ttystop(int s)
328 {
329 sig_t old_action = signal(s, SIG_DFL);
330 sigset_t nset;
331
332 (void)sigemptyset(&nset);
333 (void)sigaddset(&nset, s);
334 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
335 (void)kill(0, s);
336 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
337 (void)signal(s, old_action);
338 longjmp(rewrite, 1);
339 }
340
341 /*ARGSUSED*/
342 void
343 ttyint(int s)
344 {
345 longjmp(intjmp, 1);
346 }
347