zsh-workers
 help / color / mirror / code / Atom feed
* Re: PATCH: expanding parameters like echo/print builtins
@ 2011-05-14 11:41 Jilles Tjoelker
  2011-05-14 18:41 ` Bart Schaefer
  0 siblings, 1 reply; 20+ messages in thread
From: Jilles Tjoelker @ 2011-05-14 11:41 UTC (permalink / raw)
  To: zsh-workers

> [${(g)parameter} expansion for expanding like echo/print]

Am I really strange in thinking this should really be done using command
substitution of echo, print or printf with appropriate options?

Admittedly, this requires precautions if trailing newlines are
significant, like (for expanding similar to the System V echo):

result=$(printf '%b@' "$v")
result=${result%@}

If trailing newlines are not significant, the syntax is very readable,
like:

result=$(printf '%b' "$v")

result=$(printf "$v") # careful with %

result=$(print - "$v")

Command substitution can be slow because it forks a subshell, but ksh93
and FreeBSD sh prove it need not be -- a subshell can be emulated
without a fork in many cases and POSIX is explicitly worded to allow
this.

Other tricks are using eval with $'...' (requires special care for ' and
is not in POSIX.1-2008).

In general, I think the ${(foo)parameter} forms are mostly write-only
code for interactive use, except perhaps if you use them a lot.

-- 
Jilles Tjoelker


^ permalink raw reply	[flat|nested] 20+ messages in thread
* PATCH: expanding parameters like echo/print builtins
@ 2011-05-11 13:02 Mikael Magnusson
  2011-05-11 15:03 ` Bart Schaefer
  2011-05-11 16:21 ` Peter Stephenson
  0 siblings, 2 replies; 20+ messages in thread
From: Mikael Magnusson @ 2011-05-11 13:02 UTC (permalink / raw)
  To: zsh-workers

Is this already possible? I had an array with a lot of \u1234 escapes
in it, and couldn't find a way to do it easily. I came up with this:
${arr:gs/(#b)'\\\\u(????)/${(#):-$(( 16#$match[1] ))}'}
but it's a bit awkward to type.

The following patch adds the (g::) parameter flag which calls the
getkeystring function, and allows setting the _EMACS, _OCTAL_ESC and
_CTRL flags.

---
 Doc/Zsh/expn.yo |    8 ++++++++
 Src/subst.c     |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 0 deletions(-)

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 2c3a571..2beebd7 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -863,6 +863,14 @@ item(tt(F))(
 Join the words of arrays together using newline as a separator.
 This is a shorthand for `tt(pj:\n:)'.
 )
+item(tt(g:opts:))(
+Expand words like the echo builtin.  With the tt(o) option, octal escapes
+don't take a leading zero.  With the tt(c) option, sequences like `tt(^X)'
+are interpreted.  With the tt(e) option, interprets `tt(\M-t)' and similar
+sequences like the print builtin.  With both tt(o) and tt(e) options, behaves
+like the print builtin with the exception of the next sentence.   In none
+of these modes is `tt(\c)' interpreted.
+)
 item(tt(i))(
 Sort case-insensitively.  May be combined with `tt(n)' or `tt(O)'.
 )
diff --git a/Src/subst.c b/Src/subst.c
index 617986c..a2af01b 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1607,6 +1607,10 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
      */	
     int presc = 0;
     /*
+     * The (g) flag.  Expand words with various GETKEY_ flags.
+     */
+    int getkeys = -1;
+    /*
      * The (@) flag; interacts obscurely with qt and isarr.
      * This is one of the things that decides whether multsub
      * will produce an array, but in an extremely indirect fashion.
@@ -1932,6 +1936,36 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 		    presc++;
 		    break;
 
+		case 'z':
+		    t = get_strarg(++s, &arglen);
+		    if (getkeys < 0)
+			getkeys = 0;
+		    if (*t) {
+			sav = *t;
+			*t = 0;
+			while (*++s) {
+			    switch (*s) {
+			    case 'e':
+				getkeys |= GETKEY_EMACS;
+				break;
+			    case 'o':
+				getkeys |= GETKEY_OCTAL_ESC;
+				break;
+			    case 'c':
+				getkeys |= GETKEY_CTRL;
+				break;
+
+			    default:
+				*t = sav;
+				goto flagerr;
+			    }
+			}
+			*t = sav;
+			s = t + arglen - 1;
+		    } else
+			goto flagerr;
+                    break;
+
 		case 'z':
 		    shsplit = LEXFLAGS_ACTIVE;
 		    break;
@@ -3141,6 +3175,27 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 	opts[PROMPTPERCENT] = opp;
     }
     /*
+     * Perform GETKEY expansion
+     */
+    if (getkeys >= 0) {
+	if (isarr) {
+	    char **ap;
+
+	    if (!copied)
+		aval = arrdup(aval), copied = 1;
+	    ap = aval;
+	    for (; *ap; ap++) {
+		int len;
+		*ap = getkeystring(*ap, &len, getkeys, NULL);
+	    }
+	} else {
+	    int len;
+	    if (!copied)
+		val = dupstring(val), copied = 1;
+	    val = getkeystring(val, &len, getkeys, NULL);
+	}
+    }
+    /*
      * One of the possible set of quotes to apply, depending on
      * the repetitions of the (q) flag.
      */
-- 
1.7.4-rc1


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

end of thread, other threads:[~2011-05-14 18:41 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-14 11:41 PATCH: expanding parameters like echo/print builtins Jilles Tjoelker
2011-05-14 18:41 ` Bart Schaefer
  -- strict thread matches above, loose matches on Subject: below --
2011-05-11 13:02 Mikael Magnusson
2011-05-11 15:03 ` Bart Schaefer
2011-05-11 15:14   ` Mikael Magnusson
2011-05-11 16:03     ` Bart Schaefer
2011-05-11 16:22       ` Mikael Magnusson
2011-05-11 16:48         ` Bart Schaefer
2011-05-11 17:11           ` Mikael Magnusson
2011-05-11 15:47   ` Oliver Kiddle
2011-05-11 16:21 ` Peter Stephenson
2011-05-11 16:38   ` Mikael Magnusson
2011-05-11 17:07     ` Peter Stephenson
2011-05-11 17:19       ` Mikael Magnusson
2011-05-11 17:26         ` Peter Stephenson
2011-05-11 17:35           ` Mikael Magnusson
2011-05-11 17:43           ` Mikael Magnusson
2011-05-12 10:10             ` Bart Schaefer
2011-05-12 10:40               ` Mikael Magnusson
2011-05-12 14:04                 ` 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).