summaryrefslogtreecommitdiff
path: root/usr.bin/mail/tty.c
blob: 5eb95e0d76948080312912520b297dd16e969665 (plain)
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
/*	$NetBSD: tty.c,v 1.29 2011/09/16 15:39:27 joerg 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.
 */

#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)tty.c	8.2 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: tty.c,v 1.29 2011/09/16 15:39:27 joerg Exp $");
#endif
#endif /* not lint */

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

#include "rcv.h"
#include "extern.h"
#ifdef USE_EDITLINE
# include "complete.h"
#endif
#include "sig.h"

static	jmp_buf	tty_jmpbuf;		/* Place to go when interrupted */

#ifndef USE_EDITLINE
static	cc_t	c_erase;		/* Current erase char */
static	cc_t	c_kill;			/* Current kill char */
# ifndef TIOCSTI
static	int	ttyset;			/* We must now do erase/kill */
# endif
#endif /* USE_EDITLINE */

/*
 * Read up a header from standard input.
 * The source string has the preliminary contents to
 * be read.
 *
 * Returns: an salloc'ed copy of the line read if successful, or NULL
 * if no characters were read or if an error occurred.
 *
 */
#ifdef USE_EDITLINE
static char *
readtty(const char *pr, char *src)
{
	char *line;

	line = my_gets(&elm.string, pr, src);
	sig_check();
	if (line == NULL)
		(void)putc('\n', stdout);

	return line ? savestr(line) : __UNCONST("");
}

#else /* not USE_EDITLINE */

static char *
readtty(const char *pr, char *src)
{
	char canonb[LINESIZE];
	char *cp, *cp2;
	int c;
#ifdef TIOCSTI
	char ch;
#endif

	(void)fputs(pr, stdout);
	(void)fflush(stdout);
	if (src != NULL && strlen(src) > sizeof(canonb) - 2) {
		(void)printf("too long to edit\n");
		return src;
	}
#ifndef TIOCSTI
	if (src != NULL)
		cp = copy(src, canonb);
	else
		cp = copy(__UNCONST(""), canonb);
	c = *cp;
	(void)fputs(canonb, stdout);
	(void)fflush(stdout);
#else
	cp = src == NULL ? __UNCONST("") : src;
	while ((c = *cp++) != '\0') {
		if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
		    (c_kill != _POSIX_VDISABLE && c == c_kill)) {
			ch = '\\';
			(void)ioctl(0, TIOCSTI, &ch);
		}
		ch = c;
		(void)ioctl(0, TIOCSTI, &ch);
	}
	cp = canonb;
	*cp = '\0';
#endif
	clearerr(stdin);
	cp2 = cp;
	while (cp2 < canonb + sizeof(canonb) - 1) {
		c = getc(stdin);
		sig_check();
		if (c == EOF) {
			if (feof(stdin))
				(void)putc('\n', stdout);
			break;
		}
		if (c == '\n')
			break;
		*cp2++ = c;
	}
	*cp2 = '\0';

	if (c == EOF && ferror(stdin)) {
		cp = strlen(canonb) > 0 ? canonb : NULL;
		clearerr(stdin);
		return readtty(pr, cp);
	}
#ifndef TIOCSTI
	if (cp == NULL || *cp == '\0')
		return src;
	if (ttyset == 0)
		return strlen(canonb) > 0 ? savestr(canonb) : NULL;

	/*
	 * Do erase and kill.
	 */
	cp2 = cp;
	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 (canonb[0] == '\0')
		return __UNCONST("");
	return savestr(canonb);
}
#endif /* USE_EDITLINE */

#ifdef USE_EDITLINE
# define save_erase_and_kill(t)		0
#else
static int
save_erase_and_kill(struct termios *t)
{

# ifndef TIOCSTI
	ttyset = 0;
#endif
	if (tcgetattr(fileno(stdin), t) == -1) {
		warn("tcgetattr");
		return -1;
	}
	c_erase = t->c_cc[VERASE];
	c_kill = t->c_cc[VKILL];
	return 0;
}
#endif

