Imported from ../bash-2.05.tar.gz.
[platform/upstream/bash.git] / test.c
1 /* GNU test program (ksb and mjb) */
2
3 /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
4
5 /* Copyright (C) 1987, 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
6
7    This file is part of GNU Bash, the Bourne Again SHell.
8
9    Bash is free software; you can redistribute it and/or modify it under
10    the terms of the GNU General Public License as published by the Free
11    Software Foundation; either version 2, or (at your option) any later
12    version.
13
14    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
15    WARRANTY; without even the implied warranty of MERCHANTABILITY or
16    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17    for more details.
18
19    You should have received a copy of the GNU General Public License along
20    with Bash; see the file COPYING.  If not, write to the Free Software
21    Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22
23 /* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching
24    binary operators. */
25 /* #define PATTERN_MATCHING */
26
27 #if defined (HAVE_CONFIG_H)
28 #  include <config.h>
29 #endif
30
31 #include <stdio.h>
32
33 #include "bashtypes.h"
34
35 #if defined (HAVE_LIMITS_H)
36 #  include <limits.h>
37 #else
38 #  include <sys/param.h>
39 #endif
40
41 #if defined (HAVE_UNISTD_H)
42 #  include <unistd.h>
43 #endif
44
45 #include <errno.h>
46 #if !defined (errno)
47 extern int errno;
48 #endif /* !errno */
49
50 #if !defined (_POSIX_VERSION)
51 #  include <sys/file.h>
52 #endif /* !_POSIX_VERSION */
53 #include "posixstat.h"
54 #include "filecntl.h"
55
56 #include "shell.h"
57 #include "pathexp.h"
58 #include "test.h"
59 #include "builtins/common.h"
60
61 #include <glob/fnmatch.h>
62
63 #if !defined (STRLEN)
64 #  define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
65 #endif
66
67 #if !defined (STREQ)
68 #  define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
69 #endif /* !STREQ */
70
71 #if !defined (R_OK)
72 #define R_OK 4
73 #define W_OK 2
74 #define X_OK 1
75 #define F_OK 0
76 #endif /* R_OK */
77
78 #define EQ      0
79 #define NE      1
80 #define LT      2
81 #define GT      3
82 #define LE      4
83 #define GE      5
84
85 #define NT      0
86 #define OT      1
87 #define EF      2
88
89 /* The following few defines control the truth and false output of each stage.
90    TRUE and FALSE are what we use to compute the final output value.
91    SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
92    Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
93 #define TRUE 1
94 #define FALSE 0
95 #define SHELL_BOOLEAN(value) (!(value))
96
97 #define TEST_ERREXIT_STATUS     2
98
99 static procenv_t test_exit_buf;
100 static int test_error_return;
101 #define test_exit(val) \
102         do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
103
104 /* We have to use access(2) for machines running AFS, because it's
105    not a Unix file system.  This may produce incorrect answers for
106    non-AFS files.  I hate AFS. */
107 #if defined (AFS)
108 #  define EACCESS(path, mode)   access(path, mode)
109 #else
110 #  define EACCESS(path, mode)   test_eaccess(path, mode)
111 #endif /* AFS */
112
113 static int pos;         /* The offset of the current argument in ARGV. */
114 static int argc;        /* The number of arguments present in ARGV. */
115 static char **argv;     /* The argument list. */
116 static int noeval;
117
118 static int unary_operator ();
119 static int binary_operator ();
120 static int two_arguments ();
121 static int three_arguments ();
122 static int posixtest ();
123
124 static int expr ();
125 static int term ();
126 static int and ();
127 static int or ();
128
129 static void beyond ();
130
131 static void
132 test_syntax_error (format, arg)
133      char *format, *arg;
134 {
135   extern int interactive_shell;
136   extern char *get_name_for_error ();
137   if (interactive_shell == 0)
138     fprintf (stderr, "%s: ", get_name_for_error ());
139   fprintf (stderr, "%s: ", argv[0]);
140   fprintf (stderr, format, arg);
141   fprintf (stderr, "\n");
142   fflush (stderr);
143   test_exit (TEST_ERREXIT_STATUS);
144 }
145
146 /*
147  * beyond - call when we're beyond the end of the argument list (an
148  *      error condition)
149  */
150 static void
151 beyond ()
152 {
153   test_syntax_error ("argument expected", (char *)NULL);
154 }
155
156 /* Syntax error for when an integer argument was expected, but
157    something else was found. */
158 static void
159 integer_expected_error (pch)
160      char *pch;
161 {
162   test_syntax_error ("%s: integer expression expected", pch);
163 }
164
165 /* A wrapper for stat () which disallows pathnames that are empty strings
166    and handles /dev/fd emulation on systems that don't have it. */
167 static int
168 test_stat (path, finfo)
169      char *path;
170      struct stat *finfo;
171 {
172   if (*path == '\0')
173     {
174       errno = ENOENT;
175       return (-1);
176     }
177   if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0)
178     {
179 #if !defined (HAVE_DEV_FD)
180       long fd;
181       if (legal_number (path + 8, &fd))
182         return (fstat ((int)fd, finfo));
183       else
184         {
185           errno = EBADF;
186           return (-1);
187         }
188 #else
189   /* If HAVE_DEV_FD is defined, DEV_FD_PREFIX is defined also, and has a
190      trailing slash.  Make sure /dev/fd/xx really uses DEV_FD_PREFIX/xx.
191      On most systems, with the notable exception of linux, this is
192      effectively a no-op. */
193       char pbuf[32];
194       strcpy (pbuf, DEV_FD_PREFIX);
195       strcat (pbuf, path + 8);
196       return (stat (pbuf, finfo));
197 #endif /* !HAVE_DEV_FD */
198     }
199 #if !defined (HAVE_DEV_STDIN)
200   else if (STREQN (path, "/dev/std", 8))
201     {
202       if (STREQ (path+8, "in"))
203         return (fstat (0, finfo));
204       else if (STREQ (path+8, "out"))
205         return (fstat (1, finfo));
206       else if (STREQ (path+8, "err"))
207         return (fstat (2, finfo));
208       else
209         return (stat (path, finfo));
210     }
211 #endif /* !HAVE_DEV_STDIN */
212   return (stat (path, finfo));
213 }
214
215 /* Do the same thing access(2) does, but use the effective uid and gid,
216    and don't make the mistake of telling root that any file is
217    executable. */
218 int
219 test_eaccess (path, mode)
220      char *path;
221      int mode;
222 {
223   struct stat st;
224
225   if (test_stat (path, &st) < 0)
226     return (-1);
227
228   if (current_user.euid == 0)
229     {
230       /* Root can read or write any file. */
231       if (mode != X_OK)
232         return (0);
233
234       /* Root can execute any file that has any one of the execute
235          bits set. */
236       if (st.st_mode & S_IXUGO)
237         return (0);
238     }
239
240   if (st.st_uid == current_user.euid)   /* owner */
241     mode <<= 6;
242   else if (group_member (st.st_gid))
243     mode <<= 3;
244
245   if (st.st_mode & mode)
246     return (0);
247
248   errno = EACCES;
249   return (-1);
250 }
251
252 /* Increment our position in the argument list.  Check that we're not
253    past the end of the argument list.  This check is supressed if the
254    argument is FALSE.  Made a macro for efficiency. */
255 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
256 #define unary_advance() do { advance (1); ++pos; } while (0)
257
258 /*
259  * expr:
260  *      or
261  */
262 static int
263 expr ()
264 {
265   if (pos >= argc)
266     beyond ();
267
268   return (FALSE ^ or ());               /* Same with this. */
269 }
270
271 /*
272  * or:
273  *      and
274  *      and '-o' or
275  */
276 static int
277 or ()
278 {
279   int value, v2;
280
281   value = and ();
282   while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
283     {
284       advance (0);
285       v2 = or ();
286       return (value || v2);
287     }
288
289   return (value);
290 }
291
292 /*
293  * and:
294  *      term
295  *      term '-a' and
296  */
297 static int
298 and ()
299 {
300   int value, v2;
301
302   value = term ();
303   while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
304     {
305       advance (0);
306       v2 = and ();
307       return (value && v2);
308     }
309   return (value);
310 }
311
312 /*
313  * term - parse a term and return 1 or 0 depending on whether the term
314  *      evaluates to true or false, respectively.
315  *
316  * term ::=
317  *      '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
318  *      '-'('G'|'L'|'O'|'S'|'N') filename
319  *      '-t' [int]
320  *      '-'('z'|'n') string
321  *      '-o' option
322  *      string
323  *      string ('!='|'='|'==') string
324  *      <int> '-'(eq|ne|le|lt|ge|gt) <int>
325  *      file '-'(nt|ot|ef) file
326  *      '(' <expr> ')'
327  * int ::=
328  *      positive and negative integers
329  */
330 static int
331 term ()
332 {
333   int value;
334
335   if (pos >= argc)
336     beyond ();
337
338   /* Deal with leading `not's. */
339   if (argv[pos][0] == '!' && argv[pos][1] == '\0')
340     {
341       value = 0;
342       while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
343         {
344           advance (1);
345           value = 1 - value;
346         }
347
348       return (value ? !term() : term());
349     }
350
351   /* A paren-bracketed argument. */
352   if (argv[pos][0] == '(' && argv[pos][1] == '\0')
353     {
354       advance (1);
355       value = expr ();
356       if (argv[pos] == 0)
357         test_syntax_error ("`)' expected", (char *)NULL);
358       else if (argv[pos][0] != ')' || argv[pos][1])
359         test_syntax_error ("`)' expected, found %s", argv[pos]);
360       advance (0);
361       return (value);
362     }
363
364   /* are there enough arguments left that this could be dyadic? */
365   if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
366     value = binary_operator ();
367
368   /* Might be a switch type argument */
369   else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
370     {
371       if (test_unop (argv[pos]))
372         value = unary_operator ();
373       else
374         test_syntax_error ("%s: unary operator expected", argv[pos]);
375     }
376   else
377     {
378       value = argv[pos][0] != '\0';
379       advance (0);
380     }
381
382   return (value);
383 }
384
385 static int
386 filecomp (s, t, op)
387      char *s, *t;
388      int op;
389 {
390   struct stat st1, st2;
391
392   if (test_stat (s, &st1) < 0)
393     {
394       st1.st_mtime = 0;
395       if (op == EF)
396         return (FALSE);
397     }
398   if (test_stat (t, &st2) < 0)
399     {
400       st2.st_mtime = 0;
401       if (op == EF)
402         return (FALSE);
403     }
404   
405   switch (op)
406     {
407     case OT: return (st1.st_mtime < st2.st_mtime);
408     case NT: return (st1.st_mtime > st2.st_mtime);
409     case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
410     }
411   return (FALSE);
412 }
413
414 static int
415 arithcomp (s, t, op, flags)
416      char *s, *t;
417      int op, flags;
418 {
419   long l, r;
420   int expok;
421
422   if (flags & TEST_ARITHEXP)
423     {
424       l = evalexp (s, &expok);
425       if (expok == 0)
426         return (FALSE);         /* should probably longjmp here */
427       r = evalexp (t, &expok);
428       if (expok == 0)
429         return (FALSE);         /* ditto */
430     }
431   else
432     {
433       if (legal_number (s, &l) == 0)
434         integer_expected_error (s);
435       if (legal_number (t, &r) == 0)
436         integer_expected_error (t);
437     }
438
439   switch (op)
440     {
441     case EQ: return (l == r);
442     case NE: return (l != r);
443     case LT: return (l < r);
444     case GT: return (l > r);
445     case LE: return (l <= r);
446     case GE: return (l >= r);
447     }
448
449   return (FALSE);
450 }
451
452 static int
453 patcomp (string, pat, op)
454      char *string, *pat;
455      int op;
456 {
457   int m;
458
459   m = fnmatch (pat, string, FNMATCH_EXTFLAG);
460   return ((op == EQ) ? (m == 0) : (m != 0));
461 }
462
463 int
464 binary_test (op, arg1, arg2, flags)
465      char *op, *arg1, *arg2;
466      int flags;
467 {
468   int patmatch;
469
470   patmatch = (flags & TEST_PATMATCH);
471
472   if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
473     return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
474
475   else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
476     return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
477
478   else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
479     return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
480
481   else if (op[2] == 't')
482     {
483       switch (op[1])
484         {
485         case 'n': return (filecomp (arg1, arg2, NT));           /* -nt */
486         case 'o': return (filecomp (arg1, arg2, OT));           /* -ot */
487         case 'l': return (arithcomp (arg1, arg2, LT, flags));   /* -lt */
488         case 'g': return (arithcomp (arg1, arg2, GT, flags));   /* -gt */
489         }
490     }
491   else if (op[1] == 'e')
492     {
493       switch (op[2])
494         {
495         case 'f': return (filecomp (arg1, arg2, EF));           /* -ef */
496         case 'q': return (arithcomp (arg1, arg2, EQ, flags));   /* -eq */
497         }
498     }
499   else if (op[2] == 'e')
500     {
501       switch (op[1])
502         {
503         case 'n': return (arithcomp (arg1, arg2, NE, flags));   /* -ne */
504         case 'g': return (arithcomp (arg1, arg2, GE, flags));   /* -ge */
505         case 'l': return (arithcomp (arg1, arg2, LE, flags));   /* -le */
506         }
507     }
508
509   return (FALSE);       /* should never get here */
510 }
511
512
513 static int
514 binary_operator ()
515 {
516   int value;
517   char *w;
518
519   w = argv[pos + 1];
520   if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
521       ((w[0] == '>' || w[0] == '<') && w[1] == '\0') ||         /* <, > */
522       (w[0] == '!' && w[1] == '=' && w[2] == '\0'))             /* != */
523     {
524       value = binary_test (w, argv[pos], argv[pos + 2], 0);
525       pos += 3;
526       return (value);
527     }
528
529 #if defined (PATTERN_MATCHING)
530   if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
531     {
532       value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
533       pos += 3;
534       return (value);
535     }
536 #endif
537
538   if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
539     {
540       test_syntax_error ("%s: binary operator expected", w);
541       /* NOTREACHED */
542       return (FALSE);
543     }
544
545   value = binary_test (w, argv[pos], argv[pos + 2], 0);
546   pos += 3;
547   return value;
548 }
549
550 static int
551 unary_operator ()
552 {
553   char *op, *arg;
554   long r;
555
556   op = argv[pos];
557   if (test_unop (op) == 0)
558     return (FALSE);
559
560   /* the only tricky case is `-t', which may or may not take an argument. */
561   if (op[1] == 't')
562     {
563       advance (0);
564       if (pos < argc)
565         {
566           if (legal_number (argv[pos], &r))
567             {
568               advance (0);
569               return (unary_test (op, argv[pos - 1]));
570             }
571           else
572             return (FALSE);
573         }
574       else
575         return (unary_test (op, "1"));
576     }
577
578   /* All of the unary operators take an argument, so we first call
579      unary_advance (), which checks to make sure that there is an
580      argument, and then advances pos right past it.  This means that
581      pos - 1 is the location of the argument. */
582   unary_advance ();
583   return (unary_test (op, argv[pos - 1]));
584 }
585
586 int
587 unary_test (op, arg)
588      char *op, *arg;
589 {
590   long r;
591   struct stat stat_buf;
592      
593   switch (op[1])
594     {
595     case 'a':                   /* file exists in the file system? */
596     case 'e':
597       return (test_stat (arg, &stat_buf) == 0);
598
599     case 'r':                   /* file is readable? */
600       return (EACCESS (arg, R_OK) == 0);
601
602     case 'w':                   /* File is writeable? */
603       return (EACCESS (arg, W_OK) == 0);
604
605     case 'x':                   /* File is executable? */
606       return (EACCESS (arg, X_OK) == 0);
607
608     case 'O':                   /* File is owned by you? */
609       return (test_stat (arg, &stat_buf) == 0 &&
610               (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
611
612     case 'G':                   /* File is owned by your group? */
613       return (test_stat (arg, &stat_buf) == 0 &&
614               (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
615
616     case 'N':
617       return (test_stat (arg, &stat_buf) == 0 &&
618               stat_buf.st_atime <= stat_buf.st_mtime);
619
620     case 'f':                   /* File is a file? */
621       if (test_stat (arg, &stat_buf) < 0)
622         return (FALSE);
623
624       /* -f is true if the given file exists and is a regular file. */
625 #if defined (S_IFMT)
626       return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
627 #else
628       return (S_ISREG (stat_buf.st_mode));
629 #endif /* !S_IFMT */
630
631     case 'd':                   /* File is a directory? */
632       return (test_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
633
634     case 's':                   /* File has something in it? */
635       return (test_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
636
637     case 'S':                   /* File is a socket? */
638 #if !defined (S_ISSOCK)
639       return (FALSE);
640 #else
641       return (test_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
642 #endif /* S_ISSOCK */
643
644     case 'c':                   /* File is character special? */
645       return (test_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
646
647     case 'b':                   /* File is block special? */
648       return (test_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
649
650     case 'p':                   /* File is a named pipe? */
651 #ifndef S_ISFIFO
652       return (FALSE);
653 #else
654       return (test_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
655 #endif /* S_ISFIFO */
656
657     case 'L':                   /* Same as -h  */
658     case 'h':                   /* File is a symbolic link? */
659 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
660       return (FALSE);
661 #else
662       return ((arg[0] != '\0') &&
663               (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
664 #endif /* S_IFLNK && HAVE_LSTAT */
665
666     case 'u':                   /* File is setuid? */
667       return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
668
669     case 'g':                   /* File is setgid? */
670       return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
671
672     case 'k':                   /* File has sticky bit set? */
673 #if !defined (S_ISVTX)
674       /* This is not Posix, and is not defined on some Posix systems. */
675       return (FALSE);
676 #else
677       return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
678 #endif
679
680     case 't':   /* File fd is a terminal? */
681       if (legal_number (arg, &r) == 0)
682         return (FALSE);
683       return (isatty ((int)r));
684
685     case 'n':                   /* True if arg has some length. */
686       return (arg[0] != '\0');
687
688     case 'z':                   /* True if arg has no length. */
689       return (arg[0] == '\0');
690
691     case 'o':                   /* True if option `arg' is set. */
692       return (minus_o_option_value (arg) == 1);
693     }
694 }
695
696 /* Return TRUE if OP is one of the test command's binary operators. */
697 int
698 test_binop (op)
699      char *op;
700 {
701   if (op[0] == '=' && op[1] == '\0')
702     return (1);         /* '=' */
703   else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0')  /* string <, > */
704     return (1);
705   else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
706     return (1);         /* `==' and `!=' */
707 #if defined (PATTERN_MATCHING)
708   else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
709     return (1);
710 #endif
711   else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
712     return (0);
713   else
714     {
715       if (op[2] == 't')
716         switch (op[1])
717           {
718           case 'n':             /* -nt */
719           case 'o':             /* -ot */
720           case 'l':             /* -lt */
721           case 'g':             /* -gt */
722             return (1);
723           default:
724             return (0);
725           }
726       else if (op[1] == 'e')
727         switch (op[2])
728           {
729           case 'q':             /* -eq */
730           case 'f':             /* -ef */
731             return (1);
732           default:
733             return (0);
734           }
735       else if (op[2] == 'e')
736         switch (op[1])
737           {
738           case 'n':             /* -ne */
739           case 'g':             /* -ge */
740           case 'l':             /* -le */
741             return (1);
742           default:
743             return (0);
744           }
745       else
746         return (0);
747     }
748 }
749
750 /* Return non-zero if OP is one of the test command's unary operators. */
751 int
752 test_unop (op)
753      char *op;
754 {
755   if (op[0] != '-')
756     return (0);
757
758   switch (op[1])
759     {
760     case 'a': case 'b': case 'c': case 'd': case 'e':
761     case 'f': case 'g': case 'h': case 'k': case 'n':
762     case 'o': case 'p': case 'r': case 's': case 't':
763     case 'u': case 'w': case 'x': case 'z':
764     case 'G': case 'L': case 'O': case 'S': case 'N':
765       return (1);
766     }
767
768   return (0);
769 }
770
771 static int
772 two_arguments ()
773 {
774   if (argv[pos][0] == '!' && argv[pos][1] == '\0')
775     return (argv[pos + 1][0] == '\0');
776   else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
777     {
778       if (test_unop (argv[pos]))
779         return (unary_operator ());
780       else
781         test_syntax_error ("%s: unary operator expected", argv[pos]);
782     }
783   else
784     test_syntax_error ("%s: unary operator expected", argv[pos]);
785
786   return (0);
787 }
788
789 #define ANDOR(s)  (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
790
791 #define ONE_ARG_TEST(s)         ((s)[0] != '\0')
792
793 static int
794 three_arguments ()
795 {
796   int value;
797
798   if (test_binop (argv[pos+1]))
799     {
800       value = binary_operator ();
801       pos = argc;
802     }
803   else if (ANDOR (argv[pos+1]))
804     {
805       if (argv[pos+1][1] == 'a')
806         value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
807       else
808         value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
809       pos = argc;
810     }
811   else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
812     {
813       advance (1);
814       value = !two_arguments ();
815     }
816   else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
817     {
818       value = ONE_ARG_TEST(argv[pos+1]);
819       pos = argc;
820     }
821   else
822     test_syntax_error ("%s: binary operator expected", argv[pos+1]);
823
824   return (value);
825 }
826
827 /* This is an implementation of a Posix.2 proposal by David Korn. */
828 static int
829 posixtest ()
830 {
831   int value;
832
833   switch (argc - 1)     /* one extra passed in */
834     {
835       case 0:
836         value = FALSE;
837         pos = argc;
838         break;
839
840       case 1:
841         value = ONE_ARG_TEST(argv[1]);
842         pos = argc;
843         break;
844
845       case 2:
846         value = two_arguments ();
847         pos = argc;
848         break;
849
850       case 3:
851         value = three_arguments ();
852         break;
853
854       case 4:
855         if (argv[pos][0] == '!' && argv[pos][1] == '\0')
856           {
857             advance (1);
858             value = !three_arguments ();
859             break;
860           }
861         /* FALLTHROUGH */
862       default:
863         value = expr ();
864     }
865
866   return (value);
867 }
868
869 /*
870  * [:
871  *      '[' expr ']'
872  * test:
873  *      test expr
874  */
875 int
876 test_command (margc, margv)
877      int margc;
878      char **margv;
879 {
880   int value;
881
882   int code;
883
884   code = setjmp (test_exit_buf);
885
886   if (code)
887     return (test_error_return);
888
889   argv = margv;
890
891   if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
892     {
893       --margc;
894
895       if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
896         test_syntax_error ("missing `]'", (char *)NULL);
897
898       if (margc < 2)
899         test_exit (SHELL_BOOLEAN (FALSE));
900     }
901
902   argc = margc;
903   pos = 1;
904
905   if (pos >= argc)
906     test_exit (SHELL_BOOLEAN (FALSE));
907
908   noeval = 0;
909   value = posixtest ();
910
911   if (pos != argc)
912     test_syntax_error ("too many arguments", (char *)NULL);
913
914   test_exit (SHELL_BOOLEAN (value));
915 }