2 * Copyright (C) 1984-2007 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 about less, or for information on how to
8 * contact the author, see the README file.
13 * Routines to decode user commands.
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.
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.
37 extern int erase_char
, erase2_char
, kill_char
;
41 SK_SPECIAL_KEY, (k), 6, 1, 1, 1
43 * Command table is ordered roughly according to expected
44 * frequency of use, so the common commands are near the beginning.
47 static unsigned char cmdtable
[] =
53 SK(SK_DOWN_ARROW
),0, A_F_LINE
,
54 CONTROL('E'),0, A_F_LINE
,
55 CONTROL('N'),0, A_F_LINE
,
58 CONTROL('Y'),0, A_B_LINE
,
59 SK(SK_CONTROL_K
),0, A_B_LINE
,
60 CONTROL('P'),0, A_B_LINE
,
61 SK(SK_UP_ARROW
),0, A_B_LINE
,
66 CONTROL('D'),0, A_F_SCROLL
,
68 CONTROL('U'),0, A_B_SCROLL
,
71 CONTROL('F'),0, A_F_SCREEN
,
72 CONTROL('V'),0, A_F_SCREEN
,
73 SK(SK_PAGE_DOWN
),0, A_F_SCREEN
,
75 CONTROL('B'),0, A_B_SCREEN
,
76 ESC
,'v',0, A_B_SCREEN
,
77 SK(SK_PAGE_UP
),0, A_B_SCREEN
,
80 ESC
,' ',0, A_FF_SCREEN
,
84 CONTROL('R'),0, A_REPAINT
,
85 CONTROL('L'),0, A_REPAINT
,
86 ESC
,'u',0, A_UNDO_SEARCH
,
88 SK(SK_HOME
),0, A_GOLINE
,
97 SK(SK_RIGHT_ARROW
),0, A_RSHIFT
,
98 SK(SK_LEFT_ARROW
),0, A_LSHIFT
,
99 '{',0, A_F_BRACKET
|A_EXTRA
, '{','}',0,
100 '}',0, A_B_BRACKET
|A_EXTRA
, '{','}',0,
101 '(',0, A_F_BRACKET
|A_EXTRA
, '(',')',0,
102 ')',0, A_B_BRACKET
|A_EXTRA
, '(',')',0,
103 '[',0, A_F_BRACKET
|A_EXTRA
, '[',']',0,
104 ']',0, A_B_BRACKET
|A_EXTRA
, '[',']',0,
105 ESC
,CONTROL('F'),0, A_F_BRACKET
,
106 ESC
,CONTROL('B'),0, A_B_BRACKET
,
110 SK(SK_END
),0, A_GOEND
,
126 CONTROL('G'),0, A_STAT
,
130 ESC
,'/',0, A_F_SEARCH
|A_EXTRA
, '*',0,
131 ESC
,'?',0, A_B_SEARCH
|A_EXTRA
, '*',0,
132 'n',0, A_AGAIN_SEARCH
,
133 ESC
,'n',0, A_T_AGAIN_SEARCH
,
134 'N',0, A_REVERSE_SEARCH
,
135 ESC
,'N',0, A_T_REVERSE_SEARCH
,
138 CONTROL('X'),CONTROL('X'),0, A_GOMARK
,
140 ':','e',0, A_EXAMINE
,
141 CONTROL('X'),CONTROL('V'),0, A_EXAMINE
,
142 ':','n',0, A_NEXT_FILE
,
143 ':','p',0, A_PREV_FILE
,
146 ':','x',0, A_INDEX_FILE
,
147 ':','d',0, A_REMOVE_FILE
,
149 ':','t',0, A_OPT_TOGGLE
|A_EXTRA
, 't',0,
150 's',0, A_OPT_TOGGLE
|A_EXTRA
, 'o',0,
151 '_',0, A_DISP_OPTION
,
168 static unsigned char edittable
[] =
170 '\t',0, EC_F_COMPLETE
, /* TAB */
171 '\17',0, EC_B_COMPLETE
, /* BACKTAB */
172 SK(SK_BACKTAB
),0, EC_B_COMPLETE
, /* BACKTAB */
173 ESC
,'\t',0, EC_B_COMPLETE
, /* ESC TAB */
174 CONTROL('L'),0, EC_EXPAND
, /* CTRL-L */
175 CONTROL('V'),0, EC_LITERAL
, /* BACKSLASH */
176 CONTROL('A'),0, EC_LITERAL
, /* BACKSLASH */
177 ESC
,'l',0, EC_RIGHT
, /* ESC l */
178 SK(SK_RIGHT_ARROW
),0, EC_RIGHT
, /* RIGHTARROW */
179 ESC
,'h',0, EC_LEFT
, /* ESC h */
180 SK(SK_LEFT_ARROW
),0, EC_LEFT
, /* LEFTARROW */
181 ESC
,'b',0, EC_W_LEFT
, /* ESC b */
182 ESC
,SK(SK_LEFT_ARROW
),0, EC_W_LEFT
, /* ESC LEFTARROW */
183 SK(SK_CTL_LEFT_ARROW
),0, EC_W_LEFT
, /* CTRL-LEFTARROW */
184 ESC
,'w',0, EC_W_RIGHT
, /* ESC w */
185 ESC
,SK(SK_RIGHT_ARROW
),0, EC_W_RIGHT
, /* ESC RIGHTARROW */
186 SK(SK_CTL_RIGHT_ARROW
),0, EC_W_RIGHT
, /* CTRL-RIGHTARROW */
187 ESC
,'i',0, EC_INSERT
, /* ESC i */
188 SK(SK_INSERT
),0, EC_INSERT
, /* INSERT */
189 ESC
,'x',0, EC_DELETE
, /* ESC x */
190 SK(SK_DELETE
),0, EC_DELETE
, /* DELETE */
191 ESC
,'X',0, EC_W_DELETE
, /* ESC X */
192 ESC
,SK(SK_DELETE
),0, EC_W_DELETE
, /* ESC DELETE */
193 SK(SK_CTL_DELETE
),0, EC_W_DELETE
, /* CTRL-DELETE */
194 SK(SK_CTL_BACKSPACE
),0, EC_W_BACKSPACE
, /* CTRL-BACKSPACE */
195 ESC
,'\b',0, EC_W_BACKSPACE
, /* ESC BACKSPACE */
196 ESC
,'0',0, EC_HOME
, /* ESC 0 */
197 SK(SK_HOME
),0, EC_HOME
, /* HOME */
198 ESC
,'$',0, EC_END
, /* ESC $ */
199 SK(SK_END
),0, EC_END
, /* END */
200 ESC
,'k',0, EC_UP
, /* ESC k */
201 SK(SK_UP_ARROW
),0, EC_UP
, /* UPARROW */
202 ESC
,'j',0, EC_DOWN
, /* ESC j */
203 SK(SK_DOWN_ARROW
),0, EC_DOWN
, /* DOWNARROW */
207 * Structure to support a list of command tables.
211 struct tablelist
*t_next
;
217 * List of command tables and list of line-edit tables.
219 static struct tablelist
*list_fcmd_tables
= NULL
;
220 static struct tablelist
*list_ecmd_tables
= NULL
;
221 static struct tablelist
*list_var_tables
= NULL
;
222 static struct tablelist
*list_sysvar_tables
= NULL
;
226 * Expand special key abbreviations in a command table.
229 expand_special_keys(table
, len
)
239 for (fm
= table
; fm
< table
+ len
; )
242 * Rewrite each command in the table with any
243 * special key abbreviations expanded.
245 for (to
= fm
; *fm
!= '\0'; )
247 if (*fm
!= SK_SPECIAL_KEY
)
253 * After SK_SPECIAL_KEY, next byte is the type
254 * of special key (one of the SK_* contants),
255 * and the byte after that is the number of bytes,
256 * N, reserved by the abbreviation (including the
257 * SK_SPECIAL_KEY and key type bytes).
258 * Replace all N bytes with the actual bytes
259 * output by the special key on this terminal.
261 repl
= special_key_str(fm
[1]);
264 if (repl
== NULL
|| (int) strlen(repl
) > klen
)
266 while (*repl
!= '\0')
271 * Fill any unused bytes between end of command and
272 * the action byte with A_SKIP.
280 while (*fm
++ != '\0')
287 * Initialize the command lists.
293 * Add the default command tables.
295 add_fcmd_table((char*)cmdtable
, sizeof(cmdtable
));
296 add_ecmd_table((char*)edittable
, sizeof(edittable
));
299 * For backwards compatibility,
300 * try to add tables in the OLD system lesskey file.
303 add_hometable(NULL
, BINDIR
"/.sysless", 1);
306 * Try to add the tables in the system lesskey file.
308 add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS
, 1);
310 * Try to add the tables in the standard lesskey file "$HOME/.less".
312 add_hometable("LESSKEY", LESSKEYFILE
, 0);
317 * Add a command table.
320 add_cmd_table(tlist
, buf
, len
)
321 struct tablelist
**tlist
;
325 register struct tablelist
*t
;
330 * Allocate a tablelist structure, initialize it,
331 * and link it into the list of tables.
333 if ((t
= (struct tablelist
*)
334 calloc(1, sizeof(struct tablelist
))) == NULL
)
338 expand_special_keys(buf
, len
);
340 t
->t_end
= buf
+ len
;
347 * Add a command table.
350 add_fcmd_table(buf
, len
)
354 if (add_cmd_table(&list_fcmd_tables
, buf
, len
) < 0)
355 error("Warning: some commands disabled", NULL_PARG
);
359 * Add an editing command table.
362 add_ecmd_table(buf
, len
)
366 if (add_cmd_table(&list_ecmd_tables
, buf
, len
) < 0)
367 error("Warning: some edit commands disabled", NULL_PARG
);
371 * Add an environment variable table.
374 add_var_table(tlist
, buf
, len
)
375 struct tablelist
**tlist
;
379 if (add_cmd_table(tlist
, buf
, len
) < 0)
380 error("Warning: environment variables from lesskey file unavailable", NULL_PARG
);
384 * Search a single command table for the command string in cmd.
387 cmd_search(cmd
, table
, endtable
, sp
)
398 for (p
= table
, q
= cmd
; p
< endtable
; p
++, q
++)
403 * Current characters match.
404 * If we're at the end of the string, we've found it.
405 * Return the action code, which is the character
406 * after the null at the end of the string
407 * in the command table.
417 * We get here only if the original
418 * cmd string passed in was empty ("").
419 * I don't think that can happen,
420 * but just in case ...
425 * Check for an "extra" string.
434 } else if (*q
== '\0')
437 * Hit the end of the user's command,
438 * but not the end of the string in the command table.
439 * The user's command is incomplete.
446 * Skip ahead to the next command in the
447 * command table, and reset the pointer
448 * to the beginning of the user's command.
450 if (*p
== '\0' && p
[1] == A_END_LIST
)
453 * A_END_LIST is a special marker that tells
454 * us to abort the cmd search.
469 * No match found in the entire command table.
475 * Decode a command character and return the associated action.
476 * The "extra" string, if any, is returned in sp.
479 cmd_decode(tlist
, cmd
, sp
)
480 struct tablelist
*tlist
;
484 register struct tablelist
*t
;
485 register int action
= A_INVALID
;
488 * Search thru all the command tables.
489 * Stop when we find an action which is not A_INVALID.
491 for (t
= tlist
; t
!= NULL
; t
= t
->t_next
)
493 action
= cmd_search(cmd
, t
->t_start
, t
->t_end
, sp
);
494 if (action
!= A_INVALID
)
497 if (action
== A_UINVALID
)
503 * Decode a command from the cmdtables list.
510 return (cmd_decode(list_fcmd_tables
, cmd
, sp
));
514 * Decode a command from the edittables list.
521 return (cmd_decode(list_ecmd_tables
, cmd
, sp
));
525 * Get the value of an environment variable.
526 * Looks first in the lesskey file, then in the real environment.
535 a
= cmd_decode(list_var_tables
, var
, &s
);
539 if (s
!= NULL
&& *s
!= '\0')
541 a
= cmd_decode(list_sysvar_tables
, var
, &s
);
549 * Get an "integer" from a lesskey file.
550 * Integers are stored in a funny format:
551 * two bytes, low order first, in radix KRADIX.
560 n
+= *(*sp
)++ * KRADIX
;
565 * Process an old (pre-v241) lesskey file.
568 old_lesskey(buf
, len
)
573 * Old-style lesskey file.
574 * The file must end with either
576 * or ...,cmd,0,action|A_EXTRA,string,0
577 * So the last byte or the second to last byte must be zero.
579 if (buf
[len
-1] != '\0' && buf
[len
-2] != '\0')
581 add_fcmd_table(buf
, len
);
586 * Process a new (post-v241) lesskey file.
589 new_lesskey(buf
, len
, sysvar
)
599 * New-style lesskey file.
600 * Extract the pieces.
602 if (buf
[len
-3] != C0_END_LESSKEY_MAGIC
||
603 buf
[len
-2] != C1_END_LESSKEY_MAGIC
||
604 buf
[len
-1] != C2_END_LESSKEY_MAGIC
)
614 add_fcmd_table(p
, n
);
619 add_ecmd_table(p
, n
);
624 add_var_table((sysvar
) ?
625 &list_sysvar_tables
: &list_var_tables
, p
, n
);
632 * Unrecognized section type.
640 * Set up a user command table, based on a "lesskey" file.
643 lesskey(filename
, sysvar
)
648 register POSITION len
;
655 * Try to open the lesskey file.
657 filename
= shell_unquote(filename
);
658 f
= open(filename
, OPEN_READ
);
664 * Read the file into a buffer.
665 * We first figure out the size of the file and allocate space for it.
666 * {{ Minimal error checking is done here.
667 * A garbage .less file will produce strange results.
668 * To avoid a large amount of error checking code here, we
669 * rely on the lesskey program to generate a good .less file. }}
672 if (len
== NULL_POSITION
|| len
< 3)
675 * Bad file (valid file must have at least 3 chars).
680 if ((buf
= (char *) calloc((int)len
, sizeof(char))) == NULL
)
685 if (lseek(f
, (off_t
)0, SEEK_SET
) == BAD_LSEEK
)
691 n
= read(f
, buf
, (unsigned int) len
);
700 * Figure out if this is an old-style (before version 241)
701 * or new-style lesskey file format.
703 if (buf
[0] != C0_LESSKEY_MAGIC
|| buf
[1] != C1_LESSKEY_MAGIC
||
704 buf
[2] != C2_LESSKEY_MAGIC
|| buf
[3] != C3_LESSKEY_MAGIC
)
705 return (old_lesskey(buf
, (int)len
));
706 return (new_lesskey(buf
, (int)len
, sysvar
));
710 * Add the standard lesskey file "$HOME/.less"
713 add_hometable(envname
, def_filename
, sysvar
)
721 if (envname
!= NULL
&& (filename
= lgetenv(envname
)) != NULL
)
722 filename
= save(filename
);
724 filename
= save(def_filename
);
726 filename
= homefile(def_filename
);
727 if (filename
== NULL
)
729 if (lesskey(filename
, sysvar
) < 0)
731 parg
.p_string
= filename
;
732 error("Cannot use lesskey file \"%s\"", &parg
);
739 * See if a char is a special line-editing command.
749 char usercmd
[MAX_CMDLEN
+1];
752 * An editing character could actually be a sequence of characters;
753 * for example, an escape sequence sent by pressing the uparrow key.
754 * To match the editing string, we use the command decoder
755 * but give it the edit-commands command table
756 * This table is constructed to match the user's keyboard.
758 if (c
== erase_char
|| c
== erase2_char
)
759 return (EC_BACKSPACE
);
761 return (EC_LINEKILL
);
764 * Collect characters in a buffer.
765 * Start with the one we have, and get more if we need them.
772 usercmd
[nch
+1] = '\0';
774 action
= ecmd_decode(usercmd
, &s
);
775 } while (action
== A_PREFIX
);
777 if (flags
& EC_NORIGHTLEFT
)
788 if (flags
& EC_NOHISTORY
)
791 * The caller says there is no history list.
792 * Reject any history-manipulation action.
803 #if TAB_COMPLETE_FILENAME
804 if (flags
& EC_NOCOMPLETE
)
807 * The caller says we don't want any filename completion cmds.
820 if ((flags
& EC_PEEK
) || action
== A_INVALID
)
823 * We're just peeking, or we didn't understand the command.
824 * Unget all the characters we read in the loop above.
825 * This does NOT include the original character that was
826 * passed in as a parameter.
830 ungetcc(usercmd
[--nch
]);