zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: unset "hash[key]" isn't matched with what "key" may be
Date: Sun, 7 Feb 2016 23:05:44 -0800	[thread overview]
Message-ID: <160207230544.ZM10065@torch.brasslantern.com> (raw)
In-Reply-To: <160207133307.ZM31008@torch.brasslantern.com>

On Feb 7,  1:33pm, Bart Schaefer wrote:
}
} On Feb 7,  3:16pm, Sebastian Gniazdowski wrote:
} }                 unset "func[$i]"
} } -zplg-diff-functions:unset:36: func[opp+a\[]: invalid parameter name
} 
} Yep, "unset" is pretty naive about parsing the subscript there.  It wants
} the square brackets in balanced pairs and doesn't consider any kind of
} quoting.

Patch below for consideration.  This doesn't do any additional expansion,
but it has the effect of allowing backslash-quoting -- which therefore
requires that backslashes themselves be doubled, cf. all the caveats
about subscripts behaving as if double-quoted that appear elsewhere.

After this patch, the best way to handle unset of associative array
elements looks something like

  typeset -A ary
  ary[${key}]=value
  ...
  unset "ary[${(b)key}]"

(or use "noglob" instead of double quotes).

This does reflect a change from previous behavior, so we'll want to
consider it carefully.  If we decide to keep the old behavior, just
remove the call to remnulargs().  I think using parse_subscript() intead
of skipcomm() is still the more sensible approach; in fact, this patch
revealed an error in the E01 test script.

} (The -m option also doesn't work for subscripts.)

I haven't attempted to do anything about that, and I'm not sure that it
makes sense to.


diff --git a/Src/builtin.c b/Src/builtin.c
index 63f964d..4c8fbcd 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -3350,15 +3350,24 @@ bin_unset(char *name, char **argv, Options ops, int func)
     /* do not glob -- unset the given parameter */
     queue_signals();
     while ((s = *argv++)) {
-	char *ss = strchr(s, '[');
-	char *sse = ss;
+	char *ss = strchr(s, '['), *subscript = 0;
 	if (ss) {
-	    if (skipparens('[', ']', &sse) || *sse) {
-		zerrnam(name, "%s: invalid parameter name", s);
-		returnval = 1;
-		continue;
-	    }
+	    char *sse;
 	    *ss = 0;
+	    if ((sse = parse_subscript(ss+1, 1, ']'))) {
+		*sse = 0;
+		subscript = dupstring(ss+1);
+		*sse = ']';
+		remnulargs(subscript);
+		untokenize(subscript);
+	    }
+	}
+	if ((ss && !subscript) || !isident(s)) {
+	    if (ss)
+		*ss = '[';
+	    zerrnam(name, "%s: invalid parameter name", s);
+	    returnval = 1;
+	    continue;
 	}
 	pm = (Param) (paramtab == realparamtab ?
 		      /* getnode2() to avoid autoloading */
@@ -3376,11 +3385,8 @@ bin_unset(char *name, char **argv, Options ops, int func)
 	} else if (ss) {
 	    if (PM_TYPE(pm->node.flags) == PM_HASHED) {
 		HashTable tht = paramtab;
-		if ((paramtab = pm->gsu.h->getfn(pm))) {
-		    *--sse = 0;
-		    unsetparam(ss+1);
-		    *sse = ']';
-		}
+		if ((paramtab = pm->gsu.h->getfn(pm)))
+		    unsetparam(subscript);
 		paramtab = tht;
 	    } else if (PM_TYPE(pm->node.flags) == PM_SCALAR ||
 		       PM_TYPE(pm->node.flags) == PM_ARRAY) {
diff --git a/Test/E01options.ztst b/Test/E01options.ztst
index f270767..40e96af 100644
--- a/Test/E01options.ztst
+++ b/Test/E01options.ztst
@@ -776,7 +776,7 @@
   unsetopt pathdirs
   pathtestdir/findme
   path=($oldpath)
-  unset $oldpath
+  unset oldpath
   rm -rf pdt_topdir pathtestdir
 0:PATH_DIRS option
 >File in upper dir


  reply	other threads:[~2016-02-08  7:05 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-07 14:16 Sebastian Gniazdowski
2016-02-07 19:08 ` Peter Stephenson
2016-02-07 19:10   ` Sebastian Gniazdowski
2016-02-07 19:24     ` Peter Stephenson
2016-02-07 21:33 ` Bart Schaefer
2016-02-08  7:05   ` Bart Schaefer [this message]
2016-02-09  4:54     ` Bart Schaefer
2016-02-09  8:53       ` Peter Stephenson
2016-02-09 15:50         ` Bart Schaefer
2016-02-09 16:19           ` Peter Stephenson
2016-02-09 18:07             ` Bart Schaefer
2016-02-15  7:48   ` Sebastian Gniazdowski
2016-02-15 13:26     ` Nikolay Aleksandrovich Pavlov (ZyX)

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=160207230544.ZM10065@torch.brasslantern.com \
    --to=schaefer@brasslantern.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).