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