2003-12-05 16:37:48 +03:00
|
|
|
/* $NetBSD: parse.c,v 1.20 2003/12/05 13:37:48 lukem Exp $ */
|
1997-01-11 09:47:47 +03:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Christos Zoulas of Cornell University.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:42:00 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-06 10:01:42 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2002-03-18 19:00:50 +03:00
|
|
|
#include "config.h"
|
1994-05-06 10:01:42 +04:00
|
|
|
#if !defined(lint) && !defined(SCCSID)
|
1997-01-11 09:47:47 +03:00
|
|
|
#if 0
|
1994-05-06 10:01:42 +04:00
|
|
|
static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/4/93";
|
1997-01-11 09:47:47 +03:00
|
|
|
#else
|
2003-12-05 16:37:48 +03:00
|
|
|
__RCSID("$NetBSD: parse.c,v 1.20 2003/12/05 13:37:48 lukem Exp $");
|
1997-01-11 09:47:47 +03:00
|
|
|
#endif
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* not lint && not SCCSID */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse.c: parse an editline extended command
|
|
|
|
*
|
|
|
|
* commands are:
|
|
|
|
*
|
|
|
|
* bind
|
|
|
|
* echotc
|
1998-07-29 06:26:00 +04:00
|
|
|
* edit
|
1994-05-06 10:01:42 +04:00
|
|
|
* gettc
|
1997-01-11 09:47:47 +03:00
|
|
|
* history
|
|
|
|
* settc
|
|
|
|
* setty
|
1994-05-06 10:01:42 +04:00
|
|
|
*/
|
|
|
|
#include "el.h"
|
2000-03-10 15:25:47 +03:00
|
|
|
#include <stdlib.h>
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2001-01-23 18:55:30 +03:00
|
|
|
private const struct {
|
2002-03-18 19:00:50 +03:00
|
|
|
const char *name;
|
|
|
|
int (*func)(EditLine *, int, const char **);
|
1994-05-06 10:01:42 +04:00
|
|
|
} cmds[] = {
|
2000-09-05 02:06:28 +04:00
|
|
|
{ "bind", map_bind },
|
|
|
|
{ "echotc", term_echotc },
|
|
|
|
{ "edit", el_editmode },
|
2003-01-21 21:40:23 +03:00
|
|
|
{ "history", hist_command },
|
2000-09-05 02:06:28 +04:00
|
|
|
{ "telltc", term_telltc },
|
|
|
|
{ "settc", term_settc },
|
|
|
|
{ "setty", tty_stty },
|
|
|
|
{ NULL, NULL }
|
1994-05-06 10:01:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* parse_line():
|
|
|
|
* Parse a line and dispatch it
|
|
|
|
*/
|
|
|
|
protected int
|
2000-09-05 02:06:28 +04:00
|
|
|
parse_line(EditLine *el, const char *line)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2002-03-18 19:00:50 +03:00
|
|
|
const char **argv;
|
2000-09-05 02:06:28 +04:00
|
|
|
int argc;
|
|
|
|
Tokenizer *tok;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
tok = tok_init(NULL);
|
2003-12-05 16:37:48 +03:00
|
|
|
tok_str(tok, line, &argc, &argv);
|
2000-09-05 02:06:28 +04:00
|
|
|
argc = el_parse(el, argc, argv);
|
|
|
|
tok_end(tok);
|
|
|
|
return (argc);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/* el_parse():
|
|
|
|
* Command dispatcher
|
|
|
|
*/
|
|
|
|
public int
|
2002-03-18 19:00:50 +03:00
|
|
|
el_parse(EditLine *el, int argc, const char *argv[])
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2002-03-18 19:00:50 +03:00
|
|
|
const char *ptr;
|
2000-09-05 02:06:28 +04:00
|
|
|
int i;
|
1998-01-21 14:12:34 +03:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (argc < 1)
|
|
|
|
return (-1);
|
|
|
|
ptr = strchr(argv[0], ':');
|
|
|
|
if (ptr != NULL) {
|
|
|
|
char *tprog;
|
|
|
|
size_t l;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (ptr == argv[0])
|
|
|
|
return (0);
|
|
|
|
l = ptr - argv[0] - 1;
|
|
|
|
tprog = (char *) el_malloc(l + 1);
|
|
|
|
if (tprog == NULL)
|
|
|
|
return (0);
|
|
|
|
(void) strncpy(tprog, argv[0], l);
|
|
|
|
tprog[l] = '\0';
|
|
|
|
ptr++;
|
|
|
|
l = el_match(el->el_prog, tprog);
|
|
|
|
el_free(tprog);
|
|
|
|
if (!l)
|
|
|
|
return (0);
|
|
|
|
} else
|
|
|
|
ptr = argv[0];
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
for (i = 0; cmds[i].name != NULL; i++)
|
|
|
|
if (strcmp(cmds[i].name, ptr) == 0) {
|
|
|
|
i = (*cmds[i].func) (el, argc, argv);
|
|
|
|
return (-i);
|
|
|
|
}
|
|
|
|
return (-1);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* parse__escape():
|
|
|
|
* Parse a string of the form ^<char> \<odigit> \<char> and return
|
|
|
|
* the appropriate character or -1 if the escape is not valid
|
|
|
|
*/
|
|
|
|
protected int
|
2000-09-05 02:06:28 +04:00
|
|
|
parse__escape(const char **const ptr)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
const char *p;
|
|
|
|
int c;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
p = *ptr;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (p[1] == 0)
|
|
|
|
return (-1);
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (*p == '\\') {
|
|
|
|
p++;
|
|
|
|
switch (*p) {
|
|
|
|
case 'a':
|
|
|
|
c = '\007'; /* Bell */
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
c = '\010'; /* Backspace */
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
c = '\011'; /* Horizontal Tab */
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
c = '\012'; /* New Line */
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
c = '\013'; /* Vertical Tab */
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
c = '\014'; /* Form Feed */
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
c = '\015'; /* Carriage Return */
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
c = '\033'; /* Escape */
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
{
|
|
|
|
int cnt, ch;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
for (cnt = 0, c = 0; cnt < 3; cnt++) {
|
|
|
|
ch = *p++;
|
|
|
|
if (ch < '0' || ch > '7') {
|
|
|
|
p--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = (c << 3) | (ch - '0');
|
|
|
|
}
|
|
|
|
if ((c & 0xffffff00) != 0)
|
|
|
|
return (-1);
|
|
|
|
--p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
c = *p;
|
1994-05-06 10:01:42 +04:00
|
|
|
break;
|
|
|
|
}
|
2003-10-15 22:08:40 +04:00
|
|
|
} else if (*p == '^') {
|
2000-09-05 02:06:28 +04:00
|
|
|
p++;
|
|
|
|
c = (*p == '?') ? '\177' : (*p & 0237);
|
|
|
|
} else
|
|
|
|
c = *p;
|
|
|
|
*ptr = ++p;
|
|
|
|
return (c);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
2003-11-02 23:06:57 +03:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/* parse__string():
|
|
|
|
* Parse the escapes from in and put the raw string out
|
|
|
|
*/
|
|
|
|
protected char *
|
2000-09-05 02:06:28 +04:00
|
|
|
parse__string(char *out, const char *in)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
char *rv = out;
|
|
|
|
int n;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
for (;;)
|
|
|
|
switch (*in) {
|
|
|
|
case '\0':
|
|
|
|
*out = '\0';
|
|
|
|
return (rv);
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
case '\\':
|
|
|
|
case '^':
|
|
|
|
if ((n = parse__escape(&in)) == -1)
|
|
|
|
return (NULL);
|
|
|
|
*out++ = n;
|
|
|
|
break;
|
|
|
|
|
2003-11-02 23:06:57 +03:00
|
|
|
case 'M':
|
|
|
|
if (in[1] == '-' && in[2] != '\0') {
|
|
|
|
*out++ = '\033';
|
|
|
|
in += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
default:
|
|
|
|
*out++ = *in++;
|
|
|
|
break;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/* parse_cmd():
|
|
|
|
* Return the command number for the command string given
|
|
|
|
* or -1 if one is not found
|
|
|
|
*/
|
|
|
|
protected int
|
2000-09-05 02:06:28 +04:00
|
|
|
parse_cmd(EditLine *el, const char *cmd)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
el_bindings_t *b;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
for (b = el->el_map.help; b->name != NULL; b++)
|
|
|
|
if (strcmp(b->name, cmd) == 0)
|
|
|
|
return (b->func);
|
|
|
|
return (-1);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|