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.16 1996/12/04 19:07:53 bostic Exp $ (Berkeley) $Date: 1996/12/04 19:07:53 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #ifdef HAVE_SYS_SELECT_H
19 #include <sys/select.h>
23 #include <bitstring.h>
34 #include "../common/common.h"
35 #include "../ex/script.h"
38 static input_t cl_read
__P((SCR
*,
39 u_int32_t
, CHAR_T
*, size_t, int *, struct timeval
*));
40 static int cl_resize
__P((SCR
*, size_t, size_t));
44 * Return a single event.
46 * PUBLIC: int cl_event __P((SCR *, EVENT *, u_int32_t, int));
49 cl_event(sp
, evp
, flags
, ms
)
55 struct timeval t
, *tp
;
57 size_t lines
, columns
;
61 * Queue signal based events. We never clear SIGHUP or SIGTERM events,
62 * so that we just keep returning them until the editor dies.
65 retest
: if (LF_ISSET(EC_INTERRUPT
) || F_ISSET(clp
, CL_SIGINT
)) {
66 if (F_ISSET(clp
, CL_SIGINT
)) {
67 F_CLR(clp
, CL_SIGINT
);
68 evp
->e_event
= E_INTERRUPT
;
70 evp
->e_event
= E_TIMEOUT
;
73 if (F_ISSET(clp
, CL_SIGHUP
| CL_SIGTERM
| CL_SIGWINCH
)) {
74 if (F_ISSET(clp
, CL_SIGHUP
)) {
75 evp
->e_event
= E_SIGHUP
;
78 if (F_ISSET(clp
, CL_SIGTERM
)) {
79 evp
->e_event
= E_SIGTERM
;
82 if (F_ISSET(clp
, CL_SIGWINCH
)) {
83 F_CLR(clp
, CL_SIGWINCH
);
84 if (cl_ssize(sp
, 1, &lines
, &columns
, &changed
))
87 (void)cl_resize(sp
, lines
, columns
);
88 evp
->e_event
= E_WRESIZE
;
91 /* No real change, ignore the signal. */
100 t
.tv_usec
= (ms
% 1000) * 1000;
104 /* Read input characters. */
105 switch (cl_read(sp
, LF_ISSET(EC_QUOTED
| EC_RAW
),
106 clp
->ibuf
, sizeof(clp
->ibuf
), &nr
, tp
)) {
108 evp
->e_csp
= clp
->ibuf
;
110 evp
->e_event
= E_STRING
;
113 evp
->e_event
= E_EOF
;
116 evp
->e_event
= E_ERR
;
121 evp
->e_event
= E_TIMEOUT
;
131 * Read characters from the input.
134 cl_read(sp
, flags
, bp
, blen
, nrp
, tp
)
142 struct termios term1
, term2
;
149 int maxfd
, nr
, term_reset
;
156 * 1: A read from a file or a pipe. In this case, the reads
157 * never timeout regardless. This means that we can hang
158 * when trying to complete a map, but we're going to hang
159 * on the next read anyway.
161 if (!F_ISSET(clp
, CL_STDIN_TTY
)) {
162 switch (nr
= read(STDIN_FILENO
, bp
, blen
)) {
175 * 2: A read with an associated timeout, e.g., trying to complete
176 * a map sequence. If input exists, we fall into #3.
182 FD_SET(STDIN_FILENO
, &rdfd
);
183 switch (select(STDIN_FILENO
+ 1,
184 &rdfd
, NULL
, NULL
, tp
== NULL
? &poll
: tp
)) {
186 return (INP_TIMEOUT
);
195 * The user can enter a key in the editor to quote a character. If we
196 * get here and the next key is supposed to be quoted, do what we can.
197 * Reset the tty so that the user can enter a ^C, ^Q, ^S. There's an
198 * obvious race here, when the key has already been entered, but there's
199 * nothing that we can do to fix that problem.
201 * The editor can ask for the next literal character even thought it's
202 * generally running in line-at-a-time mode. Do what we can.
204 if (LF_ISSET(EC_QUOTED
| EC_RAW
) && !tcgetattr(STDIN_FILENO
, &term1
)) {
206 if (LF_ISSET(EC_QUOTED
)) {
208 term2
.c_lflag
&= ~ISIG
;
209 term2
.c_iflag
&= ~(IXON
| IXOFF
);
210 (void)tcsetattr(STDIN_FILENO
,
211 TCSASOFT
| TCSADRAIN
, &term2
);
213 (void)tcsetattr(STDIN_FILENO
,
214 TCSASOFT
| TCSADRAIN
, &clp
->vi_enter
);
220 * Select on the command input and scripting window file descriptors.
221 * It's ugly that we wait on scripting file descriptors here, but it's
222 * the only way to keep from locking out scripting windows.
224 if (F_ISSET(gp
, G_SCRWIN
)) {
225 loop
: FD_ZERO(&rdfd
);
226 FD_SET(STDIN_FILENO
, &rdfd
);
227 maxfd
= STDIN_FILENO
;
228 for (tsp
= gp
->dq
.cqh_first
;
229 tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
)
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(sp
, lines
, columns
)
314 size_t lines
, columns
;
318 rval
= api_opts_set(sp
, "lines", NULL
, lines
, 0);
319 if (api_opts_set(sp
, "columns", NULL
, columns
, 0))