zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Support the mksh's ${|func;} substitution
@ 2019-09-06  0:52 Sebastian Gniazdowski
  2019-09-06  0:54 ` Sebastian Gniazdowski
                   ` (2 more replies)
  0 siblings, 3 replies; 32+ messages in thread
From: Sebastian Gniazdowski @ 2019-09-06  0:52 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 869 bytes --]

Hello
Some notes on the patch:
- the subst is being handled at top of paramsubst, because it's made
uncompatible with (...)-flags (${|(U)funct;} looks awful, more on this
in the patch),
- then the `s' variable is advanced past the semicolon, so that the
rest of the function can safely progress doing (almost) nothing
- one thing that the function still does is a fetchvalue, which I
prevent from setting vunset to 1 in case of ${|func;}

If commited, the substitution will be super useful in // substitution. E.g.:

arr=( val1 val2 abc1 abc3 )
func() { REPLY="${(C)match[1]}"; }
print -rl ${arr[@]//(#b)(*)/${|func;}}

Output:
Val1
Val2
Abc1
Abc3

PS. I did install mksh and test the substitution, it works the same.
-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

[-- Attachment #2: 0001-Support-the-mksh-s-substitution-func.patch.txt --]
[-- Type: text/plain, Size: 3021 bytes --]

From fb4744562d2f03246288da2692559d7f9c4014f2 Mon Sep 17 00:00:00 2001
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date: Fri, 6 Sep 2019 02:35:14 +0200
Subject: [PATCH] Support the mksh's substitution ${|func;}

---
 Src/subst.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/Src/subst.c b/Src/subst.c
index b132f251b..80a572e17 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -29,6 +29,7 @@
 
 #include "zsh.mdh"
 #include "subst.pro"
+#include "exec.pro"
 
 #define LF_ARRAY	1
 
@@ -1847,8 +1848,17 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
      * nested (P) flags.
      */
     int fetch_needed;
+    /* Indicates ${|func;} */
+    int rplyfunc = 0;
+    /* The name of the function to be ran by ${|...;} */
+    char *cmdarg = NULL;
+    /* The length of the input string */
+    int slen = 0;
+    /* The closing brace pointer */
+    char *outbracep;
 
     *s++ = '\0';
+    slen = strlen(s);
     /*
      * Nothing to do unless the character following the $ is
      * something we recognise.
@@ -1876,6 +1886,41 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
     if (c == Inbrace) {
 	inbrace = 1;
 	s++;
+
+        /* Short-path for the function-running substitution ${|func;}
+         * The function name is extracted and called, and the
+         * substitution assigned. There's no (...)-flags processing,
+         * i.e. no ${|(U)func;}, because it looks quite awful and
+         * also requires a change to the manual, part about the
+         * substitution order. Use ${(U)${|func;}} instead, it looks
+         * cleaner. */
+        if ( ((outbracep=strchr(s,Outbrace)) ||
+             (outbracep=strchr(s,'}'))) &&
+                (s[0] == Bar || s[0] == '|') &&
+                    outbracep[-1] == ';' )
+        {
+            rplyfunc = 1;
+            cmdarg = dupstrpfx(s+1, outbracep-s-2);
+            s=outbracep;
+
+            HashNode hn = NULL;
+            if( (hn = shfunctab->getnode(shfunctab, cmdarg)) ) {
+                /* Execute the shell function */
+                doshfunc((Shfunc) hn, NULL, 1);
+                val = getsparam("REPLY");
+                if (val)
+                    vunset = 0;
+                else {
+                    vunset = 1;
+                    val = dupstring("");
+                }
+            } else {
+                zerr("no such function: %s", cmdarg);
+                return NULL;
+            }
+            fetch_needed = 0;
+        }
+
 	/*
 	 * In ksh emulation a leading `!' is a special flag working
 	 * sort of like our (k).
@@ -2519,7 +2564,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 			     scanflags)) ||
 	    (v->pm && (v->pm->node.flags & PM_UNSET)) ||
 	    (v->flags & VALFLAG_EMPTY))
