zsh-workers
 help / color / mirror / code / Atom feed
From: Oliver Kiddle <opk@zsh.org>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh workers <zsh-workers@zsh.org>
Subject: Re: PATCH: pcre callouts
Date: Wed, 01 Nov 2023 03:04:58 +0100	[thread overview]
Message-ID: <31963-1698804298.865464@oYlZ.u4GW.p43I> (raw)
In-Reply-To: <CAH+w=7bNAmZP3x+4n1qYCQBQiTM+9TZgTumVXnAftoeFcHcToA@mail.gmail.com>

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


  parent reply	other threads:[~2023-11-01  2:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-31  0:04 Oliver Kiddle
2023-10-31  3:26 ` Bart Schaefer
2023-10-31  3:40   ` Bart Schaefer
2023-10-31 13:31     ` Mikael Magnusson
2023-10-31 15:57       ` Bart Schaefer
2023-11-01  2:04   ` Oliver Kiddle [this message]
2023-11-03  3:47     ` Bart Schaefer
2023-11-03  9:50       ` Oliver Kiddle
2023-11-04 20:57         ` 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=31963-1698804298.865464@oYlZ.u4GW.p43I \
    --to=opk@zsh.org \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /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).