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