changelog for 0.9.1
[posh.git] / c_test.c
blobdd26f760529182a5bef4a1189d5b5e9a946057a3
1 /*
2 * test(1); version 7-like -- author Erik Baalbergen
3 * modified by Eric Gisin to be used as built-in.
4 * modified by Arnold Robbins to add SVR3 compatibility
5 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
6 * modified by Michael Rendell to add Korn's [[ .. ]] expressions.
7 * modified by J.T. Conklin to add POSIX compatibility.
8 */
10 #include "sh.h"
11 #include "ksh_stat.h"
12 #include "c_test.h"
14 /* test(1) accepts the following grammar:
15 oexpr ::= aexpr | aexpr "-o" oexpr ;
16 aexpr ::= nexpr | nexpr "-a" aexpr ;
17 nexpr ::= primary | "!" nexpr ;
18 primary ::= unary-operator operand
19 | operand binary-operator operand
20 | operand
23 unary-operator ::= "-a"|"-r"|"-w"|"-x"|"-e"|"-f"|"-d"|"-c"|"-b"|"-p"|
24 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|
25 "-L"|"-h"|"-S"|"-H";
27 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
28 "-nt"|"-ot"|"-ef"|"-a"|"-o"|
29 "<"|">" # rules used for [[ .. ]] expressions
31 operand ::= <any thing>
34 #define T_ERR_EXIT 2 /* POSIX says > 1 for errors */
36 struct t_op {
37 char op_text[4];
38 Test_op op_num;
40 static const struct t_op u_ops [] = {
41 #ifdef SILLY_FEATURES
42 {"-a", TO_FILAXST },
43 #endif
44 {"-b", TO_FILBDEV },
45 {"-c", TO_FILCDEV },
46 {"-d", TO_FILID },
47 {"-e", TO_FILEXST },
48 {"-f", TO_FILREG },
49 #ifdef SILLY_FEATURES
50 {"-G", TO_FILGID },
51 #endif
52 {"-g", TO_FILSETG },
53 {"-h", TO_FILSYM },
54 #ifdef SILLY_FEATURES
55 {"-H", TO_FILCDF },
56 {"-k", TO_FILSTCK },
57 #endif
58 {"-L", TO_FILSYM },
59 {"-n", TO_STNZE },
60 #ifdef SILLY_FEATURES
61 {"-O", TO_FILUID },
62 {"-o", TO_OPTION },
63 #endif
64 {"-p", TO_FILFIFO },
65 {"-r", TO_FILRD },
66 {"-s", TO_FILGZ },
67 {"-S", TO_FILSOCK },
68 {"-t", TO_FILTT },
69 {"-u", TO_FILSETU },
70 {"-w", TO_FILWR },
71 {"-x", TO_FILEX },
72 {"-z", TO_STZER },
73 {"", TO_NONOP }
75 static const struct t_op b_ops [] = {
76 {"=", TO_STEQL },
77 #ifdef KSH
78 {"==", TO_STEQL },
79 #endif /* KSH */
80 {"!=", TO_STNEQ },
81 #ifdef SILLY_FEATURES
82 {"<", TO_STLT },
83 {">", TO_STGT },
84 #endif
85 {"-eq", TO_INTEQ },
86 {"-ne", TO_INTNE },
87 {"-gt", TO_INTGT },
88 {"-ge", TO_INTGE },
89 {"-lt", TO_INTLT },
90 {"-le", TO_INTLE },
91 #ifdef SILLY_FEATURES
92 {"-ef", TO_FILEQ },
93 {"-nt", TO_FILNT },
94 {"-ot", TO_FILOT },
95 #endif
96 {"", TO_NONOP }
99 static int test_stat ARGS((const char *path, struct stat *statb));
100 static int test_eaccess ARGS((const char *path, int mode));
101 static int test_oexpr ARGS((Test_env *te, int do_eval));
102 static int test_aexpr ARGS((Test_env *te, int do_eval));
103 static int test_nexpr ARGS((Test_env *te, int do_eval));
104 static int test_primary ARGS((Test_env *te, int do_eval));
105 static int ptest_isa ARGS((Test_env *te, Test_meta meta));
106 static const char *ptest_getopnd ARGS((Test_env *te, Test_op op, int do_eval));
107 static int ptest_eval ARGS((Test_env *te, Test_op op, const char *opnd1,
108 const char *opnd2, int do_eval));
109 static void ptest_error ARGS((Test_env *te, int offset, const char *msg));
112 posh_builtin_test(int argc, char **wp, int UNUSED(flags))
114 int res;
115 Test_env te;
117 te.flags = 0;
118 te.isa = ptest_isa;
119 te.getopnd = ptest_getopnd;
120 te.eval = ptest_eval;
121 te.error = ptest_error;
123 if (strcmp(wp[0], "[") == 0) {
124 if (strcmp(wp[--argc], "]") != 0) {
125 bi_errorf("missing ]");
126 return T_ERR_EXIT;
130 te.pos.wp = wp + 1;
131 te.wp_end = wp + argc;
134 * Handle the special cases from POSIX.2, section 4.62.4.
135 * Implementation of all the rules isn't necessary since
136 * our parser does the right thing for the ommited steps.
138 if (argc <= 5) {
139 char **owp = wp;
140 int invert = 0;
141 Test_op op;
142 const char *opnd1, *opnd2;
144 while (--argc >= 0) {
145 if ((*te.isa)(&te, TM_END))
146 return !0;
147 if (argc == 3) {
148 opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
149 if ((op = (Test_op) (*te.isa)(&te, TM_BINOP))) {
150 opnd2 = (*te.getopnd)(&te, op, 1);
151 res = (*te.eval)(&te, op, opnd1, opnd2,
153 if (te.flags & TEF_ERROR)
154 return T_ERR_EXIT;
155 if (invert & 1)
156 res = !res;
157 return !res;
159 /* back up to opnd1 */
160 te.pos.wp--;
162 if (argc == 1) {
163 opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
164 /* Historically, -t by itself test if fd 1
165 * is a file descriptor, but POSIX says its
166 * a string test...
168 res = (*te.eval)(&te, TO_STNZE, opnd1,
169 (char *) 0, 1);
170 if (invert & 1)
171 res = !res;
172 return !res;
174 if ((*te.isa)(&te, TM_NOT)) {
175 invert++;
176 } else
177 break;
179 te.pos.wp = owp + 1;
182 return test_parse(&te);
186 * Generic test routines.
189 Test_op
190 test_isop(te, meta, s)
191 Test_env *te;
192 Test_meta meta;
193 const char *s;
195 char sc1;
196 const struct t_op *otab;
198 otab = meta == TM_UNOP ? u_ops : b_ops;
199 if (*s) {
200 sc1 = s[1];
201 for (; otab->op_text[0]; otab++)
202 if (sc1 == otab->op_text[1]
203 && strcmp(s, otab->op_text) == 0
204 && ((te->flags & TEF_DBRACKET)
205 || (otab->op_num != TO_STLT
206 && otab->op_num != TO_STGT)))
207 return otab->op_num;
209 return TO_NONOP;
213 test_eval(te, op, opnd1, opnd2, do_eval)
214 Test_env *te;
215 Test_op op;
216 const char *opnd1;
217 const char *opnd2;
218 int do_eval;
220 int res;
221 struct stat b1;
223 if (!do_eval)
224 return 0;
226 switch ((int) op) {
228 * Unary Operators
230 case TO_STNZE: /* -n */
231 return *opnd1 != '\0';
232 case TO_STZER: /* -z */
233 return *opnd1 == '\0';
234 #ifdef SILLY_FEATURES
235 case TO_OPTION: /* -o */
236 if ((not = *opnd1 == '!'))
237 opnd1++;
238 if ((res = option(opnd1)) < 0)
239 res = 0;
240 else {
241 res = Flag(res);
242 if (not)
243 res = !res;
245 return res;
246 #endif /* SILLY_FEATURES */
247 case TO_FILRD: /* -r */
248 return test_eaccess(opnd1, R_OK) == 0;
249 case TO_FILWR: /* -w */
250 return test_eaccess(opnd1, W_OK) == 0;
251 case TO_FILEX: /* -x */
252 return test_eaccess(opnd1, X_OK) == 0;
253 #ifdef SILLY_FEATURES
254 case TO_FILAXST: /* -a */
255 return test_stat(opnd1, &b1) == 0;
256 #endif
257 case TO_FILEXST: /* -e */
258 /* at&t ksh does not appear to do the /dev/fd/ thing for
259 * this (unless the os itself handles it)
261 return stat(opnd1, &b1) == 0;
262 case TO_FILREG: /* -r */
263 return test_stat(opnd1, &b1) == 0 && S_ISREG(b1.st_mode);
264 case TO_FILID: /* -d */
265 return test_stat(opnd1, &b1) == 0 && S_ISDIR(b1.st_mode);
266 case TO_FILCDEV: /* -c */
267 #ifdef S_ISCHR
268 return test_stat(opnd1, &b1) == 0 && S_ISCHR(b1.st_mode);
269 #else
270 return 0;
271 #endif
272 case TO_FILBDEV: /* -b */
273 #ifdef S_ISBLK
274 return test_stat(opnd1, &b1) == 0 && S_ISBLK(b1.st_mode);
275 #else
276 return 0;
277 #endif
278 case TO_FILFIFO: /* -p */
279 #ifdef S_ISFIFO
280 return test_stat(opnd1, &b1) == 0 && S_ISFIFO(b1.st_mode);
281 #else
282 return 0;
283 #endif
284 case TO_FILSYM: /* -h -L */
285 #ifdef S_ISLNK
286 return lstat(opnd1, &b1) == 0 && S_ISLNK(b1.st_mode);
287 #else
288 return 0;
289 #endif
290 case TO_FILSOCK: /* -S */
291 #ifdef S_ISSOCK
292 return test_stat(opnd1, &b1) == 0 && S_ISSOCK(b1.st_mode);
293 #else
294 return 0;
295 #endif
296 #ifdef SILLY_FEATURES
297 case TO_FILCDF:/* -H HP context dependent files (directories) */
298 #ifdef S_ISCDF
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).
308 int len = strlen(opnd1);
309 char *p = str_nsave(opnd1, len + 1, ATEMP);
311 p[len++] = '+';
312 p[len] = '\0';
313 return stat(p, &b1) == 0 && S_ISCDF(b1.st_mode);
315 #else
316 return 0;
317 #endif
318 #endif /* SILLY_FEATURES */
319 case TO_FILSETU: /* -u */
320 #ifdef S_ISUID
321 return test_stat(opnd1, &b1) == 0
322 && (b1.st_mode & S_ISUID) == S_ISUID;
323 #else
324 return 0;
325 #endif
326 case TO_FILSETG: /* -g */
327 #ifdef S_ISGID
328 return test_stat(opnd1, &b1) == 0
329 && (b1.st_mode & S_ISGID) == S_ISGID;
330 #else
331 return 0;
332 #endif
333 #ifdef SILLY_FEATURES
334 case TO_FILSTCK: /* -k */
335 return test_stat(opnd1, &b1) == 0
336 && (b1.st_mode & S_ISVTX) == S_ISVTX;
337 #endif
338 case TO_FILGZ: /* -s */
339 return test_stat(opnd1, &b1) == 0 && b1.st_size > 0L;
340 case TO_FILTT: /* -t */
341 if (opnd1 && !bi_getn(opnd1, &res)) {
342 te->flags |= TEF_ERROR;
343 res = 0;
344 } else {
345 /* generate error if in FPOSIX mode? */
346 res = isatty(opnd1 ? res : 0);
348 return res;
349 #ifdef SILLY_FEATURES
350 case TO_FILUID: /* -O */
351 return test_stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid;
352 case TO_FILGID: /* -G */
353 return test_stat(opnd1, &b1) == 0 && b1.st_gid == getegid();
354 #endif
356 * Binary Operators
358 case TO_STEQL: /* = */
359 if (te->flags & TEF_DBRACKET)
360 return gmatchx(opnd1, opnd2, FALSE);
361 return strcmp(opnd1, opnd2) == 0;
362 case TO_STNEQ: /* != */
363 if (te->flags & TEF_DBRACKET)
364 return !gmatchx(opnd1, opnd2, FALSE);
365 return strcmp(opnd1, opnd2) != 0;
366 #ifdef SILLY_FEATURES
367 case TO_STLT: /* < */
368 return strcmp(opnd1, opnd2) < 0;
369 case TO_STGT: /* > */
370 return strcmp(opnd1, opnd2) > 0;
371 #endif
372 case TO_INTEQ: /* -eq */
373 case TO_INTNE: /* -ne */
374 case TO_INTGE: /* -ge */
375 case TO_INTGT: /* -gt */
376 case TO_INTLE: /* -le */
377 case TO_INTLT: /* -lt */
379 long v1, v2;
381 if (!evaluate(opnd1, &v1, KSH_RETURN_ERROR)
382 || !evaluate(opnd2, &v2, KSH_RETURN_ERROR))
384 /* error already printed.. */
385 te->flags |= TEF_ERROR;
386 return 1;
388 switch ((int) op) {
389 case TO_INTEQ:
390 return v1 == v2;
391 case TO_INTNE:
392 return v1 != v2;
393 case TO_INTGE:
394 return v1 >= v2;
395 case TO_INTGT:
396 return v1 > v2;
397 case TO_INTLE:
398 return v1 <= v2;
399 case TO_INTLT:
400 return v1 < v2;
403 #ifdef SILLY_FEATURES
404 case TO_FILNT: /* -nt */
406 int s2;
407 /* ksh88/ksh93 succeed if file2 can't be stated
408 * (subtly different from `does not exist').
410 return stat(opnd1, &b1) == 0
411 && (((s2 = stat(opnd2, &b2)) == 0
412 && b1.st_mtime > b2.st_mtime) || s2 < 0);
414 case TO_FILOT: /* -ot */
416 int s1;
417 /* ksh88/ksh93 succeed if file1 can't be stated
418 * (subtly different from `does not exist').
420 return stat(opnd2, &b2) == 0
421 && (((s1 = stat(opnd1, &b1)) == 0
422 && b1.st_mtime < b2.st_mtime) || s1 < 0);
424 case TO_FILEQ: /* -ef */
425 return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0
426 && b1.st_dev == b2.st_dev
427 && b1.st_ino == b2.st_ino;
428 #endif /* SILLY_FEATURES */
430 (*te->error)(te, 0, "internal error: unknown op");
431 return 1;
434 /* Nasty kludge to handle Korn's bizarre /dev/fd hack */
435 static int
436 test_stat(path, statb)
437 const char *path;
438 struct stat *statb;
440 #if !defined(HAVE_DEV_FD)
441 int fd;
443 if (strncmp(path, "/dev/fd/", 8) == 0 && getn(path + 8, &fd))
444 return fstat(fd, statb);
445 #endif /* !HAVE_DEV_FD */
447 return stat(path, statb);
450 /* Routine to handle Korn's /dev/fd hack, and to deal with X_OK on
451 * non-directories when running as root.
453 static int
454 test_eaccess(path, mode)
455 const char *path;
456 int mode;
458 int res;
460 #if !defined(HAVE_DEV_FD)
461 int fd;
463 /* Note: doesn't handle //dev/fd, etc.. (this is ok) */
464 if (strncmp(path, "/dev/fd/", 8) == 0 && getn(path + 8, &fd)) {
465 int flags;
467 if ((flags = fcntl(fd, F_GETFL, 0)) < 0
468 || (mode & X_OK)
469 || ((mode & W_OK) && (flags & O_ACCMODE) == O_RDONLY)
470 || ((mode & R_OK) && (flags & O_ACCMODE) == O_WRONLY))
471 return -1;
472 return 0;
474 #endif /* !HAVE_DEV_FD */
476 /* On most (all?) unixes, access() says everything is executable for
477 * root - avoid this on files by using stat().
479 if ((mode & X_OK) && ksheuid == 0) {
480 struct stat statb;
482 if (stat(path, &statb) < 0)
483 res = -1;
484 else if (S_ISDIR(statb.st_mode))
485 res = 0;
486 else
487 res = (statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
488 ? 0 : -1;
489 /* Need to check other permissions? If so, use access() as
490 * this will deal with root on NFS.
492 if (res == 0 && (mode & (R_OK|W_OK)))
493 res = eaccess(path, mode);
494 } else
495 res = eaccess(path, mode);
497 return res;
501 test_parse(te)
502 Test_env *te;
504 int res;
506 res = test_oexpr(te, 1);
508 if (!(te->flags & TEF_ERROR) && !(*te->isa)(te, TM_END))
509 (*te->error)(te, 0, "unexpected operator/operand");
511 return (te->flags & TEF_ERROR) ? T_ERR_EXIT : !res;
514 static int
515 test_oexpr(te, do_eval)
516 Test_env *te;
517 int do_eval;
519 int res;
521 res = test_aexpr(te, do_eval);
522 if (res)
523 do_eval = 0;
524 if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_OR))
525 return test_oexpr(te, do_eval) || res;
526 return res;
529 static int
530 test_aexpr(te, do_eval)
531 Test_env *te;
532 int do_eval;
534 int res;
536 res = test_nexpr(te, do_eval);
537 if (!res)
538 do_eval = 0;
539 if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_AND))
540 return test_aexpr(te, do_eval) && res;
541 return res;
544 static int
545 test_nexpr(te, do_eval)
546 Test_env *te;
547 int do_eval;
549 if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_NOT))
550 return !test_nexpr(te, do_eval);
551 return test_primary(te, do_eval);
554 static int
555 test_primary(Test_env *te, int do_eval)
557 const char *opnd1, *opnd2;
558 Test_op op;
560 if (te->flags & TEF_ERROR)
561 return 0;
562 #ifdef TM_OPAREN
563 if ((*te->isa)(te, TM_OPAREN)) {
564 res = test_oexpr(te, do_eval);
565 if (te->flags & TEF_ERROR)
566 return 0;
567 if (!(*te->isa)(te, TM_CPAREN)) {
568 (*te->error)(te, 0, "missing closing paren");
569 return 0;
571 return res;
573 #endif
574 if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) {
575 /* unary expression */
576 opnd1 = (*te->getopnd)(te, op, do_eval);
577 if (!opnd1) {
578 (*te->error)(te, -1, "missing argument");
579 return 0;
582 return (*te->eval)(te, op, opnd1, (const char *) 0, do_eval);
584 opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval);
585 if (!opnd1) {
586 (*te->error)(te, 0, "expression expected");
587 return 0;
589 if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) {
590 /* binary expression */
591 opnd2 = (*te->getopnd)(te, op, do_eval);
592 if (!opnd2) {
593 (*te->error)(te, -1, "missing second argument");
594 return 0;
597 return (*te->eval)(te, op, opnd1, opnd2, do_eval);
599 if (te->flags & TEF_DBRACKET) {
600 (*te->error)(te, -1, "missing expression operator");
601 return 0;
603 return (*te->eval)(te, TO_STNZE, opnd1, (const char *) 0, do_eval);
607 * Plain test (test and [ .. ]) specific routines.
610 /* Test if the current token is a whatever. Accepts the current token if
611 * it is. Returns 0 if it is not, non-zero if it is (in the case of
612 * TM_UNOP and TM_BINOP, the returned value is a Test_op).
614 static int
615 ptest_isa(te, meta)
616 Test_env *te;
617 Test_meta meta;
619 /* Order important - indexed by Test_meta values */
620 static const char *const tokens[] = {
621 "-o", "-a", "!"
623 int ret;
625 if (te->pos.wp >= te->wp_end)
626 return meta == TM_END;
628 if (meta == TM_UNOP || meta == TM_BINOP)
629 ret = (int) test_isop(te, meta, *te->pos.wp);
630 else if (meta == TM_END)
631 ret = 0;
632 else
633 ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0;
635 /* Accept the token? */
636 if (ret)
637 te->pos.wp++;
639 return ret;
642 static const char *
643 ptest_getopnd(Test_env *te, Test_op op, int UNUSED(do_eval))
645 if (te->pos.wp >= te->wp_end)
646 return op == TO_FILTT ? "1" : (const char *) 0;
647 return *te->pos.wp++;
650 static int
651 ptest_eval(te, op, opnd1, opnd2, do_eval)
652 Test_env *te;
653 Test_op op;
654 const char *opnd1;
655 const char *opnd2;
656 int do_eval;
658 return test_eval(te, op, opnd1, opnd2, do_eval);
661 static void
662 ptest_error(te, offset, msg)
663 Test_env *te;
664 int offset;
665 const char *msg;
667 const char *op = te->pos.wp + offset >= te->wp_end ?
668 (const char *) 0 : te->pos.wp[offset];
670 te->flags |= TEF_ERROR;
671 if (op)
672 bi_errorf("%s: %s", op, msg);
673 else
674 bi_errorf("%s", msg);