zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Arith parsing bug with minus after $#
Date: Sat, 30 May 2015 20:40:46 +0100	[thread overview]
Message-ID: <20150530204046.0ae57c98@ntlworld.com> (raw)
In-Reply-To: <20150530202412.2ff6de78@ntlworld.com>

On Sat, 30 May 2015 20:24:12 +0100
Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> On Fri, 29 May 2015 14:24:45 -0700
> Bart Schaefer <schaefer@brasslantern.com> wrote:
> > On May 29, 2015 1:54 PM, "ZyX" <kp-pav@yandex.ru> wrote:
> > >
> > > But later it explicitly says that not enclosed in single braces may only
> > be names or single-character variables. I.e. $#- is ${#}-, $10 is ${1}0, …
> > 
> > Yes, but $#name to return the length of the value of $name is already a zsh
> > extension, so unless we're in some emulation mode, treating $#- as the
> > length of $- is perfectly reasonable.
> 
> Perhaps more interesting is $#*, since "*" is a much more commmon
> special parameter that's also an operator, and isn't quite so horrifically
> overloaded in parameter substitution.
> 
> % emulate sh -c 'fn() { echo $(( $#*3 )); }'
> % fn one
> 13
> 
> That looks like it needs some emulation, though it can wait since nobody's
> tripped over it.

It's easy, though, so no time like the present...  and this ought to be
consistent across all parameters, so make "-" behave the same.

What I'm not sure about is how to decide.  SH_WORD_SPLIT isn't the
same thing, though there's an obvious mnemonic for why it might have
this effect. We have the option of basing it on emulation alone, but
that always strikes me as something of a counsel of despair.  I'll add
documentation when this gets decided.

diff --git a/Src/subst.c b/Src/subst.c
index 168f7f1..67bd088 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2156,6 +2156,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags)
 		    nojoin = !(ifs && *ifs);
 	    }
 	} else if ((c == '#' || c == Pound) &&
+		   (inbrace || !isset(SHWORDSPLIT)) &&
 		   (itype_end(s+1, IIDENT, 0) != s + 1
 		    || (cc = s[1]) == '*' || cc == Star || cc == '@'
 		    || cc == '?' || cc == Quest
@@ -2170,7 +2171,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags)
 		     */
 		    || ((cc == '#' || cc == Pound) &&
 			s[2] == Outbrace)
-		    || (inbrace && (cc == '-' || (cc == ':' && s[2] == '-')))
+		    || cc == '-' || (cc == ':' && s[2] == '-')
 		    || (isstring(cc) && (s[2] == Inbrace || s[2] == Inpar)))) {
 	    getlen = 1 + whichlen, s++;
 	    /*
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index c41e05e..d06a73a 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -1704,7 +1704,10 @@
   [[ $funnychars = ${~${(b)funnychars}} ]]
 0:${(b)...} quoting protects from GLOB_SUBST
 
-  set --
-  print $#-1
-0:Avoid confusion after overloaded characters in braceless substitution
+  set -- foo
+  echo $(( $#*3 ))
+  emulate sh -c 'nolenwithoutbrace() { echo $#-1; }'
+  nolenwithoutbrace
+0:Avoid confusion after overloaded characters in braceless substitution in sh
+>13
 >0-1


  reply	other threads:[~2015-05-30 19:40 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-28 19:42 Martijn Dekker
2015-05-29 15:02 ` Peter Stephenson
2015-05-29 15:43   ` Martijn Dekker
2015-05-29 18:09   ` Bart Schaefer
2015-05-29 19:33     ` Martijn Dekker
2015-05-29 20:48       ` ZyX
2015-05-29 21:24         ` Bart Schaefer
2015-05-30 19:24           ` Peter Stephenson
2015-05-30 19:40             ` Peter Stephenson [this message]
2015-05-30 22:28               ` Bart Schaefer
2015-05-30 23:30                 ` Peter Stephenson
2015-06-07  1:08   ` 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=20150530204046.0ae57c98@ntlworld.com \
    --to=p.w.stephenson@ntlworld.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).