xref: /netbsd-src/lib/libedit/parse.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: parse.c,v 1.9 1998/12/12 20:08:22 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. 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.9 1998/12/12 20:08:22 christos 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 	l = ptr - argv[0] - 1;
119 	tprog = (char *)malloc(l + 1);
120 	if (tprog == NULL)
121 	    return 0;
122 	(void)strncpy(tprog, argv[0], l);
123 	tprog[l] = '\0';
124 	ptr++;
125 	l = el_match(el->el_prog, tprog);
126 	free(tprog);
127 	if (!l)
128 	    return 0;
129     }
130     else
131 	ptr = argv[0];
132 
133     for (i = 0; cmds[i].name != NULL; i++)
134 	if (strcmp(cmds[i].name, ptr) == 0) {
135 	    i = (*cmds[i].func)(el, argc, argv);
136 	    return -i;
137 	}
138 
139     return -1;
140 }
141 
142 
143 /* parse__escape():
144  *	Parse a string of the form ^<char> \<odigit> \<char> and return
145  *	the appropriate character or -1 if the escape is not valid
146  */
147 protected int
148 parse__escape(ptr)
149     const char  ** const ptr;
150 {
151     const char   *p;
152     int   c;
153 
154     p = *ptr;
155 
156     if (p[1] == 0)
157 	return -1;
158 
159     if (*p == '\\') {
160 	p++;
161 	switch (*p) {
162 	case 'a':
163 	    c = '\007';		/* Bell */
164 	    break;
165 	case 'b':
166 	    c = '\010';		/* Backspace */
167 	    break;
168 	case 't':
169 	    c = '\011';		/* Horizontal Tab */
170 	    break;
171 	case 'n':
172 	    c = '\012';		/* New Line */
173 	    break;
174 	case 'v':
175 	    c = '\013';		/* Vertical Tab */
176 	    break;
177 	case 'f':
178 	    c = '\014';		/* Form Feed */
179 	    break;
180 	case 'r':
181 	    c = '\015';		/* Carriage Return */
182 	    break;
183 	case 'e':
184 	    c = '\033';		/* Escape */
185 	    break;
186 	case '0':
187 	case '1':
188 	case '2':
189 	case '3':
190 	case '4':
191 	case '5':
192 	case '6':
193 	case '7':
194 	    {
195 		int cnt, ch;
196 
197 		for (cnt = 0, c = 0; cnt < 3; cnt++) {
198 		    ch = *p++;
199 		    if (ch < '0' || ch > '7') {
200 			p--;
201 			break;
202 		    }
203 		    c = (c << 3) | (ch - '0');
204 		}
205 		if ((c & 0xffffff00) != 0)
206 		    return -1;
207 		--p;
208 	    }
209 	    break;
210 	default:
211 	    c = *p;
212 	    break;
213 	}
214     }
215     else if (*p == '^' && isalpha((unsigned char) p[1])) {
216 	p++;
217 	c = (*p == '?') ? '\177' : (*p & 0237);
218     }
219     else
220 	c = *p;
221     *ptr = ++p;
222     return c;
223 }
224 
225 /* parse__string():
226  *	Parse the escapes from in and put the raw string out
227  */
228 protected char *
229 parse__string(out, in)
230     char *out;
231     const char *in;
232 {
233     char *rv = out;
234     int n;
235     for (;;)
236 	switch (*in) {
237 	case '\0':
238 	    *out = '\0';
239 	    return rv;
240 
241 	case '\\':
242 	case '^':
243 	    if ((n = parse__escape(&in)) == -1)
244 		return NULL;
245 	    *out++ = n;
246 	    break;
247 
248 	default:
249 	    *out++ = *in++;
250 	    break;
251 	}
252 }
253 
254 /* parse_cmd():
255  *	Return the command number for the command string given
256  *	or -1 if one is not found
257  */
258 protected int
259 parse_cmd(el, cmd)
260     EditLine *el;
261     const char *cmd;
262 {
263     el_bindings_t *b;
264 
265     for (b = el->el_map.help; b->name != NULL; b++)
266 	if (strcmp(b->name, cmd) == 0)
267 	    return b->func;
268     return -1;
269 }
270