home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / text / less-278.lha / less-278 / src.lha / source / decode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-01  |  14.0 KB  |  654 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.  
  199.  
  200. /*
  201.  * Initialize the command lists.
  202.  */
  203.     public void
  204. init_cmds()
  205. {
  206.     /*
  207.      * Add the default command tables.
  208.      */
  209.     add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
  210.     add_ecmd_table((char*)edittable, sizeof(edittable));
  211. #if USERFILE
  212.     /*
  213.      * Try to add the tables in the standard lesskey file "$HOME/.less".
  214.      */
  215.     add_hometable();
  216. #endif
  217. }
  218.  
  219. /*
  220.  * 
  221.  */
  222.     static int
  223. add_cmd_table(tlist, buf, len)
  224.     struct tablelist **tlist;
  225.     char *buf;
  226.     int len;
  227. {
  228.     register struct tablelist *t;
  229.  
  230.     if (len == 0)
  231.         return (0);
  232.     /*
  233.      * Allocate a tablelist structure, initialize it, 
  234.      * and link it into the list of tables.
  235.      */
  236.     if ((t = (struct tablelist *) 
  237.             calloc(1, sizeof(struct tablelist))) == NULL)
  238.     {
  239.         return (-1);
  240.     }
  241.     t->t_start = buf;
  242.     t->t_end = buf + len;
  243.     t->t_next = *tlist;
  244.     *tlist = t;
  245.     return (0);
  246. }
  247.  
  248. /*
  249.  * Add a command table.
  250.  */
  251.     public void
  252. add_fcmd_table(buf, len)
  253.     char *buf;
  254.     int len;
  255. {
  256.     if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
  257.         error("Warning: some commands disabled", NULL_PARG);
  258. }
  259.  
  260. /*
  261.  * Add an editing command table.
  262.  */
  263.     public void
  264. add_ecmd_table(buf, len)
  265.     char *buf;
  266.     int len;
  267. {
  268.     if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
  269.         error("Warning: some edit commands disabled", NULL_PARG);
  270. }
  271.  
  272. /*
  273.  * Search a single command table for the command string in cmd.
  274.  */
  275.     public int
  276. cmd_search(cmd, table, endtable, sp)
  277.     char *cmd;
  278.     char *table;
  279.     char *endtable;
  280.     char **sp;
  281. {
  282.     register char *p;
  283.     register char *q;
  284.     register int a;
  285.  
  286.     for (p = table, q = cmd;  p < endtable;  p++, q++)
  287.     {
  288.         if (*p == *q)
  289.         {
  290.             /*
  291.              * Current characters match.
  292.              * If we're at the end of the string, we've found it.
  293.              * Return the action code, which is the character
  294.              * after the null at the end of the string
  295.              * in the command table.
  296.              */
  297.             if (*p == '\0')
  298.             {
  299.                 a = *++p & 0377;
  300.                 if (a == A_END_LIST)
  301.                 {
  302.                     /*
  303.                      * We get here only if the original
  304.                      * cmd string passed in was empty ("").
  305.                      * I don't think that can happen,
  306.                      * but just in case ...
  307.                      */
  308.                     return (A_UINVALID);
  309.                 }
  310.                 /*
  311.                  * Check for an "extra" string.
  312.                  */
  313.                 if (a & A_EXTRA)
  314.                 {
  315.                     *sp = ++p;
  316.                     a &= ~A_EXTRA;
  317.                 } else
  318.                     *sp = NULL;
  319.                 return (a);
  320.             }
  321.         } else if (*q == '\0')
  322.         {
  323.             /*
  324.              * Hit the end of the user's command,
  325.              * but not the end of the string in the command table.
  326.              * The user's command is incomplete.
  327.              */
  328.             return (A_PREFIX);
  329.         } else
  330.         {
  331.             /*
  332.              * Not a match.
  333.              * Skip ahead to the next command in the
  334.              * command table, and reset the pointer
  335.              * to the beginning of the user's command.
  336.              */
  337.             if (*p == '\0' && p[1] == A_END_LIST)
  338.             {
  339.                 /*
  340.                  * A_END_LIST is a special marker that tells 
  341.                  * us to abort the cmd search.
  342.                  */
  343.                 return (A_UINVALID);
  344.             }
  345.             while (*p++ != '\0') ;
  346.             if (*p & A_EXTRA)
  347.                 while (*++p != '\0') ;
  348.             q = cmd-1;
  349.         }
  350.     }
  351.     /*
  352.      * No match found in the entire command table.
  353.      */
  354.     return (A_INVALID);
  355. }
  356.  
  357. /*
  358.  * Decode a command character and return the associated action.
  359.  * The "extra" string, if any, is returned in sp.
  360.  */
  361.     static int
  362. cmd_decode(tlist, cmd, sp)
  363.     struct tablelist *tlist;
  364.     char *cmd;
  365.     char **sp;
  366. {
  367.     register struct tablelist *t;
  368.     register int action = A_INVALID;
  369.  
  370.     /*
  371.      * Search thru all the command tables.
  372.      * Stop when we find an action which is not A_INVALID.
  373.      */
  374.     for (t = tlist;  t != NULL;  t = t->t_next)
  375.     {
  376.         action = cmd_search(cmd, t->t_start, t->t_end, sp);
  377.         if (action != A_INVALID)
  378.             break;
  379.     }
  380.     return (action);
  381. }
  382.  
  383. /*
  384.  * Decode a command from the cmdtables list.
  385.  */
  386.     public int
  387. fcmd_decode(cmd, sp)
  388.     char *cmd;
  389.     char **sp;
  390. {
  391.     return (cmd_decode(list_fcmd_tables, cmd, sp));
  392. }
  393.  
  394. /*
  395.  * Decode a command from the edittables list.
  396.  */
  397.     public int
  398. ecmd_decode(cmd, sp)
  399.     char *cmd;
  400.     char **sp;
  401. {
  402.     return (cmd_decode(list_ecmd_tables, cmd, sp));
  403. }
  404.  
  405. #if USERFILE
  406.     static int
  407. gint(sp)
  408.     char **sp;
  409. {
  410.     int n;
  411.  
  412.     n = *(*sp)++;
  413.     n += *(*sp)++ * KRADIX;
  414.     return (n);
  415. }
  416.  
  417.     static int
  418. old_lesskey(buf, len)
  419.     char *buf;
  420.     int len;
  421. {
  422.     /*
  423.      * Old-style lesskey file.
  424.      * The file must end with either 
  425.      *     ...,cmd,0,action
  426.      * or  ...,cmd,0,action|A_EXTRA,string,0
  427.      * So the last byte or the second to last byte must be zero.
  428.      */
  429.     if (buf[len-1] != '\0' && buf[len-2] != '\0')
  430.         return (-1);
  431.     add_fcmd_table(buf, len);
  432.     return (0);
  433. }
  434.  
  435.     static int
  436. new_lesskey(buf, len)
  437.     char *buf;
  438.     int len;
  439. {
  440.     char *p;
  441.     register int c;
  442.     register int done;
  443.     register int n;
  444.  
  445.     /*
  446.      * New-style lesskey file.
  447.      * Extract the pieces.
  448.      */
  449.     if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
  450.         buf[len-2] != C1_END_LESSKEY_MAGIC ||
  451.         buf[len-1] != C2_END_LESSKEY_MAGIC)
  452.         return (-1);
  453.     p = buf + 4;
  454.     done = 0;
  455.     while (!done)
  456.     {
  457.         c = *p++;
  458.         switch (c)
  459.         {
  460.         case CMD_SECTION:
  461.             n = gint(&p);
  462.             add_fcmd_table(p, n);
  463.             p += n;
  464.             break;
  465.         case EDIT_SECTION:
  466.             n = gint(&p);
  467.             add_ecmd_table(p, n);
  468.             p += n;
  469.             break;
  470.         case END_SECTION:
  471.             done = 1;
  472.             break;
  473.         default:
  474.             free(buf);
  475.             return (-1);
  476.         }
  477.     }
  478.     return (0);
  479. }
  480.  
  481. /*
  482.  * Set up a user command table, based on a "lesskey" file.
  483.  */
  484.     public int
  485. lesskey(filename)
  486.     char *filename;
  487. {
  488.     register char *buf;
  489.     register POSITION len;
  490.     register long n;
  491.     register int f;
  492.  
  493.     /*
  494.      * Try to open the lesskey file.
  495.      */
  496.     f = open(filename, OPEN_READ);
  497.     if (f < 0)
  498.         return (1);
  499.  
  500.     /*
  501.      * Read the file into a buffer.
  502.      * We first figure out the size of the file and allocate space for it.
  503.      * {{ Minimal error checking is done here.
  504.      *    A garbage .less file will produce strange results.
  505.      *    To avoid a large amount of error checking code here, we
  506.      *    rely on the lesskey program to generate a good .less file. }}
  507.      */
  508.     len = filesize(f);
  509.     if (len == NULL_POSITION || len < 3)
  510.     {
  511.         /*
  512.          * Bad file (valid file must have at least 3 chars).
  513.          */
  514.         close(f);
  515.         return (-1);
  516.     }
  517.     if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL)
  518.     {
  519.         close(f);
  520.         return (-1);
  521.     }
  522.     if (lseek(f, (off_t)0, 0) == BAD_LSEEK)
  523.     {
  524.         free(buf);
  525.         close(f);
  526.         return (-1);
  527.     }
  528.     n = read(f, buf, (unsigned int) len);
  529.     close(f);
  530.     if (n != len)
  531.     {
  532.         free(buf);
  533.         return (-1);
  534.     }
  535.  
  536.     /*
  537.      * Figure out if this is an old-style (before version 241)
  538.      * or new-style lesskey file format.
  539.      */
  540.     if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
  541.         buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
  542.         return (old_lesskey(buf, (int)len));
  543.     return (new_lesskey(buf, (int)len));
  544. }
  545.  
  546. /*
  547.  * Add the standard lesskey file "$HOME/.less"
  548.  */
  549.     public void
  550. add_hometable()
  551. {
  552.     char *filename;
  553.     PARG parg;
  554.  
  555.     filename = homefile(LESSKEYFILE);
  556.     if (filename == NULL)
  557.         return;
  558.     if (lesskey(filename) < 0)
  559.     {
  560.         parg.p_string = filename;
  561.         error("Cannot use lesskey file \"%s\"", &parg);
  562.     }
  563.     free(filename);
  564. }
  565. #endif
  566.  
  567. /*
  568.  * See if a char is a special line-editing command.
  569.  */
  570.     public int
  571. editchar(c, flags)
  572.     int c;
  573.     int flags;
  574. {
  575.     int action;
  576.     int nch;
  577.     char *s;
  578.     char usercmd[MAX_CMDLEN+1];
  579.     
  580.     /*
  581.      * An editing character could actually be a sequence of characters;
  582.      * for example, an escape sequence sent by pressing the uparrow key.
  583.      * To match the editing string, we use the command decoder
  584.      * but give it the edit-commands command table
  585.      * This table is constructed to match the user's keyboard.
  586.      */
  587.     if (c == erase_char)
  588.         return (EC_BACKSPACE);
  589.     if (c == kill_char)
  590.         return (EC_LINEKILL);
  591.         
  592.     /*
  593.      * Collect characters in a buffer.
  594.      * Start with the one we have, and get more if we need them.
  595.      */
  596.     nch = 0;
  597.     do {
  598.           if (nch > 0)
  599.             c = getcc();
  600.         usercmd[nch] = c;
  601.         usercmd[nch+1] = '\0';
  602.         nch++;
  603.         action = ecmd_decode(usercmd, &s);
  604.     } while (action == A_PREFIX);
  605.     
  606.     if (flags & EC_NOHISTORY) 
  607.     {
  608.         /*
  609.          * The caller says there is no history list.
  610.          * Reject any history-manipulation action.
  611.          */
  612.         switch (action)
  613.         {
  614.         case EC_UP:
  615.         case EC_DOWN:
  616.             action = A_INVALID;
  617.             break;
  618.         }
  619.     }
  620.     if (flags & EC_NOCOMPLETE) 
  621.     {
  622.         /*
  623.          * The caller says we don't want any filename completion cmds.
  624.          * Reject them.
  625.          */
  626.         switch (action)
  627.         {
  628.         case EC_F_COMPLETE:
  629.         case EC_B_COMPLETE:
  630.         case EC_EXPAND:
  631.             action = A_INVALID;
  632.             break;
  633.         }
  634.     }
  635.     if ((flags & EC_PEEK) || action == A_INVALID)
  636.     {
  637.         /*
  638.          * We're just peeking, or we didn't understand the command.
  639.          * Unget all the characters we read in the loop above.
  640.          * This does NOT include the original character that was 
  641.          * passed in as a parameter.
  642.          */
  643.         while (nch > 1) {
  644.             ungetcc(usercmd[--nch]);
  645.         }
  646.     } else
  647.     {
  648.         if (s != NULL)
  649.             ungetsc(s);
  650.     }
  651.     return action;
  652. }
  653.  
  654.