2013-06-04 00:28:17 +02:00
|
|
|
/* $OpenBSD: syn.c,v 1.29 2013/06/03 18:40:05 jca Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2012-01-03 16:32:08 +01:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
2015-03-08 23:54:36 +01:00
|
|
|
* 2011, 2012, 2013, 2014, 2015
|
2009-05-16 18:59:42 +02:00
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2015-04-29 22:07:35 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.101 2015/04/29 20:07:35 tg Exp $");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
struct nesting_state {
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
int start_token; /* token than began nesting (eg, FOR) */
|
|
|
|
int start_line; /* line nesting began on */
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
2012-10-30 21:07:15 +01:00
|
|
|
struct yyrecursive_state {
|
|
|
|
struct yyrecursive_state *next;
|
|
|
|
struct ioword **old_herep;
|
|
|
|
int old_symbol;
|
|
|
|
int old_salias;
|
|
|
|
int old_nesting_type;
|
|
|
|
bool old_reject;
|
|
|
|
};
|
|
|
|
|
2007-01-12 02:49:29 +01:00
|
|
|
static void yyparse(void);
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct op *pipeline(int);
|
|
|
|
static struct op *andor(void);
|
2011-03-06 02:25:35 +01:00
|
|
|
static struct op *c_list(bool);
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct ioword *synio(int);
|
|
|
|
static struct op *nested(int, int, int);
|
|
|
|
static struct op *get_command(int);
|
|
|
|
static struct op *dogroup(void);
|
|
|
|
static struct op *thenpart(void);
|
|
|
|
static struct op *elsepart(void);
|
|
|
|
static struct op *caselist(void);
|
|
|
|
static struct op *casepart(int);
|
2009-09-19 20:36:59 +02:00
|
|
|
static struct op *function_body(char *, bool);
|
2007-01-12 02:49:29 +01:00
|
|
|
static char **wordlist(void);
|
2013-04-26 20:27:07 +02:00
|
|
|
static struct op *block(int, struct op *, struct op *);
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct op *newtp(int);
|
2009-12-12 23:27:10 +01:00
|
|
|
static void syntaxerr(const char *) MKSH_A_NORETURN;
|
2007-01-12 02:49:29 +01:00
|
|
|
static void nesting_push(struct nesting_state *, int);
|
|
|
|
static void nesting_pop(struct nesting_state *);
|
2014-01-05 22:57:29 +01:00
|
|
|
static int inalias(struct source *) MKSH_A_PURE;
|
2010-07-18 00:09:40 +02:00
|
|
|
static Test_op dbtestp_isa(Test_env *, Test_meta);
|
2008-04-02 00:20:20 +02:00
|
|
|
static const char *dbtestp_getopnd(Test_env *, Test_op, bool);
|
2007-01-12 02:49:29 +01:00
|
|
|
static int dbtestp_eval(Test_env *, Test_op, const char *,
|
2008-04-02 00:20:20 +02:00
|
|
|
const char *, bool);
|
2009-12-12 23:27:10 +01:00
|
|
|
static void dbtestp_error(Test_env *, int, const char *) MKSH_A_NORETURN;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-01-12 02:49:29 +01:00
|
|
|
static struct op *outtree; /* yyparse output */
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct nesting_state nesting; /* \n changed to ; */
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
static bool reject; /* token(cf) gets symbol again */
|
|
|
|
static int symbol; /* yylex value */
|
2011-12-29 23:03:15 +01:00
|
|
|
static int sALIAS = ALIAS; /* 0 in yyrecursive */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
#define REJECT (reject = true)
|
|
|
|
#define ACCEPT (reject = false)
|
2009-04-07 21:06:44 +02:00
|
|
|
#define token(cf) ((reject) ? (ACCEPT, symbol) : (symbol = yylex(cf)))
|
|
|
|
#define tpeek(cf) ((reject) ? (symbol) : (REJECT, symbol = yylex(cf)))
|
2011-03-13 02:20:25 +01:00
|
|
|
#define musthave(c,cf) do { if (token(cf) != (c)) syntaxerr(NULL); } while (/* CONSTCOND */ 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-06-28 22:04:02 +02:00
|
|
|
static const char Tcbrace[] = "}";
|
|
|
|
static const char Tesac[] = "esac";
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
static void
|
|
|
|
yyparse(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
ACCEPT;
|
|
|
|
|
|
|
|
outtree = c_list(source->type == SSTRING);
|
|
|
|
c = tpeek(0);
|
|
|
|
if (c == 0 && !outtree)
|
|
|
|
outtree = newtp(TEOF);
|
|
|
|
else if (c != '\n' && c != 0)
|
|
|
|
syntaxerr(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
pipeline(int cf)
|
|
|
|
{
|
|
|
|
struct op *t, *p, *tl = NULL;
|
|
|
|
|
|
|
|
t = get_command(cf);
|
|
|
|
if (t != NULL) {
|
|
|
|
while (token(0) == '|') {
|
|
|
|
if ((p = get_command(CONTIN)) == NULL)
|
|
|
|
syntaxerr(NULL);
|
|
|
|
if (tl == NULL)
|
2013-04-26 20:27:07 +02:00
|
|
|
t = tl = block(TPIPE, t, p);
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2013-04-26 20:27:07 +02:00
|
|
|
tl = tl->right = block(TPIPE, tl->right, p);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
REJECT;
|
|
|
|
}
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
andor(void)
|
|
|
|
{
|
|
|
|
struct op *t, *p;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
t = pipeline(0);
|
|
|
|
if (t != NULL) {
|
|
|
|
while ((c = token(0)) == LOGAND || c == LOGOR) {
|
|
|
|
if ((p = pipeline(CONTIN)) == NULL)
|
|
|
|
syntaxerr(NULL);
|
2013-04-26 20:27:07 +02:00
|
|
|
t = block(c == LOGAND? TAND: TOR, t, p);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
REJECT;
|
|
|
|
}
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
2011-03-06 02:25:35 +01:00
|
|
|
c_list(bool multi)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct op *t = NULL, *p, *tl = NULL;
|
2011-03-06 02:25:35 +01:00
|
|
|
int c;
|
|
|
|
bool have_sep;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
p = andor();
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* Token has always been read/rejected at this point, so
|
2005-05-23 05:06:10 +02:00
|
|
|
* we don't worry about what flags to pass token()
|
|
|
|
*/
|
|
|
|
c = token(0);
|
2011-03-06 02:25:35 +01:00
|
|
|
have_sep = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '\n' && (multi || inalias(source))) {
|
2011-03-06 02:25:35 +01:00
|
|
|
if (!p)
|
|
|
|
/* ignore blank lines */
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
} else if (!p)
|
|
|
|
break;
|
|
|
|
else if (c == '&' || c == COPROC)
|
2013-04-26 20:27:07 +02:00
|
|
|
p = block(c == '&' ? TASYNC : TCOPROC, p, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (c != ';')
|
2011-03-06 02:25:35 +01:00
|
|
|
have_sep = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!t)
|
|
|
|
t = p;
|
|
|
|
else if (!tl)
|
2013-04-26 20:27:07 +02:00
|
|
|
t = tl = block(TLIST, t, p);
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2013-04-26 20:27:07 +02:00
|
|
|
tl = tl->right = block(TLIST, tl->right, p);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!have_sep)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
REJECT;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (t);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct ioword *
|
|
|
|
synio(int cf)
|
|
|
|
{
|
|
|
|
struct ioword *iop;
|
2011-04-22 14:15:42 +02:00
|
|
|
static struct ioword *nextiop;
|
2009-04-07 21:13:11 +02:00
|
|
|
bool ishere;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-06-29 00:51:56 +02:00
|
|
|
if (nextiop != NULL) {
|
|
|
|
iop = nextiop;
|
|
|
|
nextiop = NULL;
|
|
|
|
return (iop);
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (tpeek(cf) != REDIR)
|
2008-06-29 00:51:56 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
ACCEPT;
|
|
|
|
iop = yylval.iop;
|
2015-04-12 00:03:32 +02:00
|
|
|
if (iop->ioflag & IONDELIM)
|
2011-05-05 02:05:01 +02:00
|
|
|
goto gotnulldelim;
|
2015-04-12 00:03:32 +02:00
|
|
|
ishere = (iop->ioflag & IOTYPE) == IOHERE;
|
2005-05-23 05:06:10 +02:00
|
|
|
musthave(LWORD, ishere ? HEREDELIM : 0);
|
|
|
|
if (ishere) {
|
|
|
|
iop->delim = yylval.cp;
|
2014-12-15 23:08:55 +01:00
|
|
|
if (*ident != 0) {
|
2011-03-06 02:25:35 +01:00
|
|
|
/* unquoted */
|
2011-05-05 02:05:01 +02:00
|
|
|
gotnulldelim:
|
2015-04-12 00:03:32 +02:00
|
|
|
iop->ioflag |= IOEVAL;
|
2014-12-15 23:08:55 +01:00
|
|
|
}
|
2008-10-10 23:30:43 +02:00
|
|
|
if (herep > &heres[HERES - 1])
|
2010-08-28 20:50:58 +02:00
|
|
|
yyerror("too many %ss\n", "<<");
|
2005-05-23 05:06:10 +02:00
|
|
|
*herep++ = iop;
|
|
|
|
} else
|
|
|
|
iop->name = yylval.cp;
|
2008-06-29 00:51:56 +02:00
|
|
|
|
2015-04-12 00:03:32 +02:00
|
|
|
if (iop->ioflag & IOBASH) {
|
2008-12-02 14:20:40 +01:00
|
|
|
char *cp;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
nextiop = alloc(sizeof(*iop), ATEMP);
|
2008-12-13 18:02:18 +01:00
|
|
|
nextiop->name = cp = alloc(5, ATEMP);
|
2008-12-02 14:20:40 +01:00
|
|
|
|
|
|
|
if (iop->unit > 9) {
|
|
|
|
*cp++ = CHAR;
|
2015-04-29 22:07:35 +02:00
|
|
|
*cp++ = digits_lc[iop->unit / 10];
|
2008-12-02 14:20:40 +01:00
|
|
|
}
|
|
|
|
*cp++ = CHAR;
|
2015-04-29 22:07:35 +02:00
|
|
|
*cp++ = digits_lc[iop->unit % 10];
|
2008-12-02 14:20:40 +01:00
|
|
|
*cp = EOS;
|
2008-06-29 00:51:56 +02:00
|
|
|
|
2015-04-12 00:03:32 +02:00
|
|
|
iop->ioflag &= ~IOBASH;
|
2008-06-29 00:51:56 +02:00
|
|
|
nextiop->unit = 2;
|
2015-04-12 00:03:32 +02:00
|
|
|
nextiop->ioflag = IODUP;
|
2008-06-29 00:51:56 +02:00
|
|
|
nextiop->delim = NULL;
|
|
|
|
nextiop->heredoc = NULL;
|
|
|
|
}
|
|
|
|
return (iop);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
nested(int type, int smark, int emark)
|
|
|
|
{
|
|
|
|
struct op *t;
|
|
|
|
struct nesting_state old_nesting;
|
|
|
|
|
|
|
|
nesting_push(&old_nesting, smark);
|
|
|
|
t = c_list(true);
|
2011-12-29 23:03:15 +01:00
|
|
|
musthave(emark, KEYWORD|sALIAS);
|
2005-05-23 05:06:10 +02:00
|
|
|
nesting_pop(&old_nesting);
|
2013-04-26 20:27:07 +02:00
|
|
|
return (block(type, t, NULL));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-11 23:14:19 +01:00
|
|
|
static const char let_cmd[] = {
|
2015-03-08 23:54:36 +01:00
|
|
|
QCHAR, 'l', CHAR, 'e', CHAR, 't', CHAR, ']', EOS
|
2011-11-11 23:14:19 +01:00
|
|
|
};
|
|
|
|
static const char setA_cmd0[] = {
|
2015-03-08 23:54:36 +01:00
|
|
|
QCHAR, 's', CHAR, 'e', CHAR, 't', EOS
|
2011-11-11 23:14:19 +01:00
|
|
|
};
|
|
|
|
static const char setA_cmd1[] = {
|
|
|
|
CHAR, '-', CHAR, 'A', EOS
|
|
|
|
};
|
|
|
|
static const char setA_cmd2[] = {
|
|
|
|
CHAR, '-', CHAR, '-', EOS
|
|
|
|
};
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct op *
|
|
|
|
get_command(int cf)
|
|
|
|
{
|
|
|
|
struct op *t;
|
2011-11-11 23:14:19 +01:00
|
|
|
int c, iopn = 0, syniocf, lno;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct ioword *iop, **iops;
|
|
|
|
XPtrV args, vars;
|
2011-11-11 23:14:19 +01:00
|
|
|
char *tcp;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct nesting_state old_nesting;
|
|
|
|
|
2010-09-14 23:26:19 +02:00
|
|
|
/* NUFILE is small enough to leave this addition unchecked */
|
|
|
|
iops = alloc2((NUFILE + 1), sizeof(struct ioword *), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
XPinit(args, 16);
|
|
|
|
XPinit(vars, 16);
|
|
|
|
|
2011-12-29 23:03:15 +01:00
|
|
|
syniocf = KEYWORD|sALIAS;
|
|
|
|
switch (c = token(cf|KEYWORD|sALIAS|VARASN)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
|
|
|
REJECT;
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(iops, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
XPfree(args);
|
|
|
|
XPfree(vars);
|
2011-03-06 02:25:35 +01:00
|
|
|
/* empty line */
|
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case LWORD:
|
|
|
|
case REDIR:
|
|
|
|
REJECT;
|
2011-12-29 23:03:15 +01:00
|
|
|
syniocf &= ~(KEYWORD|sALIAS);
|
2005-05-23 05:06:10 +02:00
|
|
|
t = newtp(TCOM);
|
|
|
|
t->lineno = source->line;
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
cf = (t->u.evalflags ? ARRAYVAR : 0) |
|
2015-03-14 05:37:55 +01:00
|
|
|
(XPsize(args) == 0 ? sALIAS|VARASN : 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (tpeek(cf)) {
|
|
|
|
case REDIR:
|
2008-06-29 00:51:56 +02:00
|
|
|
while ((iop = synio(cf)) != NULL) {
|
|
|
|
if (iopn >= NUFILE)
|
2010-08-28 20:50:58 +02:00
|
|
|
yyerror("too many %ss\n",
|
|
|
|
"redirection");
|
2008-06-29 00:51:56 +02:00
|
|
|
iops[iopn++] = iop;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWORD:
|
|
|
|
ACCEPT;
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* the iopn == 0 and XPsize(vars) == 0 are
|
2009-06-11 14:42:21 +02:00
|
|
|
* dubious but AT&T ksh acts this way
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (iopn == 0 && XPsize(vars) == 0 &&
|
|
|
|
XPsize(args) == 0 &&
|
|
|
|
assign_command(ident))
|
|
|
|
t->u.evalflags = DOVACHECK;
|
|
|
|
if ((XPsize(args) == 0 || Flag(FKEYWORD)) &&
|
|
|
|
is_wdvarassign(yylval.cp))
|
|
|
|
XPput(vars, yylval.cp);
|
|
|
|
else
|
|
|
|
XPput(args, yylval.cp);
|
|
|
|
break;
|
|
|
|
|
2011-11-11 23:14:19 +01:00
|
|
|
case '(' /*)*/:
|
|
|
|
if (XPsize(args) == 0 && XPsize(vars) == 1 &&
|
|
|
|
is_wdvarassign(yylval.cp)) {
|
|
|
|
/* wdarrassign: foo=(bar) */
|
2005-05-23 05:06:10 +02:00
|
|
|
ACCEPT;
|
2011-06-04 18:11:20 +02:00
|
|
|
|
2011-11-11 23:14:19 +01:00
|
|
|
/* manipulate the vars string */
|
2012-10-03 17:50:32 +02:00
|
|
|
tcp = XPptrv(vars)[(vars.len = 0)];
|
2011-11-11 23:14:19 +01:00
|
|
|
/* 'varname=' -> 'varname' */
|
|
|
|
tcp[wdscan(tcp, EOS) - tcp - 3] = EOS;
|
|
|
|
|
|
|
|
/* construct new args strings */
|
|
|
|
XPput(args, wdcopy(setA_cmd0, ATEMP));
|
|
|
|
XPput(args, wdcopy(setA_cmd1, ATEMP));
|
|
|
|
XPput(args, tcp);
|
|
|
|
XPput(args, wdcopy(setA_cmd2, ATEMP));
|
|
|
|
|
|
|
|
/* slurp in words till closing paren */
|
|
|
|
while (token(CONTIN) == LWORD)
|
|
|
|
XPput(args, yylval.cp);
|
|
|
|
if (symbol != /*(*/ ')')
|
|
|
|
syntaxerr(NULL);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Check for "> foo (echo hi)"
|
|
|
|
* which AT&T ksh allows (not
|
|
|
|
* POSIX, but not disallowed)
|
|
|
|
*/
|
|
|
|
afree(t, ATEMP);
|
|
|
|
if (XPsize(args) == 0 &&
|
|
|
|
XPsize(vars) == 0) {
|
|
|
|
ACCEPT;
|
|
|
|
goto Subshell;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* must be a function */
|
|
|
|
if (iopn != 0 || XPsize(args) != 1 ||
|
|
|
|
XPsize(vars) != 0)
|
|
|
|
syntaxerr(NULL);
|
|
|
|
ACCEPT;
|
|
|
|
musthave(/*(*/')', 0);
|
|
|
|
t = function_body(XPptrv(args)[0], false);
|
|
|
|
}
|
2011-06-04 18:11:20 +02:00
|
|
|
goto Leave;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
goto Leave;
|
|
|
|
}
|
|
|
|
}
|
2006-08-01 15:43:28 +02:00
|
|
|
Leave:
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2012-10-22 22:19:18 +02:00
|
|
|
case '(': /*)*/ {
|
|
|
|
int subshell_nesting_type_saved;
|
2009-10-04 14:45:23 +02:00
|
|
|
Subshell:
|
2012-10-22 22:19:18 +02:00
|
|
|
subshell_nesting_type_saved = subshell_nesting_type;
|
|
|
|
subshell_nesting_type = ')';
|
2005-05-23 05:06:10 +02:00
|
|
|
t = nested(TPAREN, '(', ')');
|
2012-10-22 22:19:18 +02:00
|
|
|
subshell_nesting_type = subshell_nesting_type_saved;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2012-10-22 22:19:18 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case '{': /*}*/
|
|
|
|
t = nested(TBRACE, '{', '}');
|
|
|
|
break;
|
|
|
|
|
2011-11-11 23:14:19 +01:00
|
|
|
case MDPAREN:
|
2011-03-06 02:25:35 +01:00
|
|
|
/* leave KEYWORD in syniocf (allow if (( 1 )) then ...) */
|
2009-10-04 14:45:23 +02:00
|
|
|
lno = source->line;
|
2005-05-23 05:06:10 +02:00
|
|
|
ACCEPT;
|
2009-10-04 14:45:23 +02:00
|
|
|
switch (token(LETEXPR)) {
|
|
|
|
case LWORD:
|
|
|
|
break;
|
2011-03-06 02:25:35 +01:00
|
|
|
case '(': /*)*/
|
2009-10-04 14:45:23 +02:00
|
|
|
goto Subshell;
|
|
|
|
default:
|
|
|
|
syntaxerr(NULL);
|
|
|
|
}
|
|
|
|
t = newtp(TCOM);
|
|
|
|
t->lineno = lno;
|
2005-05-23 05:06:10 +02:00
|
|
|
XPput(args, wdcopy(let_cmd, ATEMP));
|
|
|
|
XPput(args, yylval.cp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DBRACKET: /* [[ .. ]] */
|
2011-03-06 02:25:35 +01:00
|
|
|
/* leave KEYWORD in syniocf (allow if [[ -n 1 ]] then ...) */
|
2005-05-23 05:06:10 +02:00
|
|
|
t = newtp(TDBRACKET);
|
|
|
|
ACCEPT;
|
|
|
|
{
|
|
|
|
Test_env te;
|
|
|
|
|
|
|
|
te.flags = TEF_DBRACKET;
|
|
|
|
te.pos.av = &args;
|
|
|
|
te.isa = dbtestp_isa;
|
|
|
|
te.getopnd = dbtestp_getopnd;
|
|
|
|
te.eval = dbtestp_eval;
|
|
|
|
te.error = dbtestp_error;
|
|
|
|
|
|
|
|
test_parse(&te);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FOR:
|
|
|
|
case SELECT:
|
|
|
|
t = newtp((c == FOR) ? TFOR : TSELECT);
|
|
|
|
musthave(LWORD, ARRAYVAR);
|
|
|
|
if (!is_wdvarname(yylval.cp, true))
|
2011-09-07 17:24:22 +02:00
|
|
|
yyerror("%s: %s\n", c == FOR ? "for" : Tselect,
|
2010-08-28 20:50:58 +02:00
|
|
|
"bad identifier");
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(t->str, ident, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
nesting_push(&old_nesting, c);
|
|
|
|
t->vars = wordlist();
|
|
|
|
t->left = dogroup();
|
|
|
|
nesting_pop(&old_nesting);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WHILE:
|
|
|
|
case UNTIL:
|
|
|
|
nesting_push(&old_nesting, c);
|
|
|
|
t = newtp((c == WHILE) ? TWHILE : TUNTIL);
|
|
|
|
t->left = c_list(true);
|
|
|
|
t->right = dogroup();
|
|
|
|
nesting_pop(&old_nesting);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CASE:
|
|
|
|
t = newtp(TCASE);
|
|
|
|
musthave(LWORD, 0);
|
|
|
|
t->str = yylval.cp;
|
|
|
|
nesting_push(&old_nesting, c);
|
|
|
|
t->left = caselist();
|
|
|
|
nesting_pop(&old_nesting);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IF:
|
|
|
|
nesting_push(&old_nesting, c);
|
|
|
|
t = newtp(TIF);
|
|
|
|
t->left = c_list(true);
|
|
|
|
t->right = thenpart();
|
2011-12-29 23:03:15 +01:00
|
|
|
musthave(FI, KEYWORD|sALIAS);
|
2005-05-23 05:06:10 +02:00
|
|
|
nesting_pop(&old_nesting);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BANG:
|
2011-12-29 23:03:15 +01:00
|
|
|
syniocf &= ~(KEYWORD|sALIAS);
|
2005-05-23 05:06:10 +02:00
|
|
|
t = pipeline(0);
|
|
|
|
if (t == NULL)
|
|
|
|
syntaxerr(NULL);
|
2013-04-26 20:27:07 +02:00
|
|
|
t = block(TBANG, NULL, t);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TIME:
|
2011-12-29 23:03:15 +01:00
|
|
|
syniocf &= ~(KEYWORD|sALIAS);
|
2005-05-23 05:06:10 +02:00
|
|
|
t = pipeline(0);
|
2012-01-03 16:32:08 +01:00
|
|
|
if (t && t->type == TCOM) {
|
2008-12-13 18:02:18 +01:00
|
|
|
t->str = alloc(2, ATEMP);
|
2011-03-06 02:25:35 +01:00
|
|
|
/* TF_* flags */
|
|
|
|
t->str[0] = '\0';
|
2008-07-14 14:29:06 +02:00
|
|
|
t->str[1] = '\0';
|
|
|
|
}
|
2013-04-26 20:27:07 +02:00
|
|
|
t = block(TTIME, t, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FUNCTION:
|
|
|
|
musthave(LWORD, 0);
|
|
|
|
t = function_body(yylval.cp, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((iop = synio(syniocf)) != NULL) {
|
|
|
|
if (iopn >= NUFILE)
|
2010-08-28 20:50:58 +02:00
|
|
|
yyerror("too many %ss\n", "redirection");
|
2005-05-23 05:06:10 +02:00
|
|
|
iops[iopn++] = iop;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iopn == 0) {
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(iops, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
t->ioact = NULL;
|
|
|
|
} else {
|
|
|
|
iops[iopn++] = NULL;
|
2010-09-14 23:26:19 +02:00
|
|
|
iops = aresize2(iops, iopn, sizeof(struct ioword *), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
t->ioact = iops;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t->type == TCOM || t->type == TDBRACKET) {
|
|
|
|
XPput(args, NULL);
|
2007-03-04 01:13:17 +01:00
|
|
|
t->args = (const char **)XPclose(args);
|
2005-05-23 05:06:10 +02:00
|
|
|
XPput(vars, NULL);
|
2013-04-26 20:27:07 +02:00
|
|
|
t->vars = (char **)XPclose(vars);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
XPfree(args);
|
|
|
|
XPfree(vars);
|
|
|
|
}
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (t);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
dogroup(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
struct op *list;
|
|
|
|
|
2011-12-29 23:03:15 +01:00
|
|
|
c = token(CONTIN|KEYWORD|sALIAS);
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* A {...} can be used instead of do...done for for/select loops
|
2005-05-23 05:06:10 +02:00
|
|
|
* but not for while/until loops - we don't need to check if it
|
|
|
|
* is a while loop because it would have been parsed as part of
|
|
|
|
* the conditional command list...
|
|
|
|
*/
|
|
|
|
if (c == DO)
|
|
|
|
c = DONE;
|
|
|
|
else if (c == '{')
|
|
|
|
c = '}';
|
|
|
|
else
|
|
|
|
syntaxerr(NULL);
|
|
|
|
list = c_list(true);
|
2011-12-29 23:03:15 +01:00
|
|
|
musthave(c, KEYWORD|sALIAS);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (list);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
thenpart(void)
|
|
|
|
{
|
|
|
|
struct op *t;
|
|
|
|
|
2011-12-29 23:03:15 +01:00
|
|
|
musthave(THEN, KEYWORD|sALIAS);
|
2005-05-23 05:06:10 +02:00
|
|
|
t = newtp(0);
|
|
|
|
t->left = c_list(true);
|
|
|
|
if (t->left == NULL)
|
|
|
|
syntaxerr(NULL);
|
|
|
|
t->right = elsepart();
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
elsepart(void)
|
|
|
|
{
|
|
|
|
struct op *t;
|
|
|
|
|
2011-12-29 23:03:15 +01:00
|
|
|
switch (token(KEYWORD|sALIAS|VARASN)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
case ELSE:
|
|
|
|
if ((t = c_list(true)) == NULL)
|
|
|
|
syntaxerr(NULL);
|
|
|
|
return (t);
|
|
|
|
|
|
|
|
case ELIF:
|
|
|
|
t = newtp(TELIF);
|
|
|
|
t->left = c_list(true);
|
|
|
|
t->right = thenpart();
|
|
|
|
return (t);
|
|
|
|
|
|
|
|
default:
|
|
|
|
REJECT;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
caselist(void)
|
|
|
|
{
|
|
|
|
struct op *t, *tl;
|
|
|
|
int c;
|
|
|
|
|
2011-12-29 23:03:15 +01:00
|
|
|
c = token(CONTIN|KEYWORD|sALIAS);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* A {...} can be used instead of in...esac for case statements */
|
|
|
|
if (c == IN)
|
|
|
|
c = ESAC;
|
|
|
|
else if (c == '{')
|
|
|
|
c = '}';
|
|
|
|
else
|
|
|
|
syntaxerr(NULL);
|
|
|
|
t = tl = NULL;
|
2011-03-06 02:25:35 +01:00
|
|
|
/* no ALIAS here */
|
|
|
|
while ((tpeek(CONTIN|KEYWORD|ESACONLY)) != c) {
|
2005-05-23 05:06:10 +02:00
|
|
|
struct op *tc = casepart(c);
|
|
|
|
if (tl == NULL)
|
|
|
|
t = tl = tc, tl->right = NULL;
|
|
|
|
else
|
|
|
|
tl->right = tc, tl = tc;
|
|
|
|
}
|
2011-12-29 23:03:15 +01:00
|
|
|
musthave(c, KEYWORD|sALIAS);
|
2005-05-23 05:06:10 +02:00
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
casepart(int endtok)
|
|
|
|
{
|
|
|
|
struct op *t;
|
|
|
|
XPtrV ptns;
|
|
|
|
|
|
|
|
XPinit(ptns, 16);
|
|
|
|
t = newtp(TPAT);
|
2008-11-09 20:48:02 +01:00
|
|
|
/* no ALIAS here */
|
|
|
|
if (token(CONTIN | KEYWORD) != '(')
|
2005-05-23 05:06:10 +02:00
|
|
|
REJECT;
|
|
|
|
do {
|
2012-06-28 22:04:02 +02:00
|
|
|
switch (token(0)) {
|
|
|
|
case LWORD:
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
case ESAC:
|
|
|
|
if (symbol != endtok) {
|
|
|
|
strdupx(yylval.cp,
|
|
|
|
symbol == '}' ? Tcbrace : Tesac, ATEMP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
syntaxerr(NULL);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
XPput(ptns, yylval.cp);
|
2008-11-09 20:48:02 +01:00
|
|
|
} while (token(0) == '|');
|
2005-05-23 05:06:10 +02:00
|
|
|
REJECT;
|
|
|
|
XPput(ptns, NULL);
|
2013-04-26 20:27:07 +02:00
|
|
|
t->vars = (char **)XPclose(ptns);
|
2005-05-23 05:06:10 +02:00
|
|
|
musthave(')', 0);
|
|
|
|
|
|
|
|
t->left = c_list(true);
|
2011-11-22 19:01:41 +01:00
|
|
|
|
|
|
|
/* initialise to default for ;; or omitted */
|
|
|
|
t->u.charflag = ';';
|
|
|
|
/* SUSv4 requires the ;; except in the last casepart */
|
2011-12-29 23:03:15 +01:00
|
|
|
if ((tpeek(CONTIN|KEYWORD|sALIAS)) != endtok)
|
2011-05-29 04:18:57 +02:00
|
|
|
switch (symbol) {
|
|
|
|
default:
|
|
|
|
syntaxerr(NULL);
|
|
|
|
case BRKEV:
|
2011-11-22 19:01:41 +01:00
|
|
|
t->u.charflag = '|';
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
2011-05-29 04:18:57 +02:00
|
|
|
case BRKFT:
|
2011-11-22 19:01:41 +01:00
|
|
|
t->u.charflag = '&';
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case BREAK:
|
|
|
|
/* initialised above, but we need to eat the token */
|
2011-05-29 04:18:57 +02:00
|
|
|
ACCEPT;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
function_body(char *name,
|
2011-03-06 02:25:35 +01:00
|
|
|
/* function foo { ... } vs foo() { .. } */
|
|
|
|
bool ksh_func)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
char *sname, *p;
|
|
|
|
struct op *t;
|
|
|
|
|
2011-05-03 00:52:54 +02:00
|
|
|
sname = wdstrip(name, 0);
|
2011-03-06 02:25:35 +01:00
|
|
|
/*-
|
|
|
|
* Check for valid characters in name. POSIX and AT&T ksh93 say
|
|
|
|
* only allow [a-zA-Z_0-9] but this allows more as old pdkshs
|
|
|
|
* have allowed more; the following were never allowed:
|
2009-10-30 01:57:39 +01:00
|
|
|
* NUL TAB NL SP " $ & ' ( ) ; < = > \ ` |
|
|
|
|
* C_QUOTE covers all but adds # * ? [ ]
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
for (p = sname; *p; p++)
|
2009-10-30 01:57:39 +01:00
|
|
|
if (ctype(*p, C_QUOTE))
|
2010-08-28 20:50:58 +02:00
|
|
|
yyerror("%s: %s\n", sname, "invalid function name");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* Note that POSIX allows only compound statements after foo(),
|
|
|
|
* sh and AT&T ksh allow any command, go with the later since it
|
|
|
|
* shouldn't break anything. However, for function foo, AT&T ksh
|
|
|
|
* only accepts an open-brace.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (ksh_func) {
|
2011-12-29 23:03:15 +01:00
|
|
|
if (tpeek(CONTIN|KEYWORD|sALIAS) == '(' /*)*/) {
|
2012-07-30 19:04:31 +02:00
|
|
|
/* function foo () { //}*/
|
2009-09-19 20:36:59 +02:00
|
|
|
ACCEPT;
|
|
|
|
musthave(')', 0);
|
|
|
|
/* degrade to POSIX function */
|
|
|
|
ksh_func = false;
|
|
|
|
}
|
2011-12-29 23:03:15 +01:00
|
|
|
musthave('{' /*}*/, CONTIN|KEYWORD|sALIAS);
|
2005-05-23 05:06:10 +02:00
|
|
|
REJECT;
|
|
|
|
}
|
|
|
|
|
2009-09-19 20:36:59 +02:00
|
|
|
t = newtp(TFUNCT);
|
|
|
|
t->str = sname;
|
2011-04-09 17:14:55 +02:00
|
|
|
t->u.ksh_func = tobool(ksh_func);
|
2009-09-19 20:36:59 +02:00
|
|
|
t->lineno = source->line;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((t->left = get_command(CONTIN)) == NULL) {
|
2007-03-04 01:13:17 +01:00
|
|
|
char *tv;
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
2010-09-14 23:26:19 +02:00
|
|
|
* Probably something like foo() followed by EOF or ';'.
|
2005-05-23 05:06:10 +02:00
|
|
|
* This is accepted by sh and ksh88.
|
|
|
|
* To make "typeset -f foo" work reliably (so its output can
|
|
|
|
* be used as input), we pretend there is a colon here.
|
|
|
|
*/
|
|
|
|
t->left = newtp(TCOM);
|
2010-09-14 23:26:19 +02:00
|
|
|
/* (2 * sizeof(char *)) is small enough */
|
2009-06-08 22:06:50 +02:00
|
|
|
t->left->args = alloc(2 * sizeof(char *), ATEMP);
|
2008-12-13 18:02:18 +01:00
|
|
|
t->left->args[0] = tv = alloc(3, ATEMP);
|
2015-03-14 05:38:13 +01:00
|
|
|
tv[0] = QCHAR;
|
2007-03-04 01:13:17 +01:00
|
|
|
tv[1] = ':';
|
|
|
|
tv[2] = EOS;
|
2005-05-23 05:06:10 +02:00
|
|
|
t->left->args[1] = NULL;
|
2009-06-08 22:06:50 +02:00
|
|
|
t->left->vars = alloc(sizeof(char *), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
t->left->vars[0] = NULL;
|
|
|
|
t->left->lineno = 1;
|
|
|
|
}
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (t);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
|
|
|
wordlist(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
XPtrV args;
|
|
|
|
|
|
|
|
XPinit(args, 16);
|
2009-10-02 20:08:37 +02:00
|
|
|
/* POSIX does not do alias expansion here... */
|
2011-12-29 23:03:15 +01:00
|
|
|
if ((c = token(CONTIN|KEYWORD|sALIAS)) != IN) {
|
2011-03-06 02:25:35 +01:00
|
|
|
if (c != ';')
|
|
|
|
/* non-POSIX, but AT&T ksh accepts a ; here */
|
2005-05-23 05:06:10 +02:00
|
|
|
REJECT;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
while ((c = token(0)) == LWORD)
|
|
|
|
XPput(args, yylval.cp);
|
|
|
|
if (c != '\n' && c != ';')
|
|
|
|
syntaxerr(NULL);
|
2013-06-04 00:28:17 +02:00
|
|
|
XPput(args, NULL);
|
|
|
|
return ((char **)XPclose(args));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* supporting functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct op *
|
2013-04-26 20:27:07 +02:00
|
|
|
block(int type, struct op *t1, struct op *t2)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct op *t;
|
|
|
|
|
|
|
|
t = newtp(type);
|
|
|
|
t->left = t1;
|
|
|
|
t->right = t2;
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
2012-12-05 19:54:10 +01:00
|
|
|
static const struct tokeninfo {
|
2005-05-23 05:06:10 +02:00
|
|
|
const char *name;
|
2009-08-28 20:54:01 +02:00
|
|
|
short val;
|
|
|
|
short reserved;
|
2005-05-23 05:06:10 +02:00
|
|
|
} tokentab[] = {
|
|
|
|
/* Reserved words */
|
|
|
|
{ "if", IF, true },
|
|
|
|
{ "then", THEN, true },
|
|
|
|
{ "else", ELSE, true },
|
|
|
|
{ "elif", ELIF, true },
|
|
|
|
{ "fi", FI, true },
|
|
|
|
{ "case", CASE, true },
|
2012-06-28 22:04:02 +02:00
|
|
|
{ Tesac, ESAC, true },
|
2005-05-23 05:06:10 +02:00
|
|
|
{ "for", FOR, true },
|
2011-09-07 17:24:22 +02:00
|
|
|
{ Tselect, SELECT, true },
|
2005-05-23 05:06:10 +02:00
|
|
|
{ "while", WHILE, true },
|
|
|
|
{ "until", UNTIL, true },
|
|
|
|
{ "do", DO, true },
|
|
|
|
{ "done", DONE, true },
|
|
|
|
{ "in", IN, true },
|
2011-09-07 17:24:22 +02:00
|
|
|
{ Tfunction, FUNCTION, true },
|
2005-05-23 05:06:10 +02:00
|
|
|
{ "time", TIME, true },
|
|
|
|
{ "{", '{', true },
|
2012-06-28 22:04:02 +02:00
|
|
|
{ Tcbrace, '}', true },
|
2005-05-23 05:06:10 +02:00
|
|
|
{ "!", BANG, true },
|
|
|
|
{ "[[", DBRACKET, true },
|
|
|
|
/* Lexical tokens (0[EOF], LWORD and REDIR handled specially) */
|
|
|
|
{ "&&", LOGAND, false },
|
|
|
|
{ "||", LOGOR, false },
|
|
|
|
{ ";;", BREAK, false },
|
2011-05-29 04:18:57 +02:00
|
|
|
{ ";|", BRKEV, false },
|
|
|
|
{ ";&", BRKFT, false },
|
2005-05-23 05:06:10 +02:00
|
|
|
{ "((", MDPAREN, false },
|
|
|
|
{ "|&", COPROC, false },
|
|
|
|
/* and some special cases... */
|
|
|
|
{ "newline", '\n', false },
|
|
|
|
{ NULL, 0, false }
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
initkeywords(void)
|
|
|
|
{
|
|
|
|
struct tokeninfo const *tt;
|
|
|
|
struct tbl *p;
|
|
|
|
|
2011-06-05 21:58:21 +02:00
|
|
|
ktinit(APERM, &keywords,
|
2012-07-01 17:38:09 +02:00
|
|
|
/* currently 28 keywords: 75% of 64 = 2^6 */
|
|
|
|
6);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (tt = tokentab; tt->name; tt++) {
|
|
|
|
if (tt->reserved) {
|
2009-08-28 22:30:59 +02:00
|
|
|
p = ktenter(&keywords, tt->name, hash(tt->name));
|
2005-05-23 05:06:10 +02:00
|
|
|
p->flag |= DEFINED|ISSET;
|
|
|
|
p->type = CKEYWD;
|
|
|
|
p->val.i = tt->val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
syntaxerr(const char *what)
|
|
|
|
{
|
2011-03-06 02:25:35 +01:00
|
|
|
/* 2<<- is the longest redirection, I think */
|
|
|
|
char redir[6];
|
2005-05-23 05:06:10 +02:00
|
|
|
const char *s;
|
|
|
|
struct tokeninfo const *tt;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (!what)
|
|
|
|
what = "unexpected";
|
|
|
|
REJECT;
|
|
|
|
c = token(0);
|
2006-08-01 15:43:28 +02:00
|
|
|
Again:
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (c) {
|
|
|
|
case 0:
|
|
|
|
if (nesting.start_token) {
|
|
|
|
c = nesting.start_token;
|
|
|
|
source->errline = nesting.start_line;
|
|
|
|
what = "unmatched";
|
|
|
|
goto Again;
|
|
|
|
}
|
|
|
|
/* don't quote the EOF */
|
2011-09-07 17:24:22 +02:00
|
|
|
yyerror("%s: %s %s\n", Tsynerr, "unexpected", "EOF");
|
2006-05-10 20:54:13 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case LWORD:
|
|
|
|
s = snptreef(NULL, 32, "%S", yylval.cp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REDIR:
|
|
|
|
s = snptreef(redir, sizeof(redir), "%R", yylval.iop);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
for (tt = tokentab; tt->name; tt++)
|
|
|
|
if (tt->val == c)
|
|
|
|
break;
|
|
|
|
if (tt->name)
|
|
|
|
s = tt->name;
|
|
|
|
else {
|
|
|
|
if (c > 0 && c < 256) {
|
|
|
|
redir[0] = c;
|
|
|
|
redir[1] = '\0';
|
|
|
|
} else
|
|
|
|
shf_snprintf(redir, sizeof(redir),
|
|
|
|
"?%d", c);
|
|
|
|
s = redir;
|
|
|
|
}
|
|
|
|
}
|
2011-09-07 17:24:22 +02:00
|
|
|
yyerror("%s: '%s' %s\n", Tsynerr, s, what);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nesting_push(struct nesting_state *save, int tok)
|
|
|
|
{
|
|
|
|
*save = nesting;
|
|
|
|
nesting.start_token = tok;
|
|
|
|
nesting.start_line = source->line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nesting_pop(struct nesting_state *saved)
|
|
|
|
{
|
|
|
|
nesting = *saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *
|
|
|
|
newtp(int type)
|
|
|
|
{
|
|
|
|
struct op *t;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
t = alloc(sizeof(struct op), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
t->type = type;
|
|
|
|
t->u.evalflags = 0;
|
2007-03-04 01:13:17 +01:00
|
|
|
t->args = NULL;
|
|
|
|
t->vars = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
t->ioact = NULL;
|
|
|
|
t->left = t->right = NULL;
|
|
|
|
t->str = NULL;
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct op *
|
2011-03-13 17:03:54 +01:00
|
|
|
compile(Source *s, bool skiputf8bom)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
nesting.start_token = 0;
|
|
|
|
nesting.start_line = 0;
|
|
|
|
herep = heres;
|
|
|
|
source = s;
|
2011-03-13 17:03:54 +01:00
|
|
|
if (skiputf8bom)
|
|
|
|
yyskiputf8bom();
|
2005-05-23 05:06:10 +02:00
|
|
|
yyparse();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (outtree);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/*-
|
|
|
|
* This kludge exists to take care of sh/AT&T ksh oddity in which
|
2005-05-23 05:06:10 +02:00
|
|
|
* the arguments of alias/export/readonly/typeset have no field
|
|
|
|
* splitting, file globbing, or (normal) tilde expansion done.
|
2009-06-11 14:42:21 +02:00
|
|
|
* AT&T ksh seems to do something similar to this since
|
2005-05-23 05:06:10 +02:00
|
|
|
* $ touch a=a; typeset a=[ab]; echo "$a"
|
|
|
|
* a=[ab]
|
|
|
|
* $ x=typeset; $x a=[ab]; echo "$a"
|
|
|
|
* a=a
|
|
|
|
* $
|
|
|
|
*/
|
2015-03-14 06:23:18 +01:00
|
|
|
int
|
2012-05-10 01:21:00 +02:00
|
|
|
assign_command(const char *s)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2005-07-04 14:27:28 +02:00
|
|
|
if (!*s)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2011-09-07 17:24:22 +02:00
|
|
|
return ((strcmp(s, Talias) == 0) ||
|
2012-05-10 01:21:00 +02:00
|
|
|
(strcmp(s, Texport) == 0) ||
|
|
|
|
(strcmp(s, Treadonly) == 0) ||
|
2011-09-07 17:24:22 +02:00
|
|
|
(strcmp(s, Ttypeset) == 0));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we are in the middle of reading an alias */
|
|
|
|
static int
|
|
|
|
inalias(struct source *s)
|
|
|
|
{
|
|
|
|
for (; s && s->type == SALIAS; s = s->next)
|
|
|
|
if (!(s->flags & SF_ALIASEND))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* Order important - indexed by Test_meta values
|
2005-05-23 05:06:10 +02:00
|
|
|
* Note that ||, &&, ( and ) can't appear in as unquoted strings
|
|
|
|
* in normal shell input, so these can be interpreted unambiguously
|
|
|
|
* in the evaluation pass.
|
|
|
|
*/
|
|
|
|
static const char dbtest_or[] = { CHAR, '|', CHAR, '|', EOS };
|
|
|
|
static const char dbtest_and[] = { CHAR, '&', CHAR, '&', EOS };
|
|
|
|
static const char dbtest_not[] = { CHAR, '!', EOS };
|
|
|
|
static const char dbtest_oparen[] = { CHAR, '(', EOS };
|
|
|
|
static const char dbtest_cparen[] = { CHAR, ')', EOS };
|
2012-12-28 03:28:39 +01:00
|
|
|
const char * const dbtest_tokens[] = {
|
2005-05-23 05:06:10 +02:00
|
|
|
dbtest_or, dbtest_and, dbtest_not,
|
|
|
|
dbtest_oparen, dbtest_cparen
|
|
|
|
};
|
2012-12-05 19:54:10 +01:00
|
|
|
static const char db_close[] = { CHAR, ']', CHAR, ']', EOS };
|
|
|
|
static const char db_lthan[] = { CHAR, '<', EOS };
|
|
|
|
static const char db_gthan[] = { CHAR, '>', EOS };
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-07-18 00:09:40 +02:00
|
|
|
/*
|
|
|
|
* Test if the current token is a whatever. Accepts the current token if
|
2009-06-10 20:12:51 +02:00
|
|
|
* it is. Returns 0 if it is not, non-zero if it is (in the case of
|
2005-05-23 05:06:10 +02:00
|
|
|
* TM_UNOP and TM_BINOP, the returned value is a Test_op).
|
|
|
|
*/
|
2010-07-18 00:09:40 +02:00
|
|
|
static Test_op
|
2005-05-23 05:06:10 +02:00
|
|
|
dbtestp_isa(Test_env *te, Test_meta meta)
|
|
|
|
{
|
|
|
|
int c = tpeek(ARRAYVAR | (meta == TM_BINOP ? 0 : CONTIN));
|
2013-09-10 18:30:50 +02:00
|
|
|
bool uqword;
|
2005-05-23 05:06:10 +02:00
|
|
|
char *save = NULL;
|
2010-07-18 00:09:40 +02:00
|
|
|
Test_op ret = TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* unquoted word? */
|
|
|
|
uqword = c == LWORD && *ident;
|
|
|
|
|
|
|
|
if (meta == TM_OR)
|
2010-07-18 00:09:40 +02:00
|
|
|
ret = c == LOGOR ? TO_NONNULL : TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (meta == TM_AND)
|
2010-07-18 00:09:40 +02:00
|
|
|
ret = c == LOGAND ? TO_NONNULL : TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (meta == TM_NOT)
|
2010-07-18 00:09:40 +02:00
|
|
|
ret = (uqword && !strcmp(yylval.cp,
|
|
|
|
dbtest_tokens[(int)TM_NOT])) ? TO_NONNULL : TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (meta == TM_OPAREN)
|
2010-07-18 00:09:40 +02:00
|
|
|
ret = c == '(' /*)*/ ? TO_NONNULL : TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (meta == TM_CPAREN)
|
2010-07-18 00:09:40 +02:00
|
|
|
ret = c == /*(*/ ')' ? TO_NONNULL : TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (meta == TM_UNOP || meta == TM_BINOP) {
|
|
|
|
if (meta == TM_BINOP && c == REDIR &&
|
2015-04-12 00:03:32 +02:00
|
|
|
(yylval.iop->ioflag == IOREAD ||
|
|
|
|
yylval.iop->ioflag == IOWRITE)) {
|
2010-07-18 00:09:40 +02:00
|
|
|
ret = TO_NONNULL;
|
2015-04-12 00:03:32 +02:00
|
|
|
save = wdcopy(yylval.iop->ioflag == IOREAD ?
|
2005-05-23 05:06:10 +02:00
|
|
|
db_lthan : db_gthan, ATEMP);
|
2005-10-08 21:31:00 +02:00
|
|
|
} else if (uqword && (ret = test_isop(meta, ident)))
|
2005-05-23 05:06:10 +02:00
|
|
|
save = yylval.cp;
|
2011-03-06 02:25:35 +01:00
|
|
|
} else
|
|
|
|
/* meta == TM_END */
|
2010-07-18 00:09:40 +02:00
|
|
|
ret = (uqword && !strcmp(yylval.cp,
|
|
|
|
db_close)) ? TO_NONNULL : TO_NONOP;
|
|
|
|
if (ret != TO_NONOP) {
|
2005-05-23 05:06:10 +02:00
|
|
|
ACCEPT;
|
2012-12-05 20:38:25 +01:00
|
|
|
if ((unsigned int)meta < NELEM(dbtest_tokens))
|
2007-05-13 21:00:30 +02:00
|
|
|
save = wdcopy(dbtest_tokens[(int)meta], ATEMP);
|
|
|
|
if (save)
|
2005-05-23 05:06:10 +02:00
|
|
|
XPput(*te->pos.av, save);
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ret);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2009-12-12 23:27:10 +01:00
|
|
|
dbtestp_getopnd(Test_env *te, Test_op op MKSH_A_UNUSED,
|
|
|
|
bool do_eval MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int c = tpeek(ARRAYVAR);
|
|
|
|
|
|
|
|
if (c != LWORD)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
ACCEPT;
|
|
|
|
XPput(*te->pos.av, yylval.cp);
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (null);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
dbtestp_eval(Test_env *te MKSH_A_UNUSED, Test_op op MKSH_A_UNUSED,
|
|
|
|
const char *opnd1 MKSH_A_UNUSED, const char *opnd2 MKSH_A_UNUSED,
|
|
|
|
bool do_eval MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dbtestp_error(Test_env *te, int offset, const char *msg)
|
|
|
|
{
|
|
|
|
te->flags |= TEF_ERROR;
|
|
|
|
|
|
|
|
if (offset < 0) {
|
|
|
|
REJECT;
|
|
|
|
/* Kludgy to say the least... */
|
|
|
|
symbol = LWORD;
|
|
|
|
yylval.cp = *(XPptrv(*te->pos.av) + XPsize(*te->pos.av) +
|
|
|
|
offset);
|
|
|
|
}
|
|
|
|
syntaxerr(msg);
|
|
|
|
}
|
2011-02-11 01:41:38 +01:00
|
|
|
|
|
|
|
#if HAVE_SELECT
|
|
|
|
|
|
|
|
#ifndef EOVERFLOW
|
|
|
|
#ifdef ERANGE
|
|
|
|
#define EOVERFLOW ERANGE
|
|
|
|
#else
|
|
|
|
#define EOVERFLOW EINVAL
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool
|
|
|
|
parse_usec(const char *s, struct timeval *tv)
|
|
|
|
{
|
|
|
|
struct timeval tt;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
tv->tv_sec = 0;
|
|
|
|
/* parse integral part */
|
|
|
|
while (ksh_isdigit(*s)) {
|
2015-04-29 22:07:35 +02:00
|
|
|
tt.tv_sec = tv->tv_sec * 10 + ksh_numdig(*s++);
|
|
|
|
/*XXX this overflow check maybe UB */
|
2011-02-11 01:41:38 +01:00
|
|
|
if (tt.tv_sec / 10 != tv->tv_sec) {
|
|
|
|
errno = EOVERFLOW;
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
tv->tv_sec = tt.tv_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
tv->tv_usec = 0;
|
|
|
|
if (!*s)
|
|
|
|
/* no decimal fraction */
|
|
|
|
return (false);
|
|
|
|
else if (*s++ != '.') {
|
|
|
|
/* junk after integral part */
|
|
|
|
errno = EINVAL;
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse decimal fraction */
|
|
|
|
i = 100000;
|
|
|
|
while (ksh_isdigit(*s)) {
|
2015-04-29 22:07:35 +02:00
|
|
|
tv->tv_usec += i * ksh_numdig(*s++);
|
2011-02-11 01:41:38 +01:00
|
|
|
if (i == 1)
|
|
|
|
break;
|
|
|
|
i /= 10;
|
|
|
|
}
|
|
|
|
/* check for junk after fractional part */
|
|
|
|
while (ksh_isdigit(*s))
|
|
|
|
++s;
|
|
|
|
if (*s) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end of input string reached, no errors */
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
#endif
|
2011-03-06 02:25:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper function called from within lex.c:yylex() to parse
|
|
|
|
* a COMSUB recursively using the main shell parser and lexer
|
|
|
|
*/
|
|
|
|
char *
|
2012-10-22 22:19:18 +02:00
|
|
|
yyrecursive(int subtype MKSH_A_UNUSED)
|
2011-03-06 02:25:35 +01:00
|
|
|
{
|
|
|
|
struct op *t;
|
|
|
|
char *cp;
|
2012-10-30 21:07:15 +01:00
|
|
|
struct yyrecursive_state *ys;
|
2012-10-22 22:19:18 +02:00
|
|
|
int stok, etok;
|
|
|
|
|
2013-05-02 23:59:54 +02:00
|
|
|
if (subtype != COMSUB) {
|
2012-10-22 22:19:18 +02:00
|
|
|
stok = '{';
|
|
|
|
etok = '}';
|
2012-11-30 21:19:16 +01:00
|
|
|
} else {
|
2012-10-22 22:19:18 +02:00
|
|
|
stok = '(';
|
|
|
|
etok = ')';
|
|
|
|
}
|
2011-03-12 21:20:17 +01:00
|
|
|
|
2012-10-30 21:07:15 +01:00
|
|
|
ys = alloc(sizeof(struct yyrecursive_state), ATEMP);
|
|
|
|
|
2011-03-12 21:20:17 +01:00
|
|
|
/* tell the lexer to accept a closing parenthesis as EOD */
|
2012-10-30 21:07:15 +01:00
|
|
|
ys->old_nesting_type = subshell_nesting_type;
|
2012-10-22 22:19:18 +02:00
|
|
|
subshell_nesting_type = etok;
|
2011-03-06 02:25:35 +01:00
|
|
|
|
|
|
|
/* push reject state, parse recursively, pop reject state */
|
2012-10-30 21:07:15 +01:00
|
|
|
ys->old_reject = reject;
|
|
|
|
ys->old_symbol = symbol;
|
2011-03-06 02:25:35 +01:00
|
|
|
ACCEPT;
|
2012-10-30 21:07:15 +01:00
|
|
|
ys->old_herep = herep;
|
|
|
|
ys->old_salias = sALIAS;
|
2011-12-29 23:03:15 +01:00
|
|
|
sALIAS = 0;
|
2012-10-30 21:07:15 +01:00
|
|
|
ys->next = e->yyrecursive_statep;
|
|
|
|
e->yyrecursive_statep = ys;
|
2011-03-06 02:25:35 +01:00
|
|
|
/* we use TPAREN as a helper container here */
|
2012-10-22 22:19:18 +02:00
|
|
|
t = nested(TPAREN, stok, etok);
|
2012-10-30 21:49:44 +01:00
|
|
|
yyrecursive_pop(false);
|
2011-03-06 02:25:35 +01:00
|
|
|
|
|
|
|
/* t->left because nested(TPAREN, ...) hides our goodies there */
|
|
|
|
cp = snptreef(NULL, 0, "%T", t->left);
|
|
|
|
tfree(t, ATEMP);
|
|
|
|
|
|
|
|
return (cp);
|
|
|
|
}
|
2012-10-30 21:07:15 +01:00
|
|
|
|
|
|
|
void
|
2012-10-30 21:49:44 +01:00
|
|
|
yyrecursive_pop(bool popall)
|
2012-10-30 21:07:15 +01:00
|
|
|
{
|
2012-10-30 21:49:44 +01:00
|
|
|
struct yyrecursive_state *ys;
|
2012-10-30 21:07:15 +01:00
|
|
|
|
2012-10-30 21:49:44 +01:00
|
|
|
popnext:
|
|
|
|
if (!(ys = e->yyrecursive_statep))
|
|
|
|
return;
|
2012-10-30 21:07:15 +01:00
|
|
|
e->yyrecursive_statep = ys->next;
|
|
|
|
|
|
|
|
sALIAS = ys->old_salias;
|
|
|
|
herep = ys->old_herep;
|
|
|
|
reject = ys->old_reject;
|
|
|
|
symbol = ys->old_symbol;
|
|
|
|
|
|
|
|
subshell_nesting_type = ys->old_nesting_type;
|
|
|
|
|
|
|
|
afree(ys, ATEMP);
|
2012-10-30 21:49:44 +01:00
|
|
|
if (popall)
|
|
|
|
goto popnext;
|
2012-10-30 21:07:15 +01:00
|
|
|
}
|