Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Suggestion: Option to ignore unmatched quotes when (Q) parameter-expansion flag



On Wed, Apr 13, 2022 at 7:01 PM dg1727 <dg1727@xxxxxxxxxxxxxx> 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.




Messages sorted by: Reverse Date, Date, Thread, Author