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