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