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