1 /* 2 * Copyright (C) 1984-2023 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, see the README file. 8 */ 9 10 11 /* 12 * Routines to decode user commands. 13 * 14 * This is all table driven. 15 * A command table is a sequence of command descriptors. 16 * Each command descriptor is a sequence of bytes with the following format: 17 * <c1><c2>...<cN><0><action> 18 * The characters c1,c2,...,cN are the command string; that is, 19 * the characters which the user must type. 20 * It is terminated by a null <0> byte. 21 * The byte after the null byte is the action code associated 22 * with the command string. 23 * If an action byte is OR-ed with A_EXTRA, this indicates 24 * that the option byte is followed by an extra string. 25 * 26 * There may be many command tables. 27 * The first (default) table is built-in. 28 * Other tables are read in from "lesskey" files. 29 * All the tables are linked together and are searched in order. 30 */ 31 32 #include "less.h" 33 #include "cmd.h" 34 #include "lesskey.h" 35 36 extern int erase_char, erase2_char, kill_char; 37 extern int secure; 38 extern int mousecap; 39 extern int screen_trashed; 40 extern int sc_height; 41 42 #define SK(k) \ 43 SK_SPECIAL_KEY, (k), 6, 1, 1, 1 44 /* 45 * Command table is ordered roughly according to expected 46 * frequency of use, so the common commands are near the beginning. 47 */ 48 49 static unsigned char cmdtable[] = 50 { 51 '\r',0, A_F_LINE, 52 '\n',0, A_F_LINE, 53 'e',0, A_F_LINE, 54 'j',0, A_F_LINE, 55 SK(SK_DOWN_ARROW),0, A_F_LINE, 56 CONTROL('E'),0, A_F_LINE, 57 CONTROL('N'),0, A_F_LINE, 58 'k',0, A_B_LINE, 59 'y',0, A_B_LINE, 60 CONTROL('Y'),0, A_B_LINE, 61 SK(SK_CONTROL_K),0, A_B_LINE, 62 CONTROL('P'),0, A_B_LINE, 63 SK(SK_UP_ARROW),0, A_B_LINE, 64 'J',0, A_FF_LINE, 65 'K',0, A_BF_LINE, 66 'Y',0, A_BF_LINE, 67 'd',0, A_F_SCROLL, 68 CONTROL('D'),0, A_F_SCROLL, 69 'u',0, A_B_SCROLL, 70 CONTROL('U'),0, A_B_SCROLL, 71 ESC,'[','M',0, A_X11MOUSE_IN, 72 ESC,'[','<',0, A_X116MOUSE_IN, 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 ESC,'F',0, A_F_UNTIL_HILITE, 87 'R',0, A_FREPAINT, 88 'r',0, A_REPAINT, 89 CONTROL('R'),0, A_REPAINT, 90 CONTROL('L'),0, A_REPAINT, 91 ESC,'u',0, A_UNDO_SEARCH, 92 ESC,'U',0, A_CLR_SEARCH, 93 'g',0, A_GOLINE, 94 SK(SK_HOME),0, A_GOLINE, 95 '<',0, A_GOLINE, 96 ESC,'<',0, A_GOLINE, 97 'p',0, A_PERCENT, 98 '%',0, A_PERCENT, 99 ESC,'[',0, A_LSHIFT, 100 ESC,']',0, A_RSHIFT, 101 ESC,'(',0, A_LSHIFT, 102 ESC,')',0, A_RSHIFT, 103 ESC,'{',0, A_LLSHIFT, 104 ESC,'}',0, A_RRSHIFT, 105 SK(SK_RIGHT_ARROW),0, A_RSHIFT, 106 SK(SK_LEFT_ARROW),0, A_LSHIFT, 107 SK(SK_CTL_RIGHT_ARROW),0, A_RRSHIFT, 108 SK(SK_CTL_LEFT_ARROW),0, A_LLSHIFT, 109 '{',0, A_F_BRACKET|A_EXTRA, '{','}',0, 110 '}',0, A_B_BRACKET|A_EXTRA, '{','}',0, 111 '(',0, A_F_BRACKET|A_EXTRA, '(',')',0, 112 ')',0, A_B_BRACKET|A_EXTRA, '(',')',0, 113 '[',0, A_F_BRACKET|A_EXTRA, '[',']',0, 114 ']',0, A_B_BRACKET|A_EXTRA, '[',']',0, 115 ESC,CONTROL('F'),0, A_F_BRACKET, 116 ESC,CONTROL('B'),0, A_B_BRACKET, 117 'G',0, A_GOEND, 118 ESC,'G',0, A_GOEND_BUF, 119 ESC,'>',0, A_GOEND, 120 '>',0, A_GOEND, 121 SK(SK_END),0, A_GOEND, 122 'P',0, A_GOPOS, 123 124 '0',0, A_DIGIT, 125 '1',0, A_DIGIT, 126 '2',0, A_DIGIT, 127 '3',0, A_DIGIT, 128 '4',0, A_DIGIT, 129 '5',0, A_DIGIT, 130 '6',0, A_DIGIT, 131 '7',0, A_DIGIT, 132 '8',0, A_DIGIT, 133 '9',0, A_DIGIT, 134 '.',0, A_DIGIT, 135 136 '=',0, A_STAT, 137 CONTROL('G'),0, A_STAT, 138 ':','f',0, A_STAT, 139 '/',0, A_F_SEARCH, 140 '?',0, A_B_SEARCH, 141 ESC,'/',0, A_F_SEARCH|A_EXTRA, '*',0, 142 ESC,'?',0, A_B_SEARCH|A_EXTRA, '*',0, 143 'n',0, A_AGAIN_SEARCH, 144 ESC,'n',0, A_T_AGAIN_SEARCH, 145 'N',0, A_REVERSE_SEARCH, 146 ESC,'N',0, A_T_REVERSE_SEARCH, 147 '&',0, A_FILTER, 148 'm',0, A_SETMARK, 149 'M',0, A_SETMARKBOT, 150 ESC,'m',0, A_CLRMARK, 151 '\'',0, A_GOMARK, 152 CONTROL('X'),CONTROL('X'),0, A_GOMARK, 153 'E',0, A_EXAMINE, 154 ':','e',0, A_EXAMINE, 155 CONTROL('X'),CONTROL('V'),0, A_EXAMINE, 156 ':','n',0, A_NEXT_FILE, 157 ':','p',0, A_PREV_FILE, 158 't',0, A_NEXT_TAG, 159 'T',0, A_PREV_TAG, 160 ':','x',0, A_INDEX_FILE, 161 ':','d',0, A_REMOVE_FILE, 162 '-',0, A_OPT_TOGGLE, 163 ':','t',0, A_OPT_TOGGLE|A_EXTRA, 't',0, 164 's',0, A_OPT_TOGGLE|A_EXTRA, 'o',0, 165 '_',0, A_DISP_OPTION, 166 '|',0, A_PIPE, 167 'v',0, A_VISUAL, 168 '!',0, A_SHELL, 169 '#',0, A_PSHELL, 170 '+',0, A_FIRSTCMD, 171 172 'H',0, A_HELP, 173 'h',0, A_HELP, 174 SK(SK_F1),0, A_HELP, 175 'V',0, A_VERSION, 176 'q',0, A_QUIT, 177 'Q',0, A_QUIT, 178 ':','q',0, A_QUIT, 179 ':','Q',0, A_QUIT, 180 'Z','Z',0, A_QUIT 181 }; 182 183 static unsigned char edittable[] = 184 { 185 '\t',0, EC_F_COMPLETE, /* TAB */ 186 '\17',0, EC_B_COMPLETE, /* BACKTAB */ 187 SK(SK_BACKTAB),0, EC_B_COMPLETE, /* BACKTAB */ 188 ESC,'\t',0, EC_B_COMPLETE, /* ESC TAB */ 189 CONTROL('L'),0, EC_EXPAND, /* CTRL-L */ 190 CONTROL('V'),0, EC_LITERAL, /* BACKSLASH */ 191 CONTROL('A'),0, EC_LITERAL, /* BACKSLASH */ 192 ESC,'l',0, EC_RIGHT, /* ESC l */ 193 SK(SK_RIGHT_ARROW),0, EC_RIGHT, /* RIGHTARROW */ 194 ESC,'h',0, EC_LEFT, /* ESC h */ 195 SK(SK_LEFT_ARROW),0, EC_LEFT, /* LEFTARROW */ 196 ESC,'b',0, EC_W_LEFT, /* ESC b */ 197 ESC,SK(SK_LEFT_ARROW),0, EC_W_LEFT, /* ESC LEFTARROW */ 198 SK(SK_CTL_LEFT_ARROW),0, EC_W_LEFT, /* CTRL-LEFTARROW */ 199 ESC,'w',0, EC_W_RIGHT, /* ESC w */ 200 ESC,SK(SK_RIGHT_ARROW),0, EC_W_RIGHT, /* ESC RIGHTARROW */ 201 SK(SK_CTL_RIGHT_ARROW),0, EC_W_RIGHT, /* CTRL-RIGHTARROW */ 202 ESC,'i',0, EC_INSERT, /* ESC i */ 203 SK(SK_INSERT),0, EC_INSERT, /* INSERT */ 204 ESC,'x',0, EC_DELETE, /* ESC x */ 205 SK(SK_DELETE),0, EC_DELETE, /* DELETE */ 206 ESC,'X',0, EC_W_DELETE, /* ESC X */ 207 ESC,SK(SK_DELETE),0, EC_W_DELETE, /* ESC DELETE */ 208 SK(SK_CTL_DELETE),0, EC_W_DELETE, /* CTRL-DELETE */ 209 SK(SK_CTL_BACKSPACE),0, EC_W_BACKSPACE, /* CTRL-BACKSPACE */ 210 ESC,SK(SK_BACKSPACE),0, EC_W_BACKSPACE, /* ESC BACKSPACE */ 211 ESC,'0',0, EC_HOME, /* ESC 0 */ 212 SK(SK_HOME),0, EC_HOME, /* HOME */ 213 ESC,'$',0, EC_END, /* ESC $ */ 214 SK(SK_END),0, EC_END, /* END */ 215 ESC,'k',0, EC_UP, /* ESC k */ 216 SK(SK_UP_ARROW),0, EC_UP, /* UPARROW */ 217 ESC,'j',0, EC_DOWN, /* ESC j */ 218 SK(SK_DOWN_ARROW),0, EC_DOWN, /* DOWNARROW */ 219 CONTROL('G'),0, EC_ABORT, /* CTRL-G */ 220 ESC,'[','M',0, EC_X11MOUSE, /* X11 mouse report */ 221 ESC,'[','<',0, EC_X116MOUSE, /* X11 1006 mouse report */ 222 }; 223 224 /* 225 * Structure to support a list of command tables. 226 */ 227 struct tablelist 228 { 229 struct tablelist *t_next; 230 char *t_start; 231 char *t_end; 232 }; 233 234 /* 235 * List of command tables and list of line-edit tables. 236 */ 237 static struct tablelist *list_fcmd_tables = NULL; 238 static struct tablelist *list_ecmd_tables = NULL; 239 static struct tablelist *list_var_tables = NULL; 240 static struct tablelist *list_sysvar_tables = NULL; 241 242 243 /* 244 * Expand special key abbreviations in a command table. 245 */ 246 static void expand_special_keys(char *table, int len) 247 { 248 char *fm; 249 char *to; 250 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_* constants), 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 * Expand special key abbreviations in a list of command tables. 303 */ 304 static void expand_cmd_table(struct tablelist *tlist) 305 { 306 struct tablelist *t; 307 for (t = tlist; t != NULL; t = t->t_next) 308 { 309 expand_special_keys(t->t_start, t->t_end - t->t_start); 310 } 311 } 312 313 /* 314 * Expand special key abbreviations in all command tables. 315 */ 316 public void expand_cmd_tables(void) 317 { 318 expand_cmd_table(list_fcmd_tables); 319 expand_cmd_table(list_ecmd_tables); 320 expand_cmd_table(list_var_tables); 321 expand_cmd_table(list_sysvar_tables); 322 } 323 324 325 /* 326 * Initialize the command lists. 327 */ 328 public void init_cmds(void) 329 { 330 /* 331 * Add the default command tables. 332 */ 333 add_fcmd_table((char*)cmdtable, sizeof(cmdtable)); 334 add_ecmd_table((char*)edittable, sizeof(edittable)); 335 #if USERFILE 336 #ifdef BINDIR /* For backwards compatibility */ 337 /* Try to add tables in the OLD system lesskey file. */ 338 add_hometable(lesskey, NULL, BINDIR "/.sysless", 1); 339 #endif 340 /* 341 * Try to load lesskey source file or binary file. 342 * If the source file succeeds, don't load binary file. 343 * The binary file is likely to have been generated from 344 * a (possibly out of date) copy of the src file, 345 * so loading it is at best redundant. 346 */ 347 /* 348 * Try to add tables in system lesskey src file. 349 */ 350 #if HAVE_LESSKEYSRC 351 if (add_hometable(lesskey_src, "LESSKEYIN_SYSTEM", LESSKEYINFILE_SYS, 1) != 0) 352 #endif 353 { 354 /* 355 * Try to add the tables in the system lesskey binary file. 356 */ 357 add_hometable(lesskey, "LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1); 358 } 359 /* 360 * Try to add tables in the lesskey src file "$HOME/.lesskey". 361 */ 362 #if HAVE_LESSKEYSRC 363 if (add_hometable(lesskey_src, "LESSKEYIN", DEF_LESSKEYINFILE, 0) != 0) 364 #endif 365 { 366 /* 367 * Try to add the tables in the standard lesskey binary file "$HOME/.less". 368 */ 369 add_hometable(lesskey, "LESSKEY", LESSKEYFILE, 0); 370 } 371 #endif 372 } 373 374 /* 375 * Add a command table. 376 */ 377 static int add_cmd_table(struct tablelist **tlist, char *buf, int len) 378 { 379 struct tablelist *t; 380 381 if (len == 0) 382 return (0); 383 /* 384 * Allocate a tablelist structure, initialize it, 385 * and link it into the list of tables. 386 */ 387 if ((t = (struct tablelist *) 388 calloc(1, sizeof(struct tablelist))) == NULL) 389 { 390 return (-1); 391 } 392 t->t_start = buf; 393 t->t_end = buf + len; 394 t->t_next = *tlist; 395 *tlist = t; 396 return (0); 397 } 398 399 /* 400 * Add a command table. 401 */ 402 public void add_fcmd_table(char *buf, int len) 403 { 404 if (add_cmd_table(&list_fcmd_tables, buf, len) < 0) 405 error("Warning: some commands disabled", NULL_PARG); 406 } 407 408 /* 409 * Add an editing command table. 410 */ 411 public void add_ecmd_table(char *buf, int len) 412 { 413 if (add_cmd_table(&list_ecmd_tables, buf, len) < 0) 414 error("Warning: some edit commands disabled", NULL_PARG); 415 } 416 417 /* 418 * Add an environment variable table. 419 */ 420 static void add_var_table(struct tablelist **tlist, char *buf, int len) 421 { 422 if (add_cmd_table(tlist, buf, len) < 0) 423 error("Warning: environment variables from lesskey file unavailable", NULL_PARG); 424 } 425 426 /* 427 * Return action for a mouse wheel down event. 428 */ 429 static int mouse_wheel_down(void) 430 { 431 return ((mousecap == OPT_ONPLUS) ? A_B_MOUSE : A_F_MOUSE); 432 } 433 434 /* 435 * Return action for a mouse wheel up event. 436 */ 437 static int mouse_wheel_up(void) 438 { 439 return ((mousecap == OPT_ONPLUS) ? A_F_MOUSE : A_B_MOUSE); 440 } 441 442 /* 443 * Return action for a mouse button release event. 444 */ 445 static int mouse_button_rel(int x, int y) 446 { 447 /* 448 * {{ It would be better to return an action and then do this 449 * in commands() but it's nontrivial to pass y to it. }} 450 */ 451 if (y < sc_height-1) 452 { 453 setmark('#', y); 454 screen_trashed = 1; 455 } 456 return (A_NOACTION); 457 } 458 459 /* 460 * Read a decimal integer. Return the integer and set *pterm to the terminating char. 461 */ 462 static int getcc_int(char *pterm) 463 { 464 int num = 0; 465 int digits = 0; 466 for (;;) 467 { 468 char ch = getcc(); 469 if (ch < '0' || ch > '9') 470 { 471 if (pterm != NULL) *pterm = ch; 472 if (digits == 0) 473 return (-1); 474 return (num); 475 } 476 if (ckd_mul(&num, num, 10) || ckd_add(&num, num, ch - '0')) 477 return -1; 478 ++digits; 479 } 480 } 481 482 /* 483 * Read suffix of mouse input and return the action to take. 484 * The prefix ("\e[M") has already been read. 485 */ 486 static int x11mouse_action(int skip) 487 { 488 int b = getcc() - X11MOUSE_OFFSET; 489 int x = getcc() - X11MOUSE_OFFSET-1; 490 int y = getcc() - X11MOUSE_OFFSET-1; 491 if (skip) 492 return (A_NOACTION); 493 switch (b) { 494 default: 495 return (A_NOACTION); 496 case X11MOUSE_WHEEL_DOWN: 497 return mouse_wheel_down(); 498 case X11MOUSE_WHEEL_UP: 499 return mouse_wheel_up(); 500 case X11MOUSE_BUTTON_REL: 501 return mouse_button_rel(x, y); 502 } 503 } 504 505 /* 506 * Read suffix of mouse input and return the action to take. 507 * The prefix ("\e[<") has already been read. 508 */ 509 static int x116mouse_action(int skip) 510 { 511 char ch; 512 int x, y; 513 int b = getcc_int(&ch); 514 if (b < 0 || ch != ';') return (A_NOACTION); 515 x = getcc_int(&ch) - 1; 516 if (x < 0 || ch != ';') return (A_NOACTION); 517 y = getcc_int(&ch) - 1; 518 if (y < 0) return (A_NOACTION); 519 if (skip) 520 return (A_NOACTION); 521 switch (b) { 522 case X11MOUSE_WHEEL_DOWN: 523 return mouse_wheel_down(); 524 case X11MOUSE_WHEEL_UP: 525 return mouse_wheel_up(); 526 default: 527 if (ch != 'm') return (A_NOACTION); 528 return mouse_button_rel(x, y); 529 } 530 } 531 532 /* 533 * Search a single command table for the command string in cmd. 534 */ 535 static int cmd_search(char *cmd, char *table, char *endtable, char **sp) 536 { 537 char *p; 538 char *q; 539 int a; 540 541 *sp = NULL; 542 for (p = table, q = cmd; p < endtable; p++, q++) 543 { 544 if (*p == *q) 545 { 546 /* 547 * Current characters match. 548 * If we're at the end of the string, we've found it. 549 * Return the action code, which is the character 550 * after the null at the end of the string 551 * in the command table. 552 */ 553 if (*p == '\0') 554 { 555 a = *++p & 0377; 556 while (a == A_SKIP) 557 a = *++p & 0377; 558 if (a == A_END_LIST) 559 { 560 /* 561 * We get here only if the original 562 * cmd string passed in was empty (""). 563 * I don't think that can happen, 564 * but just in case ... 565 */ 566 return (A_UINVALID); 567 } 568 /* 569 * Check for an "extra" string. 570 */ 571 if (a & A_EXTRA) 572 { 573 *sp = ++p; 574 a &= ~A_EXTRA; 575 } 576 if (a == A_X11MOUSE_IN) 577 a = x11mouse_action(0); 578 else if (a == A_X116MOUSE_IN) 579 a = x116mouse_action(0); 580 return (a); 581 } 582 } else if (*q == '\0') 583 { 584 /* 585 * Hit the end of the user's command, 586 * but not the end of the string in the command table. 587 * The user's command is incomplete. 588 */ 589 return (A_PREFIX); 590 } else 591 { 592 /* 593 * Not a match. 594 * Skip ahead to the next command in the 595 * command table, and reset the pointer 596 * to the beginning of the user's command. 597 */ 598 if (*p == '\0' && p[1] == A_END_LIST) 599 { 600 /* 601 * A_END_LIST is a special marker that tells 602 * us to abort the cmd search. 603 */ 604 return (A_UINVALID); 605 } 606 while (*p++ != '\0') 607 continue; 608 while (*p == A_SKIP) 609 p++; 610 if (*p & A_EXTRA) 611 while (*++p != '\0') 612 continue; 613 q = cmd-1; 614 } 615 } 616 /* 617 * No match found in the entire command table. 618 */ 619 return (A_INVALID); 620 } 621 622 /* 623 * Decode a command character and return the associated action. 624 * The "extra" string, if any, is returned in sp. 625 */ 626 static int cmd_decode(struct tablelist *tlist, char *cmd, char **sp) 627 { 628 struct tablelist *t; 629 int action = A_INVALID; 630 631 /* 632 * Search thru all the command tables. 633 * Stop when we find an action which is not A_INVALID. 634 */ 635 for (t = tlist; t != NULL; t = t->t_next) 636 { 637 action = cmd_search(cmd, t->t_start, t->t_end, sp); 638 if (action != A_INVALID) 639 break; 640 } 641 if (action == A_UINVALID) 642 action = A_INVALID; 643 return (action); 644 } 645 646 /* 647 * Decode a command from the cmdtables list. 648 */ 649 public int fcmd_decode(char *cmd, char **sp) 650 { 651 return (cmd_decode(list_fcmd_tables, cmd, sp)); 652 } 653 654 /* 655 * Decode a command from the edittables list. 656 */ 657 public int ecmd_decode(char *cmd, char **sp) 658 { 659 return (cmd_decode(list_ecmd_tables, cmd, sp)); 660 } 661 662 /* 663 * Get the value of an environment variable. 664 * Looks first in the lesskey file, then in the real environment. 665 */ 666 public char * lgetenv(char *var) 667 { 668 int a; 669 char *s; 670 671 a = cmd_decode(list_var_tables, var, &s); 672 if (a == EV_OK) 673 return (s); 674 s = getenv(var); 675 if (s != NULL && *s != '\0') 676 return (s); 677 a = cmd_decode(list_sysvar_tables, var, &s); 678 if (a == EV_OK) 679 return (s); 680 return (NULL); 681 } 682 683 /* 684 * Is a string null or empty? 685 */ 686 public int isnullenv(char *s) 687 { 688 return (s == NULL || *s == '\0'); 689 } 690 691 #if USERFILE 692 /* 693 * Get an "integer" from a lesskey file. 694 * Integers are stored in a funny format: 695 * two bytes, low order first, in radix KRADIX. 696 */ 697 static int gint(char **sp) 698 { 699 int n; 700 701 n = *(*sp)++; 702 n += *(*sp)++ * KRADIX; 703 return (n); 704 } 705 706 /* 707 * Process an old (pre-v241) lesskey file. 708 */ 709 static int old_lesskey(char *buf, int len) 710 { 711 /* 712 * Old-style lesskey file. 713 * The file must end with either 714 * ...,cmd,0,action 715 * or ...,cmd,0,action|A_EXTRA,string,0 716 * So the last byte or the second to last byte must be zero. 717 */ 718 if (buf[len-1] != '\0' && buf[len-2] != '\0') 719 return (-1); 720 add_fcmd_table(buf, len); 721 return (0); 722 } 723 724 /* 725 * Process a new (post-v241) lesskey file. 726 */ 727 static int new_lesskey(char *buf, int len, int sysvar) 728 { 729 char *p; 730 char *end; 731 int c; 732 int n; 733 734 /* 735 * New-style lesskey file. 736 * Extract the pieces. 737 */ 738 if (buf[len-3] != C0_END_LESSKEY_MAGIC || 739 buf[len-2] != C1_END_LESSKEY_MAGIC || 740 buf[len-1] != C2_END_LESSKEY_MAGIC) 741 return (-1); 742 p = buf + 4; 743 end = buf + len; 744 for (;;) 745 { 746 c = *p++; 747 switch (c) 748 { 749 case CMD_SECTION: 750 n = gint(&p); 751 if (n < 0 || p+n >= end) 752 return (-1); 753 add_fcmd_table(p, n); 754 p += n; 755 break; 756 case EDIT_SECTION: 757 n = gint(&p); 758 if (n < 0 || p+n >= end) 759 return (-1); 760 add_ecmd_table(p, n); 761 p += n; 762 break; 763 case VAR_SECTION: 764 n = gint(&p); 765 if (n < 0 || p+n >= end) 766 return (-1); 767 add_var_table((sysvar) ? 768 &list_sysvar_tables : &list_var_tables, p, n); 769 p += n; 770 break; 771 case END_SECTION: 772 return (0); 773 default: 774 /* 775 * Unrecognized section type. 776 */ 777 return (-1); 778 } 779 } 780 } 781 782 /* 783 * Set up a user command table, based on a "lesskey" file. 784 */ 785 public int lesskey(char *filename, int sysvar) 786 { 787 char *buf; 788 POSITION len; 789 long n; 790 int f; 791 792 if (secure) 793 return (1); 794 /* 795 * Try to open the lesskey file. 796 */ 797 f = open(filename, OPEN_READ); 798 if (f < 0) 799 return (1); 800 801 /* 802 * Read the file into a buffer. 803 * We first figure out the size of the file and allocate space for it. 804 * {{ Minimal error checking is done here. 805 * A garbage .less file will produce strange results. 806 * To avoid a large amount of error checking code here, we 807 * rely on the lesskey program to generate a good .less file. }} 808 */ 809 len = filesize(f); 810 if (len == NULL_POSITION || len < 3) 811 { 812 /* 813 * Bad file (valid file must have at least 3 chars). 814 */ 815 close(f); 816 return (-1); 817 } 818 if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL) 819 { 820 close(f); 821 return (-1); 822 } 823 if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK) 824 { 825 free(buf); 826 close(f); 827 return (-1); 828 } 829 n = read(f, buf, (unsigned int) len); 830 close(f); 831 if (n != len) 832 { 833 free(buf); 834 return (-1); 835 } 836 837 /* 838 * Figure out if this is an old-style (before version 241) 839 * or new-style lesskey file format. 840 */ 841 if (len < 4 || 842 buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC || 843 buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC) 844 return (old_lesskey(buf, (int)len)); 845 return (new_lesskey(buf, (int)len, sysvar)); 846 } 847 848 #if HAVE_LESSKEYSRC 849 public int lesskey_src(char *filename, int sysvar) 850 { 851 static struct lesskey_tables tables; 852 int r = parse_lesskey(filename, &tables); 853 if (r != 0) 854 return (r); 855 add_fcmd_table(xbuf_char_data(&tables.cmdtable.buf), tables.cmdtable.buf.end); 856 add_ecmd_table(xbuf_char_data(&tables.edittable.buf), tables.edittable.buf.end); 857 add_var_table(sysvar ? &list_sysvar_tables : &list_var_tables, 858 xbuf_char_data(&tables.vartable.buf), tables.vartable.buf.end); 859 return (0); 860 } 861 862 void lesskey_parse_error(char *s) 863 { 864 PARG parg; 865 parg.p_string = s; 866 error("%s", &parg); 867 } 868 #endif /* HAVE_LESSKEYSRC */ 869 870 /* 871 * Add a lesskey file. 872 */ 873 public int add_hometable(int (*call_lesskey)(char *, int), char *envname, char *def_filename, int sysvar) 874 { 875 char *filename; 876 int r; 877 878 if (envname != NULL && (filename = lgetenv(envname)) != NULL) 879 filename = save(filename); 880 else if (sysvar) /* def_filename is full path */ 881 filename = save(def_filename); 882 else /* def_filename is just basename */ 883 { 884 /* Remove first char (normally a dot) unless stored in $HOME. */ 885 char *xdg = lgetenv("XDG_CONFIG_HOME"); 886 if (!isnullenv(xdg)) 887 filename = dirfile(xdg, &def_filename[1], 1); 888 if (filename == NULL) 889 { 890 char *home = lgetenv("HOME"); 891 if (!isnullenv(home)) 892 { 893 char *cfg_dir = dirfile(home, ".config", 0); 894 filename = dirfile(cfg_dir, &def_filename[1], 1); 895 free(cfg_dir); 896 } 897 } 898 if (filename == NULL) 899 filename = homefile(def_filename); 900 } 901 if (filename == NULL) 902 return -1; 903 r = (*call_lesskey)(filename, sysvar); 904 free(filename); 905 return (r); 906 } 907 #endif 908 909 /* 910 * See if a char is a special line-editing command. 911 */ 912 public int editchar(int c, int flags) 913 { 914 int action; 915 int nch; 916 char *s; 917 char usercmd[MAX_CMDLEN+1]; 918 919 /* 920 * An editing character could actually be a sequence of characters; 921 * for example, an escape sequence sent by pressing the uparrow key. 922 * To match the editing string, we use the command decoder 923 * but give it the edit-commands command table 924 * This table is constructed to match the user's keyboard. 925 */ 926 if (c == erase_char || c == erase2_char) 927 return (EC_BACKSPACE); 928 if (c == kill_char) 929 { 930 #if MSDOS_COMPILER==WIN32C 931 if (!win32_kbhit()) 932 #endif 933 return (EC_LINEKILL); 934 } 935 936 /* 937 * Collect characters in a buffer. 938 * Start with the one we have, and get more if we need them. 939 */ 940 nch = 0; 941 do { 942 if (nch > 0) 943 c = getcc(); 944 usercmd[nch] = c; 945 usercmd[nch+1] = '\0'; 946 nch++; 947 action = ecmd_decode(usercmd, &s); 948 } while (action == A_PREFIX && nch < MAX_CMDLEN); 949 950 if (action == EC_X11MOUSE) 951 return (x11mouse_action(1)); 952 if (action == EC_X116MOUSE) 953 return (x116mouse_action(1)); 954 955 if (flags & ECF_NORIGHTLEFT) 956 { 957 switch (action) 958 { 959 case EC_RIGHT: 960 case EC_LEFT: 961 action = A_INVALID; 962 break; 963 } 964 } 965 #if CMD_HISTORY 966 if (flags & ECF_NOHISTORY) 967 { 968 /* 969 * The caller says there is no history list. 970 * Reject any history-manipulation action. 971 */ 972 switch (action) 973 { 974 case EC_UP: 975 case EC_DOWN: 976 action = A_INVALID; 977 break; 978 } 979 } 980 #endif 981 #if TAB_COMPLETE_FILENAME 982 if (flags & ECF_NOCOMPLETE) 983 { 984 /* 985 * The caller says we don't want any filename completion cmds. 986 * Reject them. 987 */ 988 switch (action) 989 { 990 case EC_F_COMPLETE: 991 case EC_B_COMPLETE: 992 case EC_EXPAND: 993 action = A_INVALID; 994 break; 995 } 996 } 997 #endif 998 if ((flags & ECF_PEEK) || action == A_INVALID) 999 { 1000 /* 1001 * We're just peeking, or we didn't understand the command. 1002 * Unget all the characters we read in the loop above. 1003 * This does NOT include the original character that was 1004 * passed in as a parameter. 1005 */ 1006 while (nch > 1) 1007 { 1008 ungetcc(usercmd[--nch]); 1009 } 1010 } else 1011 { 1012 if (s != NULL) 1013 ungetsc(s); 1014 } 1015 return action; 1016 } 1017 1018