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