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