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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)tty.c 8.2 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/mail/tty.c,v 1.2.8.3 2003/01/06 05:46:04 mikeh Exp $
31 * $DragonFly: src/usr.bin/mail/tty.c,v 1.5 2004/09/08 03:01:11 joerg Exp $
35 * Mail -- a mail program
37 * Generally useful tty stuff.
43 static cc_t c_erase; /* Current erase char */
44 static cc_t c_kill; /* Current kill char */
45 static jmp_buf rewrite; /* Place to go when continued */
46 static jmp_buf intjmp; /* Place to go when interrupted */
49 * Read all relevant header fields.
53 grabh(struct header *hp, int gflags)
55 struct termios ttybuf;
63 savetstp = signal(SIGTSTP, SIG_DFL);
64 savettou = signal(SIGTTOU, SIG_DFL);
65 savettin = signal(SIGTTIN, SIG_DFL);
67 if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
68 warn("tcgetattr(stdin)");
71 c_erase = ttybuf.c_cc[VERASE];
72 c_kill = ttybuf.c_cc[VKILL];
73 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
76 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
81 saveint = signal(SIGINT, ttyint);
84 extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
86 if (gflags & GSUBJECT) {
87 hp->h_subject = readtty("Subject: ", hp->h_subject);
91 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
95 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
98 signal(SIGTSTP, savetstp);
99 signal(SIGTTOU, savettou);
100 signal(SIGTTIN, savettin);
103 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
106 signal(SIGINT, saveint);
111 * Read up a header from standard input.
112 * The source string has the preliminary contents to
118 readtty(const char *pr, char *src)
120 char ch, canonb[BUFSIZ];
126 if (src != NULL && strlen(src) > BUFSIZ - 2) {
127 printf("too long to edit\n");
130 cp = src == NULL ? "" : src;
131 while ((c = *cp++) != '\0') {
132 if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
133 (c_kill != _POSIX_VDISABLE && c == c_kill)) {
135 ioctl(0, TIOCSTI, &ch);
138 ioctl(0, TIOCSTI, &ch);
143 while (cp2 < canonb + BUFSIZ)
148 signal(SIGTSTP, ttystop);
149 signal(SIGTTOU, ttystop);
150 signal(SIGTTIN, ttystop);
152 while (cp2 < canonb + BUFSIZ) {
154 if (c == EOF || c == '\n')
159 signal(SIGTSTP, SIG_DFL);
160 signal(SIGTTOU, SIG_DFL);
161 signal(SIGTTIN, SIG_DFL);
162 if (c == EOF && ferror(stdin)) {
164 cp = strlen(canonb) > 0 ? canonb : NULL;
166 return (readtty(pr, cp));
168 if (equal("", canonb))
170 return (savestr(canonb));
174 * Receipt continuation.
179 sig_t old_action = signal(s, SIG_DFL);
184 sigprocmask(SIG_BLOCK, &nset, NULL);
186 sigprocmask(SIG_UNBLOCK, &nset, NULL);
187 signal(s, old_action);