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