2 * Copyright (C) 1984-2019 Mark Nudelman
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.
7 * For more information, see the README file.
12 * Routines to decode user commands.
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.
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.
36 extern int erase_char, erase2_char, kill_char;
39 extern int screen_trashed;
43 SK_SPECIAL_KEY, (k), 6, 1, 1, 1
45 * Command table is ordered roughly according to expected
46 * frequency of use, so the common commands are near the beginning.
49 static unsigned char cmdtable[] =
55 SK(SK_DOWN_ARROW),0, A_F_LINE,
56 CONTROL('E'),0, A_F_LINE,
57 CONTROL('N'),0, A_F_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,
68 CONTROL('D'),0, A_F_SCROLL,
70 CONTROL('U'),0, A_B_SCROLL,
71 ESC,'[','M',0, A_X11MOUSE_IN,
72 ESC,'[','<',0, A_X116MOUSE_IN,
75 CONTROL('F'),0, A_F_SCREEN,
76 CONTROL('V'),0, A_F_SCREEN,
77 SK(SK_PAGE_DOWN),0, A_F_SCREEN,
79 CONTROL('B'),0, A_B_SCREEN,
80 ESC,'v',0, A_B_SCREEN,
81 SK(SK_PAGE_UP),0, A_B_SCREEN,
84 ESC,' ',0, A_FF_SCREEN,
86 ESC,'F',0, A_F_UNTIL_HILITE,
89 CONTROL('R'),0, A_REPAINT,
90 CONTROL('L'),0, A_REPAINT,
91 ESC,'u',0, A_UNDO_SEARCH,
93 SK(SK_HOME),0, A_GOLINE,
102 ESC,'{',0, A_LLSHIFT,
103 ESC,'}',0, A_RRSHIFT,
104 SK(SK_RIGHT_ARROW),0, A_RSHIFT,
105 SK(SK_LEFT_ARROW),0, A_LSHIFT,
106 SK(SK_CTL_RIGHT_ARROW),0, A_RRSHIFT,
107 SK(SK_CTL_LEFT_ARROW),0, A_LLSHIFT,
108 '{',0, A_F_BRACKET|A_EXTRA, '{','}',0,
109 '}',0, A_B_BRACKET|A_EXTRA, '{','}',0,
110 '(',0, A_F_BRACKET|A_EXTRA, '(',')',0,
111 ')',0, A_B_BRACKET|A_EXTRA, '(',')',0,
112 '[',0, A_F_BRACKET|A_EXTRA, '[',']',0,
113 ']',0, A_B_BRACKET|A_EXTRA, '[',']',0,
114 ESC,CONTROL('F'),0, A_F_BRACKET,
115 ESC,CONTROL('B'),0, A_B_BRACKET,
117 ESC,'G',0, A_GOEND_BUF,
120 SK(SK_END),0, A_GOEND,
136 CONTROL('G'),0, A_STAT,
140 ESC,'/',0, A_F_SEARCH|A_EXTRA, '*',0,
141 ESC,'?',0, A_B_SEARCH|A_EXTRA, '*',0,
142 'n',0, A_AGAIN_SEARCH,
143 ESC,'n',0, A_T_AGAIN_SEARCH,
144 'N',0, A_REVERSE_SEARCH,
145 ESC,'N',0, A_T_REVERSE_SEARCH,
149 ESC,'m',0, A_CLRMARK,
151 CONTROL('X'),CONTROL('X'),0, A_GOMARK,
153 ':','e',0, A_EXAMINE,
154 CONTROL('X'),CONTROL('V'),0, A_EXAMINE,
155 ':','n',0, A_NEXT_FILE,
156 ':','p',0, A_PREV_FILE,
159 ':','x',0, A_INDEX_FILE,
160 ':','d',0, A_REMOVE_FILE,
162 ':','t',0, A_OPT_TOGGLE|A_EXTRA, 't',0,
163 's',0, A_OPT_TOGGLE|A_EXTRA, 'o',0,
164 '_',0, A_DISP_OPTION,
181 static unsigned char edittable[] =
183 '\t',0, EC_F_COMPLETE, /* TAB */
184 '\17',0, EC_B_COMPLETE, /* BACKTAB */
185 SK(SK_BACKTAB),0, EC_B_COMPLETE, /* BACKTAB */
186 ESC,'\t',0, EC_B_COMPLETE, /* ESC TAB */
187 CONTROL('L'),0, EC_EXPAND, /* CTRL-L */
188 CONTROL('V'),0, EC_LITERAL, /* BACKSLASH */
189 CONTROL('A'),0, EC_LITERAL, /* BACKSLASH */
190 ESC,'l',0, EC_RIGHT, /* ESC l */
191 SK(SK_RIGHT_ARROW),0, EC_RIGHT, /* RIGHTARROW */
192 ESC,'h',0, EC_LEFT, /* ESC h */
193 SK(SK_LEFT_ARROW),0, EC_LEFT, /* LEFTARROW */
194 ESC,'b',0, EC_W_LEFT, /* ESC b */
195 ESC,SK(SK_LEFT_ARROW),0, EC_W_LEFT, /* ESC LEFTARROW */
196 SK(SK_CTL_LEFT_ARROW),0, EC_W_LEFT, /* CTRL-LEFTARROW */
197 ESC,'w',0, EC_W_RIGHT, /* ESC w */
198 ESC,SK(SK_RIGHT_ARROW),0, EC_W_RIGHT, /* ESC RIGHTARROW */
199 SK(SK_CTL_RIGHT_ARROW),0, EC_W_RIGHT, /* CTRL-RIGHTARROW */
200 ESC,'i',0, EC_INSERT, /* ESC i */
201 SK(SK_INSERT),0, EC_INSERT, /* INSERT */
202 ESC,'x',0, EC_DELETE, /* ESC x */
203 SK(SK_DELETE),0, EC_DELETE, /* DELETE */
204 ESC,'X',0, EC_W_DELETE, /* ESC X */
205 ESC,SK(SK_DELETE),0, EC_W_DELETE, /* ESC DELETE */
206 SK(SK_CTL_DELETE),0, EC_W_DELETE, /* CTRL-DELETE */
207 SK(SK_CTL_BACKSPACE),0, EC_W_BACKSPACE, /* CTRL-BACKSPACE */
208 ESC,'\b',0, EC_W_BACKSPACE, /* ESC BACKSPACE */
209 ESC,'0',0, EC_HOME, /* ESC 0 */
210 SK(SK_HOME),0, EC_HOME, /* HOME */
211 ESC,'$',0, EC_END, /* ESC $ */
212 SK(SK_END),0, EC_END, /* END */
213 ESC,'k',0, EC_UP, /* ESC k */
214 SK(SK_UP_ARROW),0, EC_UP, /* UPARROW */
215 ESC,'j',0, EC_DOWN, /* ESC j */
216 SK(SK_DOWN_ARROW),0, EC_DOWN, /* DOWNARROW */
217 CONTROL('G'),0, EC_ABORT, /* CTRL-G */
221 * Structure to support a list of command tables.
225 struct tablelist *t_next;
231 * List of command tables and list of line-edit tables.
233 static struct tablelist *list_fcmd_tables = NULL;
234 static struct tablelist *list_ecmd_tables = NULL;
235 static struct tablelist *list_var_tables = NULL;
236 static struct tablelist *list_sysvar_tables = NULL;
240 * Expand special key abbreviations in a command table.
243 expand_special_keys(table, len)
253 for (fm = table; fm < table + len; )
256 * Rewrite each command in the table with any
257 * special key abbreviations expanded.
259 for (to = fm; *fm != '\0'; )
261 if (*fm != SK_SPECIAL_KEY)
267 * After SK_SPECIAL_KEY, next byte is the type
268 * of special key (one of the SK_* contants),
269 * and the byte after that is the number of bytes,
270 * N, reserved by the abbreviation (including the
271 * SK_SPECIAL_KEY and key type bytes).
272 * Replace all N bytes with the actual bytes
273 * output by the special key on this terminal.
275 repl = special_key_str(fm[1]);
278 if (repl == NULL || (int) strlen(repl) > klen)
280 while (*repl != '\0')
285 * Fill any unused bytes between end of command and
286 * the action byte with A_SKIP.
294 while (*fm++ != '\0')
301 * Expand special key abbreviations in a list of command tables.
304 expand_cmd_table(tlist)
305 struct tablelist *tlist;
308 for (t = tlist; t != NULL; t = t->t_next)
310 expand_special_keys(t->t_start, t->t_end - t->t_start);
315 * Expand special key abbreviations in all command tables.
318 expand_cmd_tables(VOID_PARAM)
320 expand_cmd_table(list_fcmd_tables);
321 expand_cmd_table(list_ecmd_tables);
322 expand_cmd_table(list_var_tables);
323 expand_cmd_table(list_sysvar_tables);
328 * Initialize the command lists.
331 init_cmds(VOID_PARAM)
334 * Add the default command tables.
336 add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
337 add_ecmd_table((char*)edittable, sizeof(edittable));
340 * For backwards compatibility,
341 * try to add tables in the OLD system lesskey file.
344 add_hometable(NULL, BINDIR "/.sysless", 1);
347 * Try to add the tables in the system lesskey file.
349 add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
351 * Try to add the tables in the standard lesskey file "$HOME/.less".
353 add_hometable("LESSKEY", LESSKEYFILE, 0);
358 * Add a command table.
361 add_cmd_table(tlist, buf, len)
362 struct tablelist **tlist;
371 * Allocate a tablelist structure, initialize it,
372 * and link it into the list of tables.
374 if ((t = (struct tablelist *)
375 calloc(1, sizeof(struct tablelist))) == NULL)
380 t->t_end = buf + len;
387 * Add a command table.
390 add_fcmd_table(buf, len)
394 if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
395 error("Warning: some commands disabled", NULL_PARG);
399 * Add an editing command table.
402 add_ecmd_table(buf, len)
406 if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
407 error("Warning: some edit commands disabled", NULL_PARG);
411 * Add an environment variable table.
414 add_var_table(tlist, buf, len)
415 struct tablelist **tlist;
419 if (add_cmd_table(tlist, buf, len) < 0)
420 error("Warning: environment variables from lesskey file unavailable", NULL_PARG);
424 * Return action for a mouse wheel down event.
427 mouse_wheel_down(VOID_PARAM)
429 return ((mousecap == OPT_ONPLUS) ? A_B_MOUSE : A_F_MOUSE);
433 * Return action for a mouse wheel up event.
436 mouse_wheel_up(VOID_PARAM)
438 return ((mousecap == OPT_ONPLUS) ? A_F_MOUSE : A_B_MOUSE);
442 * Return action for a mouse button release event.
445 mouse_button_rel(x, y)
450 * {{ It would be better to return an action and then do this
451 * in commands() but it's nontrivial to pass y to it. }}
462 * Read a decimal integer. Return the integer and set *pterm to the terminating char.
473 if (ch < '0' || ch > '9')
475 if (pterm != NULL) *pterm = ch;
480 num = (10 * num) + (ch - '0');
486 * Read suffix of mouse input and return the action to take.
487 * The prefix ("\e[M") has already been read.
490 x11mouse_action(VOID_PARAM)
492 int b = getcc() - X11MOUSE_OFFSET;
493 int x = getcc() - X11MOUSE_OFFSET-1;
494 int y = getcc() - X11MOUSE_OFFSET-1;
498 case X11MOUSE_WHEEL_DOWN:
499 return mouse_wheel_down();
500 case X11MOUSE_WHEEL_UP:
501 return mouse_wheel_up();
502 case X11MOUSE_BUTTON_REL:
503 return mouse_button_rel(x, y);
508 * Read suffix of mouse input and return the action to take.
509 * The prefix ("\e[<") has already been read.
512 x116mouse_action(VOID_PARAM)
516 int b = getcc_int(&ch);
517 if (b < 0 || ch != ';') return (A_NOACTION);
518 x = getcc_int(&ch) - 1;
519 if (x < 0 || ch != ';') return (A_NOACTION);
520 y = getcc_int(&ch) - 1;
521 if (y < 0) return (A_NOACTION);
523 case X11MOUSE_WHEEL_DOWN:
524 return mouse_wheel_down();
525 case X11MOUSE_WHEEL_UP:
526 return mouse_wheel_up();
528 if (ch != 'm') return (A_NOACTION);
529 return mouse_button_rel(x, y);
534 * Search a single command table for the command string in cmd.
537 cmd_search(cmd, table, endtable, sp)
548 for (p = table, q = cmd; p < endtable; p++, q++)
553 * Current characters match.
554 * If we're at the end of the string, we've found it.
555 * Return the action code, which is the character
556 * after the null at the end of the string
557 * in the command table.
567 * We get here only if the original
568 * cmd string passed in was empty ("").
569 * I don't think that can happen,
570 * but just in case ...
575 * Check for an "extra" string.
582 if (a == A_X11MOUSE_IN)
583 a = x11mouse_action();
584 else if (a == A_X116MOUSE_IN)
585 a = x116mouse_action();
588 } else if (*q == '\0')
591 * Hit the end of the user's command,
592 * but not the end of the string in the command table.
593 * The user's command is incomplete.
600 * Skip ahead to the next command in the
601 * command table, and reset the pointer
602 * to the beginning of the user's command.
604 if (*p == '\0' && p[1] == A_END_LIST)
607 * A_END_LIST is a special marker that tells
608 * us to abort the cmd search.
623 * No match found in the entire command table.
629 * Decode a command character and return the associated action.
630 * The "extra" string, if any, is returned in sp.
633 cmd_decode(tlist, cmd, sp)
634 struct tablelist *tlist;
639 int action = A_INVALID;
642 * Search thru all the command tables.
643 * Stop when we find an action which is not A_INVALID.
645 for (t = tlist; t != NULL; t = t->t_next)
647 action = cmd_search(cmd, t->t_start, t->t_end, sp);
648 if (action != A_INVALID)
651 if (action == A_UINVALID)
657 * Decode a command from the cmdtables list.
664 return (cmd_decode(list_fcmd_tables, cmd, sp));
668 * Decode a command from the edittables list.
675 return (cmd_decode(list_ecmd_tables, cmd, sp));
679 * Get the value of an environment variable.
680 * Looks first in the lesskey file, then in the real environment.
689 a = cmd_decode(list_var_tables, var, &s);
693 if (s != NULL && *s != '\0')
695 a = cmd_decode(list_sysvar_tables, var, &s);
702 * Is a string null or empty?
708 return (s == NULL || *s == '\0');
713 * Get an "integer" from a lesskey file.
714 * Integers are stored in a funny format:
715 * two bytes, low order first, in radix KRADIX.
724 n += *(*sp)++ * KRADIX;
729 * Process an old (pre-v241) lesskey file.
732 old_lesskey(buf, len)
737 * Old-style lesskey file.
738 * The file must end with either
740 * or ...,cmd,0,action|A_EXTRA,string,0
741 * So the last byte or the second to last byte must be zero.
743 if (buf[len-1] != '\0' && buf[len-2] != '\0')
745 add_fcmd_table(buf, len);
750 * Process a new (post-v241) lesskey file.
753 new_lesskey(buf, len, sysvar)
763 * New-style lesskey file.
764 * Extract the pieces.
766 if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
767 buf[len-2] != C1_END_LESSKEY_MAGIC ||
768 buf[len-1] != C2_END_LESSKEY_MAGIC)
778 add_fcmd_table(p, n);
783 add_ecmd_table(p, n);
788 add_var_table((sysvar) ?
789 &list_sysvar_tables : &list_var_tables, p, n);
796 * Unrecognized section type.
804 * Set up a user command table, based on a "lesskey" file.
807 lesskey(filename, sysvar)
819 * Try to open the lesskey file.
821 f = open(filename, OPEN_READ);
826 * Read the file into a buffer.
827 * We first figure out the size of the file and allocate space for it.
828 * {{ Minimal error checking is done here.
829 * A garbage .less file will produce strange results.
830 * To avoid a large amount of error checking code here, we
831 * rely on the lesskey program to generate a good .less file. }}
834 if (len == NULL_POSITION || len < 3)
837 * Bad file (valid file must have at least 3 chars).
842 if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL)
847 if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
853 n = read(f, buf, (unsigned int) len);
862 * Figure out if this is an old-style (before version 241)
863 * or new-style lesskey file format.
865 if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
866 buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
867 return (old_lesskey(buf, (int)len));
868 return (new_lesskey(buf, (int)len, sysvar));
872 * Add the standard lesskey file "$HOME/.less"
875 add_hometable(envname, def_filename, sysvar)
883 if (envname != NULL && (filename = lgetenv(envname)) != NULL)
884 filename = save(filename);
886 filename = save(def_filename);
888 filename = homefile(def_filename);
889 if (filename == NULL)
891 if (lesskey(filename, sysvar) < 0)
893 parg.p_string = filename;
894 error("Cannot use lesskey file \"%s\"", &parg);
901 * See if a char is a special line-editing command.
911 char usercmd[MAX_CMDLEN+1];
914 * An editing character could actually be a sequence of characters;
915 * for example, an escape sequence sent by pressing the uparrow key.
916 * To match the editing string, we use the command decoder
917 * but give it the edit-commands command table
918 * This table is constructed to match the user's keyboard.
920 if (c == erase_char || c == erase2_char)
921 return (EC_BACKSPACE);
924 #if MSDOS_COMPILER==WIN32C
928 return (EC_LINEKILL);
932 * Collect characters in a buffer.
933 * Start with the one we have, and get more if we need them.
940 usercmd[nch+1] = '\0';
942 action = ecmd_decode(usercmd, &s);
943 } while (action == A_PREFIX);
945 if (flags & EC_NORIGHTLEFT)
956 if (flags & EC_NOHISTORY)
959 * The caller says there is no history list.
960 * Reject any history-manipulation action.
971 #if TAB_COMPLETE_FILENAME
972 if (flags & EC_NOCOMPLETE)
975 * The caller says we don't want any filename completion cmds.
988 if ((flags & EC_PEEK) || action == A_INVALID)
991 * We're just peeking, or we didn't understand the command.
992 * Unget all the characters we read in the loop above.
993 * This does NOT include the original character that was
994 * passed in as a parameter.
998 ungetcc(usercmd[--nch]);