decode.c revision 1.3 1 1.3 christos /* $NetBSD: decode.c,v 1.3 1998/02/04 11:08:45 christos 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.3 christos #include <sys/cdefs.h>
38 1.1 cjs #ifndef lint
39 1.3 christos #if 0
40 1.1 cjs static char sccsid[] = "@(#)decode.c 8.1 (Berkeley) 6/6/93";
41 1.3 christos #else
42 1.3 christos __RCSID("$NetBSD: decode.c,v 1.3 1998/02/04 11:08:45 christos Exp $");
43 1.3 christos #endif
44 1.1 cjs #endif /* not lint */
45 1.1 cjs
46 1.1 cjs /*
47 1.1 cjs * Routines to decode user commands.
48 1.1 cjs *
49 1.1 cjs * This is all table driven.
50 1.1 cjs * A command table is a sequence of command descriptors.
51 1.1 cjs * Each command descriptor is a sequence of bytes with the following format:
52 1.1 cjs * <c1><c2>...<cN><0><action>
53 1.1 cjs * The characters c1,c2,...,cN are the command string; that is,
54 1.1 cjs * the characters which the user must type.
55 1.1 cjs * It is terminated by a null <0> byte.
56 1.1 cjs * The byte after the null byte is the action code associated
57 1.1 cjs * with the command string.
58 1.1 cjs *
59 1.1 cjs * The default commands are described by cmdtable.
60 1.1 cjs */
61 1.1 cjs
62 1.1 cjs #include <sys/param.h>
63 1.1 cjs #include <sys/file.h>
64 1.1 cjs #include <stdio.h>
65 1.3 christos
66 1.3 christos #include "less.h"
67 1.3 christos #include "extern.h"
68 1.1 cjs
69 1.1 cjs /*
70 1.1 cjs * Command table is ordered roughly according to expected
71 1.1 cjs * frequency of use, so the common commands are near the beginning.
72 1.1 cjs */
73 1.1 cjs #define CONTROL(c) ((c)&037)
74 1.1 cjs
75 1.1 cjs static char cmdtable[] = {
76 1.1 cjs '\r',0, A_F_LINE,
77 1.1 cjs '\n',0, A_F_LINE,
78 1.1 cjs 'j',0, A_F_LINE,
79 1.1 cjs 'k',0, A_B_LINE,
80 1.1 cjs 'd',0, A_F_SCROLL,
81 1.1 cjs CONTROL('D'),0, A_F_SCROLL,
82 1.1 cjs 'u',0, A_B_SCROLL,
83 1.1 cjs CONTROL('U'),0, A_B_SCROLL,
84 1.1 cjs ' ',0, A_F_SCREEN,
85 1.1 cjs 'f',0, A_F_SCREEN,
86 1.1 cjs CONTROL('F'),0, A_F_SCREEN,
87 1.1 cjs 'b',0, A_B_SCREEN,
88 1.1 cjs CONTROL('B'),0, A_B_SCREEN,
89 1.1 cjs 'R',0, A_FREPAINT,
90 1.1 cjs 'r',0, A_REPAINT,
91 1.1 cjs CONTROL('L'),0, A_REPAINT,
92 1.1 cjs 'g',0, A_GOLINE,
93 1.1 cjs 'p',0, A_PERCENT,
94 1.1 cjs '%',0, A_PERCENT,
95 1.1 cjs 'G',0, A_GOEND,
96 1.1 cjs '0',0, A_DIGIT,
97 1.1 cjs '1',0, A_DIGIT,
98 1.1 cjs '2',0, A_DIGIT,
99 1.1 cjs '3',0, A_DIGIT,
100 1.1 cjs '4',0, A_DIGIT,
101 1.1 cjs '5',0, A_DIGIT,
102 1.1 cjs '6',0, A_DIGIT,
103 1.1 cjs '7',0, A_DIGIT,
104 1.1 cjs '8',0, A_DIGIT,
105 1.1 cjs '9',0, A_DIGIT,
106 1.1 cjs
107 1.1 cjs '=',0, A_STAT,
108 1.1 cjs CONTROL('G'),0, A_STAT,
109 1.1 cjs '/',0, A_F_SEARCH,
110 1.1 cjs '?',0, A_B_SEARCH,
111 1.1 cjs 'n',0, A_AGAIN_SEARCH,
112 1.1 cjs 'm',0, A_SETMARK,
113 1.1 cjs '\'',0, A_GOMARK,
114 1.1 cjs 'E',0, A_EXAMINE,
115 1.1 cjs 'N',0, A_NEXT_FILE,
116 1.1 cjs ':','n',0, A_NEXT_FILE,
117 1.1 cjs 'P',0, A_PREV_FILE,
118 1.1 cjs ':','p',0, A_PREV_FILE,
119 1.1 cjs 'v',0, A_VISUAL,
120 1.1 cjs
121 1.1 cjs 'h',0, A_HELP,
122 1.1 cjs 'q',0, A_QUIT,
123 1.1 cjs ':','q',0, A_QUIT,
124 1.1 cjs ':','t',0, A_TAGFILE,
125 1.1 cjs ':', 'a', 0, A_FILE_LIST,
126 1.1 cjs 'Z','Z',0, A_QUIT,
127 1.1 cjs };
128 1.1 cjs
129 1.1 cjs char *cmdendtable = cmdtable + sizeof(cmdtable);
130 1.1 cjs
131 1.1 cjs #define MAX_CMDLEN 16
132 1.1 cjs
133 1.1 cjs static char kbuf[MAX_CMDLEN+1];
134 1.1 cjs static char *kp = kbuf;
135 1.1 cjs
136 1.1 cjs /*
137 1.1 cjs * Indicate that we're not in a prefix command
138 1.1 cjs * by resetting the command buffer pointer.
139 1.1 cjs */
140 1.3 christos void
141 1.1 cjs noprefix()
142 1.1 cjs {
143 1.1 cjs kp = kbuf;
144 1.1 cjs }
145 1.1 cjs
146 1.1 cjs /*
147 1.1 cjs * Decode a command character and return the associated action.
148 1.1 cjs */
149 1.3 christos int
150 1.1 cjs cmd_decode(c)
151 1.1 cjs int c;
152 1.1 cjs {
153 1.3 christos int action = A_INVALID;
154 1.1 cjs
155 1.1 cjs /*
156 1.1 cjs * Append the new command character to the command string in kbuf.
157 1.1 cjs */
158 1.1 cjs *kp++ = c;
159 1.1 cjs *kp = '\0';
160 1.1 cjs
161 1.1 cjs action = cmd_search(cmdtable, cmdendtable);
162 1.1 cjs
163 1.1 cjs /* This is not a prefix character. */
164 1.1 cjs if (action != A_PREFIX)
165 1.1 cjs noprefix();
166 1.1 cjs return(action);
167 1.1 cjs }
168 1.1 cjs
169 1.1 cjs /*
170 1.1 cjs * Search a command table for the current command string (in kbuf).
171 1.1 cjs */
172 1.3 christos int
173 1.1 cjs cmd_search(table, endtable)
174 1.1 cjs char *table;
175 1.1 cjs char *endtable;
176 1.1 cjs {
177 1.3 christos char *p, *q;
178 1.1 cjs
179 1.1 cjs for (p = table, q = kbuf; p < endtable; p++, q++) {
180 1.1 cjs if (*p == *q) {
181 1.1 cjs /*
182 1.1 cjs * Current characters match.
183 1.1 cjs * If we're at the end of the string, we've found it.
184 1.1 cjs * Return the action code, which is the character
185 1.1 cjs * after the null at the end of the string
186 1.1 cjs * in the command table.
187 1.1 cjs */
188 1.1 cjs if (*p == '\0')
189 1.1 cjs return(p[1]);
190 1.1 cjs }
191 1.1 cjs else if (*q == '\0') {
192 1.1 cjs /*
193 1.1 cjs * Hit the end of the user's command,
194 1.1 cjs * but not the end of the string in the command table.
195 1.1 cjs * The user's command is incomplete.
196 1.1 cjs */
197 1.1 cjs return(A_PREFIX);
198 1.1 cjs } else {
199 1.1 cjs /*
200 1.1 cjs * Not a match.
201 1.1 cjs * Skip ahead to the next command in the
202 1.1 cjs * command table, and reset the pointer
203 1.1 cjs * to the user's command.
204 1.1 cjs */
205 1.1 cjs while (*p++ != '\0');
206 1.1 cjs q = kbuf-1;
207 1.1 cjs }
208 1.1 cjs }
209 1.1 cjs /*
210 1.1 cjs * No match found in the entire command table.
211 1.1 cjs */
212 1.1 cjs return(A_INVALID);
213 1.1 cjs }
214