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