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
13 static 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 #if 0
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 
38 static input_t	cl_read __P((SCR *,
39     u_int32_t, CHAR_T *, size_t, int *, struct timeval *));
40 static 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  */
48 int
cl_event(sp,evp,flags,ms)49 cl_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);
65 retest:	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 /* Call this before any read from a file */
130 static void
block_for_read(fd)131 block_for_read(fd)
132 	int fd;
133 {
134 	fd_set fds;
135 	FD_ZERO(&fds);
136 	FD_SET(fd, &fds);
137 	mega_select(fd+1, &fds, NULL, NULL, NULL);
138 }
139 
140 /*
141  * cl_read --
142  *	Read characters from the input.
143  */
144 static input_t
cl_read(sp,flags,bp,blen,nrp,tp)145 cl_read(sp, flags, bp, blen, nrp, tp)
146 	SCR *sp;
147 	u_int32_t flags;
148 	CHAR_T *bp;
149 	size_t blen;
150 	int *nrp;
151 	struct timeval *tp;
152 {
153 	struct termios term1, term2;
154 	struct timeval poll;
155 	CL_PRIVATE *clp;
156 	GS *gp;
157 	SCR *tsp;
158 	fd_set rdfd;
159 	input_t rval;
160 	int maxfd, nr, term_reset;
161 
162 	gp = sp->gp;
163 	clp = CLP(sp);
164 	term_reset = 0;
165 
166 	/*
167 	 * 1: A read from a file or a pipe.  In this case, the reads
168 	 *    never timeout regardless.  This means that we can hang
169 	 *    when trying to complete a map, but we're going to hang
170 	 *    on the next read anyway.
171 	 */
172 	if (!F_ISSET(clp, CL_STDIN_TTY)) {
173 		block_for_read(STDIN_FILENO);
174 		switch (nr = read(STDIN_FILENO, bp, blen)) {
175 		case 0:
176 			return (INP_EOF);
177 		case -1:
178 			goto err;
179 		default:
180 			*nrp = nr;
181 			return (INP_OK);
182 		}
183 		/* NOTREACHED */
184 	}
185 
186 	/*
187 	 * 2: A read with an associated timeout, e.g., trying to complete
188 	 *    a map sequence.  If input exists, we fall into #3.
189 	 */
190 	FD_ZERO(&rdfd);
191 	poll.tv_sec = 0;
192 	poll.tv_usec = 0;
193 	if (tp != NULL) {
194 		FD_SET(STDIN_FILENO, &rdfd);
195 		switch (mega_select(STDIN_FILENO + 1,
196 		    &rdfd, NULL, NULL, tp == NULL ? &poll : tp)) {
197 		case 0:
198 			return (INP_TIMEOUT);
199 		case -1:
200 			goto err;
201 		default:
202 			break;
203 		}
204 	}
205 
206 	/*
207 	 * The user can enter a key in the editor to quote a character.  If we
208 	 * get here and the next key is supposed to be quoted, do what we can.
209 	 * Reset the tty so that the user can enter a ^C, ^Q, ^S.  There's an
210 	 * obvious race here, when the key has already been entered, but there's
211 	 * nothing that we can do to fix that problem.
212 	 *
213 	 * The editor can ask for the next literal character even thought it's
214 	 * generally running in line-at-a-time mode.  Do what we can.
215 	 */
216 	if (LF_ISSET(EC_QUOTED | EC_RAW) && !tcgetattr(STDIN_FILENO, &term1)) {
217 		term_reset = 1;
218 		if (LF_ISSET(EC_QUOTED)) {
219 			term2 = term1;
220 			term2.c_lflag &= ~ISIG;
221 			term2.c_iflag &= ~(IXON | IXOFF);
222 			(void)tcsetattr(STDIN_FILENO,
223 			    TCSASOFT | TCSADRAIN, &term2);
224 		} else
225 			(void)tcsetattr(STDIN_FILENO,
226 			    TCSASOFT | TCSADRAIN, &clp->vi_enter);
227 	}
228 
229 	/*
230 	 * 3: Wait for input.
231 	 *
232 	 * Select on the command input and scripting window file descriptors.
233 	 * It's ugly that we wait on scripting file descriptors here, but it's
234 	 * the only way to keep from locking out scripting windows.
235 	 */
236 	if (F_ISSET(gp, G_SCRWIN)) {
237 loop:		FD_ZERO(&rdfd);
238 		FD_SET(STDIN_FILENO, &rdfd);
239 		maxfd = STDIN_FILENO;
240 		for (tsp = gp->dq.cqh_first;
241 		    tsp != (void *)&gp->dq; tsp = tsp->q.cqe_next)
242 			if (F_ISSET(sp, SC_SCRIPT)) {
243 				FD_SET(sp->script->sh_master, &rdfd);
244 				if (sp->script->sh_master > maxfd)
245 					maxfd = sp->script->sh_master;
246 			}
247 		switch (mega_select(maxfd + 1, &rdfd, NULL, NULL, NULL)) {
248 		case 0:
249 			abort();
250 		case -1:
251 			goto err;
252 		default:
253 			break;
254 		}
255 		if (!FD_ISSET(STDIN_FILENO, &rdfd)) {
256 			if (sscr_input(sp))
257 				return (INP_ERR);
258 			goto loop;
259 		}
260 	}
261 
262 	/*
263 	 * 4: Read the input.
264 	 *
265 	 * !!!
266 	 * What's going on here is some scary stuff.  Ex runs the terminal in
267 	 * canonical mode.  So, the <newline> character terminating a line of
268 	 * input is returned in the buffer, but a trailing <EOF> character is
269 	 * not similarly included.  As ex uses 0<EOF> and ^<EOF> as autoindent
270 	 * commands, it has to see the trailing <EOF> characters to determine
271 	 * the difference between the user entering "0ab" and "0<EOF>ab".  We
272 	 * leave an extra slot in the buffer, so that we can add a trailing
273 	 * <EOF> character if the buffer isn't terminated by a <newline>.  We
274 	 * lose if the buffer is too small for the line and exactly N characters
275 	 * are entered followed by an <EOF> character.
276 	 */
277 #define	ONE_FOR_EOF	1
278 	block_for_read(STDIN_FILENO);
279 	switch (nr = read(STDIN_FILENO, bp, blen - ONE_FOR_EOF)) {
280 	case  0:				/* EOF. */
281 		/*
282 		 * ^D in canonical mode returns a read of 0, i.e. EOF.  EOF is
283 		 * a valid command, but we don't want to loop forever because
284 		 * the terminal driver is returning EOF because the user has
285 		 * disconnected. The editor will almost certainly try to write
286 		 * something before this fires, which should kill us, but You
287 		 * Never Know.
288 		 */
289 		if (++clp->eof_count < 50) {
290 			bp[0] = clp->orig.c_cc[VEOF];
291 			*nrp = 1;
292 			rval = INP_OK;
293 
294 		} else
295 			rval = INP_EOF;
296 		break;
297 	case -1:				/* Error or interrupt. */
298 err:		if (errno == EINTR)
299 			rval = INP_INTR;
300 		else {
301 			rval = INP_ERR;
302 			msgq(sp, M_SYSERR, "input");
303 		}
304 		break;
305 	default:				/* Input characters. */
306 		if (F_ISSET(sp, SC_EX) && bp[nr - 1] != '\n')
307 			bp[nr++] = clp->orig.c_cc[VEOF];
308 		*nrp = nr;
309 		clp->eof_count = 0;
310 		rval = INP_OK;
311 		break;
312 	}
313 
314 	/* Restore the terminal state if it was modified. */
315 	if (term_reset)
316 		(void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &term1);
317 	return (rval);
318 }
319 
320 /*
321  * cl_resize --
322  *	Reset the options for a resize event.
323  */
324 static int
cl_resize(sp,lines,columns)325 cl_resize(sp, lines, columns)
326 	SCR *sp;
327 	size_t lines, columns;
328 {
329 	ARGS *argv[2], a, b;
330 	char b1[1024];
331 
332 	a.bp = b1;
333 	b.bp = NULL;
334 	a.len = b.len = 0;
335 	argv[0] = &a;
336 	argv[1] = &b;
337 
338 	(void)snprintf(b1, sizeof(b1), "lines=%lu", (u_long)lines);
339 	a.len = strlen(b1);
340 	if (opts_set(sp, argv, NULL))
341 		return (1);
342 	(void)snprintf(b1, sizeof(b1), "columns=%lu", (u_long)columns);
343 	a.len = strlen(b1);
344 	if (opts_set(sp, argv, NULL))
345 		return (1);
346 	return (0);
347 }
348