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