parse.c revision 1.30 1 1.30 christos /* $NetBSD: parse.c,v 1.30 2016/02/16 14:06:05 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.17 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.15 christos #include "config.h"
36 1.1 cgd #if !defined(lint) && !defined(SCCSID)
37 1.4 lukem #if 0
38 1.1 cgd static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/4/93";
39 1.4 lukem #else
40 1.30 christos __RCSID("$NetBSD: parse.c,v 1.30 2016/02/16 14:06:05 christos Exp $");
41 1.4 lukem #endif
42 1.1 cgd #endif /* not lint && not SCCSID */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * parse.c: parse an editline extended command
46 1.1 cgd *
47 1.1 cgd * commands are:
48 1.1 cgd *
49 1.1 cgd * bind
50 1.1 cgd * echotc
51 1.8 lukem * edit
52 1.4 lukem * gettc
53 1.4 lukem * history
54 1.1 cgd * settc
55 1.4 lukem * setty
56 1.1 cgd */
57 1.30 christos #include <stdlib.h>
58 1.30 christos
59 1.30 christos #include "histedit.h"
60 1.1 cgd #include "el.h"
61 1.1 cgd
62 1.14 jdolecek private const struct {
63 1.23 christos const Char *name;
64 1.23 christos int (*func)(EditLine *, int, const Char **);
65 1.1 cgd } cmds[] = {
66 1.23 christos { STR("bind"), map_bind },
67 1.24 christos { STR("echotc"), terminal_echotc },
68 1.23 christos { STR("edit"), el_editmode },
69 1.23 christos { STR("history"), hist_command },
70 1.24 christos { STR("telltc"), terminal_telltc },
71 1.24 christos { STR("settc"), terminal_settc },
72 1.23 christos { STR("setty"), tty_stty },
73 1.23 christos { 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.23 christos parse_line(EditLine *el, const Char *line)
82 1.1 cgd {
83 1.23 christos const Char **argv;
84 1.13 lukem int argc;
85 1.23 christos TYPE(Tokenizer) *tok;
86 1.13 lukem
87 1.23 christos tok = FUN(tok,init)(NULL);
88 1.23 christos FUN(tok,str)(tok, line, &argc, &argv);
89 1.23 christos argc = FUN(el,parse)(el, argc, argv);
90 1.23 christos FUN(tok,end)(tok);
91 1.25 christos return argc;
92 1.1 cgd }
93 1.1 cgd
94 1.13 lukem
95 1.1 cgd /* el_parse():
96 1.1 cgd * Command dispatcher
97 1.1 cgd */
98 1.1 cgd public int
99 1.23 christos FUN(el,parse)(EditLine *el, int argc, const Char *argv[])
100 1.1 cgd {
101 1.23 christos const Char *ptr;
102 1.13 lukem int i;
103 1.1 cgd
104 1.13 lukem if (argc < 1)
105 1.25 christos return -1;
106 1.23 christos ptr = Strchr(argv[0], ':');
107 1.13 lukem if (ptr != NULL) {
108 1.23 christos Char *tprog;
109 1.13 lukem size_t l;
110 1.13 lukem
111 1.13 lukem if (ptr == argv[0])
112 1.25 christos return 0;
113 1.26 christos l = (size_t)(ptr - argv[0] - 1);
114 1.23 christos tprog = el_malloc((l + 1) * sizeof(*tprog));
115 1.13 lukem if (tprog == NULL)
116 1.25 christos return 0;
117 1.23 christos (void) Strncpy(tprog, argv[0], l);
118 1.13 lukem tprog[l] = '\0';
119 1.13 lukem ptr++;
120 1.26 christos l = (size_t)el_match(el->el_prog, tprog);
121 1.13 lukem el_free(tprog);
122 1.13 lukem if (!l)
123 1.25 christos return 0;
124 1.13 lukem } else
125 1.13 lukem ptr = argv[0];
126 1.13 lukem
127 1.13 lukem for (i = 0; cmds[i].name != NULL; i++)
128 1.23 christos if (Strcmp(cmds[i].name, ptr) == 0) {
129 1.13 lukem i = (*cmds[i].func) (el, argc, argv);
130 1.25 christos return -i;
131 1.13 lukem }
132 1.25 christos return -1;
133 1.1 cgd }
134 1.1 cgd
135 1.1 cgd
136 1.1 cgd /* parse__escape():
137 1.23 christos * Parse a string of the form ^<char> \<odigit> \<char> \U+xxxx and return
138 1.1 cgd * the appropriate character or -1 if the escape is not valid
139 1.1 cgd */
140 1.1 cgd protected int
141 1.23 christos parse__escape(const Char **ptr)
142 1.1 cgd {
143 1.23 christos const Char *p;
144 1.29 christos wint_t c;
145 1.1 cgd
146 1.13 lukem p = *ptr;
147 1.1 cgd
148 1.13 lukem if (p[1] == 0)
149 1.25 christos return -1;
150 1.1 cgd
151 1.13 lukem if (*p == '\\') {
152 1.13 lukem p++;
153 1.13 lukem switch (*p) {
154 1.13 lukem case 'a':
155 1.13 lukem c = '\007'; /* Bell */
156 1.13 lukem break;
157 1.13 lukem case 'b':
158 1.13 lukem c = '\010'; /* Backspace */
159 1.13 lukem break;
160 1.13 lukem case 't':
161 1.13 lukem c = '\011'; /* Horizontal Tab */
162 1.13 lukem break;
163 1.13 lukem case 'n':
164 1.13 lukem c = '\012'; /* New Line */
165 1.13 lukem break;
166 1.13 lukem case 'v':
167 1.13 lukem c = '\013'; /* Vertical Tab */
168 1.13 lukem break;
169 1.13 lukem case 'f':
170 1.13 lukem c = '\014'; /* Form Feed */
171 1.13 lukem break;
172 1.13 lukem case 'r':
173 1.13 lukem c = '\015'; /* Carriage Return */
174 1.13 lukem break;
175 1.13 lukem case 'e':
176 1.13 lukem c = '\033'; /* Escape */
177 1.13 lukem break;
178 1.23 christos case 'U': /* Unicode \U+xxxx or \U+xxxxx format */
179 1.23 christos {
180 1.23 christos int i;
181 1.23 christos const Char hex[] = STR("0123456789ABCDEF");
182 1.23 christos const Char *h;
183 1.23 christos ++p;
184 1.23 christos if (*p++ != '+')
185 1.25 christos return -1;
186 1.23 christos c = 0;
187 1.23 christos for (i = 0; i < 5; ++i) {
188 1.23 christos h = Strchr(hex, *p++);
189 1.23 christos if (!h && i < 4)
190 1.25 christos return -1;
191 1.23 christos else if (h)
192 1.23 christos c = (c << 4) | ((int)(h - hex));
193 1.23 christos else
194 1.23 christos --p;
195 1.23 christos }
196 1.23 christos if (c > 0x10FFFF) /* outside valid character range */
197 1.23 christos return -1;
198 1.23 christos break;
199 1.23 christos }
200 1.13 lukem case '0':
201 1.13 lukem case '1':
202 1.13 lukem case '2':
203 1.13 lukem case '3':
204 1.13 lukem case '4':
205 1.13 lukem case '5':
206 1.13 lukem case '6':
207 1.13 lukem case '7':
208 1.13 lukem {
209 1.13 lukem int cnt, ch;
210 1.13 lukem
211 1.13 lukem for (cnt = 0, c = 0; cnt < 3; cnt++) {
212 1.13 lukem ch = *p++;
213 1.13 lukem if (ch < '0' || ch > '7') {
214 1.13 lukem p--;
215 1.13 lukem break;
216 1.13 lukem }
217 1.13 lukem c = (c << 3) | (ch - '0');
218 1.13 lukem }
219 1.26 christos if ((c & (wint_t)0xffffff00) != (wint_t)0)
220 1.25 christos return -1;
221 1.13 lukem --p;
222 1.13 lukem break;
223 1.13 lukem }
224 1.13 lukem default:
225 1.13 lukem c = *p;
226 1.1 cgd break;
227 1.1 cgd }
228 1.18 christos } else if (*p == '^') {
229 1.13 lukem p++;
230 1.13 lukem c = (*p == '?') ? '\177' : (*p & 0237);
231 1.13 lukem } else
232 1.13 lukem c = *p;
233 1.13 lukem *ptr = ++p;
234 1.25 christos return c;
235 1.1 cgd }
236 1.19 christos
237 1.1 cgd /* parse__string():
238 1.1 cgd * Parse the escapes from in and put the raw string out
239 1.1 cgd */
240 1.23 christos protected Char *
241 1.23 christos parse__string(Char *out, const Char *in)
242 1.1 cgd {
243 1.23 christos Char *rv = out;
244 1.13 lukem int n;
245 1.13 lukem
246 1.13 lukem for (;;)
247 1.13 lukem switch (*in) {
248 1.13 lukem case '\0':
249 1.13 lukem *out = '\0';
250 1.25 christos return rv;
251 1.13 lukem
252 1.13 lukem case '\\':
253 1.13 lukem case '^':
254 1.13 lukem if ((n = parse__escape(&in)) == -1)
255 1.25 christos return NULL;
256 1.28 christos *out++ = (Char)n;
257 1.13 lukem break;
258 1.19 christos
259 1.19 christos case 'M':
260 1.19 christos if (in[1] == '-' && in[2] != '\0') {
261 1.19 christos *out++ = '\033';
262 1.19 christos in += 2;
263 1.19 christos break;
264 1.19 christos }
265 1.19 christos /*FALLTHROUGH*/
266 1.13 lukem
267 1.13 lukem default:
268 1.13 lukem *out++ = *in++;
269 1.13 lukem break;
270 1.13 lukem }
271 1.1 cgd }
272 1.1 cgd
273 1.13 lukem
274 1.1 cgd /* parse_cmd():
275 1.1 cgd * Return the command number for the command string given
276 1.1 cgd * or -1 if one is not found
277 1.1 cgd */
278 1.1 cgd protected int
279 1.23 christos parse_cmd(EditLine *el, const Char *cmd)
280 1.1 cgd {
281 1.27 christos el_bindings_t *b = el->el_map.help;
282 1.27 christos size_t i;
283 1.1 cgd
284 1.27 christos for (i = 0; i < el->el_map.nfunc; i++)
285 1.27 christos if (Strcmp(b[i].name, cmd) == 0)
286 1.27 christos return b[i].func;
287 1.25 christos return -1;
288 1.1 cgd }
289