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

[PATCH] Enable further expansion of parameter name by ${!...}



With this change, ${!...} will enable further expansion of parameter name,
which is equivalent to (P) expansion flag. This will enable zsh to run some
scripts using variable references for bash. For example:

 function trueorfalse {
     local default=$1
     local literal=$2
     local testval=${!literal}

     [[ -z "$testval" ]] && { echo "$default"; return; }
     [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
     [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
     echo "$default"
 }

 echo $(trueorfalse 0 SOME_VAR)
---
 Doc/Zsh/expn.yo | 5 +++++
 Src/subst.c     | 4 ++++
 2 files changed, 9 insertions(+)

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 8728803..1029aef 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -833,6 +833,11 @@ the pattern tt(*.c), which may be expanded by filename generation, but
 tt(${${~foo}//\*/*.c}) substitutes to the string tt(*.c), which will not
 be further expanded.
 )
+item(tt(${!)var(spec)tt(}))(
+This is equivalent to 'tt((P))' flag, which forces the value of the
+parameter var(name) to be interpreted as a further parameter name,
+whose value will be used where appropriate.
+)
 enditem()
 
 If a tt(${)...tt(}) type parameter expression or a
diff --git a/Src/subst.c b/Src/subst.c
index a2bb648..8897350 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2179,6 +2179,10 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags)
 		zerr("bad substitution");
 		return NULL;
 	    }
+	} else if (c == '!') {
+	    /* equivalent to (P) */
+	    aspar = 1;
+	    s++;
 	} else if (inbrace && inull(*s)) {
 	    /*
 	     * Handles things like ${(f)"$(<file)"} by skipping 
-- 
1.9.3 (Apple Git-50)



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