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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/* $OpenBSD: tty.c,v 1.24 2023/11/03 19:32:28 anton 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. 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.
*/
/*
* Mail -- a mail program
*
* Generally useful tty stuff.
*/
#include "rcv.h"
#include "extern.h"
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#define TABWIDTH 8
struct tty {
int fdin;
int fdout;
int flags;
#define TTY_ALTWERASE 0x1
#define TTY_ERR 0x2
cc_t *keys;
char *buf;
size_t size;
size_t len;
size_t cursor;
};
static void tty_flush(struct tty *);
static int tty_getc(struct tty *);
static int tty_insert(struct tty *, int, int);
static void tty_putc(struct tty *, int);
static void tty_reset(struct tty *);
static void tty_visc(struct tty *, int);
static struct tty tty;
static volatile sig_atomic_t ttysignal; /* Interrupted by a signal? */
/*
* Read all relevant header fields.
*/
int
grabh(struct header *hp, int gflags)
{
struct termios newtio, oldtio;
#ifdef TIOCEXT
int extproc;
int flag;
#endif
struct sigaction savetstp;
struct sigaction savettou;
struct sigaction savettin;
struct sigaction act;
char *s;
int error;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
act.sa_handler = SIG_DFL;
(void)sigaction(SIGTSTP, &act, &savetstp);
(void)sigaction(SIGTTOU, &act, &savettou);
(void)sigaction(SIGTTIN, &act, &savettin);
error = 1;
memset(&tty, 0, sizeof(tty));
tty.fdin = fileno(stdin);
tty.fdout = fileno(stdout);
if (tcgetattr(tty.fdin, &oldtio) == -1) {
warn("tcgetattr");
return(-1);
}
tty.keys = oldtio.c_cc;
if (oldtio.c_lflag & ALTWERASE)
tty.flags |= TTY_ALTWERASE;
newtio = oldtio;
newtio.c_lflag &= ~(ECHO | ICANON);
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
if (tcsetattr(tty.fdin, TCSADRAIN, &newtio) == -1) {
warn("tcsetattr");
return(-1);
}
#ifdef TIOCEXT
extproc = ((oldtio.c_lflag & EXTPROC) ? 1 : 0);
if (extproc) {
flag = 0;
if (ioctl(fileno(stdin), TIOCEXT, &flag) == -1)
warn("TIOCEXT: off");
}
#endif
if (gflags & GTO) {
s = readtty("To: ", detract(hp->h_to, 0));
if (s == NULL)
goto out;
hp->h_to = extract(s, GTO);
}
if (gflags & GSUBJECT) {
s = readtty("Subject: ", hp->h_subject);
if (s == NULL)
goto out;
hp->h_subject = s;
}
if (gflags & GCC) {
s = readtty("Cc: ", detract(hp->h_cc, 0));
if (s == NULL)
goto out;
hp->h_cc = extract(s, GCC);
}
if (gflags & GBCC) {
s = readtty("Bcc: ", detract(hp->h_bcc, 0));
if (s == NULL)
goto out;
hp->h_bcc = extract(s, GBCC);
}
error = 0;
out:
(void)sigaction(SIGTSTP, &savetstp, NULL);
(void)sigaction(SIGTTOU, &savettou, NULL);
(void)sigaction(SIGTTIN, &savettin, NULL);
#ifdef TIOCEXT
if (extproc) {
flag = 1;
if (ioctl(fileno(stdin), TIOCEXT, &flag) == -1)
warn("TIOCEXT: on");
}
#endif
if (tcsetattr(tty.fdin, TCSADRAIN, &oldtio) == -1)
warn("tcsetattr");
return(error);
}
/*
* Read up a header from standard input.
* The source string has the preliminary contents to
* be read.
*
*/
char *
readtty(char *pr, char *src)
{
struct sigaction act, saveint;
unsigned char canonb[BUFSIZ];
char *cp;
sigset_t oset;
int c, done;
memset(canonb, 0, sizeof(canonb));
tty.buf = canonb;
tty.size = sizeof(canonb) - 1;
for (cp = pr; *cp != '\0'; cp++)
tty_insert(&tty, *cp, 1);
tty_flush(&tty);
tty_reset(&tty);
if (src != NULL && strlen(src) > sizeof(canonb) - 2) {
puts("too long to edit");
return(src);
}
if (src != NULL) {
for (cp = src; *cp != '\0'; cp++)
tty_insert(&tty, *cp, 1);
tty_flush(&tty);
}
sigemptyset(&act.sa_mask);
act.sa_flags = 0; /* Note: will not restart syscalls */
act.sa_handler = ttyint;
(void)sigaction(SIGINT, &act, &saveint);
act.sa_handler = ttystop;
(void)sigaction(SIGTSTP, &act, NULL);
(void)sigaction(SIGTTOU, &act, NULL);
(void)sigaction(SIGTTIN, &act, NULL);
(void)sigprocmask(SIG_UNBLOCK, &intset, &oset);
for (;;) {
c = tty_getc(&tty);
switch (ttysignal) {
case SIGINT:
tty_visc(&tty, '\003'); /* output ^C */
/* FALLTHROUGH */
case 0:
break;
default:
ttysignal = 0;
goto redo;
}
if (c == 0) {
done = 1;
} else if (c == '\n') {
tty_putc(&tty, c);
done = 1;
} else {
done = tty_insert(&tty, c, 0);
tty_flush(&tty);
}
if (done)
break;
}
act.sa_handler = SIG_DFL;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
(void)sigprocmask(SIG_SETMASK, &oset, NULL);
(void)sigaction(SIGTSTP, &act, NULL);
(void)sigaction(SIGTTOU, &act, NULL);
(void)sigaction(SIGTTIN, &act, NULL);
(void)sigaction(SIGINT, &saveint, NULL);
if (tty.flags & TTY_ERR) {
if (ttysignal == SIGINT) {
ttysignal = 0;
return(NULL); /* user hit ^C */
}
redo:
cp = strlen(canonb) > 0 ? canonb : NULL;
/* XXX - make iterative, not recursive */
return(readtty(pr, cp));
}
if (equal("", canonb))
return("");
return(savestr(canonb));
}
/*
* Receipt continuation.
*/
void
ttystop(int s)
{
struct sigaction act, oact;
sigset_t nset;
int save_errno;
/*
* Save old handler and set to default.
* Unblock receipt of 's' and then resend it.
*/
save_errno = errno;
(void)sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
act.sa_handler = SIG_DFL;
(void)sigaction(s, &act, &oact);
(void)sigemptyset(&nset);
(void)sigaddset(&nset, s);
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
(void)kill(0, s);
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
(void)sigaction(s, &oact, NULL);
ttysignal = s;
errno = save_errno;
}
void
ttyint(int s)
{
ttysignal = s;
}
static void
tty_flush(struct tty *t)
{
size_t i, len;
int c;
if (t->cursor < t->len) {
for (; t->cursor < t->len; t->cursor++)
tty_visc(t, t->buf[t->cursor]);
} else if (t->cursor > t->len) {
len = t->cursor - t->len;
for (i = len; i > 0; i--) {
c = t->buf[--t->cursor];
if (c == '\t')
len += TABWIDTH - 1;
else if (iscntrl(c))
len++; /* account for leading ^ */
}
for (i = 0; i < len; i++)
tty_putc(t, '\b');
for (i = 0; i < len; i++)
tty_putc(t, ' ');
for (i = 0; i < len; i++)
tty_putc(t, '\b');
t->cursor = t->len;
}
t->buf[t->len] = '\0';
}
static int
tty_getc(struct tty *t)
{
ssize_t n;
unsigned char c;
if (ttysignal != 0)
n = -1;
else
n = read(t->fdin, &c, 1);
switch (n) {
case -1:
t->flags |= TTY_ERR;
/* FALLTHROUGH */
case 0:
return 0;
default:
return c & 0x7f;
}
}
static int
tty_insert(struct tty *t, int c, int nocntrl)
{
const unsigned char *ws = " \t";
if (CCEQ(t->keys[VERASE], c)) {
if (nocntrl)
return 0;
if (t->len > 0)
t->len--;
} else if (CCEQ(t->keys[VWERASE], c)) {
if (nocntrl)
return 0;
for (; t->len > 0; t->len--)
if (strchr(ws, t->buf[t->len - 1]) == NULL
&& ((t->flags & TTY_ALTWERASE) == 0
|| isalpha(t->buf[t->len - 1])))
break;
for (; t->len > 0; t->len--)
if (strchr(ws, t->buf[t->len - 1]) != NULL
|| ((t->flags & TTY_ALTWERASE)
&& !isalpha(t->buf[t->len - 1])))
break;
} else if (CCEQ(t->keys[VKILL], c)) {
if (nocntrl)
return 0;
t->len = 0;
} else {
if (t->len == t->size)
return 1;
t->buf[t->len++] = c;
}
return 0;
}
static void
tty_putc(struct tty *t, int c)
{
unsigned char cc = c;
write(t->fdout, &cc, 1);
}
static void
tty_reset(struct tty *t)
{
memset(t->buf, 0, t->len);
t->len = t->cursor = 0;
}
static void
tty_visc(struct tty *t, int c)
{
int i;
if (c == '\t') {
for (i = 0; i < TABWIDTH; i++)
tty_putc(t, ' ');
} else if (iscntrl(c)) {
tty_putc(t, '^');
if (c == 0x7F)
tty_putc(t, '?');
else
tty_putc(t, (c | 0x40));
} else {
tty_putc(t, c);
}
}
|