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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/select.h>
16 #include <bitstring.h>
26 #include "../common/common.h"
27 #include "../ex/script.h"
30 /* Pollution by Solaris curses. */
34 static input_t cl_read(SCR *,
35 u_int32_t, char *, size_t, int *, struct timeval *);
36 static int cl_resize(SCR *, size_t, size_t);
40 * Return a single event.
42 * PUBLIC: int cl_event(SCR *, EVENT *, u_int32_t, int);
45 cl_event(SCR *sp, EVENT *evp, u_int32_t flags, int ms)
47 struct timeval t, *tp;
49 size_t lines, columns;
56 * Queue signal based events. We never clear SIGHUP or SIGTERM events,
57 * so that we just keep returning them until the editor dies.
60 retest: if (LF_ISSET(EC_INTERRUPT) || F_ISSET(clp, CL_SIGINT)) {
61 if (F_ISSET(clp, CL_SIGINT)) {
62 F_CLR(clp, CL_SIGINT);
63 evp->e_event = E_INTERRUPT;
65 evp->e_event = E_TIMEOUT;
68 if (F_ISSET(clp, CL_SIGHUP | CL_SIGTERM | CL_SIGWINCH)) {
69 if (F_ISSET(clp, CL_SIGHUP)) {
70 evp->e_event = E_SIGHUP;
73 if (F_ISSET(clp, CL_SIGTERM)) {
74 evp->e_event = E_SIGTERM;
77 if (F_ISSET(clp, CL_SIGWINCH)) {
78 F_CLR(clp, CL_SIGWINCH);
79 if (cl_ssize(sp, 1, &lines, &columns, &changed))
82 (void)cl_resize(sp, lines, columns);
83 evp->e_event = E_WRESIZE;
86 /* No real change, ignore the signal. */
95 t.tv_usec = (ms % 1000) * 1000;
99 /* Read input characters. */
101 switch (cl_read(sp, LF_ISSET(EC_QUOTED | EC_RAW),
102 clp->ibuf + clp->skip, SIZE(clp->ibuf) - clp->skip, &nr, tp)) {
104 rc = INPUT2INT5(sp, clp->cw, clp->ibuf, nr + clp->skip,
108 evp->e_event = E_STRING;
111 memmove(clp->ibuf, clp->ibuf + nr + clp->skip - n, n);
118 msgq(sp, M_ERR, "323|Invalid input. Truncated.");
121 evp->e_event = E_EOF;
124 evp->e_event = E_ERR;
129 evp->e_event = E_TIMEOUT;
139 * Read characters from the input.
142 cl_read(SCR *sp, u_int32_t flags, char *bp, size_t blen, int *nrp,
145 struct termios term1, term2;
150 int maxfd, nr, term_reset;
157 * 1: A read from a file or a pipe. In this case, the reads
158 * never timeout regardless. This means that we can hang
159 * when trying to complete a map, but we're going to hang
160 * on the next read anyway.
162 if (!F_ISSET(clp, CL_STDIN_TTY)) {
163 switch (nr = read(STDIN_FILENO, bp, blen)) {
176 * 2: A read with an associated timeout, e.g., trying to complete
177 * a map sequence. If input exists, we fall into #3.
181 FD_SET(STDIN_FILENO, &rdfd);
182 switch (select(STDIN_FILENO + 1, &rdfd, NULL, NULL, tp)) {
184 return (INP_TIMEOUT);
193 * The user can enter a key in the editor to quote a character. If we
194 * get here and the next key is supposed to be quoted, do what we can.
195 * Reset the tty so that the user can enter a ^C, ^Q, ^S. There's an
196 * obvious race here, when the key has already been entered, but there's
197 * nothing that we can do to fix that problem.
199 * The editor can ask for the next literal character even thought it's
200 * generally running in line-at-a-time mode. Do what we can.
202 if (LF_ISSET(EC_QUOTED | EC_RAW) && !tcgetattr(STDIN_FILENO, &term1)) {
204 if (LF_ISSET(EC_QUOTED)) {
206 term2.c_lflag &= ~ISIG;
207 term2.c_iflag &= ~(IXON | IXOFF);
208 (void)tcsetattr(STDIN_FILENO,
209 TCSASOFT | TCSADRAIN, &term2);
211 (void)tcsetattr(STDIN_FILENO,
212 TCSASOFT | TCSADRAIN, &clp->vi_enter);
218 * Select on the command input and scripting window file descriptors.
219 * It's ugly that we wait on scripting file descriptors here, but it's
220 * the only way to keep from locking out scripting windows.
222 if (F_ISSET(gp, G_SCRWIN)) {
223 loop: FD_ZERO(&rdfd);
224 FD_SET(STDIN_FILENO, &rdfd);
225 maxfd = STDIN_FILENO;
226 if (F_ISSET(sp, SC_SCRIPT)) {
227 FD_SET(sp->script->sh_master, &rdfd);
228 if (sp->script->sh_master > maxfd)
229 maxfd = sp->script->sh_master;
231 switch (select(maxfd + 1, &rdfd, NULL, NULL, NULL)) {
239 if (!FD_ISSET(STDIN_FILENO, &rdfd)) {
250 * What's going on here is some scary stuff. Ex runs the terminal in
251 * canonical mode. So, the <newline> character terminating a line of
252 * input is returned in the buffer, but a trailing <EOF> character is
253 * not similarly included. As ex uses 0<EOF> and ^<EOF> as autoindent
254 * commands, it has to see the trailing <EOF> characters to determine
255 * the difference between the user entering "0ab" and "0<EOF>ab". We
256 * leave an extra slot in the buffer, so that we can add a trailing
257 * <EOF> character if the buffer isn't terminated by a <newline>. We
258 * lose if the buffer is too small for the line and exactly N characters
259 * are entered followed by an <EOF> character.
261 #define ONE_FOR_EOF 1
262 switch (nr = read(STDIN_FILENO, bp, blen - ONE_FOR_EOF)) {
265 * ^D in canonical mode returns a read of 0, i.e. EOF. EOF is
266 * a valid command, but we don't want to loop forever because
267 * the terminal driver is returning EOF because the user has
268 * disconnected. The editor will almost certainly try to write
269 * something before this fires, which should kill us, but You
272 if (++clp->eof_count < 50) {
273 bp[0] = clp->orig.c_cc[VEOF];
280 case -1: /* Error or interrupt. */
281 err: if (errno == EINTR)
285 msgq(sp, M_SYSERR, "input");
288 default: /* Input characters. */
289 if (F_ISSET(sp, SC_EX) && bp[nr - 1] != '\n')
290 bp[nr++] = clp->orig.c_cc[VEOF];
297 /* Restore the terminal state if it was modified. */
299 (void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &term1);
305 * Reset the options for a resize event.
308 cl_resize(SCR *sp, size_t lines, size_t columns)
319 a.len = SPRINTF(b1, sizeof(b1), L("lines=%lu"), (u_long)lines);
320 if (opts_set(sp, argv, NULL))
322 a.len = SPRINTF(b1, sizeof(b1), L("columns=%lu"), (u_long)columns);
323 if (opts_set(sp, argv, NULL))