Home | History | Annotate | Line # | Download | only in more
decode.c revision 1.1
      1  1.1  cjs /*
      2  1.1  cjs  * Copyright (c) 1988 Mark Nudleman
      3  1.1  cjs  * Copyright (c) 1988, 1993
      4  1.1  cjs  *	The Regents of the University of California.  All rights reserved.
      5  1.1  cjs  *
      6  1.1  cjs  * Redistribution and use in source and binary forms, with or without
      7  1.1  cjs  * modification, are permitted provided that the following conditions
      8  1.1  cjs  * are met:
      9  1.1  cjs  * 1. Redistributions of source code must retain the above copyright
     10  1.1  cjs  *    notice, this list of conditions and the following disclaimer.
     11  1.1  cjs  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  cjs  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  cjs  *    documentation and/or other materials provided with the distribution.
     14  1.1  cjs  * 3. All advertising materials mentioning features or use of this software
     15  1.1  cjs  *    must display the following acknowledgement:
     16  1.1  cjs  *	This product includes software developed by the University of
     17  1.1  cjs  *	California, Berkeley and its contributors.
     18  1.1  cjs  * 4. Neither the name of the University nor the names of its contributors
     19  1.1  cjs  *    may be used to endorse or promote products derived from this software
     20  1.1  cjs  *    without specific prior written permission.
     21  1.1  cjs  *
     22  1.1  cjs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  cjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  cjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  cjs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1  cjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  cjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  cjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  cjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  cjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  cjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  cjs  * SUCH DAMAGE.
     33  1.1  cjs  */
     34  1.1  cjs 
     35  1.1  cjs #ifndef lint
     36  1.1  cjs static char sccsid[] = "@(#)decode.c	8.1 (Berkeley) 6/6/93";
     37  1.1  cjs #endif /* not lint */
     38  1.1  cjs 
     39  1.1  cjs /*
     40  1.1  cjs  * Routines to decode user commands.
     41  1.1  cjs  *
     42  1.1  cjs  * This is all table driven.
     43  1.1  cjs  * A command table is a sequence of command descriptors.
     44  1.1  cjs  * Each command descriptor is a sequence of bytes with the following format:
     45  1.1  cjs  *	<c1><c2>...<cN><0><action>
     46  1.1  cjs  * The characters c1,c2,...,cN are the command string; that is,
     47  1.1  cjs  * the characters which the user must type.
     48  1.1  cjs  * It is terminated by a null <0> byte.
     49  1.1  cjs  * The byte after the null byte is the action code associated
     50  1.1  cjs  * with the command string.
     51  1.1  cjs  *
     52  1.1  cjs  * The default commands are described by cmdtable.
     53  1.1  cjs  */
     54  1.1  cjs 
     55  1.1  cjs #include <sys/param.h>
     56  1.1  cjs #include <sys/file.h>
     57  1.1  cjs #include <stdio.h>
     58  1.1  cjs #include <less.h>
     59  1.1  cjs 
     60  1.1  cjs /*
     61  1.1  cjs  * Command table is ordered roughly according to expected
     62  1.1  cjs  * frequency of use, so the common commands are near the beginning.
     63  1.1  cjs  */
     64  1.1  cjs #define	CONTROL(c)		((c)&037)
     65  1.1  cjs 
     66  1.1  cjs static char cmdtable[] = {
     67  1.1  cjs 	'\r',0,				A_F_LINE,
     68  1.1  cjs 	'\n',0,				A_F_LINE,
     69  1.1  cjs 	'j',0,				A_F_LINE,
     70  1.1  cjs 	'k',0,				A_B_LINE,
     71  1.1  cjs 	'd',0,				A_F_SCROLL,
     72  1.1  cjs 	CONTROL('D'),0,			A_F_SCROLL,
     73  1.1  cjs 	'u',0,				A_B_SCROLL,
     74  1.1  cjs 	CONTROL('U'),0,			A_B_SCROLL,
     75  1.1  cjs 	' ',0,				A_F_SCREEN,
     76  1.1  cjs 	'f',0,				A_F_SCREEN,
     77  1.1  cjs 	CONTROL('F'),0,			A_F_SCREEN,
     78  1.1  cjs 	'b',0,				A_B_SCREEN,
     79  1.1  cjs 	CONTROL('B'),0,			A_B_SCREEN,
     80  1.1  cjs 	'R',0,				A_FREPAINT,
     81  1.1  cjs 	'r',0,				A_REPAINT,
     82  1.1  cjs 	CONTROL('L'),0,			A_REPAINT,
     83  1.1  cjs 	'g',0,				A_GOLINE,
     84  1.1  cjs 	'p',0,				A_PERCENT,
     85  1.1  cjs 	'%',0,				A_PERCENT,
     86  1.1  cjs 	'G',0,				A_GOEND,
     87  1.1  cjs 	'0',0,				A_DIGIT,
     88  1.1  cjs 	'1',0,				A_DIGIT,
     89  1.1  cjs 	'2',0,				A_DIGIT,
     90  1.1  cjs 	'3',0,				A_DIGIT,
     91  1.1  cjs 	'4',0,				A_DIGIT,
     92  1.1  cjs 	'5',0,				A_DIGIT,
     93  1.1  cjs 	'6',0,				A_DIGIT,
     94  1.1  cjs 	'7',0,				A_DIGIT,
     95  1.1  cjs 	'8',0,				A_DIGIT,
     96  1.1  cjs 	'9',0,				A_DIGIT,
     97  1.1  cjs 
     98  1.1  cjs 	'=',0,				A_STAT,
     99  1.1  cjs 	CONTROL('G'),0,			A_STAT,
    100  1.1  cjs 	'/',0,				A_F_SEARCH,
    101  1.1  cjs 	'?',0,				A_B_SEARCH,
    102  1.1  cjs 	'n',0,				A_AGAIN_SEARCH,
    103  1.1  cjs 	'm',0,				A_SETMARK,
    104  1.1  cjs 	'\'',0,				A_GOMARK,
    105  1.1  cjs 	'E',0,				A_EXAMINE,
    106  1.1  cjs 	'N',0,				A_NEXT_FILE,
    107  1.1  cjs 	':','n',0,			A_NEXT_FILE,
    108  1.1  cjs 	'P',0,				A_PREV_FILE,
    109  1.1  cjs 	':','p',0,			A_PREV_FILE,
    110  1.1  cjs 	'v',0,				A_VISUAL,
    111  1.1  cjs 
    112  1.1  cjs 	'h',0,				A_HELP,
    113  1.1  cjs 	'q',0,				A_QUIT,
    114  1.1  cjs 	':','q',0,			A_QUIT,
    115  1.1  cjs 	':','t',0,			A_TAGFILE,
    116  1.1  cjs 	':', 'a', 0,			A_FILE_LIST,
    117  1.1  cjs 	'Z','Z',0,			A_QUIT,
    118  1.1  cjs };
    119  1.1  cjs 
    120  1.1  cjs char *cmdendtable = cmdtable + sizeof(cmdtable);
    121  1.1  cjs 
    122  1.1  cjs #define	MAX_CMDLEN	16
    123  1.1  cjs 
    124  1.1  cjs static char kbuf[MAX_CMDLEN+1];
    125  1.1  cjs static char *kp = kbuf;
    126  1.1  cjs 
    127  1.1  cjs /*
    128  1.1  cjs  * Indicate that we're not in a prefix command
    129  1.1  cjs  * by resetting the command buffer pointer.
    130  1.1  cjs  */
    131  1.1  cjs noprefix()
    132  1.1  cjs {
    133  1.1  cjs 	kp = kbuf;
    134  1.1  cjs }
    135  1.1  cjs 
    136  1.1  cjs /*
    137  1.1  cjs  * Decode a command character and return the associated action.
    138  1.1  cjs  */
    139  1.1  cjs cmd_decode(c)
    140  1.1  cjs 	int c;
    141  1.1  cjs {
    142  1.1  cjs 	register int action = A_INVALID;
    143  1.1  cjs 
    144  1.1  cjs 	/*
    145  1.1  cjs 	 * Append the new command character to the command string in kbuf.
    146  1.1  cjs 	 */
    147  1.1  cjs 	*kp++ = c;
    148  1.1  cjs 	*kp = '\0';
    149  1.1  cjs 
    150  1.1  cjs 	action = cmd_search(cmdtable, cmdendtable);
    151  1.1  cjs 
    152  1.1  cjs 	/* This is not a prefix character. */
    153  1.1  cjs 	if (action != A_PREFIX)
    154  1.1  cjs 		noprefix();
    155  1.1  cjs 	return(action);
    156  1.1  cjs }
    157  1.1  cjs 
    158  1.1  cjs /*
    159  1.1  cjs  * Search a command table for the current command string (in kbuf).
    160  1.1  cjs  */
    161  1.1  cjs cmd_search(table, endtable)
    162  1.1  cjs 	char *table;
    163  1.1  cjs 	char *endtable;
    164  1.1  cjs {
    165  1.1  cjs 	register char *p, *q;
    166  1.1  cjs 
    167  1.1  cjs 	for (p = table, q = kbuf;  p < endtable;  p++, q++) {
    168  1.1  cjs 		if (*p == *q) {
    169  1.1  cjs 			/*
    170  1.1  cjs 			 * Current characters match.
    171  1.1  cjs 			 * If we're at the end of the string, we've found it.
    172  1.1  cjs 			 * Return the action code, which is the character
    173  1.1  cjs 			 * after the null at the end of the string
    174  1.1  cjs 			 * in the command table.
    175  1.1  cjs 			 */
    176  1.1  cjs 			if (*p == '\0')
    177  1.1  cjs 				return(p[1]);
    178  1.1  cjs 		}
    179  1.1  cjs 		else if (*q == '\0') {
    180  1.1  cjs 			/*
    181  1.1  cjs 			 * Hit the end of the user's command,
    182  1.1  cjs 			 * but not the end of the string in the command table.
    183  1.1  cjs 			 * The user's command is incomplete.
    184  1.1  cjs 			 */
    185  1.1  cjs 			return(A_PREFIX);
    186  1.1  cjs 		} else {
    187  1.1  cjs 			/*
    188  1.1  cjs 			 * Not a match.
    189  1.1  cjs 			 * Skip ahead to the next command in the
    190  1.1  cjs 			 * command table, and reset the pointer
    191  1.1  cjs 			 * to the user's command.
    192  1.1  cjs 			 */
    193  1.1  cjs 			while (*p++ != '\0');
    194  1.1  cjs 			q = kbuf-1;
    195  1.1  cjs 		}
    196  1.1  cjs 	}
    197  1.1  cjs 	/*
    198  1.1  cjs 	 * No match found in the entire command table.
    199  1.1  cjs 	 */
    200  1.1  cjs 	return(A_INVALID);
    201  1.1  cjs }
    202