Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: behavior of test true -a \( ! -a \)
On 2024-03-25 10:36:13 -0700, Bart Schaefer wrote:
> So in other words you're intentionally breaking this:
>
> % test \( ! -a \) \)
> test: too many arguments
I suppose that with zsh 5.9, zsh sees that -a is a binary primary
(so, no contradictions with POSIX[*] since for the decision of the
matching parenthesis, at least 5 arguments need to be considered,
and this is where the results are unspecified).
[*] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
> In the name of "fixing" this:
>
> % test \) -a \( ! -a \)
>
> If we're arguing here based on spec, POSIX says the below should
> return 1 because $2 is a "binary primary" which takes precedence over
> parens, but it's broken with or without this patch:
>
> % test \( -a \(
> test: parse error
>
> To be fair, /bin/test on MacOS and /usr/bin/test on Ubuntu both choke
> (or not) in exactly those same cases.
In Debian:
With dash 0.5.12-6:
$ test \( -a \( ; echo $?
sh: 1: test: closing paren expected
2
With ksh93u+m 1.0.8-1:
$ test \( -a \( ; echo $?
ksh93: test: argument expected
2
With mksh 59c-35:
$ test \( -a \( ; echo $?
0
With bash 5.2.21-2:
vinc17@qaa:~$ test \( -a \( ; echo $?
0
With coreutils 9.4-3.1:
$ /usr/bin/test \( -a \( ; echo $?
/usr/bin/test: ‘-a’: unary operator expected
2
Note that POSIX says that the results are unspecified if
$1 is '(' and $3 is ')', but here both $1 and $3 are '('.
So the rule is:
If $2 is a binary primary, perform the binary test of $1 and $3.
so that 0 is expected (only mksh and bash are correct).
IMHO, once you have reached a subsequence with at most 4 arguments
(like here), you should apply the POSIX rules. Doing otherwise is
surprising.
Said otherwise, I suppose that the following should work:
* If there are at most 4 arguments, apply the POSIX rules.
* If the first argument is an opening parenthesis, choose a rule
to determine the matching closing parenthesis (say, arg n), or
possibly regard this first argument as a string.
* In the case of a real opening parenthesis, arg n needs to be the
last argument or be followed by -a or -o. Apply the test algorithm
on arg 2 to n-1, and in case of -a or -o, also on arg n+2 to the
last arg (possibly with early termination in the parsing).
* Otherwise:
Choose a rule to determine the operator. Note that the obtained
expression associated with this operator is necessarily followed
by a -a or -o binary primary (say, arg n). Evaluate the expression
and apply the test algorithm on arg n+1 to the last arg (possibly
with early termination in the parsing).
--
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
Messages sorted by:
Reverse Date,
Date,
Thread,
Author