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