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