Home | History | Annotate | Line # | Download | only in csh
exp.c revision 1.12
      1  1.12       wiz /* $NetBSD: exp.c,v 1.12 2001/09/14 14:04:00 wiz Exp $ */
      2   1.6       cgd 
      3   1.1       cgd /*-
      4   1.5   mycroft  * Copyright (c) 1980, 1991, 1993
      5   1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.8  christos #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38   1.6       cgd #if 0
     39   1.6       cgd static char sccsid[] = "@(#)exp.c	8.1 (Berkeley) 5/31/93";
     40   1.6       cgd #else
     41  1.12       wiz __RCSID("$NetBSD: exp.c,v 1.12 2001/09/14 14:04:00 wiz Exp $");
     42   1.6       cgd #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/types.h>
     46   1.1       cgd #include <sys/stat.h>
     47  1.12       wiz 
     48   1.1       cgd #include <stdlib.h>
     49   1.1       cgd #include <unistd.h>
     50  1.12       wiz 
     51   1.5   mycroft #ifndef SHORT_STRINGS
     52   1.5   mycroft #include <string.h>
     53   1.5   mycroft #endif /* SHORT_STRINGS */
     54  1.12       wiz 
     55   1.1       cgd #if __STDC__
     56   1.1       cgd # include <stdarg.h>
     57   1.1       cgd #else
     58   1.1       cgd # include <varargs.h>
     59   1.1       cgd #endif
     60   1.1       cgd 
     61   1.1       cgd #include "csh.h"
     62   1.1       cgd #include "extern.h"
     63   1.1       cgd 
     64   1.1       cgd #define IGNORE	1	/* in ignore, it means to ignore value, just parse */
     65   1.1       cgd #define NOGLOB	2	/* in ignore, it means not to globone */
     66   1.1       cgd 
     67   1.1       cgd #define	ADDOP	1
     68   1.1       cgd #define	MULOP	2
     69   1.1       cgd #define	EQOP	4
     70   1.1       cgd #define	RELOP	8
     71   1.1       cgd #define	RESTOP	16
     72   1.1       cgd #define	ANYOP	31
     73   1.1       cgd 
     74   1.1       cgd #define	EQEQ	1
     75   1.1       cgd #define	GTR	2
     76   1.1       cgd #define	LSS	4
     77   1.1       cgd #define	NOTEQ	6
     78   1.1       cgd #define EQMATCH 7
     79   1.1       cgd #define NOTEQMATCH 8
     80   1.1       cgd 
     81  1.12       wiz static int exp1(Char ***, bool);
     82  1.12       wiz static int exp2(Char ***, bool);
     83  1.12       wiz static int exp2a(Char ***, bool);
     84  1.12       wiz static int exp2b(Char ***, bool);
     85  1.12       wiz static int exp2c(Char ***, bool);
     86  1.12       wiz static Char *exp3(Char ***, bool);
     87  1.12       wiz static Char *exp3a(Char ***, bool);
     88  1.12       wiz static Char *exp4(Char ***, bool);
     89  1.12       wiz static Char *exp5(Char ***, bool);
     90  1.12       wiz static Char *exp6(Char ***, bool);
     91  1.12       wiz static void evalav(Char **);
     92  1.12       wiz static int isa(Char *, int);
     93  1.12       wiz static int egetn(Char *);
     94   1.1       cgd 
     95   1.1       cgd #ifdef EDEBUG
     96  1.12       wiz static void etracc(char *, Char *, Char ***);
     97  1.12       wiz static void etraci(char *, int, Char ***);
     98   1.1       cgd #endif
     99   1.1       cgd 
    100   1.1       cgd int
    101  1.12       wiz expr(Char ***vp)
    102   1.1       cgd {
    103   1.1       cgd     return (exp0(vp, 0));
    104   1.1       cgd }
    105   1.1       cgd 
    106   1.1       cgd int
    107  1.12       wiz exp0(Char ***vp, bool ignore)
    108   1.1       cgd {
    109  1.12       wiz     int p1;
    110   1.1       cgd 
    111  1.12       wiz     p1 = exp1(vp, ignore);
    112   1.1       cgd #ifdef EDEBUG
    113   1.1       cgd     etraci("exp0 p1", p1, vp);
    114   1.1       cgd #endif
    115   1.1       cgd     if (**vp && eq(**vp, STRor2)) {
    116   1.7       tls 	int p2;
    117   1.1       cgd 
    118   1.1       cgd 	(*vp)++;
    119   1.1       cgd 	p2 = exp0(vp, (ignore & IGNORE) || p1);
    120   1.1       cgd #ifdef EDEBUG
    121   1.1       cgd 	etraci("exp0 p2", p2, vp);
    122   1.1       cgd #endif
    123   1.1       cgd 	return (p1 || p2);
    124   1.1       cgd     }
    125   1.1       cgd     return (p1);
    126   1.1       cgd }
    127   1.1       cgd 
    128   1.1       cgd static int
    129  1.12       wiz exp1(Char ***vp, bool ignore)
    130   1.1       cgd {
    131  1.12       wiz     int p1;
    132   1.1       cgd 
    133  1.12       wiz     p1 = exp2(vp, ignore);
    134   1.1       cgd #ifdef EDEBUG
    135   1.1       cgd     etraci("exp1 p1", p1, vp);
    136   1.1       cgd #endif
    137   1.1       cgd     if (**vp && eq(**vp, STRand2)) {
    138   1.7       tls 	int p2;
    139   1.1       cgd 
    140   1.1       cgd 	(*vp)++;
    141   1.1       cgd 	p2 = exp1(vp, (ignore & IGNORE) || !p1);
    142   1.1       cgd #ifdef EDEBUG
    143   1.1       cgd 	etraci("exp1 p2", p2, vp);
    144   1.1       cgd #endif
    145   1.1       cgd 	return (p1 && p2);
    146   1.1       cgd     }
    147   1.1       cgd     return (p1);
    148   1.1       cgd }
    149   1.1       cgd 
    150   1.1       cgd static int
    151  1.12       wiz exp2(Char ***vp, bool ignore)
    152   1.1       cgd {
    153  1.12       wiz     int p1;
    154   1.1       cgd 
    155  1.12       wiz     p1 = exp2a(vp, ignore);
    156   1.1       cgd #ifdef EDEBUG
    157   1.1       cgd     etraci("exp3 p1", p1, vp);
    158   1.1       cgd #endif
    159   1.1       cgd     if (**vp && eq(**vp, STRor)) {
    160   1.7       tls 	int p2;
    161   1.1       cgd 
    162   1.1       cgd 	(*vp)++;
    163   1.1       cgd 	p2 = exp2(vp, ignore);
    164   1.1       cgd #ifdef EDEBUG
    165   1.1       cgd 	etraci("exp3 p2", p2, vp);
    166   1.1       cgd #endif
    167   1.1       cgd 	return (p1 | p2);
    168   1.1       cgd     }
    169   1.1       cgd     return (p1);
    170   1.1       cgd }
    171   1.1       cgd 
    172   1.1       cgd static int
    173  1.12       wiz exp2a(Char ***vp, bool ignore)
    174   1.1       cgd {
    175  1.12       wiz     int p1;
    176   1.1       cgd 
    177  1.12       wiz     p1 = exp2b(vp, ignore);
    178   1.1       cgd #ifdef EDEBUG
    179   1.1       cgd     etraci("exp2a p1", p1, vp);
    180   1.1       cgd #endif
    181   1.1       cgd     if (**vp && eq(**vp, STRcaret)) {
    182   1.7       tls 	int p2;
    183   1.1       cgd 
    184   1.1       cgd 	(*vp)++;
    185   1.1       cgd 	p2 = exp2a(vp, ignore);
    186   1.1       cgd #ifdef EDEBUG
    187   1.1       cgd 	etraci("exp2a p2", p2, vp);
    188   1.1       cgd #endif
    189   1.1       cgd 	return (p1 ^ p2);
    190   1.1       cgd     }
    191   1.1       cgd     return (p1);
    192   1.1       cgd }
    193   1.1       cgd 
    194   1.1       cgd static int
    195  1.12       wiz exp2b(Char ***vp, bool ignore)
    196   1.1       cgd {
    197  1.12       wiz     int p1;
    198   1.1       cgd 
    199  1.12       wiz     p1 = exp2c(vp, ignore);
    200   1.1       cgd #ifdef EDEBUG
    201   1.1       cgd     etraci("exp2b p1", p1, vp);
    202   1.1       cgd #endif
    203   1.1       cgd     if (**vp && eq(**vp, STRand)) {
    204   1.7       tls 	int p2;
    205   1.1       cgd 
    206   1.1       cgd 	(*vp)++;
    207   1.1       cgd 	p2 = exp2b(vp, ignore);
    208   1.1       cgd #ifdef EDEBUG
    209   1.1       cgd 	etraci("exp2b p2", p2, vp);
    210   1.1       cgd #endif
    211   1.1       cgd 	return (p1 & p2);
    212   1.1       cgd     }
    213   1.1       cgd     return (p1);
    214   1.1       cgd }
    215   1.1       cgd 
    216   1.1       cgd static int
    217  1.12       wiz exp2c(Char ***vp, bool ignore)
    218   1.1       cgd {
    219  1.12       wiz     Char *p1, *p2;
    220   1.7       tls     int i;
    221   1.1       cgd 
    222  1.12       wiz     p1 = exp3(vp, ignore);
    223   1.1       cgd #ifdef EDEBUG
    224   1.1       cgd     etracc("exp2c p1", p1, vp);
    225   1.1       cgd #endif
    226   1.5   mycroft     if ((i = isa(**vp, EQOP)) != 0) {
    227   1.1       cgd 	(*vp)++;
    228   1.1       cgd 	if (i == EQMATCH || i == NOTEQMATCH)
    229   1.1       cgd 	    ignore |= NOGLOB;
    230   1.1       cgd 	p2 = exp3(vp, ignore);
    231   1.1       cgd #ifdef EDEBUG
    232   1.1       cgd 	etracc("exp2c p2", p2, vp);
    233   1.1       cgd #endif
    234   1.1       cgd 	if (!(ignore & IGNORE))
    235   1.1       cgd 	    switch (i) {
    236   1.1       cgd 	    case EQEQ:
    237   1.1       cgd 		i = eq(p1, p2);
    238   1.1       cgd 		break;
    239  1.12       wiz 	    case EQMATCH:
    240  1.12       wiz 		i = Gmatch(p1, p2);
    241  1.12       wiz 		break;
    242   1.1       cgd 	    case NOTEQ:
    243   1.1       cgd 		i = !eq(p1, p2);
    244   1.1       cgd 		break;
    245   1.1       cgd 	    case NOTEQMATCH:
    246   1.1       cgd 		i = !Gmatch(p1, p2);
    247   1.1       cgd 		break;
    248   1.1       cgd 	    }
    249   1.1       cgd 	xfree((ptr_t) p1);
    250   1.1       cgd 	xfree((ptr_t) p2);
    251   1.1       cgd 	return (i);
    252   1.1       cgd     }
    253   1.1       cgd     i = egetn(p1);
    254   1.1       cgd     xfree((ptr_t) p1);
    255   1.1       cgd     return (i);
    256   1.1       cgd }
    257   1.1       cgd 
    258   1.1       cgd static Char *
    259  1.12       wiz exp3(Char ***vp, bool ignore)
    260   1.1       cgd {
    261   1.7       tls     Char *p1, *p2;
    262   1.7       tls     int i;
    263   1.1       cgd 
    264   1.1       cgd     p1 = exp3a(vp, ignore);
    265   1.1       cgd #ifdef EDEBUG
    266   1.1       cgd     etracc("exp3 p1", p1, vp);
    267   1.1       cgd #endif
    268   1.5   mycroft     if ((i = isa(**vp, RELOP)) != 0) {
    269   1.1       cgd 	(*vp)++;
    270   1.1       cgd 	if (**vp && eq(**vp, STRequal))
    271   1.1       cgd 	    i |= 1, (*vp)++;
    272   1.1       cgd 	p2 = exp3(vp, ignore);
    273   1.1       cgd #ifdef EDEBUG
    274   1.1       cgd 	etracc("exp3 p2", p2, vp);
    275   1.1       cgd #endif
    276   1.1       cgd 	if (!(ignore & IGNORE))
    277   1.1       cgd 	    switch (i) {
    278   1.1       cgd 	    case GTR:
    279   1.1       cgd 		i = egetn(p1) > egetn(p2);
    280   1.1       cgd 		break;
    281   1.1       cgd 	    case GTR | 1:
    282   1.1       cgd 		i = egetn(p1) >= egetn(p2);
    283   1.1       cgd 		break;
    284   1.1       cgd 	    case LSS:
    285   1.1       cgd 		i = egetn(p1) < egetn(p2);
    286   1.1       cgd 		break;
    287   1.1       cgd 	    case LSS | 1:
    288   1.1       cgd 		i = egetn(p1) <= egetn(p2);
    289   1.1       cgd 		break;
    290   1.1       cgd 	    }
    291   1.1       cgd 	xfree((ptr_t) p1);
    292   1.1       cgd 	xfree((ptr_t) p2);
    293   1.1       cgd 	return (putn(i));
    294   1.1       cgd     }
    295   1.1       cgd     return (p1);
    296   1.1       cgd }
    297   1.1       cgd 
    298   1.1       cgd static Char *
    299  1.12       wiz exp3a(Char ***vp, bool ignore)
    300   1.1       cgd {
    301  1.12       wiz     Char *op, *p1, *p2;
    302   1.7       tls     int i;
    303   1.1       cgd 
    304   1.1       cgd     p1 = exp4(vp, ignore);
    305   1.1       cgd #ifdef EDEBUG
    306   1.1       cgd     etracc("exp3a p1", p1, vp);
    307   1.1       cgd #endif
    308   1.1       cgd     op = **vp;
    309   1.1       cgd     if (op && any("<>", op[0]) && op[0] == op[1]) {
    310   1.1       cgd 	(*vp)++;
    311   1.1       cgd 	p2 = exp3a(vp, ignore);
    312   1.1       cgd #ifdef EDEBUG
    313   1.1       cgd 	etracc("exp3a p2", p2, vp);
    314   1.1       cgd #endif
    315   1.1       cgd 	if (op[0] == '<')
    316   1.1       cgd 	    i = egetn(p1) << egetn(p2);
    317   1.1       cgd 	else
    318   1.1       cgd 	    i = egetn(p1) >> egetn(p2);
    319   1.1       cgd 	xfree((ptr_t) p1);
    320   1.1       cgd 	xfree((ptr_t) p2);
    321   1.1       cgd 	return (putn(i));
    322   1.1       cgd     }
    323   1.1       cgd     return (p1);
    324   1.1       cgd }
    325   1.1       cgd 
    326   1.1       cgd static Char *
    327  1.12       wiz exp4(Char ***vp, bool ignore)
    328   1.1       cgd {
    329   1.7       tls     Char *p1, *p2;
    330  1.12       wiz     int i;
    331   1.1       cgd 
    332  1.12       wiz     i = 0;
    333   1.1       cgd     p1 = exp5(vp, ignore);
    334   1.1       cgd #ifdef EDEBUG
    335   1.1       cgd     etracc("exp4 p1", p1, vp);
    336   1.1       cgd #endif
    337   1.1       cgd     if (isa(**vp, ADDOP)) {
    338  1.12       wiz 	Char *op;
    339   1.1       cgd 
    340  1.12       wiz 	op = *(*vp)++;
    341   1.1       cgd 	p2 = exp4(vp, ignore);
    342   1.1       cgd #ifdef EDEBUG
    343   1.1       cgd 	etracc("exp4 p2", p2, vp);
    344   1.1       cgd #endif
    345   1.1       cgd 	if (!(ignore & IGNORE))
    346   1.1       cgd 	    switch (op[0]) {
    347   1.1       cgd 	    case '+':
    348   1.1       cgd 		i = egetn(p1) + egetn(p2);
    349   1.1       cgd 		break;
    350   1.1       cgd 	    case '-':
    351   1.1       cgd 		i = egetn(p1) - egetn(p2);
    352   1.1       cgd 		break;
    353   1.1       cgd 	    }
    354   1.1       cgd 	xfree((ptr_t) p1);
    355   1.1       cgd 	xfree((ptr_t) p2);
    356   1.1       cgd 	return (putn(i));
    357   1.1       cgd     }
    358   1.1       cgd     return (p1);
    359   1.1       cgd }
    360   1.1       cgd 
    361   1.1       cgd static Char *
    362  1.12       wiz exp5(Char ***vp, bool ignore)
    363   1.1       cgd {
    364   1.7       tls     Char *p1, *p2;
    365  1.12       wiz     int i;
    366   1.1       cgd 
    367  1.12       wiz     i = 0;
    368   1.1       cgd     p1 = exp6(vp, ignore);
    369   1.1       cgd #ifdef EDEBUG
    370   1.1       cgd     etracc("exp5 p1", p1, vp);
    371   1.1       cgd #endif
    372   1.1       cgd     if (isa(**vp, MULOP)) {
    373  1.12       wiz 	Char *op;
    374   1.1       cgd 
    375  1.12       wiz 	op = *(*vp)++;
    376   1.1       cgd 	p2 = exp5(vp, ignore);
    377   1.1       cgd #ifdef EDEBUG
    378   1.1       cgd 	etracc("exp5 p2", p2, vp);
    379   1.1       cgd #endif
    380   1.1       cgd 	if (!(ignore & IGNORE))
    381   1.1       cgd 	    switch (op[0]) {
    382   1.1       cgd 	    case '*':
    383   1.1       cgd 		i = egetn(p1) * egetn(p2);
    384   1.1       cgd 		break;
    385   1.1       cgd 	    case '/':
    386   1.1       cgd 		i = egetn(p2);
    387  1.11   mycroft 		if (i == 0)
    388   1.1       cgd 		    stderror(ERR_DIV0);
    389   1.1       cgd 		i = egetn(p1) / i;
    390   1.1       cgd 		break;
    391   1.1       cgd 	    case '%':
    392   1.1       cgd 		i = egetn(p2);
    393  1.11   mycroft 		if (i == 0)
    394   1.1       cgd 		    stderror(ERR_MOD0);
    395   1.1       cgd 		i = egetn(p1) % i;
    396   1.1       cgd 		break;
    397   1.1       cgd 	    }
    398   1.1       cgd 	xfree((ptr_t) p1);
    399   1.1       cgd 	xfree((ptr_t) p2);
    400   1.1       cgd 	return (putn(i));
    401   1.1       cgd     }
    402   1.1       cgd     return (p1);
    403   1.1       cgd }
    404   1.1       cgd 
    405   1.1       cgd static Char *
    406  1.12       wiz exp6(Char ***vp, bool ignore)
    407   1.1       cgd {
    408   1.7       tls     Char *cp, *dp, *ep;
    409  1.12       wiz     int ccode, i;
    410   1.1       cgd 
    411  1.12       wiz     i = 0;
    412  1.11   mycroft     if (**vp == 0)
    413   1.1       cgd 	stderror(ERR_NAME | ERR_EXPRESSION);
    414   1.1       cgd     if (eq(**vp, STRbang)) {
    415   1.1       cgd 	(*vp)++;
    416   1.1       cgd 	cp = exp6(vp, ignore);
    417   1.1       cgd #ifdef EDEBUG
    418   1.1       cgd 	etracc("exp6 ! cp", cp, vp);
    419   1.1       cgd #endif
    420   1.1       cgd 	i = egetn(cp);
    421   1.1       cgd 	xfree((ptr_t) cp);
    422   1.1       cgd 	return (putn(!i));
    423   1.1       cgd     }
    424   1.1       cgd     if (eq(**vp, STRtilde)) {
    425   1.1       cgd 	(*vp)++;
    426   1.1       cgd 	cp = exp6(vp, ignore);
    427   1.1       cgd #ifdef EDEBUG
    428   1.1       cgd 	etracc("exp6 ~ cp", cp, vp);
    429   1.1       cgd #endif
    430   1.1       cgd 	i = egetn(cp);
    431   1.1       cgd 	xfree((ptr_t) cp);
    432   1.1       cgd 	return (putn(~i));
    433   1.1       cgd     }
    434   1.1       cgd     if (eq(**vp, STRLparen)) {
    435   1.1       cgd 	(*vp)++;
    436   1.1       cgd 	ccode = exp0(vp, ignore);
    437   1.1       cgd #ifdef EDEBUG
    438   1.1       cgd 	etraci("exp6 () ccode", ccode, vp);
    439   1.1       cgd #endif
    440  1.11   mycroft 	if (*vp == 0 || **vp == 0 || ***vp != ')')
    441   1.1       cgd 	    stderror(ERR_NAME | ERR_EXPRESSION);
    442   1.1       cgd 	(*vp)++;
    443   1.1       cgd 	return (putn(ccode));
    444   1.1       cgd     }
    445   1.1       cgd     if (eq(**vp, STRLbrace)) {
    446  1.12       wiz 	struct command faket;
    447  1.12       wiz 	Char *fakecom[2];
    448   1.7       tls 	Char **v;
    449   1.1       cgd 
    450   1.1       cgd 	faket.t_dtyp = NODE_COMMAND;
    451   1.1       cgd 	faket.t_dflg = 0;
    452   1.1       cgd 	faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
    453   1.1       cgd 	faket.t_dcom = fakecom;
    454   1.1       cgd 	fakecom[0] = STRfakecom;
    455   1.1       cgd 	fakecom[1] = NULL;
    456   1.1       cgd 	(*vp)++;
    457   1.1       cgd 	v = *vp;
    458   1.1       cgd 	for (;;) {
    459  1.11   mycroft 	    if (!**vp)
    460   1.1       cgd 		stderror(ERR_NAME | ERR_MISSING, '}');
    461   1.1       cgd 	    if (eq(*(*vp)++, STRRbrace))
    462   1.1       cgd 		break;
    463   1.1       cgd 	}
    464   1.1       cgd 	if (ignore & IGNORE)
    465   1.1       cgd 	    return (Strsave(STRNULL));
    466   1.1       cgd 	psavejob();
    467   1.1       cgd 	if (pfork(&faket, -1) == 0) {
    468   1.1       cgd 	    *--(*vp) = 0;
    469   1.1       cgd 	    evalav(v);
    470   1.1       cgd 	    exitstat();
    471   1.1       cgd 	}
    472   1.1       cgd 	pwait();
    473   1.1       cgd 	prestjob();
    474   1.1       cgd #ifdef EDEBUG
    475   1.1       cgd 	etraci("exp6 {} status", egetn(value(STRstatus)), vp);
    476   1.1       cgd #endif
    477   1.1       cgd 	return (putn(egetn(value(STRstatus)) == 0));
    478   1.1       cgd     }
    479   1.1       cgd     if (isa(**vp, ANYOP))
    480   1.1       cgd 	return (Strsave(STRNULL));
    481   1.1       cgd     cp = *(*vp)++;
    482   1.1       cgd     if (*cp == '-' && any("erwxfdzopls", cp[1])) {
    483   1.1       cgd 	struct stat stb;
    484   1.1       cgd 
    485  1.11   mycroft 	if (cp[2] != '\0')
    486   1.1       cgd 	    stderror(ERR_NAME | ERR_FILEINQ);
    487   1.1       cgd 	/*
    488   1.1       cgd 	 * Detect missing file names by checking for operator in the file name
    489   1.1       cgd 	 * position.  However, if an operator name appears there, we must make
    490   1.1       cgd 	 * sure that there's no file by that name (e.g., "/") before announcing
    491   1.1       cgd 	 * an error.  Even this check isn't quite right, since it doesn't take
    492   1.1       cgd 	 * globbing into account.
    493   1.1       cgd 	 */
    494  1.11   mycroft 	if (isa(**vp, ANYOP) && stat(short2str(**vp), &stb))
    495   1.1       cgd 	    stderror(ERR_NAME | ERR_FILENAME);
    496   1.1       cgd 
    497   1.1       cgd 	dp = *(*vp)++;
    498   1.1       cgd 	if (ignore & IGNORE)
    499   1.1       cgd 	    return (Strsave(STRNULL));
    500   1.1       cgd 	ep = globone(dp, G_ERROR);
    501   1.1       cgd 	switch (cp[1]) {
    502   1.1       cgd 	case 'r':
    503   1.1       cgd 	    i = !access(short2str(ep), R_OK);
    504   1.1       cgd 	    break;
    505   1.1       cgd 	case 'w':
    506   1.1       cgd 	    i = !access(short2str(ep), W_OK);
    507   1.1       cgd 	    break;
    508   1.1       cgd 	case 'x':
    509   1.1       cgd 	    i = !access(short2str(ep), X_OK);
    510   1.1       cgd 	    break;
    511   1.1       cgd 	default:
    512   1.9   mycroft 	    if (cp[1] == 'l' ?
    513   1.9   mycroft 		lstat(short2str(ep), &stb) : stat(short2str(ep), &stb)) {
    514   1.1       cgd 		xfree((ptr_t) ep);
    515   1.1       cgd 		return (Strsave(STR0));
    516   1.1       cgd 	    }
    517   1.1       cgd 	    switch (cp[1]) {
    518  1.12       wiz 	    case 'd':
    519  1.12       wiz 		i = S_ISDIR(stb.st_mode);
    520  1.12       wiz 		break;
    521  1.12       wiz 	    case 'e':
    522  1.12       wiz 		i = 1;
    523  1.12       wiz 		break;
    524   1.1       cgd 	    case 'f':
    525   1.1       cgd 		i = S_ISREG(stb.st_mode);
    526   1.1       cgd 		break;
    527  1.12       wiz 	    case 'l':
    528  1.12       wiz #ifdef S_ISLNK
    529  1.12       wiz 		i = S_ISLNK(stb.st_mode);
    530  1.12       wiz #else
    531  1.12       wiz 		i = 0;
    532  1.12       wiz #endif
    533  1.12       wiz 		break;
    534  1.12       wiz 	    case 'o':
    535  1.12       wiz 		i = stb.st_uid == uid;
    536   1.1       cgd 		break;
    537   1.1       cgd 	    case 'p':
    538   1.1       cgd #ifdef S_ISFIFO
    539   1.1       cgd 		i = S_ISFIFO(stb.st_mode);
    540   1.1       cgd #else
    541   1.1       cgd 		i = 0;
    542   1.1       cgd #endif
    543   1.1       cgd 		break;
    544   1.1       cgd 	    case 's':
    545   1.1       cgd #ifdef S_ISSOCK
    546   1.1       cgd 		i = S_ISSOCK(stb.st_mode);
    547   1.1       cgd #else
    548   1.1       cgd 		i = 0;
    549   1.1       cgd #endif
    550   1.1       cgd 		break;
    551   1.1       cgd 	    case 'z':
    552   1.1       cgd 		i = stb.st_size == 0;
    553   1.1       cgd 		break;
    554   1.1       cgd 	    }
    555   1.1       cgd 	}
    556   1.1       cgd #ifdef EDEBUG
    557   1.1       cgd 	etraci("exp6 -? i", i, vp);
    558   1.1       cgd #endif
    559   1.1       cgd 	xfree((ptr_t) ep);
    560   1.1       cgd 	return (putn(i));
    561   1.1       cgd     }
    562   1.1       cgd #ifdef EDEBUG
    563   1.1       cgd     etracc("exp6 default", cp, vp);
    564   1.1       cgd #endif
    565   1.1       cgd     return (ignore & NOGLOB ? Strsave(cp) : globone(cp, G_ERROR));
    566   1.1       cgd }
    567   1.1       cgd 
    568   1.1       cgd static void
    569  1.12       wiz evalav(Char **v)
    570   1.1       cgd {
    571  1.12       wiz     struct wordent *hp, paraml1, *wdp;
    572   1.1       cgd     struct command *t;
    573   1.1       cgd 
    574  1.12       wiz     hp = &paraml1;
    575  1.12       wiz     wdp = hp;
    576   1.1       cgd     set(STRstatus, Strsave(STR0));
    577   1.1       cgd     hp->prev = hp->next = hp;
    578   1.1       cgd     hp->word = STRNULL;
    579   1.1       cgd     while (*v) {
    580  1.12       wiz 	struct wordent *new;
    581   1.1       cgd 
    582  1.12       wiz 	new = (struct wordent *)xcalloc(1, sizeof *wdp);
    583   1.1       cgd 	new->prev = wdp;
    584   1.1       cgd 	new->next = hp;
    585   1.1       cgd 	wdp->next = new;
    586   1.1       cgd 	wdp = new;
    587   1.1       cgd 	wdp->word = Strsave(*v++);
    588   1.1       cgd     }
    589   1.1       cgd     hp->prev = wdp;
    590   1.1       cgd     alias(&paraml1);
    591   1.1       cgd     t = syntax(paraml1.next, &paraml1, 0);
    592  1.11   mycroft     if (seterr)
    593   1.1       cgd 	stderror(ERR_OLD);
    594   1.1       cgd     execute(t, -1, NULL, NULL);
    595   1.1       cgd     freelex(&paraml1), freesyn(t);
    596   1.1       cgd }
    597   1.1       cgd 
    598   1.1       cgd static int
    599  1.12       wiz isa(Char *cp, int what)
    600   1.1       cgd {
    601   1.1       cgd     if (cp == 0)
    602   1.1       cgd 	return ((what & RESTOP) != 0);
    603   1.1       cgd     if (cp[1] == 0) {
    604   1.1       cgd 	if (what & ADDOP && (*cp == '+' || *cp == '-'))
    605   1.1       cgd 	    return (1);
    606   1.1       cgd 	if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
    607   1.1       cgd 	    return (1);
    608   1.1       cgd 	if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
    609   1.1       cgd 			      *cp == '~' || *cp == '^' || *cp == '"'))
    610   1.1       cgd 	    return (1);
    611   1.1       cgd     }
    612   1.1       cgd     else if (cp[2] == 0) {
    613   1.1       cgd 	if (what & RESTOP) {
    614   1.1       cgd 	    if (cp[0] == '|' && cp[1] == '&')
    615   1.1       cgd 		return (1);
    616   1.1       cgd 	    if (cp[0] == '<' && cp[1] == '<')
    617   1.1       cgd 		return (1);
    618   1.1       cgd 	    if (cp[0] == '>' && cp[1] == '>')
    619   1.1       cgd 		return (1);
    620   1.1       cgd 	}
    621   1.1       cgd 	if (what & EQOP) {
    622   1.1       cgd 	    if (cp[0] == '=') {
    623   1.1       cgd 		if (cp[1] == '=')
    624   1.1       cgd 		    return (EQEQ);
    625   1.1       cgd 		if (cp[1] == '~')
    626   1.1       cgd 		    return (EQMATCH);
    627   1.1       cgd 	    }
    628   1.1       cgd 	    else if (cp[0] == '!') {
    629   1.1       cgd 		if (cp[1] == '=')
    630   1.1       cgd 		    return (NOTEQ);
    631   1.1       cgd 		if (cp[1] == '~')
    632   1.1       cgd 		    return (NOTEQMATCH);
    633   1.1       cgd 	    }
    634   1.1       cgd 	}
    635   1.1       cgd     }
    636   1.1       cgd     if (what & RELOP) {
    637   1.1       cgd 	if (*cp == '<')
    638   1.1       cgd 	    return (LSS);
    639   1.1       cgd 	if (*cp == '>')
    640   1.1       cgd 	    return (GTR);
    641   1.1       cgd     }
    642   1.1       cgd     return (0);
    643   1.1       cgd }
    644   1.1       cgd 
    645   1.1       cgd static int
    646  1.12       wiz egetn(Char *cp)
    647   1.1       cgd {
    648  1.11   mycroft     if (*cp && *cp != '-' && !Isdigit(*cp))
    649   1.1       cgd 	stderror(ERR_NAME | ERR_EXPRESSION);
    650   1.1       cgd     return (getn(cp));
    651   1.1       cgd }
    652   1.1       cgd 
    653   1.1       cgd /* Phew! */
    654   1.1       cgd 
    655   1.1       cgd #ifdef EDEBUG
    656   1.1       cgd static void
    657  1.12       wiz etraci(char *str, int i, Char ***vp)
    658   1.1       cgd {
    659  1.12       wiz     (void)fprintf(csherr, "%s=%d\t", str, i);
    660   1.5   mycroft     blkpr(csherr, *vp);
    661  1.12       wiz     (void)fprintf(csherr, "\n");
    662   1.1       cgd }
    663   1.1       cgd static void
    664  1.12       wiz etracc(char *str, Char *cp, Char ***vp)
    665   1.1       cgd {
    666  1.12       wiz     (void)fprintf(csherr, "%s=%s\t", str, vis_str(cp));
    667   1.5   mycroft     blkpr(csherr, *vp);
    668  1.12       wiz     (void)fprintf(csherr, "\n");
    669   1.1       cgd }
    670   1.1       cgd #endif
    671