decode.c revision 240121
1127664Sbms/*
2127664Sbms * Copyright (C) 1984-2012  Mark Nudelman
3127664Sbms *
4127664Sbms * You may distribute under the terms of either the GNU General Public
5127664Sbms * License or the Less License, as specified in the README file.
6127664Sbms *
7127664Sbms * For more information, see the README file.
8127664Sbms */
9127664Sbms
10127664Sbms
11127664Sbms/*
12127664Sbms * Routines to decode user commands.
13127664Sbms *
14127664Sbms * This is all table driven.
15127664Sbms * A command table is a sequence of command descriptors.
16127664Sbms * Each command descriptor is a sequence of bytes with the following format:
17127664Sbms *	<c1><c2>...<cN><0><action>
18127664Sbms * The characters c1,c2,...,cN are the command string; that is,
19127664Sbms * the characters which the user must type.
20127664Sbms * It is terminated by a null <0> byte.
21127664Sbms * The byte after the null byte is the action code associated
22127664Sbms * with the command string.
23127664Sbms * If an action byte is OR-ed with A_EXTRA, this indicates
24127664Sbms * that the option byte is followed by an extra string.
25127664Sbms *
26127664Sbms * There may be many command tables.
27127664Sbms * The first (default) table is built-in.
28127664Sbms * Other tables are read in from "lesskey" files.
29127664Sbms * All the tables are linked together and are searched in order.
30127664Sbms */
31127664Sbms
32127664Sbms#include "less.h"
33127664Sbms#include "cmd.h"
34127664Sbms#include "lesskey.h"
35127664Sbms
36127664Sbmsextern int erase_char, erase2_char, kill_char;
37146768Ssamextern int secure;
38127664Sbms
39127664Sbms#define SK(k) \
40127664Sbms	SK_SPECIAL_KEY, (k), 6, 1, 1, 1
41127664Sbms/*
42127664Sbms * Command table is ordered roughly according to expected
43127664Sbms * frequency of use, so the common commands are near the beginning.
44127664Sbms */
45127664Sbms
46127664Sbmsstatic unsigned char cmdtable[] =
47127664Sbms{
48127664Sbms	'\r',0,				A_F_LINE,
49127664Sbms	'\n',0,				A_F_LINE,
50127664Sbms	'e',0,				A_F_LINE,
51127664Sbms	'j',0,				A_F_LINE,
52127664Sbms	SK(SK_DOWN_ARROW),0,		A_F_LINE,
53127664Sbms	CONTROL('E'),0,			A_F_LINE,
54127664Sbms	CONTROL('N'),0,			A_F_LINE,
55127664Sbms	'k',0,				A_B_LINE,
56127664Sbms	'y',0,				A_B_LINE,
57127664Sbms	CONTROL('Y'),0,			A_B_LINE,
58127664Sbms	SK(SK_CONTROL_K),0,		A_B_LINE,
59127664Sbms	CONTROL('P'),0,			A_B_LINE,
60127664Sbms	SK(SK_UP_ARROW),0,		A_B_LINE,
61127664Sbms	'J',0,				A_FF_LINE,
62127664Sbms	'K',0,				A_BF_LINE,
63127664Sbms	'Y',0,				A_BF_LINE,
64127664Sbms	'd',0,				A_F_SCROLL,
65127664Sbms	CONTROL('D'),0,			A_F_SCROLL,
66127664Sbms	'u',0,				A_B_SCROLL,
67127664Sbms	CONTROL('U'),0,			A_B_SCROLL,
68127664Sbms	' ',0,				A_F_SCREEN,
69127664Sbms	'f',0,				A_F_SCREEN,
70127664Sbms	CONTROL('F'),0,			A_F_SCREEN,
71127664Sbms	CONTROL('V'),0,			A_F_SCREEN,
72127664Sbms	SK(SK_PAGE_DOWN),0,		A_F_SCREEN,
73127664Sbms	'b',0,				A_B_SCREEN,
74127664Sbms	CONTROL('B'),0,			A_B_SCREEN,
75127664Sbms	ESC,'v',0,			A_B_SCREEN,
76127664Sbms	SK(SK_PAGE_UP),0,		A_B_SCREEN,
77127664Sbms	'z',0,				A_F_WINDOW,
78127664Sbms	'w',0,				A_B_WINDOW,
79127664Sbms	ESC,' ',0,			A_FF_SCREEN,
80127664Sbms	'F',0,				A_F_FOREVER,
81127664Sbms	ESC,'F',0,			A_F_UNTIL_HILITE,
82127664Sbms	'R',0,				A_FREPAINT,
83127664Sbms	'r',0,				A_REPAINT,
84127664Sbms	CONTROL('R'),0,			A_REPAINT,
85127664Sbms	CONTROL('L'),0,			A_REPAINT,
86127664Sbms	ESC,'u',0,			A_UNDO_SEARCH,
87127664Sbms	'g',0,				A_GOLINE,
88127664Sbms	SK(SK_HOME),0,			A_GOLINE,
89127664Sbms	'<',0,				A_GOLINE,
90127664Sbms	ESC,'<',0,			A_GOLINE,
91127664Sbms	'p',0,				A_PERCENT,
92146768Ssam	'%',0,				A_PERCENT,
93146768Ssam	ESC,'[',0,			A_LSHIFT,
94146768Ssam	ESC,']',0,			A_RSHIFT,
95127664Sbms	ESC,'(',0,			A_LSHIFT,
96127664Sbms	ESC,')',0,			A_RSHIFT,
97127664Sbms	SK(SK_RIGHT_ARROW),0,		A_RSHIFT,
98127664Sbms	SK(SK_LEFT_ARROW),0,		A_LSHIFT,
99127664Sbms	'{',0,				A_F_BRACKET|A_EXTRA,	'{','}',0,
100127664Sbms	'}',0,				A_B_BRACKET|A_EXTRA,	'{','}',0,
101127664Sbms	'(',0,				A_F_BRACKET|A_EXTRA,	'(',')',0,
102127664Sbms	')',0,				A_B_BRACKET|A_EXTRA,	'(',')',0,
103127664Sbms	'[',0,				A_F_BRACKET|A_EXTRA,	'[',']',0,
104127664Sbms	']',0,				A_B_BRACKET|A_EXTRA,	'[',']',0,
105127664Sbms	ESC,CONTROL('F'),0,		A_F_BRACKET,
106127664Sbms	ESC,CONTROL('B'),0,		A_B_BRACKET,
107127664Sbms	'G',0,				A_GOEND,
108127664Sbms	ESC,'>',0,			A_GOEND,
109127664Sbms	'>',0,				A_GOEND,
110127664Sbms	SK(SK_END),0,			A_GOEND,
111127664Sbms	'P',0,				A_GOPOS,
112127664Sbms
113127664Sbms	'0',0,				A_DIGIT,
114127664Sbms	'1',0,				A_DIGIT,
115127664Sbms	'2',0,				A_DIGIT,
116127664Sbms	'3',0,				A_DIGIT,
117127664Sbms	'4',0,				A_DIGIT,
118127664Sbms	'5',0,				A_DIGIT,
119127664Sbms	'6',0,				A_DIGIT,
120127664Sbms	'7',0,				A_DIGIT,
121127664Sbms	'8',0,				A_DIGIT,
122127664Sbms	'9',0,				A_DIGIT,
123127664Sbms	'.',0,				A_DIGIT,
124127664Sbms
125127664Sbms	'=',0,				A_STAT,
126127664Sbms	CONTROL('G'),0,			A_STAT,
127127664Sbms	':','f',0,			A_STAT,
128127664Sbms	'/',0,				A_F_SEARCH,
129127664Sbms	'?',0,				A_B_SEARCH,
130127664Sbms	ESC,'/',0,			A_F_SEARCH|A_EXTRA,	'*',0,
131127664Sbms	ESC,'?',0,			A_B_SEARCH|A_EXTRA,	'*',0,
132127664Sbms	'n',0,				A_AGAIN_SEARCH,
133127664Sbms	ESC,'n',0,			A_T_AGAIN_SEARCH,
134127664Sbms	'N',0,				A_REVERSE_SEARCH,
135127664Sbms	ESC,'N',0,			A_T_REVERSE_SEARCH,
136127664Sbms	'&',0,				A_FILTER,
137127664Sbms	'm',0,				A_SETMARK,
138127664Sbms	'\'',0,				A_GOMARK,
139127664Sbms	CONTROL('X'),CONTROL('X'),0,	A_GOMARK,
140127664Sbms	'E',0,				A_EXAMINE,
141127664Sbms	':','e',0,			A_EXAMINE,
142127664Sbms	CONTROL('X'),CONTROL('V'),0,	A_EXAMINE,
143127664Sbms	':','n',0,			A_NEXT_FILE,
144127664Sbms	':','p',0,			A_PREV_FILE,
145127664Sbms	't',0,				A_NEXT_TAG,
146127664Sbms	'T',0,				A_PREV_TAG,
147127664Sbms	':','x',0,			A_INDEX_FILE,
148127664Sbms	':','d',0,			A_REMOVE_FILE,
149127664Sbms	'-',0,				A_OPT_TOGGLE,
150127664Sbms	':','t',0,			A_OPT_TOGGLE|A_EXTRA,	't',0,
151127664Sbms	's',0,				A_OPT_TOGGLE|A_EXTRA,	'o',0,
152127664Sbms	'_',0,				A_DISP_OPTION,
153127664Sbms	'|',0,				A_PIPE,
154127664Sbms	'v',0,				A_VISUAL,
155127664Sbms	'!',0,				A_SHELL,
156127664Sbms	'+',0,				A_FIRSTCMD,
157127664Sbms
158127664Sbms	'H',0,				A_HELP,
159127664Sbms	'h',0,				A_HELP,
160127664Sbms	SK(SK_F1),0,			A_HELP,
161127664Sbms	'V',0,				A_VERSION,
162127664Sbms	'q',0,				A_QUIT,
163127664Sbms	'Q',0,				A_QUIT,
164127664Sbms	':','q',0,			A_QUIT,
165127664Sbms	':','Q',0,			A_QUIT,
166127664Sbms	'Z','Z',0,			A_QUIT
167127664Sbms};
168127664Sbms
169127664Sbmsstatic unsigned char edittable[] =
170127664Sbms{
171127664Sbms	'\t',0,	    			EC_F_COMPLETE,	/* TAB */
172127664Sbms	'\17',0,			EC_B_COMPLETE,	/* BACKTAB */
173127664Sbms	SK(SK_BACKTAB),0,		EC_B_COMPLETE,	/* BACKTAB */
174127664Sbms	ESC,'\t',0,			EC_B_COMPLETE,	/* ESC TAB */
175127664Sbms	CONTROL('L'),0,			EC_EXPAND,	/* CTRL-L */
176127664Sbms	CONTROL('V'),0,			EC_LITERAL,	/* BACKSLASH */
177127664Sbms	CONTROL('A'),0,			EC_LITERAL,	/* BACKSLASH */
178127664Sbms   	ESC,'l',0,			EC_RIGHT,	/* ESC l */
179127664Sbms	SK(SK_RIGHT_ARROW),0,		EC_RIGHT,	/* RIGHTARROW */
180127664Sbms	ESC,'h',0,			EC_LEFT,	/* ESC h */
181146768Ssam	SK(SK_LEFT_ARROW),0,		EC_LEFT,	/* LEFTARROW */
182146768Ssam	ESC,'b',0,			EC_W_LEFT,	/* ESC b */
183146768Ssam	ESC,SK(SK_LEFT_ARROW),0,	EC_W_LEFT,	/* ESC LEFTARROW */
184146768Ssam	SK(SK_CTL_LEFT_ARROW),0,	EC_W_LEFT,	/* CTRL-LEFTARROW */
185146768Ssam	ESC,'w',0,			EC_W_RIGHT,	/* ESC w */
186146768Ssam	ESC,SK(SK_RIGHT_ARROW),0,	EC_W_RIGHT,	/* ESC RIGHTARROW */
187146768Ssam	SK(SK_CTL_RIGHT_ARROW),0,	EC_W_RIGHT,	/* CTRL-RIGHTARROW */
188146768Ssam	ESC,'i',0,			EC_INSERT,	/* ESC i */
189146768Ssam	SK(SK_INSERT),0,		EC_INSERT,	/* INSERT */
190146768Ssam	ESC,'x',0,			EC_DELETE,	/* ESC x */
191146768Ssam	SK(SK_DELETE),0,		EC_DELETE,	/* DELETE */
192146768Ssam	ESC,'X',0,			EC_W_DELETE,	/* ESC X */
193146768Ssam	ESC,SK(SK_DELETE),0,		EC_W_DELETE,	/* ESC DELETE */
194146768Ssam	SK(SK_CTL_DELETE),0,		EC_W_DELETE,	/* CTRL-DELETE */
195146768Ssam	SK(SK_CTL_BACKSPACE),0,		EC_W_BACKSPACE, /* CTRL-BACKSPACE */
196146768Ssam	ESC,'\b',0,			EC_W_BACKSPACE,	/* ESC BACKSPACE */
197146768Ssam	ESC,'0',0,			EC_HOME,	/* ESC 0 */
198146768Ssam	SK(SK_HOME),0,			EC_HOME,	/* HOME */
199146768Ssam	ESC,'$',0,			EC_END,		/* ESC $ */
200146768Ssam	SK(SK_END),0,			EC_END,		/* END */
201146768Ssam	ESC,'k',0,			EC_UP,		/* ESC k */
202146768Ssam	SK(SK_UP_ARROW),0,		EC_UP,		/* UPARROW */
203146768Ssam	ESC,'j',0,			EC_DOWN,	/* ESC j */
204146768Ssam	SK(SK_DOWN_ARROW),0,		EC_DOWN,	/* DOWNARROW */
205146768Ssam	CONTROL('G'),0,			EC_ABORT,	/* CTRL-G */
206146768Ssam};
207146768Ssam
208146768Ssam/*
209146768Ssam * Structure to support a list of command tables.
210146768Ssam */
211127664Sbmsstruct tablelist
212127664Sbms{
213127664Sbms	struct tablelist *t_next;
214127664Sbms	char *t_start;
215127664Sbms	char *t_end;
216127664Sbms};
217127664Sbms
218127664Sbms/*
219127664Sbms * List of command tables and list of line-edit tables.
220127664Sbms */
221127664Sbmsstatic struct tablelist *list_fcmd_tables = NULL;
222127664Sbmsstatic struct tablelist *list_ecmd_tables = NULL;
223127664Sbmsstatic struct tablelist *list_var_tables = NULL;
224127664Sbmsstatic struct tablelist *list_sysvar_tables = NULL;
225127664Sbms
226127664Sbms
227127664Sbms/*
228127664Sbms * Expand special key abbreviations in a command table.
229127664Sbms */
230127664Sbms	static void
231127664Sbmsexpand_special_keys(table, len)
232127664Sbms	char *table;
233127664Sbms	int len;
234127664Sbms{
235127664Sbms	register char *fm;
236127664Sbms	register char *to;
237127664Sbms	register int a;
238127664Sbms	char *repl;
239127664Sbms	int klen;
240127664Sbms
241127664Sbms	for (fm = table;  fm < table + len; )
242127664Sbms	{
243127664Sbms		/*
244127664Sbms		 * Rewrite each command in the table with any
245127664Sbms		 * special key abbreviations expanded.
246127664Sbms		 */
247127664Sbms		for (to = fm;  *fm != '\0'; )
248127664Sbms		{
249127664Sbms			if (*fm != SK_SPECIAL_KEY)
250127664Sbms			{
251127664Sbms				*to++ = *fm++;
252127664Sbms				continue;
253127664Sbms			}
254127664Sbms			/*
255127664Sbms			 * After SK_SPECIAL_KEY, next byte is the type
256127664Sbms			 * of special key (one of the SK_* contants),
257127664Sbms			 * and the byte after that is the number of bytes,
258127664Sbms			 * N, reserved by the abbreviation (including the
259127664Sbms			 * SK_SPECIAL_KEY and key type bytes).
260127664Sbms			 * Replace all N bytes with the actual bytes
261127664Sbms			 * output by the special key on this terminal.
262127664Sbms			 */
263127664Sbms			repl = special_key_str(fm[1]);
264127664Sbms			klen = fm[2] & 0377;
265127664Sbms			fm += klen;
266127664Sbms			if (repl == NULL || (int) strlen(repl) > klen)
267127664Sbms				repl = "\377";
268127664Sbms			while (*repl != '\0')
269127664Sbms				*to++ = *repl++;
270127664Sbms		}
271127664Sbms		*to++ = '\0';
272127664Sbms		/*
273127664Sbms		 * Fill any unused bytes between end of command and
274127664Sbms		 * the action byte with A_SKIP.
275127664Sbms		 */
276127664Sbms		while (to <= fm)
277127664Sbms			*to++ = A_SKIP;
278127664Sbms		fm++;
279127664Sbms		a = *fm++ & 0377;
280127664Sbms		if (a & A_EXTRA)
281127664Sbms		{
282127664Sbms			while (*fm++ != '\0')
283127664Sbms				continue;
284127664Sbms		}
285127664Sbms	}
286127664Sbms}
287127664Sbms
288127664Sbms/*
289127664Sbms * Initialize the command lists.
290127664Sbms */
291127664Sbms	public void
292127664Sbmsinit_cmds()
293127664Sbms{
294127664Sbms	/*
295127664Sbms	 * Add the default command tables.
296127664Sbms	 */
297127664Sbms	add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
298127664Sbms	add_ecmd_table((char*)edittable, sizeof(edittable));
299127664Sbms#if USERFILE
300127664Sbms	/*
301127664Sbms	 * For backwards compatibility,
302127664Sbms	 * try to add tables in the OLD system lesskey file.
303127664Sbms	 */
304127664Sbms#ifdef BINDIR
305127664Sbms	add_hometable(NULL, BINDIR "/.sysless", 1);
306127664Sbms#endif
307127664Sbms	/*
308127664Sbms	 * Try to add the tables in the system lesskey file.
309127664Sbms	 */
310127664Sbms	add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
311127664Sbms	/*
312127664Sbms	 * Try to add the tables in the standard lesskey file "$HOME/.less".
313127664Sbms	 */
314127664Sbms	add_hometable("LESSKEY", LESSKEYFILE, 0);
315127664Sbms#endif
316127664Sbms}
317127664Sbms
318127664Sbms/*
319127664Sbms * Add a command table.
320146768Ssam */
321127664Sbms	static int
322146768Ssamadd_cmd_table(tlist, buf, len)
323146768Ssam	struct tablelist **tlist;
324146768Ssam	char *buf;
325146768Ssam	int len;
326146768Ssam{
327146768Ssam	register struct tablelist *t;
328146768Ssam
329146768Ssam	if (len == 0)
330146768Ssam		return (0);
331146768Ssam	/*
332146768Ssam	 * Allocate a tablelist structure, initialize it,
333146768Ssam	 * and link it into the list of tables.
334146768Ssam	 */
335146768Ssam	if ((t = (struct tablelist *)
336146768Ssam			calloc(1, sizeof(struct tablelist))) == NULL)
337146768Ssam	{
338146768Ssam		return (-1);
339146768Ssam	}
340146768Ssam	expand_special_keys(buf, len);
341146768Ssam	t->t_start = buf;
342146768Ssam	t->t_end = buf + len;
343146768Ssam	t->t_next = *tlist;
344146768Ssam	*tlist = t;
345146768Ssam	return (0);
346146768Ssam}
347146768Ssam
348146768Ssam/*
349127664Sbms * Add a command table.
350127664Sbms */
351127664Sbms	public void
352127664Sbmsadd_fcmd_table(buf, len)
353127664Sbms	char *buf;
354127664Sbms	int len;
355127664Sbms{
356127664Sbms	if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
357127664Sbms		error("Warning: some commands disabled", NULL_PARG);
358127664Sbms}
359127664Sbms
360127664Sbms/*
361127664Sbms * Add an editing command table.
362127664Sbms */
363127664Sbms	public void
364127664Sbmsadd_ecmd_table(buf, len)
365127664Sbms	char *buf;
366127664Sbms	int len;
367127664Sbms{
368127664Sbms	if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
369127664Sbms		error("Warning: some edit commands disabled", NULL_PARG);
370127664Sbms}
371127664Sbms
372127664Sbms/*
373127664Sbms * Add an environment variable table.
374127664Sbms */
375127664Sbms	static void
376127664Sbmsadd_var_table(tlist, buf, len)
377127664Sbms	struct tablelist **tlist;
378127664Sbms	char *buf;
379127664Sbms	int len;
380127664Sbms{
381127664Sbms	if (add_cmd_table(tlist, buf, len) < 0)
382127664Sbms		error("Warning: environment variables from lesskey file unavailable", NULL_PARG);
383127664Sbms}
384127664Sbms
385127664Sbms/*
386127664Sbms * Search a single command table for the command string in cmd.
387 */
388	static int
389cmd_search(cmd, table, endtable, sp)
390	char *cmd;
391	char *table;
392	char *endtable;
393	char **sp;
394{
395	register char *p;
396	register char *q;
397	register int a;
398
399	*sp = NULL;
400	for (p = table, q = cmd;  p < endtable;  p++, q++)
401	{
402		if (*p == *q)
403		{
404			/*
405			 * Current characters match.
406			 * If we're at the end of the string, we've found it.
407			 * Return the action code, which is the character
408			 * after the null at the end of the string
409			 * in the command table.
410			 */
411			if (*p == '\0')
412			{
413				a = *++p & 0377;
414				while (a == A_SKIP)
415					a = *++p & 0377;
416				if (a == A_END_LIST)
417				{
418					/*
419					 * We get here only if the original
420					 * cmd string passed in was empty ("").
421					 * I don't think that can happen,
422					 * but just in case ...
423					 */
424					return (A_UINVALID);
425				}
426				/*
427				 * Check for an "extra" string.
428				 */
429				if (a & A_EXTRA)
430				{
431					*sp = ++p;
432					a &= ~A_EXTRA;
433				}
434				return (a);
435			}
436		} else if (*q == '\0')
437		{
438			/*
439			 * Hit the end of the user's command,
440			 * but not the end of the string in the command table.
441			 * The user's command is incomplete.
442			 */
443			return (A_PREFIX);
444		} else
445		{
446			/*
447			 * Not a match.
448			 * Skip ahead to the next command in the
449			 * command table, and reset the pointer
450			 * to the beginning of the user's command.
451			 */
452			if (*p == '\0' && p[1] == A_END_LIST)
453			{
454				/*
455				 * A_END_LIST is a special marker that tells
456				 * us to abort the cmd search.
457				 */
458				return (A_UINVALID);
459			}
460			while (*p++ != '\0')
461				continue;
462			while (*p == A_SKIP)
463				p++;
464			if (*p & A_EXTRA)
465				while (*++p != '\0')
466					continue;
467			q = cmd-1;
468		}
469	}
470	/*
471	 * No match found in the entire command table.
472	 */
473	return (A_INVALID);
474}
475
476/*
477 * Decode a command character and return the associated action.
478 * The "extra" string, if any, is returned in sp.
479 */
480	static int
481cmd_decode(tlist, cmd, sp)
482	struct tablelist *tlist;
483	char *cmd;
484	char **sp;
485{
486	register struct tablelist *t;
487	register int action = A_INVALID;
488
489	/*
490	 * Search thru all the command tables.
491	 * Stop when we find an action which is not A_INVALID.
492	 */
493	for (t = tlist;  t != NULL;  t = t->t_next)
494	{
495		action = cmd_search(cmd, t->t_start, t->t_end, sp);
496		if (action != A_INVALID)
497			break;
498	}
499	if (action == A_UINVALID)
500		action = A_INVALID;
501	return (action);
502}
503
504/*
505 * Decode a command from the cmdtables list.
506 */
507	public int
508fcmd_decode(cmd, sp)
509	char *cmd;
510	char **sp;
511{
512	return (cmd_decode(list_fcmd_tables, cmd, sp));
513}
514
515/*
516 * Decode a command from the edittables list.
517 */
518	public int
519ecmd_decode(cmd, sp)
520	char *cmd;
521	char **sp;
522{
523	return (cmd_decode(list_ecmd_tables, cmd, sp));
524}
525
526/*
527 * Get the value of an environment variable.
528 * Looks first in the lesskey file, then in the real environment.
529 */
530	public char *
531lgetenv(var)
532	char *var;
533{
534	int a;
535	char *s;
536
537	a = cmd_decode(list_var_tables, var, &s);
538	if (a == EV_OK)
539		return (s);
540	s = getenv(var);
541	if (s != NULL && *s != '\0')
542		return (s);
543	a = cmd_decode(list_sysvar_tables, var, &s);
544	if (a == EV_OK)
545		return (s);
546	return (NULL);
547}
548
549#if USERFILE
550/*
551 * Get an "integer" from a lesskey file.
552 * Integers are stored in a funny format:
553 * two bytes, low order first, in radix KRADIX.
554 */
555	static int
556gint(sp)
557	char **sp;
558{
559	int n;
560
561	n = *(*sp)++;
562	n += *(*sp)++ * KRADIX;
563	return (n);
564}
565
566/*
567 * Process an old (pre-v241) lesskey file.
568 */
569	static int
570old_lesskey(buf, len)
571	char *buf;
572	int len;
573{
574	/*
575	 * Old-style lesskey file.
576	 * The file must end with either
577	 *     ...,cmd,0,action
578	 * or  ...,cmd,0,action|A_EXTRA,string,0
579	 * So the last byte or the second to last byte must be zero.
580	 */
581	if (buf[len-1] != '\0' && buf[len-2] != '\0')
582		return (-1);
583	add_fcmd_table(buf, len);
584	return (0);
585}
586
587/*
588 * Process a new (post-v241) lesskey file.
589 */
590	static int
591new_lesskey(buf, len, sysvar)
592	char *buf;
593	int len;
594	int sysvar;
595{
596	char *p;
597	register int c;
598	register int n;
599
600	/*
601	 * New-style lesskey file.
602	 * Extract the pieces.
603	 */
604	if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
605	    buf[len-2] != C1_END_LESSKEY_MAGIC ||
606	    buf[len-1] != C2_END_LESSKEY_MAGIC)
607		return (-1);
608	p = buf + 4;
609	for (;;)
610	{
611		c = *p++;
612		switch (c)
613		{
614		case CMD_SECTION:
615			n = gint(&p);
616			add_fcmd_table(p, n);
617			p += n;
618			break;
619		case EDIT_SECTION:
620			n = gint(&p);
621			add_ecmd_table(p, n);
622			p += n;
623			break;
624		case VAR_SECTION:
625			n = gint(&p);
626			add_var_table((sysvar) ?
627				&list_sysvar_tables : &list_var_tables, p, n);
628			p += n;
629			break;
630		case END_SECTION:
631			return (0);
632		default:
633			/*
634			 * Unrecognized section type.
635			 */
636			return (-1);
637		}
638	}
639}
640
641/*
642 * Set up a user command table, based on a "lesskey" file.
643 */
644	public int
645lesskey(filename, sysvar)
646	char *filename;
647	int sysvar;
648{
649	register char *buf;
650	register POSITION len;
651	register long n;
652	register int f;
653
654	if (secure)
655		return (1);
656	/*
657	 * Try to open the lesskey file.
658	 */
659	filename = shell_unquote(filename);
660	f = open(filename, OPEN_READ);
661	free(filename);
662	if (f < 0)
663		return (1);
664
665	/*
666	 * Read the file into a buffer.
667	 * We first figure out the size of the file and allocate space for it.
668	 * {{ Minimal error checking is done here.
669	 *    A garbage .less file will produce strange results.
670	 *    To avoid a large amount of error checking code here, we
671	 *    rely on the lesskey program to generate a good .less file. }}
672	 */
673	len = filesize(f);
674	if (len == NULL_POSITION || len < 3)
675	{
676		/*
677		 * Bad file (valid file must have at least 3 chars).
678		 */
679		close(f);
680		return (-1);
681	}
682	if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL)
683	{
684		close(f);
685		return (-1);
686	}
687	if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
688	{
689		free(buf);
690		close(f);
691		return (-1);
692	}
693	n = read(f, buf, (unsigned int) len);
694	close(f);
695	if (n != len)
696	{
697		free(buf);
698		return (-1);
699	}
700
701	/*
702	 * Figure out if this is an old-style (before version 241)
703	 * or new-style lesskey file format.
704	 */
705	if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
706	    buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
707		return (old_lesskey(buf, (int)len));
708	return (new_lesskey(buf, (int)len, sysvar));
709}
710
711/*
712 * Add the standard lesskey file "$HOME/.less"
713 */
714	public void
715add_hometable(envname, def_filename, sysvar)
716	char *envname;
717	char *def_filename;
718	int sysvar;
719{
720	char *filename;
721	PARG parg;
722
723	if (envname != NULL && (filename = lgetenv(envname)) != NULL)
724		filename = save(filename);
725	else if (sysvar)
726		filename = save(def_filename);
727	else
728		filename = homefile(def_filename);
729	if (filename == NULL)
730		return;
731	if (lesskey(filename, sysvar) < 0)
732	{
733		parg.p_string = filename;
734		error("Cannot use lesskey file \"%s\"", &parg);
735	}
736	free(filename);
737}
738#endif
739
740/*
741 * See if a char is a special line-editing command.
742 */
743	public int
744editchar(c, flags)
745	int c;
746	int flags;
747{
748	int action;
749	int nch;
750	char *s;
751	char usercmd[MAX_CMDLEN+1];
752
753	/*
754	 * An editing character could actually be a sequence of characters;
755	 * for example, an escape sequence sent by pressing the uparrow key.
756	 * To match the editing string, we use the command decoder
757	 * but give it the edit-commands command table
758	 * This table is constructed to match the user's keyboard.
759	 */
760	if (c == erase_char || c == erase2_char)
761		return (EC_BACKSPACE);
762	if (c == kill_char)
763		return (EC_LINEKILL);
764
765	/*
766	 * Collect characters in a buffer.
767	 * Start with the one we have, and get more if we need them.
768	 */
769	nch = 0;
770	do {
771	  	if (nch > 0)
772			c = getcc();
773		usercmd[nch] = c;
774		usercmd[nch+1] = '\0';
775		nch++;
776		action = ecmd_decode(usercmd, &s);
777	} while (action == A_PREFIX);
778
779	if (flags & EC_NORIGHTLEFT)
780	{
781		switch (action)
782		{
783		case EC_RIGHT:
784		case EC_LEFT:
785			action = A_INVALID;
786			break;
787		}
788	}
789#if CMD_HISTORY
790	if (flags & EC_NOHISTORY)
791	{
792		/*
793		 * The caller says there is no history list.
794		 * Reject any history-manipulation action.
795		 */
796		switch (action)
797		{
798		case EC_UP:
799		case EC_DOWN:
800			action = A_INVALID;
801			break;
802		}
803	}
804#endif
805#if TAB_COMPLETE_FILENAME
806	if (flags & EC_NOCOMPLETE)
807	{
808		/*
809		 * The caller says we don't want any filename completion cmds.
810		 * Reject them.
811		 */
812		switch (action)
813		{
814		case EC_F_COMPLETE:
815		case EC_B_COMPLETE:
816		case EC_EXPAND:
817			action = A_INVALID;
818			break;
819		}
820	}
821#endif
822	if ((flags & EC_PEEK) || action == A_INVALID)
823	{
824		/*
825		 * We're just peeking, or we didn't understand the command.
826		 * Unget all the characters we read in the loop above.
827		 * This does NOT include the original character that was
828		 * passed in as a parameter.
829		 */
830		while (nch > 1)
831		{
832			ungetcc(usercmd[--nch]);
833		}
834	} else
835	{
836		if (s != NULL)
837			ungetsc(s);
838	}
839	return action;
840}
841
842