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