zsh-workers
 help / color / mirror / code / Atom feed
* Pattern matching: backslash escaping not active in pattern passed from variable
@ 2015-05-18  3:07 Martijn Dekker
  2015-05-18  8:33 ` Mikael Magnusson
  0 siblings, 1 reply; 3+ messages in thread
From: Martijn Dekker @ 2015-05-18  3:07 UTC (permalink / raw)
  To: zsh-workers

Peter Stephenson's patch for case pattern matching applied cleanly
against the latest git code and is working perfectly. Many thanks.

There is another snag I've found in pattern matching, which seems to
have survived this patch: backslash escaping does not seem match the
standard if a glob pattern is passed from a variable or parameter. The
standard says:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_01
> A <backslash> character shall escape the following character. The
> escaping <backslash> shall be discarded.

This works correctly with a literal pattern:

case abc in ( a\bc ) echo yes ;; ( * ) echo no ;; esac
Got expected output: yes

But it does *not* work correctly if the pattern is in a variable or
parameter:

glob='a\bc'
case abc in ( $glob ) echo yes ;; ( * ) echo no ;; esac
Expected output: yes (produced by bash, ksh93, dash)
Got output: no (produced by zsh, mksh, ash)

To be fair, existing implementations seem wildly inconsistent here, so
perhaps I'm misreading something. In any case, the inconsistency is
unfortunate. I like to use this simple function so I can write something
like: if match "$somestuff" '*stuff*'; then [...]

match() {
        case "$1" in ( $2 ) return 0 ;; esac
        return 1
}

But the inconsistent handling of backslash escaping in glob patterns
passed from variables or parameters makes it impossible to portably
match a literal '*' or '?' using a method like this. mksh and ash never
match a literal escaped \* and \?; oddly enough, zsh does, but does not
match a (redundantly) escaped alphanumeric character.

In any case, I would expect backslash escaping to work the same way
whether the glob pattern is passed from a variable or parameter or not.

Thanks,

- Martijn


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Pattern matching: backslash escaping not active in pattern passed from variable
  2015-05-18  3:07 Pattern matching: backslash escaping not active in pattern passed from variable Martijn Dekker
@ 2015-05-18  8:33 ` Mikael Magnusson
  2015-05-18  8:49   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Mikael Magnusson @ 2015-05-18  8:33 UTC (permalink / raw)
  To: Martijn Dekker; +Cc: zsh workers

On Mon, May 18, 2015 at 5:07 AM, Martijn Dekker <martijn@inlv.org> wrote:
> Peter Stephenson's patch for case pattern matching applied cleanly
> against the latest git code and is working perfectly. Many thanks.
>
> There is another snag I've found in pattern matching, which seems to
> have survived this patch: backslash escaping does not seem match the
> standard if a glob pattern is passed from a variable or parameter. The
> standard says:
[...]
> In any case, I would expect backslash escaping to work the same way
> whether the glob pattern is passed from a variable or parameter or not.

see the GLOB_SUBST option.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Pattern matching: backslash escaping not active in pattern passed from variable
  2015-05-18  8:33 ` Mikael Magnusson
@ 2015-05-18  8:49   ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2015-05-18  8:49 UTC (permalink / raw)
  To: Mikael Magnusson, zsh workers

On Mon, 18 May 2015 10:33:31 +0200
Mikael Magnusson <mikachu@gmail.com> wrote:
> On Mon, May 18, 2015 at 5:07 AM, Martijn Dekker <martijn@inlv.org> wrote:
> > Peter Stephenson's patch for case pattern matching applied cleanly
> > against the latest git code and is working perfectly. Many thanks.
> >
> > There is another snag I've found in pattern matching, which seems to
> > have survived this patch: backslash escaping does not seem match the
> > standard if a glob pattern is passed from a variable or parameter. The
> > standard says:
> [...]
> > In any case, I would expect backslash escaping to work the same way
> > whether the glob pattern is passed from a variable or parameter or not.
> 
> see the GLOB_SUBST option.

GLOB_SUBST roughly covers the last remark (and GLOB_SUBST is on in POSIX
mode), but not the thing about backslashes --- zsh only removes the
backslashes around pattern characters, not other characters, if it knows
the context is just quoting patterns, which is true of characters made
"live" by the effect of GLOB_SUBST.  That's why the new (b) parameter
expansion flag turned up.

I suppose that ought to be fixable in compatibility mode --- we
certainly don't want to mess around with quoting in native mode.

(I said "roughly" above because there's a big can of worms here about
when quoting happens --- normal backslash quoting happens on input to
the lexer, backslash quoting of patterns much later and in this case
possibly in addition --- but the intention is GLOB_SUBST should at least
make zsh look like other shells, messy though that is.)

pws


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-05-18  8:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-18  3:07 Pattern matching: backslash escaping not active in pattern passed from variable Martijn Dekker
2015-05-18  8:33 ` Mikael Magnusson
2015-05-18  8:49   ` Peter Stephenson

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).