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