parse.c revision 1.7 1 /* $NetBSD: parse.c,v 1.7 1998/01/21 11:12:35 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if !defined(lint) && !defined(SCCSID)
41 #if 0
42 static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/4/93";
43 #else
44 __RCSID("$NetBSD: parse.c,v 1.7 1998/01/21 11:12:35 lukem Exp $");
45 #endif
46 #endif /* not lint && not SCCSID */
47
48 /*
49 * parse.c: parse an editline extended command
50 *
51 * commands are:
52 *
53 * bind
54 * echotc
55 * gettc
56 * history
57 * settc
58 * setty
59 */
60 #include "sys.h"
61 #include "el.h"
62 #include "tokenizer.h"
63
64 private struct {
65 char *name;
66 int (*func) __P((EditLine *, int, char **));
67 } cmds[] = {
68 { "bind", map_bind },
69 { "echotc", term_echotc },
70 { "history", hist_list },
71 { "telltc", term_telltc },
72 { "settc", term_settc },
73 { "setty", tty_stty },
74 { NULL, NULL }
75 };
76
77
78 /* parse_line():
79 * Parse a line and dispatch it
80 */
81 protected int
82 parse_line(el, line)
83 EditLine *el;
84 const char *line;
85 {
86 char **argv;
87 int argc;
88 Tokenizer *tok;
89
90 tok = tok_init(NULL);
91 tok_line(tok, line, &argc, &argv);
92 argc = el_parse(el, argc, argv);
93 tok_end(tok);
94 return argc;
95 }
96
97 /* el_parse():
98 * Command dispatcher
99 */
100 public int
101 el_parse(el, argc, argv)
102 EditLine *el;
103 int argc;
104 char *argv[];
105 {
106 char *ptr;
107 int i;
108
109 if (argc < 1)
110 return -1;
111 ptr = strchr(argv[0], ':');
112 if (ptr != NULL) {
113 char *tprog;
114 int l;
115
116 l = ptr - argv[0] - 1;
117 tprog = (char *)malloc(l + 1);
118 if (tprog == NULL)
119 return 0;
120 strncpy(tprog, argv[0], l);
121 tprog[l] = '\0';
122 ptr++;
123 l = el_match(el->el_prog, tprog);
124 free(tprog);
125 if (!l)
126 return 0;
127 }
128 else
129 ptr = argv[0];
130
131 for (i = 0; cmds[i].name != NULL; i++)
132 if (strcmp(cmds[i].name, ptr) == 0) {
133 i = (*cmds[i].func)(el, argc, argv);
134 return -i;
135 }
136
137 return -1;
138 }
139
140
141 /* parse__escape():
142 * Parse a string of the form ^<char> \<odigit> \<char> and return
143 * the appropriate character or -1 if the escape is not valid
144 */
145 protected int
146 parse__escape(ptr)
147 const char ** const ptr;
148 {
149 const char *p;
150 int c;
151
152 p = *ptr;
153
154 if (p[1] == 0)
155 return -1;
156
157 if (*p == '\\') {
158 p++;
159 switch (*p) {
160 case 'a':
161 c = '\007'; /* Bell */
162 break;
163 case 'b':
164 c = '\010'; /* Backspace */
165 break;
166 case 't':
167 c = '\011'; /* Horizontal Tab */
168 break;
169 case 'n':
170 c = '\012'; /* New Line */
171 break;
172 case 'v':
173 c = '\013'; /* Vertical Tab */
174 break;
175 case 'f':
176 c = '\014'; /* Form Feed */
177 break;
178 case 'r':
179 c = '\015'; /* Carriage Return */
180 break;
181 case 'e':
182 c = '\033'; /* Escape */
183 break;
184 case '0':
185 case '1':
186 case '2':
187 case '3':
188 case '4':
189 case '5':
190 case '6':
191 case '7':
192 {
193 int cnt, ch;
194
195 for (cnt = 0, c = 0; cnt < 3; cnt++) {
196 ch = *p++;
197 if (ch < '0' || ch > '7') {
198 p--;
199 break;
200 }
201 c = (c << 3) | (ch - '0');
202 }
203 if ((c & 0xffffff00) != 0)
204 return -1;
205 --p;
206 }
207 break;
208 default:
209 c = *p;
210 break;
211 }
212 }
213 else if (*p == '^' && isalpha((unsigned char) p[1])) {
214 p++;
215 c = (*p == '?') ? '\177' : (*p & 0237);
216 }
217 else
218 c = *p;
219 *ptr = ++p;
220 return c;
221 }
222
223 /* parse__string():
224 * Parse the escapes from in and put the raw string out
225 */
226 protected char *
227 parse__string(out, in)
228 char *out;
229 const char *in;
230 {
231 char *rv = out;
232 int n;
233 for (;;)
234 switch (*in) {
235 case '\0':
236 *out = '\0';
237 return rv;
238
239 case '\\':
240 case '^':
241 if ((n = parse__escape(&in)) == -1)
242 return NULL;
243 *out++ = n;
244 break;
245
246 default:
247 *out++ = *in++;
248 break;
249 }
250 }
251
252 /* parse_cmd():
253 * Return the command number for the command string given
254 * or -1 if one is not found
255 */
256 protected int
257 parse_cmd(el, cmd)
258 EditLine *el;
259 const char *cmd;
260 {
261 el_bindings_t *b;
262
263 for (b = el->el_map.help; b->name != NULL; b++)
264 if (strcmp(b->name, cmd) == 0)
265 return b->func;
266 return -1;
267 }
268