home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / less2912.zip / decode.c < prev    next >
C/C++ Source or Header  |  1995-11-09  |  16KB  |  729 lines

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