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