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