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