#if defined(USE_EDITLINE) || defined(TIOCSTI)
# define disable_erase_and_kill(t)
#else
static void
disable_erase_and_kill(struct termios *t)
{

	if (ttyset == 0) {
		ttyset = 1;
		t->c_cc[VERASE] = _POSIX_VDISABLE;
		t->c_cc[VKILL] = _POSIX_VDISABLE;
		(void)tcsetattr(fileno(stdin), TCSADRAIN, t);
	}
}
#endif

#if defined(USE_EDITLINE) || defined(TIOCSTI)
# define restore_erase_and_kill(t)
#else
static void
restore_erase_and_kill(struct termios *t)
{

	if (ttyset != 0) {
		ttyset = 0;
		t->c_cc[VERASE] = c_erase;
		t->c_cc[VKILL] = c_kill;
		(void)tcsetattr(fileno(stdin), TCSADRAIN, t);
	}
}
#endif

/*
 * Do a shell-like extraction of a line
 * and make a list of name from it.
 * Return the list or NULL if none found.
 */
static struct name *
shextract(char *line, int ntype)
{
	struct name *begin, *np, *t;
	char *argv[MAXARGC];
	size_t argc, i;

	begin = NULL;
	if (line) {
		np = NULL;
		argc = getrawlist(line, argv, (int)__arraycount(argv));
		for (i = 0; i < argc; i++) {
			t = nalloc(argv[i], ntype);
			if (begin == NULL)
				begin = t;
			else
				np->n_flink = t;
			t->n_blink = np;
			np = t;
		}
	}
	return begin;
}

/*ARGSUSED*/
__dead static void
tty_sigint(int signo __unused)
{

	longjmp(tty_jmpbuf, 1);
}

/*
 * Read all relevant header fields.
 * Returns 0 on success; 1 if there was an error or signal.
 */
PUBLIC int
grabh(struct header *hp, int gflags)
{
	sig_t volatile old_sigint;
	int retval;
#ifndef USE_EDITLINE
	struct termios ttybuf;
# if defined(TIOCSTI) && defined(TIOCEXT)
	int extproc;
# endif

	if (save_erase_and_kill(&ttybuf))
		return -1;

# if defined(TIOCSTI) && defined(TIOCEXT)
	extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
	if (extproc) {
		int flag;

		flag = 0;
		if (ioctl(fileno(stdin), TIOCEXT, &flag) == -1)
			warn("TIOCEXT: off");
	}
# endif
#endif /* USE_EDITLINE */

	sig_check();
	old_sigint = sig_signal(SIGINT, tty_sigint);

	/* return here if we detect a SIGINT */
	if ((retval = setjmp(tty_jmpbuf)) != 0) {
		(void)putc('\n', stdout);
		goto out;
	}

	/*
	 * Do this irrespective of whether the initial string is empty.
	 * Otherwise, the editing is inconsistent.
	 */
	disable_erase_and_kill(&ttybuf);

	if (gflags & GTO) {
		hp->h_to =
		    extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
	}
	if (gflags & GSUBJECT) {
		hp->h_subject = readtty("Subject: ", hp->h_subject);
	}
	if (gflags & GCC) {
		hp->h_cc =
		    extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
	}
	if (gflags & GBCC) {
		hp->h_bcc =
		    extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
	}
	if (gflags & GSMOPTS) {
		hp->h_smopts =
		    shextract(readtty("Smopts: ", detract(hp->h_smopts, 0)),
			GSMOPTS);
	}
#ifdef MIME_SUPPORT
	if (gflags & GSMOPTS) {	/* XXX - Use a new flag for this? */
		if (hp->h_attach) {
			struct attachment *ap;
			int i;

			i = 0;
			for (ap = hp->h_attach; ap; ap = ap->a_flink)
				i++;
			(void)printf("Attachment%s: %d\n", i > 1 ? "s" : "", i);
		}
	}
#endif
 out:
	restore_erase_and_kill(&ttybuf);

#ifndef USE_EDITLINE
# if defined(TIOCSTI) && defined(TIOCEXT)
	if (extproc) {
		int flag;
		flag = 1;
		if (ioctl(fileno(stdin), TIOCEXT, &flag) == -1)
			warn("TIOCEXT: on");
	}
# endif
#endif
	(void)sig_signal(SIGINT, old_sigint);
	sig_check();
	return retval;
}