vi/v_txt.c: txt_dent: introduce sequence point between decrement and access
[nvi.git] / cl / cl_read.c
blob6c182bdf23ef197f3be68d56ccdda13f2b0dabf7
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.29 2001/08/18 21:51:59 skimo Exp $ (Berkeley) $Date: 2001/08/18 21:51:59 $";
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 /* Pollution by Solaris curses. */
38 #undef columns
39 #undef lines
41 static input_t cl_read __P((SCR *,
42 u_int32_t, char *, size_t, int *, struct timeval *));
43 static int cl_resize __P((SCR *, size_t, size_t));
46 * cl_event --
47 * Return a single event.
49 * PUBLIC: int cl_event __P((SCR *, EVENT *, u_int32_t, int));
51 int
52 cl_event(SCR *sp, EVENT *evp, u_int32_t flags, 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;
60 int rc;
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 read:
108 switch (cl_read(sp, LF_ISSET(EC_QUOTED | EC_RAW),
109 clp->ibuf + clp->skip, SIZE(clp->ibuf) - clp->skip, &nr, tp)) {
110 case INP_OK:
111 rc = INPUT2INT5(sp, clp->cw, clp->ibuf, nr + clp->skip,
112 wp, wlen);
113 evp->e_csp = wp;
114 evp->e_len = wlen;
115 evp->e_event = E_STRING;
116 if (rc < 0) {
117 int n = -rc;
118 memmove(clp->ibuf, clp->ibuf + nr + clp->skip - n, n);
119 clp->skip = n;
120 if (wlen == 0)
121 goto read;
122 } else if (rc == 0)
123 clp->skip = 0;
124 else
125 msgq(sp, M_ERR, "323|Invalid input. Truncated.");
126 break;
127 case INP_ERR:
128 evp->e_event = E_ERR;
129 break;
130 case INP_EOF:
131 evp->e_event = E_EOF;
132 break;
133 case INP_INTR:
134 goto retest;
135 case INP_TIMEOUT:
136 evp->e_event = E_TIMEOUT;
137 break;
138 default:
139 abort();
141 return (0);
145 * cl_read --
146 * Read characters from the input.
148 static input_t
149 cl_read(SCR *sp, u_int32_t flags, char *bp, size_t blen, int *nrp, struct timeval *tp)
151 struct termios term1, term2;
152 struct timeval poll;
153 CL_PRIVATE *clp;
154 GS *gp;
155 SCR *tsp;
156 fd_set rdfd;
157 input_t rval;
158 int maxfd, nr, term_reset;
160 gp = sp->gp;
161 clp = CLP(sp);
162 term_reset = 0;
165 * 1: A read from a file or a pipe. In this case, the reads
166 * never timeout regardless. This means that we can hang
167 * when trying to complete a map, but we're going to hang
168 * on the next read anyway.
170 if (!F_ISSET(clp, CL_STDIN_TTY)) {
171 switch (nr = read(STDIN_FILENO, bp, blen)) {
172 case 0:
173 return (INP_EOF);
174 case -1:
175 goto err;
176 default:
177 *nrp = nr;
178 return (INP_OK);
180 /* NOTREACHED */
184 * 2: A read with an associated timeout, e.g., trying to complete
185 * a map sequence. If input exists, we fall into #3.
187 FD_ZERO(&rdfd);
188 poll.tv_sec = 0;
189 poll.tv_usec = 0;
190 if (tp != NULL) {
191 FD_SET(STDIN_FILENO, &rdfd);
192 switch (select(STDIN_FILENO + 1,
193 &rdfd, NULL, NULL, tp == NULL ? &poll : tp)) {
194 case 0:
195 return (INP_TIMEOUT);
196 case -1:
197 goto err;
198 default:
199 break;
204 * The user can enter a key in the editor to quote a character. If we
205 * get here and the next key is supposed to be quoted, do what we can.
206 * Reset the tty so that the user can enter a ^C, ^Q, ^S. There's an
207 * obvious race here, when the key has already been entered, but there's
208 * nothing that we can do to fix that problem.
210 * The editor can ask for the next literal character even thought it's
211 * generally running in line-at-a-time mode. Do what we can.
213 if (LF_ISSET(EC_QUOTED | EC_RAW) && !tcgetattr(STDIN_FILENO, &term1)) {
214 term_reset = 1;
215 if (LF_ISSET(EC_QUOTED)) {
216 term2 = term1;
217 term2.c_lflag &= ~ISIG;
218 term2.c_iflag &= ~(IXON | IXOFF);
219 (void)tcsetattr(STDIN_FILENO,
220 TCSASOFT | TCSADRAIN, &term2);
221 } else
222 (void)tcsetattr(STDIN_FILENO,
223 TCSASOFT | TCSADRAIN, &clp->vi_enter);
227 * 3: Wait for input.
229 * Select on the command input and scripting window file descriptors.
230 * It's ugly that we wait on scripting file descriptors here, but it's
231 * the only way to keep from locking out scripting windows.
233 if (F_ISSET(gp, G_SCRWIN)) {
234 FD_ZERO(&rdfd);
235 FD_SET(STDIN_FILENO, &rdfd);
236 maxfd = STDIN_FILENO;
237 if (sscr_check_input(sp, &rdfd, maxfd))
238 goto err;
242 * 4: Read the input.
244 * !!!
245 * What's going on here is some scary stuff. Ex runs the terminal in
246 * canonical mode. So, the <newline> character terminating a line of
247 * input is returned in the buffer, but a trailing <EOF> character is
248 * not similarly included. As ex uses 0<EOF> and ^<EOF> as autoindent
249 * commands, it has to see the trailing <EOF> characters to determine
250 * the difference between the user entering "0ab" and "0<EOF>ab". We
251 * leave an extra slot in the buffer, so that we can add a trailing
252 * <EOF> character if the buffer isn't terminated by a <newline>. We
253 * lose if the buffer is too small for the line and exactly N characters
254 * are entered followed by an <EOF> character.
256 #define ONE_FOR_EOF 1
257 switch (nr = read(STDIN_FILENO, bp, blen - ONE_FOR_EOF)) {
258 case 0: /* EOF. */
260 * ^D in canonical mode returns a read of 0, i.e. EOF. EOF is
261 * a valid command, but we don't want to loop forever because
262 * the terminal driver is returning EOF because the user has
263 * disconnected. The editor will almost certainly try to write
264 * something before this fires, which should kill us, but You
265 * Never Know.
267 if (++clp->eof_count < 50) {
268 bp[0] = clp->orig.c_cc[VEOF];
269 *nrp = 1;
270 rval = INP_OK;
272 } else
273 rval = INP_EOF;
274 break;
275 case -1: /* Error or interrupt. */
276 err: if (errno == EINTR)
277 rval = INP_INTR;
278 else {
279 rval = INP_ERR;
280 msgq(sp, M_SYSERR, "input");
282 break;
283 default: /* Input characters. */
284 if (F_ISSET(sp, SC_EX) && bp[nr - 1] != '\n')
285 bp[nr++] = clp->orig.c_cc[VEOF];
286 *nrp = nr;
287 clp->eof_count = 0;
288 rval = INP_OK;
289 break;
292 /* Restore the terminal state if it was modified. */
293 if (term_reset)
294 (void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &term1);
295 return (rval);
299 * cl_resize --
300 * Reset the options for a resize event.
302 static int
303 cl_resize(SCR *sp, size_t lines, size_t columns)
305 int rval;
307 rval = api_opts_set(sp, L("lines"), NULL, lines, 0);
308 if (api_opts_set(sp, L("columns"), NULL, columns, 0))
309 rval = 1;
310 return (rval);