-	    vunset = 1;
+        {
+            if (!rplyfunc) {
+                vunset = 1;
+            }
+        }
 
 	if (wantt) {
 	    /*
-- 
2.21.0


^ permalink raw reply	[flat|nested] 32+ messages in thread
* Re: [PATCH] Support the mksh's ${|func;} substitution
@ 2023-05-03 14:46 Sebastian Gniazdowski
  2023-07-04  1:55 ` Bart Schaefer
  0 siblings, 1 reply; 32+ messages in thread
From: Sebastian Gniazdowski @ 2023-05-03 14:46 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 1057 bytes --]

Hi,
I was looking at some of my old patches and stumbled upon this one
which implements mksh' ${|func;} substitution. The old thread went
over a complex (x) flag choice as a expected candidate for such use
case, however I've now thought that something is better than nothing
and that implementing simpler ${!func;} doesn't block us from adding a
more advanced x-flag in the future. Also, the simplicity of this
substitution is a plus for it, opposed to x-flag complexity (think of
existing e flag, whose's use is fairly complex).

So I thought that I send the patch again upon cleaning conflicts, for
consideration. Or should we revive the advanced x-flag?

To recall - the ${|func;} substitution runs function "func" and
substitutes (returned string in…) $REPLY after it has completed,
without (!) doing forks, which is a performance and functionality
benefit over currently available equivalent: $(func;print -rn --
$REPLY). Original thread:
https://zsh.org/mla/workers/2019/msg00768.html

-- 
Best regards,
Sebastian Gniazdowski

[-- Attachment #2: mksh-exec-subst.patch --]
[-- Type: text/x-patch, Size: 3087 bytes --]

From 5129820336782902f0dc43d7f196bb66c4579fb9 Mon Sep 17 00:00:00 2001
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date: Wed, 3 May 2023 14:47:01 +0059
Subject: Support the mksh's ${|func;} substitution

---
 Src/subst.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/Src/subst.c b/Src/subst.c
index 974d6171e..abb458195 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -29,6 +29,7 @@
 
 #include "zsh.mdh"
 #include "subst.pro"
+#include "exec.pro"
 
 #define LF_ARRAY	1
 
@@ -1860,8 +1861,17 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
      * joining the array into a string (for compatibility with ksh/bash).
      */
     int quoted_array_with_offset = 0;
+    /* Indicates ${|func;} */
+    int rplyfunc = 0;
+    /* The name of the function to be ran by ${|...;} */
+    char *cmdarg = NULL;
+    /* The length of the input string */
+    int slen = 0;
+    /* The closing brace pointer */
+    char *outbracep;
 
     *s++ = '\0';
+    slen = strlen(s);
     /*
      * Nothing to do unless the character following the $ is
      * something we recognise.
@@ -1889,6 +1899,41 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
     if (c == Inbrace) {
 	inbrace = 1;
 	s++;
+
+        /* Short-path for the function-running substitution ${|func;}
+         * The function name is extracted and called, and the
+         * substitution assigned. There's no (...)-flags processing,
+         * i.e. no ${|(U)func;}, because it looks quite awful and
+         * also requires a change to the manual, part about the
+         * substitution order. Use ${(U)${|func;}} instead, it looks
+         * cleaner. */
+        if ( ((outbracep=strchr(s,Outbrace)) ||
+             (outbracep=strchr(s,'}'))) &&
+                (s[0] == Bar || s[0] == '|') &&
+                    outbracep[-1] == ';' )
+        {
+            rplyfunc = 1;
+            cmdarg = dupstrpfx(s+1, outbracep-s-2);
+            s=outbracep;
+
+            HashNode hn = NULL;
+            if( (hn = shfunctab->getnode(shfunctab, cmdarg)) ) {
+                /* Execute the shell function */
+                doshfunc((Shfunc) hn, NULL, 1);
+                val = getsparam("REPLY");
+                if (val)
+                    vunset = 0;
+                else {
+                    vunset = 1;
+                    val = dupstring("");
+            }
+        } else {
+                zerr("no such function: %s", cmdarg);
+                return NULL;
+        }
+            fetch_needed = 0;
+    }
+
 	/*
 	 * In ksh emulation a leading `!' is a special flag working
 	 * sort of like our (k).  This is true only for arrays or
@@ -2589,7 +2634,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 			     scanflags)) ||
 	    (v->pm && (v->pm->node.flags & PM_UNSET)) ||
 	    (v->flags & VALFLAG_EMPTY))
-	    vunset = 1;
+        {
+            if (!rplyfunc) {
+                vunset = 1;
+        }
+    }
 
 	if (wantt) {
 	    /*
-- 
2.28.0


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

end of thread, other threads:[~2023-07-18  3:14 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-06  0:52 [PATCH] Support the mksh's ${|func;} substitution Sebastian Gniazdowski
2019-09-06  0:54 ` Sebastian Gniazdowski
2019-09-06 23:16 ` Sebastian Gniazdowski
2019-09-07 12:16   ` Daniel Shahaf
2019-09-07 15:07 ` Stephane Chazelas
2019-09-07 18:09   ` Sebastian Gniazdowski
2019-09-07 20:19     ` Stephane Chazelas
2019-09-07 21:19       ` Sebastian Gniazdowski
2019-09-10  2:20         ` Sebastian Gniazdowski
2019-09-10  5:29           ` Bart Schaefer
2019-09-10 18:21             ` Sebastian Gniazdowski
2019-09-10 19:38               ` Bart Schaefer
2019-09-12  0:08                 ` Sebastian Gniazdowski
2019-09-12  1:03                   ` Bart Schaefer
2019-09-12  2:06                     ` Sebastian Gniazdowski
2019-09-12  5:35                       ` Bart Schaefer
2019-09-12  6:00                         ` Sebastian Gniazdowski
2019-09-12  6:55                           ` Bart Schaefer
2019-09-13 20:28                             ` Sebastian Gniazdowski
2019-09-13 21:33                               ` Bart Schaefer
2019-09-13 21:36                                 ` Bart Schaefer
2019-09-14  0:41                                   ` Sebastian Gniazdowski
2019-09-14  0:44                                     ` Sebastian Gniazdowski
2023-05-03 14:46 Sebastian Gniazdowski
2023-07-04  1:55 ` Bart Schaefer
2023-07-04 18:53   ` Lawrence Velázquez
2023-07-05  5:32     ` Bart Schaefer
2023-07-05  6:30       ` Bart Schaefer
2023-07-06  1:39       ` Bart Schaefer
2023-07-06  2:27         ` Mikael Magnusson
2023-07-06  4:00           ` Bart Schaefer
2023-07-18  3:14             ` Bart Schaefer

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