checkpoint of hacking for mail.cs.berkeley.edu
[unix-history] / usr / src / usr.bin / more / decode.c
CommitLineData
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
bc0c28c1 10static char sccsid[] = "@(#)decode.c 5.9 (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
40static 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
94char *cmdendtable = cmdtable + sizeof(cmdtable);
95
bc258617 96#define MAX_CMDLEN 16
bfe13c81
KB
97
98static char kbuf[MAX_CMDLEN+1];
99static 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 */
105noprefix()
106{
107 kp = kbuf;
108}
109
bfe13c81
KB
110/*
111 * Decode a command character and return the associated action.
112 */
bfe13c81
KB
113cmd_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 */
bfe13c81
KB
135cmd_search(table, endtable)
136 char *table;
137 char *endtable;
138{
bc258617 139 register char *p, *q;
bfe13c81 140
bc258617
KB
141 for (p = table, q = kbuf; p < endtable; p++, q++) {
142 if (*p == *q) {
bfe13c81
KB
143 /*
144 * Current characters match.
145 * If we're at the end of the string, we've found it.
146 * Return the action code, which is the character
147 * after the null at the end of the string
148 * in the command table.
149 */
150 if (*p == '\0')
bc258617
KB
151 return(p[1]);
152 }
153 else if (*q == '\0') {
bfe13c81
KB
154 /*
155 * Hit the end of the user's command,
156 * but not the end of the string in the command table.
157 * The user's command is incomplete.
158 */
bc258617
KB
159 return(A_PREFIX);
160 } else {
bfe13c81
KB
161 /*
162 * Not a match.
163 * Skip ahead to the next command in the
164 * command table, and reset the pointer
165 * to the user's command.
166 */
bc258617 167 while (*p++ != '\0');
bfe13c81
KB
168 q = kbuf-1;
169 }
170 }
171 /*
172 * No match found in the entire command table.
173 */
bc258617 174 return(A_INVALID);
bfe13c81 175}