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.
13 static const char sccsid
[] = "$Id: cl_read.c,v 10.30 2012/07/12 18:28:58 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/select.h>
20 #include <bitstring.h>
30 #include "../common/common.h"
31 #include "../ex/script.h"
34 /* Pollution by Solaris curses. */
38 static input_t
cl_read(SCR
*,
39 u_int32_t
, char *, size_t, int *, struct timeval
*);
40 static int cl_resize(SCR
*, size_t, size_t);
44 * Return a single event.
46 * PUBLIC: int cl_event(SCR *, EVENT *, u_int32_t, int);
49 cl_event(SCR
*sp
, EVENT
*evp
, u_int32_t flags
, int ms
)
51 struct timeval t
, *tp
;
53 size_t lines
, columns
;
60 * Queue signal based events. We never clear SIGHUP or SIGTERM events,
61 * so that we just keep returning them until the editor dies.
64 retest
: if (LF_ISSET(EC_INTERRUPT
) || F_ISSET(clp
, CL_SIGINT
)) {
65 if (F_ISSET(clp
, CL_SIGINT
)) {
66 F_CLR(clp
, CL_SIGINT
);
67 evp
->e_event
= E_INTERRUPT
;
69 evp
->e_event
= E_TIMEOUT
;
72 if (F_ISSET(clp
, CL_SIGHUP
| CL_SIGTERM
| CL_SIGWINCH
)) {
73 if (F_ISSET(clp
, CL_SIGHUP
)) {
74 evp
->e_event
= E_SIGHUP
;
77 if (F_ISSET(clp
, CL_SIGTERM
)) {
78 evp
->e_event
= E_SIGTERM
;
81 if (F_ISSET(clp
, CL_SIGWINCH
)) {
82 F_CLR(clp
, CL_SIGWINCH
);
83 if (cl_ssize(sp
, 1, &lines
, &columns
, &changed
))
86 (void)cl_resize(sp
, lines
, columns
);
87 evp
->e_event
= E_WRESIZE
;
90 /* No real change, ignore the signal. */
99 t
.tv_usec
= (ms
% 1000) * 1000;
103 /* Read input characters. */
105 switch (cl_read(sp
, LF_ISSET(EC_QUOTED
| EC_RAW
),
106 clp
->ibuf
+ clp
->skip
, SIZE(clp
->ibuf
) - clp
->skip
, &nr
, tp
)) {
108 rc
= INPUT2INT5(sp
, clp
->cw
, clp
->ibuf
, nr
+ clp
->skip
,
112 evp
->e_event
= E_STRING
;
115 memmove(clp
->ibuf
, clp
->ibuf
+ nr
+ clp
->skip
- n
, n
);
122 msgq(sp
, M_ERR
, "323|Invalid input. Truncated.");
125 evp
->e_event
= E_EOF
;
128 evp
->e_event
= E_ERR
;
133 evp
->e_event
= E_TIMEOUT
;
143 * Read characters from the input.
146 cl_read(SCR
*sp
, u_int32_t flags
, char *bp
, size_t blen
, int *nrp
,
149 struct termios term1
, term2
;
154 int maxfd
, nr
, term_reset
;
161 * 1: A read from a file or a pipe. In this case, the reads
162 * never timeout regardless. This means that we can hang
163 * when trying to complete a map, but we're going to hang
164 * on the next read anyway.
166 if (!F_ISSET(clp
, CL_STDIN_TTY
)) {
167 switch (nr
= read(STDIN_FILENO
, bp
, blen
)) {
180 * 2: A read with an associated timeout, e.g., trying to complete
181 * a map sequence. If input exists, we fall into #3.
185 FD_SET(STDIN_FILENO
, &rdfd
);
186 switch (select(STDIN_FILENO
+ 1, &rdfd
, NULL
, NULL
, tp
)) {
188 return (INP_TIMEOUT
);
197 * The user can enter a key in the editor to quote a character. If we
198 * get here and the next key is supposed to be quoted, do what we can.
199 * Reset the tty so that the user can enter a ^C, ^Q, ^S. There's an
200 * obvious race here, when the key has already been entered, but there's
201 * nothing that we can do to fix that problem.
203 * The editor can ask for the next literal character even thought it's
204 * generally running in line-at-a-time mode. Do what we can.
206 if (LF_ISSET(EC_QUOTED
| EC_RAW
) && !tcgetattr(STDIN_FILENO
, &term1
)) {
208 if (LF_ISSET(EC_QUOTED
)) {
210 term2
.c_lflag
&= ~ISIG
;
211 term2
.c_iflag
&= ~(IXON
| IXOFF
);
212 (void)tcsetattr(STDIN_FILENO
,
213 TCSASOFT
| TCSADRAIN
, &term2
);
215 (void)tcsetattr(STDIN_FILENO
,
216 TCSASOFT
| TCSADRAIN
, &clp
->vi_enter
);
222 * Select on the command input and scripting window file descriptors.
223 * It's ugly that we wait on scripting file descriptors here, but it's
224 * the only way to keep from locking out scripting windows.
226 if (F_ISSET(gp
, G_SCRWIN
)) {
227 loop
: FD_ZERO(&rdfd
);
228 FD_SET(STDIN_FILENO
, &rdfd
);
229 maxfd
= STDIN_FILENO
;
230 if (F_ISSET(sp
, SC_SCRIPT
)) {
231 FD_SET(sp
->script
->sh_master
, &rdfd
);
232 if (sp
->script
->sh_master
> maxfd
)
233 maxfd
= sp
->script
->sh_master
;
235 switch (select(maxfd
+ 1, &rdfd
, NULL
, NULL
, NULL
)) {
243 if (!FD_ISSET(STDIN_FILENO
, &rdfd
)) {
254 * What's going on here is some scary stuff. Ex runs the terminal in
255 * canonical mode. So, the <newline> character terminating a line of
256 * input is returned in the buffer, but a trailing <EOF> character is
257 * not similarly included. As ex uses 0<EOF> and ^<EOF> as autoindent
258 * commands, it has to see the trailing <EOF> characters to determine
259 * the difference between the user entering "0ab" and "0<EOF>ab". We
260 * leave an extra slot in the buffer, so that we can add a trailing
261 * <EOF> character if the buffer isn't terminated by a <newline>. We
262 * lose if the buffer is too small for the line and exactly N characters
263 * are entered followed by an <EOF> character.
265 #define ONE_FOR_EOF 1
266 switch (nr
= read(STDIN_FILENO
, bp
, blen
- ONE_FOR_EOF
)) {
269 * ^D in canonical mode returns a read of 0, i.e. EOF. EOF is
270 * a valid command, but we don't want to loop forever because
271 * the terminal driver is returning EOF because the user has
272 * disconnected. The editor will almost certainly try to write
273 * something before this fires, which should kill us, but You
276 if (++clp
->eof_count
< 50) {
277 bp
[0] = clp
->orig
.c_cc
[VEOF
];
284 case -1: /* Error or interrupt. */
285 err
: if (errno
== EINTR
)
289 msgq(sp
, M_SYSERR
, "input");
292 default: /* Input characters. */
293 if (F_ISSET(sp
, SC_EX
) && bp
[nr
- 1] != '\n')
294 bp
[nr
++] = clp
->orig
.c_cc
[VEOF
];
301 /* Restore the terminal state if it was modified. */
303 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &term1
);
309 * Reset the options for a resize event.
312 cl_resize(SCR
*sp
, size_t lines
, size_t columns
)
323 a
.len
= SPRINTF(b1
, sizeof(b1
), L("lines=%lu"), (u_long
)lines
);
324 if (opts_set(sp
, argv
, NULL
))
326 a
.len
= SPRINTF(b1
, sizeof(b1
), L("columns=%lu"), (u_long
)columns
);
327 if (opts_set(sp
, argv
, NULL
))