the script used to extract a release
[nvi.git] / cl / cl_read.c
blob6ea50fe0dcaed0f1776e723c1b10d62880572db4
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.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: cl_read.c,v 10.20 2000/07/23 11:14:44 skimo Exp $ (Berkeley) $Date: 2000/07/23 11:14:44 $";
14 #endif /* not lint */
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>
23 #include <bitstring.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
33 #include "../common/common.h"
34 #include "../ex/script.h"
35 #include "cl.h"
37 static input_t cl_read __P((SCR *,
38 u_int32_t, char *, size_t, int *, struct timeval *));
39 static int cl_resize __P((SCR *, size_t, size_t));
42 * cl_event --
43 * Return a single event.
45 * PUBLIC: int cl_event __P((SCR *, EVENT *, u_int32_t, int));
47 int
48 cl_event(sp, evp, flags, ms)
49 SCR *sp;
50 EVENT *evp;
51 u_int32_t flags;
52 int ms;
54 struct timeval t, *tp;
55 CL_PRIVATE *clp;
56 size_t lines, columns;
57 int changed, nr;
58 CHAR_T *wp;
59 size_t wlen;
62 * Queue signal based events. We never clear SIGHUP or SIGTERM events,
63 * so that we just keep returning them until the editor dies.
65 clp = CLP(sp);
66 retest: if (LF_ISSET(EC_INTERRUPT) || F_ISSET(clp, CL_SIGINT)) {
67 if (F_ISSET(clp, CL_SIGINT)) {
68 F_CLR(clp, CL_SIGINT);
69 evp->e_event = E_INTERRUPT;
70 } else
71 evp->e_event = E_TIMEOUT;
72 return (0);
74 if (F_ISSET(clp, CL_SIGHUP | CL_SIGTERM | CL_SIGWINCH)) {
75 if (F_ISSET(clp, CL_SIGHUP)) {
76 evp->e_event = E_SIGHUP;
77 return (0);
79 if (F_ISSET(clp, CL_SIGTERM)) {
80 evp->e_event = E_SIGTERM;
81 return (0);
83 if (F_ISSET(clp, CL_SIGWINCH)) {
84 F_CLR(clp, CL_SIGWINCH);
85 if (cl_ssize(sp, 1, &lines, &columns, &changed))
86 return (1);
87 if (changed) {
88 (void)cl_resize(sp, lines, columns);
89 evp->e_event = E_WRESIZE;
90 return (0);
92 /* No real change, ignore the signal. */
96 /* Set timer. */
97 if (ms == 0)
98 tp = NULL;
99 else {
100 t.tv_sec = ms / 1000;
101 t.tv_usec = (ms % 1000) * 1000;
102 tp = &t;
105 /* Read input characters. */
106 switch (cl_read(sp, LF_ISSET(EC_QUOTED | EC_RAW),
107 (char *)clp->ibuf, sizeof(clp->ibuf)/sizeof(CHAR_T), &nr, tp)) {
108 case INP_OK:
109 CHAR2INT(sp, (char *)clp->ibuf, nr, wp, wlen);
110 MEMMOVEW(clp->ibuf, wp, wlen);
111 evp->e_csp = clp->ibuf;
112 evp->e_len = wlen;
113 evp->e_event = E_STRING;
114 break;
115 case INP_EOF:
116 evp->e_event = E_EOF;
117 break;
118 case INP_ERR:
119 evp->e_event = E_ERR;
120 break;
121 case INP_INTR:
122 goto retest;
123 case INP_TIMEOUT:
124 evp->e_event = E_TIMEOUT;
125 break;
126 default:
127 abort();
129 return (0);
133 * cl_read --
134 * Read characters from the input.
136 static input_t
137 cl_read(sp, flags, bp, blen, nrp, tp)
138 SCR *sp;
139 u_int32_t flags;
140 char *bp;
141 size_t blen;
142 int *nrp;
143 struct timeval *tp;
145 struct termios term1, term2;
146 struct timeval poll;
147 CL_PRIVATE *clp;
148 GS *gp;
149 SCR *tsp;
150 fd_set rdfd;
151 input_t rval;
152 int maxfd, nr, term_reset;
154 gp = sp->gp;
155 clp = CLP(sp);
156 term_reset = 0;
159 * 1: A read from a file or a pipe. In this case, the reads
160 * never timeout regardless. This means that we can hang
161 * when trying to complete a map, but we're going to hang
162 * on the next read anyway.
164 if (!F_ISSET(clp, CL_STDIN_TTY)) {
165 switch (nr = read(STDIN_FILENO, bp, blen)) {
166 case 0:
167 return (INP_EOF);
168 case -1:
169 goto err;
170 default:
171 *nrp = nr;
172 return (INP_OK);
174 /* NOTREACHED */
178 * 2: A read with an associated timeout, e.g., trying to complete
179 * a map sequence. If input exists, we fall into #3.
181 FD_ZERO(&rdfd);
182 poll.tv_sec = 0;
183 poll.tv_usec = 0;
184 if (tp != NULL) {
185 FD_SET(STDIN_FILENO, &rdfd);
186 switch (select(STDIN_FILENO + 1,
187 &rdfd, NULL, NULL, tp == NULL ? &poll : tp)) {
188 case 0:
189 return (INP_TIMEOUT);
190 case -1:
191 goto err;
192 default:
193 break;
198 * The user can enter a key in the editor to quote a character. If we
199 * get here and the next key is supposed to be quoted, do what we can.
200 * Reset the tty so that the user can enter a ^C, ^Q, ^S. There's an
201 * obvious race here, when the key has already been entered, but there's
202 * nothing that we can do to fix that problem.
204 * The editor can ask for the next literal character even thought it's
205 * generally running in line-at-a-time mode. Do what we can.
207 if (LF_ISSET(EC_QUOTED | EC_RAW) && !tcgetattr(STDIN_FILENO, &term1)) {
208 term_reset = 1;
209 if (LF_ISSET(EC_QUOTED)) {
210 term2 = term1;
211 term2.c_lflag &= ~ISIG;
212 term2.c_iflag &= ~(IXON | IXOFF);
213 (void)tcsetattr(STDIN_FILENO,
214 TCSASOFT | TCSADRAIN, &term2);
215 } else
216 (void)tcsetattr(STDIN_FILENO,
217 TCSASOFT | TCSADRAIN, &clp->vi_enter);
221 * 3: Wait for input.
223 * Select on the command input and scripting window file descriptors.
224 * It's ugly that we wait on scripting file descriptors here, but it's
225 * the only way to keep from locking out scripting windows.
227 if (F_ISSET(gp, G_SCRWIN)) {
228 FD_ZERO(&rdfd);
229 FD_SET(STDIN_FILENO, &rdfd);
230 maxfd = STDIN_FILENO;
231 if (sscr_check_input(sp, &rdfd, maxfd))
232 goto err;
236 * 4: Read the input.
238 * !!!
239 * What's going on here is some scary stuff. Ex runs the terminal in
240 * canonical mode. So, the <newline> character terminating a line of
241 * input is returned in the buffer, but a trailing <EOF> character is
242 * not similarly included. As ex uses 0<EOF> and ^<EOF> as autoindent
243 * commands, it has to see the trailing <EOF> characters to determine
244 * the difference between the user entering "0ab" and "0<EOF>ab". We
245 * leave an extra slot in the buffer, so that we can add a trailing
246 * <EOF> character if the buffer isn't terminated by a <newline>. We
247 * lose if the buffer is too small for the line and exactly N characters
248 * are entered followed by an <EOF> character.
250 #define ONE_FOR_EOF 1
251 switch (nr = read(STDIN_FILENO, bp, blen - ONE_FOR_EOF)) {
252 case 0: /* EOF. */
254 * ^D in canonical mode returns a read of 0, i.e. EOF. EOF is
255 * a valid command, but we don't want to loop forever because
256 * the terminal driver is returning EOF because the user has
257 * disconnected. The editor will almost certainly try to write
258 * something before this fires, which should kill us, but You
259 * Never Know.
261 if (++clp->eof_count < 50) {
262 bp[0] = clp->orig.c_cc[VEOF];
263 *nrp = 1;
264 rval = INP_OK;
266 } else
267 rval = INP_EOF;
268 break;
269 case -1: /* Error or interrupt. */
270 err: if (errno == EINTR)
271 rval = INP_INTR;
272 else {
273 rval = INP_ERR;
274 msgq(sp, M_SYSERR, "input");
276 break;
277 default: /* Input characters. */
278 if (F_ISSET(sp, SC_EX) && bp[nr - 1] != '\n')
279 bp[nr++] = clp->orig.c_cc[VEOF];
280 *nrp = nr;
281 clp->eof_count = 0;
282 rval = INP_OK;
283 break;
286 /* Restore the terminal state if it was modified. */
287 if (term_reset)
288 (void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &term1);
289 return (rval);
293 * cl_resize --
294 * Reset the options for a resize event.
296 static int
297 cl_resize(sp, lines, columns)
298 SCR *sp;
299 size_t lines, columns;
301 int rval;
303 rval = api_opts_set(sp, "lines", NULL, lines, 0);
304 if (api_opts_set(sp, "columns", NULL, columns, 0))
305 rval = 1;
306 return (rval);