]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/less/decode.c
MFV 316896
[FreeBSD/FreeBSD.git] / contrib / less / decode.c
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(char *table, int len)
233 {
234         char *fm;
235         char *to;
236         int a;
237         char *repl;
238         int klen;
239
240         for (fm = table;  fm < table + len; )
241         {
242                 /*
243                  * Rewrite each command in the table with any
244                  * special key abbreviations expanded.
245                  */
246                 for (to = fm;  *fm != '\0'; )
247                 {
248                         if (*fm != SK_SPECIAL_KEY)
249                         {
250                                 *to++ = *fm++;
251                                 continue;
252                         }
253                         /*
254                          * After SK_SPECIAL_KEY, next byte is the type
255                          * of special key (one of the SK_* contants),
256                          * and the byte after that is the number of bytes,
257                          * N, reserved by the abbreviation (including the
258                          * SK_SPECIAL_KEY and key type bytes).
259                          * Replace all N bytes with the actual bytes
260                          * output by the special key on this terminal.
261                          */
262                         repl = special_key_str(fm[1]);
263                         klen = fm[2] & 0377;
264                         fm += klen;
265                         if (repl == NULL || (int) strlen(repl) > klen)
266                                 repl = "\377";
267                         while (*repl != '\0')
268                                 *to++ = *repl++;
269                 }
270                 *to++ = '\0';
271                 /*
272                  * Fill any unused bytes between end of command and 
273                  * the action byte with A_SKIP.
274                  */
275                 while (to <= fm)
276                         *to++ = A_SKIP;
277                 fm++;
278                 a = *fm++ & 0377;
279                 if (a & A_EXTRA)
280                 {
281                         while (*fm++ != '\0')
282                                 continue;
283                 }
284         }
285 }
286
287 /*
288  * Initialize the command lists.
289  */
290         public void
291 init_cmds(void)
292 {
293         /*
294          * Add the default command tables.
295          */
296         add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
297         add_ecmd_table((char*)edittable, sizeof(edittable));
298 #if USERFILE
299         /*
300          * For backwards compatibility,
301          * try to add tables in the OLD system lesskey file.
302          */
303 #ifdef BINDIR
304         add_hometable(NULL, BINDIR "/.sysless", 1);
305 #endif
306         /*
307          * Try to add the tables in the system lesskey file.
308          */
309         add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
310         /*
311          * Try to add the tables in the standard lesskey file "$HOME/.less".
312          */
313         add_hometable("LESSKEY", LESSKEYFILE, 0);
314 #endif
315 }
316
317 /*
318  * Add a command table.
319  */
320         static int
321 add_cmd_table(struct tablelist **tlist, char *buf, int len)
322 {
323         struct tablelist *t;
324
325         if (len == 0)
326                 return (0);
327         /*
328          * Allocate a tablelist structure, initialize it, 
329          * and link it into the list of tables.
330          */
331         if ((t = (struct tablelist *) 
332                         calloc(1, sizeof(struct tablelist))) == NULL)
333         {
334                 return (-1);
335         }
336         expand_special_keys(buf, len);
337         t->t_start = buf;
338         t->t_end = buf + len;
339         t->t_next = *tlist;
340         *tlist = t;
341         return (0);
342 }
343
344 /*
345  * Add a command table.
346  */
347         public void
348 add_fcmd_table(char *buf, int len)
349 {
350         if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
351                 error("Warning: some commands disabled", NULL_PARG);
352 }
353
354 /*
355  * Add an editing command table.
356  */
357         public void
358 add_ecmd_table(char *buf, int len)
359 {
360         if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
361                 error("Warning: some edit commands disabled", NULL_PARG);
362 }
363
364 /*
365  * Add an environment variable table.
366  */
367         static void
368 add_var_table(struct tablelist **tlist, char *buf, int len)
369 {
370         if (add_cmd_table(tlist, buf, len) < 0)
371                 error("Warning: environment variables from lesskey file unavailable", NULL_PARG);
372 }
373
374 /*
375  * Search a single command table for the command string in cmd.
376  */
377         static int
378 cmd_search(char *cmd, char *table, char *endtable, char **sp)
379 {
380         char *p;
381         char *q;
382         int a;
383
384         *sp = NULL;
385         for (p = table, q = cmd;  p < endtable;  p++, q++)
386         {
387                 if (*p == *q)
388                 {
389                         /*
390                          * Current characters match.
391                          * If we're at the end of the string, we've found it.
392                          * Return the action code, which is the character
393                          * after the null at the end of the string
394                          * in the command table.
395                          */
396                         if (*p == '\0')
397                         {
398                                 a = *++p & 0377;
399                                 while (a == A_SKIP)
400                                         a = *++p & 0377;
401                                 if (a == A_END_LIST)
402                                 {
403                                         /*
404                                          * We get here only if the original
405                                          * cmd string passed in was empty ("").
406                                          * I don't think that can happen,
407                                          * but just in case ...
408                                          */
409                                         return (A_UINVALID);
410                                 }
411                                 /*
412                                  * Check for an "extra" string.
413                                  */
414                                 if (a & A_EXTRA)
415                                 {
416                                         *sp = ++p;
417                                         a &= ~A_EXTRA;
418                                 }
419                                 return (a);
420                         }
421                 } else if (*q == '\0')
422                 {
423                         /*
424                          * Hit the end of the user's command,
425                          * but not the end of the string in the command table.
426                          * The user's command is incomplete.
427                          */
428                         return (A_PREFIX);
429                 } else
430                 {
431                         /*
432                          * Not a match.
433                          * Skip ahead to the next command in the
434                          * command table, and reset the pointer
435                          * to the beginning of the user's command.
436                          */
437                         if (*p == '\0' && p[1] == A_END_LIST)
438                         {
439                                 /*
440                                  * A_END_LIST is a special marker that tells 
441                                  * us to abort the cmd search.
442                                  */
443                                 return (A_UINVALID);
444                         }
445                         while (*p++ != '\0')
446                                 continue;
447                         while (*p == A_SKIP)
448                                 p++;
449                         if (*p & A_EXTRA)
450                                 while (*++p != '\0')
451                                         continue;
452                         q = cmd-1;
453                 }
454         }
455         /*
456          * No match found in the entire command table.
457          */
458         return (A_INVALID);
459 }
460
461 /*
462  * Decode a command character and return the associated action.
463  * The "extra" string, if any, is returned in sp.
464  */
465         static int
466 cmd_decode(struct tablelist *tlist, char *cmd, char **sp)
467 {
468         struct tablelist *t;
469         int action = A_INVALID;
470
471         /*
472          * Search thru all the command tables.
473          * Stop when we find an action which is not A_INVALID.
474          */
475         for (t = tlist;  t != NULL;  t = t->t_next)
476         {
477                 action = cmd_search(cmd, t->t_start, t->t_end, sp);
478                 if (action != A_INVALID)
479                         break;
480         }
481         if (action == A_UINVALID)
482                 action = A_INVALID;
483         return (action);
484 }
485
486 /*
487  * Decode a command from the cmdtables list.
488  */
489         public int
490 fcmd_decode(char *cmd, char **sp)
491 {
492         return (cmd_decode(list_fcmd_tables, cmd, sp));
493 }
494
495 /*
496  * Decode a command from the edittables list.
497  */
498         public int
499 ecmd_decode(char *cmd, char **sp)
500 {
501         return (cmd_decode(list_ecmd_tables, cmd, sp));
502 }
503
504 /*
505  * Get the value of an environment variable.
506  * Looks first in the lesskey file, then in the real environment.
507  */
508         public char *
509 lgetenv(char *var)
510 {
511         int a;
512         char *s;
513
514         a = cmd_decode(list_var_tables, var, &s);
515         if (a == EV_OK)
516                 return (s);
517         s = getenv(var);
518         if (s != NULL && *s != '\0')
519                 return (s);
520         a = cmd_decode(list_sysvar_tables, var, &s);
521         if (a == EV_OK)
522                 return (s);
523         return (NULL);
524 }
525
526 #if USERFILE
527 /*
528  * Get an "integer" from a lesskey file.
529  * Integers are stored in a funny format: 
530  * two bytes, low order first, in radix KRADIX.
531  */
532         static int
533 gint(char **sp)
534 {
535         int n;
536
537         n = *(*sp)++;
538         n += *(*sp)++ * KRADIX;
539         return (n);
540 }
541
542 /*
543  * Process an old (pre-v241) lesskey file.
544  */
545         static int
546 old_lesskey(char *buf, int len)
547 {
548         /*
549          * Old-style lesskey file.
550          * The file must end with either 
551          *     ...,cmd,0,action
552          * or  ...,cmd,0,action|A_EXTRA,string,0
553          * So the last byte or the second to last byte must be zero.
554          */
555         if (buf[len-1] != '\0' && buf[len-2] != '\0')
556                 return (-1);
557         add_fcmd_table(buf, len);
558         return (0);
559 }
560
561 /* 
562  * Process a new (post-v241) lesskey file.
563  */
564         static int
565 new_lesskey(char *buf, int len, int sysvar)
566 {
567         char *p;
568         int c;
569         int n;
570
571         /*
572          * New-style lesskey file.
573          * Extract the pieces.
574          */
575         if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
576             buf[len-2] != C1_END_LESSKEY_MAGIC ||
577             buf[len-1] != C2_END_LESSKEY_MAGIC)
578                 return (-1);
579         p = buf + 4;
580         for (;;)
581         {
582                 c = *p++;
583                 switch (c)
584                 {
585                 case CMD_SECTION:
586                         n = gint(&p);
587                         add_fcmd_table(p, n);
588                         p += n;
589                         break;
590                 case EDIT_SECTION:
591                         n = gint(&p);
592                         add_ecmd_table(p, n);
593                         p += n;
594                         break;
595                 case VAR_SECTION:
596                         n = gint(&p);
597                         add_var_table((sysvar) ? 
598                                 &list_sysvar_tables : &list_var_tables, p, n);
599                         p += n;
600                         break;
601                 case END_SECTION:
602                         return (0);
603                 default:
604                         /*
605                          * Unrecognized section type.
606                          */
607                         return (-1);
608                 }
609         }
610 }
611
612 /*
613  * Set up a user command table, based on a "lesskey" file.
614  */
615         public int
616 lesskey(char *filename, int sysvar)
617 {
618         char *buf;
619         POSITION len;
620         long n;
621         int f;
622
623         if (secure)
624                 return (1);
625         /*
626          * Try to open the lesskey file.
627          */
628         filename = shell_unquote(filename);
629         f = open(filename, OPEN_READ);
630         free(filename);
631         if (f < 0)
632                 return (1);
633
634         /*
635          * Read the file into a buffer.
636          * We first figure out the size of the file and allocate space for it.
637          * {{ Minimal error checking is done here.
638          *    A garbage .less file will produce strange results.
639          *    To avoid a large amount of error checking code here, we
640          *    rely on the lesskey program to generate a good .less file. }}
641          */
642         len = filesize(f);
643         if (len == NULL_POSITION || len < 3)
644         {
645                 /*
646                  * Bad file (valid file must have at least 3 chars).
647                  */
648                 close(f);
649                 return (-1);
650         }
651         if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL)
652         {
653                 close(f);
654                 return (-1);
655         }
656         if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
657         {
658                 free(buf);
659                 close(f);
660                 return (-1);
661         }
662         n = read(f, buf, (unsigned int) len);
663         close(f);
664         if (n != len)
665         {
666                 free(buf);
667                 return (-1);
668         }
669
670         /*
671          * Figure out if this is an old-style (before version 241)
672          * or new-style lesskey file format.
673          */
674         if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
675             buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
676                 return (old_lesskey(buf, (int)len));
677         return (new_lesskey(buf, (int)len, sysvar));
678 }
679
680 /*
681  * Add the standard lesskey file "$HOME/.less"
682  */
683         public void
684 add_hometable(char *envname, char *def_filename, int sysvar)
685 {
686         char *filename;
687         PARG parg;
688
689         if (envname != NULL && (filename = lgetenv(envname)) != NULL)
690                 filename = save(filename);
691         else if (sysvar)
692                 filename = save(def_filename);
693         else
694                 filename = homefile(def_filename);
695         if (filename == NULL)
696                 return;
697         if (lesskey(filename, sysvar) < 0)
698         {
699                 parg.p_string = filename;
700                 error("Cannot use lesskey file \"%s\"", &parg);
701         }
702         free(filename);
703 }
704 #endif
705
706 /*
707  * See if a char is a special line-editing command.
708  */
709         public int
710 editchar(int c, int flags)
711 {
712         int action;
713         int nch;
714         char *s;
715         char usercmd[MAX_CMDLEN+1];
716         
717         /*
718          * An editing character could actually be a sequence of characters;
719          * for example, an escape sequence sent by pressing the uparrow key.
720          * To match the editing string, we use the command decoder
721          * but give it the edit-commands command table
722          * This table is constructed to match the user's keyboard.
723          */
724         if (c == erase_char || c == erase2_char)
725                 return (EC_BACKSPACE);
726         if (c == kill_char)
727                 return (EC_LINEKILL);
728                 
729         /*
730          * Collect characters in a buffer.
731          * Start with the one we have, and get more if we need them.
732          */
733         nch = 0;
734         do {
735                 if (nch > 0)
736                         c = getcc();
737                 usercmd[nch] = c;
738                 usercmd[nch+1] = '\0';
739                 nch++;
740                 action = ecmd_decode(usercmd, &s);
741         } while (action == A_PREFIX);
742         
743         if (flags & EC_NORIGHTLEFT)
744         {
745                 switch (action)
746                 {
747                 case EC_RIGHT:
748                 case EC_LEFT:
749                         action = A_INVALID;
750                         break;
751                 }
752         }
753 #if CMD_HISTORY
754         if (flags & EC_NOHISTORY) 
755         {
756                 /*
757                  * The caller says there is no history list.
758                  * Reject any history-manipulation action.
759                  */
760                 switch (action)
761                 {
762                 case EC_UP:
763                 case EC_DOWN:
764                         action = A_INVALID;
765                         break;
766                 }
767         }
768 #endif
769 #if TAB_COMPLETE_FILENAME
770         if (flags & EC_NOCOMPLETE) 
771         {
772                 /*
773                  * The caller says we don't want any filename completion cmds.
774                  * Reject them.
775                  */
776                 switch (action)
777                 {
778                 case EC_F_COMPLETE:
779                 case EC_B_COMPLETE:
780                 case EC_EXPAND:
781                         action = A_INVALID;
782                         break;
783                 }
784         }
785 #endif
786         if ((flags & EC_PEEK) || action == A_INVALID)
787         {
788                 /*
789                  * We're just peeking, or we didn't understand the command.
790                  * Unget all the characters we read in the loop above.
791                  * This does NOT include the original character that was 
792                  * passed in as a parameter.
793                  */
794                 while (nch > 1) 
795                 {
796                         ungetcc(usercmd[--nch]);
797                 }
798         } else
799         {
800                 if (s != NULL)
801                         ungetsc(s);
802         }
803         return action;
804 }
805