key.c revision 1.4.8.2 1 1.4.8.2 tls /* $NetBSD: key.c,v 1.4.8.2 2014/08/19 23:51:51 tls Exp $ */
2 1.4.8.2 tls /*-
3 1.4.8.2 tls * Copyright (c) 1991, 1993, 1994
4 1.4.8.2 tls * The Regents of the University of California. All rights reserved.
5 1.4.8.2 tls * Copyright (c) 1991, 1993, 1994, 1995, 1996
6 1.4.8.2 tls * Keith Bostic. All rights reserved.
7 1.4.8.2 tls *
8 1.4.8.2 tls * See the LICENSE file for redistribution information.
9 1.4.8.2 tls */
10 1.4.8.2 tls
11 1.4.8.2 tls #include "config.h"
12 1.4.8.2 tls
13 1.4.8.2 tls #include <sys/cdefs.h>
14 1.4.8.2 tls #if 0
15 1.4.8.2 tls #ifndef lint
16 1.4.8.2 tls static const char sccsid[] = "Id: key.c,v 10.48 2001/06/25 15:19:10 skimo Exp (Berkeley) Date: 2001/06/25 15:19:10 ";
17 1.4.8.2 tls #endif /* not lint */
18 1.4.8.2 tls #else
19 1.4.8.2 tls __RCSID("$NetBSD: key.c,v 1.4.8.2 2014/08/19 23:51:51 tls Exp $");
20 1.4.8.2 tls #endif
21 1.4.8.2 tls
22 1.4.8.2 tls #include <sys/types.h>
23 1.4.8.2 tls #include <sys/queue.h>
24 1.4.8.2 tls #include <sys/time.h>
25 1.4.8.2 tls
26 1.4.8.2 tls #include <bitstring.h>
27 1.4.8.2 tls #include <ctype.h>
28 1.4.8.2 tls #include <errno.h>
29 1.4.8.2 tls #include <limits.h>
30 1.4.8.2 tls #include <locale.h>
31 1.4.8.2 tls #include <stdio.h>
32 1.4.8.2 tls #include <stdlib.h>
33 1.4.8.2 tls #include <string.h>
34 1.4.8.2 tls #include <unistd.h>
35 1.4.8.2 tls
36 1.4.8.2 tls #include "common.h"
37 1.4.8.2 tls #include "../vi/vi.h"
38 1.4.8.2 tls
39 1.4.8.2 tls static int v_event_append __P((SCR *, EVENT *));
40 1.4.8.2 tls static int v_event_grow __P((SCR *, int));
41 1.4.8.2 tls static int v_key_cmp __P((const void *, const void *));
42 1.4.8.2 tls static void v_keyval __P((SCR *, int, scr_keyval_t));
43 1.4.8.2 tls static void v_sync __P((SCR *, int));
44 1.4.8.2 tls
45 1.4.8.2 tls /*
46 1.4.8.2 tls * !!!
47 1.4.8.2 tls * Historic vi always used:
48 1.4.8.2 tls *
49 1.4.8.2 tls * ^D: autoindent deletion
50 1.4.8.2 tls * ^H: last character deletion
51 1.4.8.2 tls * ^W: last word deletion
52 1.4.8.2 tls * ^Q: quote the next character (if not used in flow control).
53 1.4.8.2 tls * ^V: quote the next character
54 1.4.8.2 tls *
55 1.4.8.2 tls * regardless of the user's choices for these characters. The user's erase
56 1.4.8.2 tls * and kill characters worked in addition to these characters. Nvi wires
57 1.4.8.2 tls * down the above characters, but in addition permits the VEOF, VERASE, VKILL
58 1.4.8.2 tls * and VWERASE characters described by the user's termios structure.
59 1.4.8.2 tls *
60 1.4.8.2 tls * Ex was not consistent with this scheme, as it historically ran in tty
61 1.4.8.2 tls * cooked mode. This meant that the scroll command and autoindent erase
62 1.4.8.2 tls * characters were mapped to the user's EOF character, and the character
63 1.4.8.2 tls * and word deletion characters were the user's tty character and word
64 1.4.8.2 tls * deletion characters. This implementation makes it all consistent, as
65 1.4.8.2 tls * described above for vi.
66 1.4.8.2 tls *
67 1.4.8.2 tls * !!!
68 1.4.8.2 tls * This means that all screens share a special key set.
69 1.4.8.2 tls */
70 1.4.8.2 tls KEYLIST keylist[] = {
71 1.4.8.2 tls {K_BACKSLASH, '\\'}, /* \ */
72 1.4.8.2 tls {K_CARAT, '^'}, /* ^ */
73 1.4.8.2 tls {K_CNTRLD, '\004'}, /* ^D */
74 1.4.8.2 tls {K_CNTRLR, '\022'}, /* ^R */
75 1.4.8.2 tls {K_CNTRLT, '\024'}, /* ^T */
76 1.4.8.2 tls {K_CNTRLZ, '\032'}, /* ^Z */
77 1.4.8.2 tls {K_COLON, ':'}, /* : */
78 1.4.8.2 tls {K_CR, '\r'}, /* \r */
79 1.4.8.2 tls {K_ESCAPE, '\033'}, /* ^[ */
80 1.4.8.2 tls {K_FORMFEED, '\f'}, /* \f */
81 1.4.8.2 tls {K_HEXCHAR, '\030'}, /* ^X */
82 1.4.8.2 tls {K_NL, '\n'}, /* \n */
83 1.4.8.2 tls {K_RIGHTBRACE, '}'}, /* } */
84 1.4.8.2 tls {K_RIGHTPAREN, ')'}, /* ) */
85 1.4.8.2 tls {K_TAB, '\t'}, /* \t */
86 1.4.8.2 tls {K_VERASE, '\b'}, /* \b */
87 1.4.8.2 tls {K_VKILL, '\025'}, /* ^U */
88 1.4.8.2 tls {K_VLNEXT, '\021'}, /* ^Q */
89 1.4.8.2 tls {K_VLNEXT, '\026'}, /* ^V */
90 1.4.8.2 tls {K_VWERASE, '\027'}, /* ^W */
91 1.4.8.2 tls {K_ZERO, '0'}, /* 0 */
92 1.4.8.2 tls
93 1.4.8.2 tls #define ADDITIONAL_CHARACTERS 4
94 1.4.8.2 tls {K_NOTUSED, 0}, /* VEOF, VERASE, VKILL, VWERASE */
95 1.4.8.2 tls {K_NOTUSED, 0},
96 1.4.8.2 tls {K_NOTUSED, 0},
97 1.4.8.2 tls {K_NOTUSED, 0},
98 1.4.8.2 tls };
99 1.4.8.2 tls static int nkeylist =
100 1.4.8.2 tls (sizeof(keylist) / sizeof(keylist[0])) - ADDITIONAL_CHARACTERS;
101 1.4.8.2 tls
102 1.4.8.2 tls /*
103 1.4.8.2 tls * v_key_init --
104 1.4.8.2 tls * Initialize the special key lookup table.
105 1.4.8.2 tls *
106 1.4.8.2 tls * PUBLIC: int v_key_init __P((SCR *));
107 1.4.8.2 tls */
108 1.4.8.2 tls int
109 1.4.8.2 tls v_key_init(SCR *sp)
110 1.4.8.2 tls {
111 1.4.8.2 tls int ch;
112 1.4.8.2 tls GS *gp;
113 1.4.8.2 tls KEYLIST *kp;
114 1.4.8.2 tls int cnt;
115 1.4.8.2 tls
116 1.4.8.2 tls gp = sp->gp;
117 1.4.8.2 tls
118 1.4.8.2 tls /*
119 1.4.8.2 tls * XXX
120 1.4.8.2 tls * 8-bit only, for now. Recompilation should get you any 8-bit
121 1.4.8.2 tls * character set, as long as nul isn't a character.
122 1.4.8.2 tls */
123 1.4.8.2 tls (void)setlocale(LC_ALL, "");
124 1.4.8.2 tls #if __linux__
125 1.4.8.2 tls /*
126 1.4.8.2 tls * In libc 4.5.26, setlocale(LC_ALL, ""), doesn't setup the table
127 1.4.8.2 tls * for ctype(3c) correctly. This bug is fixed in libc 4.6.x.
128 1.4.8.2 tls *
129 1.4.8.2 tls * This code works around this problem for libc 4.5.x users.
130 1.4.8.2 tls * Note that this code is harmless if you're using libc 4.6.x.
131 1.4.8.2 tls */
132 1.4.8.2 tls (void)setlocale(LC_CTYPE, "");
133 1.4.8.2 tls #endif
134 1.4.8.2 tls v_key_ilookup(sp);
135 1.4.8.2 tls
136 1.4.8.2 tls v_keyval(sp, K_CNTRLD, KEY_VEOF);
137 1.4.8.2 tls v_keyval(sp, K_VERASE, KEY_VERASE);
138 1.4.8.2 tls v_keyval(sp, K_VKILL, KEY_VKILL);
139 1.4.8.2 tls v_keyval(sp, K_VWERASE, KEY_VWERASE);
140 1.4.8.2 tls
141 1.4.8.2 tls /* Sort the special key list. */
142 1.4.8.2 tls qsort(keylist, nkeylist, sizeof(keylist[0]), v_key_cmp);
143 1.4.8.2 tls
144 1.4.8.2 tls /* Initialize the fast lookup table. */
145 1.4.8.2 tls for (kp = keylist, cnt = nkeylist; cnt--; ++kp)
146 1.4.8.2 tls gp->special_key[kp->ch] = kp->value;
147 1.4.8.2 tls
148 1.4.8.2 tls /* Find a non-printable character to use as a message separator. */
149 1.4.8.2 tls for (ch = 1; ch <= UCHAR_MAX; ++ch)
150 1.4.8.2 tls if (!isprint(ch)) {
151 1.4.8.2 tls gp->noprint = ch;
152 1.4.8.2 tls break;
153 1.4.8.2 tls }
154 1.4.8.2 tls if (ch != gp->noprint) {
155 1.4.8.2 tls msgq(sp, M_ERR, "079|No non-printable character found");
156 1.4.8.2 tls return (1);
157 1.4.8.2 tls }
158 1.4.8.2 tls return (0);
159 1.4.8.2 tls }
160 1.4.8.2 tls
161 1.4.8.2 tls /*
162 1.4.8.2 tls * v_keyval --
163 1.4.8.2 tls * Set key values.
164 1.4.8.2 tls *
165 1.4.8.2 tls * We've left some open slots in the keylist table, and if these values exist,
166 1.4.8.2 tls * we put them into place. Note, they may reset (or duplicate) values already
167 1.4.8.2 tls * in the table, so we check for that first.
168 1.4.8.2 tls */
169 1.4.8.2 tls static void
170 1.4.8.2 tls v_keyval(SCR *sp, int val, scr_keyval_t name)
171 1.4.8.2 tls {
172 1.4.8.2 tls KEYLIST *kp;
173 1.4.8.2 tls CHAR_T ch;
174 1.4.8.2 tls int dne;
175 1.4.8.2 tls
176 1.4.8.2 tls /* Get the key's value from the screen. */
177 1.4.8.2 tls if (sp->gp->scr_keyval(sp, name, &ch, &dne))
178 1.4.8.2 tls return;
179 1.4.8.2 tls if (dne)
180 1.4.8.2 tls return;
181 1.4.8.2 tls
182 1.4.8.2 tls /* Check for duplication. */
183 1.4.8.2 tls for (kp = keylist; kp->value != K_NOTUSED; ++kp)
184 1.4.8.2 tls if (kp->ch == ch) {
185 1.4.8.2 tls kp->value = val;
186 1.4.8.2 tls return;
187 1.4.8.2 tls }
188 1.4.8.2 tls
189 1.4.8.2 tls /* Add a new entry. */
190 1.4.8.2 tls if (kp->value == K_NOTUSED) {
191 1.4.8.2 tls keylist[nkeylist].ch = ch;
192 1.4.8.2 tls keylist[nkeylist].value = val;
193 1.4.8.2 tls ++nkeylist;
194 1.4.8.2 tls }
195 1.4.8.2 tls }
196 1.4.8.2 tls
197 1.4.8.2 tls /*
198 1.4.8.2 tls * v_key_ilookup --
199 1.4.8.2 tls * Build the fast-lookup key display array.
200 1.4.8.2 tls *
201 1.4.8.2 tls * PUBLIC: void v_key_ilookup __P((SCR *));
202 1.4.8.2 tls */
203 1.4.8.2 tls void
204 1.4.8.2 tls v_key_ilookup(SCR *sp)
205 1.4.8.2 tls {
206 1.4.8.2 tls UCHAR_T ch;
207 1.4.8.2 tls unsigned char *p, *t;
208 1.4.8.2 tls GS *gp;
209 1.4.8.2 tls size_t len;
210 1.4.8.2 tls
211 1.4.8.2 tls for (gp = sp->gp, ch = 0;; ++ch) {
212 1.4.8.2 tls for (p = gp->cname[ch].name, t = v_key_name(sp, ch),
213 1.4.8.2 tls len = gp->cname[ch].len = sp->clen; len--;)
214 1.4.8.2 tls *p++ = *t++;
215 1.4.8.2 tls if (ch == MAX_FAST_KEY)
216 1.4.8.2 tls break;
217 1.4.8.2 tls }
218 1.4.8.2 tls }
219 1.4.8.2 tls
220 1.4.8.2 tls /*
221 1.4.8.2 tls * v_key_len --
222 1.4.8.2 tls * Return the length of the string that will display the key.
223 1.4.8.2 tls * This routine is the backup for the KEY_LEN() macro.
224 1.4.8.2 tls *
225 1.4.8.2 tls * PUBLIC: size_t v_key_len __P((SCR *, ARG_CHAR_T));
226 1.4.8.2 tls */
227 1.4.8.2 tls size_t
228 1.4.8.2 tls v_key_len(SCR *sp, ARG_CHAR_T ch)
229 1.4.8.2 tls {
230 1.4.8.2 tls (void)v_key_name(sp, ch);
231 1.4.8.2 tls return (sp->clen);
232 1.4.8.2 tls }
233 1.4.8.2 tls
234 1.4.8.2 tls /*
235 1.4.8.2 tls * v_key_name --
236 1.4.8.2 tls * Return the string that will display the key. This routine
237 1.4.8.2 tls * is the backup for the KEY_NAME() macro.
238 1.4.8.2 tls *
239 1.4.8.2 tls * PUBLIC: u_char *v_key_name __P((SCR *, ARG_CHAR_T));
240 1.4.8.2 tls */
241 1.4.8.2 tls u_char *
242 1.4.8.2 tls v_key_name(SCR *sp, ARG_CHAR_T ach)
243 1.4.8.2 tls {
244 1.4.8.2 tls static const char hexdigit[] = "0123456789abcdef";
245 1.4.8.2 tls static const char octdigit[] = "01234567";
246 1.4.8.2 tls int ch;
247 1.4.8.2 tls size_t len, i;
248 1.4.8.2 tls const char *chp;
249 1.4.8.2 tls
250 1.4.8.2 tls if (INTISWIDE(ach))
251 1.4.8.2 tls goto vis;
252 1.4.8.2 tls ch = (unsigned char)ach;
253 1.4.8.2 tls
254 1.4.8.2 tls /* See if the character was explicitly declared printable or not. */
255 1.4.8.2 tls if ((chp = O_STR(sp, O_PRINT)) != NULL)
256 1.4.8.2 tls for (; *chp != '\0'; ++chp)
257 1.4.8.2 tls if (*chp == ch)
258 1.4.8.2 tls goto pr;
259 1.4.8.2 tls if ((chp = O_STR(sp, O_NOPRINT)) != NULL)
260 1.4.8.2 tls for (; *chp != '\0'; ++chp)
261 1.4.8.2 tls if (*chp == ch)
262 1.4.8.2 tls goto nopr;
263 1.4.8.2 tls
264 1.4.8.2 tls /*
265 1.4.8.2 tls * Historical (ARPA standard) mappings. Printable characters are left
266 1.4.8.2 tls * alone. Control characters less than 0x20 are represented as '^'
267 1.4.8.2 tls * followed by the character offset from the '@' character in the ASCII
268 1.4.8.2 tls * character set. Del (0x7f) is represented as '^' followed by '?'.
269 1.4.8.2 tls *
270 1.4.8.2 tls * XXX
271 1.4.8.2 tls * The following code depends on the current locale being identical to
272 1.4.8.2 tls * the ASCII map from 0x40 to 0x5f (since 0x1f + 0x40 == 0x5f). I'm
273 1.4.8.2 tls * told that this is a reasonable assumption...
274 1.4.8.2 tls *
275 1.4.8.2 tls * XXX
276 1.4.8.2 tls * This code will only work with CHAR_T's that are multiples of 8-bit
277 1.4.8.2 tls * bytes.
278 1.4.8.2 tls *
279 1.4.8.2 tls * XXX
280 1.4.8.2 tls * NB: There's an assumption here that all printable characters take
281 1.4.8.2 tls * up a single column on the screen. This is not always correct.
282 1.4.8.2 tls */
283 1.4.8.2 tls if (isprint(ch)) {
284 1.4.8.2 tls pr: sp->cname[0] = ch;
285 1.4.8.2 tls len = 1;
286 1.4.8.2 tls goto done;
287 1.4.8.2 tls }
288 1.4.8.2 tls nopr: if (iscntrl(ch) && (ch < 0x20 || ch == 0x7f)) {
289 1.4.8.2 tls sp->cname[0] = '^';
290 1.4.8.2 tls sp->cname[1] = ch == 0x7f ? '?' : '@' + ch;
291 1.4.8.2 tls len = 2;
292 1.4.8.2 tls goto done;
293 1.4.8.2 tls }
294 1.4.8.2 tls vis: for (i = 1; i <= sizeof(CHAR_T); ++i)
295 1.4.8.2 tls if ((ach >> i * CHAR_BIT) == 0)
296 1.4.8.2 tls break;
297 1.4.8.2 tls ch = (ach >> --i * CHAR_BIT) & UCHAR_MAX;
298 1.4.8.2 tls if (O_ISSET(sp, O_OCTAL)) {
299 1.4.8.2 tls sp->cname[0] = '\\';
300 1.4.8.2 tls sp->cname[1] = octdigit[(ch & 0300) >> 6];
301 1.4.8.2 tls sp->cname[2] = octdigit[(ch & 070) >> 3];
302 1.4.8.2 tls sp->cname[3] = octdigit[ ch & 07 ];
303 1.4.8.2 tls } else {
304 1.4.8.2 tls sp->cname[0] = '\\';
305 1.4.8.2 tls sp->cname[1] = 'x';
306 1.4.8.2 tls sp->cname[2] = hexdigit[(ch & 0xf0) >> 4];
307 1.4.8.2 tls sp->cname[3] = hexdigit[ ch & 0x0f ];
308 1.4.8.2 tls }
309 1.4.8.2 tls len = 4;
310 1.4.8.2 tls done: sp->cname[sp->clen = len] = '\0';
311 1.4.8.2 tls return (sp->cname);
312 1.4.8.2 tls }
313 1.4.8.2 tls
314 1.4.8.2 tls /*
315 1.4.8.2 tls * v_key_val --
316 1.4.8.2 tls * Fill in the value for a key. This routine is the backup
317 1.4.8.2 tls * for the KEY_VAL() macro.
318 1.4.8.2 tls *
319 1.4.8.2 tls * PUBLIC: e_key_t v_key_val __P((SCR *, ARG_CHAR_T));
320 1.4.8.2 tls */
321 1.4.8.2 tls e_key_t
322 1.4.8.2 tls v_key_val(SCR *sp, ARG_CHAR_T ch)
323 1.4.8.2 tls {
324 1.4.8.2 tls KEYLIST k, *kp;
325 1.4.8.2 tls
326 1.4.8.2 tls k.ch = ch;
327 1.4.8.2 tls kp = bsearch(&k, keylist, nkeylist, sizeof(keylist[0]), v_key_cmp);
328 1.4.8.2 tls return (kp == NULL ? K_NOTUSED : kp->value);
329 1.4.8.2 tls }
330 1.4.8.2 tls
331 1.4.8.2 tls /*
332 1.4.8.2 tls * v_event_push --
333 1.4.8.2 tls * Push events/keys onto the front of the buffer.
334 1.4.8.2 tls *
335 1.4.8.2 tls * There is a single input buffer in ex/vi. Characters are put onto the
336 1.4.8.2 tls * end of the buffer by the terminal input routines, and pushed onto the
337 1.4.8.2 tls * front of the buffer by various other functions in ex/vi. Each key has
338 1.4.8.2 tls * an associated flag value, which indicates if it has already been quoted,
339 1.4.8.2 tls * and if it is the result of a mapping or an abbreviation.
340 1.4.8.2 tls *
341 1.4.8.2 tls * PUBLIC: int v_event_push __P((SCR *, EVENT *, const CHAR_T *, size_t, u_int));
342 1.4.8.2 tls */
343 1.4.8.2 tls int
344 1.4.8.2 tls v_event_push(SCR *sp, EVENT *p_evp, const CHAR_T *p_s, size_t nitems, u_int flags)
345 1.4.8.2 tls
346 1.4.8.2 tls /* Push event. */
347 1.4.8.2 tls /* Push characters. */
348 1.4.8.2 tls /* Number of items to push. */
349 1.4.8.2 tls /* CH_* flags. */
350 1.4.8.2 tls {
351 1.4.8.2 tls EVENT *evp;
352 1.4.8.2 tls WIN *wp;
353 1.4.8.2 tls size_t total;
354 1.4.8.2 tls
355 1.4.8.2 tls /* If we have room, stuff the items into the buffer. */
356 1.4.8.2 tls wp = sp->wp;
357 1.4.8.2 tls if (nitems <= wp->i_next ||
358 1.4.8.2 tls (wp->i_event != NULL && wp->i_cnt == 0 && nitems <= wp->i_nelem)) {
359 1.4.8.2 tls if (wp->i_cnt != 0)
360 1.4.8.2 tls wp->i_next -= nitems;
361 1.4.8.2 tls goto copy;
362 1.4.8.2 tls }
363 1.4.8.2 tls
364 1.4.8.2 tls /*
365 1.4.8.2 tls * If there are currently items in the queue, shift them up,
366 1.4.8.2 tls * leaving some extra room. Get enough space plus a little
367 1.4.8.2 tls * extra.
368 1.4.8.2 tls */
369 1.4.8.2 tls #define TERM_PUSH_SHIFT 30
370 1.4.8.2 tls total = wp->i_cnt + wp->i_next + nitems + TERM_PUSH_SHIFT;
371 1.4.8.2 tls if (total >= wp->i_nelem && v_event_grow(sp, MAX(total, 64)))
372 1.4.8.2 tls return (1);
373 1.4.8.2 tls if (wp->i_cnt)
374 1.4.8.2 tls MEMMOVE(wp->i_event + TERM_PUSH_SHIFT + nitems,
375 1.4.8.2 tls wp->i_event + wp->i_next, wp->i_cnt);
376 1.4.8.2 tls wp->i_next = TERM_PUSH_SHIFT;
377 1.4.8.2 tls
378 1.4.8.2 tls /* Put the new items into the queue. */
379 1.4.8.2 tls copy: wp->i_cnt += nitems;
380 1.4.8.2 tls for (evp = wp->i_event + wp->i_next; nitems--; ++evp) {
381 1.4.8.2 tls if (p_evp != NULL)
382 1.4.8.2 tls *evp = *p_evp++;
383 1.4.8.2 tls else {
384 1.4.8.2 tls evp->e_event = E_CHARACTER;
385 1.4.8.2 tls evp->e_c = *p_s++;
386 1.4.8.2 tls evp->e_value = KEY_VAL(sp, evp->e_c);
387 1.4.8.2 tls FL_INIT(evp->e_flags, flags);
388 1.4.8.2 tls }
389 1.4.8.2 tls }
390 1.4.8.2 tls return (0);
391 1.4.8.2 tls }
392 1.4.8.2 tls
393 1.4.8.2 tls /*
394 1.4.8.2 tls * v_event_append --
395 1.4.8.2 tls * Append events onto the tail of the buffer.
396 1.4.8.2 tls */
397 1.4.8.2 tls static int
398 1.4.8.2 tls v_event_append(SCR *sp, EVENT *argp)
399 1.4.8.2 tls {
400 1.4.8.2 tls CHAR_T *s; /* Characters. */
401 1.4.8.2 tls EVENT *evp;
402 1.4.8.2 tls WIN *wp;
403 1.4.8.2 tls size_t nevents; /* Number of events. */
404 1.4.8.2 tls
405 1.4.8.2 tls /* Grow the buffer as necessary. */
406 1.4.8.2 tls nevents = argp->e_event == E_STRING ? argp->e_len : 1;
407 1.4.8.2 tls wp = sp->wp;
408 1.4.8.2 tls if (wp->i_event == NULL ||
409 1.4.8.2 tls nevents > wp->i_nelem - (wp->i_next + wp->i_cnt))
410 1.4.8.2 tls v_event_grow(sp, MAX(nevents, 64));
411 1.4.8.2 tls evp = wp->i_event + wp->i_next + wp->i_cnt;
412 1.4.8.2 tls wp->i_cnt += nevents;
413 1.4.8.2 tls
414 1.4.8.2 tls /* Transform strings of characters into single events. */
415 1.4.8.2 tls if (argp->e_event == E_STRING)
416 1.4.8.2 tls for (s = argp->e_csp; nevents--; ++evp) {
417 1.4.8.2 tls evp->e_event = E_CHARACTER;
418 1.4.8.2 tls evp->e_c = *s++;
419 1.4.8.2 tls evp->e_value = KEY_VAL(sp, evp->e_c);
420 1.4.8.2 tls evp->e_flags = 0;
421 1.4.8.2 tls }
422 1.4.8.2 tls else
423 1.4.8.2 tls *evp = *argp;
424 1.4.8.2 tls return (0);
425 1.4.8.2 tls }
426 1.4.8.2 tls
427 1.4.8.2 tls /* Remove events from the queue. */
428 1.4.8.2 tls #define QREM(len) { \
429 1.4.8.2 tls if ((wp->i_cnt -= len) == 0) \
430 1.4.8.2 tls wp->i_next = 0; \
431 1.4.8.2 tls else \
432 1.4.8.2 tls wp->i_next += len; \
433 1.4.8.2 tls }
434 1.4.8.2 tls
435 1.4.8.2 tls /*
436 1.4.8.2 tls * v_event_get --
437 1.4.8.2 tls * Return the next event.
438 1.4.8.2 tls *
439 1.4.8.2 tls * !!!
440 1.4.8.2 tls * The flag EC_NODIGIT probably needs some explanation. First, the idea of
441 1.4.8.2 tls * mapping keys is that one or more keystrokes act like a function key.
442 1.4.8.2 tls * What's going on is that vi is reading a number, and the character following
443 1.4.8.2 tls * the number may or may not be mapped (EC_MAPCOMMAND). For example, if the
444 1.4.8.2 tls * user is entering the z command, a valid command is "z40+", and we don't want
445 1.4.8.2 tls * to map the '+', i.e. if '+' is mapped to "xxx", we don't want to change it
446 1.4.8.2 tls * into "z40xxx". However, if the user enters "35x", we want to put all of the
447 1.4.8.2 tls * characters through the mapping code.
448 1.4.8.2 tls *
449 1.4.8.2 tls * Historical practice is a bit muddled here. (Surprise!) It always permitted
450 1.4.8.2 tls * mapping digits as long as they weren't the first character of the map, e.g.
451 1.4.8.2 tls * ":map ^A1 xxx" was okay. It also permitted the mapping of the digits 1-9
452 1.4.8.2 tls * (the digit 0 was a special case as it doesn't indicate the start of a count)
453 1.4.8.2 tls * as the first character of the map, but then ignored those mappings. While
454 1.4.8.2 tls * it's probably stupid to map digits, vi isn't your mother.
455 1.4.8.2 tls *
456 1.4.8.2 tls * The way this works is that the EC_MAPNODIGIT causes term_key to return the
457 1.4.8.2 tls * end-of-digit without "looking" at the next character, i.e. leaving it as the
458 1.4.8.2 tls * user entered it. Presumably, the next term_key call will tell us how the
459 1.4.8.2 tls * user wants it handled.
460 1.4.8.2 tls *
461 1.4.8.2 tls * There is one more complication. Users might map keys to digits, and, as
462 1.4.8.2 tls * it's described above, the commands:
463 1.4.8.2 tls *
464 1.4.8.2 tls * :map g 1G
465 1.4.8.2 tls * d2g
466 1.4.8.2 tls *
467 1.4.8.2 tls * would return the keys "d2<end-of-digits>1G", when the user probably wanted
468 1.4.8.2 tls * "d21<end-of-digits>G". So, if a map starts off with a digit we continue as
469 1.4.8.2 tls * before, otherwise, we pretend we haven't mapped the character, and return
470 1.4.8.2 tls * <end-of-digits>.
471 1.4.8.2 tls *
472 1.4.8.2 tls * Now that that's out of the way, let's talk about Energizer Bunny macros.
473 1.4.8.2 tls * It's easy to create macros that expand to a loop, e.g. map x 3x. It's
474 1.4.8.2 tls * fairly easy to detect this example, because it's all internal to term_key.
475 1.4.8.2 tls * If we're expanding a macro and it gets big enough, at some point we can
476 1.4.8.2 tls * assume it's looping and kill it. The examples that are tough are the ones
477 1.4.8.2 tls * where the parser is involved, e.g. map x "ayyx"byy. We do an expansion
478 1.4.8.2 tls * on 'x', and get "ayyx"byy. We then return the first 4 characters, and then
479 1.4.8.2 tls * find the looping macro again. There is no way that we can detect this
480 1.4.8.2 tls * without doing a full parse of the command, because the character that might
481 1.4.8.2 tls * cause the loop (in this case 'x') may be a literal character, e.g. the map
482 1.4.8.2 tls * map x "ayy"xyy"byy is perfectly legal and won't cause a loop.
483 1.4.8.2 tls *
484 1.4.8.2 tls * Historic vi tried to detect looping macros by disallowing obvious cases in
485 1.4.8.2 tls * the map command, maps that that ended with the same letter as they started
486 1.4.8.2 tls * (which wrongly disallowed "map x 'x"), and detecting macros that expanded
487 1.4.8.2 tls * too many times before keys were returned to the command parser. It didn't
488 1.4.8.2 tls * get many (most?) of the tricky cases right, however, and it was certainly
489 1.4.8.2 tls * possible to create macros that ran forever. And, even if it did figure out
490 1.4.8.2 tls * what was going on, the user was usually tossed into ex mode. Finally, any
491 1.4.8.2 tls * changes made before vi realized that the macro was recursing were left in
492 1.4.8.2 tls * place. We recover gracefully, but the only recourse the user has in an
493 1.4.8.2 tls * infinite macro loop is to interrupt.
494 1.4.8.2 tls *
495 1.4.8.2 tls * !!!
496 1.4.8.2 tls * It is historic practice that mapping characters to themselves as the first
497 1.4.8.2 tls * part of the mapped string was legal, and did not cause infinite loops, i.e.
498 1.4.8.2 tls * ":map! { {^M^T" and ":map n nz." were known to work. The initial, matching
499 1.4.8.2 tls * characters were returned instead of being remapped.
500 1.4.8.2 tls *
501 1.4.8.2 tls * !!!
502 1.4.8.2 tls * It is also historic practice that the macro "map ] ]]^" caused a single ]
503 1.4.8.2 tls * keypress to behave as the command ]] (the ^ got the map past the vi check
504 1.4.8.2 tls * for "tail recursion"). Conversely, the mapping "map n nn^" went recursive.
505 1.4.8.2 tls * What happened was that, in the historic vi, maps were expanded as the keys
506 1.4.8.2 tls * were retrieved, but not all at once and not centrally. So, the keypress ]
507 1.4.8.2 tls * pushed ]]^ on the stack, and then the first ] from the stack was passed to
508 1.4.8.2 tls * the ]] command code. The ]] command then retrieved a key without entering
509 1.4.8.2 tls * the mapping code. This could bite us anytime a user has a map that depends
510 1.4.8.2 tls * on secondary keys NOT being mapped. I can't see any possible way to make
511 1.4.8.2 tls * this work in here without the complete abandonment of Rationality Itself.
512 1.4.8.2 tls *
513 1.4.8.2 tls * XXX
514 1.4.8.2 tls * The final issue is recovery. It would be possible to undo all of the work
515 1.4.8.2 tls * that was done by the macro if we entered a record into the log so that we
516 1.4.8.2 tls * knew when the macro started, and, in fact, this might be worth doing at some
517 1.4.8.2 tls * point. Given that this might make the log grow unacceptably (consider that
518 1.4.8.2 tls * cursor keys are done with maps), for now we leave any changes made in place.
519 1.4.8.2 tls *
520 1.4.8.2 tls * PUBLIC: int v_event_get __P((SCR *, EVENT *, int, u_int32_t));
521 1.4.8.2 tls */
522 1.4.8.2 tls int
523 1.4.8.2 tls v_event_get(SCR *sp, EVENT *argp, int timeout, u_int32_t flags)
524 1.4.8.2 tls {
525 1.4.8.2 tls EVENT *evp, ev;
526 1.4.8.2 tls GS *gp;
527 1.4.8.2 tls SEQ *qp;
528 1.4.8.2 tls int init_nomap, ispartial, istimeout, remap_cnt;
529 1.4.8.2 tls WIN *wp;
530 1.4.8.2 tls
531 1.4.8.2 tls gp = sp->gp;
532 1.4.8.2 tls wp = sp->wp;
533 1.4.8.2 tls
534 1.4.8.2 tls /* If simply checking for interrupts, argp may be NULL. */
535 1.4.8.2 tls if (argp == NULL)
536 1.4.8.2 tls argp = &ev;
537 1.4.8.2 tls
538 1.4.8.2 tls retry: istimeout = remap_cnt = 0;
539 1.4.8.2 tls
540 1.4.8.2 tls /*
541 1.4.8.2 tls * If the queue isn't empty and we're timing out for characters,
542 1.4.8.2 tls * return immediately.
543 1.4.8.2 tls */
544 1.4.8.2 tls if (wp->i_cnt != 0 && LF_ISSET(EC_TIMEOUT))
545 1.4.8.2 tls return (0);
546 1.4.8.2 tls
547 1.4.8.2 tls /*
548 1.4.8.2 tls * If the queue is empty, we're checking for interrupts, or we're
549 1.4.8.2 tls * timing out for characters, get more events.
550 1.4.8.2 tls */
551 1.4.8.2 tls if (wp->i_cnt == 0 || LF_ISSET(EC_INTERRUPT | EC_TIMEOUT)) {
552 1.4.8.2 tls /*
553 1.4.8.2 tls * If we're reading new characters, check any scripting
554 1.4.8.2 tls * windows for input.
555 1.4.8.2 tls */
556 1.4.8.2 tls if (F_ISSET(gp, G_SCRWIN) && sscr_input(sp))
557 1.4.8.2 tls return (1);
558 1.4.8.2 tls loop: if (gp->scr_event(sp, argp,
559 1.4.8.2 tls LF_ISSET(EC_INTERRUPT | EC_QUOTED | EC_RAW), timeout))
560 1.4.8.2 tls return (1);
561 1.4.8.2 tls switch (argp->e_event) {
562 1.4.8.2 tls case E_ERR:
563 1.4.8.2 tls case E_SIGHUP:
564 1.4.8.2 tls case E_SIGTERM:
565 1.4.8.2 tls /*
566 1.4.8.2 tls * Fatal conditions cause the file to be synced to
567 1.4.8.2 tls * disk immediately.
568 1.4.8.2 tls */
569 1.4.8.2 tls v_sync(sp, RCV_ENDSESSION | RCV_PRESERVE |
570 1.4.8.2 tls (argp->e_event == E_SIGTERM ? 0: RCV_EMAIL));
571 1.4.8.2 tls return (1);
572 1.4.8.2 tls case E_TIMEOUT:
573 1.4.8.2 tls istimeout = 1;
574 1.4.8.2 tls break;
575 1.4.8.2 tls case E_INTERRUPT:
576 1.4.8.2 tls /* Set the global interrupt flag. */
577 1.4.8.2 tls F_SET(sp->gp, G_INTERRUPTED);
578 1.4.8.2 tls
579 1.4.8.2 tls /*
580 1.4.8.2 tls * If the caller was interested in interrupts, return
581 1.4.8.2 tls * immediately.
582 1.4.8.2 tls */
583 1.4.8.2 tls if (LF_ISSET(EC_INTERRUPT))
584 1.4.8.2 tls return (0);
585 1.4.8.2 tls goto append;
586 1.4.8.2 tls default:
587 1.4.8.2 tls append: if (v_event_append(sp, argp))
588 1.4.8.2 tls return (1);
589 1.4.8.2 tls break;
590 1.4.8.2 tls }
591 1.4.8.2 tls }
592 1.4.8.2 tls
593 1.4.8.2 tls /*
594 1.4.8.2 tls * If the caller was only interested in interrupts or timeouts, return
595 1.4.8.2 tls * immediately. (We may have gotten characters, and that's okay, they
596 1.4.8.2 tls * were queued up for later use.)
597 1.4.8.2 tls */
598 1.4.8.2 tls if (LF_ISSET(EC_INTERRUPT | EC_TIMEOUT))
599 1.4.8.2 tls return (0);
600 1.4.8.2 tls
601 1.4.8.2 tls newmap: evp = &wp->i_event[wp->i_next];
602 1.4.8.2 tls
603 1.4.8.2 tls /*
604 1.4.8.2 tls * If the next event in the queue isn't a character event, return
605 1.4.8.2 tls * it, we're done.
606 1.4.8.2 tls */
607 1.4.8.2 tls if (evp->e_event != E_CHARACTER) {
608 1.4.8.2 tls *argp = *evp;
609 1.4.8.2 tls QREM(1);
610 1.4.8.2 tls return (0);
611 1.4.8.2 tls }
612 1.4.8.2 tls
613 1.4.8.2 tls /*
614 1.4.8.2 tls * If the key isn't mappable because:
615 1.4.8.2 tls *
616 1.4.8.2 tls * + ... the timeout has expired
617 1.4.8.2 tls * + ... it's not a mappable key
618 1.4.8.2 tls * + ... neither the command or input map flags are set
619 1.4.8.2 tls * + ... there are no maps that can apply to it
620 1.4.8.2 tls *
621 1.4.8.2 tls * return it forthwith.
622 1.4.8.2 tls */
623 1.4.8.2 tls if (istimeout || FL_ISSET(evp->e_flags, CH_NOMAP) ||
624 1.4.8.2 tls !LF_ISSET(EC_MAPCOMMAND | EC_MAPINPUT) ||
625 1.4.8.2 tls ((evp->e_c & ~MAX_BIT_SEQ) == 0 &&
626 1.4.8.2 tls !bit_test(gp->seqb, evp->e_c)))
627 1.4.8.2 tls goto nomap;
628 1.4.8.2 tls
629 1.4.8.2 tls /* Search the map. */
630 1.4.8.2 tls qp = seq_find(sp, NULL, evp, NULL, wp->i_cnt,
631 1.4.8.2 tls LF_ISSET(EC_MAPCOMMAND) ? SEQ_COMMAND : SEQ_INPUT, &ispartial);
632 1.4.8.2 tls
633 1.4.8.2 tls /*
634 1.4.8.2 tls * If get a partial match, get more characters and retry the map.
635 1.4.8.2 tls * If time out without further characters, return the characters
636 1.4.8.2 tls * unmapped.
637 1.4.8.2 tls *
638 1.4.8.2 tls * !!!
639 1.4.8.2 tls * <escape> characters are a problem. Cursor keys start with <escape>
640 1.4.8.2 tls * characters, so there's almost always a map in place that begins with
641 1.4.8.2 tls * an <escape> character. If we timeout <escape> keys in the same way
642 1.4.8.2 tls * that we timeout other keys, the user will get a noticeable pause as
643 1.4.8.2 tls * they enter <escape> to terminate input mode. If key timeout is set
644 1.4.8.2 tls * for a slow link, users will get an even longer pause. Nvi used to
645 1.4.8.2 tls * simply timeout <escape> characters at 1/10th of a second, but this
646 1.4.8.2 tls * loses over PPP links where the latency is greater than 100Ms.
647 1.4.8.2 tls */
648 1.4.8.2 tls if (ispartial) {
649 1.4.8.2 tls if (O_ISSET(sp, O_TIMEOUT))
650 1.4.8.2 tls timeout = (evp->e_value == K_ESCAPE ?
651 1.4.8.2 tls O_VAL(sp, O_ESCAPETIME) :
652 1.4.8.2 tls O_VAL(sp, O_KEYTIME)) * 100;
653 1.4.8.2 tls else
654 1.4.8.2 tls timeout = 0;
655 1.4.8.2 tls goto loop;
656 1.4.8.2 tls }
657 1.4.8.2 tls
658 1.4.8.2 tls /* If no map, return the character. */
659 1.4.8.2 tls if (qp == NULL) {
660 1.4.8.2 tls nomap: if (!ISDIGIT(evp->e_c) && LF_ISSET(EC_MAPNODIGIT))
661 1.4.8.2 tls goto not_digit;
662 1.4.8.2 tls *argp = *evp;
663 1.4.8.2 tls QREM(1);
664 1.4.8.2 tls return (0);
665 1.4.8.2 tls }
666 1.4.8.2 tls
667 1.4.8.2 tls /*
668 1.4.8.2 tls * If looking for the end of a digit string, and the first character
669 1.4.8.2 tls * of the map is it, pretend we haven't seen the character.
670 1.4.8.2 tls */
671 1.4.8.2 tls if (LF_ISSET(EC_MAPNODIGIT) &&
672 1.4.8.2 tls qp->output != NULL && !ISDIGIT(qp->output[0])) {
673 1.4.8.2 tls not_digit: argp->e_c = CH_NOT_DIGIT;
674 1.4.8.2 tls argp->e_value = K_NOTUSED;
675 1.4.8.2 tls argp->e_event = E_CHARACTER;
676 1.4.8.2 tls FL_INIT(argp->e_flags, 0);
677 1.4.8.2 tls return (0);
678 1.4.8.2 tls }
679 1.4.8.2 tls
680 1.4.8.2 tls /* Find out if the initial segments are identical. */
681 1.4.8.2 tls init_nomap = !e_memcmp(qp->output, &wp->i_event[wp->i_next], qp->ilen);
682 1.4.8.2 tls
683 1.4.8.2 tls /* Delete the mapped characters from the queue. */
684 1.4.8.2 tls QREM(qp->ilen);
685 1.4.8.2 tls
686 1.4.8.2 tls /* If keys mapped to nothing, go get more. */
687 1.4.8.2 tls if (qp->output == NULL)
688 1.4.8.2 tls goto retry;
689 1.4.8.2 tls
690 1.4.8.2 tls /* If remapping characters... */
691 1.4.8.2 tls if (O_ISSET(sp, O_REMAP)) {
692 1.4.8.2 tls /*
693 1.4.8.2 tls * Periodically check for interrupts. Always check the first
694 1.4.8.2 tls * time through, because it's possible to set up a map that
695 1.4.8.2 tls * will return a character every time, but will expand to more,
696 1.4.8.2 tls * e.g. "map! a aaaa" will always return a 'a', but we'll never
697 1.4.8.2 tls * get anywhere useful.
698 1.4.8.2 tls */
699 1.4.8.2 tls if ((++remap_cnt == 1 || remap_cnt % 10 == 0) &&
700 1.4.8.2 tls (gp->scr_event(sp, &ev,
701 1.4.8.2 tls EC_INTERRUPT, 0) || ev.e_event == E_INTERRUPT)) {
702 1.4.8.2 tls F_SET(sp->gp, G_INTERRUPTED);
703 1.4.8.2 tls argp->e_event = E_INTERRUPT;
704 1.4.8.2 tls return (0);
705 1.4.8.2 tls }
706 1.4.8.2 tls
707 1.4.8.2 tls /*
708 1.4.8.2 tls * If an initial part of the characters mapped, they are not
709 1.4.8.2 tls * further remapped -- return the first one. Push the rest
710 1.4.8.2 tls * of the characters, or all of the characters if no initial
711 1.4.8.2 tls * part mapped, back on the queue.
712 1.4.8.2 tls */
713 1.4.8.2 tls if (init_nomap) {
714 1.4.8.2 tls if (v_event_push(sp, NULL, qp->output + qp->ilen,
715 1.4.8.2 tls qp->olen - qp->ilen, CH_MAPPED))
716 1.4.8.2 tls return (1);
717 1.4.8.2 tls if (v_event_push(sp, NULL,
718 1.4.8.2 tls qp->output, qp->ilen, CH_NOMAP | CH_MAPPED))
719 1.4.8.2 tls return (1);
720 1.4.8.2 tls evp = &wp->i_event[wp->i_next];
721 1.4.8.2 tls goto nomap;
722 1.4.8.2 tls }
723 1.4.8.2 tls if (v_event_push(sp, NULL, qp->output, qp->olen, CH_MAPPED))
724 1.4.8.2 tls return (1);
725 1.4.8.2 tls goto newmap;
726 1.4.8.2 tls }
727 1.4.8.2 tls
728 1.4.8.2 tls /* Else, push the characters on the queue and return one. */
729 1.4.8.2 tls if (v_event_push(sp, NULL, qp->output, qp->olen, CH_MAPPED | CH_NOMAP))
730 1.4.8.2 tls return (1);
731 1.4.8.2 tls
732 1.4.8.2 tls goto nomap;
733 1.4.8.2 tls }
734 1.4.8.2 tls
735 1.4.8.2 tls /*
736 1.4.8.2 tls * v_sync --
737 1.4.8.2 tls * Walk the screen lists, sync'ing files to their backup copies.
738 1.4.8.2 tls */
739 1.4.8.2 tls static void
740 1.4.8.2 tls v_sync(SCR *sp, int flags)
741 1.4.8.2 tls {
742 1.4.8.2 tls GS *gp;
743 1.4.8.2 tls WIN *wp;
744 1.4.8.2 tls
745 1.4.8.2 tls gp = sp->gp;
746 1.4.8.2 tls TAILQ_FOREACH(wp, &gp->dq, q)
747 1.4.8.2 tls TAILQ_FOREACH(sp, &wp->scrq, q)
748 1.4.8.2 tls rcv_sync(sp, flags);
749 1.4.8.2 tls TAILQ_FOREACH(sp, &gp->hq, q)
750 1.4.8.2 tls rcv_sync(sp, flags);
751 1.4.8.2 tls }
752 1.4.8.2 tls
753 1.4.8.2 tls /*
754 1.4.8.2 tls * v_event_err --
755 1.4.8.2 tls * Unexpected event.
756 1.4.8.2 tls *
757 1.4.8.2 tls * PUBLIC: void v_event_err __P((SCR *, EVENT *));
758 1.4.8.2 tls */
759 1.4.8.2 tls void
760 1.4.8.2 tls v_event_err(SCR *sp, EVENT *evp)
761 1.4.8.2 tls {
762 1.4.8.2 tls switch (evp->e_event) {
763 1.4.8.2 tls case E_CHARACTER:
764 1.4.8.2 tls msgq(sp, M_ERR, "276|Unexpected character event");
765 1.4.8.2 tls break;
766 1.4.8.2 tls case E_EOF:
767 1.4.8.2 tls msgq(sp, M_ERR, "277|Unexpected end-of-file event");
768 1.4.8.2 tls break;
769 1.4.8.2 tls case E_INTERRUPT:
770 1.4.8.2 tls msgq(sp, M_ERR, "279|Unexpected interrupt event");
771 1.4.8.2 tls break;
772 1.4.8.2 tls case E_IPCOMMAND:
773 1.4.8.2 tls msgq(sp, M_ERR, "318|Unexpected command or input");
774 1.4.8.2 tls break;
775 1.4.8.2 tls case E_REPAINT:
776 1.4.8.2 tls msgq(sp, M_ERR, "281|Unexpected repaint event");
777 1.4.8.2 tls break;
778 1.4.8.2 tls case E_STRING:
779 1.4.8.2 tls msgq(sp, M_ERR, "285|Unexpected string event");
780 1.4.8.2 tls break;
781 1.4.8.2 tls case E_TIMEOUT:
782 1.4.8.2 tls msgq(sp, M_ERR, "286|Unexpected timeout event");
783 1.4.8.2 tls break;
784 1.4.8.2 tls case E_WRESIZE:
785 1.4.8.2 tls msgq(sp, M_ERR, "316|Unexpected resize event");
786 1.4.8.2 tls break;
787 1.4.8.2 tls
788 1.4.8.2 tls /*
789 1.4.8.2 tls * Theoretically, none of these can occur, as they're handled at the
790 1.4.8.2 tls * top editor level.
791 1.4.8.2 tls */
792 1.4.8.2 tls case E_ERR:
793 1.4.8.2 tls case E_SIGHUP:
794 1.4.8.2 tls case E_SIGTERM:
795 1.4.8.2 tls default:
796 1.4.8.2 tls abort();
797 1.4.8.2 tls }
798 1.4.8.2 tls }
799 1.4.8.2 tls
800 1.4.8.2 tls /*
801 1.4.8.2 tls * v_event_flush --
802 1.4.8.2 tls * Flush any flagged keys, returning if any keys were flushed.
803 1.4.8.2 tls *
804 1.4.8.2 tls * PUBLIC: int v_event_flush __P((SCR *, u_int));
805 1.4.8.2 tls */
806 1.4.8.2 tls int
807 1.4.8.2 tls v_event_flush(SCR *sp, u_int flags)
808 1.4.8.2 tls {
809 1.4.8.2 tls WIN *wp;
810 1.4.8.2 tls int rval;
811 1.4.8.2 tls
812 1.4.8.2 tls for (rval = 0, wp = sp->wp; wp->i_cnt != 0 &&
813 1.4.8.2 tls FL_ISSET(wp->i_event[wp->i_next].e_flags, flags); rval = 1)
814 1.4.8.2 tls QREM(1);
815 1.4.8.2 tls return (rval);
816 1.4.8.2 tls }
817 1.4.8.2 tls
818 1.4.8.2 tls /*
819 1.4.8.2 tls * v_event_grow --
820 1.4.8.2 tls * Grow the terminal queue.
821 1.4.8.2 tls */
822 1.4.8.2 tls static int
823 1.4.8.2 tls v_event_grow(SCR *sp, int add)
824 1.4.8.2 tls {
825 1.4.8.2 tls WIN *wp;
826 1.4.8.2 tls size_t new_nelem, olen;
827 1.4.8.2 tls
828 1.4.8.2 tls wp = sp->wp;
829 1.4.8.2 tls new_nelem = wp->i_nelem + add;
830 1.4.8.2 tls olen = wp->i_nelem * sizeof(wp->i_event[0]);
831 1.4.8.2 tls BINC_RET(sp, EVENT, wp->i_event, olen, new_nelem * sizeof(EVENT));
832 1.4.8.2 tls wp->i_nelem = olen / sizeof(wp->i_event[0]);
833 1.4.8.2 tls return (0);
834 1.4.8.2 tls }
835 1.4.8.2 tls
836 1.4.8.2 tls /*
837 1.4.8.2 tls * v_key_cmp --
838 1.4.8.2 tls * Compare two keys for sorting.
839 1.4.8.2 tls */
840 1.4.8.2 tls static int
841 1.4.8.2 tls v_key_cmp(const void *ap, const void *bp)
842 1.4.8.2 tls {
843 1.4.8.2 tls return (((const KEYLIST *)ap)->ch - ((const KEYLIST *)bp)->ch);
844 1.4.8.2 tls }
845