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