1 /* $NetBSD: decode.c,v 1.4 2003/08/07 09:27:59 agc Exp $ */
4 * Copyright (c) 1988 Mark Nudelman
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
36 static char sccsid
[] = "@(#)decode.c 8.1 (Berkeley) 6/6/93";
38 __RCSID("$NetBSD: decode.c,v 1.4 2003/08/07 09:27:59 agc Exp $");
43 * Routines to decode user commands.
45 * This is all table driven.
46 * A command table is a sequence of command descriptors.
47 * Each command descriptor is a sequence of bytes with the following format:
48 * <c1><c2>...<cN><0><action>
49 * The characters c1,c2,...,cN are the command string; that is,
50 * the characters which the user must type.
51 * It is terminated by a null <0> byte.
52 * The byte after the null byte is the action code associated
53 * with the command string.
55 * The default commands are described by cmdtable.
58 #include <sys/param.h>
66 * Command table is ordered roughly according to expected
67 * frequency of use, so the common commands are near the beginning.
69 #define CONTROL(c) ((c)&037)
71 static char cmdtable
[] = {
77 CONTROL('D'),0, A_F_SCROLL
,
79 CONTROL('U'),0, A_B_SCROLL
,
82 CONTROL('F'),0, A_F_SCREEN
,
84 CONTROL('B'),0, A_B_SCREEN
,
87 CONTROL('L'),0, A_REPAINT
,
104 CONTROL('G'),0, A_STAT
,
107 'n',0, A_AGAIN_SEARCH
,
112 ':','n',0, A_NEXT_FILE
,
114 ':','p',0, A_PREV_FILE
,
120 ':','t',0, A_TAGFILE
,
121 ':', 'a', 0, A_FILE_LIST
,
125 char *cmdendtable
= cmdtable
+ sizeof(cmdtable
);
127 #define MAX_CMDLEN 16
129 static char kbuf
[MAX_CMDLEN
+1];
130 static char *kp
= kbuf
;
133 * Indicate that we're not in a prefix command
134 * by resetting the command buffer pointer.
143 * Decode a command character and return the associated action.
149 int action
= A_INVALID
;
152 * Append the new command character to the command string in kbuf.
157 action
= cmd_search(cmdtable
, cmdendtable
);
159 /* This is not a prefix character. */
160 if (action
!= A_PREFIX
)
166 * Search a command table for the current command string (in kbuf).
169 cmd_search(table
, endtable
)
175 for (p
= table
, q
= kbuf
; p
< endtable
; p
++, q
++) {
178 * Current characters match.
179 * If we're at the end of the string, we've found it.
180 * Return the action code, which is the character
181 * after the null at the end of the string
182 * in the command table.
187 else if (*q
== '\0') {
189 * Hit the end of the user's command,
190 * but not the end of the string in the command table.
191 * The user's command is incomplete.
197 * Skip ahead to the next command in the
198 * command table, and reset the pointer
199 * to the user's command.
201 while (*p
++ != '\0');
206 * No match found in the entire command table.