zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-workers@sunsite.dk
Subject: Re: [bug] backslash stripped in sh/ksh emulation
Date: Tue, 11 Oct 2005 12:36:24 +0100	[thread overview]
Message-ID: <20051011123624.70fc9626.pws@csr.com> (raw)
In-Reply-To: <20051011083842.GA5380@sc>

Stephane Chazelas <Stephane_Chazelas@yahoo.fr> wrote:
> $ ARGV0=ksh zsh -xc 'a="\\*"; case $a in *\\*) echo a;; esac'
> + a='\*'
> + case * (*\*)
> 
> Can anyone explain it? It's OK if $a is quoted as in <case "$a">
> 
> After investigation, it appears it is triggered by globsubst.

You're basically pointing out that in:

a='\\*'
print -r ${~a}

the output is

\*

with only a single backslash (I'm assuming nonomatch is set and the pattern
doesn't match a file --- everything is OK if it does).  Yes, this does seem
to be a bug: the string shouldn't be altered if it doesn't match a pattern,
and the use is inconsistent with other shells, which is bad here since
glob_subst is partly designed to provide compatibility.

Fixing it isn't that trivial, given the constraints:

- Nothing else should change, i.e. the myriad other points at which
  quoting takes place shouldn't be affected.
- Also, the quoting effect of the first backslash should be retained
  in pattern matching (in other words, ${~a} in the example should match a
  single backslash).
- zsh removes the so-called "null" arguments, essentially the ghosts
  of quoting characters that are only needed to recreate a printed
  representation of the input string, before pattern matching takes
  place, so restoring it after isn't natural in the current code.

The last one is the really tricky one.  Possible a complete rewrite of the
way quoting is handled would make this all work, but I'm not going down
that road.

So I've introduced a variant of Bnull, the ghost of a backslash, called
Bnullkeep.  This is only inserted in the code used for globsubst, isn't
removed by remnulargs(), and is explicitly ignored by pattern matching.  If
the pattern match failed then untokenize() will restore the backslash to
output the original string.

It's possible there are inconsistencies in that there are places that Bnull
and Bnullkeep are treated the same when they should be different, or vice
versa.  It's also possible that there's a point in the code that expects a
tokenized string but hasn't been taught properly about Bnullkeep, since the
pattern matching is the only part I've modified.  However, none of the
existing tests fail, and the new test I've added succeeds.

Nonetheless, this doesn't give me a warm, fuzzy feeling.  If anyone can
think of places where this might have the wrong effect...

I will not be applying this to 4.2.

(It's just possible we could get away with only using Bnullkeep by suitable
changes, simplifying the code a bit.)

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com


  parent reply	other threads:[~2005-10-11 11:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-11  8:38 Stephane Chazelas
2005-10-11  8:53 ` Stephane Chazelas
2005-10-11 11:36 ` Peter Stephenson [this message]
2005-10-11 11:43   ` Peter Stephenson
2005-10-11 12:57   ` Stephane Chazelas
2005-10-11 13:20     ` Peter Stephenson
2005-10-11 14:45       ` Stephane Chazelas
2005-10-11 14:43   ` Bart Schaefer
2005-10-11 15:06     ` Peter Stephenson

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=20051011123624.70fc9626.pws@csr.com \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /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).