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