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