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