home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 036 / less232.zip / DECODE.C < prev    next >
C/C++ Source or Header  |  1994-09-24  |  11KB  |  465 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994  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.  
  52. extern int erase_char, kill_char;
  53.  
  54. /*
  55.  * Command table is ordered roughly according to expected
  56.  * frequency of use, so the common commands are near the beginning.
  57.  */
  58. static char cmdtable[] =
  59. {
  60.     '\r',0,                A_F_LINE,
  61.     '\n',0,                A_F_LINE,
  62.     'e',0,                A_F_LINE,
  63.     'j',0,                A_F_LINE,
  64.     CONTROL('E'),0,            A_F_LINE,
  65.     CONTROL('N'),0,            A_F_LINE,
  66.     'k',0,                A_B_LINE,
  67.     'y',0,                A_B_LINE,
  68.     CONTROL('Y'),0,            A_B_LINE,
  69.     CONTROL('K'),0,            A_B_LINE,
  70.     CONTROL('P'),0,            A_B_LINE,
  71.     'J',0,                A_FF_LINE,
  72.     'K',0,                A_BF_LINE,
  73.     'Y',0,                A_BF_LINE,
  74.     'd',0,                A_F_SCROLL,
  75.     CONTROL('D'),0,            A_F_SCROLL,
  76.     'u',0,                A_B_SCROLL,
  77.     CONTROL('U'),0,            A_B_SCROLL,
  78.     ' ',0,                A_F_SCREEN,
  79.     'f',0,                A_F_SCREEN,
  80.     CONTROL('F'),0,            A_F_SCREEN,
  81.     CONTROL('V'),0,            A_F_SCREEN,
  82.     'b',0,                A_B_SCREEN,
  83.     CONTROL('B'),0,            A_B_SCREEN,
  84.     ESC,'v',0,            A_B_SCREEN,
  85.     'z',0,                A_F_WINDOW,
  86.     'w',0,                A_B_WINDOW,
  87.     'F',0,                A_F_FOREVER,
  88.     'R',0,                A_FREPAINT,
  89.     'r',0,                A_REPAINT,
  90.     CONTROL('R'),0,            A_REPAINT,
  91.     CONTROL('L'),0,            A_REPAINT,
  92.     ESC,'u',0,            A_UNDO_SEARCH,
  93.     'g',0,                A_GOLINE,
  94.     '<',0,                A_GOLINE,
  95.     ESC,'<',0,            A_GOLINE,
  96.     'p',0,                A_PERCENT,
  97.     '%',0,                A_PERCENT,
  98.     '{',0,                A_F_BRACKET|A_EXTRA,    '{','}',0,
  99.     '}',0,                A_B_BRACKET|A_EXTRA,    '{','}',0,
  100.     '(',0,                A_F_BRACKET|A_EXTRA,    '(',')',0,
  101.     ')',0,                A_B_BRACKET|A_EXTRA,    '(',')',0,
  102.     '[',0,                A_F_BRACKET|A_EXTRA,    '[',']',0,
  103.     ']',0,                A_B_BRACKET|A_EXTRA,    '[',']',0,
  104.     ESC,CONTROL('F'),0,        A_F_BRACKET,
  105.     ESC,CONTROL('B'),0,        A_B_BRACKET,
  106.     'G',0,                A_GOEND,
  107.     ESC,'>',0,            A_GOEND,
  108.     '>',0,                A_GOEND,
  109.     'P',0,                A_GOPOS,
  110.  
  111.     '0',0,                A_DIGIT,
  112.     '1',0,                A_DIGIT,
  113.     '2',0,                A_DIGIT,
  114.     '3',0,                A_DIGIT,
  115.     '4',0,                A_DIGIT,
  116.     '5',0,                A_DIGIT,
  117.     '6',0,                A_DIGIT,
  118.     '7',0,                A_DIGIT,
  119.     '8',0,                A_DIGIT,
  120.     '9',0,                A_DIGIT,
  121.  
  122.     '=',0,                A_STAT,
  123.     CONTROL('G'),0,            A_STAT,
  124.     ':','f',0,            A_STAT,
  125.     '/',0,                A_F_SEARCH,
  126.     '?',0,                A_B_SEARCH,
  127.     ESC,'/',0,            A_F_SEARCH|A_EXTRA,    '*',0,
  128.     ESC,'?',0,            A_B_SEARCH|A_EXTRA,    '*',0,
  129.     'n',0,                A_AGAIN_SEARCH,
  130.     ESC,'n',0,            A_T_AGAIN_SEARCH,
  131.     'N',0,                A_REVERSE_SEARCH,
  132.     ESC,'N',0,            A_T_REVERSE_SEARCH,
  133.     'm',0,                A_SETMARK,
  134.     '\'',0,                A_GOMARK,
  135.     CONTROL('X'),CONTROL('X'),0,    A_GOMARK,
  136.     'E',0,                A_EXAMINE,
  137.     ':','e',0,            A_EXAMINE,
  138.     CONTROL('X'),CONTROL('V'),0,    A_EXAMINE,
  139.     ':','n',0,            A_NEXT_FILE,
  140.     ':','p',0,            A_PREV_FILE,
  141.     ':','x',0,            A_INDEX_FILE,
  142.     '-',0,                A_OPT_TOGGLE,
  143.     ':','t',0,            A_OPT_TOGGLE|A_EXTRA,    't',0,
  144.     's',0,                A_OPT_TOGGLE|A_EXTRA,    'o',0,
  145.     '_',0,                A_DISP_OPTION,
  146.     '|',0,                A_PIPE,
  147.     'v',0,                A_VISUAL,
  148.     '!',0,                A_SHELL,
  149.     '+',0,                A_FIRSTCMD,
  150.  
  151.     'H',0,                A_HELP,
  152.     'h',0,                A_HELP,
  153.     'V',0,                A_VERSION,
  154.     'q',0,                A_QUIT,
  155.     ':','q',0,            A_QUIT,
  156.     ':','Q',0,            A_QUIT,
  157.     'Z','Z',0,            A_QUIT
  158. };
  159.  
  160. /*
  161.  * Structure to support a list of command tables.
  162.  */
  163. struct tablelist
  164. {
  165.     struct tablelist *t_next;
  166.     char *t_start;
  167.     char *t_end;
  168. };
  169.  
  170. /*
  171.  * Structure for the default command table.
  172.  */
  173. static struct tablelist deftable = 
  174.     { NULL, cmdtable, cmdtable+sizeof(cmdtable) };
  175.  
  176. /*
  177.  * List of tables; initially contains only the default table.
  178.  */
  179. static struct tablelist *tables = &deftable;
  180.  
  181.  
  182. /*
  183.  * 
  184.  */
  185.     public void
  186. cmd_init()
  187. {
  188.     struct tablelist *t;
  189.     static struct tablelist klist;
  190.     extern char kcmdtable[];
  191.     extern int sz_kcmdtable;
  192.  
  193.     klist.t_start = kcmdtable;
  194.     klist.t_end = kcmdtable + sz_kcmdtable;
  195.     /*
  196.      * Put the editkey command table at end of list of tables.
  197.      */
  198.     klist.t_next = NULL;
  199.     for (t = tables;  t->t_next != NULL;  t = t->t_next)
  200.         continue;
  201.     t->t_next = &klist;
  202. }
  203.  
  204. /*
  205.  * Decode a command character and return the associated action.
  206.  * The "extra" string, if any, is returned in sp.
  207.  */
  208.     public int
  209. cmd_decode(cmd, sp)
  210.     char *cmd;
  211.     char **sp;
  212. {
  213.     register struct tablelist *t;
  214.     register int action = A_INVALID;
  215.  
  216.     /*
  217.      * Search thru all the command tables.
  218.      * Stop when we find an action which is not A_INVALID.
  219.      */
  220.     for (t = tables;  t != NULL;  t = t->t_next)
  221.     {
  222.         action = cmd_search(cmd, t->t_start, t->t_end, sp);
  223.         if (action != A_INVALID)
  224.             break;
  225.     }
  226.     return (action);
  227. }
  228.  
  229. /*
  230.  * Search a command table for the current command string (in cmd).
  231.  */
  232.     public int
  233. cmd_search(cmd, table, endtable, sp)
  234.     char *cmd;
  235.     char *table;
  236.     char *endtable;
  237.     char **sp;
  238. {
  239.     register char *p;
  240.     register char *q;
  241.     register int a;
  242.  
  243.     for (p = table, q = cmd;  p < endtable;  p++, q++)
  244.     {
  245.         if (*p == *q)
  246.         {
  247.             /*
  248.              * Current characters match.
  249.              * If we're at the end of the string, we've found it.
  250.              * Return the action code, which is the character
  251.              * after the null at the end of the string
  252.              * in the command table.
  253.              */
  254.             if (*p == '\0')
  255.             {
  256.                 a = *++p & 0377;
  257.                 /*
  258.                  * Check for an "extra" string.
  259.                  */
  260.                 if (a & A_EXTRA)
  261.                 {
  262.                     *sp = ++p;
  263.                     a &= ~A_EXTRA;
  264.                 } else
  265.                     *sp = NULL;
  266.                 return (a);
  267.             }
  268.         } else if (*q == '\0')
  269.         {
  270.             /*
  271.              * Hit the end of the user's command,
  272.              * but not the end of the string in the command table.
  273.              * The user's command is incomplete.
  274.              */
  275.             return (A_PREFIX);
  276.         } else
  277.         {
  278.             /*
  279.              * Not a match.
  280.              * Skip ahead to the next command in the
  281.              * command table, and reset the pointer
  282.              * to the beginning of the user's command.
  283.              */
  284.             while (*p++ != '\0') ;
  285.             if (*p & A_EXTRA)
  286.                 while (*++p != '\0') ;
  287.             q = cmd-1;
  288.         }
  289.     }
  290.     /*
  291.      * No match found in the entire command table.
  292.      */
  293.     return (A_INVALID);
  294. }
  295.  
  296. #if USERFILE
  297. /*
  298.  * Set up a user command table, based on a "lesskey" file.
  299.  */
  300.     public int
  301. add_cmdtable(filename)
  302.     char *filename;
  303. {
  304.     register struct tablelist *t;
  305.     register POSITION len;
  306.     register long n;
  307.     register int f;
  308.  
  309.     /*
  310.      * Try to open the lesskey file.
  311.      * If we can't, return an error.
  312.      */
  313.         f = open(filename, O_RDONLY | O_BINARY);
  314.     if (f < 0)
  315.         return (-1);
  316.  
  317.     /*
  318.      * Read the file into the user table.
  319.      * We first figure out the size of the file and allocate space for it.
  320.      * {{ Minimal error checking is done here.
  321.      *    A garbage .less file will produce strange results.
  322.      *    To avoid a large amount of error checking code here, we
  323.      *    rely on the lesskey program to generate a good .less file. }}
  324.      */
  325.     len = filesize(f);
  326.     if (len == NULL_POSITION || len < 3)
  327.     {
  328.         /*
  329.          * Bad file (valid file must have at least 3 chars).
  330.          */
  331.         close(f);
  332.         return (-1);
  333.     }
  334.     if ((t = (struct tablelist *) 
  335.             calloc(1, sizeof(struct tablelist))) == NULL)
  336.     {
  337.         close(f);
  338.         return (-1);
  339.     }
  340.     if ((t->t_start = (char *) calloc((int)len, sizeof(char))) == NULL)
  341.     {
  342.         free((char *)t);
  343.         close(f);
  344.         return (-1);
  345.     }
  346.     if (lseek(f, (off_t)0, 0) == BAD_LSEEK)
  347.     {
  348.         free(t->t_start);
  349.         free((char *)t);
  350.         close(f);
  351.         return (-1);
  352.     }
  353.     n = read(f, t->t_start, (unsigned int) len);
  354.     close(f);
  355.  
  356.     /*
  357.      * In a valid lesskey file, the last byte or 
  358.      * the second to the last byte must be zero.
  359.      */
  360.     if (n != len || (t->t_start[n-1] != '\0' && t->t_start[n-2] != '\0'))
  361.     {
  362.         free(t->t_start);
  363.         free((char *)t);
  364.         return (-1);
  365.     }
  366.     t->t_end = t->t_start + n;
  367.  
  368.     /*
  369.      * Link it into the list of tables.
  370.      */
  371.     t->t_next = tables;
  372.     tables = t;
  373.     return (0);
  374. }
  375.  
  376. /*
  377.  * Try to add the lesskey file "$HOME/.less"
  378.  */
  379.     public void
  380. add_hometable()
  381. {
  382.     char *filename;
  383.  
  384.     filename = homefile(LESSKEYFILE);
  385.     if (filename == NULL)
  386.         return;
  387.     /*
  388.      * Ignore errors.
  389.      */
  390.     (void) add_cmdtable(filename);
  391.     free(filename);
  392. }
  393. #endif
  394.  
  395. /*
  396.  * See if a char is a special line-editing command.
  397.  */
  398.     public int
  399. editchar(c, flags)
  400.     int c;
  401.     int flags;
  402. {
  403.     int action;
  404.     int nch;
  405.     char *s;
  406.     char usercmd[30];
  407.     
  408.     /*
  409.      * An editing character could actually be a sequence of characters;
  410.      * for example, an escape sequence sent by pressing the uparrow key.
  411.      * To match the editing string, we use the command decoder, cmd_search, 
  412.      * but give it a special command table supplied by screen.c.
  413.      * This table is constructed to match the user's keyboard.
  414.      */
  415.     extern char edittable[];
  416.     extern int sz_edittable;
  417.     
  418.     if (c == erase_char)
  419.         return (EC_BACKSPACE);
  420.     if (c == kill_char)
  421.         return (EC_LINEKILL);
  422.         
  423.     /*
  424.      * Collect characters in a buffer.
  425.      * Start with the one we have, and get more if we need them.
  426.      */
  427.     nch = 0;
  428.     do {
  429.           if (nch > 0)
  430.             c = getcc();
  431.         usercmd[nch] = c;
  432.         usercmd[nch+1] = '\0';
  433.         nch++;
  434.         action = cmd_search(usercmd, edittable, edittable + sz_edittable, &s);
  435.     } while (action == A_PREFIX);
  436.     
  437.     if (flags & EC_NOHISTORY) 
  438.     {
  439.         /*
  440.          * The caller says there is no history list.
  441.          * Reject any history-manipulation action.
  442.          */
  443.         switch (action)
  444.         {
  445.         case EC_UP:
  446.         case EC_DOWN:
  447.             action = A_INVALID;
  448.             break;
  449.         }
  450.     }
  451.     if ((flags & EC_PEEK) || action == A_INVALID)
  452.     {
  453.         /*
  454.          * Unget all the characters we read in the loop above.
  455.          * This does NOT include the original character that was 
  456.          * passed in as a parameter.
  457.          */
  458.         while (nch > 1) {
  459.             ungetcc(usercmd[--nch]);
  460.         }
  461.     }
  462.     return action;
  463. }
  464.  
  465.