xref: /netbsd-src/external/bsd/less/dist/decode.c (revision 76b1013682e26a06abb45d246f7e04f266c943dc)
1*76b10136Ssimonb /*	$NetBSD: decode.c,v 1.7 2023/10/06 07:13:13 simonb Exp $	*/
220006a0bStron 
320006a0bStron /*
4838f5788Ssimonb  * Copyright (C) 1984-2023  Mark Nudelman
520006a0bStron  *
620006a0bStron  * You may distribute under the terms of either the GNU General Public
720006a0bStron  * License or the Less License, as specified in the README file.
820006a0bStron  *
9ec18bca0Stron  * For more information, see the README file.
1020006a0bStron  */
1120006a0bStron 
1220006a0bStron 
1320006a0bStron /*
1420006a0bStron  * Routines to decode user commands.
1520006a0bStron  *
1620006a0bStron  * This is all table driven.
1720006a0bStron  * A command table is a sequence of command descriptors.
1820006a0bStron  * Each command descriptor is a sequence of bytes with the following format:
1920006a0bStron  *     <c1><c2>...<cN><0><action>
2020006a0bStron  * The characters c1,c2,...,cN are the command string; that is,
2120006a0bStron  * the characters which the user must type.
2220006a0bStron  * It is terminated by a null <0> byte.
2320006a0bStron  * The byte after the null byte is the action code associated
2420006a0bStron  * with the command string.
2520006a0bStron  * If an action byte is OR-ed with A_EXTRA, this indicates
2620006a0bStron  * that the option byte is followed by an extra string.
2720006a0bStron  *
2820006a0bStron  * There may be many command tables.
2920006a0bStron  * The first (default) table is built-in.
3020006a0bStron  * Other tables are read in from "lesskey" files.
3120006a0bStron  * All the tables are linked together and are searched in order.
3220006a0bStron  */
3320006a0bStron 
3420006a0bStron #include "less.h"
3520006a0bStron #include "cmd.h"
3620006a0bStron #include "lesskey.h"
3720006a0bStron 
3820006a0bStron extern int erase_char, erase2_char, kill_char;
3920006a0bStron extern int secure;
40838f5788Ssimonb extern int mousecap;
41838f5788Ssimonb extern int screen_trashed;
42838f5788Ssimonb extern int sc_height;
4320006a0bStron 
4420006a0bStron #define SK(k) \
4520006a0bStron 	SK_SPECIAL_KEY, (k), 6, 1, 1, 1
4620006a0bStron /*
4720006a0bStron  * Command table is ordered roughly according to expected
4820006a0bStron  * frequency of use, so the common commands are near the beginning.
4920006a0bStron  */
5020006a0bStron 
5120006a0bStron static unsigned char cmdtable[] =
5220006a0bStron {
5320006a0bStron 	'\r',0,                         A_F_LINE,
5420006a0bStron 	'\n',0,                         A_F_LINE,
5520006a0bStron 	'e',0,                          A_F_LINE,
5620006a0bStron 	'j',0,                          A_F_LINE,
5720006a0bStron 	SK(SK_DOWN_ARROW),0,            A_F_LINE,
5820006a0bStron 	CONTROL('E'),0,                 A_F_LINE,
5920006a0bStron 	CONTROL('N'),0,                 A_F_LINE,
6020006a0bStron 	'k',0,                          A_B_LINE,
6120006a0bStron 	'y',0,                          A_B_LINE,
6220006a0bStron 	CONTROL('Y'),0,                 A_B_LINE,
6320006a0bStron 	SK(SK_CONTROL_K),0,             A_B_LINE,
6420006a0bStron 	CONTROL('P'),0,                 A_B_LINE,
6520006a0bStron 	SK(SK_UP_ARROW),0,              A_B_LINE,
6620006a0bStron 	'J',0,                          A_FF_LINE,
6720006a0bStron 	'K',0,                          A_BF_LINE,
6820006a0bStron 	'Y',0,                          A_BF_LINE,
6920006a0bStron 	'd',0,                          A_F_SCROLL,
7020006a0bStron 	CONTROL('D'),0,                 A_F_SCROLL,
7120006a0bStron 	'u',0,                          A_B_SCROLL,
7220006a0bStron 	CONTROL('U'),0,                 A_B_SCROLL,
73838f5788Ssimonb 	ESC,'[','M',0,                  A_X11MOUSE_IN,
74838f5788Ssimonb 	ESC,'[','<',0,                  A_X116MOUSE_IN,
7520006a0bStron 	' ',0,                          A_F_SCREEN,
7620006a0bStron 	'f',0,                          A_F_SCREEN,
7720006a0bStron 	CONTROL('F'),0,                 A_F_SCREEN,
7820006a0bStron 	CONTROL('V'),0,                 A_F_SCREEN,
7920006a0bStron 	SK(SK_PAGE_DOWN),0,             A_F_SCREEN,
8020006a0bStron 	'b',0,                          A_B_SCREEN,
8120006a0bStron 	CONTROL('B'),0,                 A_B_SCREEN,
8220006a0bStron 	ESC,'v',0,                      A_B_SCREEN,
8320006a0bStron 	SK(SK_PAGE_UP),0,               A_B_SCREEN,
8420006a0bStron 	'z',0,                          A_F_WINDOW,
8520006a0bStron 	'w',0,                          A_B_WINDOW,
8620006a0bStron 	ESC,' ',0,                      A_FF_SCREEN,
8720006a0bStron 	'F',0,                          A_F_FOREVER,
88ec18bca0Stron 	ESC,'F',0,                      A_F_UNTIL_HILITE,
8920006a0bStron 	'R',0,                          A_FREPAINT,
9020006a0bStron 	'r',0,                          A_REPAINT,
9120006a0bStron 	CONTROL('R'),0,                 A_REPAINT,
9220006a0bStron 	CONTROL('L'),0,                 A_REPAINT,
9320006a0bStron 	ESC,'u',0,                      A_UNDO_SEARCH,
94838f5788Ssimonb 	ESC,'U',0,                      A_CLR_SEARCH,
9520006a0bStron 	'g',0,                          A_GOLINE,
9620006a0bStron 	SK(SK_HOME),0,                  A_GOLINE,
9720006a0bStron 	'<',0,                          A_GOLINE,
9820006a0bStron 	ESC,'<',0,                      A_GOLINE,
9920006a0bStron 	'p',0,                          A_PERCENT,
10020006a0bStron 	'%',0,                          A_PERCENT,
10120006a0bStron 	ESC,'[',0,                      A_LSHIFT,
10220006a0bStron 	ESC,']',0,                      A_RSHIFT,
10320006a0bStron 	ESC,'(',0,                      A_LSHIFT,
10420006a0bStron 	ESC,')',0,                      A_RSHIFT,
105838f5788Ssimonb 	ESC,'{',0,                      A_LLSHIFT,
106838f5788Ssimonb 	ESC,'}',0,                      A_RRSHIFT,
10720006a0bStron 	SK(SK_RIGHT_ARROW),0,           A_RSHIFT,
10820006a0bStron 	SK(SK_LEFT_ARROW),0,            A_LSHIFT,
109838f5788Ssimonb 	SK(SK_CTL_RIGHT_ARROW),0,       A_RRSHIFT,
110838f5788Ssimonb 	SK(SK_CTL_LEFT_ARROW),0,        A_LLSHIFT,
11120006a0bStron 	'{',0,                          A_F_BRACKET|A_EXTRA,        '{','}',0,
11220006a0bStron 	'}',0,                          A_B_BRACKET|A_EXTRA,        '{','}',0,
11320006a0bStron 	'(',0,                          A_F_BRACKET|A_EXTRA,        '(',')',0,
11420006a0bStron 	')',0,                          A_B_BRACKET|A_EXTRA,        '(',')',0,
11520006a0bStron 	'[',0,                          A_F_BRACKET|A_EXTRA,        '[',']',0,
11620006a0bStron 	']',0,                          A_B_BRACKET|A_EXTRA,        '[',']',0,
11720006a0bStron 	ESC,CONTROL('F'),0,             A_F_BRACKET,
11820006a0bStron 	ESC,CONTROL('B'),0,             A_B_BRACKET,
11920006a0bStron 	'G',0,                          A_GOEND,
120838f5788Ssimonb 	ESC,'G',0,                      A_GOEND_BUF,
12120006a0bStron 	ESC,'>',0,                      A_GOEND,
12220006a0bStron 	'>',0,                          A_GOEND,
12320006a0bStron 	SK(SK_END),0,                   A_GOEND,
12420006a0bStron 	'P',0,                          A_GOPOS,
12520006a0bStron 
12620006a0bStron 	'0',0,                          A_DIGIT,
12720006a0bStron 	'1',0,                          A_DIGIT,
12820006a0bStron 	'2',0,                          A_DIGIT,
12920006a0bStron 	'3',0,                          A_DIGIT,
13020006a0bStron 	'4',0,                          A_DIGIT,
13120006a0bStron 	'5',0,                          A_DIGIT,
13220006a0bStron 	'6',0,                          A_DIGIT,
13320006a0bStron 	'7',0,                          A_DIGIT,
13420006a0bStron 	'8',0,                          A_DIGIT,
13520006a0bStron 	'9',0,                          A_DIGIT,
13620006a0bStron 	'.',0,                          A_DIGIT,
13720006a0bStron 
13820006a0bStron 	'=',0,                          A_STAT,
13920006a0bStron 	CONTROL('G'),0,                 A_STAT,
14020006a0bStron 	':','f',0,                      A_STAT,
14120006a0bStron 	'/',0,                          A_F_SEARCH,
14220006a0bStron 	'?',0,                          A_B_SEARCH,
14320006a0bStron 	ESC,'/',0,                      A_F_SEARCH|A_EXTRA,        '*',0,
14420006a0bStron 	ESC,'?',0,                      A_B_SEARCH|A_EXTRA,        '*',0,
14520006a0bStron 	'n',0,                          A_AGAIN_SEARCH,
14620006a0bStron 	ESC,'n',0,                      A_T_AGAIN_SEARCH,
14720006a0bStron 	'N',0,                          A_REVERSE_SEARCH,
14820006a0bStron 	ESC,'N',0,                      A_T_REVERSE_SEARCH,
14920006a0bStron 	'&',0,                          A_FILTER,
15020006a0bStron 	'm',0,                          A_SETMARK,
151838f5788Ssimonb 	'M',0,                          A_SETMARKBOT,
152838f5788Ssimonb 	ESC,'m',0,                      A_CLRMARK,
15320006a0bStron 	'\'',0,                         A_GOMARK,
15420006a0bStron 	CONTROL('X'),CONTROL('X'),0,    A_GOMARK,
15520006a0bStron 	'E',0,                          A_EXAMINE,
15620006a0bStron 	':','e',0,                      A_EXAMINE,
15720006a0bStron 	CONTROL('X'),CONTROL('V'),0,    A_EXAMINE,
15820006a0bStron 	':','n',0,                      A_NEXT_FILE,
15920006a0bStron 	':','p',0,                      A_PREV_FILE,
16020006a0bStron 	't',0,                          A_NEXT_TAG,
16120006a0bStron 	'T',0,                          A_PREV_TAG,
16220006a0bStron 	':','x',0,                      A_INDEX_FILE,
16320006a0bStron 	':','d',0,                      A_REMOVE_FILE,
16420006a0bStron 	'-',0,                          A_OPT_TOGGLE,
16520006a0bStron 	':','t',0,                      A_OPT_TOGGLE|A_EXTRA,        't',0,
16620006a0bStron 	's',0,                          A_OPT_TOGGLE|A_EXTRA,        'o',0,
16720006a0bStron 	'_',0,                          A_DISP_OPTION,
16820006a0bStron 	'|',0,                          A_PIPE,
16920006a0bStron 	'v',0,                          A_VISUAL,
17020006a0bStron 	'!',0,                          A_SHELL,
171838f5788Ssimonb 	'#',0,                          A_PSHELL,
17220006a0bStron 	'+',0,                          A_FIRSTCMD,
17320006a0bStron 
17420006a0bStron 	'H',0,                          A_HELP,
17520006a0bStron 	'h',0,                          A_HELP,
17620006a0bStron 	SK(SK_F1),0,                    A_HELP,
17720006a0bStron 	'V',0,                          A_VERSION,
17820006a0bStron 	'q',0,                          A_QUIT,
17920006a0bStron 	'Q',0,                          A_QUIT,
18020006a0bStron 	':','q',0,                      A_QUIT,
18120006a0bStron 	':','Q',0,                      A_QUIT,
18220006a0bStron 	'Z','Z',0,                      A_QUIT
18320006a0bStron };
18420006a0bStron 
18520006a0bStron static unsigned char edittable[] =
18620006a0bStron {
18720006a0bStron 	'\t',0,                         EC_F_COMPLETE,  /* TAB */
18820006a0bStron 	'\17',0,                        EC_B_COMPLETE,  /* BACKTAB */
18920006a0bStron 	SK(SK_BACKTAB),0,               EC_B_COMPLETE,  /* BACKTAB */
19020006a0bStron 	ESC,'\t',0,                     EC_B_COMPLETE,  /* ESC TAB */
19120006a0bStron 	CONTROL('L'),0,                 EC_EXPAND,      /* CTRL-L */
19220006a0bStron 	CONTROL('V'),0,                 EC_LITERAL,     /* BACKSLASH */
19320006a0bStron 	CONTROL('A'),0,                 EC_LITERAL,     /* BACKSLASH */
19420006a0bStron 	ESC,'l',0,                      EC_RIGHT,       /* ESC l */
19520006a0bStron 	SK(SK_RIGHT_ARROW),0,           EC_RIGHT,       /* RIGHTARROW */
19620006a0bStron 	ESC,'h',0,                      EC_LEFT,        /* ESC h */
19720006a0bStron 	SK(SK_LEFT_ARROW),0,            EC_LEFT,        /* LEFTARROW */
19820006a0bStron 	ESC,'b',0,                      EC_W_LEFT,      /* ESC b */
19920006a0bStron 	ESC,SK(SK_LEFT_ARROW),0,        EC_W_LEFT,      /* ESC LEFTARROW */
20020006a0bStron 	SK(SK_CTL_LEFT_ARROW),0,        EC_W_LEFT,      /* CTRL-LEFTARROW */
20120006a0bStron 	ESC,'w',0,                      EC_W_RIGHT,     /* ESC w */
20220006a0bStron 	ESC,SK(SK_RIGHT_ARROW),0,       EC_W_RIGHT,     /* ESC RIGHTARROW */
20320006a0bStron 	SK(SK_CTL_RIGHT_ARROW),0,       EC_W_RIGHT,     /* CTRL-RIGHTARROW */
20420006a0bStron 	ESC,'i',0,                      EC_INSERT,      /* ESC i */
20520006a0bStron 	SK(SK_INSERT),0,                EC_INSERT,      /* INSERT */
20620006a0bStron 	ESC,'x',0,                      EC_DELETE,      /* ESC x */
20720006a0bStron 	SK(SK_DELETE),0,                EC_DELETE,      /* DELETE */
20820006a0bStron 	ESC,'X',0,                      EC_W_DELETE,    /* ESC X */
20920006a0bStron 	ESC,SK(SK_DELETE),0,            EC_W_DELETE,    /* ESC DELETE */
21020006a0bStron 	SK(SK_CTL_DELETE),0,            EC_W_DELETE,    /* CTRL-DELETE */
21120006a0bStron 	SK(SK_CTL_BACKSPACE),0,         EC_W_BACKSPACE, /* CTRL-BACKSPACE */
212838f5788Ssimonb 	ESC,SK(SK_BACKSPACE),0,         EC_W_BACKSPACE, /* ESC BACKSPACE */
21320006a0bStron 	ESC,'0',0,                      EC_HOME,        /* ESC 0 */
21420006a0bStron 	SK(SK_HOME),0,                  EC_HOME,        /* HOME */
21520006a0bStron 	ESC,'$',0,                      EC_END,         /* ESC $ */
21620006a0bStron 	SK(SK_END),0,                   EC_END,         /* END */
21720006a0bStron 	ESC,'k',0,                      EC_UP,          /* ESC k */
21820006a0bStron 	SK(SK_UP_ARROW),0,              EC_UP,          /* UPARROW */
21920006a0bStron 	ESC,'j',0,                      EC_DOWN,        /* ESC j */
22020006a0bStron 	SK(SK_DOWN_ARROW),0,            EC_DOWN,        /* DOWNARROW */
22120006a0bStron 	CONTROL('G'),0,                 EC_ABORT,       /* CTRL-G */
222838f5788Ssimonb 	ESC,'[','M',0,                  EC_X11MOUSE,    /* X11 mouse report */
223838f5788Ssimonb 	ESC,'[','<',0,                  EC_X116MOUSE,   /* X11 1006 mouse report */
22420006a0bStron };
22520006a0bStron 
22620006a0bStron /*
22720006a0bStron  * Structure to support a list of command tables.
22820006a0bStron  */
22920006a0bStron struct tablelist
23020006a0bStron {
23120006a0bStron 	struct tablelist *t_next;
23220006a0bStron 	char *t_start;
23320006a0bStron 	char *t_end;
23420006a0bStron };
23520006a0bStron 
23620006a0bStron /*
23720006a0bStron  * List of command tables and list of line-edit tables.
23820006a0bStron  */
23920006a0bStron static struct tablelist *list_fcmd_tables = NULL;
24020006a0bStron static struct tablelist *list_ecmd_tables = NULL;
24120006a0bStron static struct tablelist *list_var_tables = NULL;
24220006a0bStron static struct tablelist *list_sysvar_tables = NULL;
24320006a0bStron 
244085b99e6Ssimonb 
24520006a0bStron /*
24620006a0bStron  * Expand special key abbreviations in a command table.
24720006a0bStron  */
expand_special_keys(char * table,int len)248838f5788Ssimonb static void expand_special_keys(char *table, int len)
24920006a0bStron {
250838f5788Ssimonb 	char *fm;
251838f5788Ssimonb 	char *to;
252838f5788Ssimonb 	int a;
25320006a0bStron 	char *repl;
25420006a0bStron 	int klen;
25520006a0bStron 
25620006a0bStron 	for (fm = table;  fm < table + len; )
25720006a0bStron 	{
25820006a0bStron 		/*
25920006a0bStron 		 * Rewrite each command in the table with any
26020006a0bStron 		 * special key abbreviations expanded.
26120006a0bStron 		 */
26220006a0bStron 		for (to = fm;  *fm != '\0'; )
26320006a0bStron 		{
26420006a0bStron 			if (*fm != SK_SPECIAL_KEY)
26520006a0bStron 			{
26620006a0bStron 				*to++ = *fm++;
26720006a0bStron 				continue;
26820006a0bStron 			}
26920006a0bStron 			/*
27020006a0bStron 			 * After SK_SPECIAL_KEY, next byte is the type
271838f5788Ssimonb 			 * of special key (one of the SK_* constants),
27220006a0bStron 			 * and the byte after that is the number of bytes,
27320006a0bStron 			 * N, reserved by the abbreviation (including the
27420006a0bStron 			 * SK_SPECIAL_KEY and key type bytes).
27520006a0bStron 			 * Replace all N bytes with the actual bytes
27620006a0bStron 			 * output by the special key on this terminal.
27720006a0bStron 			 */
27820006a0bStron 			repl = special_key_str(fm[1]);
27920006a0bStron 			klen = fm[2] & 0377;
28020006a0bStron 			fm += klen;
28120006a0bStron 			if (repl == NULL || (int) strlen(repl) > klen)
28220006a0bStron 				repl = "\377";
28320006a0bStron 			while (*repl != '\0')
28420006a0bStron 				*to++ = *repl++;
28520006a0bStron 		}
28620006a0bStron 		*to++ = '\0';
28720006a0bStron 		/*
28820006a0bStron 		 * Fill any unused bytes between end of command and
28920006a0bStron 		 * the action byte with A_SKIP.
29020006a0bStron 		 */
29120006a0bStron 		while (to <= fm)
29220006a0bStron 			*to++ = A_SKIP;
29320006a0bStron 		fm++;
29420006a0bStron 		a = *fm++ & 0377;
29520006a0bStron 		if (a & A_EXTRA)
29620006a0bStron 		{
29720006a0bStron 			while (*fm++ != '\0')
29820006a0bStron 				continue;
29920006a0bStron 		}
30020006a0bStron 	}
30120006a0bStron }
30220006a0bStron 
30320006a0bStron /*
304838f5788Ssimonb  * Expand special key abbreviations in a list of command tables.
305838f5788Ssimonb  */
expand_cmd_table(struct tablelist * tlist)306838f5788Ssimonb static void expand_cmd_table(struct tablelist *tlist)
307838f5788Ssimonb {
308838f5788Ssimonb 	struct tablelist *t;
309838f5788Ssimonb 	for (t = tlist;  t != NULL;  t = t->t_next)
310838f5788Ssimonb 	{
311838f5788Ssimonb 		expand_special_keys(t->t_start, t->t_end - t->t_start);
312838f5788Ssimonb 	}
313838f5788Ssimonb }
314838f5788Ssimonb 
315838f5788Ssimonb /*
316838f5788Ssimonb  * Expand special key abbreviations in all command tables.
317838f5788Ssimonb  */
expand_cmd_tables(void)318838f5788Ssimonb public void expand_cmd_tables(void)
319838f5788Ssimonb {
320838f5788Ssimonb 	expand_cmd_table(list_fcmd_tables);
321838f5788Ssimonb 	expand_cmd_table(list_ecmd_tables);
322838f5788Ssimonb 	expand_cmd_table(list_var_tables);
323838f5788Ssimonb 	expand_cmd_table(list_sysvar_tables);
324838f5788Ssimonb }
325838f5788Ssimonb 
326838f5788Ssimonb 
327838f5788Ssimonb /*
32820006a0bStron  * Initialize the command lists.
32920006a0bStron  */
init_cmds(void)330838f5788Ssimonb public void init_cmds(void)
33120006a0bStron {
33220006a0bStron 	/*
33320006a0bStron 	 * Add the default command tables.
33420006a0bStron 	 */
33520006a0bStron 	add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
33620006a0bStron 	add_ecmd_table((char*)edittable, sizeof(edittable));
33720006a0bStron #if USERFILE
338838f5788Ssimonb #ifdef BINDIR /* For backwards compatibility */
339838f5788Ssimonb 	/* Try to add tables in the OLD system lesskey file. */
340838f5788Ssimonb 	add_hometable(lesskey, NULL, BINDIR "/.sysless", 1);
34120006a0bStron #endif
34220006a0bStron 	/*
343838f5788Ssimonb 	 * Try to load lesskey source file or binary file.
344838f5788Ssimonb 	 * If the source file succeeds, don't load binary file.
345838f5788Ssimonb 	 * The binary file is likely to have been generated from
346838f5788Ssimonb 	 * a (possibly out of date) copy of the src file,
347838f5788Ssimonb 	 * so loading it is at best redundant.
34820006a0bStron 	 */
34920006a0bStron 	/*
350838f5788Ssimonb 	 * Try to add tables in system lesskey src file.
35120006a0bStron 	 */
352838f5788Ssimonb #if HAVE_LESSKEYSRC
353838f5788Ssimonb 	if (add_hometable(lesskey_src, "LESSKEYIN_SYSTEM", LESSKEYINFILE_SYS, 1) != 0)
354838f5788Ssimonb #endif
355838f5788Ssimonb 	{
356838f5788Ssimonb 		/*
357838f5788Ssimonb 		 * Try to add the tables in the system lesskey binary file.
358838f5788Ssimonb 		 */
359838f5788Ssimonb 		add_hometable(lesskey, "LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
360838f5788Ssimonb 	}
361838f5788Ssimonb 	/*
362838f5788Ssimonb 	 * Try to add tables in the lesskey src file "$HOME/.lesskey".
363838f5788Ssimonb 	 */
364838f5788Ssimonb #if HAVE_LESSKEYSRC
365838f5788Ssimonb 	if (add_hometable(lesskey_src, "LESSKEYIN", DEF_LESSKEYINFILE, 0) != 0)
366838f5788Ssimonb #endif
367838f5788Ssimonb 	{
368838f5788Ssimonb 		/*
369838f5788Ssimonb 		 * Try to add the tables in the standard lesskey binary file "$HOME/.less".
370838f5788Ssimonb 		 */
371838f5788Ssimonb 		add_hometable(lesskey, "LESSKEY", LESSKEYFILE, 0);
372838f5788Ssimonb 	}
37320006a0bStron #endif
37420006a0bStron }
37520006a0bStron 
37620006a0bStron /*
37720006a0bStron  * Add a command table.
37820006a0bStron  */
add_cmd_table(struct tablelist ** tlist,char * buf,int len)379838f5788Ssimonb static int add_cmd_table(struct tablelist **tlist, char *buf, int len)
38020006a0bStron {
381838f5788Ssimonb 	struct tablelist *t;
38220006a0bStron 
38320006a0bStron 	if (len == 0)
38420006a0bStron 		return (0);
38520006a0bStron 	/*
38620006a0bStron 	 * Allocate a tablelist structure, initialize it,
38720006a0bStron 	 * and link it into the list of tables.
38820006a0bStron 	 */
38920006a0bStron 	if ((t = (struct tablelist *)
39020006a0bStron 			calloc(1, sizeof(struct tablelist))) == NULL)
39120006a0bStron 	{
39220006a0bStron 		return (-1);
39320006a0bStron 	}
39420006a0bStron 	t->t_start = buf;
39520006a0bStron 	t->t_end = buf + len;
39620006a0bStron 	t->t_next = *tlist;
39720006a0bStron 	*tlist = t;
39820006a0bStron 	return (0);
39920006a0bStron }
40020006a0bStron 
40120006a0bStron /*
40220006a0bStron  * Add a command table.
40320006a0bStron  */
add_fcmd_table(char * buf,int len)404838f5788Ssimonb public void add_fcmd_table(char *buf, int len)
40520006a0bStron {
40620006a0bStron 	if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
40720006a0bStron 		error("Warning: some commands disabled", NULL_PARG);
40820006a0bStron }
40920006a0bStron 
41020006a0bStron /*
41120006a0bStron  * Add an editing command table.
41220006a0bStron  */
add_ecmd_table(char * buf,int len)413838f5788Ssimonb public void add_ecmd_table(char *buf, int len)
41420006a0bStron {
41520006a0bStron 	if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
41620006a0bStron 		error("Warning: some edit commands disabled", NULL_PARG);
41720006a0bStron }
41820006a0bStron 
41920006a0bStron /*
42020006a0bStron  * Add an environment variable table.
42120006a0bStron  */
add_var_table(struct tablelist ** tlist,char * buf,int len)422838f5788Ssimonb static void add_var_table(struct tablelist **tlist, char *buf, int len)
42320006a0bStron {
42420006a0bStron 	if (add_cmd_table(tlist, buf, len) < 0)
42520006a0bStron 		error("Warning: environment variables from lesskey file unavailable", NULL_PARG);
42620006a0bStron }
42720006a0bStron 
42820006a0bStron /*
429838f5788Ssimonb  * Return action for a mouse wheel down event.
430838f5788Ssimonb  */
mouse_wheel_down(void)431838f5788Ssimonb static int mouse_wheel_down(void)
432838f5788Ssimonb {
433838f5788Ssimonb 	return ((mousecap == OPT_ONPLUS) ? A_B_MOUSE : A_F_MOUSE);
434838f5788Ssimonb }
435838f5788Ssimonb 
436838f5788Ssimonb /*
437838f5788Ssimonb  * Return action for a mouse wheel up event.
438838f5788Ssimonb  */
mouse_wheel_up(void)439838f5788Ssimonb static int mouse_wheel_up(void)
440838f5788Ssimonb {
441838f5788Ssimonb 	return ((mousecap == OPT_ONPLUS) ? A_F_MOUSE : A_B_MOUSE);
442838f5788Ssimonb }
443838f5788Ssimonb 
444838f5788Ssimonb /*
445838f5788Ssimonb  * Return action for a mouse button release event.
446838f5788Ssimonb  */
mouse_button_rel(int x,int y)447838f5788Ssimonb static int mouse_button_rel(int x, int y)
448838f5788Ssimonb {
449838f5788Ssimonb 	/*
450838f5788Ssimonb 	 * {{ It would be better to return an action and then do this
451838f5788Ssimonb 	 *    in commands() but it's nontrivial to pass y to it. }}
452838f5788Ssimonb 	 */
453838f5788Ssimonb 	if (y < sc_height-1)
454838f5788Ssimonb 	{
455838f5788Ssimonb 		setmark('#', y);
456838f5788Ssimonb 		screen_trashed = 1;
457838f5788Ssimonb 	}
458838f5788Ssimonb 	return (A_NOACTION);
459838f5788Ssimonb }
460838f5788Ssimonb 
461838f5788Ssimonb /*
462838f5788Ssimonb  * Read a decimal integer. Return the integer and set *pterm to the terminating char.
463838f5788Ssimonb  */
getcc_int(char * pterm)464838f5788Ssimonb static int getcc_int(char *pterm)
465838f5788Ssimonb {
466838f5788Ssimonb 	int num = 0;
467838f5788Ssimonb 	int digits = 0;
468838f5788Ssimonb 	for (;;)
469838f5788Ssimonb 	{
470838f5788Ssimonb 		char ch = getcc();
471838f5788Ssimonb 		if (ch < '0' || ch > '9')
472838f5788Ssimonb 		{
473838f5788Ssimonb 			if (pterm != NULL) *pterm = ch;
474838f5788Ssimonb 			if (digits == 0)
475838f5788Ssimonb 				return (-1);
476838f5788Ssimonb 			return (num);
477838f5788Ssimonb 		}
478838f5788Ssimonb 		if (ckd_mul(&num, num, 10) || ckd_add(&num, num, ch - '0'))
479838f5788Ssimonb 			return -1;
480838f5788Ssimonb 		++digits;
481838f5788Ssimonb 	}
482838f5788Ssimonb }
483838f5788Ssimonb 
484838f5788Ssimonb /*
485838f5788Ssimonb  * Read suffix of mouse input and return the action to take.
486838f5788Ssimonb  * The prefix ("\e[M") has already been read.
487838f5788Ssimonb  */
x11mouse_action(int skip)488838f5788Ssimonb static int x11mouse_action(int skip)
489838f5788Ssimonb {
490838f5788Ssimonb 	int b = getcc() - X11MOUSE_OFFSET;
491838f5788Ssimonb 	int x = getcc() - X11MOUSE_OFFSET-1;
492838f5788Ssimonb 	int y = getcc() - X11MOUSE_OFFSET-1;
493838f5788Ssimonb 	if (skip)
494838f5788Ssimonb 		return (A_NOACTION);
495838f5788Ssimonb 	switch (b) {
496838f5788Ssimonb 	default:
497838f5788Ssimonb 		return (A_NOACTION);
498838f5788Ssimonb 	case X11MOUSE_WHEEL_DOWN:
499838f5788Ssimonb 		return mouse_wheel_down();
500838f5788Ssimonb 	case X11MOUSE_WHEEL_UP:
501838f5788Ssimonb 		return mouse_wheel_up();
502838f5788Ssimonb 	case X11MOUSE_BUTTON_REL:
503838f5788Ssimonb 		return mouse_button_rel(x, y);
504838f5788Ssimonb 	}
505838f5788Ssimonb }
506838f5788Ssimonb 
507838f5788Ssimonb /*
508838f5788Ssimonb  * Read suffix of mouse input and return the action to take.
509838f5788Ssimonb  * The prefix ("\e[<") has already been read.
510838f5788Ssimonb  */
x116mouse_action(int skip)511838f5788Ssimonb static int x116mouse_action(int skip)
512838f5788Ssimonb {
513838f5788Ssimonb 	char ch;
514838f5788Ssimonb 	int x, y;
515838f5788Ssimonb 	int b = getcc_int(&ch);
516838f5788Ssimonb 	if (b < 0 || ch != ';') return (A_NOACTION);
517838f5788Ssimonb 	x = getcc_int(&ch) - 1;
518838f5788Ssimonb 	if (x < 0 || ch != ';') return (A_NOACTION);
519838f5788Ssimonb 	y = getcc_int(&ch) - 1;
520838f5788Ssimonb 	if (y < 0) return (A_NOACTION);
521838f5788Ssimonb 	if (skip)
522838f5788Ssimonb 		return (A_NOACTION);
523838f5788Ssimonb 	switch (b) {
524838f5788Ssimonb 	case X11MOUSE_WHEEL_DOWN:
525838f5788Ssimonb 		return mouse_wheel_down();
526838f5788Ssimonb 	case X11MOUSE_WHEEL_UP:
527838f5788Ssimonb 		return mouse_wheel_up();
528838f5788Ssimonb 	default:
529838f5788Ssimonb 		if (ch != 'm') return (A_NOACTION);
530838f5788Ssimonb 		return mouse_button_rel(x, y);
531838f5788Ssimonb 	}
532838f5788Ssimonb }
533838f5788Ssimonb 
534838f5788Ssimonb /*
53520006a0bStron  * Search a single command table for the command string in cmd.
53620006a0bStron  */
cmd_search(char * cmd,char * table,char * endtable,char ** sp)537838f5788Ssimonb static int cmd_search(char *cmd, char *table, char *endtable, char **sp)
53820006a0bStron {
539838f5788Ssimonb 	char *p;
540838f5788Ssimonb 	char *q;
541838f5788Ssimonb 	int a;
54220006a0bStron 
54320006a0bStron 	*sp = NULL;
54420006a0bStron 	for (p = table, q = cmd;  p < endtable;  p++, q++)
54520006a0bStron 	{
54620006a0bStron 		if (*p == *q)
54720006a0bStron 		{
54820006a0bStron 			/*
54920006a0bStron 			 * Current characters match.
55020006a0bStron 			 * If we're at the end of the string, we've found it.
55120006a0bStron 			 * Return the action code, which is the character
55220006a0bStron 			 * after the null at the end of the string
55320006a0bStron 			 * in the command table.
55420006a0bStron 			 */
55520006a0bStron 			if (*p == '\0')
55620006a0bStron 			{
55720006a0bStron 				a = *++p & 0377;
55820006a0bStron 				while (a == A_SKIP)
55920006a0bStron 					a = *++p & 0377;
56020006a0bStron 				if (a == A_END_LIST)
56120006a0bStron 				{
56220006a0bStron 					/*
56320006a0bStron 					 * We get here only if the original
56420006a0bStron 					 * cmd string passed in was empty ("").
56520006a0bStron 					 * I don't think that can happen,
56620006a0bStron 					 * but just in case ...
56720006a0bStron 					 */
56820006a0bStron 					return (A_UINVALID);
56920006a0bStron 				}
57020006a0bStron 				/*
57120006a0bStron 				 * Check for an "extra" string.
57220006a0bStron 				 */
57320006a0bStron 				if (a & A_EXTRA)
57420006a0bStron 				{
57520006a0bStron 					*sp = ++p;
57620006a0bStron 					a &= ~A_EXTRA;
57720006a0bStron 				}
578838f5788Ssimonb 				if (a == A_X11MOUSE_IN)
579838f5788Ssimonb 					a = x11mouse_action(0);
580838f5788Ssimonb 				else if (a == A_X116MOUSE_IN)
581838f5788Ssimonb 					a = x116mouse_action(0);
58220006a0bStron 				return (a);
58320006a0bStron 			}
58420006a0bStron 		} else if (*q == '\0')
58520006a0bStron 		{
58620006a0bStron 			/*
58720006a0bStron 			 * Hit the end of the user's command,
58820006a0bStron 			 * but not the end of the string in the command table.
58920006a0bStron 			 * The user's command is incomplete.
59020006a0bStron 			 */
59120006a0bStron 			return (A_PREFIX);
59220006a0bStron 		} else
59320006a0bStron 		{
59420006a0bStron 			/*
59520006a0bStron 			 * Not a match.
59620006a0bStron 			 * Skip ahead to the next command in the
59720006a0bStron 			 * command table, and reset the pointer
59820006a0bStron 			 * to the beginning of the user's command.
59920006a0bStron 			 */
60020006a0bStron 			if (*p == '\0' && p[1] == A_END_LIST)
60120006a0bStron 			{
60220006a0bStron 				/*
60320006a0bStron 				 * A_END_LIST is a special marker that tells
60420006a0bStron 				 * us to abort the cmd search.
60520006a0bStron 				 */
60620006a0bStron 				return (A_UINVALID);
60720006a0bStron 			}
60820006a0bStron 			while (*p++ != '\0')
60920006a0bStron 				continue;
61020006a0bStron 			while (*p == A_SKIP)
61120006a0bStron 				p++;
61220006a0bStron 			if (*p & A_EXTRA)
61320006a0bStron 				while (*++p != '\0')
61420006a0bStron 					continue;
61520006a0bStron 			q = cmd-1;
61620006a0bStron 		}
61720006a0bStron 	}
61820006a0bStron 	/*
61920006a0bStron 	 * No match found in the entire command table.
62020006a0bStron 	 */
62120006a0bStron 	return (A_INVALID);
62220006a0bStron }
62320006a0bStron 
62420006a0bStron /*
62520006a0bStron  * Decode a command character and return the associated action.
62620006a0bStron  * The "extra" string, if any, is returned in sp.
62720006a0bStron  */
cmd_decode(struct tablelist * tlist,char * cmd,char ** sp)628838f5788Ssimonb static int cmd_decode(struct tablelist *tlist, char *cmd, char **sp)
62920006a0bStron {
630838f5788Ssimonb 	struct tablelist *t;
631838f5788Ssimonb 	int action = A_INVALID;
63220006a0bStron 
63320006a0bStron 	/*
63420006a0bStron 	 * Search thru all the command tables.
63520006a0bStron 	 * Stop when we find an action which is not A_INVALID.
63620006a0bStron 	 */
63720006a0bStron 	for (t = tlist;  t != NULL;  t = t->t_next)
63820006a0bStron 	{
63920006a0bStron 		action = cmd_search(cmd, t->t_start, t->t_end, sp);
64020006a0bStron 		if (action != A_INVALID)
64120006a0bStron 			break;
64220006a0bStron 	}
64320006a0bStron 	if (action == A_UINVALID)
64420006a0bStron 		action = A_INVALID;
64520006a0bStron 	return (action);
64620006a0bStron }
64720006a0bStron 
64820006a0bStron /*
64920006a0bStron  * Decode a command from the cmdtables list.
65020006a0bStron  */
fcmd_decode(char * cmd,char ** sp)651838f5788Ssimonb public int fcmd_decode(char *cmd, char **sp)
65220006a0bStron {
65320006a0bStron 	return (cmd_decode(list_fcmd_tables, cmd, sp));
65420006a0bStron }
65520006a0bStron 
65620006a0bStron /*
65720006a0bStron  * Decode a command from the edittables list.
65820006a0bStron  */
ecmd_decode(char * cmd,char ** sp)659838f5788Ssimonb public int ecmd_decode(char *cmd, char **sp)
66020006a0bStron {
66120006a0bStron 	return (cmd_decode(list_ecmd_tables, cmd, sp));
66220006a0bStron }
66320006a0bStron 
66420006a0bStron /*
66520006a0bStron  * Get the value of an environment variable.
66620006a0bStron  * Looks first in the lesskey file, then in the real environment.
66720006a0bStron  */
lgetenv(char * var)668838f5788Ssimonb public char * lgetenv(char *var)
66920006a0bStron {
67020006a0bStron 	int a;
67120006a0bStron 	char *s;
67220006a0bStron 
67320006a0bStron 	a = cmd_decode(list_var_tables, var, &s);
67420006a0bStron 	if (a == EV_OK)
67520006a0bStron 		return (s);
67620006a0bStron 	s = getenv(var);
67720006a0bStron 	if (s != NULL && *s != '\0')
67820006a0bStron 		return (s);
67920006a0bStron 	a = cmd_decode(list_sysvar_tables, var, &s);
68020006a0bStron 	if (a == EV_OK)
68120006a0bStron 		return (s);
68220006a0bStron 	return (NULL);
68320006a0bStron }
68420006a0bStron 
685838f5788Ssimonb /*
686838f5788Ssimonb  * Is a string null or empty?
687838f5788Ssimonb  */
isnullenv(char * s)688838f5788Ssimonb public int isnullenv(char *s)
689838f5788Ssimonb {
690838f5788Ssimonb 	return (s == NULL || *s == '\0');
691838f5788Ssimonb }
692838f5788Ssimonb 
69320006a0bStron #if USERFILE
69420006a0bStron /*
69520006a0bStron  * Get an "integer" from a lesskey file.
69620006a0bStron  * Integers are stored in a funny format:
69720006a0bStron  * two bytes, low order first, in radix KRADIX.
69820006a0bStron  */
gint(char ** sp)699838f5788Ssimonb static int gint(char **sp)
70020006a0bStron {
70120006a0bStron 	int n;
70220006a0bStron 
70320006a0bStron 	n = *(*sp)++;
70420006a0bStron 	n += *(*sp)++ * KRADIX;
70520006a0bStron 	return (n);
70620006a0bStron }
70720006a0bStron 
70820006a0bStron /*
70920006a0bStron  * Process an old (pre-v241) lesskey file.
71020006a0bStron  */
old_lesskey(char * buf,int len)711838f5788Ssimonb static int old_lesskey(char *buf, int len)
71220006a0bStron {
71320006a0bStron 	/*
71420006a0bStron 	 * Old-style lesskey file.
71520006a0bStron 	 * The file must end with either
71620006a0bStron 	 *     ...,cmd,0,action
71720006a0bStron 	 * or  ...,cmd,0,action|A_EXTRA,string,0
71820006a0bStron 	 * So the last byte or the second to last byte must be zero.
71920006a0bStron 	 */
72020006a0bStron 	if (buf[len-1] != '\0' && buf[len-2] != '\0')
72120006a0bStron 		return (-1);
72220006a0bStron 	add_fcmd_table(buf, len);
72320006a0bStron 	return (0);
72420006a0bStron }
72520006a0bStron 
72620006a0bStron /*
72720006a0bStron  * Process a new (post-v241) lesskey file.
72820006a0bStron  */
new_lesskey(char * buf,int len,int sysvar)729838f5788Ssimonb static int new_lesskey(char *buf, int len, int sysvar)
73020006a0bStron {
73120006a0bStron 	char *p;
732838f5788Ssimonb 	char *end;
733838f5788Ssimonb 	int c;
734838f5788Ssimonb 	int n;
73520006a0bStron 
73620006a0bStron 	/*
73720006a0bStron 	 * New-style lesskey file.
73820006a0bStron 	 * Extract the pieces.
73920006a0bStron 	 */
74020006a0bStron 	if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
74120006a0bStron 	    buf[len-2] != C1_END_LESSKEY_MAGIC ||
74220006a0bStron 	    buf[len-1] != C2_END_LESSKEY_MAGIC)
74320006a0bStron 		return (-1);
74420006a0bStron 	p = buf + 4;
745838f5788Ssimonb 	end = buf + len;
74620006a0bStron 	for (;;)
74720006a0bStron 	{
74820006a0bStron 		c = *p++;
74920006a0bStron 		switch (c)
75020006a0bStron 		{
75120006a0bStron 		case CMD_SECTION:
75220006a0bStron 			n = gint(&p);
753838f5788Ssimonb 			if (n < 0 || p+n >= end)
754838f5788Ssimonb 				return (-1);
75520006a0bStron 			add_fcmd_table(p, n);
75620006a0bStron 			p += n;
75720006a0bStron 			break;
75820006a0bStron 		case EDIT_SECTION:
75920006a0bStron 			n = gint(&p);
760838f5788Ssimonb 			if (n < 0 || p+n >= end)
761838f5788Ssimonb 				return (-1);
76220006a0bStron 			add_ecmd_table(p, n);
76320006a0bStron 			p += n;
76420006a0bStron 			break;
76520006a0bStron 		case VAR_SECTION:
76620006a0bStron 			n = gint(&p);
767838f5788Ssimonb 			if (n < 0 || p+n >= end)
768838f5788Ssimonb 				return (-1);
76920006a0bStron 			add_var_table((sysvar) ?
77020006a0bStron 				&list_sysvar_tables : &list_var_tables, p, n);
77120006a0bStron 			p += n;
77220006a0bStron 			break;
77320006a0bStron 		case END_SECTION:
77420006a0bStron 			return (0);
77520006a0bStron 		default:
77620006a0bStron 			/*
77720006a0bStron 			 * Unrecognized section type.
77820006a0bStron 			 */
77920006a0bStron 			return (-1);
78020006a0bStron 		}
78120006a0bStron 	}
78220006a0bStron }
78320006a0bStron 
78420006a0bStron /*
78520006a0bStron  * Set up a user command table, based on a "lesskey" file.
78620006a0bStron  */
lesskey(char * filename,int sysvar)787838f5788Ssimonb public int lesskey(char *filename, int sysvar)
78820006a0bStron {
789838f5788Ssimonb 	char *buf;
790838f5788Ssimonb 	POSITION len;
791838f5788Ssimonb 	long n;
792838f5788Ssimonb 	int f;
79320006a0bStron 
79420006a0bStron 	if (secure)
79520006a0bStron 		return (1);
79620006a0bStron 	/*
79720006a0bStron 	 * Try to open the lesskey file.
79820006a0bStron 	 */
79920006a0bStron 	f = open(filename, OPEN_READ);
80020006a0bStron 	if (f < 0)
80120006a0bStron 		return (1);
80220006a0bStron 
80320006a0bStron 	/*
80420006a0bStron 	 * Read the file into a buffer.
80520006a0bStron 	 * We first figure out the size of the file and allocate space for it.
80620006a0bStron 	 * {{ Minimal error checking is done here.
80720006a0bStron 	 *    A garbage .less file will produce strange results.
80820006a0bStron 	 *    To avoid a large amount of error checking code here, we
80920006a0bStron 	 *    rely on the lesskey program to generate a good .less file. }}
81020006a0bStron 	 */
81120006a0bStron 	len = filesize(f);
81220006a0bStron 	if (len == NULL_POSITION || len < 3)
81320006a0bStron 	{
81420006a0bStron 		/*
81520006a0bStron 		 * Bad file (valid file must have at least 3 chars).
81620006a0bStron 		 */
81720006a0bStron 		close(f);
81820006a0bStron 		return (-1);
81920006a0bStron 	}
82020006a0bStron 	if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL)
82120006a0bStron 	{
82220006a0bStron 		close(f);
82320006a0bStron 		return (-1);
82420006a0bStron 	}
82520006a0bStron 	if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
82620006a0bStron 	{
82720006a0bStron 		free(buf);
82820006a0bStron 		close(f);
82920006a0bStron 		return (-1);
83020006a0bStron 	}
83120006a0bStron 	n = read(f, buf, (unsigned int) len);
83220006a0bStron 	close(f);
83320006a0bStron 	if (n != len)
83420006a0bStron 	{
83520006a0bStron 		free(buf);
83620006a0bStron 		return (-1);
83720006a0bStron 	}
83820006a0bStron 
83920006a0bStron 	/*
84020006a0bStron 	 * Figure out if this is an old-style (before version 241)
84120006a0bStron 	 * or new-style lesskey file format.
84220006a0bStron 	 */
843838f5788Ssimonb 	if (len < 4 ||
844838f5788Ssimonb 	    buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
84520006a0bStron 	    buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
84620006a0bStron 		return (old_lesskey(buf, (int)len));
84720006a0bStron 	return (new_lesskey(buf, (int)len, sysvar));
84820006a0bStron }
84920006a0bStron 
850838f5788Ssimonb #if HAVE_LESSKEYSRC
lesskey_src(char * filename,int sysvar)851838f5788Ssimonb public int lesskey_src(char *filename, int sysvar)
852838f5788Ssimonb {
853838f5788Ssimonb 	static struct lesskey_tables tables;
854838f5788Ssimonb 	int r = parse_lesskey(filename, &tables);
855838f5788Ssimonb 	if (r != 0)
856838f5788Ssimonb 		return (r);
857838f5788Ssimonb 	add_fcmd_table(xbuf_char_data(&tables.cmdtable.buf), tables.cmdtable.buf.end);
858838f5788Ssimonb 	add_ecmd_table(xbuf_char_data(&tables.edittable.buf), tables.edittable.buf.end);
859838f5788Ssimonb 	add_var_table(sysvar ? &list_sysvar_tables : &list_var_tables,
860838f5788Ssimonb 		xbuf_char_data(&tables.vartable.buf), tables.vartable.buf.end);
861838f5788Ssimonb 	return (0);
862838f5788Ssimonb }
863838f5788Ssimonb 
lesskey_parse_error(char * s)864838f5788Ssimonb void lesskey_parse_error(char *s)
865838f5788Ssimonb {
866838f5788Ssimonb 	PARG parg;
867838f5788Ssimonb 	parg.p_string = s;
868838f5788Ssimonb 	error("%s", &parg);
869838f5788Ssimonb }
870838f5788Ssimonb #endif /* HAVE_LESSKEYSRC */
871838f5788Ssimonb 
87220006a0bStron /*
873838f5788Ssimonb  * Add a lesskey file.
87420006a0bStron  */
add_hometable(int (* call_lesskey)(char *,int),char * envname,char * def_filename,int sysvar)875838f5788Ssimonb public int add_hometable(int (*call_lesskey)(char *, int), char *envname, char *def_filename, int sysvar)
87620006a0bStron {
87720006a0bStron 	char *filename;
878838f5788Ssimonb 	int r;
87920006a0bStron 
88020006a0bStron 	if (envname != NULL && (filename = lgetenv(envname)) != NULL)
88120006a0bStron 		filename = save(filename);
882838f5788Ssimonb 	else if (sysvar) /* def_filename is full path */
88320006a0bStron 		filename = save(def_filename);
884838f5788Ssimonb 	else /* def_filename is just basename */
88520006a0bStron 	{
886838f5788Ssimonb 		/* Remove first char (normally a dot) unless stored in $HOME. */
887838f5788Ssimonb 		char *xdg = lgetenv("XDG_CONFIG_HOME");
888838f5788Ssimonb 		if (!isnullenv(xdg))
889838f5788Ssimonb 			filename = dirfile(xdg, &def_filename[1], 1);
890838f5788Ssimonb 		if (filename == NULL)
891838f5788Ssimonb 		{
892838f5788Ssimonb 			char *home = lgetenv("HOME");
893838f5788Ssimonb 			if (!isnullenv(home))
894838f5788Ssimonb 			{
895838f5788Ssimonb 				char *cfg_dir = dirfile(home, ".config", 0);
896838f5788Ssimonb 				filename = dirfile(cfg_dir, &def_filename[1], 1);
897838f5788Ssimonb 				free(cfg_dir);
89820006a0bStron 			}
899838f5788Ssimonb 		}
900838f5788Ssimonb 		if (filename == NULL)
901838f5788Ssimonb 			filename = homefile(def_filename);
902838f5788Ssimonb 	}
903838f5788Ssimonb 	if (filename == NULL)
904838f5788Ssimonb 		return -1;
905838f5788Ssimonb 	r = (*call_lesskey)(filename, sysvar);
90620006a0bStron 	free(filename);
907838f5788Ssimonb 	return (r);
90820006a0bStron }
90920006a0bStron #endif
91020006a0bStron 
91120006a0bStron /*
91220006a0bStron  * See if a char is a special line-editing command.
91320006a0bStron  */
editchar(int c,int flags)914838f5788Ssimonb public int editchar(int c, int flags)
91520006a0bStron {
91620006a0bStron 	int action;
91720006a0bStron 	int nch;
918*76b10136Ssimonb 	char *s;
91920006a0bStron 	char usercmd[MAX_CMDLEN+1];
92020006a0bStron 
92120006a0bStron 	/*
92220006a0bStron 	 * An editing character could actually be a sequence of characters;
92320006a0bStron 	 * for example, an escape sequence sent by pressing the uparrow key.
92420006a0bStron 	 * To match the editing string, we use the command decoder
92520006a0bStron 	 * but give it the edit-commands command table
92620006a0bStron 	 * This table is constructed to match the user's keyboard.
92720006a0bStron 	 */
92820006a0bStron 	if (c == erase_char || c == erase2_char)
92920006a0bStron 		return (EC_BACKSPACE);
93020006a0bStron 	if (c == kill_char)
931838f5788Ssimonb 	{
932838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
933838f5788Ssimonb 		if (!win32_kbhit())
934838f5788Ssimonb #endif
93520006a0bStron 		return (EC_LINEKILL);
936838f5788Ssimonb 	}
93720006a0bStron 
93820006a0bStron 	/*
93920006a0bStron 	 * Collect characters in a buffer.
94020006a0bStron 	 * Start with the one we have, and get more if we need them.
94120006a0bStron 	 */
94220006a0bStron 	nch = 0;
94320006a0bStron 	do {
94420006a0bStron 	        if (nch > 0)
94520006a0bStron 			c = getcc();
94620006a0bStron 		usercmd[nch] = c;
94720006a0bStron 		usercmd[nch+1] = '\0';
94820006a0bStron 		nch++;
94920006a0bStron 		action = ecmd_decode(usercmd, &s);
950838f5788Ssimonb 	} while (action == A_PREFIX && nch < MAX_CMDLEN);
95120006a0bStron 
952838f5788Ssimonb 	if (action == EC_X11MOUSE)
953838f5788Ssimonb 		return (x11mouse_action(1));
954838f5788Ssimonb 	if (action == EC_X116MOUSE)
955838f5788Ssimonb 		return (x116mouse_action(1));
956838f5788Ssimonb 
957838f5788Ssimonb 	if (flags & ECF_NORIGHTLEFT)
95820006a0bStron 	{
95920006a0bStron 		switch (action)
96020006a0bStron 		{
96120006a0bStron 		case EC_RIGHT:
96220006a0bStron 		case EC_LEFT:
96320006a0bStron 			action = A_INVALID;
96420006a0bStron 			break;
96520006a0bStron 		}
96620006a0bStron 	}
96720006a0bStron #if CMD_HISTORY
968838f5788Ssimonb 	if (flags & ECF_NOHISTORY)
96920006a0bStron 	{
97020006a0bStron 		/*
97120006a0bStron 		 * The caller says there is no history list.
97220006a0bStron 		 * Reject any history-manipulation action.
97320006a0bStron 		 */
97420006a0bStron 		switch (action)
97520006a0bStron 		{
97620006a0bStron 		case EC_UP:
97720006a0bStron 		case EC_DOWN:
97820006a0bStron 			action = A_INVALID;
97920006a0bStron 			break;
98020006a0bStron 		}
98120006a0bStron 	}
98220006a0bStron #endif
98320006a0bStron #if TAB_COMPLETE_FILENAME
984838f5788Ssimonb 	if (flags & ECF_NOCOMPLETE)
98520006a0bStron 	{
98620006a0bStron 		/*
98720006a0bStron 		 * The caller says we don't want any filename completion cmds.
98820006a0bStron 		 * Reject them.
98920006a0bStron 		 */
99020006a0bStron 		switch (action)
99120006a0bStron 		{
99220006a0bStron 		case EC_F_COMPLETE:
99320006a0bStron 		case EC_B_COMPLETE:
99420006a0bStron 		case EC_EXPAND:
99520006a0bStron 			action = A_INVALID;
99620006a0bStron 			break;
99720006a0bStron 		}
99820006a0bStron 	}
99920006a0bStron #endif
1000838f5788Ssimonb 	if ((flags & ECF_PEEK) || action == A_INVALID)
100120006a0bStron 	{
100220006a0bStron 		/*
100320006a0bStron 		 * We're just peeking, or we didn't understand the command.
100420006a0bStron 		 * Unget all the characters we read in the loop above.
100520006a0bStron 		 * This does NOT include the original character that was
100620006a0bStron 		 * passed in as a parameter.
100720006a0bStron 		 */
100820006a0bStron 		while (nch > 1)
100920006a0bStron 		{
101020006a0bStron 			ungetcc(usercmd[--nch]);
101120006a0bStron 		}
101220006a0bStron 	} else
101320006a0bStron 	{
101420006a0bStron 		if (s != NULL)
101520006a0bStron 			ungetsc(s);
101620006a0bStron 	}
101720006a0bStron 	return action;
101820006a0bStron }
101920006a0bStron 
1020