1/* 2 * Copyright (C) 1984-2007 Mark Nudelman 3 * 4 * You may distribute under the terms of either the GNU General Public 5 * License or the Less License, as specified in the README file. 6 * 7 * For more information about less, or for information on how to 8 * contact the author, see the README file. 9 */ 10 11 12/* 13 * Routines to decode user commands. 14 * 15 * This is all table driven. 16 * A command table is a sequence of command descriptors. 17 * Each command descriptor is a sequence of bytes with the following format: 18 * <c1><c2>...<cN><0><action> 19 * The characters c1,c2,...,cN are the command string; that is, 20 * the characters which the user must type. 21 * It is terminated by a null <0> byte. 22 * The byte after the null byte is the action code associated 23 * with the command string. 24 * If an action byte is OR-ed with A_EXTRA, this indicates 25 * that the option byte is followed by an extra string. 26 * 27 * There may be many command tables. 28 * The first (default) table is built-in. 29 * Other tables are read in from "lesskey" files. 30 * All the tables are linked together and are searched in order. 31 */ 32 33#include "less.h" 34#include "cmd.h" 35#ifdef __APPLE__ 36#include "get_compat.h" 37#else 38#define COMPAT_MODE(func, mode) 1 39#endif 40 41extern int erase_char, erase2_char, kill_char; 42extern int secure; 43 44#define SK(k) \ 45 SK_SPECIAL_KEY, (k), 6, 1, 1, 1 46/* 47 * Command table is ordered roughly according to expected 48 * frequency of use, so the common commands are near the beginning. 49 */ 50 51static unsigned char cmdtable[] = 52{ 53 '\r',0, A_F_LINE, 54 '\n',0, A_F_LINE, 55 'e',0, A_F_LINE, 56 'j',0, A_F_LINE, 57 SK(SK_DOWN_ARROW),0, A_F_LINE, 58 CONTROL('E'),0, A_F_LINE, 59 CONTROL('N'),0, A_F_LINE, 60 'k',0, A_B_LINE, 61 'y',0, A_B_LINE, 62 CONTROL('Y'),0, A_B_LINE, 63 SK(SK_CONTROL_K),0, A_B_LINE, 64 CONTROL('P'),0, A_B_LINE, 65 SK(SK_UP_ARROW),0, A_B_LINE, 66 'J',0, A_FF_LINE, 67 'K',0, A_BF_LINE, 68 'Y',0, A_BF_LINE, 69 'd',0, A_F_SCROLL, 70 CONTROL('D'),0, A_F_SCROLL, 71 'u',0, A_B_SCROLL, 72 CONTROL('U'),0, A_B_SCROLL, 73 ' ',0, A_F_SCREEN, 74 'f',0, A_F_SCREEN, 75 CONTROL('F'),0, A_F_SCREEN, 76 CONTROL('V'),0, A_F_SCREEN, 77 SK(SK_PAGE_DOWN),0, A_F_SCREEN, 78 'b',0, A_B_SCREEN, 79 CONTROL('B'),0, A_B_SCREEN, 80 ESC,'v',0, A_B_SCREEN, 81 SK(SK_PAGE_UP),0, A_B_SCREEN, 82 'z',0, A_F_WINDOW, 83 'w',0, A_B_WINDOW, 84 ESC,' ',0, A_FF_SCREEN, 85 'F',0, A_F_FOREVER, 86 'R',0, A_FREPAINT, 87 'r',0, A_REPAINT, 88 CONTROL('R'),0, A_REPAINT, 89 CONTROL('L'),0, A_REPAINT, 90 ESC,'u',0, A_UNDO_SEARCH, 91 'g',0, A_GOLINE, 92 SK(SK_HOME),0, A_GOLINE, 93 '<',0, A_GOLINE, 94 ESC,'<',0, A_GOLINE, 95 'p',0, A_PERCENT, 96 '%',0, A_PERCENT, 97 ESC,'[',0, A_LSHIFT, 98 ESC,']',0, A_RSHIFT, 99 ESC,'(',0, A_LSHIFT, 100 ESC,')',0, A_RSHIFT, 101 SK(SK_RIGHT_ARROW),0, A_RSHIFT, 102 SK(SK_LEFT_ARROW),0, A_LSHIFT, 103 '{',0, A_F_BRACKET|A_EXTRA, '{','}',0, 104 '}',0, A_B_BRACKET|A_EXTRA, '{','}',0, 105 '(',0, A_F_BRACKET|A_EXTRA, '(',')',0, 106 ')',0, A_B_BRACKET|A_EXTRA, '(',')',0, 107 '[',0, A_F_BRACKET|A_EXTRA, '[',']',0, 108 ']',0, A_B_BRACKET|A_EXTRA, '[',']',0, 109 ESC,CONTROL('F'),0, A_F_BRACKET, 110 ESC,CONTROL('B'),0, A_B_BRACKET, 111 'G',0, A_GOEND, 112 ESC,'>',0, A_GOEND, 113 '>',0, A_GOEND, 114 SK(SK_END),0, A_GOEND, 115 'P',0, A_GOPOS, 116 117 '0',0, A_DIGIT, 118 '1',0, A_DIGIT, 119 '2',0, A_DIGIT, 120 '3',0, A_DIGIT, 121 '4',0, A_DIGIT, 122 '5',0, A_DIGIT, 123 '6',0, A_DIGIT, 124 '7',0, A_DIGIT, 125 '8',0, A_DIGIT, 126 '9',0, A_DIGIT, 127 '.',0, A_DIGIT, 128 129 '=',0, A_STAT, 130 CONTROL('G'),0, A_STAT, 131 ':','f',0, A_STAT, 132 '/',0, A_F_SEARCH, 133 '?',0, A_B_SEARCH, 134 ESC,'/',0, A_F_SEARCH|A_EXTRA, '*',0, 135 ESC,'?',0, A_B_SEARCH|A_EXTRA, '*',0, 136 'n',0, A_AGAIN_SEARCH, 137 ESC,'n',0, A_T_AGAIN_SEARCH, 138 'N',0, A_REVERSE_SEARCH, 139 ESC,'N',0, A_T_REVERSE_SEARCH, 140 'm',0, A_SETMARK, 141 '\'',0, A_GOMARK, 142 CONTROL('X'),CONTROL('X'),0, A_GOMARK, 143 'E',0, A_EXAMINE, 144 ':','e',0, A_EXAMINE, 145 CONTROL('X'),CONTROL('V'),0, A_EXAMINE, 146 ':','n',0, A_NEXT_FILE, 147 ':','p',0, A_PREV_FILE, 148 't',0, A_NEXT_TAG, 149 'T',0, A_PREV_TAG, 150 ':','x',0, A_INDEX_FILE, 151 ':','d',0, A_REMOVE_FILE, 152 '-',0, A_OPT_TOGGLE, 153 ':','t',0, A_OPT_TOGGLE|A_EXTRA, 't',0, 154 's',0, A_OPT_TOGGLE|A_EXTRA, 'o',0, 155 '_',0, A_DISP_OPTION, 156 '|',0, A_PIPE, 157 'v',0, A_VISUAL, 158 '!',0, A_SHELL, 159 '+',0, A_FIRSTCMD, 160 161 'H',0, A_HELP, 162 'h',0, A_HELP, 163 SK(SK_F1),0, A_HELP, 164 'V',0, A_VERSION, 165 'q',0, A_QUIT, 166 'Q',0, A_QUIT, 167 ':','q',0, A_QUIT, 168 ':','Q',0, A_QUIT, 169 'Z','Z',0, A_QUIT 170}; 171 172/* 173 * Command table for UNIX 2003 compatibility: added first before builtin 174 * so that these commands override the normal LESS commands 175 */ 176 177static unsigned char UNIX03cmdtable[] = 178{ 179 's',0, A_F_LINE 180}; 181 182static unsigned char edittable[] = 183{ 184 '\t',0, EC_F_COMPLETE, /* TAB */ 185 '\17',0, EC_B_COMPLETE, /* BACKTAB */ 186 SK(SK_BACKTAB),0, EC_B_COMPLETE, /* BACKTAB */ 187 ESC,'\t',0, EC_B_COMPLETE, /* ESC TAB */ 188 CONTROL('L'),0, EC_EXPAND, /* CTRL-L */ 189 CONTROL('V'),0, EC_LITERAL, /* BACKSLASH */ 190 CONTROL('A'),0, EC_LITERAL, /* BACKSLASH */ 191 ESC,'l',0, EC_RIGHT, /* ESC l */ 192 SK(SK_RIGHT_ARROW),0, EC_RIGHT, /* RIGHTARROW */ 193 ESC,'h',0, EC_LEFT, /* ESC h */ 194 SK(SK_LEFT_ARROW),0, EC_LEFT, /* LEFTARROW */ 195 ESC,'b',0, EC_W_LEFT, /* ESC b */ 196 ESC,SK(SK_LEFT_ARROW),0, EC_W_LEFT, /* ESC LEFTARROW */ 197 SK(SK_CTL_LEFT_ARROW),0, EC_W_LEFT, /* CTRL-LEFTARROW */ 198 ESC,'w',0, EC_W_RIGHT, /* ESC w */ 199 ESC,SK(SK_RIGHT_ARROW),0, EC_W_RIGHT, /* ESC RIGHTARROW */ 200 SK(SK_CTL_RIGHT_ARROW),0, EC_W_RIGHT, /* CTRL-RIGHTARROW */ 201 ESC,'i',0, EC_INSERT, /* ESC i */ 202 SK(SK_INSERT),0, EC_INSERT, /* INSERT */ 203 ESC,'x',0, EC_DELETE, /* ESC x */ 204 SK(SK_DELETE),0, EC_DELETE, /* DELETE */ 205 ESC,'X',0, EC_W_DELETE, /* ESC X */ 206 ESC,SK(SK_DELETE),0, EC_W_DELETE, /* ESC DELETE */ 207 SK(SK_CTL_DELETE),0, EC_W_DELETE, /* CTRL-DELETE */ 208 SK(SK_CTL_BACKSPACE),0, EC_W_BACKSPACE, /* CTRL-BACKSPACE */ 209 ESC,'\b',0, EC_W_BACKSPACE, /* ESC BACKSPACE */ 210 ESC,'0',0, EC_HOME, /* ESC 0 */ 211 SK(SK_HOME),0, EC_HOME, /* HOME */ 212 ESC,'$',0, EC_END, /* ESC $ */ 213 SK(SK_END),0, EC_END, /* END */ 214 ESC,'k',0, EC_UP, /* ESC k */ 215 SK(SK_UP_ARROW),0, EC_UP, /* UPARROW */ 216 ESC,'j',0, EC_DOWN, /* ESC j */ 217 SK(SK_DOWN_ARROW),0, EC_DOWN, /* DOWNARROW */ 218}; 219 220/* 221 * Structure to support a list of command tables. 222 */ 223struct tablelist 224{ 225 struct tablelist *t_next; 226 char *t_start; 227 char *t_end; 228}; 229 230/* 231 * List of command tables and list of line-edit tables. 232 */ 233static struct tablelist *list_UNIX03cmd_tables = NULL; 234static struct tablelist *list_fcmd_tables = NULL; 235static struct tablelist *list_ecmd_tables = NULL; 236static struct tablelist *list_var_tables = NULL; 237static struct tablelist *list_sysvar_tables = NULL; 238 239 240/* 241 * Expand special key abbreviations in a command table. 242 */ 243 static void 244expand_special_keys(table, len) 245 char *table; 246 int len; 247{ 248 register char *fm; 249 register char *to; 250 register int a; 251 char *repl; 252 int klen; 253 254 for (fm = table; fm < table + len; ) 255 { 256 /* 257 * Rewrite each command in the table with any 258 * special key abbreviations expanded. 259 */ 260 for (to = fm; *fm != '\0'; ) 261 { 262 if (*fm != SK_SPECIAL_KEY) 263 { 264 *to++ = *fm++; 265 continue; 266 } 267 /* 268 * After SK_SPECIAL_KEY, next byte is the type 269 * of special key (one of the SK_* contants), 270 * and the byte after that is the number of bytes, 271 * N, reserved by the abbreviation (including the 272 * SK_SPECIAL_KEY and key type bytes). 273 * Replace all N bytes with the actual bytes 274 * output by the special key on this terminal. 275 */ 276 repl = special_key_str(fm[1]); 277 klen = fm[2] & 0377; 278 fm += klen; 279 if (repl == NULL || (int) strlen(repl) > klen) 280 repl = "\377"; 281 while (*repl != '\0') 282 *to++ = *repl++; 283 } 284 *to++ = '\0'; 285 /* 286 * Fill any unused bytes between end of command and 287 * the action byte with A_SKIP. 288 */ 289 while (to <= fm) 290 *to++ = A_SKIP; 291 fm++; 292 a = *fm++ & 0377; 293 if (a & A_EXTRA) 294 { 295 while (*fm++ != '\0') 296 continue; 297 } 298 } 299} 300 301/* 302 * Initialize the command lists. 303 */ 304 public void 305init_cmds() 306{ 307 /* 308 * Add the default command tables. 309 */ 310 if (COMPAT_MODE("bin/more", "unix2003")) { 311 add_UNIX03cmd_table((char*)UNIX03cmdtable, sizeof(UNIX03cmdtable)); 312 } 313 add_fcmd_table((char*)cmdtable, sizeof(cmdtable)); 314 add_ecmd_table((char*)edittable, sizeof(edittable)); 315#if USERFILE 316 /* 317 * For backwards compatibility, 318 * try to add tables in the OLD system lesskey file. 319 */ 320#ifdef BINDIR 321 add_hometable(NULL, BINDIR "/.sysless", 1); 322#endif 323 /* 324 * Try to add the tables in the system lesskey file. 325 */ 326 add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1); 327 /* 328 * Try to add the tables in the standard lesskey file "$HOME/.less". 329 */ 330 add_hometable("LESSKEY", LESSKEYFILE, 0); 331#endif 332} 333 334/* 335 * Add a command table. 336 */ 337 static int 338add_cmd_table(tlist, buf, len) 339 struct tablelist **tlist; 340 char *buf; 341 int len; 342{ 343 register struct tablelist *t; 344 345 if (len == 0) 346 return (0); 347 /* 348 * Allocate a tablelist structure, initialize it, 349 * and link it into the list of tables. 350 */ 351 if ((t = (struct tablelist *) 352 calloc(1, sizeof(struct tablelist))) == NULL) 353 { 354 return (-1); 355 } 356 expand_special_keys(buf, len); 357 t->t_start = buf; 358 t->t_end = buf + len; 359 t->t_next = *tlist; 360 *tlist = t; 361 return (0); 362} 363 364/* 365 * Add the UNIX2003 command table. 366 */ 367 public void 368add_UNIX03cmd_table(buf, len) 369 char *buf; 370 int len; 371{ 372 if (add_cmd_table(&list_UNIX03cmd_tables, buf, len) < 0) 373 error("Warning: some commands disabled", NULL_PARG); 374} 375 376/* 377 * Add a command table. 378 */ 379 public void 380add_fcmd_table(buf, len) 381 char *buf; 382 int len; 383{ 384 if (add_cmd_table(&list_fcmd_tables, buf, len) < 0) 385 error("Warning: some commands disabled", NULL_PARG); 386} 387 388/* 389 * Add an editing command table. 390 */ 391 public void 392add_ecmd_table(buf, len) 393 char *buf; 394 int len; 395{ 396 if (add_cmd_table(&list_ecmd_tables, buf, len) < 0) 397 error("Warning: some edit commands disabled", NULL_PARG); 398} 399 400/* 401 * Add an environment variable table. 402 */ 403 static void 404add_var_table(tlist, buf, len) 405 struct tablelist **tlist; 406 char *buf; 407 int len; 408{ 409 if (add_cmd_table(tlist, buf, len) < 0) 410 error("Warning: environment variables from lesskey file unavailable", NULL_PARG); 411} 412 413/* 414 * Search a single command table for the command string in cmd. 415 */ 416 static int 417cmd_search(cmd, table, endtable, sp) 418 char *cmd; 419 char *table; 420 char *endtable; 421 char **sp; 422{ 423 register char *p; 424 register char *q; 425 register int a; 426 427 *sp = NULL; 428 for (p = table, q = cmd; p < endtable; p++, q++) 429 { 430 if (*p == *q) 431 { 432 /* 433 * Current characters match. 434 * If we're at the end of the string, we've found it. 435 * Return the action code, which is the character 436 * after the null at the end of the string 437 * in the command table. 438 */ 439 if (*p == '\0') 440 { 441 a = *++p & 0377; 442 while (a == A_SKIP) 443 a = *++p & 0377; 444 if (a == A_END_LIST) 445 { 446 /* 447 * We get here only if the original 448 * cmd string passed in was empty (""). 449 * I don't think that can happen, 450 * but just in case ... 451 */ 452 return (A_UINVALID); 453 } 454 /* 455 * Check for an "extra" string. 456 */ 457 if (a & A_EXTRA) 458 { 459 *sp = ++p; 460 a &= ~A_EXTRA; 461 } 462 return (a); 463 } 464 } else if (*q == '\0') 465 { 466 /* 467 * Hit the end of the user's command, 468 * but not the end of the string in the command table. 469 * The user's command is incomplete. 470 */ 471 return (A_PREFIX); 472 } else 473 { 474 /* 475 * Not a match. 476 * Skip ahead to the next command in the 477 * command table, and reset the pointer 478 * to the beginning of the user's command. 479 */ 480 if (*p == '\0' && p[1] == A_END_LIST) 481 { 482 /* 483 * A_END_LIST is a special marker that tells 484 * us to abort the cmd search. 485 */ 486 return (A_UINVALID); 487 } 488 while (*p++ != '\0') 489 continue; 490 while (*p == A_SKIP) 491 p++; 492 if (*p & A_EXTRA) 493 while (*++p != '\0') 494 continue; 495 q = cmd-1; 496 } 497 } 498 /* 499 * No match found in the entire command table. 500 */ 501 return (A_INVALID); 502} 503 504/* 505 * Decode a command character and return the associated action. 506 * The "extra" string, if any, is returned in sp. 507 */ 508 static int 509cmd_decode(tlist, cmd, sp) 510 struct tablelist *tlist; 511 char *cmd; 512 char **sp; 513{ 514 register struct tablelist *t; 515 register int action = A_INVALID; 516 517 /* 518 * Search thru all the command tables. 519 * Stop when we find an action which is not A_INVALID. 520 */ 521 for (t = tlist; t != NULL; t = t->t_next) 522 { 523 action = cmd_search(cmd, t->t_start, t->t_end, sp); 524 if (action != A_INVALID) 525 break; 526 } 527 if (action == A_UINVALID) 528 action = A_INVALID; 529 return (action); 530} 531 532/* 533 * Decode a command from the cmdtables list. 534 */ 535 public int 536fcmd_decode(cmd, sp) 537 char *cmd; 538 char **sp; 539{ 540 if (list_UNIX03cmd_tables != NULL) { 541 /* If it exists, try to decode it first */ 542 int v = cmd_decode(list_UNIX03cmd_tables, cmd, sp); 543 if (v != A_INVALID) return v; 544 } 545 return (cmd_decode(list_fcmd_tables, cmd, sp)); 546} 547 548/* 549 * Decode a command from the edittables list. 550 */ 551 public int 552ecmd_decode(cmd, sp) 553 char *cmd; 554 char **sp; 555{ 556 return (cmd_decode(list_ecmd_tables, cmd, sp)); 557} 558 559/* 560 * Get the value of an environment variable. 561 * Looks first in the lesskey file, then in the real environment. 562 */ 563 public char * 564lgetenv(var) 565 char *var; 566{ 567 int a; 568 char *s; 569 570 a = cmd_decode(list_var_tables, var, &s); 571 if (a == EV_OK) 572 return (s); 573 s = getenv(var); 574 if (s != NULL && *s != '\0') 575 return (s); 576 a = cmd_decode(list_sysvar_tables, var, &s); 577 if (a == EV_OK) 578 return (s); 579 return (NULL); 580} 581 582#if USERFILE 583/* 584 * Get an "integer" from a lesskey file. 585 * Integers are stored in a funny format: 586 * two bytes, low order first, in radix KRADIX. 587 */ 588 static int 589gint(sp) 590 char **sp; 591{ 592 int n; 593 594 n = *(*sp)++; 595 n += *(*sp)++ * KRADIX; 596 return (n); 597} 598 599/* 600 * Process an old (pre-v241) lesskey file. 601 */ 602 static int 603old_lesskey(buf, len) 604 char *buf; 605 int len; 606{ 607 /* 608 * Old-style lesskey file. 609 * The file must end with either 610 * ...,cmd,0,action 611 * or ...,cmd,0,action|A_EXTRA,string,0 612 * So the last byte or the second to last byte must be zero. 613 */ 614 if (buf[len-1] != '\0' && buf[len-2] != '\0') 615 return (-1); 616 add_fcmd_table(buf, len); 617 return (0); 618} 619 620/* 621 * Process a new (post-v241) lesskey file. 622 */ 623 static int 624new_lesskey(buf, len, sysvar) 625 char *buf; 626 int len; 627 int sysvar; 628{ 629 char *p; 630 register int c; 631 register int n; 632 633 /* 634 * New-style lesskey file. 635 * Extract the pieces. 636 */ 637 if (buf[len-3] != C0_END_LESSKEY_MAGIC || 638 buf[len-2] != C1_END_LESSKEY_MAGIC || 639 buf[len-1] != C2_END_LESSKEY_MAGIC) 640 return (-1); 641 p = buf + 4; 642 for (;;) 643 { 644 c = *p++; 645 switch (c) 646 { 647 case CMD_SECTION: 648 n = gint(&p); 649 add_fcmd_table(p, n); 650 p += n; 651 break; 652 case EDIT_SECTION: 653 n = gint(&p); 654 add_ecmd_table(p, n); 655 p += n; 656 break; 657 case VAR_SECTION: 658 n = gint(&p); 659 add_var_table((sysvar) ? 660 &list_sysvar_tables : &list_var_tables, p, n); 661 p += n; 662 break; 663 case END_SECTION: 664 return (0); 665 default: 666 /* 667 * Unrecognized section type. 668 */ 669 return (-1); 670 } 671 } 672} 673 674/* 675 * Set up a user command table, based on a "lesskey" file. 676 */ 677 public int 678lesskey(filename, sysvar) 679 char *filename; 680 int sysvar; 681{ 682 register char *buf; 683 register POSITION len; 684 register long n; 685 register int f; 686 687 if (secure) 688 return (1); 689 /* 690 * Try to open the lesskey file. 691 */ 692 filename = shell_unquote(filename); 693 f = open(filename, OPEN_READ); 694 free(filename); 695 if (f < 0) 696 return (1); 697 698 /* 699 * Read the file into a buffer. 700 * We first figure out the size of the file and allocate space for it. 701 * {{ Minimal error checking is done here. 702 * A garbage .less file will produce strange results. 703 * To avoid a large amount of error checking code here, we 704 * rely on the lesskey program to generate a good .less file. }} 705 */ 706 len = filesize(f); 707 if (len == NULL_POSITION || len < 3) 708 { 709 /* 710 * Bad file (valid file must have at least 3 chars). 711 */ 712 close(f); 713 return (-1); 714 } 715 if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL) 716 { 717 close(f); 718 return (-1); 719 } 720 if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK) 721 { 722 free(buf); 723 close(f); 724 return (-1); 725 } 726 n = read(f, buf, (unsigned int) len); 727 close(f); 728 if (n != len) 729 { 730 free(buf); 731 return (-1); 732 } 733 734 /* 735 * Figure out if this is an old-style (before version 241) 736 * or new-style lesskey file format. 737 */ 738 if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC || 739 buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC) 740 return (old_lesskey(buf, (int)len)); 741 return (new_lesskey(buf, (int)len, sysvar)); 742} 743 744/* 745 * Add the standard lesskey file "$HOME/.less" 746 */ 747 public void 748add_hometable(envname, def_filename, sysvar) 749 char *envname; 750 char *def_filename; 751 int sysvar; 752{ 753 char *filename; 754 PARG parg; 755 756 if (envname != NULL && (filename = lgetenv(envname)) != NULL) 757 filename = save(filename); 758 else if (sysvar) 759 filename = save(def_filename); 760 else 761 filename = homefile(def_filename); 762 if (filename == NULL) 763 return; 764 if (lesskey(filename, sysvar) < 0) 765 { 766 parg.p_string = filename; 767 error("Cannot use lesskey file \"%s\"", &parg); 768 } 769 free(filename); 770} 771#endif 772 773/* 774 * See if a char is a special line-editing command. 775 */ 776 public int 777editchar(c, flags) 778 int c; 779 int flags; 780{ 781 int action; 782 int nch; 783 char *s; 784 char usercmd[MAX_CMDLEN+1]; 785 786 /* 787 * An editing character could actually be a sequence of characters; 788 * for example, an escape sequence sent by pressing the uparrow key. 789 * To match the editing string, we use the command decoder 790 * but give it the edit-commands command table 791 * This table is constructed to match the user's keyboard. 792 */ 793 if (c == erase_char || c == erase2_char) 794 return (EC_BACKSPACE); 795 if (c == kill_char) 796 return (EC_LINEKILL); 797 798 /* 799 * Collect characters in a buffer. 800 * Start with the one we have, and get more if we need them. 801 */ 802 nch = 0; 803 do { 804 if (nch > 0) 805 c = getcc(); 806 usercmd[nch] = c; 807 usercmd[nch+1] = '\0'; 808 nch++; 809 action = ecmd_decode(usercmd, &s); 810 } while (action == A_PREFIX); 811 812 if (flags & EC_NORIGHTLEFT) 813 { 814 switch (action) 815 { 816 case EC_RIGHT: 817 case EC_LEFT: 818 action = A_INVALID; 819 break; 820 } 821 } 822#if CMD_HISTORY 823 if (flags & EC_NOHISTORY) 824 { 825 /* 826 * The caller says there is no history list. 827 * Reject any history-manipulation action. 828 */ 829 switch (action) 830 { 831 case EC_UP: 832 case EC_DOWN: 833 action = A_INVALID; 834 break; 835 } 836 } 837#endif 838#if TAB_COMPLETE_FILENAME 839 if (flags & EC_NOCOMPLETE) 840 { 841 /* 842 * The caller says we don't want any filename completion cmds. 843 * Reject them. 844 */ 845 switch (action) 846 { 847 case EC_F_COMPLETE: 848 case EC_B_COMPLETE: 849 case EC_EXPAND: 850 action = A_INVALID; 851 break; 852 } 853 } 854#endif 855 if ((flags & EC_PEEK) || action == A_INVALID) 856 { 857 /* 858 * We're just peeking, or we didn't understand the command. 859 * Unget all the characters we read in the loop above. 860 * This does NOT include the original character that was 861 * passed in as a parameter. 862 */ 863 while (nch > 1) 864 { 865 ungetcc(usercmd[--nch]); 866 } 867 } else 868 { 869 if (s != NULL) 870 ungetsc(s); 871 } 872 return action; 873} 874 875