about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog10
-rw-r--r--Src/glob.c2
-rw-r--r--Src/params.c25
-rw-r--r--Src/subst.c8
-rw-r--r--Src/zsh.h1
-rw-r--r--Test/D06subscript.ztst26
6 files changed, 54 insertions, 18 deletions
diff --git a/ChangeLog b/ChangeLog
index 6db931b6c..6890fb9a1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,14 @@
+2001-04-23  Bart Schaefer  <schaefer@brasslantern.com>
+
+	* 14080: Src/glob.c, Src/params.c, Src/subst.c, Src/zsh.h,
+	Test/D06subscript.ztst: Optimize subscript parsing slightly by
+	passing down an indication of whether the expression is in double
+	quotes; fail noisily rather than silently on certain illegal
+	associative array assignments; still more subscripting tests.
+
 2001-04-23  Clint Adams <schizo@debian.org>
 
-	* 14xxx: Doc/Zsh/mod_termcap.yo, Doc/Zsh/mod_terminfo.yo:
+	* 14078: Doc/Zsh/mod_termcap.yo, Doc/Zsh/mod_terminfo.yo:
 	Document $termcap, change reference to termcap/terminfo
 	'strings' to 'values'.
 
diff --git a/Src/glob.c b/Src/glob.c
index 90b291459..5be79329b 100644
--- a/Src/glob.c
+++ b/Src/glob.c
@@ -1344,7 +1344,7 @@ glob(LinkList list, LinkNode np, int nountok)
 			    v.pm = NULL;
 			    v.end = -1;
 			    v.inv = 0;
