parse.c revision 1.14 1 1.14 jdolecek /* $NetBSD: parse.c,v 1.14 2001/01/23 15:55:30 jdolecek 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.6 christos #include <sys/cdefs.h>
40 1.1 cgd #if !defined(lint) && !defined(SCCSID)
41 1.4 lukem #if 0
42 1.1 cgd static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/4/93";
43 1.4 lukem #else
44 1.14 jdolecek __RCSID("$NetBSD: parse.c,v 1.14 2001/01/23 15:55:30 jdolecek Exp $");
45 1.4 lukem #endif
46 1.1 cgd #endif /* not lint && not SCCSID */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * parse.c: parse an editline extended command
50 1.1 cgd *
51 1.1 cgd * commands are:
52 1.1 cgd *
53 1.1 cgd * bind
54 1.1 cgd * echotc
55 1.8 lukem * edit
56 1.4 lukem * gettc
57 1.4 lukem * history
58 1.1 cgd * settc
59 1.4 lukem * setty
60 1.1 cgd */
61 1.1 cgd #include "sys.h"
62 1.1 cgd #include "el.h"
63 1.1 cgd #include "tokenizer.h"
64 1.12 jdolecek #include <stdlib.h>
65 1.1 cgd
66 1.14 jdolecek private const struct {
67 1.13 lukem char *name;
68 1.13 lukem int (*func)(EditLine *, int, char **);
69 1.1 cgd } cmds[] = {
70 1.13 lukem { "bind", map_bind },
71 1.13 lukem { "echotc", term_echotc },
72 1.13 lukem { "edit", el_editmode },
73 1.13 lukem { "history", hist_list },
74 1.13 lukem { "telltc", term_telltc },
75 1.13 lukem { "settc", term_settc },
76 1.13 lukem { "setty", tty_stty },
77 1.13 lukem { NULL, NULL }
78 1.1 cgd };
79 1.1 cgd
80 1.1 cgd
81 1.1 cgd /* parse_line():
82 1.1 cgd * Parse a line and dispatch it
83 1.1 cgd */
84 1.1 cgd protected int
85 1.13 lukem parse_line(EditLine *el, const char *line)
86 1.1 cgd {
87 1.13 lukem char **argv;
88 1.13 lukem int argc;
89 1.13 lukem Tokenizer *tok;
90 1.13 lukem
91 1.13 lukem tok = tok_init(NULL);
92 1.13 lukem tok_line(tok, line, &argc, &argv);
93 1.13 lukem argc = el_parse(el, argc, argv);
94 1.13 lukem tok_end(tok);
95 1.13 lukem return (argc);
96 1.1 cgd }
97 1.1 cgd
98 1.13 lukem
99 1.1 cgd /* el_parse():
100 1.1 cgd * Command dispatcher
101 1.1 cgd */
102 1.1 cgd public int
103 1.13 lukem el_parse(EditLine *el, int argc, char *argv[])
104 1.1 cgd {
105 1.13 lukem char *ptr;
106 1.13 lukem int i;
107 1.1 cgd
108 1.13 lukem if (argc < 1)
109 1.13 lukem return (-1);
110 1.13 lukem ptr = strchr(argv[0], ':');
111 1.13 lukem if (ptr != NULL) {
112 1.13 lukem char *tprog;
113 1.13 lukem size_t l;
114 1.13 lukem
115 1.13 lukem if (ptr == argv[0])
116 1.13 lukem return (0);
117 1.13 lukem l = ptr - argv[0] - 1;
118 1.13 lukem tprog = (char *) el_malloc(l + 1);
119 1.13 lukem if (tprog == NULL)
120 1.13 lukem return (0);
121 1.13 lukem (void) strncpy(tprog, argv[0], l);
122 1.13 lukem tprog[l] = '\0';
123 1.13 lukem ptr++;
124 1.13 lukem l = el_match(el->el_prog, tprog);
125 1.13 lukem el_free(tprog);
126 1.13 lukem if (!l)
127 1.13 lukem return (0);
128 1.13 lukem } else
129 1.13 lukem ptr = argv[0];
130 1.13 lukem
131 1.13 lukem for (i = 0; cmds[i].name != NULL; i++)
132 1.13 lukem if (strcmp(cmds[i].name, ptr) == 0) {
133 1.13 lukem i = (*cmds[i].func) (el, argc, argv);
134 1.13 lukem return (-i);
135 1.13 lukem }
136 1.13 lukem return (-1);
137 1.1 cgd }
138 1.1 cgd
139 1.1 cgd
140 1.1 cgd /* parse__escape():
141 1.1 cgd * Parse a string of the form ^<char> \<odigit> \<char> and return
142 1.1 cgd * the appropriate character or -1 if the escape is not valid
143 1.1 cgd */
144 1.1 cgd protected int
145 1.13 lukem parse__escape(const char **const ptr)
146 1.1 cgd {
147 1.13 lukem const char *p;
148 1.13 lukem int c;
149 1.1 cgd
150 1.13 lukem p = *ptr;
151 1.1 cgd
152 1.13 lukem if (p[1] == 0)
153 1.13 lukem return (-1);
154 1.1 cgd
155 1.13 lukem if (*p == '\\') {
156 1.13 lukem p++;
157 1.13 lukem switch (*p) {
158 1.13 lukem case 'a':
159 1.13 lukem c = '\007'; /* Bell */
160 1.13 lukem break;
161 1.13 lukem case 'b':
162 1.13 lukem c = '\010'; /* Backspace */
163 1.13 lukem break;
164 1.13 lukem case 't':
165 1.13 lukem c = '\011'; /* Horizontal Tab */
166 1.13 lukem break;
167 1.13 lukem case 'n':
168 1.13 lukem c = '\012'; /* New Line */
169 1.13 lukem break;
170 1.13 lukem case 'v':
171 1.13 lukem c = '\013'; /* Vertical Tab */
172 1.13 lukem break;
173 1.13 lukem case 'f':
174 1.13 lukem c = '\014'; /* Form Feed */
175 1.13 lukem break;
176 1.13 lukem case 'r':
177 1.13 lukem c = '\015'; /* Carriage Return */
178 1.13 lukem break;
179 1.13 lukem case 'e':
180 1.13 lukem c = '\033'; /* Escape */
181 1.13 lukem break;
182 1.13 lukem case '0':
183 1.13 lukem case '1':
184 1.13 lukem case '2':
185 1.13 lukem case '3':
186 1.13 lukem case '4':
187 1.13 lukem case '5':
188 1.13 lukem case '6':
189 1.13 lukem case '7':
190 1.13 lukem {
191 1.13 lukem int cnt, ch;
192 1.13 lukem
193 1.13 lukem for (cnt = 0, c = 0; cnt < 3; cnt++) {
194 1.13 lukem ch = *p++;
195 1.13 lukem if (ch < '0' || ch > '7') {
196 1.13 lukem p--;
197 1.13 lukem break;
198 1.13 lukem }
199 1.13 lukem c = (c << 3) | (ch - '0');
200 1.13 lukem }
201 1.13 lukem if ((c & 0xffffff00) != 0)
202 1.13 lukem return (-1);
203 1.13 lukem --p;
204 1.13 lukem break;
205 1.13 lukem }
206 1.13 lukem default:
207 1.13 lukem c = *p;
208 1.1 cgd break;
209 1.1 cgd }
210 1.13 lukem } else if (*p == '^' && isalpha((unsigned char) p[1])) {
211 1.13 lukem p++;
212 1.13 lukem c = (*p == '?') ? '\177' : (*p & 0237);
213 1.13 lukem } else
214 1.13 lukem c = *p;
215 1.13 lukem *ptr = ++p;
216 1.13 lukem return (c);
217 1.1 cgd }
218 1.1 cgd /* parse__string():
219 1.1 cgd * Parse the escapes from in and put the raw string out
220 1.1 cgd */
221 1.1 cgd protected char *
222 1.13 lukem parse__string(char *out, const char *in)
223 1.1 cgd {
224 1.13 lukem char *rv = out;
225 1.13 lukem int n;
226 1.13 lukem
227 1.13 lukem for (;;)
228 1.13 lukem switch (*in) {
229 1.13 lukem case '\0':
230 1.13 lukem *out = '\0';
231 1.13 lukem return (rv);
232 1.13 lukem
233 1.13 lukem case '\\':
234 1.13 lukem case '^':
235 1.13 lukem if ((n = parse__escape(&in)) == -1)
236 1.13 lukem return (NULL);
237 1.13 lukem *out++ = n;
238 1.13 lukem break;
239 1.13 lukem
240 1.13 lukem default:
241 1.13 lukem *out++ = *in++;
242 1.13 lukem break;
243 1.13 lukem }
244 1.1 cgd }
245 1.1 cgd
246 1.13 lukem
247 1.1 cgd /* parse_cmd():
248 1.1 cgd * Return the command number for the command string given
249 1.1 cgd * or -1 if one is not found
250 1.1 cgd */
251 1.1 cgd protected int
252 1.13 lukem parse_cmd(EditLine *el, const char *cmd)
253 1.1 cgd {
254 1.13 lukem el_bindings_t *b;
255 1.1 cgd
256 1.13 lukem for (b = el->el_map.help; b->name != NULL; b++)
257 1.13 lukem if (strcmp(b->name, cmd) == 0)
258 1.13 lukem return (b->func);
259 1.13 lukem return (-1);
260 1.1 cgd }
261