zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Enable further expansion of parameter name by ${!...}
@ 2015-01-25  9:15 Tomoki Sekiyama
  2015-01-25 18:14 ` Peter Stephenson
  2015-01-25 20:24 ` Bart Schaefer
  0 siblings, 2 replies; 12+ messages in thread
From: Tomoki Sekiyama @ 2015-01-25  9:15 UTC (permalink / raw)
  To: zsh-workers; +Cc: Tomoki Sekiyama

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)


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

end of thread, other threads:[~2016-11-11 18:48 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-25  9:15 [PATCH] Enable further expansion of parameter name by ${!...} Tomoki Sekiyama
2015-01-25 18:14 ` Peter Stephenson
2015-01-25 18:23   ` Mikael Magnusson
2015-01-25 20:24 ` Bart Schaefer
2015-01-25 20:38   ` ZyX
2015-01-25 21:39     ` Bart Schaefer
2015-01-26 20:00       ` Tomoki Sekiyama
2015-01-26 20:44         ` ZyX
2015-01-27  5:48         ` Bart Schaefer
2015-01-28  3:21           ` Tomoki Sekiyama
2015-01-28 18:41             ` Bart Schaefer
2016-11-11 16:58           ` 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).