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