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

Re: PATCH: pcre callouts



Bart Schaefer wrote:
> You could actually use ${.pcre._} I suppose.  I'm undecided on whether
> that's better.

That still needs braces which makes it harder to embed in a callout.
Perhaps callouts are unlikely to be used other than with functions. If
so, it might just as well be .pcre.subject.

> > and should it perhaps start and end a new scope
> > to make that local?
>
> We do have a precedent for that now with ${|...} creating a scope.

The patch below tries this approach along with your suggestion of using
PM_HIDE for $_. The shell's $_ is not a feature I use much so I may not
be the best person to decide on whether it can be used here.

> > This won't do anything for numeric callouts. They look mostly useful for
> > debugging. They could perhaps call a standard function passing the
> > number and string as parameters.
>
> What's an example of using a number callout outside of zsh?

I'm not finding a whole lot of uses but it isn't entirely easy to
search for. It's fairly common for projects to include the pcre sources
directly within their own source tree so source code searches are just
finding copies of the pcre test code. Some Windows scripting language
called AutoHotKey uses them and has a pcre_callout function which you
need to define. I found one other use which also looked like a language
and also used a function.

> I see you're calling parse_string() here:
>
> > +    if (!block->callout_number &&
> > +           ((prog = parse_string((char *) block->callout_string, 0))))
>
> How are you solving the problem of finding the end of the callout?

That's pcre's problem as it parses the regular expressions. It has
always needed to parse callouts but without the patch, they are ignored.
You can't really use the feature without using shell quoting for the
whole regex. The [[ str =~ regex ]] form can be used without quoting to
a limited extent but that appears to be looking for the matching ")" so
seems sane enough.

Oliver

diff --git a/Src/Modules/pcre.c b/Src/Modules/pcre.c
index e321f18a4..173ab4a69 100644
--- a/Src/Modules/pcre.c
+++ b/Src/Modules/pcre.c
@@ -138,11 +138,20 @@ pcre_callout(pcre2_callout_block_8 *block, void *)
 	    ((prog = parse_string((char *) block->callout_string, 0))))
     {
 	int ef = errflag, lv = lastval;
+	Param pm;
+	char *subject = metafy((char *) block->subject,
+		block->subject_length, META_DUP);
 
-	setsparam(".pcre.subject",
-		metafy((char *) block->subject, block->subject_length, META_DUP));
+	startparamscope();
+	if ((pm = createparam("_", PM_LOCAL|PM_HIDE|PM_UNSET))) {
+	    pm->level = locallevel;
+	    setsparam("_", ztrdup(subject));
+	}
+	setsparam(".pcre.subject", subject);
+	setiparam(".pcre.pos", block->current_position + 1);
 	execode(prog, 1, 0, "pcre");
 	ret = lastval | errflag;
+	endparamscope();
 
 	/* Restore any user interrupt error status */
 	errflag = ef | (errflag & ERRFLAG_INT);




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