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

[PATCH] Support the mksh's ${|func;} substitution



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
From fb4744562d2f03246288da2692559d7f9c4014f2 Mon Sep 17 00:00:00 2001
From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
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



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