add K_VINTR/VINTR, <interrupt> stops input on the command lien
[nvi.git] / common / key.c
blobda4b3ae6e0a87e39e2642008cab17acbe69ea506
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: key.c,v 8.38 1993/12/29 17:26:44 bostic Exp $ (Berkeley) $Date: 1993/12/29 17:26:44 $";
10 #endif /* not lint */
12 #include <sys/types.h>
13 #include <sys/time.h>
15 #include <ctype.h>
16 #include <curses.h>
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
22 #include "vi.h"
23 #include "seq.h"
25 static int keycmp __P((const void *, const void *));
28 * If we're reading less than 20 characters, up the size of the tty buffer.
29 * This shouldn't ever happen, other than the first time through, but it's
30 * possible if a map is large enough.
32 #define term_read_grow(sp, tty) \
33 (tty)->len - (tty)->cnt >= 20 ? 0 : __term_read_grow(sp, tty)
34 static int __term_read_grow __P((SCR *, IBUF *));
37 * XXX
38 * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
40 typedef struct _tklist {
41 char *ts; /* Key's termcap string. */
42 char *output; /* Corresponding vi command. */
43 char *name; /* Name. */
44 } TKLIST;
45 static TKLIST const tklist[] = {
46 {"kA", "O", "insert line"},
47 {"kD", "x", "delete character"},
48 {"kd", "j", "cursor down"},
49 {"kE", "D", "delete to eol"},
50 {"kF", "\004", "scroll down"},
51 {"kH", "$", "go to eol"},
52 {"kh", "^", "go to sol"},
53 {"kI", "i", "insert at cursor"},
54 {"kL", "dd", "delete line"},
55 {"kl", "h", "cursor left"},
56 {"kN", "\006", "page down"},
57 {"kP", "\002", "page up"},
58 {"kR", "\025", "scroll up"},
59 {"kS", "dG", "delete to end of screen"},
60 {"kr", "l", "cursor right"},
61 {"ku", "k", "cursor up"},
62 {NULL},
66 * XXX
67 * THIS REQUIRES THAT ALL SCREENS SHARE A SPECIAL KEY SET.
69 typedef struct _keylist {
70 u_char value; /* Special value. */
71 CHAR_T ch; /* Key. */
72 } KEYLIST;
73 static KEYLIST keylist[] = {
74 {K_CARAT, '^'},
75 {K_CNTRLR, '\022'},
76 {K_CNTRLT, '\024'},
77 {K_CNTRLZ, '\032'},
78 {K_COLON, ':'},
79 {K_CR, '\r'},
80 {K_ESCAPE, '\033'},
81 {K_FORMFEED, '\f'},
82 {K_NL, '\n'},
83 {K_RIGHTBRACE, '}'},
84 {K_RIGHTPAREN, ')'},
85 {K_TAB, '\t'},
86 {K_VEOF, '\004'},
87 {K_VERASE, '\b'},
88 {K_VINTR, '\003'},
89 {K_VKILL, '\025'},
90 {K_VLNEXT, '\026'},
91 {K_VWERASE, '\027'},
92 {K_ZERO, '0'},
96 * term_init --
97 * Initialize the special key lookup table, and the special keys
98 * defined by the terminal's termcap entry.
101 term_init(sp)
102 SCR *sp;
104 extern CHNAME const asciiname[]; /* XXX */
105 GS *gp;
106 KEYLIST *kp;
107 TKLIST const *tkp;
108 cc_t ch;
109 int cnt;
110 char *sbp, *t, buf[2 * 1024], sbuf[128];
113 * XXX
114 * 8-bit, ASCII only, for now. Recompilation should get you
115 * any 8-bit character set, as long as nul isn't a character.
117 gp = sp->gp;
118 gp->cname = asciiname; /* XXX */
120 /* Set keys found in the termios structure. */
121 #define TERMSET(name, val) { \
122 if ((ch = gp->original_termios.c_cc[name]) != _POSIX_VDISABLE) \
123 for (kp = keylist;; ++kp) \
124 if (kp->value == (val)) { \
125 kp->ch = ch; \
126 break; \
130 * VEOF, VERASE, VKILL are required by POSIX 1003.1-1990,
131 * VWERASE is a 4.4BSD extension.
133 #ifdef VEOF
134 TERMSET(VEOF, K_VEOF);
135 #endif
136 #ifdef VERASE
137 TERMSET(VERASE, K_VERASE);
138 #endif
139 #ifdef VINTR
140 TERMSET(VINTR, K_VINTR);
141 #endif
142 #ifdef VKILL
143 TERMSET(VKILL, K_VKILL);
144 #endif
145 #ifdef VWERASE
146 TERMSET(VWERASE, K_VWERASE);
147 #endif
149 /* Sort the special key list. */
150 qsort(keylist,
151 sizeof(keylist) / sizeof(keylist[0]), sizeof(keylist[0]), keycmp);
153 /* Initialize the fast lookup table. */
154 CALLOC_RET(sp,
155 gp->special_key, u_char *, MAX_FAST_KEY + 1, sizeof(u_char));
156 for (gp->max_special = 0, kp = keylist,
157 cnt = sizeof(keylist) / sizeof(keylist[0]); cnt--; ++kp) {
158 if (gp->max_special < kp->value)
159 gp->max_special = kp->value;
160 if (kp->ch <= MAX_FAST_KEY)
161 gp->special_key[kp->ch] = kp->value;
164 /* Set key sequences found in the termcap entry. */
165 switch (tgetent(buf, O_STR(sp, O_TERM))) {
166 case -1:
167 msgq(sp, M_ERR,
168 "tgetent: %s: %s.", O_STR(sp, O_TERM), strerror(errno));
169 return (0);
170 case 0:
171 msgq(sp, M_ERR,
172 "%s: unknown terminal type.", O_STR(sp, O_TERM));
173 return (0);
176 for (tkp = tklist; tkp->name != NULL; ++tkp) {
177 sbp = sbuf;
178 if ((t = tgetstr(tkp->ts, &sbp)) == NULL)
179 continue;
180 if (seq_set(sp, tkp->name, strlen(tkp->name), t, strlen(t),
181 tkp->output, strlen(tkp->output), SEQ_COMMAND, 0))
182 return (1);
184 return (0);
188 * term_push --
189 * Push keys onto the front of a buffer.
191 * There is a single input buffer in ex/vi. Characters are read onto the
192 * end of the buffer by the terminal input routines, and pushed onto the
193 * front of the buffer various other functions in ex/vi. Each key has an
194 * associated flag value, which indicates if it has already been quoted,
195 * if it is the result of a mapping or an abbreviation.
198 term_push(sp, s, len, cmap, flags)
199 SCR *sp;
200 CHAR_T *s; /* Characters. */
201 size_t len; /* Number of chars. */
202 u_int cmap; /* Map count. */
203 u_int flags; /* CH_* flags. */
205 IBUF *tty;
206 size_t nlen;
208 /* If we have room, stuff the keys into the buffer. */
209 tty = sp->gp->tty;
210 if (len <= tty->next ||
211 (tty->ch != NULL && tty->cnt == 0 && len <= tty->len)) {
212 if (tty->cnt != 0)
213 tty->next -= len;
214 tty->cnt += len;
215 memmove(tty->ch + tty->next, s, len * sizeof(CHAR_T));
216 memset(tty->chf + tty->next, flags, len);
217 memset(tty->cmap + tty->next, cmap, len);
218 return (0);
221 /* Get enough space plus a little extra. */
222 nlen = tty->cnt + len;
223 if (nlen > tty->len) {
224 size_t olen;
226 nlen += 64;
227 olen = tty->len;
228 BINC_RET(sp, tty->ch, olen, nlen * sizeof(tty->ch[0]));
229 olen = tty->len;
230 BINC_RET(sp, tty->chf, olen, nlen * sizeof(tty->chf[0]));
231 BINC_RET(sp, tty->cmap, tty->len, nlen * sizeof(tty->cmap[0]));
235 * If there are currently characters in the queue, shift them up,
236 * leaving some extra room.
238 #define TERM_PUSH_SHIFT 30
239 if (tty->cnt) {
240 memmove(tty->ch + TERM_PUSH_SHIFT + len,
241 tty->ch + tty->next, tty->cnt * sizeof(tty->ch[0]));
242 memmove(tty->chf + TERM_PUSH_SHIFT + len,
243 tty->chf + tty->next, tty->cnt * sizeof(tty->chf[0]));
244 memmove(tty->cmap + TERM_PUSH_SHIFT + len,
245 tty->cmap + tty->next, tty->cnt * sizeof(tty->cmap[0]));
248 /* Put the new characters into the queue. */
249 tty->next = TERM_PUSH_SHIFT;
250 tty->cnt += len;
251 memmove(tty->ch + TERM_PUSH_SHIFT, s, len * sizeof(tty->ch[0]));
252 memset(tty->chf + TERM_PUSH_SHIFT, flags, len * sizeof(tty->chf[0]));
253 memset(tty->cmap + TERM_PUSH_SHIFT, cmap, len * sizeof(tty->cmap[0]));
254 return (0);
258 * Remove characters from the queue, simultaneously clearing the
259 * flag and map counts.
261 #define QREM_HEAD(q, len) { \
262 size_t __off = (q)->next; \
263 if (len == 1) { \
264 tty->chf[__off] = 0; \
265 tty->cmap[__off] = 0; \
266 } else { \
267 memset(tty->chf + __off, 0, len); \
268 memset(tty->cmap + __off, 0, len); \
270 if (((q)->cnt -= len) == 0) \
271 (q)->next = 0; \
272 else \
273 (q)->next += len; \
275 #define QREM_TAIL(q, len) { \
276 size_t __off = (q)->next + (q)->cnt - 1; \
277 if (len == 1) { \
278 tty->chf[__off] = 0; \
279 tty->cmap[__off] = 0; \
280 } else { \
281 memset(tty->chf + __off, 0, len); \
282 memset(tty->cmap + __off, 0, len); \
284 if (((q)->cnt -= len) == 0) \
285 (q)->next = 0; \
289 * term_key --
290 * Get the next key.
292 * !!!
293 * The flag TXT_MAPNODIGIT probably needs some explanation. First, the idea
294 * of mapping keys is that one or more keystrokes act like a function key.
295 * What's going on is that vi is reading a number, and the character following
296 * the number may or may not be mapped (TXT_MAPCOMMAND). For example, if the
297 * user is entering the z command, a valid command is "z40+", and we don't want
298 * to map the '+', i.e. if '+' is mapped to "xxx", we don't want to change it
299 * into "z40xxx". However, if the user enters "35x", we want to put all of the
300 * characters through the mapping code.
302 * Historical practice is a bit muddled here. (Surprise!) It always permitted
303 * mapping digits as long as they weren't the first character of the map, e.g.
304 * ":map ^A1 xxx" was okay. It also permitted the mapping of the digits 1-9
305 * (the digit 0 was a special case as it doesn't indicate the start of a count)
306 * as the first character of the map, but then ignored those mappings. While
307 * it's probably stupid to map digits, vi isn't your mother.
309 * The way this works is that the TXT_MAPNODIGIT causes term_key to return the
310 * end-of-digit without "looking" at the next character, i.e. leaving it as the
311 * user entered it. Presumably, the next term_key call will tell us how the
312 * user wants it handled.
314 * There is one more complication. Users might map keys to digits, and, as
315 * it's described above, the commands "map g 1G|d2g" would return the keys
316 * "d2<end-of-digits>1G", when the user probably wanted "d21<end-of-digits>G".
317 * So, if a map starts off with a digit we continue as before, otherwise, we
318 * pretend that we haven't mapped the character and return <end-of-digits>.
320 * Now that that's out of the way, let's talk about Energizer Bunny macros.
321 * It's easy to create macros that expand to a loop, e.g. map x 3x. It's
322 * fairly easy to detect this example, because it's all internal to term_key.
323 * If we're expanding a macro and it gets big enough, at some point we can
324 * assume it's looping and kill it. The examples that are tough are the ones
325 * where the parser is involved, e.g. map x "ayyx"byy. We do an expansion
326 * on 'x', and get "ayyx"byy. We then return the first 4 characters, and then
327 * find the looping macro again. There is no way that we can detect this
328 * without doing a full parse of the command, because the character that might
329 * cause the loop (in this case 'x') may be a literal character, e.g. the map
330 * map x "ayy"xyy"byy is perfectly legal and won't cause a loop.
332 * Historic vi tried to detect looping macros by disallowing obvious cases in
333 * the map command, maps that that ended with the same letter as they started
334 * (which wrongly disallowed "map x 'x"), and detecting macros that expanded
335 * too many times before keys were returned to the command parser. It didn't
336 * get many (most?) of the tricky cases right, however, and it was certainly
337 * possible to create macros that ran forever. And, even if it did figure out
338 * what was going on, the user was usually tossed into ex mode. Finally, any
339 * changes made before vi realized that the macro was recursing were left in
340 * place. This implementation counts how many times each input character has
341 * been mapped. If it reaches some arbitrary value, we flush all mapped keys
342 * and return an error.
344 * XXX
345 * The final issue is recovery. It would be possible to undo all of the work
346 * that was done by the macro if we entered a record into the log so that we
347 * knew when the macro started, and, in fact, this might be worth doing at some
348 * point. Given that this might make the log grow unacceptably (consider that
349 * cursor keys are done with maps), for now we leave any changes made in place.
351 enum input
352 term_key(sp, chp, flags)
353 SCR *sp;
354 CH *chp;
355 u_int flags;
357 enum input rval;
358 struct timeval t, *tp;
359 CHAR_T ch;
360 GS *gp;
361 IBUF *tty;
362 SEQ *qp;
363 int cmap, ispartial, nr;
365 gp = sp->gp;
366 tty = gp->tty;
369 * If the queue is empty, read more keys in. Since no timeout is
370 * requested, s_key_read will either return an error or will read
371 * some number of characters.
373 loop: if (tty->cnt == 0) {
374 if (term_read_grow(sp, tty))
375 return (INP_ERR);
376 if (rval = sp->s_key_read(sp, &nr, NULL))
377 return (rval);
379 * If there's something on the mode line that we wanted
380 * the user to see, they just entered a character so we
381 * can presume they saw it.
383 if (F_ISSET(sp, S_UPDATE_MODE))
384 F_CLR(sp, S_UPDATE_MODE);
387 /* If the key is mappable and should be mapped, look it up. */
388 if (!(tty->chf[tty->next] & CH_NOMAP) &&
389 LF_ISSET(TXT_MAPCOMMAND | TXT_MAPINPUT)) {
390 /* Set up timeout value. */
391 if (O_ISSET(sp, O_TIMEOUT)) {
392 tp = &t;
393 t.tv_sec = O_VAL(sp, O_KEYTIME) / 10;
394 t.tv_usec = (O_VAL(sp, O_KEYTIME) % 10) * 100000L;
395 } else
396 tp = NULL;
398 /* Get the next key. */
399 newmap: ch = tty->ch[tty->next];
400 if (ch < MAX_BIT_SEQ && !bit_test(gp->seqb, ch))
401 goto nomap;
403 /* Search the map. */
404 remap: qp = seq_find(sp, NULL, &tty->ch[tty->next], tty->cnt,
405 LF_ISSET(TXT_MAPCOMMAND) ? SEQ_COMMAND : SEQ_INPUT,
406 &ispartial);
409 * If get a partial match, read more characters and retry
410 * the map. If no characters read, return the characters
411 * unmapped.
413 if (ispartial) {
414 if (term_read_grow(sp, tty))
415 return (INP_ERR);
416 if (rval = sp->s_key_read(sp, &nr, tp))
417 return (rval);
418 if (nr)
419 goto remap;
420 goto nomap;
423 /* If no map, return the character. */
424 if (qp == NULL)
425 goto nomap;
428 * If looking for the end of a digit string, and the first
429 * character of the map is it, pretend we haven't seen the
430 * character.
432 if (LF_ISSET(TXT_MAPNODIGIT) && !isdigit(qp->output[0]))
433 goto not_digit_ch;
436 * Only permit a character to be remapped a certain number
437 * of times before we figure that it's not going to finish.
439 if ((cmap = tty->cmap[tty->next]) > MAX_MAP_COUNT) {
440 term_map_flush(sp, "Character remapped too many times");
441 return (INP_ERR);
444 /* Delete the mapped characters from the queue. */
445 QREM_HEAD(tty, qp->ilen);
447 /* If remapping characters, push the character on the queue. */
448 if (O_ISSET(sp, O_REMAP)) {
449 if (term_push(sp, qp->output, qp->olen, ++cmap, 0))
450 return (INP_ERR);
451 goto newmap;
454 /* Else, push the characters on the queue and return one. */
455 if (term_push(sp, qp->output, qp->olen, 0, CH_NOMAP))
456 return (INP_ERR);
459 nomap: ch = tty->ch[tty->next];
460 if (LF_ISSET(TXT_MAPNODIGIT) && !isdigit(ch)) {
461 not_digit_ch: chp->ch = NOT_DIGIT_CH;
462 chp->value = 0;
463 chp->flags = 0;
464 return (INP_OK);
467 /* Fill in the return information. */
468 chp->ch = ch;
469 chp->flags = tty->chf[tty->next];
470 chp->value = term_key_val(sp, ch);
472 /* Delete the character from the queue. */
473 QREM_HEAD(tty, 1);
476 * O_BEAUTIFY eliminates all control characters except
477 * escape, form-feed, newline and tab.
479 if (isprint(ch) ||
480 !LF_ISSET(TXT_BEAUTIFY) || !O_ISSET(sp, O_BEAUTIFY) ||
481 chp->value == K_ESCAPE || chp->value == K_FORMFEED ||
482 chp->value == K_NL || chp->value == K_TAB)
483 return (INP_OK);
485 goto loop;
489 * term_ab_flush --
490 * Flush any abbreviated keys.
492 void
493 term_ab_flush(sp, msg)
494 SCR *sp;
495 char *msg;
497 IBUF *tty;
499 tty = sp->gp->tty;
500 if (!tty->cnt || !(tty->chf[tty->next] & CH_ABBREVIATED))
501 return;
502 do {
503 QREM_HEAD(tty, 1);
504 } while (tty->cnt && tty->chf[tty->next] & CH_ABBREVIATED);
505 msgq(sp, M_ERR, "%s: keys flushed.", msg);
509 * term_map_flush --
510 * Flush any mapped keys.
512 void
513 term_map_flush(sp, msg)
514 SCR *sp;
515 char *msg;
517 IBUF *tty;
519 tty = sp->gp->tty;
520 if (!tty->cnt || !tty->cmap[tty->next])
521 return;
522 do {
523 QREM_HEAD(tty, 1);
524 } while (tty->cnt && tty->cmap[tty->next]);
525 msgq(sp, M_ERR, "%s: keys flushed.", msg);
530 * term_user_key --
531 * Get the next key, but require the user enter one.
533 enum input
534 term_user_key(sp, chp)
535 SCR *sp;
536 CH *chp;
538 enum input rval;
539 struct timeval t;
540 IBUF *tty;
541 int nr;
544 * Read any keys the user has waiting. Make the race
545 * condition as short as possible.
547 t.tv_sec = 0;
548 t.tv_usec = 0;
549 for (tty = sp->gp->tty;;) {
550 if (term_read_grow(sp, tty))
551 return (INP_ERR);
552 if (rval = sp->s_key_read(sp, &nr, &t))
553 return (rval);
554 if (nr == 0)
555 break;
558 /* Read another key. */
559 if (rval = sp->s_key_read(sp, &nr, NULL))
560 return (rval);
562 /* Fill in the return information. */
563 chp->ch = tty->ch[tty->next + (tty->cnt - 1)];
564 chp->flags = 0;
565 chp->value = term_key_val(sp, chp->ch);
567 QREM_TAIL(tty, 1);
568 return (INP_OK);
572 * term_key_ch --
573 * Fill in the key for a value.
576 term_key_ch(sp, val, chp)
577 SCR *sp;
578 int val;
579 CHAR_T *chp;
581 KEYLIST *kp;
583 for (kp = keylist;; ++kp)
584 if (kp->value == val) {
585 *chp = kp->ch;
586 return (0);
588 return (1);
592 * __term_key_val --
593 * Fill in the value for a key. This routine is the backup
594 * for the term_key_val() macro.
597 __term_key_val(sp, ch)
598 SCR *sp;
599 ARG_CHAR_T ch;
601 KEYLIST k, *kp;
603 k.ch = ch;
604 kp = bsearch(&k, keylist,
605 sizeof(keylist) / sizeof(keylist[0]), sizeof(keylist[0]), keycmp);
606 return (kp == NULL ? 0 : kp->value);
610 * __term_read_grow --
611 * Grow the terminal queue. This routine is the backup for
612 * the term_read_grow() macro.
614 static int
615 __term_read_grow(sp, tty)
616 SCR *sp;
617 IBUF *tty;
619 size_t alen, len, nlen;
621 nlen = tty->len + 64;
622 alen = tty->len - (tty->next + tty->cnt);
624 len = tty->len;
625 BINC_RET(sp, tty->ch, len, nlen * sizeof(tty->ch[0]));
626 memset(tty->ch + tty->next + tty->cnt, 0, alen * sizeof(tty->ch[0]));
628 len = tty->len;
629 BINC_RET(sp, tty->chf, len, nlen * sizeof(tty->chf[0]));
630 memset(tty->chf + tty->next + tty->cnt, 0, alen * sizeof(tty->chf[0]));
632 BINC_RET(sp, tty->cmap, tty->len, nlen * sizeof(tty->cmap[0]));
633 memset(tty->cmap +
634 tty->next + tty->cnt, 0, alen * sizeof(tty->cmap[0]));
635 return (0);
638 static int
639 keycmp(ap, bp)
640 const void *ap, *bp;
642 return (((KEYLIST *)ap)->ch - ((KEYLIST *)bp)->ch);