zsh-workers
 help / color / mirror / code / Atom feed
From: Vincent Lefevre <vincent@vinc17.net>
To: zsh-workers@zsh.org
Subject: Re: behavior of test true -a \( ! -a \)
Date: Wed, 3 Apr 2024 15:59:20 +0200	[thread overview]
Message-ID: <20240403135920.GA112457@qaa.vinc17.org> (raw)
In-Reply-To: <CAH+w=7bKh_8e9dcLCbMyLCbhevt=R40keXnNgV4Oa9p138rWBw@mail.gmail.com>

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@vinc17.net> - 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)


  reply	other threads:[~2024-04-03 13:59 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-21 10:07 Vincent Lefevre
2024-03-21 10:28 ` Peter Stephenson
2024-03-21 11:04   ` Vincent Lefevre
2024-03-21 11:29     ` Peter Stephenson
2024-03-21 12:18       ` Vincent Lefevre
2024-03-21 12:25         ` Peter Stephenson
2024-03-21 19:06           ` Bart Schaefer
2024-03-22  5:02             ` Bart Schaefer
2024-03-25 16:38       ` Peter Stephenson
2024-03-25 17:36         ` Bart Schaefer
2024-04-03 13:59           ` Vincent Lefevre [this message]
2024-03-21 17:39 ` Bart Schaefer
2024-03-23 21:48   ` Bart Schaefer
2024-03-23 22:20     ` Vincent Lefevre
2024-03-23 22:41       ` Bart Schaefer
2024-03-23 23:33         ` Lawrence Velázquez
2024-03-24  0:14           ` Bart Schaefer
2024-03-24  2:52             ` Lawrence Velázquez
2024-03-25 10:23         ` Vincent Lefevre
2024-03-25 15:21           ` Bart Schaefer
2024-03-25 17:33             ` Vincent Lefevre
2024-03-25 17:43               ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240403135920.GA112457@qaa.vinc17.org \
    --to=vincent@vinc17.net \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).