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