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