2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * Mail -- a mail program
43 * Generally useful tty stuff.
49 static cc_t c_erase; /* Current erase char */
50 static cc_t c_kill; /* Current kill char */
51 static jmp_buf rewrite; /* Place to go when continued */
52 static jmp_buf intjmp; /* Place to go when interrupted */
54 static int ttyset; /* We must now do erase/kill */
58 * Read all relevant header fields.
62 grabh(struct header *hp, int gflags)
64 struct termios ttybuf;
78 savetstp = signal(SIGTSTP, SIG_DFL);
79 savettou = signal(SIGTTOU, SIG_DFL);
80 savettin = signal(SIGTTIN, SIG_DFL);
85 if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
86 warn("tcgetattr(stdin)");
89 c_erase = ttybuf.c_cc[VERASE];
90 c_kill = ttybuf.c_cc[VKILL];
92 ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
93 ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
94 if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
95 (void)signal(SIGINT, SIG_DFL);
96 if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
97 (void)signal(SIGQUIT, SIG_DFL);
100 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
103 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
104 warn("TIOCEXT: off");
106 # endif /* TIOCEXT */
109 saveint = signal(SIGINT, ttyint);
113 if (!ttyset && hp->h_to != NULL)
114 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
117 extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
119 if (gflags & GSUBJECT) {
121 if (!ttyset && hp->h_subject != NULL)
122 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
124 hp->h_subject = readtty("Subject: ", hp->h_subject);
128 if (!ttyset && hp->h_cc != NULL)
129 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
132 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
136 if (!ttyset && hp->h_bcc != NULL)
137 ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
140 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
143 (void)signal(SIGTSTP, savetstp);
144 (void)signal(SIGTTOU, savettou);
145 (void)signal(SIGTTIN, savettin);
147 ttybuf.c_cc[VERASE] = c_erase;
148 ttybuf.c_cc[VKILL] = c_kill;
150 tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
151 (void)signal(SIGQUIT, savequit);
156 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
159 # endif /* TIOCEXT */
161 (void)signal(SIGINT, saveint);
166 * Read up a header from standard input.
167 * The source string has the preliminary contents to
173 readtty(const char *pr, char src[])
175 char ch, canonb[BUFSIZ];
180 (void)fflush(stdout);
181 if (src != NULL && strlen(src) > BUFSIZ - 2) {
182 printf("too long to edit\n");
187 strlcpy(canonb, src, sizeof(canonb));
190 fputs(canonb, stdout);
191 (void)fflush(stdout);
193 cp = src == NULL ? "" : src;
194 while ((c = *cp++) != '\0') {
195 if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
196 (c_kill != _POSIX_VDISABLE && c == c_kill)) {
198 ioctl(0, TIOCSTI, &ch);
201 ioctl(0, TIOCSTI, &ch);
207 while (cp2 < canonb + BUFSIZ)
212 (void)signal(SIGTSTP, ttystop);
213 (void)signal(SIGTTOU, ttystop);
214 (void)signal(SIGTTIN, ttystop);
216 while (cp2 < canonb + BUFSIZ) {
218 if (c == EOF || c == '\n')
223 (void)signal(SIGTSTP, SIG_DFL);
224 (void)signal(SIGTTOU, SIG_DFL);
225 (void)signal(SIGTTIN, SIG_DFL);
226 if (c == EOF && ferror(stdin)) {
228 cp = strlen(canonb) > 0 ? canonb : NULL;
230 return (readtty(pr, cp));
233 if (cp == NULL || *cp == '\0')
237 return (strlen(canonb) > 0 ? savestr(canonb) : NULL);
238 while (*cp != '\0') {
240 if (c_erase != _POSIX_VDISABLE && c == c_erase) {
243 if (cp2[-1] == '\\') {
250 if (c_kill != _POSIX_VDISABLE && c == c_kill) {
253 if (cp2[-1] == '\\') {
264 if (equal("", canonb))
266 return (savestr(canonb));
270 * Receipt continuation.
275 sig_t old_action = signal(s, SIG_DFL);
278 (void)sigemptyset(&nset);
279 (void)sigaddset(&nset, s);
280 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
282 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
283 (void)signal(s, old_action);
288 ttyint(int s __unused)