1 /*
2 * Copyright (C) 1984-2021 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_FIRSTCMD,
170
171 'H',0, A_HELP,
172 'h',0, A_HELP,
173 SK(SK_F1),0, A_HELP,
174 'V',0, A_VERSION,
175 'q',0, A_QUIT,
176 'Q',0, A_QUIT,
177 ':','q',0, A_QUIT,
178 ':','Q',0, A_QUIT,
179 'Z','Z',0, A_QUIT
180 };
181
182 static 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 CONTROL('G'),0, EC_ABORT, /* CTRL-G */
219 ESC,'[','M',0, EC_X11MOUSE, /* X11 mouse report */
220 ESC,'[','<',0, EC_X116MOUSE, /* X11 1006 mouse report */
221 };
222
223 /*
224 * Structure to support a list of command tables.
225 */
226 struct tablelist
227 {
228 struct tablelist *t_next;
229 char *t_start;
230 char *t_end;
231 };
232
233 /*
234 * List of command tables and list of line-edit tables.
235 */
236 static struct tablelist *list_fcmd_tables = NULL;
237 static struct tablelist *list_ecmd_tables = NULL;
238 static struct tablelist *list_var_tables = NULL;
239 static struct tablelist *list_sysvar_tables = NULL;
240
241
242 /*
243 * Expand special key abbreviations in a command table.
244 */
245 static void
expand_special_keys(table,len)246 expand_special_keys(table, len)
247 char *table;
248 int len;
249 {
250 char *fm;
251 char *to;
252 int a;
253 char *repl;
254 int klen;
255
256 for (fm = table; fm < table + len; )
257 {
258 /*
259 * Rewrite each command in the table with any
260 * special key abbreviations expanded.
261 */
262 for (to = fm; *fm != '\0'; )
263 {
264 if (*fm != SK_SPECIAL_KEY)
265 {
266 *to++ = *fm++;
267 continue;
268 }
269 /*
270 * After SK_SPECIAL_KEY, next byte is the type
271 * of special key (one of the SK_* contants),
272 * and the byte after that is the number of bytes,
273 * N, reserved by the abbreviation (including the
274 * SK_SPECIAL_KEY and key type bytes).
275 * Replace all N bytes with the actual bytes
276 * output by the special key on this terminal.
277 */
278 repl = special_key_str(fm[1]);
279 klen = fm[2] & 0377;
280 fm += klen;
281 if (repl == NULL || (int) strlen(repl) > klen)
282 repl = "\377";
283 while (*repl != '\0')
284 *to++ = *repl++;
285 }
286 *to++ = '\0';
287 /*
288 * Fill any unused bytes between end of command and
289 * the action byte with A_SKIP.
290 */
291 while (to <= fm)
292 *to++ = A_SKIP;
293 fm++;
294 a = *fm++ & 0377;
295 if (a & A_EXTRA)
296 {
297 while (*fm++ != '\0')
298 continue;
299 }
300 }
301 }
302
303 /*
304 * Expand special key abbreviations in a list of command tables.
305 */
306 static void
expand_cmd_table(tlist)307 expand_cmd_table(tlist)
308 struct tablelist *tlist;
309 {
310 struct tablelist *t;
311 for (t = tlist; t != NULL; t = t->t_next)
312 {
313 expand_special_keys(t->t_start, t->t_end - t->t_start);
314 }
315 }
316
317 /*
318 * Expand special key abbreviations in all command tables.
319 */
320 public void
expand_cmd_tables(VOID_PARAM)321 expand_cmd_tables(VOID_PARAM)
322 {
323 expand_cmd_table(list_fcmd_tables);
324 expand_cmd_table(list_ecmd_tables);
325 expand_cmd_table(list_var_tables);
326 expand_cmd_table(list_sysvar_tables);
327 }
328
329
330 /*
331 * Initialize the command lists.
332 */
333 public void
init_cmds(VOID_PARAM)334 init_cmds(VOID_PARAM)
335 {
336 /*
337 * Add the default command tables.
338 */
339 add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
340 add_ecmd_table((char*)edittable, sizeof(edittable));
341 #if USERFILE
342 /*
343 * For backwards compatibility,
344 * try to add tables in the OLD system lesskey file.
345 */
346 #ifdef BINDIR
347 add_hometable(NULL, BINDIR "/.sysless", 1);
348 #endif
349 /*
350 * Try to add the tables in the system lesskey file.
351 */
352 add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
353 /*
354 * Try to add the tables in the standard lesskey file "$HOME/.less".
355 */
356 add_hometable("LESSKEY", LESSKEYFILE, 0);
357 #endif
358 }
359
360 /*
361 * Add a command table.
362 */
363 static int
add_cmd_table(tlist,buf,len)364 add_cmd_table(tlist, buf, len)
365 struct tablelist **tlist;
366 char *buf;
367 int len;
368 {
369 struct tablelist *t;
370
371 if (len == 0)
372 return (0);
373 /*
374 * Allocate a tablelist structure, initialize it,
375 * and link it into the list of tables.
376 */
377 if ((t = (struct tablelist *)
378 calloc(1, sizeof(struct tablelist))) == NULL)
379 {
380 return (-1);
381 }
382 t->t_start = buf;
383 t->t_end = buf + len;
384 t->t_next = *tlist;
385 *tlist = t;
386 return (0);
387 }
388
389 /*
390 * Add a command table.
391 */
392 public void
add_fcmd_table(buf,len)393 add_fcmd_table(buf, len)
394 char *buf;
395 int len;
396 {
397 if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
398 error("Warning: some commands disabled", NULL_PARG);
399 }
400
401 /*
402 * Add an editing command table.
403 */
404 public void
add_ecmd_table(buf,len)405 add_ecmd_table(buf, len)
406 char *buf;
407 int len;
408 {
409 if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
410 error("Warning: some edit commands disabled", NULL_PARG);
411 }
412
413 /*
414 * Add an environment variable table.
415 */
416 static void
add_var_table(tlist,buf,len)417 add_var_table(tlist, buf, len)
418 struct tablelist **tlist;
419 char *buf;
420 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_PARAM)430 mouse_wheel_down(VOID_PARAM)
431 {
432 return ((mousecap == OPT_ONPLUS) ? A_B_MOUSE : A_F_MOUSE);
433 }
434
435 /*
436 * Return action for a mouse wheel up event.
437 */
438 static int
mouse_wheel_up(VOID_PARAM)439 mouse_wheel_up(VOID_PARAM)
440 {
441 return ((mousecap == OPT_ONPLUS) ? A_F_MOUSE : A_B_MOUSE);
442 }
443
444 /*
445 * Return action for a mouse button release event.
446 */
447 static int
mouse_button_rel(x,y)448 mouse_button_rel(x, y)
449 int x;
450 int y;
451 {
452 /*
453 * {{ It would be better to return an action and then do this
454 * in commands() but it's nontrivial to pass y to it. }}
455 */
456 if (y < sc_height-1)
457 {
458 setmark('#', y);
459 screen_trashed = 1;
460 }
461 return (A_NOACTION);
462 }
463
464 /*
465 * Read a decimal integer. Return the integer and set *pterm to the terminating char.
466 */
467 static int
getcc_int(pterm)468 getcc_int(pterm)
469 char* pterm;
470 {
471 int num = 0;
472 int digits = 0;
473 for (;;)
474 {
475 char ch = getcc();
476 if (ch < '0' || ch > '9')
477 {
478 if (pterm != NULL) *pterm = ch;
479 if (digits == 0)
480 return (-1);
481 return (num);
482 }
483 num = (10 * num) + (ch - '0');
484 ++digits;
485 }
486 }
487
488 /*
489 * Read suffix of mouse input and return the action to take.
490 * The prefix ("\e[M") has already been read.
491 */
492 static int
x11mouse_action(skip)493 x11mouse_action(skip)
494 int skip;
495 {
496 int b = getcc() - X11MOUSE_OFFSET;
497 int x = getcc() - X11MOUSE_OFFSET-1;
498 int y = getcc() - X11MOUSE_OFFSET-1;
499 if (skip)
500 return (A_NOACTION);
501 switch (b) {
502 default:
503 return (A_NOACTION);
504 case X11MOUSE_WHEEL_DOWN:
505 return mouse_wheel_down();
506 case X11MOUSE_WHEEL_UP:
507 return mouse_wheel_up();
508 case X11MOUSE_BUTTON_REL:
509 return mouse_button_rel(x, y);
510 }
511 }
512
513 /*
514 * Read suffix of mouse input and return the action to take.
515 * The prefix ("\e[<") has already been read.
516 */
517 static int
x116mouse_action(skip)518 x116mouse_action(skip)
519 int skip;
520 {
521 char ch;
522 int x, y;
523 int b = getcc_int(&ch);
524 if (b < 0 || ch != ';') return (A_NOACTION);
525 x = getcc_int(&ch) - 1;
526 if (x < 0 || ch != ';') return (A_NOACTION);
527 y = getcc_int(&ch) - 1;
528 if (y < 0) return (A_NOACTION);
529 if (skip)
530 return (A_NOACTION);
531 switch (b) {
532 case X11MOUSE_WHEEL_DOWN:
533 return mouse_wheel_down();
534 case X11MOUSE_WHEEL_UP:
535 return mouse_wheel_up();
536 default:
537 if (ch != 'm') return (A_NOACTION);
538 return mouse_button_rel(x, y);
539 }
540 }
541
542 /*
543 * Search a single command table for the command string in cmd.
544 */
545 static int
cmd_search(cmd,table,endtable,sp)546 cmd_search(cmd, table, endtable, sp)
547 char *cmd;
548 char *table;
549 char *endtable;
550 char **sp;
551 {
552 char *p;
553 char *q;
554 int a;
555
556 *sp = NULL;
557 for (p = table, q = cmd; p < endtable; p++, q++)
558 {
559 if (*p == *q)
560 {
561 /*
562 * Current characters match.
563 * If we're at the end of the string, we've found it.
564 * Return the action code, which is the character
565 * after the null at the end of the string
566 * in the command table.
567 */
568 if (*p == '\0')
569 {
570 a = *++p & 0377;
571 while (a == A_SKIP)
572 a = *++p & 0377;
573 if (a == A_END_LIST)
574 {
575 /*
576 * We get here only if the original
577 * cmd string passed in was empty ("").
578 * I don't think that can happen,
579 * but just in case ...
580 */
581 return (A_UINVALID);
582 }
583 /*
584 * Check for an "extra" string.
585 */
586 if (a & A_EXTRA)
587 {
588 *sp = ++p;
589 a &= ~A_EXTRA;
590 }
591 if (a == A_X11MOUSE_IN)
592 a = x11mouse_action(0);
593 else if (a == A_X116MOUSE_IN)
594 a = x116mouse_action(0);
595 return (a);
596 }
597 } else if (*q == '\0')
598 {
599 /*
600 * Hit the end of the user's command,
601 * but not the end of the string in the command table.
602 * The user's command is incomplete.
603 */
604 return (A_PREFIX);
605 } else
606 {
607 /*
608 * Not a match.
609 * Skip ahead to the next command in the
610 * command table, and reset the pointer
611 * to the beginning of the user's command.
612 */
613 if (*p == '\0' && p[1] == A_END_LIST)
614 {
615 /*
616 * A_END_LIST is a special marker that tells
617 * us to abort the cmd search.
618 */
619 return (A_UINVALID);
620 }
621 while (*p++ != '\0')
622 continue;
623 while (*p == A_SKIP)
624 p++;
625 if (*p & A_EXTRA)
626 while (*++p != '\0')
627 continue;
628 q = cmd-1;
629 }
630 }
631 /*
632 * No match found in the entire command table.
633 */
634 return (A_INVALID);
635 }
636
637 /*
638 * Decode a command character and return the associated action.
639 * The "extra" string, if any, is returned in sp.
640 */
641 static int
cmd_decode(tlist,cmd,sp)642 cmd_decode(tlist, cmd, sp)
643 struct tablelist *tlist;
644 char *cmd;
645 char **sp;
646 {
647 struct tablelist *t;
648 int action = A_INVALID;
649
650 /*
651 * Search thru all the command tables.
652 * Stop when we find an action which is not A_INVALID.
653 */
654 for (t = tlist; t != NULL; t = t->t_next)
655 {
656 action = cmd_search(cmd, t->t_start, t->t_end, sp);
657 if (action != A_INVALID)
658 break;
659 }
660 if (action == A_UINVALID)
661 action = A_INVALID;
662 return (action);
663 }
664
665 /*
666 * Decode a command from the cmdtables list.
667 */
668 public int
fcmd_decode(cmd,sp)669 fcmd_decode(cmd, sp)
670 char *cmd;
671 char **sp;
672 {
673 return (cmd_decode(list_fcmd_tables, cmd, sp));
674 }
675
676 /*
677 * Decode a command from the edittables list.
678 */
679 public int
ecmd_decode(cmd,sp)680 ecmd_decode(cmd, sp)
681 char *cmd;
682 char **sp;
683 {
684 return (cmd_decode(list_ecmd_tables, cmd, sp));
685 }
686
687 /*
688 * Get the value of an environment variable.
689 * Looks first in the lesskey file, then in the real environment.
690 */
691 public char *
lgetenv(var)692 lgetenv(var)
693 char *var;
694 {
695 int a;
696 char *s;
697
698 a = cmd_decode(list_var_tables, var, &s);
699 if (a == EV_OK)
700 return (s);
701 s = getenv(var);
702 if (s != NULL && *s != '\0')
703 return (s);
704 a = cmd_decode(list_sysvar_tables, var, &s);
705 if (a == EV_OK)
706 return (s);
707 return (NULL);
708 }
709
710 /*
711 * Is a string null or empty?
712 */
713 public int
isnullenv(s)714 isnullenv(s)
715 char* s;
716 {
717 return (s == NULL || *s == '\0');
718 }
719
720 #if USERFILE
721 /*
722 * Get an "integer" from a lesskey file.
723 * Integers are stored in a funny format:
724 * two bytes, low order first, in radix KRADIX.
725 */
726 static int
gint(sp)727 gint(sp)
728 char **sp;
729 {
730 int n;
731
732 n = *(*sp)++;
733 n += *(*sp)++ * KRADIX;
734 return (n);
735 }
736
737 /*
738 * Process an old (pre-v241) lesskey file.
739 */
740 static int
old_lesskey(buf,len)741 old_lesskey(buf, len)
742 char *buf;
743 int len;
744 {
745 /*
746 * Old-style lesskey file.
747 * The file must end with either
748 * ...,cmd,0,action
749 * or ...,cmd,0,action|A_EXTRA,string,0
750 * So the last byte or the second to last byte must be zero.
751 */
752 if (buf[len-1] != '\0' && buf[len-2] != '\0')
753 return (-1);
754 add_fcmd_table(buf, len);
755 return (0);
756 }
757
758 /*
759 * Process a new (post-v241) lesskey file.
760 */
761 static int
new_lesskey(buf,len,sysvar)762 new_lesskey(buf, len, sysvar)
763 char *buf;
764 int len;
765 int sysvar;
766 {
767 char *p;
768 int c;
769 int n;
770
771 /*
772 * New-style lesskey file.
773 * Extract the pieces.
774 */
775 if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
776 buf[len-2] != C1_END_LESSKEY_MAGIC ||
777 buf[len-1] != C2_END_LESSKEY_MAGIC)
778 return (-1);
779 p = buf + 4;
780 for (;;)
781 {
782 c = *p++;
783 switch (c)
784 {
785 case CMD_SECTION:
786 n = gint(&p);
787 add_fcmd_table(p, n);
788 p += n;
789 break;
790 case EDIT_SECTION:
791 n = gint(&p);
792 add_ecmd_table(p, n);
793 p += n;
794 break;
795 case VAR_SECTION:
796 n = gint(&p);
797 add_var_table((sysvar) ?
798 &list_sysvar_tables : &list_var_tables, p, n);
799 p += n;
800 break;
801 case END_SECTION:
802 return (0);
803 default:
804 /*
805 * Unrecognized section type.
806 */
807 return (-1);
808 }
809 }
810 }
811
812 /*
813 * Set up a user command table, based on a "lesskey" file.
814 */
815 public int
lesskey(filename,sysvar)816 lesskey(filename, sysvar)
817 char *filename;
818 int sysvar;
819 {
820 char *buf;
821 POSITION len;
822 long n;
823 int f;
824
825 if (secure)
826 return (1);
827 /*
828 * Try to open the lesskey file.
829 */
830 f = open(filename, OPEN_READ);
831 if (f < 0)
832 return (1);
833
834 /*
835 * Read the file into a buffer.
836 * We first figure out the size of the file and allocate space for it.
837 * {{ Minimal error checking is done here.
838 * A garbage .less file will produce strange results.
839 * To avoid a large amount of error checking code here, we
840 * rely on the lesskey program to generate a good .less file. }}
841 */
842 len = filesize(f);
843 if (len == NULL_POSITION || len < 3)
844 {
845 /*
846 * Bad file (valid file must have at least 3 chars).
847 */
848 close(f);
849 return (-1);
850 }
851 if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL)
852 {
853 close(f);
854 return (-1);
855 }
856 if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
857 {
858 free(buf);
859 close(f);
860 return (-1);
861 }
862 n = read(f, buf, (unsigned int) len);
863 close(f);
864 if (n != len)
865 {
866 free(buf);
867 return (-1);
868 }
869
870 /*
871 * Figure out if this is an old-style (before version 241)
872 * or new-style lesskey file format.
873 */
874 if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
875 buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
876 return (old_lesskey(buf, (int)len));
877 return (new_lesskey(buf, (int)len, sysvar));
878 }
879
880 /*
881 * Add the standard lesskey file "$HOME/.less"
882 */
883 public void
add_hometable(envname,def_filename,sysvar)884 add_hometable(envname, def_filename, sysvar)
885 char *envname;
886 char *def_filename;
887 int sysvar;
888 {
889 char *filename;
890 PARG parg;
891
892 if (envname != NULL && (filename = lgetenv(envname)) != NULL)
893 filename = save(filename);
894 else if (sysvar)
895 filename = save(def_filename);
896 else
897 filename = homefile(def_filename);
898 if (filename == NULL)
899 return;
900 if (lesskey(filename, sysvar) < 0)
901 {
902 parg.p_string = filename;
903 error("Cannot use lesskey file \"%s\"", &parg);
904 }
905 free(filename);
906 }
907 #endif
908
909 /*
910 * See if a char is a special line-editing command.
911 */
912 public int
editchar(c,flags)913 editchar(c, flags)
914 int c;
915 int flags;
916 {
917 int action;
918 int nch;
919 char *s;
920 char usercmd[MAX_CMDLEN+1];
921
922 /*
923 * An editing character could actually be a sequence of characters;
924 * for example, an escape sequence sent by pressing the uparrow key.
925 * To match the editing string, we use the command decoder
926 * but give it the edit-commands command table
927 * This table is constructed to match the user's keyboard.
928 */
929 if (c == erase_char || c == erase2_char)
930 return (EC_BACKSPACE);
931 if (c == kill_char)
932 {
933 #if MSDOS_COMPILER==WIN32C
934 if (!win32_kbhit())
935 #endif
936 return (EC_LINEKILL);
937 }
938
939 /*
940 * Collect characters in a buffer.
941 * Start with the one we have, and get more if we need them.
942 */
943 nch = 0;
944 do {
945 if (nch > 0)
946 c = getcc();
947 usercmd[nch] = c;
948 usercmd[nch+1] = '\0';
949 nch++;
950 action = ecmd_decode(usercmd, &s);
951 } while (action == A_PREFIX && nch < MAX_CMDLEN);
952
953 if (action == EC_X11MOUSE)
954 return (x11mouse_action(1));
955 if (action == EC_X116MOUSE)
956 return (x116mouse_action(1));
957
958 if (flags & ECF_NORIGHTLEFT)
959 {
960 switch (action)
961 {
962 case EC_RIGHT:
963 case EC_LEFT:
964 action = A_INVALID;
965 break;
966 }
967 }
968 #if CMD_HISTORY
969 if (flags & ECF_NOHISTORY)
970 {
971 /*
972 * The caller says there is no history list.
973 * Reject any history-manipulation action.
974 */
975 switch (action)
976 {
977 case EC_UP:
978 case EC_DOWN:
979 action = A_INVALID;
980 break;
981 }
982 }
983 #endif
984 #if TAB_COMPLETE_FILENAME
985 if (flags & ECF_NOCOMPLETE)
986 {
987 /*
988 * The caller says we don't want any filename completion cmds.
989 * Reject them.
990 */
991 switch (action)
992 {
993 case EC_F_COMPLETE:
994 case EC_B_COMPLETE:
995 case EC_EXPAND:
996 action = A_INVALID;
997 break;
998 }
999 }
1000 #endif
1001 if ((flags & ECF_PEEK) || action == A_INVALID)
1002 {
1003 /*
1004 * We're just peeking, or we didn't understand the command.
1005 * Unget all the characters we read in the loop above.
1006 * This does NOT include the original character that was
1007 * passed in as a parameter.
1008 */
1009 while (nch > 1)
1010 {
1011 ungetcc(usercmd[--nch]);
1012 }
1013 } else
1014 {
1015 if (s != NULL)
1016 ungetsc(s);
1017 }
1018 return action;
1019 }
1020
1021