File: tty.c

package info (click to toggle)
heirloom-mailx 12.4-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,524 kB
  • ctags: 1,960
  • sloc: ansic: 29,443; sh: 333; makefile: 126
file content (436 lines) | stat: -rw-r--r-- 10,852 bytes parent folder | download | duplicates (5)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
 *
 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
 */
/*
 * 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
#ifdef	DOSCCS
static char sccsid[] = "@(#)tty.c	2.29 (gritter) 3/9/07";
#endif
#endif /* not lint */

/*
 * Mail -- a mail program
 *
 * Generally useful tty stuff.
 */

#include "rcv.h"
#include "extern.h"
#include <errno.h>
#include <termios.h>
#include <unistd.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
static	struct termios	ttybuf;
static	long		vdis;		/* _POSIX_VDISABLE char */

static void ttystop(int s);
static void ttyint(int s);
static int safe_getc(FILE *ibuf);
static char *rtty_internal(const char *pr, char *src);

/*
 * Receipt continuation.
 */
static void 
ttystop(int s)
{
	sighandler_type old_action = safe_signal(s, SIG_DFL);
	sigset_t nset;

	sigemptyset(&nset);
	sigaddset(&nset, s);
	sigprocmask(SIG_BLOCK, &nset, NULL);
	kill(0, s);
	sigprocmask(SIG_UNBLOCK, &nset, NULL);
	safe_signal(s, old_action);
	siglongjmp(rewrite, 1);
}

/*ARGSUSED*/
static void 
ttyint(int s)
{
	siglongjmp(intjmp, 1);
}

/*
 * Interrupts will cause trouble if we are inside a stdio call. As
 * this is only relevant if input comes from a terminal, we can simply
 * bypass it by read() then.
 */
static int
safe_getc(FILE *ibuf)
{
	if (fileno(ibuf) == 0 && is_a_tty[0]) {
		char c;
		int sz;

again:
		if ((sz = read(0, &c, 1)) != 1) {
			if (sz < 0 && errno == EINTR)
				goto again;
			return EOF;
		}
		return c & 0377;
	} else
		return getc(ibuf);
}

/*
 * Read up a header from standard input.
 * The source string has the preliminary contents to
 * be read.
 */
static char *
rtty_internal(const char *pr, char *src)
{
	char ch, canonb[LINESIZE];
	int c;
	char *cp, *cp2;

	(void) &c;
	(void) &cp2;
	fputs(pr, stdout);
	fflush(stdout);
	if (src != NULL && strlen(src) > sizeof canonb - 2) {
		printf(catgets(catd, CATSET, 200, "too long to edit\n"));
		return(src);
	}
#ifndef TIOCSTI
	if (src != NULL)
		cp = sstpcpy(canonb, src);
	else
		cp = sstpcpy(canonb, "");
	fputs(canonb, stdout);
	fflush(stdout);
#else
	cp = src == NULL ? "" : src;
	while ((c = *cp++) != '\0') {
		if ((c_erase != vdis && c == c_erase) ||
		    (c_kill != vdis && c == c_kill)) {
			ch = '\\';
			ioctl(0, TIOCSTI, &ch);
		}
		ch = c;
		ioctl(0, TIOCSTI, &ch);
	}
	cp = canonb;
	*cp = 0;
#endif
	cp2 = cp;
	while (cp2 < canonb + sizeof canonb)
		*cp2++ = 0;
	cp2 = cp;
	if (sigsetjmp(rewrite, 1))
		goto redo;
	safe_signal(SIGTSTP, ttystop);
	safe_signal(SIGTTOU, ttystop);
	safe_signal(SIGTTIN, ttystop);
	clearerr(stdin);
	while (cp2 < canonb + sizeof canonb - 1) {
		c = safe_getc(stdin);
		if (c == EOF || c == '\n')
			break;
		*cp2++ = c;
	}
	*cp2 = 0;
	safe_signal(SIGTSTP, SIG_DFL);
	safe_signal(SIGTTOU, SIG_DFL);
	safe_signal(SIGTTIN, SIG_DFL);
	if (c == EOF && ferror(stdin)) {
redo:
		cp = strlen(canonb) > 0 ? canonb : NULL;
		clearerr(stdin);
		return(rtty_internal(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 != vdis && c == c_erase) {
			if (cp2 == canonb)
				continue;
			if (cp2[-1] == '\\') {
				cp2[-1] = c;
				continue;
			}
			cp2--;
			continue;
		}
		if (c_kill != vdis && 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));
}

/*
 * Read all relevant header fields.
 */

#ifndef	TIOCSTI
#define	TTYSET_CHECK(h)	if (!ttyset && (h) != NULL) \
					ttyset++, tcsetattr(0, TCSADRAIN, \
					&ttybuf);
#else
#define	TTYSET_CHECK(h)
#endif

#define	GRAB_SUBJECT	if (gflags & GSUBJECT) { \
				TTYSET_CHECK(hp->h_subject) \
				hp->h_subject = rtty_internal("Subject: ", \
						hp->h_subject); \
			}

