2 * Copyright (C) 1984-2022 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,
92 ESC,'U',0, A_CLR_SEARCH,
94 SK(SK_HOME),0, A_GOLINE,
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,
118 ESC,'G',0, A_GOEND_BUF,
121 SK(SK_END),0, A_GOEND,
137 CONTROL('G'),0, A_STAT,
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,
150 ESC,'m',0, A_CLRMARK,
152 CONTROL('X'),CONTROL('X'),0, A_GOMARK,
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,
160 ':','x',0, A_INDEX_FILE,
161 ':','d',0, A_REMOVE_FILE,
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,
182 static unsigned char edittable[] =
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,SK(SK_BACKSPACE),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 */
224 * Structure to support a list of command tables.
228 struct tablelist *t_next;
234 * List of command tables and list of line-edit tables.
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;
243 * Expand special key abbreviations in a command table.
246 expand_special_keys(table, len)
256 for (fm = table; fm < table + len; )
259 * Rewrite each command in the table with any
260 * special key abbreviations expanded.
262 for (to = fm; *fm != '\0'; )
264 if (*fm != SK_SPECIAL_KEY)
270 * After SK_SPECIAL_KEY, next byte is the type
271 * of special key (one of the SK_* constants),
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.
278 repl = special_key_str(fm[1]);
281 if (repl == NULL || (int) strlen(repl) > klen)
283 while (*repl != '\0')
288 * Fill any unused bytes between end of command and
289 * the action byte with A_SKIP.
297 while (*fm++ != '\0')
304 * Expand special key abbreviations in a list of command tables.
307 expand_cmd_table(tlist)
308 struct tablelist *tlist;
311 for (t = tlist; t != NULL; t = t->t_next)
313 expand_special_keys(t->t_start, t->t_end - t->t_start);
318 * Expand special key abbreviations in all command tables.
321 expand_cmd_tables(VOID_PARAM)
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);
331 * Initialize the command lists.
334 init_cmds(VOID_PARAM)
337 * Add the default command tables.
339 add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
340 add_ecmd_table((char*)edittable, sizeof(edittable));
342 #ifdef BINDIR /* For backwards compatibility */
343 /* Try to add tables in the OLD system lesskey file. */
344 add_hometable(lesskey, NULL, BINDIR "/.sysless", 1);
347 * Try to load lesskey source file or binary file.
348 * If the source file succeeds, don't load binary file.
349 * The binary file is likely to have been generated from
350 * a (possibly out of date) copy of the src file,
351 * so loading it is at best redundant.
354 * Try to add tables in system lesskey src file.
357 if (add_hometable(lesskey_src, "LESSKEYIN_SYSTEM", LESSKEYINFILE_SYS, 1) != 0)
361 * Try to add the tables in the system lesskey binary file.
363 add_hometable(lesskey, "LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
366 * Try to add tables in the lesskey src file "$HOME/.lesskey".
369 if (add_hometable(lesskey_src, "LESSKEYIN", DEF_LESSKEYINFILE, 0) != 0)
373 * Try to add the tables in the standard lesskey binary file "$HOME/.less".
375 add_hometable(lesskey, "LESSKEY", LESSKEYFILE, 0);
381 * Add a command table.
384 add_cmd_table(tlist, buf, len)
385 struct tablelist **tlist;
394 * Allocate a tablelist structure, initialize it,
395 * and link it into the list of tables.
397 if ((t = (struct tablelist *)
398 calloc(1, sizeof(struct tablelist))) == NULL)
403 t->t_end = buf + len;
410 * Add a command table.
413 add_fcmd_table(buf, len)
417 if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
418 error("Warning: some commands disabled", NULL_PARG);
422 * Add an editing command table.
425 add_ecmd_table(buf, len)
429 if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
430 error("Warning: some edit commands disabled", NULL_PARG);
434 * Add an environment variable table.
437 add_var_table(tlist, buf, len)
438 struct tablelist **tlist;
442 if (add_cmd_table(tlist, buf, len) < 0)
443 error("Warning: environment variables from lesskey file unavailable", NULL_PARG);
447 * Return action for a mouse wheel down event.
450 mouse_wheel_down(VOID_PARAM)
452 return ((mousecap == OPT_ONPLUS) ? A_B_MOUSE : A_F_MOUSE);
456 * Return action for a mouse wheel up event.
459 mouse_wheel_up(VOID_PARAM)
461 return ((mousecap == OPT_ONPLUS) ? A_F_MOUSE : A_B_MOUSE);
465 * Return action for a mouse button release event.
468 mouse_button_rel(x, y)
473 * {{ It would be better to return an action and then do this
474 * in commands() but it's nontrivial to pass y to it. }}
485 * Read a decimal integer. Return the integer and set *pterm to the terminating char.
496 if (ch < '0' || ch > '9')
498 if (pterm != NULL) *pterm = ch;
503 num = (10 * num) + (ch - '0');
509 * Read suffix of mouse input and return the action to take.
510 * The prefix ("\e[M") has already been read.
513 x11mouse_action(skip)
516 int b = getcc() - X11MOUSE_OFFSET;
517 int x = getcc() - X11MOUSE_OFFSET-1;
518 int y = getcc() - X11MOUSE_OFFSET-1;
524 case X11MOUSE_WHEEL_DOWN:
525 return mouse_wheel_down();
526 case X11MOUSE_WHEEL_UP:
527 return mouse_wheel_up();
528 case X11MOUSE_BUTTON_REL:
529 return mouse_button_rel(x, y);
534 * Read suffix of mouse input and return the action to take.
535 * The prefix ("\e[<") has already been read.
538 x116mouse_action(skip)
543 int b = getcc_int(&ch);
544 if (b < 0 || ch != ';') return (A_NOACTION);
545 x = getcc_int(&ch) - 1;
546 if (x < 0 || ch != ';') return (A_NOACTION);
547 y = getcc_int(&ch) - 1;
548 if (y < 0) return (A_NOACTION);
552 case X11MOUSE_WHEEL_DOWN:
553 return mouse_wheel_down();
554 case X11MOUSE_WHEEL_UP:
555 return mouse_wheel_up();
557 if (ch != 'm') return (A_NOACTION);
558 return mouse_button_rel(x, y);
563 * Search a single command table for the command string in cmd.
566 cmd_search(cmd, table, endtable, sp)
577 for (p = table, q = cmd; p < endtable; p++, q++)
582 * Current characters match.
583 * If we're at the end of the string, we've found it.
584 * Return the action code, which is the character
585 * after the null at the end of the string
586 * in the command table.
596 * We get here only if the original
597 * cmd string passed in was empty ("").
598 * I don't think that can happen,
599 * but just in case ...
604 * Check for an "extra" string.
611 if (a == A_X11MOUSE_IN)
612 a = x11mouse_action(0);
613 else if (a == A_X116MOUSE_IN)
614 a = x116mouse_action(0);
617 } else if (*q == '\0')
620 * Hit the end of the user's command,
621 * but not the end of the string in the command table.
622 * The user's command is incomplete.
629 * Skip ahead to the next command in the
630 * command table, and reset the pointer
631 * to the beginning of the user's command.
633 if (*p == '\0' && p[1] == A_END_LIST)
636 * A_END_LIST is a special marker that tells
637 * us to abort the cmd search.
652 * No match found in the entire command table.
658 * Decode a command character and return the associated action.
659 * The "extra" string, if any, is returned in sp.
662 cmd_decode(tlist, cmd, sp)
663 struct tablelist *tlist;
668 int action = A_INVALID;
671 * Search thru all the command tables.
672 * Stop when we find an action which is not A_INVALID.
674 for (t = tlist; t != NULL; t = t->t_next)
676 action = cmd_search(cmd, t->t_start, t->t_end, sp);
677 if (action != A_INVALID)
680 if (action == A_UINVALID)
686 * Decode a command from the cmdtables list.
693 return (cmd_decode(list_fcmd_tables, cmd, sp));
697 * Decode a command from the edittables list.
704 return (cmd_decode(list_ecmd_tables, cmd, sp));
708 * Get the value of an environment variable.
709 * Looks first in the lesskey file, then in the real environment.
718 a = cmd_decode(list_var_tables, var, &s);
722 if (s != NULL && *s != '\0')
724 a = cmd_decode(list_sysvar_tables, var, &s);
731 * Is a string null or empty?
737 return (s == NULL || *s == '\0');
742 * Get an "integer" from a lesskey file.
743 * Integers are stored in a funny format:
744 * two bytes, low order first, in radix KRADIX.
753 n += *(*sp)++ * KRADIX;
758 * Process an old (pre-v241) lesskey file.
761 old_lesskey(buf, len)
766 * Old-style lesskey file.
767 * The file must end with either
769 * or ...,cmd,0,action|A_EXTRA,string,0
770 * So the last byte or the second to last byte must be zero.
772 if (buf[len-1] != '\0' && buf[len-2] != '\0')
774 add_fcmd_table(buf, len);
779 * Process a new (post-v241) lesskey file.
782 new_lesskey(buf, len, sysvar)
793 * New-style lesskey file.
794 * Extract the pieces.
796 if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
797 buf[len-2] != C1_END_LESSKEY_MAGIC ||
798 buf[len-1] != C2_END_LESSKEY_MAGIC)
809 if (n < 0 || p+n >= end)
811 add_fcmd_table(p, n);
816 if (n < 0 || p+n >= end)
818 add_ecmd_table(p, n);
823 if (n < 0 || p+n >= end)
825 add_var_table((sysvar) ?
826 &list_sysvar_tables : &list_var_tables, p, n);
833 * Unrecognized section type.
841 * Set up a user command table, based on a "lesskey" file.
844 lesskey(filename, sysvar)
856 * Try to open the lesskey file.
858 f = open(filename, OPEN_READ);
863 * Read the file into a buffer.
864 * We first figure out the size of the file and allocate space for it.
865 * {{ Minimal error checking is done here.
866 * A garbage .less file will produce strange results.
867 * To avoid a large amount of error checking code here, we
868 * rely on the lesskey program to generate a good .less file. }}
871 if (len == NULL_POSITION || len < 3)
874 * Bad file (valid file must have at least 3 chars).
879 if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL)
884 if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
890 n = read(f, buf, (unsigned int) len);
899 * Figure out if this is an old-style (before version 241)
900 * or new-style lesskey file format.
903 buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
904 buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
905 return (old_lesskey(buf, (int)len));
906 return (new_lesskey(buf, (int)len, sysvar));
911 lesskey_src(filename, sysvar)
915 static struct lesskey_tables tables;
916 int r = parse_lesskey(filename, &tables);
919 add_fcmd_table(tables.cmdtable.buf.data, tables.cmdtable.buf.end);
920 add_ecmd_table(tables.edittable.buf.data, tables.edittable.buf.end);
921 add_var_table(sysvar ? &list_sysvar_tables : &list_var_tables,
922 tables.vartable.buf.data, tables.vartable.buf.end);
927 lesskey_parse_error(s)
934 #endif /* HAVE_LESSKEYSRC */
937 * Add a lesskey file.
940 add_hometable(call_lesskey, envname, def_filename, sysvar)
941 int (*call_lesskey)(char *, int);
949 if (envname != NULL && (filename = lgetenv(envname)) != NULL)
950 filename = save(filename);
951 else if (sysvar) /* def_filename is full path */
952 filename = save(def_filename);
953 else /* def_filename is just basename */
955 /* Remove first char (normally a dot) unless stored in $HOME. */
956 char *xdg = lgetenv("XDG_CONFIG_HOME");
958 filename = dirfile(xdg, &def_filename[1], 1);
959 if (filename == NULL)
961 char *home = lgetenv("HOME");
962 if (!isnullenv(home))
964 char *cfg_dir = dirfile(home, ".config", 0);
965 filename = dirfile(cfg_dir, &def_filename[1], 1);
969 if (filename == NULL)
970 filename = homefile(def_filename);
972 if (filename == NULL)
974 r = (*call_lesskey)(filename, sysvar);
981 * See if a char is a special line-editing command.
991 char usercmd[MAX_CMDLEN+1];
994 * An editing character could actually be a sequence of characters;
995 * for example, an escape sequence sent by pressing the uparrow key.
996 * To match the editing string, we use the command decoder
997 * but give it the edit-commands command table
998 * This table is constructed to match the user's keyboard.
1000 if (c == erase_char || c == erase2_char)
1001 return (EC_BACKSPACE);
1004 #if MSDOS_COMPILER==WIN32C
1007 return (EC_LINEKILL);
1011 * Collect characters in a buffer.
1012 * Start with the one we have, and get more if we need them.
1019 usercmd[nch+1] = '\0';
1021 action = ecmd_decode(usercmd, &s);
1022 } while (action == A_PREFIX && nch < MAX_CMDLEN);
1024 if (action == EC_X11MOUSE)
1025 return (x11mouse_action(1));
1026 if (action == EC_X116MOUSE)
1027 return (x116mouse_action(1));
1029 if (flags & ECF_NORIGHTLEFT)
1040 if (flags & ECF_NOHISTORY)
1043 * The caller says there is no history list.
1044 * Reject any history-manipulation action.
1055 #if TAB_COMPLETE_FILENAME
1056 if (flags & ECF_NOCOMPLETE)
1059 * The caller says we don't want any filename completion cmds.
1072 if ((flags & ECF_PEEK) || action == A_INVALID)
1075 * We're just peeking, or we didn't understand the command.
1076 * Unget all the characters we read in the loop above.
1077 * This does NOT include the original character that was
1078 * passed in as a parameter.
1082 ungetcc(usercmd[--nch]);