135209Sbostic /*
235209Sbostic * Copyright (c) 1988 Mark Nudleman
3*62131Sbostic * Copyright (c) 1988, 1993
4*62131Sbostic * The Regents of the University of California. All rights reserved.
535209Sbostic *
642742Sbostic * %sccs.include.redist.c%
735209Sbostic */
835209Sbostic
935209Sbostic #ifndef lint
10*62131Sbostic static char sccsid[] = "@(#)decode.c 8.1 (Berkeley) 06/06/93";
1135209Sbostic #endif /* not lint */
1235209Sbostic
1335209Sbostic /*
1435209Sbostic * Routines to decode user commands.
1535209Sbostic *
1635209Sbostic * This is all table driven.
1735209Sbostic * A command table is a sequence of command descriptors.
1835209Sbostic * Each command descriptor is a sequence of bytes with the following format:
1935209Sbostic * <c1><c2>...<cN><0><action>
2035209Sbostic * The characters c1,c2,...,cN are the command string; that is,
2135209Sbostic * the characters which the user must type.
2235209Sbostic * It is terminated by a null <0> byte.
2335209Sbostic * The byte after the null byte is the action code associated
2435209Sbostic * with the command string.
2535209Sbostic *
2635209Sbostic * The default commands are described by cmdtable.
2735209Sbostic */
2835209Sbostic
2936251Sbostic #include <sys/param.h>
3036251Sbostic #include <sys/file.h>
3136251Sbostic #include <stdio.h>
3236251Sbostic #include <less.h>
3335209Sbostic
3435209Sbostic /*
3535209Sbostic * Command table is ordered roughly according to expected
3635209Sbostic * frequency of use, so the common commands are near the beginning.
3735209Sbostic */
3836251Sbostic #define CONTROL(c) ((c)&037)
3936251Sbostic
4036251Sbostic static char cmdtable[] = {
4135209Sbostic '\r',0, A_F_LINE,
4235209Sbostic '\n',0, A_F_LINE,
4335209Sbostic 'j',0, A_F_LINE,
4435209Sbostic 'k',0, A_B_LINE,
4535209Sbostic 'd',0, A_F_SCROLL,
4635209Sbostic CONTROL('D'),0, A_F_SCROLL,
4735209Sbostic 'u',0, A_B_SCROLL,
4835209Sbostic CONTROL('U'),0, A_B_SCROLL,
4935209Sbostic ' ',0, A_F_SCREEN,
5035209Sbostic 'f',0, A_F_SCREEN,
5135209Sbostic CONTROL('F'),0, A_F_SCREEN,
5235209Sbostic 'b',0, A_B_SCREEN,
5335209Sbostic CONTROL('B'),0, A_B_SCREEN,
5435209Sbostic 'R',0, A_FREPAINT,
5535209Sbostic 'r',0, A_REPAINT,
5635209Sbostic CONTROL('L'),0, A_REPAINT,
5735209Sbostic 'g',0, A_GOLINE,
5835209Sbostic 'p',0, A_PERCENT,
5935209Sbostic '%',0, A_PERCENT,
6035209Sbostic 'G',0, A_GOEND,
6135209Sbostic '0',0, A_DIGIT,
6235209Sbostic '1',0, A_DIGIT,
6335209Sbostic '2',0, A_DIGIT,
6435209Sbostic '3',0, A_DIGIT,
6535209Sbostic '4',0, A_DIGIT,
6635209Sbostic '5',0, A_DIGIT,
6735209Sbostic '6',0, A_DIGIT,
6835209Sbostic '7',0, A_DIGIT,
6935209Sbostic '8',0, A_DIGIT,
7035209Sbostic '9',0, A_DIGIT,
7135209Sbostic
7235209Sbostic '=',0, A_STAT,
7335209Sbostic CONTROL('G'),0, A_STAT,
7435209Sbostic '/',0, A_F_SEARCH,
7535209Sbostic '?',0, A_B_SEARCH,
7635209Sbostic 'n',0, A_AGAIN_SEARCH,
7735209Sbostic 'm',0, A_SETMARK,
7835209Sbostic '\'',0, A_GOMARK,
7935209Sbostic 'E',0, A_EXAMINE,
8035209Sbostic 'N',0, A_NEXT_FILE,
8136251Sbostic ':','n',0, A_NEXT_FILE,
8235209Sbostic 'P',0, A_PREV_FILE,
8335209Sbostic ':','p',0, A_PREV_FILE,
8435209Sbostic 'v',0, A_VISUAL,
8535209Sbostic
8635209Sbostic 'h',0, A_HELP,
8735209Sbostic 'q',0, A_QUIT,
8835209Sbostic ':','q',0, A_QUIT,
8936251Sbostic ':','t',0, A_TAGFILE,
9036294Sbostic ':', 'a', 0, A_FILE_LIST,
9136251Sbostic 'Z','Z',0, A_QUIT,
9235209Sbostic };
9335209Sbostic
9435209Sbostic char *cmdendtable = cmdtable + sizeof(cmdtable);
9535209Sbostic
9636251Sbostic #define MAX_CMDLEN 16
9735209Sbostic
9835209Sbostic static char kbuf[MAX_CMDLEN+1];
9935209Sbostic static char *kp = kbuf;
10035209Sbostic
10135209Sbostic /*
10236251Sbostic * Indicate that we're not in a prefix command
10336251Sbostic * by resetting the command buffer pointer.
10436251Sbostic */
noprefix()10536251Sbostic noprefix()
10636251Sbostic {
10736251Sbostic kp = kbuf;
10836251Sbostic }
10936251Sbostic
11036251Sbostic /*
11135209Sbostic * Decode a command character and return the associated action.
11235209Sbostic */
cmd_decode(c)11335209Sbostic cmd_decode(c)
11435209Sbostic int c;
11535209Sbostic {
11635209Sbostic register int action = A_INVALID;
11735209Sbostic
11835209Sbostic /*
11935209Sbostic * Append the new command character to the command string in kbuf.
12035209Sbostic */
12135209Sbostic *kp++ = c;
12235209Sbostic *kp = '\0';
12335209Sbostic
12436251Sbostic action = cmd_search(cmdtable, cmdendtable);
12535209Sbostic
12636251Sbostic /* This is not a prefix character. */
12735209Sbostic if (action != A_PREFIX)
12835209Sbostic noprefix();
12936251Sbostic return(action);
13035209Sbostic }
13135209Sbostic
13235209Sbostic /*
13335209Sbostic * Search a command table for the current command string (in kbuf).
13435209Sbostic */
cmd_search(table,endtable)13535209Sbostic cmd_search(table, endtable)
13635209Sbostic char *table;
13735209Sbostic char *endtable;
13835209Sbostic {
13936251Sbostic register char *p, *q;
14035209Sbostic
14136251Sbostic for (p = table, q = kbuf; p < endtable; p++, q++) {
14236251Sbostic if (*p == *q) {
14335209Sbostic /*
14435209Sbostic * Current characters match.
14535209Sbostic * If we're at the end of the string, we've found it.
14635209Sbostic * Return the action code, which is the character
14735209Sbostic * after the null at the end of the string
14835209Sbostic * in the command table.
14935209Sbostic */
15035209Sbostic if (*p == '\0')
15136251Sbostic return(p[1]);
15236251Sbostic }
15336251Sbostic else if (*q == '\0') {
15435209Sbostic /*
15535209Sbostic * Hit the end of the user's command,
15635209Sbostic * but not the end of the string in the command table.
15735209Sbostic * The user's command is incomplete.
15835209Sbostic */
15936251Sbostic return(A_PREFIX);
16036251Sbostic } else {
16135209Sbostic /*
16235209Sbostic * Not a match.
16335209Sbostic * Skip ahead to the next command in the
16435209Sbostic * command table, and reset the pointer
16535209Sbostic * to the user's command.
16635209Sbostic */
16736251Sbostic while (*p++ != '\0');
16835209Sbostic q = kbuf-1;
16935209Sbostic }
17035209Sbostic }
17135209Sbostic /*
17235209Sbostic * No match found in the entire command table.
17335209Sbostic */
17436251Sbostic return(A_INVALID);
17535209Sbostic }
176