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