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