Correct param.h entry for this version.
[dragonfly.git] / usr.bin / mail / tty.c
blob19ba8e7a23d9282a961de3130762806b7a0d07d5
1 /*
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
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. 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
27 * SUCH DAMAGE.
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.
40 #include "rcv.h"
41 #include "extern.h"
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.
52 int
53 grabh(struct header *hp, int gflags)
55 struct termios ttybuf;
56 sig_t saveint;
57 sig_t savetstp;
58 sig_t savettou;
59 sig_t savettin;
60 int errs;
61 int extproc, flag;
63 savetstp = signal(SIGTSTP, SIG_DFL);
64 savettou = signal(SIGTTOU, SIG_DFL);
65 savettin = signal(SIGTTIN, SIG_DFL);
66 errs = 0;
67 if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
68 warn("tcgetattr(stdin)");
69 return (-1);
71 c_erase = ttybuf.c_cc[VERASE];
72 c_kill = ttybuf.c_cc[VKILL];
73 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
74 if (extproc) {
75 flag = 0;
76 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
77 warn("TIOCEXT: off");
79 if (setjmp(intjmp))
80 goto out;
81 saveint = signal(SIGINT, ttyint);
82 if (gflags & GTO) {
83 hp->h_to =
84 extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
86 if (gflags & GSUBJECT) {
87 hp->h_subject = readtty("Subject: ", hp->h_subject);
89 if (gflags & GCC) {
90 hp->h_cc =
91 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
93 if (gflags & GBCC) {
94 hp->h_bcc =
95 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
97 out:
98 signal(SIGTSTP, savetstp);
99 signal(SIGTTOU, savettou);
100 signal(SIGTTIN, savettin);
101 if (extproc) {
102 flag = 1;
103 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
104 warn("TIOCEXT: on");
106 signal(SIGINT, saveint);
107 return (errs);
111 * Read up a header from standard input.
112 * The source string has the preliminary contents to
113 * be read.
117 char *
118 readtty(const char *pr, char *src)
120 char ch, canonb[BUFSIZ];
121 int c;
122 char *cp, *cp2;
124 fputs(pr, stdout);
125 fflush(stdout);
126 if (src != NULL && strlen(src) > BUFSIZ - 2) {
127 printf("too long to edit\n");
128 return (src);
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)) {
134 ch = '\\';
135 ioctl(0, TIOCSTI, &ch);
137 ch = c;
138 ioctl(0, TIOCSTI, &ch);
140 cp = canonb;
141 *cp = '\0';
142 cp2 = cp;
143 while (cp2 < canonb + BUFSIZ)
144 *cp2++ = '\0';
145 cp2 = cp;
146 if (setjmp(rewrite))
147 goto redo;
148 signal(SIGTSTP, ttystop);
149 signal(SIGTTOU, ttystop);
150 signal(SIGTTIN, ttystop);
151 clearerr(stdin);
152 while (cp2 < canonb + BUFSIZ) {
153 c = getc(stdin);
154 if (c == EOF || c == '\n')
155 break;
156 *cp2++ = c;
158 *cp2 = '\0';
159 signal(SIGTSTP, SIG_DFL);
160 signal(SIGTTOU, SIG_DFL);
161 signal(SIGTTIN, SIG_DFL);
162 if (c == EOF && ferror(stdin)) {
163 redo:
164 cp = strlen(canonb) > 0 ? canonb : NULL;
165 clearerr(stdin);
166 return (readtty(pr, cp));
168 if (equal("", canonb))
169 return (NULL);
170 return (savestr(canonb));
174 * Receipt continuation.
176 void
177 ttystop(int s)
179 sig_t old_action = signal(s, SIG_DFL);
180 sigset_t nset;
182 sigemptyset(&nset);
183 sigaddset(&nset, s);
184 sigprocmask(SIG_BLOCK, &nset, NULL);
185 kill(0, s);
186 sigprocmask(SIG_UNBLOCK, &nset, NULL);
187 signal(s, old_action);
188 longjmp(rewrite, 1);
191 /*ARGSUSED*/
192 void
193 ttyint(int s)
195 longjmp(intjmp, 1);