static struct name *
grabaddrs(const char *field, struct name *np, int comma, enum gfield gflags)
{
	struct name	*nq;

	TTYSET_CHECK(np);
	loop:
		np = sextract(rtty_internal(field, detract(np, comma)), gflags);
		for (nq = np; nq != NULL; nq = nq->n_flink)
			if (mime_name_invalid(nq->n_name, 1))
				goto loop;
	return np;
}

int 
grabh(struct header *hp, enum gfield gflags, int subjfirst)
{
	sighandler_type saveint;
#ifndef TIOCSTI
	sighandler_type savequit;
#endif
	sighandler_type savetstp;
	sighandler_type savettou;
	sighandler_type savettin;
	int errs;
	int comma;

	(void) &comma;
	(void) &saveint;
	savetstp = safe_signal(SIGTSTP, SIG_DFL);
	savettou = safe_signal(SIGTTOU, SIG_DFL);
	savettin = safe_signal(SIGTTIN, SIG_DFL);
	errs = 0;
	comma = value("bsdcompat") || value("bsdmsgs") ? 0 : GCOMMA;
#ifndef TIOCSTI
	ttyset = 0;
#endif
	if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
		perror("tcgetattr");
		return(-1);
	}
	c_erase = ttybuf.c_cc[VERASE];
	c_kill = ttybuf.c_cc[VKILL];
#if defined (_PC_VDISABLE) && defined (HAVE_FPATHCONF)
	if ((vdis = fpathconf(0, _PC_VDISABLE)) < 0)
		vdis = '\377';
#elif defined (_POSIX_VDISABLE)
	vdis = _POSIX_VDISABLE;
#else
	vdis = '\377';
