tty.c revision 1.10 1 /* $NetBSD: tty.c,v 1.10 1998/12/19 16:35:10 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: tty.c,v 1.10 1998/12/19 16:35:10 christos Exp $");
42 #endif
43 #endif /* not lint */
44
45 /*
46 * Mail -- a mail program
47 *
48 * Generally useful tty stuff.
49 */
50
51 #include "rcv.h"
52 #include "extern.h"
53
54 static cc_t c_erase; /* Current erase char */
55 static cc_t c_kill; /* Current kill char */
56 static jmp_buf rewrite; /* Place to go when continued */
57 static jmp_buf intjmp; /* Place to go when interrupted */
58 #ifndef TIOCSTI
59 static int ttyset; /* We must now do erase/kill */
60 #endif
61
62 /*
63 * Read all relevant header fields.
64 */
65
66 int
67 grabh(hp, gflags)
68 struct header *hp;
69 int gflags;
70 {
71 struct termios ttybuf;
72 sig_t saveint;
73 sig_t savetstp;
74 sig_t savettou;
75 sig_t savettin;
76 int errs;
77 #ifndef TIOCSTI
78 sig_t savequit;
79 #else
80 # ifdef TIOCEXT
81 int extproc, flag;
82 # endif /* TIOCEXT */
83 #endif /* TIOCSTI */
84
85 #ifdef __GNUC__
86 /* Avoid longjmp clobbering */
87 # if !defined(TIOCSTI) && defined(TIOCEXT)
88 (void) &extproc;
89 # endif
90 (void) &saveint;
91 #endif /* __GNUC__ */
92
93 savetstp = signal(SIGTSTP, SIG_DFL);
94 savettou = signal(SIGTTOU, SIG_DFL);
95 savettin = signal(SIGTTIN, SIG_DFL);
96 errs = 0;
97 #ifndef TIOCSTI
98 ttyset = 0;
99 #endif
100 if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
101 perror("tcgetattr");
102 return(-1);
103 }
104 c_erase = ttybuf.c_cc[VERASE];
105 c_kill = ttybuf.c_cc[VKILL];
106 #ifndef TIOCSTI
107 ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
108 ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
109 if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
110 signal(SIGINT, SIG_DFL);
111 if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
112 signal(SIGQUIT, SIG_DFL);
113 #else
114 # ifdef TIOCEXT
115 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
116 if (extproc) {
117 flag = 0;
118 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
119 perror("TIOCEXT: off");
120 }
121 # endif /* TIOCEXT */
122 if (setjmp(intjmp))
123 goto out;
124 saveint = signal(SIGINT, ttyint);
125 #endif
126 if (gflags & GTO) {
127 #ifndef TIOCSTI
128 if (!ttyset && hp->h_to != NIL)
129 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
130 #endif
131 hp->h_to =
132 extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
133 }
134 if (gflags & GSUBJECT) {
135 #ifndef TIOCSTI
136 if (!ttyset && hp->h_subject != NOSTR)
137 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
138 #endif
139 hp->h_subject = readtty("Subject: ", hp->h_subject);
140 }
141 if (gflags & GCC) {
142 #ifndef TIOCSTI
143 if (!ttyset && hp->h_cc != NIL)
144 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
145 #endif
146 hp->h_cc =
147 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
148 }
149 if (gflags & GBCC) {
150 #ifndef TIOCSTI
151 if (!ttyset && hp->h_bcc != NIL)
152 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
153 #endif
154 hp->h_bcc =
155 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
156 }
157 out:
158 signal(SIGTSTP, savetstp);
159 signal(SIGTTOU, savettou);
160 signal(SIGTTIN, savettin);
161 #ifndef TIOCSTI
162 ttybuf.c_cc[VERASE] = c_erase;
163 ttybuf.c_cc[VKILL] = c_kill;
164 if (ttyset)
165 tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
166 signal(SIGQUIT, savequit);
167 #else
168 # ifdef TIOCEXT
169 if (extproc) {
170 flag = 1;
171 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
172 perror("TIOCEXT: on");
173 }
174 # endif /* TIOCEXT */
175 #endif
176 signal(SIGINT, saveint);
177 return(errs);
178 }
179
180 /*
181 * Read up a header from standard input.
182 * The source string has the preliminary contents to
183 * be read.
184 *
185 */
186
187 char *
188 readtty(pr, src)
189 char pr[], src[];
190 {
191 char ch, canonb[BUFSIZ];
192 int c;
193 char *cp, *cp2;
194 #if __GNUC__
195 /* Avoid longjmp clobbering */
196 (void) &c;
197 (void) &cp2;
198 #endif
199
200 fputs(pr, stdout);
201 fflush(stdout);
202 if (src != NOSTR && strlen(src) > BUFSIZ - 2) {
203 printf("too long to edit\n");
204 return(src);
205 }
206 #ifndef TIOCSTI
207 if (src != NOSTR)
208 cp = copy(src, canonb);
209 else
210 cp = copy("", canonb);
211 fputs(canonb, stdout);
212 fflush(stdout);
213 #else
214 cp = src == NOSTR ? "" : src;
215 while ((c = *cp++) != '\0') {
216 if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
217 (c_kill != _POSIX_VDISABLE && c == c_kill)) {
218 ch = '\\';
219 ioctl(0, TIOCSTI, &ch);
220 }
221 ch = c;
222 ioctl(0, TIOCSTI, &ch);
223 }
224 cp = canonb;
225 *cp = 0;
226 #endif
227 cp2 = cp;
228 while (cp2 < canonb + BUFSIZ)
229 *cp2++ = 0;
230 cp2 = cp;
231 if (setjmp(rewrite))
232 goto redo;
233 signal(SIGTSTP, ttystop);
234 signal(SIGTTOU, ttystop);
235 signal(SIGTTIN, ttystop);
236 clearerr(stdin);
237 while (cp2 < canonb + BUFSIZ) {
238 c = getc(stdin);
239 if (c == EOF || c == '\n')
240 break;
241 *cp2++ = c;
242 }
243 *cp2 = 0;
244 signal(SIGTSTP, SIG_DFL);
245 signal(SIGTTOU, SIG_DFL);
246 signal(SIGTTIN, SIG_DFL);
247 if (c == EOF && ferror(stdin)) {
248 redo:
249 cp = strlen(canonb) > 0 ? canonb : NOSTR;
250 clearerr(stdin);
251 return(readtty(pr, cp));
252 }
253 #ifndef TIOCSTI
254 if (cp == NOSTR || *cp == '\0')
255 return(src);
256 cp2 = cp;
257 if (!ttyset)
258 return(strlen(canonb) > 0 ? savestr(canonb) : NOSTR);
259 while (*cp != '\0') {
260 c = *cp++;
261 if (c_erase != _POSIX_VDISABLE && c == c_erase) {
262 if (cp2 == canonb)
263 continue;
264 if (cp2[-1] == '\\') {
265 cp2[-1] = c;
266 continue;
267 }
268 cp2--;
269 continue;
270 }
271 if (c_kill != _POSIX_VDISABLE && c == c_kill) {
272 if (cp2 == canonb)
273 continue;
274 if (cp2[-1] == '\\') {
275 cp2[-1] = c;
276 continue;
277 }
278 cp2 = canonb;
279 continue;
280 }
281 *cp2++ = c;
282 }
283 *cp2 = '\0';
284 #endif
285 if (equal("", canonb))
286 return(NOSTR);
287 return(savestr(canonb));
288 }
289
290 /*
291 * Receipt continuation.
292 */
293 void
294 ttystop(s)
295 int s;
296 {
297 sig_t old_action = signal(s, SIG_DFL);
298 sigset_t nset;
299
300 sigemptyset(&nset);
301 sigaddset(&nset, s);
302 sigprocmask(SIG_BLOCK, &nset, NULL);
303 kill(0, s);
304 sigprocmask(SIG_UNBLOCK, &nset, NULL);
305 signal(s, old_action);
306 longjmp(rewrite, 1);
307 }
308
309 /*ARGSUSED*/
310 void
311 ttyint(s)
312 int s;
313 {
314 longjmp(intjmp, 1);
315 }
316