2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 6/6/93";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * Mail -- a mail program
45 * Generally useful tty stuff.
51 static cc_t c_erase; /* Current erase char */
52 static cc_t c_kill; /* Current kill char */
53 static jmp_buf rewrite; /* Place to go when continued */
54 static jmp_buf intjmp; /* Place to go when interrupted */
56 static int ttyset; /* We must now do erase/kill */
60 * Read all relevant header fields.
68 struct termios ttybuf;
82 savetstp = signal(SIGTSTP, SIG_DFL);
83 savettou = signal(SIGTTOU, SIG_DFL);
84 savettin = signal(SIGTTIN, SIG_DFL);
89 if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
90 warn("tcgetattr(stdin)");
93 c_erase = ttybuf.c_cc[VERASE];
94 c_kill = ttybuf.c_cc[VKILL];
96 ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
97 ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
98 if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
99 (void)signal(SIGINT, SIG_DFL);
100 if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
101 (void)signal(SIGQUIT, SIG_DFL);
104 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
107 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
108 warn("TIOCEXT: off");
110 # endif /* TIOCEXT */
113 saveint = signal(SIGINT, ttyint);
117 if (!ttyset && hp->h_to != NULL)
118 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
121 extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
123 if (gflags & GSUBJECT) {
125 if (!ttyset && hp->h_subject != NULL)
126 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
128 hp->h_subject = readtty("Subject: ", hp->h_subject);
132 if (!ttyset && hp->h_cc != NULL)
133 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
136 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
140 if (!ttyset && hp->h_bcc != NULL)
141 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
144 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
147 (void)signal(SIGTSTP, savetstp);
148 (void)signal(SIGTTOU, savettou);
149 (void)signal(SIGTTIN, savettin);
151 ttybuf.c_cc[VERASE] = c_erase;
152 ttybuf.c_cc[VKILL] = c_kill;
154 tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
155 (void)signal(SIGQUIT, savequit);
160 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
163 # endif /* TIOCEXT */
165 (void)signal(SIGINT, saveint);
170 * Read up a header from standard input.
171 * The source string has the preliminary contents to
181 char ch, canonb[BUFSIZ];
186 (void)fflush(stdout);
187 if (src != NULL && strlen(src) > BUFSIZ - 2) {
188 printf("too long to edit\n");
193 strlcpy(canonb, src, sizeof(canonb));
196 fputs(canonb, stdout);
197 (void)fflush(stdout);
199 cp = src == NULL ? "" : src;
200 while ((c = *cp++) != '\0') {
201 if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
202 (c_kill != _POSIX_VDISABLE && c == c_kill)) {
204 ioctl(0, TIOCSTI, &ch);
207 ioctl(0, TIOCSTI, &ch);
213 while (cp2 < canonb + BUFSIZ)
218 (void)signal(SIGTSTP, ttystop);
219 (void)signal(SIGTTOU, ttystop);
220 (void)signal(SIGTTIN, ttystop);
222 while (cp2 < canonb + BUFSIZ) {
224 if (c == EOF || c == '\n')
229 (void)signal(SIGTSTP, SIG_DFL);
230 (void)signal(SIGTTOU, SIG_DFL);
231 (void)signal(SIGTTIN, SIG_DFL);
232 if (c == EOF && ferror(stdin)) {
234 cp = strlen(canonb) > 0 ? canonb : NULL;
236 return (readtty(pr, cp));
239 if (cp == NULL || *cp == '\0')
243 return (strlen(canonb) > 0 ? savestr(canonb) : NULL);
244 while (*cp != '\0') {
246 if (c_erase != _POSIX_VDISABLE && c == c_erase) {
249 if (cp2[-1] == '\\') {
256 if (c_kill != _POSIX_VDISABLE && c == c_kill) {
259 if (cp2[-1] == '\\') {
270 if (equal("", canonb))
272 return (savestr(canonb));
276 * Receipt continuation.
282 sig_t old_action = signal(s, SIG_DFL);
285 (void)sigemptyset(&nset);
286 (void)sigaddset(&nset, s);
287 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
289 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
290 (void)signal(s, old_action);