1/*-
2 * Copyright (c) 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 *	Keith Bostic.  All rights reserved.
6 *
7 * See the LICENSE file for redistribution information.
8 */
9
10#include "config.h"
11
12#ifndef lint
13static const char sccsid[] = "@(#)cl_read.c	10.15 (Berkeley) 9/24/96";
14#endif /* not lint */
15
16#include <sys/types.h>
17#include <sys/queue.h>
18#ifdef HAVE_SYS_SELECT_H
19#include <sys/select.h>
20#endif
21#include <sys/time.h>
22
23#include <bitstring.h>
24#include <curses.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <signal.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <termios.h>
32#include <unistd.h>
33
34#include "../common/common.h"
35#include "../ex/script.h"
36#include "cl.h"
37
38static input_t	cl_read __P((SCR *,
39    u_int32_t, CHAR_T *, size_t, int *, struct timeval *));
40static int	cl_resize __P((SCR *, size_t, size_t));
41
42/*
43 * cl_event --
44 *	Return a single event.
45 *
46 * PUBLIC: int cl_event __P((SCR *, EVENT *, u_int32_t, int));
47 */
48int
49cl_event(sp, evp, flags, ms)
50	SCR *sp;
51	EVENT *evp;
52	u_int32_t flags;
53	int ms;
54{
55	struct timeval t, *tp;
56	CL_PRIVATE *clp;
57	size_t lines, columns;
58	int changed, nr;
59
60	/*
61	 * Queue signal based events.  We never clear SIGHUP or SIGTERM events,
62	 * so that we just keep returning them until the editor dies.
63	 */
64	clp = CLP(sp);
65retest:	if (LF_ISSET(EC_INTERRUPT) || F_ISSET(clp, CL_SIGINT)) {
66		if (F_ISSET(clp, CL_SIGINT)) {
67			F_CLR(clp, CL_SIGINT);
68			evp->e_event = E_INTERRUPT;
69		} else
70			evp->e_event = E_TIMEOUT;
71		return (0);
72	}
73	if (F_ISSET(clp, CL_SIGHUP | CL_SIGTERM | CL_SIGWINCH)) {
74		if (F_ISSET(clp, CL_SIGHUP)) {
75			evp->e_event = E_SIGHUP;
76			return (0);
77		}
78		if (F_ISSET(clp, CL_SIGTERM)) {
79			evp->e_event = E_SIGTERM;
80			return (0);
81		}
82		if (F_ISSET(clp, CL_SIGWINCH)) {
83			F_CLR(clp, CL_SIGWINCH);
84			if (cl_ssize(sp, 1, &lines, &columns, &changed))
85				return (1);
86			if (changed) {
87				(void)cl_resize(sp, lines, columns);
88				evp->e_event = E_WRESIZE;
89				return (0);
90			}
91			/* No real change, ignore the signal. */
92		}
93	}
94
95	/* Set timer. */
96	if (ms == 0)
97		tp = NULL;
98	else {
99		t.tv_sec = ms / 1000;
100		t.tv_usec = (ms % 1000) * 1000;
101		tp = &t;
102	}
103
104	/* Read input characters. */
105	switch (cl_read(sp, LF_ISSET(EC_QUOTED | EC_RAW),
106	    clp->ibuf, sizeof(clp->ibuf), &nr, tp)) {
107	case INP_OK:
108		evp->e_csp = clp->ibuf;
109		evp->e_len = nr;
110		evp->e_event = E_STRING;
111		break;
112	case INP_EOF:
113		evp->e_event = E_EOF;
114		break;
115	case INP_ERR:
116		evp->e_event = E_ERR;
117		break;
118	case INP_INTR:
119		goto retest;
120	case INP_TIMEOUT:
121		evp->e_event = E_TIMEOUT;
122		break;
123	default:
124		abort();
125	}
126	return (0);
127}
128
129/*
130 * cl_read --
131 *	Read characters from the input.
132 */
133static input_t
134cl_read(sp, flags, bp, blen, nrp, tp)
135	SCR *sp;
136	u_int32_t flags;
137	CHAR_T *bp;
138	size_t blen;
139	int *nrp;
140	struct timeval *tp;
141{
142	struct termios term1, term2;
143	struct timeval poll;
144	CL_PRIVATE *clp;
145	GS *gp;
146	SCR *tsp;
147	fd_set rdfd;
148	input_t rval;
149	int maxfd, nr, term_reset;
150
151	gp = sp->gp;
152	clp = CLP(sp);
153	term_reset = 0;
154
155	/*
156	 * 1: A read from a file or a pipe.  In this case, the reads
157	 *    never timeout regardless.  This means that we can hang
158	 *    when trying to complete a map, but we're going to hang
159	 *    on the next read anyway.
160	 */
161	if (!F_ISSET(clp, CL_STDIN_TTY)) {
162		switch (nr = read(STDIN_FILENO, bp, blen)) {
163		case 0:
164			return (INP_EOF);
165		case -1:
166			goto err;
167		default:
168			*nrp = nr;
169			return (INP_OK);
170		}
171		/* NOTREACHED */
172	}
173
174	/*
175	 * 2: A read with an associated timeout, e.g., trying to complete
176	 *    a map sequence.  If input exists, we fall into #3.
177	 */
178	FD_ZERO(&rdfd);
179	poll.tv_sec = 0;
180	poll.tv_usec = 0;
181	if (tp != NULL) {
182		FD_SET(STDIN_FILENO, &rdfd);
183		switch (select(STDIN_FILENO + 1,
184		    &rdfd, NULL, NULL, tp == NULL ? &poll : tp)) {
185		case 0:
186			return (INP_TIMEOUT);
187		case -1:
188			goto err;
189		default:
190			break;
191		}
192	}
193
194	/*
195	 * The user can enter a key in the editor to quote a character.  If we
196	 * get here and the next key is supposed to be quoted, do what we can.
197	 * Reset the tty so that the user can enter a ^C, ^Q, ^S.  There's an
198	 * obvious race here, when the key has already been entered, but there's
199	 * nothing that we can do to fix that problem.
200	 *
201	 * The editor can ask for the next literal character even thought it's
202	 * generally running in line-at-a-time mode.  Do what we can.
203	 */
204	if (LF_ISSET(EC_QUOTED | EC_RAW) && !tcgetattr(STDIN_FILENO, &term1)) {
205		term_reset = 1;
206		if (LF_ISSET(EC_QUOTED)) {
207			term2 = term1;
208			term2.c_lflag &= ~ISIG;
209			term2.c_iflag &= ~(IXON | IXOFF);
210			(void)tcsetattr(STDIN_FILENO,
211			    TCSASOFT | TCSADRAIN, &term2);
212		} else
213			(void)tcsetattr(STDIN_FILENO,
214			    TCSASOFT | TCSADRAIN, &clp->vi_enter);
215	}
216
217	/*
218	 * 3: Wait for input.
219	 *
220	 * Select on the command input and scripting window file descriptors.
221	 * It's ugly that we wait on scripting file descriptors here, but it's
222	 * the only way to keep from locking out scripting windows.
223	 */
224	if (F_ISSET(gp, G_SCRWIN)) {
225loop:		FD_ZERO(&rdfd);
226		FD_SET(STDIN_FILENO, &rdfd);
227		maxfd = STDIN_FILENO;
228		for (tsp = gp->dq.cqh_first;
229		    tsp != (void *)&gp->dq; tsp = tsp->q.cqe_next)
230			if (F_ISSET(sp, SC_SCRIPT)) {
231				FD_SET(sp->script->sh_master, &rdfd);
232				if (sp->script->sh_master > maxfd)
233					maxfd = sp->script->sh_master;
234			}
235		switch (select(maxfd + 1, &rdfd, NULL, NULL, NULL)) {
236		case 0:
237			abort();
238		case -1:
239			goto err;
240		default:
241			break;
242		}
243		if (!FD_ISSET(STDIN_FILENO, &rdfd)) {
244			if (sscr_input(sp))
245				return (INP_ERR);
246			goto loop;
247		}
248	}
249
250	/*
251	 * 4: Read the input.
252	 *
253	 * !!!
254	 * What's going on here is some scary stuff.  Ex runs the terminal in
255	 * canonical mode.  So, the <newline> character terminating a line of
256	 * input is returned in the buffer, but a trailing <EOF> character is
257	 * not similarly included.  As ex uses 0<EOF> and ^<EOF> as autoindent
258	 * commands, it has to see the trailing <EOF> characters to determine
259	 * the difference between the user entering "0ab" and "0<EOF>ab".  We
260	 * leave an extra slot in the buffer, so that we can add a trailing
261	 * <EOF> character if the buffer isn't terminated by a <newline>.  We
262	 * lose if the buffer is too small for the line and exactly N characters
263	 * are entered followed by an <EOF> character.
264	 */
265#define	ONE_FOR_EOF	1
266	switch (nr = read(STDIN_FILENO, bp, blen - ONE_FOR_EOF)) {
267	case  0:				/* EOF. */
268		/*
269		 * ^D in canonical mode returns a read of 0, i.e. EOF.  EOF is
270		 * a valid command, but we don't want to loop forever because
271		 * the terminal driver is returning EOF because the user has
272		 * disconnected. The editor will almost certainly try to write
273		 * something before this fires, which should kill us, but You
274		 * Never Know.
275		 */
276		if (++clp->eof_count < 50) {
277			bp[0] = clp->orig.c_cc[VEOF];
278			*nrp = 1;
279			rval = INP_OK;
280
281		} else
282			rval = INP_EOF;
283		break;
284	case -1:				/* Error or interrupt. */
285err:		if (errno == EINTR)
286			rval = INP_INTR;
287		else {
288			rval = INP_ERR;
289			msgq(sp, M_SYSERR, "input");
290		}
291		break;
292	default:				/* Input characters. */
293		if (F_ISSET(sp, SC_EX) && bp[nr - 1] != '\n')
294			bp[nr++] = clp->orig.c_cc[VEOF];
295		*nrp = nr;
296		clp->eof_count = 0;
297		rval = INP_OK;
298		break;
299	}
300
301	/* Restore the terminal state if it was modified. */
302	if (term_reset)
303		(void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &term1);
304	return (rval);
305}
306
307/*
308 * cl_resize --
309 *	Reset the options for a resize event.
310 */
311static int
312cl_resize(sp, lines, columns)
313	SCR *sp;
314	size_t lines, columns;
315{
316	ARGS *argv[2], a, b;
317	char b1[1024];
318
319	a.bp = b1;
320	b.bp = NULL;
321	a.len = b.len = 0;
322	argv[0] = &a;
323	argv[1] = &b;
324
325	(void)snprintf(b1, sizeof(b1), "lines=%lu", (u_long)lines);
326	a.len = strlen(b1);
327	if (opts_set(sp, argv, NULL))
328		return (1);
329	(void)snprintf(b1, sizeof(b1), "columns=%lu", (u_long)columns);
330	a.len = strlen(b1);
331	if (opts_set(sp, argv, NULL))
332		return (1);
333	return (0);
334}
335