#endif
#ifndef TIOCSTI
	ttybuf.c_cc[VERASE] = 0;
	ttybuf.c_cc[VKILL] = 0;
	if ((saveint = safe_signal(SIGINT, SIG_IGN)) == SIG_DFL)
		safe_signal(SIGINT, SIG_DFL);
	if ((savequit = safe_signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
		safe_signal(SIGQUIT, SIG_DFL);
#else	/* TIOCSTI */
	saveint = safe_signal(SIGINT, SIG_IGN);
	if (sigsetjmp(intjmp, 1)) {
		/* avoid garbled output with C-c */
		printf("\n");
		fflush(stdout);
		goto out;
	}
	if (saveint != SIG_IGN)
		safe_signal(SIGINT, ttyint);
#endif	/* TIOCSTI */
	if (gflags & GTO)
		hp->h_to = grabaddrs("To: ", hp->h_to, comma, GTO|GFULL);
	if (subjfirst)
		GRAB_SUBJECT
	if (gflags & GCC)
		hp->h_cc = grabaddrs("Cc: ", hp->h_cc, comma, GCC|GFULL);
	if (gflags & GBCC)
		hp->h_bcc = grabaddrs("Bcc: ", hp->h_bcc, comma, GBCC|GFULL);
	if (gflags & GEXTRA) {
		if (hp->h_from == NULL)
			hp->h_from = sextract(myaddrs(hp), GEXTRA|GFULL);
		hp->h_from = grabaddrs("From: ", hp->h_from, comma,
				GEXTRA|GFULL);
		if (hp->h_replyto == NULL)
			hp->h_replyto = sextract(value("replyto"),
					GEXTRA|GFULL);
		hp->h_replyto = grabaddrs("Reply-To: ", hp->h_replyto, comma,
				GEXTRA|GFULL);
		if (hp->h_sender == NULL)
			hp->h_sender = sextract(value("sender"),
					GEXTRA|GFULL);
		hp->h_sender = grabaddrs("Sender: ", hp->h_sender, comma,
				GEXTRA|GFULL);
		if (hp->h_organization == NULL)
			hp->h_organization = value("ORGANIZATION");
		TTYSET_CHECK(hp->h_organization);
		hp->h_organization = rtty_internal("Organization: ",
				hp->h_organization);
	}
	if (!subjfirst)
		GRAB_SUBJECT
out:
	safe_signal(SIGTSTP, savetstp);
	safe_signal(SIGTTOU, savettou);
	safe_signal(SIGTTIN, savettin);
#ifndef TIOCSTI
	ttybuf.c_cc[VERASE] = c_erase;
	ttybuf.c_cc[VKILL] = c_kill;
	if (ttyset)
		tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
	safe_signal(SIGQUIT, savequit);
#endif
	safe_signal(SIGINT, saveint);
	return(errs);
}

/*
 * Read a line from tty; to be called from elsewhere
 */

char *
readtty(char *prefix, char *string)
{
	char *ret = NULL;
	struct termios ttybuf;
	sighandler_type saveint = SIG_DFL;
#ifndef TIOCSTI
	sighandler_type savequit;
#endif
	sighandler_type savetstp;
	sighandler_type savettou;
	sighandler_type savettin;

	(void) &saveint;
	(void) &ret;
	savetstp = safe_signal(SIGTSTP, SIG_DFL);
	savettou = safe_signal(SIGTTOU, SIG_DFL);
	savettin = safe_signal(SIGTTIN, SIG_DFL);
#ifndef TIOCSTI
	ttyset = 0;
#endif
	if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
		perror("tcgetattr");
		return NULL;
	}
	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 = safe_signal(SIGINT, SIG_IGN)) == SIG_DFL)
		safe_signal(SIGINT, SIG_DFL);
	if ((savequit = safe_signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
		safe_signal(SIGQUIT, SIG_DFL);
#else
	if (sigsetjmp(intjmp, 1)) {
		/* avoid garbled output with C-c */
		printf("\n");
		fflush(stdout);
		goto out2;
	}
	saveint = safe_signal(SIGINT, ttyint);
#endif
	TTYSET_CHECK(string)
	ret = rtty_internal(prefix, string);
	if (ret != NULL && *ret == '\0')
		ret = NULL;
out2:
	safe_signal(SIGTSTP, savetstp);
	safe_signal(SIGTTOU, savettou);
	safe_signal(SIGTTIN, savettin);
#ifndef TIOCSTI
	ttybuf.c_cc[VERASE] = c_erase;
	ttybuf.c_cc[VKILL] = c_kill;
	if (ttyset)
		tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
	safe_signal(SIGQUIT, savequit);
#endif
	safe_signal(SIGINT, saveint);
	return ret;
}

int 
yorn(char *msg)
{
	char	*cp;

	if (value("interactive") == NULL)
		return 1;
	do
		cp = readtty(msg, NULL);
	while (cp == NULL ||
		*cp != 'y' && *cp != 'Y' && *cp != 'n' && *cp != 'N');
	return *cp == 'y' || *cp == 'Y';
}