xref: /freebsd/contrib/less/decode.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*
2  * Copyright (C) 1984-2015  Mark Nudelman
3  *
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.
6  *
7  * For more information, see the README file.
8  */
9 
10 
11 /*
12  * Routines to decode user commands.
13  *
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.
25  *
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.
30  */
31 
32 #include "less.h"
33 #include "cmd.h"
34 #include "lesskey.h"
35 
36 extern int erase_char, erase2_char, kill_char;
37 extern int secure;
38 
39 #define SK(k) \
40 	SK_SPECIAL_KEY, (k), 6, 1, 1, 1
41 /*
42  * Command table is ordered roughly according to expected
43  * frequency of use, so the common commands are near the beginning.
44  */
45 
46 static unsigned char cmdtable[] =
47 {
48 	'\r',0,				A_F_LINE,
49 	'\n',0,				A_F_LINE,
50 	'e',0,				A_F_LINE,
51 	'j',0,				A_F_LINE,
52 	SK(SK_DOWN_ARROW),0,		A_F_LINE,
53 	CONTROL('E'),0,			A_F_LINE,
54 	CONTROL('N'),0,			A_F_LINE,
55 	'k',0,				A_B_LINE,
56 	'y',0,				A_B_LINE,
57 	CONTROL('Y'),0,			A_B_LINE,
58 	SK(SK_CONTROL_K),0,		A_B_LINE,
59 	CONTROL('P'),0,			A_B_LINE,
60 	SK(SK_UP_ARROW),0,		A_B_LINE,
61 	'J',0,				A_FF_LINE,
62 	'K',0,				A_BF_LINE,
63 	'Y',0,				A_BF_LINE,
64 	'd',0,				A_F_SCROLL,
65 	CONTROL('D'),0,			A_F_SCROLL,
66 	'u',0,				A_B_SCROLL,
67 	CONTROL('U'),0,			A_B_SCROLL,
68 	' ',0,				A_F_SCREEN,
69 	'f',0,				A_F_SCREEN,
70 	CONTROL('F'),0,			A_F_SCREEN,
71 	CONTROL('V'),0,			A_F_SCREEN,
72 	SK(SK_PAGE_DOWN),0,		A_F_SCREEN,
73 	'b',0,				A_B_SCREEN,
74 	CONTROL('B'),0,			A_B_SCREEN,
75 	ESC,'v',0,			A_B_SCREEN,
76 	SK(SK_PAGE_UP),0,		A_B_SCREEN,
77 	'z',0,				A_F_WINDOW,
78 	'w',0,				A_B_WINDOW,
79 	ESC,' ',0,			A_FF_SCREEN,
80 	'F',0,				A_F_FOREVER,
81 	ESC,'F',0,			A_F_UNTIL_HILITE,
82 	'R',0,				A_FREPAINT,
83 	'r',0,				A_REPAINT,
84 	CONTROL('R'),0,			A_REPAINT,
85 	CONTROL('L'),0,			A_REPAINT,
86 	ESC,'u',0,			A_UNDO_SEARCH,
87 	'g',0,				A_GOLINE,
88 	SK(SK_HOME),0,			A_GOLINE,
89 	'<',0,				A_GOLINE,
90 	ESC,'<',0,			A_GOLINE,
91 	'p',0,				A_PERCENT,
92 	'%',0,				A_PERCENT,
93 	ESC,'[',0,			A_LSHIFT,
94 	ESC,']',0,			A_RSHIFT,
95 	ESC,'(',0,			A_LSHIFT,
96 	ESC,')',0,			A_RSHIFT,
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,
107 	'G',0,				A_GOEND,
108 	ESC,'G',0,			A_GOEND_BUF,
109 	ESC,'>',0,			A_GOEND,
110 	'>',0,				A_GOEND,
111 	SK(SK_END),0,			A_GOEND,
112 	'P',0,				A_GOPOS,
113 
114 	'0',0,				A_DIGIT,
115 	'1',0,				A_DIGIT,
116 	'2',0,				A_DIGIT,
117 	'3',0,				A_DIGIT,
118 	'4',0,				A_DIGIT,
119 	'5',0,				A_DIGIT,
120 	'6',0,				A_DIGIT,
121 	'7',0,				A_DIGIT,
122 	'8',0,				A_DIGIT,
123 	'9',0,				A_DIGIT,
124 	'.',0,				A_DIGIT,
125 
126 	'=',0,				A_STAT,
127 	CONTROL('G'),0,			A_STAT,
128 	':','f',0,			A_STAT,
129 	'/',0,				A_F_SEARCH,
130 	'?',0,				A_B_SEARCH,
131 	ESC,'/',0,			A_F_SEARCH|A_EXTRA,	'*',0,
132 	ESC,'?',0,			A_B_SEARCH|A_EXTRA,	'*',0,
133 	'n',0,				A_AGAIN_SEARCH,
134 	ESC,'n',0,			A_T_AGAIN_SEARCH,
135 	'N',0,				A_REVERSE_SEARCH,
136 	ESC,'N',0,			A_T_REVERSE_SEARCH,
137 	'&',0,				A_FILTER,
138 	'm',0,				A_SETMARK,
139 	'\'',0,				A_GOMARK,
140 	CONTROL('X'),CONTROL('X'),0,	A_GOMARK,
141 	'E',0,				A_EXAMINE,
142 	':','e',0,			A_EXAMINE,
143 	CONTROL('X'),CONTROL('V'),0,	A_EXAMINE,
144 	':','n',0,			A_NEXT_FILE,
145 	':','p',0,			A_PREV_FILE,
146 	't',0,				A_NEXT_TAG,
147 	'T',0,				A_PREV_TAG,
148 	':','x',0,			A_INDEX_FILE,
149 	':','d',0,			A_REMOVE_FILE,
150 	'-',0,				A_OPT_TOGGLE,
151 	':','t',0,			A_OPT_TOGGLE|A_EXTRA,	't',0,
152 	's',0,				A_OPT_TOGGLE|A_EXTRA,	'o',0,
153 	'_',0,				A_DISP_OPTION,
154 	'|',0,				A_PIPE,
155 	'v',0,				A_VISUAL,
156 	'!',0,				A_SHELL,
157 	'+',0,				A_FIRSTCMD,
158 
159 	'H',0,				A_HELP,
160 	'h',0,				A_HELP,
161 	SK(SK_F1),0,			A_HELP,
162 	'V',0,				A_VERSION,
163 	'q',0,				A_QUIT,
164 	'Q',0,				A_QUIT,
165 	':','q',0,			A_QUIT,
166 	':','Q',0,			A_QUIT,
167 	'Z','Z',0,			A_QUIT
168 };
169 
170 static unsigned char edittable[] =
171 {
172 	'\t',0,	    			EC_F_COMPLETE,	/* TAB */
173 	'\17',0,			EC_B_COMPLETE,	/* BACKTAB */
174 	SK(SK_BACKTAB),0,		EC_B_COMPLETE,	/* BACKTAB */
175 	ESC,'\t',0,			EC_B_COMPLETE,	/* ESC TAB */
176 	CONTROL('L'),0,			EC_EXPAND,	/* CTRL-L */
177 	CONTROL('V'),0,			EC_LITERAL,	/* BACKSLASH */
178 	CONTROL('A'),0,			EC_LITERAL,	/* BACKSLASH */
179    	ESC,'l',0,			EC_RIGHT,	/* ESC l */
180 	SK(SK_RIGHT_ARROW),0,		EC_RIGHT,	/* RIGHTARROW */
181 	ESC,'h',0,			EC_LEFT,	/* ESC h */
182 	SK(SK_LEFT_ARROW),0,		EC_LEFT,	/* LEFTARROW */
183 	ESC,'b',0,			EC_W_LEFT,	/* ESC b */
184 	ESC,SK(SK_LEFT_ARROW),0,	EC_W_LEFT,	/* ESC LEFTARROW */
185 	SK(SK_CTL_LEFT_ARROW),0,	EC_W_LEFT,	/* CTRL-LEFTARROW */
186 	ESC,'w',0,			EC_W_RIGHT,	/* ESC w */
187 	ESC,SK(SK_RIGHT_ARROW),0,	EC_W_RIGHT,	/* ESC RIGHTARROW */
188 	SK(SK_CTL_RIGHT_ARROW),0,	EC_W_RIGHT,	/* CTRL-RIGHTARROW */
189 	ESC,'i',0,			EC_INSERT,	/* ESC i */
190 	SK(SK_INSERT),0,		EC_INSERT,	/* INSERT */
191 	ESC,'x',0,			EC_DELETE,	/* ESC x */
192 	SK(SK_DELETE),0,		EC_DELETE,	/* DELETE */
193 	ESC,'X',0,			EC_W_DELETE,	/* ESC X */
194 	ESC,SK(SK_DELETE),0,		EC_W_DELETE,	/* ESC DELETE */
195 	SK(SK_CTL_DELETE),0,		EC_W_DELETE,	/* CTRL-DELETE */
196 	SK(SK_CTL_BACKSPACE),0,		EC_W_BACKSPACE, /* CTRL-BACKSPACE */
197 	ESC,'\b',0,			EC_W_BACKSPACE,	/* ESC BACKSPACE */
198 	ESC,'0',0,			EC_HOME,	/* ESC 0 */
199 	SK(SK_HOME),0,			EC_HOME,	/* HOME */
200 	ESC,'$',0,			EC_END,		/* ESC $ */
201 	SK(SK_END),0,			EC_END,		/* END */
202 	ESC,'k',0,			EC_UP,		/* ESC k */
203 	SK(SK_UP_ARROW),0,		EC_UP,		/* UPARROW */
204 	ESC,'j',0,			EC_DOWN,	/* ESC j */
205 	SK(SK_DOWN_ARROW),0,		EC_DOWN,	/* DOWNARROW */
206 	CONTROL('G'),0,			EC_ABORT,	/* CTRL-G */
207 };
208 
209 /*
210  * Structure to support a list of command tables.
211  */
212 struct tablelist
213 {
214 	struct tablelist *t_next;
215 	char *t_start;
216 	char *t_end;
217 };
218 
219 /*
220  * List of command tables and list of line-edit tables.
221  */
222 static struct tablelist *list_fcmd_tables = NULL;
223 static struct tablelist *list_ecmd_tables = NULL;
224 static struct tablelist *list_var_tables = NULL;
225 static struct tablelist *list_sysvar_tables = NULL;
226 
227 
228 /*
229  * Expand special key abbreviations in a command table.
230  */
231 	static void
232 expand_special_keys(table, len)
233 	char *table;
234 	int len;
235 {
236 	register char *fm;
237 	register char *to;
238 	register int a;
239 	char *repl;
240 	int klen;
241 
242 	for (fm = table;  fm < table + len; )
243 	{
244 		/*
245 		 * Rewrite each command in the table with any
246 		 * special key abbreviations expanded.
247 		 */
248 		for (to = fm;  *fm != '\0'; )
249 		{
250 			if (*fm != SK_SPECIAL_KEY)
251 			{
252 				*to++ = *fm++;
253 				continue;
254 			}
255 			/*
256 			 * After SK_SPECIAL_KEY, next byte is the type
257 			 * of special key (one of the SK_* contants),
258 			 * and the byte after that is the number of bytes,
259 			 * N, reserved by the abbreviation (including the
260 			 * SK_SPECIAL_KEY and key type bytes).
261 			 * Replace all N bytes with the actual bytes
262 			 * output by the special key on this terminal.
263 			 */
264 			repl = special_key_str(fm[1]);
265 			klen = fm[2] & 0377;
266 			fm += klen;
267 			if (repl == NULL || (int) strlen(repl) > klen)
268 				repl = "\377";
269 			while (*repl != '\0')
270 				*to++ = *repl++;
271 		}
272 		*to++ = '\0';
273 		/*
274 		 * Fill any unused bytes between end of command and
275 		 * the action byte with A_SKIP.
276 		 */
277 		while (to <= fm)
278 			*to++ = A_SKIP;
279 		fm++;
280 		a = *fm++ & 0377;
281 		if (a & A_EXTRA)
282 		{
283 			while (*fm++ != '\0')
284 				continue;
285 		}
286 	}
287 }
288 
289 /*
290  * Initialize the command lists.
291  */
292 	public void
293 init_cmds()
294 {
295 	/*
296 	 * Add the default command tables.
297 	 */
298 	add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
299 	add_ecmd_table((char*)edittable, sizeof(edittable));
300 #if USERFILE
301 	/*
302 	 * For backwards compatibility,
303 	 * try to add tables in the OLD system lesskey file.
304 	 */
305 #ifdef BINDIR
306 	add_hometable(NULL, BINDIR "/.sysless", 1);
307 #endif
308 	/*
309 	 * Try to add the tables in the system lesskey file.
310 	 */
311 	add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
312 	/*
313 	 * Try to add the tables in the standard lesskey file "$HOME/.less".
314 	 */
315 	add_hometable("LESSKEY", LESSKEYFILE, 0);
316 #endif
317 }
318 
319 /*
320  * Add a command table.
321  */
322 	static int
323 add_cmd_table(tlist, buf, len)
324 	struct tablelist **tlist;
325 	char *buf;
326 	int len;
327 {
328 	register struct tablelist *t;
329 
330 	if (len == 0)
331 		return (0);
332 	/*
333 	 * Allocate a tablelist structure, initialize it,
334 	 * and link it into the list of tables.
335 	 */
336 	if ((t = (struct tablelist *)
337 			calloc(1, sizeof(struct tablelist))) == NULL)
338 	{
339 		return (-1);
340 	}
341 	expand_special_keys(buf, len);
342 	t->t_start = buf;
343 	t->t_end = buf + len;
344 	t->t_next = *tlist;
345 	*tlist = t;
346 	return (0);
347 }
348 
349 /*
350  * Add a command table.
351  */
352 	public void
353 add_fcmd_table(buf, len)
354 	char *buf;
355 	int len;
356 {
357 	if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
358 		error("Warning: some commands disabled", NULL_PARG);
359 }
360 
361 /*
362  * Add an editing command table.
363  */
364 	public void
365 add_ecmd_table(buf, len)
366 	char *buf;
367 	int len;
368 {
369 	if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
370 		error("Warning: some edit commands disabled", NULL_PARG);
371 }
372 
373 /*
374  * Add an environment variable table.
375  */
376 	static void
377 add_var_table(tlist, buf, len)
378 	struct tablelist **tlist;
379 	char *buf;
380 	int len;
381 {
382 	if (add_cmd_table(tlist, buf, len) < 0)
383 		error("Warning: environment variables from lesskey file unavailable", NULL_PARG);
384 }
385 
386 /*
387  * Search a single command table for the command string in cmd.
388  */
389 	static int
390 cmd_search(cmd, table, endtable, sp)
391 	char *cmd;
392 	char *table;
393 	char *endtable;
394 	char **sp;
395 {
396 	register char *p;
397 	register char *q;
398 	register int a;
399 
400 	*sp = NULL;
401 	for (p = table, q = cmd;  p < endtable;  p++, q++)
402 	{
403 		if (*p == *q)
404 		{
405 			/*
406 			 * Current characters match.
407 			 * If we're at the end of the string, we've found it.
408 			 * Return the action code, which is the character
409 			 * after the null at the end of the string
410 			 * in the command table.
411 			 */
412 			if (*p == '\0')
413 			{
414 				a = *++p & 0377;
415 				while (a == A_SKIP)
416 					a = *++p & 0377;
417 				if (a == A_END_LIST)
418 				{
419 					/*
420 					 * We get here only if the original
421 					 * cmd string passed in was empty ("").
422 					 * I don't think that can happen,
423 					 * but just in case ...
424 					 */
425 					return (A_UINVALID);
426 				}
427 				/*
428 				 * Check for an "extra" string.
429 				 */
430 				if (a & A_EXTRA)
431 				{
432 					*sp = ++p;
433 					a &= ~A_EXTRA;
434 				}
435 				return (a);
436 			}
437 		} else if (*q == '\0')
438 		{
439 			/*
440 			 * Hit the end of the user's command,
441 			 * but not the end of the string in the command table.
442 			 * The user's command is incomplete.
443 			 */
444 			return (A_PREFIX);
445 		} else
446 		{
447 			/*
448 			 * Not a match.
449 			 * Skip ahead to the next command in the
450 			 * command table, and reset the pointer
451 			 * to the beginning of the user's command.
452 			 */
453 			if (*p == '\0' && p[1] == A_END_LIST)
454 			{
455 				/*
456 				 * A_END_LIST is a special marker that tells
457 				 * us to abort the cmd search.
458 				 */
459 				return (A_UINVALID);
460 			}
461 			while (*p++ != '\0')
462 				continue;
463 			while (*p == A_SKIP)
464 				p++;
465 			if (*p & A_EXTRA)
466 				while (*++p != '\0')
467 					continue;
468 			q = cmd-1;
469 		}
470 	}
471 	/*
472 	 * No match found in the entire command table.
473 	 */
474 	return (A_INVALID);
475 }
476 
477 /*
478  * Decode a command character and return the associated action.
479  * The "extra" string, if any, is returned in sp.
480  */
481 	static int
482 cmd_decode(tlist, cmd, sp)
483 	struct tablelist *tlist;
484 	char *cmd;
485 	char **sp;
486 {
487 	register struct tablelist *t;
488 	register int action = A_INVALID;
489 
490 	/*
491 	 * Search thru all the command tables.
492 	 * Stop when we find an action which is not A_INVALID.
493 	 */
494 	for (t = tlist;  t != NULL;  t = t->t_next)
495 	{
496 		action = cmd_search(cmd, t->t_start, t->t_end, sp);
497 		if (action != A_INVALID)
498 			break;
499 	}
500 	if (action == A_UINVALID)
501 		action = A_INVALID;
502 	return (action);
503 }
504 
505 /*
506  * Decode a command from the cmdtables list.
507  */
508 	public int
509 fcmd_decode(cmd, sp)
510 	char *cmd;
511 	char **sp;
512 {
513 	return (cmd_decode(list_fcmd_tables, cmd, sp));
514 }
515 
516 /*
517  * Decode a command from the edittables list.
518  */
519 	public int
520 ecmd_decode(cmd, sp)
521 	char *cmd;
522 	char **sp;
523 {
524 	return (cmd_decode(list_ecmd_tables, cmd, sp));
525 }
526 
527 /*
528  * Get the value of an environment variable.
529  * Looks first in the lesskey file, then in the real environment.
530  */
531 	public char *
532 lgetenv(var)
533 	char *var;
534 {
535 	int a;
536 	char *s;
537 
538 	a = cmd_decode(list_var_tables, var, &s);
539 	if (a == EV_OK)
540 		return (s);
541 	s = getenv(var);
542 	if (s != NULL && *s != '\0')
543 		return (s);
544 	a = cmd_decode(list_sysvar_tables, var, &s);
545 	if (a == EV_OK)
546 		return (s);
547 	return (NULL);
548 }
549 
550 #if USERFILE
551 /*
552  * Get an "integer" from a lesskey file.
553  * Integers are stored in a funny format:
554  * two bytes, low order first, in radix KRADIX.
555  */
556 	static int
557 gint(sp)
558 	char **sp;
559 {
560 	int n;
561 
562 	n = *(*sp)++;
563 	n += *(*sp)++ * KRADIX;
564 	return (n);
565 }
566 
567 /*
568  * Process an old (pre-v241) lesskey file.
569  */
570 	static int
571 old_lesskey(buf, len)
572 	char *buf;
573 	int len;
574 {
575 	/*
576 	 * Old-style lesskey file.
577 	 * The file must end with either
578 	 *     ...,cmd,0,action
579 	 * or  ...,cmd,0,action|A_EXTRA,string,0
580 	 * So the last byte or the second to last byte must be zero.
581 	 */
582 	if (buf[len-1] != '\0' && buf[len-2] != '\0')
583 		return (-1);
584 	add_fcmd_table(buf, len);
585 	return (0);
586 }
587 
588 /*
589  * Process a new (post-v241) lesskey file.
590  */
591 	static int
592 new_lesskey(buf, len, sysvar)
593 	char *buf;
594 	int len;
595 	int sysvar;
596 {
597 	char *p;
598 	register int c;
599 	register int n;
600 
601 	/*
602 	 * New-style lesskey file.
603 	 * Extract the pieces.
604 	 */
605 	if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
606 	    buf[len-2] != C1_END_LESSKEY_MAGIC ||
607 	    buf[len-1] != C2_END_LESSKEY_MAGIC)
608 		return (-1);
609 	p = buf + 4;
610 	for (;;)
611 	{
612 		c = *p++;
613 		switch (c)
614 		{
615 		case CMD_SECTION:
616 			n = gint(&p);
617 			add_fcmd_table(p, n);
618 			p += n;
619 			break;
620 		case EDIT_SECTION:
621 			n = gint(&p);
622 			add_ecmd_table(p, n);
623 			p += n;
624 			break;
625 		case VAR_SECTION:
626 			n = gint(&p);
627 			add_var_table((sysvar) ?
628 				&list_sysvar_tables : &list_var_tables, p, n);
629 			p += n;
630 			break;
631 		case END_SECTION:
632 			return (0);
633 		default:
634 			/*
635 			 * Unrecognized section type.
636 			 */
637 			return (-1);
638 		}
639 	}
640 }
641 
642 /*
643  * Set up a user command table, based on a "lesskey" file.
644  */
645 	public int
646 lesskey(filename, sysvar)
647 	char *filename;
648 	int sysvar;
649 {
650 	register char *buf;
651 	register POSITION len;
652 	register long n;
653 	register int f;
654 
655 	if (secure)
656 		return (1);
657 	/*
658 	 * Try to open the lesskey file.
659 	 */
660 	filename = shell_unquote(filename);
661 	f = open(filename, OPEN_READ);
662 	free(filename);
663 	if (f < 0)
664 		return (1);
665 
666 	/*
667 	 * Read the file into a buffer.
668 	 * We first figure out the size of the file and allocate space for it.
669 	 * {{ Minimal error checking is done here.
670 	 *    A garbage .less file will produce strange results.
671 	 *    To avoid a large amount of error checking code here, we
672 	 *    rely on the lesskey program to generate a good .less file. }}
673 	 */
674 	len = filesize(f);
675 	if (len == NULL_POSITION || len < 3)
676 	{
677 		/*
678 		 * Bad file (valid file must have at least 3 chars).
679 		 */
680 		close(f);
681 		return (-1);
682 	}
683 	if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL)
684 	{
685 		close(f);
686 		return (-1);
687 	}
688 	if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
689 	{
690 		free(buf);
691 		close(f);
692 		return (-1);
693 	}
694 	n = read(f, buf, (unsigned int) len);
695 	close(f);
696 	if (n != len)
697 	{
698 		free(buf);
699 		return (-1);
700 	}
701 
702 	/*
703 	 * Figure out if this is an old-style (before version 241)
704 	 * or new-style lesskey file format.
705 	 */
706 	if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
707 	    buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
708 		return (old_lesskey(buf, (int)len));
709 	return (new_lesskey(buf, (int)len, sysvar));
710 }
711 
712 /*
713  * Add the standard lesskey file "$HOME/.less"
714  */
715 	public void
716 add_hometable(envname, def_filename, sysvar)
717 	char *envname;
718 	char *def_filename;
719 	int sysvar;
720 {
721 	char *filename;
722 	PARG parg;
723 
724 	if (envname != NULL && (filename = lgetenv(envname)) != NULL)
725 		filename = save(filename);
726 	else if (sysvar)
727 		filename = save(def_filename);
728 	else
729 		filename = homefile(def_filename);
730 	if (filename == NULL)
731 		return;
732 	if (lesskey(filename, sysvar) < 0)
733 	{
734 		parg.p_string = filename;
735 		error("Cannot use lesskey file \"%s\"", &parg);
736 	}
737 	free(filename);
738 }
739 #endif
740 
741 /*
742  * See if a char is a special line-editing command.
743  */
744 	public int
745 editchar(c, flags)
746 	int c;
747 	int flags;
748 {
749 	int action;
750 	int nch;
751 	char *s;
752 	char usercmd[MAX_CMDLEN+1];
753 
754 	/*
755 	 * An editing character could actually be a sequence of characters;
756 	 * for example, an escape sequence sent by pressing the uparrow key.
757 	 * To match the editing string, we use the command decoder
758 	 * but give it the edit-commands command table
759 	 * This table is constructed to match the user's keyboard.
760 	 */
761 	if (c == erase_char || c == erase2_char)
762 		return (EC_BACKSPACE);
763 	if (c == kill_char)
764 		return (EC_LINEKILL);
765 
766 	/*
767 	 * Collect characters in a buffer.
768 	 * Start with the one we have, and get more if we need them.
769 	 */
770 	nch = 0;
771 	do {
772 	  	if (nch > 0)
773 			c = getcc();
774 		usercmd[nch] = c;
775 		usercmd[nch+1] = '\0';
776 		nch++;
777 		action = ecmd_decode(usercmd, &s);
778 	} while (action == A_PREFIX);
779 
780 	if (flags & EC_NORIGHTLEFT)
781 	{
782 		switch (action)
783 		{
784 		case EC_RIGHT:
785 		case EC_LEFT:
786 			action = A_INVALID;
787 			break;
788 		}
789 	}
790 #if CMD_HISTORY
791 	if (flags & EC_NOHISTORY)
792 	{
793 		/*
794 		 * The caller says there is no history list.
795 		 * Reject any history-manipulation action.
796 		 */
797 		switch (action)
798 		{
799 		case EC_UP:
800 		case EC_DOWN:
801 			action = A_INVALID;
802 			break;
803 		}
804 	}
805 #endif
806 #if TAB_COMPLETE_FILENAME
807 	if (flags & EC_NOCOMPLETE)
808 	{
809 		/*
810 		 * The caller says we don't want any filename completion cmds.
811 		 * Reject them.
812 		 */
813 		switch (action)
814 		{
815 		case EC_F_COMPLETE:
816 		case EC_B_COMPLETE:
817 		case EC_EXPAND:
818 			action = A_INVALID;
819 			break;
820 		}
821 	}
822 #endif
823 	if ((flags & EC_PEEK) || action == A_INVALID)
824 	{
825 		/*
826 		 * We're just peeking, or we didn't understand the command.
827 		 * Unget all the characters we read in the loop above.
828 		 * This does NOT include the original character that was
829 		 * passed in as a parameter.
830 		 */
831 		while (nch > 1)
832 		{
833 			ungetcc(usercmd[--nch]);
834 		}
835 	} else
836 	{
837 		if (s != NULL)
838 			ungetsc(s);
839 	}
840 	return action;
841 }
842 
843