-			    if (getindex(&s, &v) || s == os) {
+			    if (getindex(&s, &v, 0) || s == os) {
 				zerr("invalid subscript", NULL, 0);
 				restore_globstate(saved);
 				return;
diff --git a/Src/params.c b/Src/params.c
index de928ea1b..192ca74ad 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -845,7 +845,8 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w)
 		sep = "\n";
 		break;
 	    case 'e':
-		/* obsolate compatibility flag without any real effect */
+		/* Compatibility flag with no effect except to prevent *
+		 * special interpretation by getindex() of `*' or `@'. */
 		break;
 	    case 'n':
 		t = get_strarg(++s);
@@ -876,7 +877,7 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w)
 		break;
 	    case 's':
 		/* This gives the string that separates words *
-		 * (for use with the `w' flag.                */
+		 * (for use with the `w' flag).               */
 		t = get_strarg(++s);
 		if (!*t)
 		    goto flagerr;
@@ -1177,11 +1178,10 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w)
 
 /**/
 int
-getindex(char **pptr, Value v)
+getindex(char **pptr, Value v, int dq)
 {
     int start, end, inv = 0;
     char *s = *pptr, *tbrack;
-    int dq = !!strchr(s, Dnull);
 
     *s++ = '[';
     s = parse_subscript(s, dq);	/* Error handled after untokenizing */
@@ -1358,7 +1358,7 @@ fetchvalue(Value v, char **pptr, int bracks, int flags)
 	v->start = 0;
 	v->end = -1;
 	if (bracks > 0 && (*s == '[' || *s == Inbrack)) {
-	    if (getindex(&s, v)) {
+	    if (getindex(&s, v, (flags & SCANPM_DQUOTED))) {
 		*pptr = s;
 		return v;
 	    }
@@ -1409,7 +1409,7 @@ getstrvalue(Value v)
 	/* (!v->isarr) should be impossible unless emulating ksh */
 	if (!v->isarr && emulation == EMULATE_KSH) {
 	    s = dupstring("[0]");
-	    if (getindex(&s, v) == 0)
+	    if (getindex(&s, v, 0) == 0)
 		s = getstrvalue(v);
 	    return s;
 	} /* else fall through */
@@ -1575,6 +1575,10 @@ setstrvalue(Value v, char *val)
 	zsfree(val);
 	return;
     }
+    if (v->pm->flags & PM_HASHED) {
+	zerr("%s: attempt to set slice of associative array", v->pm->nam, 0);
+	return;
+    }
     v->pm->flags &= ~PM_UNSET;
     switch (PM_TYPE(v->pm->flags)) {
     case PM_SCALAR:
@@ -1698,7 +1702,8 @@ setarrvalue(Value v, char **val)
     }
     if (!(PM_TYPE(v->pm->flags) & (PM_ARRAY|PM_HASHED))) {
 	freearray(val);
-	zerr("attempt to assign array value to non-array", NULL, 0);
+	zerr("%s: attempt to assign array value to non-array",
+	     v->pm->nam, 0);
 	return;
     }
     if (v->start == 0 && v->end == -1) {
@@ -1712,7 +1717,8 @@ setarrvalue(Value v, char **val)
 
 	if ((PM_TYPE(v->pm->flags) == PM_HASHED)) {
 	    freearray(val);
-	    zerr("attempt to set slice of associative array", NULL, 0);
+	    zerr("%s: attempt to set slice of associative array",
+		 v->pm->nam, 0);
 	    return;
 	}
 	if (v->inv && unset(KSHARRAYS))
@@ -1906,7 +1912,8 @@ setaparam(char *s, char **val)
 	*ss = '[';
 	if (v && PM_TYPE(v->pm->flags) == PM_HASHED) {
 	    unqueue_signals();
-	    zerr("attempt to set slice of associative array", NULL, 0);
+	    zerr("%s: attempt to set slice of associative array",
+		 v->pm->nam, 0);
 	    freearray(val);
 	    errflag = 1;
 	    return NULL;
diff --git a/Src/subst.c b/Src/subst.c
index 27ed6af07..2113123ef 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1068,7 +1068,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 	    s++;
 	v = (Value) NULL;
     } else if (aspar) {
-	if ((v = getvalue(&vbuf, &s, 1))) {
+	if ((v = fetchvalue(&vbuf, &s, 1, (qt ? SCANPM_DQUOTED : 0)))) {
 	    val = idbeg = getstrvalue(v);
 	    subexp = 1;
 	} else
@@ -1080,7 +1080,9 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 	if (!(v = fetchvalue(&vbuf, (subexp ? &ov : &s),
 			     (wantt ? -1 :
 			      ((unset(KSHARRAYS) || inbrace) ? 1 : -1)),
-			     hkeys|hvals|(arrasg ? SCANPM_ASSIGNING : 0))) ||
+			     hkeys|hvals|
+			     (arrasg ? SCANPM_ASSIGNING : 0)|
+			     (qt ? SCANPM_DQUOTED : 0))) ||
 	    (v->pm && (v->pm->flags & PM_UNSET)))
 	    vunset = 1;
 
@@ -1151,7 +1153,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 	    v->isarr = isarr;
 	    v->pm = pm;
 	    v->end = -1;
-	    if (getindex(&s, v) || s == os)
+	    if (getindex(&s, v, qt) || s == os)
 		break;
 	}
 	if ((isarr = v->isarr)) {
diff --git a/Src/zsh.h b/Src/zsh.h
index 315c1d3bd..ed63b1a49 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -1148,6 +1148,7 @@ struct param {
 #define SCANPM_MATCHMANY  (1<<5)
 #define SCANPM_ASSIGNING  (1<<6)
 #define SCANPM_KEYMATCH   (1<<7)
+#define SCANPM_DQUOTED    (1<<8)
 #define SCANPM_ISVAR_AT   ((-1)<<15)	/* Only sign bit is significant */
 
 /*
diff --git a/Test/D06subscript.ztst b/Test/D06subscript.ztst
index 4a96b01ab..d7ba59b3f 100644
--- a/Test/D06subscript.ztst
+++ b/Test/D06subscript.ztst
@@ -93,14 +93,15 @@
 >\? \2 \? \?
 >\\]
 
+  eval 'A[*]=star'
+1:Illegal associative array assignment
+?ZTST_execchunk:2: A: attempt to set slice of associative array
+
   x='*'
   A[$x]=xstar
   A[${(q)x}]=qxstar
   print -R ${(k)A[(r)xstar]} $A[$x]
   print -R ${(k)A[(r)qxstar]} $A[${(q)x}]
-  # A[*] is interpreted specially, assignment to it fails silently (oops)
-  A[*]=star
-  print -R ${(k)A[(r)star]} $A[$x]
   A[(e)*]=star
   A[\*]=backstar
   print -R ${(k)A[(r)star]} $A[(e)*]
@@ -108,7 +109,6 @@
 0:Associative array assignment
 >* xstar
 >\* qxstar
->xstar
 >* star
 >\* backstar
 
@@ -160,3 +160,21 @@
 >QqQq
 >qqq
 >QQQ
+
+  print ${x::=$A[$A[(i)one\"two\"three\"quotes]]}
+  print $x
+  print ${x::="$A[$A[(i)one\"two\"three\"quotes]]"}
+  print $x
+0:More keys with double quotes, used in assignment-expansion
+>qqq
+>qqq
+>QQQ
+>QQQ
+
+  qqq=lower
+  QQQ=upper
+  print ${(P)A[one\"two\"three\"quotes]}
+  print "${(P)A[$A[(i)one\"two\"three\"quotes]]}"
+0:Keys with double quotes and the (P) expansion flag
+>lower
+>upper