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