zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: zsh-workers@zsh.org
Subject: Re: Can't call multi-arg math function from array subscript
Date: Mon, 03 Apr 2017 09:51:40 +0100	[thread overview]
Message-ID: <20170403095140.5796e742@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <170402115156.ZM27355@torch.brasslantern.com>

On Sun, 2 Apr 2017 11:51:56 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> Add another argument and strange things start happening:
> 
> % twoarg() { return $(( $2 - $1 )) }
> % functions -M twoarg
> % print ${string[1,twoarg(1,4)]}
> twoarg: bad math expression: operand expected at end of string
> twoarg: bad math expression: operand expected at end of string

It's going to be because getarg() doesn't do much parsing.  The answer
will be something like this, though I'm not sure what to do about
quoting.

pws

diff --git a/Src/params.c b/Src/params.c
index 785b9ea..a9683a6 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -1183,7 +1183,7 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
        int *prevcharlen, int *nextcharlen)
 {
     int hasbeg = 0, word = 0, rev = 0, ind = 0, down = 0, l, i, ishash;
-    int keymatch = 0, needtok = 0, arglen, len;
+    int keymatch = 0, needtok = 0, arglen, len, inpar = 0;
     char *s = *str, *sep = NULL, *t, sav, *d, **ta, **p, *tt, c;
     zlong num = 1, beg = 0, r = 0, quote_arg = 0;
     Patprog pprog = NULL;
@@ -1322,8 +1322,9 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
     }
 
     for (t = s, i = 0;
-	 (c = *t) && ((c != Outbrack &&
-		       (ishash || c != ',')) || i); t++) {
+	 (c = *t) &&
+	     ((c != Outbrack && (ishash || c != ',')) || i || inpar);
+	 t++) {
 	/* Untokenize inull() except before brackets and double-quotes */
 	if (inull(c)) {
 	    c = t[1];
@@ -1344,6 +1345,10 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
 	    i++;
 	else if (c == ']' || c == Outbrack)
 	    i--;
+	if (c == '(' || c == Inpar)
+	    inpar++;
+	else if (c == ')' || c == Outpar)
+	    inpar--;
 	if (ispecial(c))
 	    needtok = 1;
     }
diff --git a/Test/D06subscript.ztst b/Test/D06subscript.ztst
index 1449236..f0a858b 100644
--- a/Test/D06subscript.ztst
+++ b/Test/D06subscript.ztst
@@ -266,3 +266,10 @@
 >of the gang
 >of the gang
 >of the gang
+
+ string='abcde'
+ twoarg() { return $(( $2 - $1 )) }
+ functions -M twoarg
+ print ${string[1,twoarg(1,4)]}
+0:Commas inside parentheses do not confuse subscripts
+>abc


      parent reply	other threads:[~2017-04-03  8:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20170402185230epcas1p467fce7a708bf94d7f2c8e032327196d9@epcas1p4.samsung.com>
2017-04-02 18:51 ` Bart Schaefer
2017-04-02 20:36   ` Mikael Magnusson
2017-04-03  8:51   ` Peter Stephenson [this message]

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=20170403095140.5796e742@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.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).