Home | History | Annotate | Line # | Download | only in ksh
c_test.c revision 1.1.1.1
      1  1.1  jtc /*
      2  1.1  jtc  * test(1); version 7-like  --  author Erik Baalbergen
      3  1.1  jtc  * modified by Eric Gisin to be used as built-in.
      4  1.1  jtc  * modified by Arnold Robbins to add SVR3 compatibility
      5  1.1  jtc  * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
      6  1.1  jtc  * modified by Michael Rendell to add Korn's [[ .. ]] expressions.
      7  1.1  jtc  * modified by J.T. Conklin to add POSIX compatibility.
      8  1.1  jtc  */
      9  1.1  jtc 
     10  1.1  jtc #include "sh.h"
     11  1.1  jtc #include "ksh_stat.h"
     12  1.1  jtc #include "c_test.h"
     13  1.1  jtc 
     14  1.1  jtc /* test(1) accepts the following grammar:
     15  1.1  jtc 	oexpr	::= aexpr | aexpr "-o" oexpr ;
     16  1.1  jtc 	aexpr	::= nexpr | nexpr "-a" aexpr ;
     17  1.1  jtc 	nexpr	::= primary | "!" nexpr ;
     18  1.1  jtc 	primary	::= unary-operator operand
     19  1.1  jtc 		| operand binary-operator operand
     20  1.1  jtc 		| operand
     21  1.1  jtc 		| "(" oexpr ")"
     22  1.1  jtc 		;
     23  1.1  jtc 
     24  1.1  jtc 	unary-operator ::= "-a"|"-r"|"-w"|"-x"|"-e"|"-f"|"-d"|"-c"|"-b"|"-p"|
     25  1.1  jtc 			   "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|
     26  1.1  jtc 			   "-L"|"-h"|"-S"|"-H";
     27  1.1  jtc 
     28  1.1  jtc 	binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
     29  1.1  jtc 			    "-nt"|"-ot"|"-ef"|
     30  1.1  jtc 			    "<"|">"	# rules used for [[ .. ]] expressions
     31  1.1  jtc 			    ;
     32  1.1  jtc 	operand ::= <any thing>
     33  1.1  jtc */
     34  1.1  jtc 
     35  1.1  jtc #define T_ERR_EXIT	2	/* POSIX says > 1 for errors */
     36  1.1  jtc 
     37  1.1  jtc struct t_op {
     38  1.1  jtc 	char	op_text[4];
     39  1.1  jtc 	Test_op	op_num;
     40  1.1  jtc };
     41  1.1  jtc static const struct t_op u_ops [] = {
     42  1.1  jtc 	{"-a",	TO_FILAXST },
     43  1.1  jtc 	{"-b",	TO_FILBDEV },
     44  1.1  jtc 	{"-c",	TO_FILCDEV },
     45  1.1  jtc 	{"-d",	TO_FILID },
     46  1.1  jtc 	{"-e",	TO_FILEXST },
     47  1.1  jtc 	{"-f",	TO_FILREG },
     48  1.1  jtc 	{"-G",	TO_FILGID },
     49  1.1  jtc 	{"-g",	TO_FILSETG },
     50  1.1  jtc 	{"-h",	TO_FILSYM },
     51  1.1  jtc 	{"-H",	TO_FILCDF },
     52  1.1  jtc 	{"-k",	TO_FILSTCK },
     53  1.1  jtc 	{"-L",	TO_FILSYM },
     54  1.1  jtc 	{"-n",	TO_STNZE },
     55  1.1  jtc 	{"-O",	TO_FILUID },
     56  1.1  jtc 	{"-o",	TO_OPTION },
     57  1.1  jtc 	{"-p",	TO_FILFIFO },
     58  1.1  jtc 	{"-r",	TO_FILRD },
     59  1.1  jtc 	{"-s",	TO_FILGZ },
     60  1.1  jtc 	{"-S",	TO_FILSOCK },
     61  1.1  jtc 	{"-t",	TO_FILTT },
     62  1.1  jtc 	{"-u",	TO_FILSETU },
     63  1.1  jtc 	{"-w",	TO_FILWR },
     64  1.1  jtc 	{"-x",	TO_FILEX },
     65  1.1  jtc 	{"-z",	TO_STZER },
     66  1.1  jtc 	{"",	TO_NONOP }
     67  1.1  jtc     };
     68  1.1  jtc static const struct t_op b_ops [] = {
     69  1.1  jtc 	{"=",	TO_STEQL },
     70  1.1  jtc #ifdef KSH
     71  1.1  jtc 	{"==",	TO_STEQL },
     72  1.1  jtc #endif /* KSH */
     73  1.1  jtc 	{"!=",	TO_STNEQ },
     74  1.1  jtc 	{"<",	TO_STLT },
     75  1.1  jtc 	{">",	TO_STGT },
     76  1.1  jtc 	{"-eq",	TO_INTEQ },
     77  1.1  jtc 	{"-ne",	TO_INTNE },
     78  1.1  jtc 	{"-gt",	TO_INTGT },
     79  1.1  jtc 	{"-ge",	TO_INTGE },
     80  1.1  jtc 	{"-lt",	TO_INTLT },
     81  1.1  jtc 	{"-le",	TO_INTLE },
     82  1.1  jtc 	{"-ef",	TO_FILEQ },
     83  1.1  jtc 	{"-nt",	TO_FILNT },
     84  1.1  jtc 	{"-ot",	TO_FILOT },
     85  1.1  jtc 	{"",	TO_NONOP }
     86  1.1  jtc     };
     87  1.1  jtc 
     88  1.1  jtc static int	test_stat ARGS((const char *path, struct stat *statb));
     89  1.1  jtc static int	test_eaccess ARGS((const char *path, int mode));
     90  1.1  jtc static int	test_oexpr ARGS((Test_env *te, int do_eval));
     91  1.1  jtc static int	test_aexpr ARGS((Test_env *te, int do_eval));
     92  1.1  jtc static int	test_nexpr ARGS((Test_env *te, int do_eval));
     93  1.1  jtc static int	test_primary ARGS((Test_env *te, int do_eval));
     94  1.1  jtc static int	ptest_isa ARGS((Test_env *te, Test_meta meta));
     95  1.1  jtc static const char *ptest_getopnd ARGS((Test_env *te, Test_op op, int do_eval));
     96  1.1  jtc static int	ptest_eval ARGS((Test_env *te, Test_op op, const char *opnd1,
     97  1.1  jtc 				const char *opnd2, int do_eval));
     98  1.1  jtc static void	ptest_error ARGS((Test_env *te, int offset, const char *msg));
     99  1.1  jtc 
    100  1.1  jtc int
    101  1.1  jtc c_test(wp)
    102  1.1  jtc 	char **wp;
    103  1.1  jtc {
    104  1.1  jtc 	int argc;
    105  1.1  jtc 	int res;
    106  1.1  jtc 	Test_env te;
    107  1.1  jtc 
    108  1.1  jtc 	te.flags = 0;
    109  1.1  jtc 	te.isa = ptest_isa;
    110  1.1  jtc 	te.getopnd = ptest_getopnd;
    111  1.1  jtc 	te.eval = ptest_eval;
    112  1.1  jtc 	te.error = ptest_error;
    113  1.1  jtc 
    114  1.1  jtc 	for (argc = 0; wp[argc]; argc++)
    115  1.1  jtc 		;
    116  1.1  jtc 
    117  1.1  jtc 	if (strcmp(wp[0], "[") == 0) {
    118  1.1  jtc 		if (strcmp(wp[--argc], "]") != 0) {
    119  1.1  jtc 			bi_errorf("missing ]");
    120  1.1  jtc 			return T_ERR_EXIT;
    121  1.1  jtc 		}
    122  1.1  jtc 	}
    123  1.1  jtc 
    124  1.1  jtc 	te.pos.wp = wp + 1;
    125  1.1  jtc 	te.wp_end = wp + argc;
    126  1.1  jtc 
    127  1.1  jtc 	/*
    128  1.1  jtc 	 * Handle the special cases from POSIX.2, section 4.62.4.
    129  1.1  jtc 	 * Implementation of all the rules isn't necessary since
    130  1.1  jtc 	 * our parser does the right thing for the ommited steps.
    131  1.1  jtc 	 */
    132  1.1  jtc 	if (argc <= 5) {
    133  1.1  jtc 		char **owp = wp;
    134  1.1  jtc 		int invert = 0;
    135  1.1  jtc 		Test_op	op;
    136  1.1  jtc 		const char *opnd1, *opnd2;
    137  1.1  jtc 
    138  1.1  jtc 		while (--argc >= 0) {
    139  1.1  jtc 			if ((*te.isa)(&te, TM_END))
    140  1.1  jtc 				return !0;
    141  1.1  jtc 			if (argc == 3) {
    142  1.1  jtc 				opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
    143  1.1  jtc 				if ((op = (Test_op) (*te.isa)(&te, TM_BINOP))) {
    144  1.1  jtc 					opnd2 = (*te.getopnd)(&te, op, 1);
    145  1.1  jtc 					res = (*te.eval)(&te, op, opnd1, opnd2,
    146  1.1  jtc 							1);
    147  1.1  jtc 					if (te.flags & TEF_ERROR)
    148  1.1  jtc 						return T_ERR_EXIT;
    149  1.1  jtc 					if (invert & 1)
    150  1.1  jtc 						res = !res;
    151  1.1  jtc 					return !res;
    152  1.1  jtc 				}
    153  1.1  jtc 				/* back up to opnd1 */
    154  1.1  jtc 				te.pos.wp--;
    155  1.1  jtc 			}
    156  1.1  jtc 			if (argc == 1) {
    157  1.1  jtc 				opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
    158  1.1  jtc 				res = (*te.eval)(&te, TO_STNZE, opnd1,
    159  1.1  jtc 						(char *) 0, 1);
    160  1.1  jtc 				if (invert & 1)
    161  1.1  jtc 					res = !res;
    162  1.1  jtc 				return !res;
    163  1.1  jtc 			}
    164  1.1  jtc 			if ((*te.isa)(&te, TM_NOT)) {
    165  1.1  jtc 				invert++;
    166  1.1  jtc 			} else
    167  1.1  jtc 				break;
    168  1.1  jtc 		}
    169  1.1  jtc 		te.pos.wp = owp + 1;
    170  1.1  jtc 	}
    171  1.1  jtc 
    172  1.1  jtc 	return test_parse(&te);
    173  1.1  jtc }
    174  1.1  jtc 
    175  1.1  jtc /*
    176  1.1  jtc  * Generic test routines.
    177  1.1  jtc  */
    178  1.1  jtc 
    179  1.1  jtc Test_op
    180  1.1  jtc test_isop(te, meta, s)
    181  1.1  jtc 	Test_env *te;
    182  1.1  jtc 	Test_meta meta;
    183  1.1  jtc 	const char *s;
    184  1.1  jtc {
    185  1.1  jtc 	char sc1;
    186  1.1  jtc 	const struct t_op *otab;
    187  1.1  jtc 
    188  1.1  jtc 	otab = meta == TM_UNOP ? u_ops : b_ops;
    189  1.1  jtc 	if (*s) {
    190  1.1  jtc 		sc1 = s[1];
    191  1.1  jtc 		for (; otab->op_text[0]; otab++)
    192  1.1  jtc 			if (sc1 == otab->op_text[1]
    193  1.1  jtc 			    && strcmp(s, otab->op_text) == 0
    194  1.1  jtc 			    && ((te->flags & TEF_DBRACKET)
    195  1.1  jtc 				|| (otab->op_num != TO_STLT
    196  1.1  jtc 				    && otab->op_num != TO_STGT)))
    197  1.1  jtc 				return otab->op_num;
    198  1.1  jtc 	}
    199  1.1  jtc 	return TO_NONOP;
    200  1.1  jtc }
    201  1.1  jtc 
    202  1.1  jtc int
    203  1.1  jtc test_eval(te, op, opnd1, opnd2, do_eval)
    204  1.1  jtc 	Test_env *te;
    205  1.1  jtc 	Test_op op;
    206  1.1  jtc 	const char *opnd1;
    207  1.1  jtc 	const char *opnd2;
    208  1.1  jtc 	int do_eval;
    209  1.1  jtc {
    210  1.1  jtc 	int res;
    211  1.1  jtc 	int not;
    212  1.1  jtc 	struct stat b1, b2;
    213  1.1  jtc 
    214  1.1  jtc 	if (!do_eval)
    215  1.1  jtc 		return 0;
    216  1.1  jtc 
    217  1.1  jtc 	switch ((int) op) {
    218  1.1  jtc 	/*
    219  1.1  jtc 	 * Unary Operators
    220  1.1  jtc 	 */
    221  1.1  jtc 	  case TO_STNZE: /* -n */
    222  1.1  jtc 		return *opnd1 != '\0';
    223  1.1  jtc 	  case TO_STZER: /* -z */
    224  1.1  jtc 		return *opnd1 == '\0';
    225  1.1  jtc 	  case TO_OPTION: /* -o */
    226  1.1  jtc 		if ((not = *opnd1 == '!'))
    227  1.1  jtc 			opnd1++;
    228  1.1  jtc 		if ((res = option(opnd1)) < 0)
    229  1.1  jtc 			res = 0;
    230  1.1  jtc 		else {
    231  1.1  jtc 			res = Flag(res);
    232  1.1  jtc 			if (not)
    233  1.1  jtc 				res = !res;
    234  1.1  jtc 		}
    235  1.1  jtc 		return res;
    236  1.1  jtc 	  case TO_FILRD: /* -r */
    237  1.1  jtc 		return test_eaccess(opnd1, R_OK) == 0;
    238  1.1  jtc 	  case TO_FILWR: /* -w */
    239  1.1  jtc 		return test_eaccess(opnd1, W_OK) == 0;
    240  1.1  jtc 	  case TO_FILEX: /* -x */
    241  1.1  jtc 		return test_eaccess(opnd1, X_OK) == 0;
    242  1.1  jtc 	  case TO_FILAXST: /* -a */
    243  1.1  jtc 		return test_stat(opnd1, &b1) == 0;
    244  1.1  jtc 	  case TO_FILEXST: /* -e */
    245  1.1  jtc 		/* at&t ksh does not appear to do the /dev/fd/ thing for
    246  1.1  jtc 		 * this (unless the os itself handles it)
    247  1.1  jtc 		 */
    248  1.1  jtc 		return stat(opnd1, &b1) == 0;
    249  1.1  jtc 	  case TO_FILREG: /* -r */
    250  1.1  jtc 		return test_stat(opnd1, &b1) == 0 && S_ISREG(b1.st_mode);
    251  1.1  jtc 	  case TO_FILID: /* -d */
    252  1.1  jtc 		return test_stat(opnd1, &b1) == 0 && S_ISDIR(b1.st_mode);
    253  1.1  jtc 	  case TO_FILCDEV: /* -c */
    254  1.1  jtc #ifdef S_ISCHR
    255  1.1  jtc 		return test_stat(opnd1, &b1) == 0 && S_ISCHR(b1.st_mode);
    256  1.1  jtc #else
    257  1.1  jtc 		return 0;
    258  1.1  jtc #endif
    259  1.1  jtc 	  case TO_FILBDEV: /* -b */
    260  1.1  jtc #ifdef S_ISBLK
    261  1.1  jtc 		return test_stat(opnd1, &b1) == 0 && S_ISBLK(b1.st_mode);
    262  1.1  jtc #else
    263  1.1  jtc 		return 0;
    264  1.1  jtc #endif
    265  1.1  jtc 	  case TO_FILFIFO: /* -p */
    266  1.1  jtc #ifdef S_ISFIFO
    267  1.1  jtc 		return test_stat(opnd1, &b1) == 0 && S_ISFIFO(b1.st_mode);
    268  1.1  jtc #else
    269  1.1  jtc 		return 0;
    270  1.1  jtc #endif
    271  1.1  jtc 	  case TO_FILSYM: /* -h -L */
    272  1.1  jtc #ifdef S_ISLNK
    273  1.1  jtc 		return lstat(opnd1, &b1) == 0 && S_ISLNK(b1.st_mode);
    274  1.1  jtc #else
    275  1.1  jtc 		return 0;
    276  1.1  jtc #endif
    277  1.1  jtc 	  case TO_FILSOCK: /* -S */
    278  1.1  jtc #ifdef S_ISSOCK
    279  1.1  jtc 		return test_stat(opnd1, &b1) == 0 && S_ISSOCK(b1.st_mode);
    280  1.1  jtc #else
    281  1.1  jtc 		return 0;
    282  1.1  jtc #endif
    283  1.1  jtc 	  case TO_FILCDF:/* -H HP context dependent files (directories) */
    284  1.1  jtc #ifdef S_ISCDF
    285  1.1  jtc 	  {
    286  1.1  jtc 		/* Append a + to filename and check to see if result is a
    287  1.1  jtc 		 * setuid directory.  CDF stuff in general is hookey, since
    288  1.1  jtc 		 * it breaks for the following sequence: echo hi > foo+;
    289  1.1  jtc 		 * mkdir foo; echo bye > foo/default; chmod u+s foo
    290  1.1  jtc 		 * (foo+ refers to the file with hi in it, there is no way
    291  1.1  jtc 		 * to get at the file with bye in it - please correct me if
    292  1.1  jtc 		 * I'm wrong about this).
    293  1.1  jtc 		 */
    294  1.1  jtc 		int len = strlen(opnd1);
    295  1.1  jtc 		char *p = str_nsave(opnd1, len + 1, ATEMP);
    296  1.1  jtc 
    297  1.1  jtc 		p[len++] = '+';
    298  1.1  jtc 		p[len] = '\0';
    299  1.1  jtc 		return stat(p, &b1) == 0 && S_ISCDF(b1.st_mode);
    300  1.1  jtc 	  }
    301  1.1  jtc #else
    302  1.1  jtc 		return 0;
    303  1.1  jtc #endif
    304  1.1  jtc 	  case TO_FILSETU: /* -u */
    305  1.1  jtc #ifdef S_ISUID
    306  1.1  jtc 		return test_stat(opnd1, &b1) == 0
    307  1.1  jtc 			&& (b1.st_mode & S_ISUID) == S_ISUID;
    308  1.1  jtc #else
    309  1.1  jtc 		return 0;
    310  1.1  jtc #endif
    311  1.1  jtc 	  case TO_FILSETG: /* -g */
    312  1.1  jtc #ifdef S_ISGID
    313  1.1  jtc 		return test_stat(opnd1, &b1) == 0
    314  1.1  jtc 			&& (b1.st_mode & S_ISGID) == S_ISGID;
    315  1.1  jtc #else
    316  1.1  jtc 		return 0;
    317  1.1  jtc #endif
    318  1.1  jtc 	  case TO_FILSTCK: /* -k */
    319  1.1  jtc 		return test_stat(opnd1, &b1) == 0
    320  1.1  jtc 			&& (b1.st_mode & S_ISVTX) == S_ISVTX;
    321  1.1  jtc 	  case TO_FILGZ: /* -s */
    322  1.1  jtc 		return test_stat(opnd1, &b1) == 0 && b1.st_size > 0L;
    323  1.1  jtc 	  case TO_FILTT: /* -t */
    324  1.1  jtc 		if (opnd1 && !bi_getn(opnd1, &res)) {
    325  1.1  jtc 			te->flags |= TEF_ERROR;
    326  1.1  jtc 			res = 0;
    327  1.1  jtc 		} else
    328  1.1  jtc 			res = isatty(opnd1 ? res : 0);
    329  1.1  jtc 		return res;
    330  1.1  jtc 	  case TO_FILUID: /* -O */
    331  1.1  jtc 		return test_stat(opnd1, &b1) == 0 && b1.st_uid == geteuid();
    332  1.1  jtc 	  case TO_FILGID: /* -G */
    333  1.1  jtc 		return test_stat(opnd1, &b1) == 0 && b1.st_gid == getegid();
    334  1.1  jtc 	/*
    335  1.1  jtc 	 * Binary Operators
    336  1.1  jtc 	 */
    337  1.1  jtc 	  case TO_STEQL: /* = */
    338  1.1  jtc 		if (te->flags & TEF_DBRACKET)
    339  1.1  jtc 			return gmatch(opnd1, opnd2, FALSE);
    340  1.1  jtc 		return strcmp(opnd1, opnd2) == 0;
    341  1.1  jtc 	  case TO_STNEQ: /* != */
    342  1.1  jtc 		if (te->flags & TEF_DBRACKET)
    343  1.1  jtc 			return !gmatch(opnd1, opnd2, FALSE);
    344  1.1  jtc 		return strcmp(opnd1, opnd2) != 0;
    345  1.1  jtc 	  case TO_STLT: /* < */
    346  1.1  jtc 		return strcmp(opnd1, opnd2) < 0;
    347  1.1  jtc 	  case TO_STGT: /* > */
    348  1.1  jtc 		return strcmp(opnd1, opnd2) > 0;
    349  1.1  jtc 	  case TO_INTEQ: /* -eq */
    350  1.1  jtc 	  case TO_INTNE: /* -ne */
    351  1.1  jtc 	  case TO_INTGE: /* -ge */
    352  1.1  jtc 	  case TO_INTGT: /* -gt */
    353  1.1  jtc 	  case TO_INTLE: /* -le */
    354  1.1  jtc 	  case TO_INTLT: /* -lt */
    355  1.1  jtc 		{
    356  1.1  jtc 			long v1, v2;
    357  1.1  jtc 
    358  1.1  jtc 			if (!evaluate(opnd1, &v1, TRUE)
    359  1.1  jtc 			    || !evaluate(opnd2, &v2, TRUE))
    360  1.1  jtc 			{
    361  1.1  jtc 				/* error already printed.. */
    362  1.1  jtc 				te->flags |= TEF_ERROR;
    363  1.1  jtc 				return 1;
    364  1.1  jtc 			}
    365  1.1  jtc 			switch ((int) op) {
    366  1.1  jtc 			  case TO_INTEQ:
    367  1.1  jtc 				return v1 == v2;
    368  1.1  jtc 			  case TO_INTNE:
    369  1.1  jtc 				return v1 != v2;
    370  1.1  jtc 			  case TO_INTGE:
    371  1.1  jtc 				return v1 >= v2;
    372  1.1  jtc 			  case TO_INTGT:
    373  1.1  jtc 				return v1 > v2;
    374  1.1  jtc 			  case TO_INTLE:
    375  1.1  jtc 				return v1 <= v2;
    376  1.1  jtc 			  case TO_INTLT:
    377  1.1  jtc 				return v1 < v2;
    378  1.1  jtc 			}
    379  1.1  jtc 		}
    380  1.1  jtc 	  case TO_FILNT: /* -nt */
    381  1.1  jtc 		return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0
    382  1.1  jtc 		       && b1.st_mtime > b2.st_mtime;
    383  1.1  jtc 	  case TO_FILOT: /* -ot */
    384  1.1  jtc 		return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0
    385  1.1  jtc 		       && b1.st_mtime < b2.st_mtime;
    386  1.1  jtc 	  case TO_FILEQ: /* -ef */
    387  1.1  jtc 		return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0
    388  1.1  jtc 		       && b1.st_dev == b2.st_dev
    389  1.1  jtc 		       && b1.st_ino == b2.st_ino;
    390  1.1  jtc 	}
    391  1.1  jtc 	(*te->error)(te, 0, "internal error: unknown op");
    392  1.1  jtc 	return 1;
    393  1.1  jtc }
    394  1.1  jtc 
    395  1.1  jtc /* Nasty kludge to handle Korn's bizarre /dev/fd hack */
    396  1.1  jtc static int
    397  1.1  jtc test_stat(path, statb)
    398  1.1  jtc 	const char *path;
    399  1.1  jtc 	struct stat *statb;
    400  1.1  jtc {
    401  1.1  jtc #if !defined(HAVE_DEV_FD)
    402  1.1  jtc 	int fd;
    403  1.1  jtc 
    404  1.1  jtc 	if (strncmp(path, "/dev/fd/", 8) == 0 && getn(path + 8, &fd))
    405  1.1  jtc 		return fstat(fd, statb);
    406  1.1  jtc #endif /* !HAVE_DEV_FD */
    407  1.1  jtc 
    408  1.1  jtc 	return stat(path, statb);
    409  1.1  jtc }
    410  1.1  jtc 
    411  1.1  jtc /* Another nasty kludge to handle Korn's bizarre /dev/fd hack */
    412  1.1  jtc static int
    413  1.1  jtc test_eaccess(path, mode)
    414  1.1  jtc 	const char *path;
    415  1.1  jtc 	int mode;
    416  1.1  jtc {
    417  1.1  jtc #if !defined(HAVE_DEV_FD)
    418  1.1  jtc 	int fd;
    419  1.1  jtc 
    420  1.1  jtc 	if (strncmp(path, "/dev/fd/", 8) == 0 && getn(path + 8, &fd)) {
    421  1.1  jtc 		int flags;
    422  1.1  jtc 
    423  1.1  jtc 		if ((flags = fcntl(fd, F_GETFL, 0)) < 0
    424  1.1  jtc 		    || (mode & X_OK)
    425  1.1  jtc 		    || ((mode & W_OK) && (flags & O_ACCMODE) == O_RDONLY)
    426  1.1  jtc 		    || ((mode & R_OK) && (flags & O_ACCMODE) == O_WRONLY))
    427  1.1  jtc 			return -1;
    428  1.1  jtc 		return 0;
    429  1.1  jtc 	}
    430  1.1  jtc #endif /* !HAVE_DEV_FD */
    431  1.1  jtc 
    432  1.1  jtc 	return eaccess(path, mode);
    433  1.1  jtc }
    434  1.1  jtc 
    435  1.1  jtc int
    436  1.1  jtc test_parse(te)
    437  1.1  jtc 	Test_env *te;
    438  1.1  jtc {
    439  1.1  jtc 	int res;
    440  1.1  jtc 
    441  1.1  jtc 	res = test_oexpr(te, 1);
    442  1.1  jtc 
    443  1.1  jtc 	if (!(te->flags & TEF_ERROR) && !(*te->isa)(te, TM_END))
    444  1.1  jtc 		(*te->error)(te, 0, "unexpected operator/operand");
    445  1.1  jtc 
    446  1.1  jtc 	return (te->flags & TEF_ERROR) ? T_ERR_EXIT : !res;
    447  1.1  jtc }
    448  1.1  jtc 
    449  1.1  jtc static int
    450  1.1  jtc test_oexpr(te, do_eval)
    451  1.1  jtc 	Test_env *te;
    452  1.1  jtc 	int do_eval;
    453  1.1  jtc {
    454  1.1  jtc 	int res;
    455  1.1  jtc 
    456  1.1  jtc 	res = test_aexpr(te, do_eval);
    457  1.1  jtc 	if (res)
    458  1.1  jtc 		do_eval = 0;
    459  1.1  jtc 	if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_OR))
    460  1.1  jtc 		return test_oexpr(te, do_eval) || res;
    461  1.1  jtc 	return res;
    462  1.1  jtc }
    463  1.1  jtc 
    464  1.1  jtc static int
    465  1.1  jtc test_aexpr(te, do_eval)
    466  1.1  jtc 	Test_env *te;
    467  1.1  jtc 	int do_eval;
    468  1.1  jtc {
    469  1.1  jtc 	int res;
    470  1.1  jtc 
    471  1.1  jtc 	res = test_nexpr(te, do_eval);
    472  1.1  jtc 	if (!res)
    473  1.1  jtc 		do_eval = 0;
    474  1.1  jtc 	if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_AND))
    475  1.1  jtc 		return test_aexpr(te, do_eval) && res;
    476  1.1  jtc 	return res;
    477  1.1  jtc }
    478  1.1  jtc 
    479  1.1  jtc static int
    480  1.1  jtc test_nexpr(te, do_eval)
    481  1.1  jtc 	Test_env *te;
    482  1.1  jtc 	int do_eval;
    483  1.1  jtc {
    484  1.1  jtc 	if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_NOT))
    485  1.1  jtc 		return !test_nexpr(te, do_eval);
    486  1.1  jtc 	return test_primary(te, do_eval);
    487  1.1  jtc }
    488  1.1  jtc 
    489  1.1  jtc static int
    490  1.1  jtc test_primary(te, do_eval)
    491  1.1  jtc 	Test_env *te;
    492  1.1  jtc 	int do_eval;
    493  1.1  jtc {
    494  1.1  jtc 	const char *opnd1, *opnd2;
    495  1.1  jtc 	int res;
    496  1.1  jtc 	Test_op op;
    497  1.1  jtc 
    498  1.1  jtc 	if (te->flags & TEF_ERROR)
    499  1.1  jtc 		return 0;
    500  1.1  jtc 	if ((*te->isa)(te, TM_OPAREN)) {
    501  1.1  jtc 		res = test_oexpr(te, do_eval);
    502  1.1  jtc 		if (te->flags & TEF_ERROR)
    503  1.1  jtc 			return 0;
    504  1.1  jtc 		if (!(*te->isa)(te, TM_CPAREN)) {
    505  1.1  jtc 			(*te->error)(te, 0, "missing closing paren");
    506  1.1  jtc 			return 0;
    507  1.1  jtc 		}
    508  1.1  jtc 		return res;
    509  1.1  jtc 	}
    510  1.1  jtc 	if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) {
    511  1.1  jtc 		/* unary expression */
    512  1.1  jtc 		opnd1 = (*te->getopnd)(te, op, do_eval);
    513  1.1  jtc 		if (!opnd1) {
    514  1.1  jtc 			(*te->error)(te, -1, "missing argument");
    515  1.1  jtc 			return 0;
    516  1.1  jtc 		}
    517  1.1  jtc 
    518  1.1  jtc 		return (*te->eval)(te, op, opnd1, (const char *) 0, do_eval);
    519  1.1  jtc 	}
    520  1.1  jtc 	opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval);
    521  1.1  jtc 	if (!opnd1) {
    522  1.1  jtc 		(*te->error)(te, 0, "expression expected");
    523  1.1  jtc 		return 0;
    524  1.1  jtc 	}
    525  1.1  jtc 	if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) {
    526  1.1  jtc 		/* binary expression */
    527  1.1  jtc 		opnd2 = (*te->getopnd)(te, op, do_eval);
    528  1.1  jtc 		if (!opnd2) {
    529  1.1  jtc 			(*te->error)(te, -1, "missing second argument");
    530  1.1  jtc 			return 0;
    531  1.1  jtc 		}
    532  1.1  jtc 
    533  1.1  jtc 		return (*te->eval)(te, op, opnd1, opnd2, do_eval);
    534  1.1  jtc 	}
    535  1.1  jtc 	if (te->flags & TEF_DBRACKET) {
    536  1.1  jtc 		(*te->error)(te, -1, "missing expression operator");
    537  1.1  jtc 		return 0;
    538  1.1  jtc 	}
    539  1.1  jtc 	return (*te->eval)(te, TO_STNZE, opnd1, (const char *) 0, do_eval);
    540  1.1  jtc }
    541  1.1  jtc 
    542  1.1  jtc /*
    543  1.1  jtc  * Plain test (test and [ .. ]) specific routines.
    544  1.1  jtc  */
    545  1.1  jtc 
    546  1.1  jtc /* Test if the current token is a whatever.  Accepts the current token if
    547  1.1  jtc  * it is.  Returns 0 if it is not, non-zero if it is (in the case of
    548  1.1  jtc  * TM_UNOP and TM_BINOP, the returned value is a Test_op).
    549  1.1  jtc  */
    550  1.1  jtc static int
    551  1.1  jtc ptest_isa(te, meta)
    552  1.1  jtc 	Test_env *te;
    553  1.1  jtc 	Test_meta meta;
    554  1.1  jtc {
    555  1.1  jtc 	/* Order important - indexed by Test_meta values */
    556  1.1  jtc 	static const char *const tokens[] = {
    557  1.1  jtc 				"-o", "-a", "!", "(", ")"
    558  1.1  jtc 			};
    559  1.1  jtc 	int ret;
    560  1.1  jtc 
    561  1.1  jtc 	if (te->pos.wp >= te->wp_end)
    562  1.1  jtc 		return meta == TM_END;
    563  1.1  jtc 
    564  1.1  jtc 	if (meta == TM_UNOP || meta == TM_BINOP)
    565  1.1  jtc 		ret = (int) test_isop(te, meta, *te->pos.wp);
    566  1.1  jtc 	else if (meta == TM_END)
    567  1.1  jtc 		ret = 0;
    568  1.1  jtc 	else
    569  1.1  jtc 		ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0;
    570  1.1  jtc 
    571  1.1  jtc 	/* Accept the token? */
    572  1.1  jtc 	if (ret)
    573  1.1  jtc 		te->pos.wp++;
    574  1.1  jtc 
    575  1.1  jtc 	return ret;
    576  1.1  jtc }
    577  1.1  jtc 
    578  1.1  jtc static const char *
    579  1.1  jtc ptest_getopnd(te, op, do_eval)
    580  1.1  jtc 	Test_env *te;
    581  1.1  jtc 	Test_op op;
    582  1.1  jtc 	int do_eval;
    583  1.1  jtc {
    584  1.1  jtc 	if (te->pos.wp >= te->wp_end)
    585  1.1  jtc 		return op == TO_FILTT ? "1" : (const char *) 0;
    586  1.1  jtc 	return *te->pos.wp++;
    587  1.1  jtc }
    588  1.1  jtc 
    589  1.1  jtc static int
    590  1.1  jtc ptest_eval(te, op, opnd1, opnd2, do_eval)
    591  1.1  jtc 	Test_env *te;
    592  1.1  jtc 	Test_op op;
    593  1.1  jtc 	const char *opnd1;
    594  1.1  jtc 	const char *opnd2;
    595  1.1  jtc 	int do_eval;
    596  1.1  jtc {
    597  1.1  jtc 	return test_eval(te, op, opnd1, opnd2, do_eval);
    598  1.1  jtc }
    599  1.1  jtc 
    600  1.1  jtc static void
    601  1.1  jtc ptest_error(te, offset, msg)
    602  1.1  jtc 	Test_env *te;
    603  1.1  jtc 	int offset;
    604  1.1  jtc 	const char *msg;
    605  1.1  jtc {
    606  1.1  jtc 	const char *op = te->pos.wp + offset >= te->wp_end ?
    607  1.1  jtc 				(const char *) 0 : te->pos.wp[offset];
    608  1.1  jtc 
    609  1.1  jtc 	te->flags |= TEF_ERROR;
    610  1.1  jtc 	if (op)
    611  1.1  jtc 		bi_errorf("%s: %s", op, msg);
    612  1.1  jtc 	else
    613  1.1  jtc 		bi_errorf("%s", msg);
    614  1.1  jtc }
    615