2009-02-14 10:12:29 +03:00
|
|
|
/* $NetBSD: exp.c,v 1.20 2009/02/14 07:12:29 lukem Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-09-21 04:10:23 +04:00
|
|
|
* Copyright (c) 1980, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03: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.
|
|
|
|
*/
|
|
|
|
|
1997-07-05 01:23:50 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)exp.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
2009-02-14 10:12:29 +03:00
|
|
|
__RCSID("$NetBSD: exp.c,v 1.20 2009/02/14 07:12:29 lukem Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2001-09-14 18:03:59 +04:00
|
|
|
|
2002-05-26 03:29:16 +04:00
|
|
|
#include <stdarg.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2001-09-14 18:03:59 +04:00
|
|
|
|
1994-09-21 04:10:23 +04:00
|
|
|
#ifndef SHORT_STRINGS
|
|
|
|
#include <string.h>
|
|
|
|
#endif /* SHORT_STRINGS */
|
2001-09-14 18:03:59 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "csh.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
#define IGNORE 1 /* in ignore, it means to ignore value, just parse */
|
|
|
|
#define NOGLOB 2 /* in ignore, it means not to globone */
|
|
|
|
|
|
|
|
#define ADDOP 1
|
|
|
|
#define MULOP 2
|
|
|
|
#define EQOP 4
|
|
|
|
#define RELOP 8
|
|
|
|
#define RESTOP 16
|
|
|
|
#define ANYOP 31
|
|
|
|
|
|
|
|
#define EQEQ 1
|
|
|
|
#define GTR 2
|
|
|
|
#define LSS 4
|
|
|
|
#define NOTEQ 6
|
|
|
|
#define EQMATCH 7
|
|
|
|
#define NOTEQMATCH 8
|
|
|
|
|
2007-07-16 22:26:09 +04:00
|
|
|
static int exp1(Char ***, int);
|
|
|
|
static int csh_exp2(Char ***, int);
|
|
|
|
static int exp2a(Char ***, int);
|
|
|
|
static int exp2b(Char ***, int);
|
|
|
|
static int exp2c(Char ***, int);
|
|
|
|
static Char *exp3(Char ***, int);
|
|
|
|
static Char *exp3a(Char ***, int);
|
|
|
|
static Char *exp4(Char ***, int);
|
|
|
|
static Char *exp5(Char ***, int);
|
|
|
|
static Char *exp6(Char ***, int);
|
2001-09-14 18:03:59 +04:00
|
|
|
static void evalav(Char **);
|
|
|
|
static int isa(Char *, int);
|
|
|
|
static int egetn(Char *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#ifdef EDEBUG
|
2001-09-14 18:03:59 +04:00
|
|
|
static void etracc(char *, Char *, Char ***);
|
|
|
|
static void etraci(char *, int, Char ***);
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
2001-09-14 18:03:59 +04:00
|
|
|
expr(Char ***vp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
return (exp0(vp, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-07-16 22:26:09 +04:00
|
|
|
exp0(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
int p1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
p1 = exp1(vp, ignore);
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp0 p1", p1, vp);
|
|
|
|
#endif
|
|
|
|
if (**vp && eq(**vp, STRor2)) {
|
1997-01-13 20:53:15 +03:00
|
|
|
int p2;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(*vp)++;
|
|
|
|
p2 = exp0(vp, (ignore & IGNORE) || p1);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp0 p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
return (p1 || p2);
|
|
|
|
}
|
|
|
|
return (p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-07-16 22:26:09 +04:00
|
|
|
exp1(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
int p1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2006-05-11 01:14:48 +04:00
|
|
|
p1 = csh_exp2(vp, ignore);
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp1 p1", p1, vp);
|
|
|
|
#endif
|
|
|
|
if (**vp && eq(**vp, STRand2)) {
|
1997-01-13 20:53:15 +03:00
|
|
|
int p2;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(*vp)++;
|
|
|
|
p2 = exp1(vp, (ignore & IGNORE) || !p1);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp1 p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
return (p1 && p2);
|
|
|
|
}
|
|
|
|
return (p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-07-16 22:26:09 +04:00
|
|
|
csh_exp2(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
int p1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
p1 = exp2a(vp, ignore);
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp3 p1", p1, vp);
|
|
|
|
#endif
|
|
|
|
if (**vp && eq(**vp, STRor)) {
|
1997-01-13 20:53:15 +03:00
|
|
|
int p2;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(*vp)++;
|
2006-05-11 01:14:48 +04:00
|
|
|
p2 = csh_exp2(vp, ignore);
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp3 p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
return (p1 | p2);
|
|
|
|
}
|
|
|
|
return (p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-07-16 22:26:09 +04:00
|
|
|
exp2a(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
int p1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
p1 = exp2b(vp, ignore);
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp2a p1", p1, vp);
|
|
|
|
#endif
|
|
|
|
if (**vp && eq(**vp, STRcaret)) {
|
1997-01-13 20:53:15 +03:00
|
|
|
int p2;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(*vp)++;
|
|
|
|
p2 = exp2a(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp2a p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
return (p1 ^ p2);
|
|
|
|
}
|
|
|
|
return (p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-07-16 22:26:09 +04:00
|
|
|
exp2b(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
int p1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
p1 = exp2c(vp, ignore);
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp2b p1", p1, vp);
|
|
|
|
#endif
|
|
|
|
if (**vp && eq(**vp, STRand)) {
|
1997-01-13 20:53:15 +03:00
|
|
|
int p2;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(*vp)++;
|
|
|
|
p2 = exp2b(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp2b p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
return (p1 & p2);
|
|
|
|
}
|
|
|
|
return (p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-07-16 22:26:09 +04:00
|
|
|
exp2c(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *p1, *p2;
|
1997-01-13 20:53:15 +03:00
|
|
|
int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
p1 = exp3(vp, ignore);
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp2c p1", p1, vp);
|
|
|
|
#endif
|
1994-09-21 04:10:23 +04:00
|
|
|
if ((i = isa(**vp, EQOP)) != 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
(*vp)++;
|
|
|
|
if (i == EQMATCH || i == NOTEQMATCH)
|
|
|
|
ignore |= NOGLOB;
|
|
|
|
p2 = exp3(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp2c p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
if (!(ignore & IGNORE))
|
|
|
|
switch (i) {
|
|
|
|
case EQEQ:
|
|
|
|
i = eq(p1, p2);
|
|
|
|
break;
|
|
|
|
case EQMATCH:
|
|
|
|
i = Gmatch(p1, p2);
|
|
|
|
break;
|
2001-09-14 18:03:59 +04:00
|
|
|
case NOTEQ:
|
|
|
|
i = !eq(p1, p2);
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case NOTEQMATCH:
|
|
|
|
i = !Gmatch(p1, p2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xfree((ptr_t) p1);
|
|
|
|
xfree((ptr_t) p2);
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
i = egetn(p1);
|
|
|
|
xfree((ptr_t) p1);
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Char *
|
2007-07-16 22:26:09 +04:00
|
|
|
exp3(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *p1, *p2;
|
|
|
|
int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
p1 = exp3a(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp3 p1", p1, vp);
|
|
|
|
#endif
|
1994-09-21 04:10:23 +04:00
|
|
|
if ((i = isa(**vp, RELOP)) != 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
(*vp)++;
|
|
|
|
if (**vp && eq(**vp, STRequal))
|
|
|
|
i |= 1, (*vp)++;
|
|
|
|
p2 = exp3(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp3 p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
if (!(ignore & IGNORE))
|
|
|
|
switch (i) {
|
|
|
|
case GTR:
|
|
|
|
i = egetn(p1) > egetn(p2);
|
|
|
|
break;
|
|
|
|
case GTR | 1:
|
|
|
|
i = egetn(p1) >= egetn(p2);
|
|
|
|
break;
|
|
|
|
case LSS:
|
|
|
|
i = egetn(p1) < egetn(p2);
|
|
|
|
break;
|
|
|
|
case LSS | 1:
|
|
|
|
i = egetn(p1) <= egetn(p2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xfree((ptr_t) p1);
|
|
|
|
xfree((ptr_t) p2);
|
|
|
|
return (putn(i));
|
|
|
|
}
|
|
|
|
return (p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Char *
|
2007-07-16 22:26:09 +04:00
|
|
|
exp3a(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *op, *p1, *p2;
|
1997-01-13 20:53:15 +03:00
|
|
|
int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
p1 = exp4(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp3a p1", p1, vp);
|
|
|
|
#endif
|
|
|
|
op = **vp;
|
|
|
|
if (op && any("<>", op[0]) && op[0] == op[1]) {
|
|
|
|
(*vp)++;
|
|
|
|
p2 = exp3a(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp3a p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
if (op[0] == '<')
|
|
|
|
i = egetn(p1) << egetn(p2);
|
|
|
|
else
|
|
|
|
i = egetn(p1) >> egetn(p2);
|
|
|
|
xfree((ptr_t) p1);
|
|
|
|
xfree((ptr_t) p2);
|
|
|
|
return (putn(i));
|
|
|
|
}
|
|
|
|
return (p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Char *
|
2007-07-16 22:26:09 +04:00
|
|
|
exp4(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *p1, *p2;
|
2001-09-14 18:03:59 +04:00
|
|
|
int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
i = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
p1 = exp5(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp4 p1", p1, vp);
|
|
|
|
#endif
|
|
|
|
if (isa(**vp, ADDOP)) {
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *op;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
op = *(*vp)++;
|
1993-03-21 12:45:37 +03:00
|
|
|
p2 = exp4(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp4 p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
if (!(ignore & IGNORE))
|
|
|
|
switch (op[0]) {
|
|
|
|
case '+':
|
|
|
|
i = egetn(p1) + egetn(p2);
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
i = egetn(p1) - egetn(p2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xfree((ptr_t) p1);
|
|
|
|
xfree((ptr_t) p2);
|
|
|
|
return (putn(i));
|
|
|
|
}
|
|
|
|
return (p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Char *
|
2007-07-16 22:26:09 +04:00
|
|
|
exp5(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *p1, *p2;
|
2001-09-14 18:03:59 +04:00
|
|
|
int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
i = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
p1 = exp6(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp5 p1", p1, vp);
|
|
|
|
#endif
|
|
|
|
if (isa(**vp, MULOP)) {
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *op;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
op = *(*vp)++;
|
1993-03-21 12:45:37 +03:00
|
|
|
p2 = exp5(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp5 p2", p2, vp);
|
|
|
|
#endif
|
|
|
|
if (!(ignore & IGNORE))
|
|
|
|
switch (op[0]) {
|
|
|
|
case '*':
|
|
|
|
i = egetn(p1) * egetn(p2);
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
i = egetn(p2);
|
1998-07-28 15:41:40 +04:00
|
|
|
if (i == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_DIV0);
|
|
|
|
i = egetn(p1) / i;
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
i = egetn(p2);
|
1998-07-28 15:41:40 +04:00
|
|
|
if (i == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_MOD0);
|
|
|
|
i = egetn(p1) % i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xfree((ptr_t) p1);
|
|
|
|
xfree((ptr_t) p2);
|
|
|
|
return (putn(i));
|
|
|
|
}
|
|
|
|
return (p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Char *
|
2007-07-16 22:26:09 +04:00
|
|
|
exp6(Char ***vp, int ignore)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *cp, *dp, *ep;
|
2001-09-14 18:03:59 +04:00
|
|
|
int ccode, i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
i = 0;
|
1998-07-28 15:41:40 +04:00
|
|
|
if (**vp == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_EXPRESSION);
|
|
|
|
if (eq(**vp, STRbang)) {
|
|
|
|
(*vp)++;
|
|
|
|
cp = exp6(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp6 ! cp", cp, vp);
|
|
|
|
#endif
|
|
|
|
i = egetn(cp);
|
|
|
|
xfree((ptr_t) cp);
|
|
|
|
return (putn(!i));
|
|
|
|
}
|
|
|
|
if (eq(**vp, STRtilde)) {
|
|
|
|
(*vp)++;
|
|
|
|
cp = exp6(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp6 ~ cp", cp, vp);
|
|
|
|
#endif
|
|
|
|
i = egetn(cp);
|
|
|
|
xfree((ptr_t) cp);
|
|
|
|
return (putn(~i));
|
|
|
|
}
|
|
|
|
if (eq(**vp, STRLparen)) {
|
|
|
|
(*vp)++;
|
|
|
|
ccode = exp0(vp, ignore);
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp6 () ccode", ccode, vp);
|
|
|
|
#endif
|
2006-05-14 01:18:57 +04:00
|
|
|
if (**vp == 0 || ***vp != ')')
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_EXPRESSION);
|
|
|
|
(*vp)++;
|
|
|
|
return (putn(ccode));
|
|
|
|
}
|
|
|
|
if (eq(**vp, STRLbrace)) {
|
|
|
|
struct command faket;
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *fakecom[2];
|
|
|
|
Char **v;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
faket.t_dtyp = NODE_COMMAND;
|
|
|
|
faket.t_dflg = 0;
|
|
|
|
faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
|
|
|
|
faket.t_dcom = fakecom;
|
|
|
|
fakecom[0] = STRfakecom;
|
|
|
|
fakecom[1] = NULL;
|
|
|
|
(*vp)++;
|
|
|
|
v = *vp;
|
|
|
|
for (;;) {
|
1998-07-28 15:41:40 +04:00
|
|
|
if (!**vp)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_MISSING, '}');
|
|
|
|
if (eq(*(*vp)++, STRRbrace))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ignore & IGNORE)
|
|
|
|
return (Strsave(STRNULL));
|
|
|
|
psavejob();
|
|
|
|
if (pfork(&faket, -1) == 0) {
|
|
|
|
*--(*vp) = 0;
|
|
|
|
evalav(v);
|
|
|
|
exitstat();
|
|
|
|
}
|
|
|
|
pwait();
|
|
|
|
prestjob();
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp6 {} status", egetn(value(STRstatus)), vp);
|
|
|
|
#endif
|
|
|
|
return (putn(egetn(value(STRstatus)) == 0));
|
|
|
|
}
|
|
|
|
if (isa(**vp, ANYOP))
|
|
|
|
return (Strsave(STRNULL));
|
|
|
|
cp = *(*vp)++;
|
|
|
|
if (*cp == '-' && any("erwxfdzopls", cp[1])) {
|
|
|
|
struct stat stb;
|
|
|
|
|
1998-07-28 15:41:40 +04:00
|
|
|
if (cp[2] != '\0')
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_FILEINQ);
|
|
|
|
/*
|
|
|
|
* Detect missing file names by checking for operator in the file name
|
|
|
|
* position. However, if an operator name appears there, we must make
|
|
|
|
* sure that there's no file by that name (e.g., "/") before announcing
|
|
|
|
* an error. Even this check isn't quite right, since it doesn't take
|
|
|
|
* globbing into account.
|
|
|
|
*/
|
1998-07-28 15:41:40 +04:00
|
|
|
if (isa(**vp, ANYOP) && stat(short2str(**vp), &stb))
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_FILENAME);
|
|
|
|
|
|
|
|
dp = *(*vp)++;
|
|
|
|
if (ignore & IGNORE)
|
|
|
|
return (Strsave(STRNULL));
|
|
|
|
ep = globone(dp, G_ERROR);
|
|
|
|
switch (cp[1]) {
|
|
|
|
case 'r':
|
|
|
|
i = !access(short2str(ep), R_OK);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
i = !access(short2str(ep), W_OK);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
i = !access(short2str(ep), X_OK);
|
|
|
|
break;
|
|
|
|
default:
|
1997-10-19 21:38:05 +04:00
|
|
|
if (cp[1] == 'l' ?
|
|
|
|
lstat(short2str(ep), &stb) : stat(short2str(ep), &stb)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
xfree((ptr_t) ep);
|
|
|
|
return (Strsave(STR0));
|
|
|
|
}
|
|
|
|
switch (cp[1]) {
|
|
|
|
case 'd':
|
|
|
|
i = S_ISDIR(stb.st_mode);
|
|
|
|
break;
|
2001-09-14 18:03:59 +04:00
|
|
|
case 'e':
|
|
|
|
i = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
i = S_ISREG(stb.st_mode);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
#ifdef S_ISLNK
|
|
|
|
i = S_ISLNK(stb.st_mode);
|
|
|
|
#else
|
|
|
|
i = 0;
|
|
|
|
#endif
|
|
|
|
break;
|
2001-09-14 18:03:59 +04:00
|
|
|
case 'o':
|
2009-02-14 10:12:29 +03:00
|
|
|
i = stb.st_uid == (uid_t)uid;
|
2001-09-14 18:03:59 +04:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
#ifdef S_ISFIFO
|
|
|
|
i = S_ISFIFO(stb.st_mode);
|
|
|
|
#else
|
|
|
|
i = 0;
|
|
|
|
#endif
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 's':
|
|
|
|
#ifdef S_ISSOCK
|
|
|
|
i = S_ISSOCK(stb.st_mode);
|
|
|
|
#else
|
|
|
|
i = 0;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
i = stb.st_size == 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etraci("exp6 -? i", i, vp);
|
|
|
|
#endif
|
|
|
|
xfree((ptr_t) ep);
|
|
|
|
return (putn(i));
|
|
|
|
}
|
|
|
|
#ifdef EDEBUG
|
|
|
|
etracc("exp6 default", cp, vp);
|
|
|
|
#endif
|
|
|
|
return (ignore & NOGLOB ? Strsave(cp) : globone(cp, G_ERROR));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
evalav(Char **v)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
struct wordent *hp, paraml1, *wdp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct command *t;
|
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
hp = ¶ml1;
|
|
|
|
wdp = hp;
|
1993-03-21 12:45:37 +03:00
|
|
|
set(STRstatus, Strsave(STR0));
|
|
|
|
hp->prev = hp->next = hp;
|
|
|
|
hp->word = STRNULL;
|
|
|
|
while (*v) {
|
2001-09-14 18:03:59 +04:00
|
|
|
struct wordent *new;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
new = (struct wordent *)xcalloc(1, sizeof *wdp);
|
1993-03-21 12:45:37 +03:00
|
|
|
new->prev = wdp;
|
|
|
|
new->next = hp;
|
|
|
|
wdp->next = new;
|
|
|
|
wdp = new;
|
|
|
|
wdp->word = Strsave(*v++);
|
|
|
|
}
|
|
|
|
hp->prev = wdp;
|
|
|
|
alias(¶ml1);
|
|
|
|
t = syntax(paraml1.next, ¶ml1, 0);
|
1998-07-28 15:41:40 +04:00
|
|
|
if (seterr)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_OLD);
|
|
|
|
execute(t, -1, NULL, NULL);
|
|
|
|
freelex(¶ml1), freesyn(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-14 18:03:59 +04:00
|
|
|
isa(Char *cp, int what)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
if (cp == 0)
|
|
|
|
return ((what & RESTOP) != 0);
|
|
|
|
if (cp[1] == 0) {
|
|
|
|
if (what & ADDOP && (*cp == '+' || *cp == '-'))
|
|
|
|
return (1);
|
|
|
|
if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
|
|
|
|
return (1);
|
|
|
|
if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
|
|
|
|
*cp == '~' || *cp == '^' || *cp == '"'))
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
else if (cp[2] == 0) {
|
|
|
|
if (what & RESTOP) {
|
|
|
|
if (cp[0] == '|' && cp[1] == '&')
|
|
|
|
return (1);
|
|
|
|
if (cp[0] == '<' && cp[1] == '<')
|
|
|
|
return (1);
|
|
|
|
if (cp[0] == '>' && cp[1] == '>')
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (what & EQOP) {
|
|
|
|
if (cp[0] == '=') {
|
|
|
|
if (cp[1] == '=')
|
|
|
|
return (EQEQ);
|
|
|
|
if (cp[1] == '~')
|
|
|
|
return (EQMATCH);
|
|
|
|
}
|
|
|
|
else if (cp[0] == '!') {
|
|
|
|
if (cp[1] == '=')
|
|
|
|
return (NOTEQ);
|
|
|
|
if (cp[1] == '~')
|
|
|
|
return (NOTEQMATCH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (what & RELOP) {
|
|
|
|
if (*cp == '<')
|
|
|
|
return (LSS);
|
|
|
|
if (*cp == '>')
|
|
|
|
return (GTR);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-14 18:03:59 +04:00
|
|
|
egetn(Char *cp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-28 15:41:40 +04:00
|
|
|
if (*cp && *cp != '-' && !Isdigit(*cp))
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_EXPRESSION);
|
|
|
|
return (getn(cp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Phew! */
|
|
|
|
|
|
|
|
#ifdef EDEBUG
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
etraci(char *str, int i, Char ***vp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fprintf(csherr, "%s=%d\t", str, i);
|
1994-09-21 04:10:23 +04:00
|
|
|
blkpr(csherr, *vp);
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fprintf(csherr, "\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
etracc(char *str, Char *cp, Char ***vp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fprintf(csherr, "%s=%s\t", str, vis_str(cp));
|
1994-09-21 04:10:23 +04:00
|
|
|
blkpr(csherr, *vp);
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fprintf(csherr, "\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
#endif
|