135209Sbostic /* 235209Sbostic * Copyright (c) 1988 Mark Nudleman 335209Sbostic * Copyright (c) 1988 Regents of the University of California. 435209Sbostic * All rights reserved. 535209Sbostic * 6*42742Sbostic * %sccs.include.redist.c% 735209Sbostic */ 835209Sbostic 935209Sbostic #ifndef lint 10*42742Sbostic static char sccsid[] = "@(#)decode.c 5.8 (Berkeley) 06/01/90"; 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 */ 10536251Sbostic noprefix() 10636251Sbostic { 10736251Sbostic kp = kbuf; 10836251Sbostic } 10936251Sbostic 11036251Sbostic /* 11135209Sbostic * Decode a command character and return the associated action. 11235209Sbostic */ 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 */ 13536251Sbostic static 13635209Sbostic cmd_search(table, endtable) 13735209Sbostic char *table; 13835209Sbostic char *endtable; 13935209Sbostic { 14036251Sbostic register char *p, *q; 14135209Sbostic 14236251Sbostic for (p = table, q = kbuf; p < endtable; p++, q++) { 14336251Sbostic if (*p == *q) { 14435209Sbostic /* 14535209Sbostic * Current characters match. 14635209Sbostic * If we're at the end of the string, we've found it. 14735209Sbostic * Return the action code, which is the character 14835209Sbostic * after the null at the end of the string 14935209Sbostic * in the command table. 15035209Sbostic */ 15135209Sbostic if (*p == '\0') 15236251Sbostic return(p[1]); 15336251Sbostic } 15436251Sbostic else if (*q == '\0') { 15535209Sbostic /* 15635209Sbostic * Hit the end of the user's command, 15735209Sbostic * but not the end of the string in the command table. 15835209Sbostic * The user's command is incomplete. 15935209Sbostic */ 16036251Sbostic return(A_PREFIX); 16136251Sbostic } else { 16235209Sbostic /* 16335209Sbostic * Not a match. 16435209Sbostic * Skip ahead to the next command in the 16535209Sbostic * command table, and reset the pointer 16635209Sbostic * to the user's command. 16735209Sbostic */ 16836251Sbostic while (*p++ != '\0'); 16935209Sbostic q = kbuf-1; 17035209Sbostic } 17135209Sbostic } 17235209Sbostic /* 17335209Sbostic * No match found in the entire command table. 17435209Sbostic */ 17536251Sbostic return(A_INVALID); 17635209Sbostic } 177