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