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