zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: dg1727 <dg1727@protonmail.com>
Cc: "zsh-users@zsh.org" <zsh-users@zsh.org>
Subject: Re: Suggestion: Option to ignore unmatched quotes when (Q) parameter-expansion flag
Date: Tue, 19 Apr 2022 17:46:45 -0700	[thread overview]
Message-ID: <CAH+w=7Y1kVW0K_bH_sVnmrmsHLNLgNrWnU1vD_KsA1ptZBr91w@mail.gmail.com> (raw)
In-Reply-To: <Iz0kbyGCbwv0PGnsLHEURh1T-7yKwXK6k-rDmc0b-pmNPXBfa7gVnkU2Eg1ttq56Qfy7iZYIU1jBX0Y0wThgHpkcao5KY8EbdAQ2gkdcNG4=@protonmail.com>

On Wed, Apr 13, 2022 at 7:01 PM dg1727 <dg1727@protonmail.com> wrote:
>
> I prefer that the user be able to use not only backslash quoting, but also other forms of quoting (double quotes, single quotes, dollar single-quoting) to disable the pattern-matching meaning of characters the user may type, such as [].

The problem, as Daniel already touched upon, is that you're trying to
enable a limited form of input syntax rather than using pattern
syntax.  The parameter flags for handling patterns and quoting are not
designed for that.

Jumping ahead a bit ...

> The zshexpn documentation for X says "Without the [X] flag, errors are silently ignored."  It seems that, without (X), the unbalanced (") isn't 'ignored,' but rather causes the (Q) flag to fail entirely.

It's the error that ignored, not the value that produced the error.
That is, yes, the Q flag failed, because it could not remove quoting,
but that didn't cause the surrounding command context to
perceive/report an error state.

A possible way to detect this is to use a test something like [[
"${var}" == "${(Q)var}" ]], which is true only if (Q) did nothing.

> Even with shell option GLOB_SUBST enabled, the only quoting honoured when substituting the contents of a variable into a shell pattern is '\' backslash.

Yes, because that's how patterns are defined.  You're trying to
translate between a different syntax and pattern syntax ... so what
you first need is a parser for your other syntax.  Unmatched quotes
are probably the least of your problems.

> u_input='*"[abc]"*'
> a_string='one[abc]two'

Let's think about what constitutes "a parser for your other syntax".
The [[ ]] operator fits the bill, but it has to parse the contents of
$u_input as the expression, rather than first parsing the expression
and then expanding $u_input.

Fortunately zsh has a trick up its sleeve:  You can create and modify
function definitions by assignment to fields in the $functions special
parameter.  Thus something like this:

zmodload zsh/parameter
functions[funkymatcher]='[[ $1 == '"${u_input}"' ]]'

This will even throw parse errors on most "Bobby Tables" inputs,
although if you want to prevent $(command) substitutions (and
backticks) you'll need to figure that out yourself.  Anyway, with that
you can now call

funkymatcher "${a_string}"

and it will return 0 for a match and nonzero otherwise in exactly the
way you want.


  parent reply	other threads:[~2022-04-20  0:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-13 21:12 dg1727
2022-04-17 19:43 ` Daniel Shahaf
2022-04-20  0:46 ` Bart Schaefer [this message]
2022-04-20  1:28   ` 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='CAH+w=7Y1kVW0K_bH_sVnmrmsHLNLgNrWnU1vD_KsA1ptZBr91w@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=dg1727@protonmail.com \
    --cc=zsh-users@zsh.org \
    --subject='Re: Suggestion: Option to ignore unmatched quotes when (Q) parameter-expansion flag' \
    /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

Code repositories for project(s) associated with this 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).