zsh-workers
 help / color / mirror / code / Atom feed
From: Tomoki Sekiyama <tomoki.sekiyama@gmail.com>
To: zsh-workers@zsh.org
Cc: Tomoki Sekiyama <tomoki.sekiyama@gmail.com>
Subject: [PATCH] Enable further expansion of parameter name by ${!...}
Date: Sun, 25 Jan 2015 04:15:38 -0500	[thread overview]
Message-ID: <1422177338-85472-1-git-send-email-tomoki.sekiyama@gmail.com> (raw)

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)


             reply	other threads:[~2015-01-25  9:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-25  9:15 Tomoki Sekiyama [this message]
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

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=1422177338-85472-1-git-send-email-tomoki.sekiyama@gmail.com \
    --to=tomoki.sekiyama@gmail.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).