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