2017-01-30 22:11:53 +00:00
|
|
|
/* $NetBSD: parse.c,v 1.35 2016/02/17 19:47:49 christos Exp $ */
|
2015-01-09 07:40:56 +00:00
|
|
|
|
1994-05-27 05:00:24 +00: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.
|
2005-08-07 20:55:59 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 05:00:24 +00: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.
|
|
|
|
*/
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
#include "config.h"
|
1994-05-27 05:00:24 +00:00
|
|
|
#if !defined(lint) && !defined(SCCSID)
|
2015-01-09 07:40:56 +00:00
|
|
|
#if 0
|
1994-05-27 05:00:24 +00:00
|
|
|
static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/4/93";
|
2015-01-09 07:40:56 +00:00
|
|
|
#else
|
2017-01-30 22:11:53 +00:00
|
|
|
__RCSID("$NetBSD: parse.c,v 1.35 2016/02/17 19:47:49 christos Exp $");
|
2015-01-09 07:40:56 +00:00
|
|
|
#endif
|
1994-05-27 05:00:24 +00:00
|
|
|
#endif /* not lint && not SCCSID */
|
2001-10-01 08:41:27 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* parse.c: parse an editline extended command
|
|
|
|
*
|
|
|
|
* commands are:
|
|
|
|
*
|
|
|
|
* bind
|
|
|
|
* echotc
|
2001-10-01 08:41:27 +00:00
|
|
|
* edit
|
1994-05-27 05:00:24 +00:00
|
|
|
* gettc
|
1997-06-25 08:14:24 +00:00
|
|
|
* history
|
|
|
|
* settc
|
|
|
|
* setty
|
1994-05-27 05:00:24 +00:00
|
|
|
*/
|
2016-03-06 21:32:54 +00:00
|
|
|
#include <stdlib.h>
|
2017-01-30 22:11:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "el.h"
|
|
|
|
#include "parse.h"
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
private const struct {
|
2015-01-09 07:40:56 +00:00
|
|
|
const Char *name;
|
|
|
|
int (*func)(EditLine *, int, const Char **);
|
1994-05-27 05:00:24 +00:00
|
|
|
} cmds[] = {
|
2017-01-30 22:11:53 +00:00
|
|
|
{ STR("bind"), map_bind },
|
2015-01-09 07:40:56 +00:00
|
|
|
{ STR("echotc"), terminal_echotc },
|
2017-01-30 22:11:53 +00:00
|
|
|
{ STR("edit"), el_editmode },
|
2015-01-09 07:40:56 +00:00
|
|
|
{ STR("history"), hist_command },
|
|
|
|
{ STR("telltc"), terminal_telltc },
|
|
|
|
{ STR("settc"), terminal_settc },
|
|
|
|
{ STR("setty"), tty_stty },
|
|
|
|
{ NULL, NULL }
|
1994-05-27 05:00:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* parse_line():
|
|
|
|
* Parse a line and dispatch it
|
|
|
|
*/
|
|
|
|
protected int
|
2015-01-09 07:40:56 +00:00
|
|
|
parse_line(EditLine *el, const Char *line)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
const Char **argv;
|
2001-10-01 08:41:27 +00:00
|
|
|
int argc;
|
2015-01-09 07:40:56 +00:00
|
|
|
TYPE(Tokenizer) *tok;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
tok = FUN(tok,init)(NULL);
|
|
|
|
FUN(tok,str)(tok, line, &argc, &argv);
|
|
|
|
argc = FUN(el,parse)(el, argc, argv);
|
|
|
|
FUN(tok,end)(tok);
|
|
|
|
return argc;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* el_parse():
|
|
|
|
* Command dispatcher
|
|
|
|
*/
|
|
|
|
public int
|
2015-01-09 07:40:56 +00:00
|
|
|
FUN(el,parse)(EditLine *el, int argc, const Char *argv[])
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
const Char *ptr;
|
2001-10-01 08:41:27 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc < 1)
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
|
|
|
ptr = Strchr(argv[0], ':');
|
2001-10-01 08:41:27 +00:00
|
|
|
if (ptr != NULL) {
|
2015-01-09 07:40:56 +00:00
|
|
|
Char *tprog;
|
2001-10-01 08:41:27 +00:00
|
|
|
size_t l;
|
|
|
|
|
|
|
|
if (ptr == argv[0])
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
|
|
|
l = (size_t)(ptr - argv[0] - 1);
|
|
|
|
tprog = el_malloc((l + 1) * sizeof(*tprog));
|
2001-10-01 08:41:27 +00:00
|
|
|
if (tprog == NULL)
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
|
|
|
(void) Strncpy(tprog, argv[0], l);
|
2001-10-01 08:41:27 +00:00
|
|
|
tprog[l] = '\0';
|
|
|
|
ptr++;
|
2015-01-09 07:40:56 +00:00
|
|
|
l = (size_t)el_match(el->el_prog, tprog);
|
2001-10-01 08:41:27 +00:00
|
|
|
el_free(tprog);
|
|
|
|
if (!l)
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
} else
|
|
|
|
ptr = argv[0];
|
|
|
|
|
|
|
|
for (i = 0; cmds[i].name != NULL; i++)
|
2015-01-09 07:40:56 +00:00
|
|
|
if (Strcmp(cmds[i].name, ptr) == 0) {
|
2001-10-01 08:41:27 +00:00
|
|
|
i = (*cmds[i].func) (el, argc, argv);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -i;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* parse__escape():
|
2015-01-09 07:40:56 +00:00
|
|
|
* Parse a string of the form ^<char> \<odigit> \<char> \U+xxxx and return
|
1994-05-27 05:00:24 +00:00
|
|
|
* the appropriate character or -1 if the escape is not valid
|
|
|
|
*/
|
|
|
|
protected int
|
2015-01-09 07:40:56 +00:00
|
|
|
parse__escape(const Char **ptr)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
const Char *p;
|
2017-01-30 22:11:53 +00:00
|
|
|
wint_t c;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
p = *ptr;
|
|
|
|
|
|
|
|
if (p[1] == 0)
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00: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;
|
2015-01-09 07:40:56 +00:00
|
|
|
case 'U': /* Unicode \U+xxxx or \U+xxxxx format */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const Char hex[] = STR("0123456789ABCDEF");
|
|
|
|
const Char *h;
|
|
|
|
++p;
|
|
|
|
if (*p++ != '+')
|
|
|
|
return -1;
|
|
|
|
c = 0;
|
|
|
|
for (i = 0; i < 5; ++i) {
|
|
|
|
h = Strchr(hex, *p++);
|
|
|
|
if (!h && i < 4)
|
|
|
|
return -1;
|
|
|
|
else if (h)
|
|
|
|
c = (c << 4) | ((int)(h - hex));
|
|
|
|
else
|
|
|
|
--p;
|
|
|
|
}
|
|
|
|
if (c > 0x10FFFF) /* outside valid character range */
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
2001-10-01 08:41:27 +00:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
{
|
|
|
|
int cnt, ch;
|
|
|
|
|
|
|
|
for (cnt = 0, c = 0; cnt < 3; cnt++) {
|
|
|
|
ch = *p++;
|
|
|
|
if (ch < '0' || ch > '7') {
|
|
|
|
p--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = (c << 3) | (ch - '0');
|
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
if ((c & (wint_t)0xffffff00) != (wint_t)0)
|
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
--p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
c = *p;
|
1994-05-27 05:00:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-08-07 20:55:59 +00:00
|
|
|
} else if (*p == '^') {
|
2001-10-01 08:41:27 +00:00
|
|
|
p++;
|
|
|
|
c = (*p == '?') ? '\177' : (*p & 0237);
|
|
|
|
} else
|
|
|
|
c = *p;
|
|
|
|
*ptr = ++p;
|
2015-01-09 07:40:56 +00:00
|
|
|
return c;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
2005-08-07 20:55:59 +00:00
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* parse__string():
|
|
|
|
* Parse the escapes from in and put the raw string out
|
|
|
|
*/
|
2015-01-09 07:40:56 +00:00
|
|
|
protected Char *
|
|
|
|
parse__string(Char *out, const Char *in)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
Char *rv = out;
|
2001-10-01 08:41:27 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
switch (*in) {
|
|
|
|
case '\0':
|
|
|
|
*out = '\0';
|
2015-01-09 07:40:56 +00:00
|
|
|
return rv;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
case '\\':
|
|
|
|
case '^':
|
|
|
|
if ((n = parse__escape(&in)) == -1)
|
2015-01-09 07:40:56 +00:00
|
|
|
return NULL;
|
2017-01-30 22:11:53 +00:00
|
|
|
*out++ = (Char)n;
|
2001-10-01 08:41:27 +00:00
|
|
|
break;
|
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
case 'M':
|
|
|
|
if (in[1] == '-' && in[2] != '\0') {
|
|
|
|
*out++ = '\033';
|
|
|
|
in += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
default:
|
|
|
|
*out++ = *in++;
|
|
|
|
break;
|
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* parse_cmd():
|
|
|
|
* Return the command number for the command string given
|
|
|
|
* or -1 if one is not found
|
|
|
|
*/
|
|
|
|
protected int
|
2015-01-09 07:40:56 +00:00
|
|
|
parse_cmd(EditLine *el, const Char *cmd)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
el_bindings_t *b = el->el_map.help;
|
|
|
|
size_t i;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
for (i = 0; i < el->el_map.nfunc; i++)
|
|
|
|
if (Strcmp(b[i].name, cmd) == 0)
|
|
|
|
return b[i].func;
|
|
|
|
return -1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|