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