about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMikael Magnusson <mikachu@gmail.com>2016-05-08 21:33:24 +0200
committerMikael Magnusson <mikachu@gmail.com>2016-05-08 21:57:06 +0200
commitb7c2ddf65c9175b77bb9efc4ec7de7ef472d7a65 (patch)
treeeca4db8b437ffdb51468a28968ec5f1bdeab2c14
parent546a6b42b3a2b1172fbd099a0fee2864af16461c (diff)
downloadzsh-b7c2ddf65c9175b77bb9efc4ec7de7ef472d7a65.tar.gz
zsh-b7c2ddf65c9175b77bb9efc4ec7de7ef472d7a65.tar.xz
zsh-b7c2ddf65c9175b77bb9efc4ec7de7ef472d7a65.zip
Add typeset -c to control when cached length is used
-rw-r--r--Src/builtin.c10
-rw-r--r--Src/params.c136
-rw-r--r--Src/subst.c8
-rw-r--r--Src/zsh.h3
4 files changed, 116 insertions, 41 deletions
diff --git a/Src/builtin.c b/Src/builtin.c
index 534f49b57..6ce313d88 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -55,7 +55,7 @@ static struct builtin builtins[] =
     BUILTIN("cd", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 2, BIN_CD, "qsPL", NULL),
     BUILTIN("chdir", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 2, BIN_CD, "qsPL", NULL),
     BUILTIN("continue", BINF_PSPECIAL, bin_break, 0, 1, BIN_CONTINUE, NULL, NULL),
-    BUILTIN("declare", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%klmprtuxz", NULL),
+    BUILTIN("declare", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%acfghi:%klmprtuxz", NULL),
     BUILTIN("dirs", 0, bin_dirs, 0, -1, 0, "clpv", NULL),
     BUILTIN("disable", 0, bin_enable, 0, -1, BIN_DISABLE, "afmprs", NULL),
     BUILTIN("disown", 0, bin_fg, 0, -1, BIN_DISOWN, NULL, NULL),
@@ -88,7 +88,7 @@ static struct builtin builtins[] =
     BUILTIN("jobs", 0, bin_fg, 0, -1, BIN_JOBS, "dlpZrs", NULL),
     BUILTIN("kill", BINF_HANDLES_OPTS, bin_kill, 0, -1, 0, NULL, NULL),
     BUILTIN("let", 0, bin_let, 1, -1, 0, NULL, NULL),
-    BUILTIN("local", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%ahi:%lprtux", NULL),
+    BUILTIN("local", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%achi:%lprtux", NULL),
     BUILTIN("log", 0, bin_log, 0, 0, 0, NULL, NULL),
     BUILTIN("logout", 0, bin_break, 0, 1, BIN_LOGOUT, NULL, NULL),
 
@@ -108,7 +108,7 @@ static struct builtin builtins[] =
     BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL),
     BUILTIN("r", 0, bin_fc, 0, -1, BIN_R, "IlLnr", NULL),
     BUILTIN("read", 0, bin_read, 0, -1, 0, "cd:ek:%lnpqrst:%zu:AE", NULL),
-    BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, BIN_READONLY, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux", "r"),
+    BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, BIN_READONLY, "AE:%F:%HL:%R:%TUZ:%acfghi:%lptux", "r"),
     BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"),
     BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL, NULL),
     BUILTIN("set", BINF_PSPECIAL | BINF_HANDLES_OPTS, bin_set, 0, -1, 0, NULL, NULL),
@@ -122,7 +122,7 @@ static struct builtin builtins[] =
     BUILTIN("trap", BINF_PSPECIAL | BINF_HANDLES_OPTS, bin_trap, 0, -1, 0, NULL, NULL),
     BUILTIN("true", 0, bin_true, 0, -1, 0, NULL, NULL),
     BUILTIN("type", 0, bin_whence, 0, -1, 0, "ampfsSw", "v"),
-    BUILTIN("typeset", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%klprtuxmz", NULL),
+    BUILTIN("typeset", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%acfghi:%klprtuxmz", NULL),
     BUILTIN("umask", 0, bin_umask, 0, 1, 0, "S", NULL),
     BUILTIN("unalias", 0, bin_unhash, 0, -1, BIN_UNALIAS, "ams", NULL),
     BUILTIN("unfunction", 0, bin_unhash, 1, -1, BIN_UNFUNCTION, "m", "f"),
@@ -3406,6 +3406,8 @@ bin_unset(char *name, char **argv, Options ops, int func)
 		    } else {
 			/* start is after the element for reverse index */
 			int start = vbuf.start - !!(vbuf.flags & VALFLAG_INV);
+			if (!(vbuf.pm->node.flags & PM_CACHELEN))
+			    vbuf.pm->length = arrlen(vbuf.pm->u.arr);
 			assert(vbuf.pm->length == arrlen(vbuf.pm->u.arr));
 			if (start < vbuf.pm->length) {
 			    char *arr[2];
diff --git a/Src/params.c b/Src/params.c
index cdaa59a92..4a5db3c1d 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -28,6 +28,7 @@
  */
 
 #include <assert.h>
+//#define assert(x)
 
 #include "zsh.mdh"
 #include "params.pro"
@@ -661,6 +662,19 @@ split_env_string(char *env, char **name, char **value)
     } else
 	return 0;
 }
+
+int
+arrcachelen(Param pm)
+{
+    int len;
+
+    len = pm->length;
+    if (len == 0 && pm->u.arr) {
+	len = arrlen(pm->u.arr);
+	pm->length = len;
+    }
+    return len;
+}
     
 /* Set up parameter hash table.  This will add predefined  *
  * parameter entries as well as setting up parameter table *
@@ -1454,8 +1468,11 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
 		ta = getarrvalue(v);
 	    if (!ta || !*ta)
 		return !down;
-	    len = v->pm->length;
-	    assert(len == arrlen(ta));
+	    if (v->pm->node.flags & PM_CACHELEN) {
+		len = arrcachelen(v->pm);
+		assert(len == arrlen(ta));
+	    } else
+		len = arrlen(ta);
 	    if (beg < 0)
 		beg += len;
 	    if (down) {
@@ -1478,8 +1495,11 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
 	    }
 	} else if (word) {
 	    ta = sepsplit(d = s = getstrvalue(v), sep, 1, 1);
-	    len = v->pm->length;
-	    assert(len == arrlen(ta));
+	    if (v->pm->node.flags & PM_CACHELEN) {
+		len = arrcachelen(v->pm);
+		assert(len == arrlen(ta));
+	    } else
+		len = arrlen(ta);
 	    if (beg < 0)
 		beg += len;
 	    if (down) {
@@ -2029,11 +2049,20 @@ getstrvalue(Value v)
 	if (v->isarr)
 	    s = sepjoin(ss, NULL, 1);
 	else {
-	    assert(v->pm->length == arrlen(ss));
-	    if (v->start < 0)
-		v->start += v->pm->length;
-	    s = (v->start >= v->pm->length || v->start < 0) ?
-		(char *) hcalloc(1) : ss[v->start];
+	    if (v->pm->node.flags & PM_CACHELEN) {
+		int len = arrcachelen(v->pm);
+		assert(v->pm->length == arrlen(ss));
+		if (v->start < 0)
+		    v->start += len;
+		s = (v->start >= len || v->start < 0) ?
+		    (char *) hcalloc(1) : ss[v->start];
+	    } else {
+		int len = arrlen(ss);
+		if (v->start < 0)
+		    v->start += len;
+		s = (v->start >= len || v->start < 0) ?
+		    (char *) hcalloc(1) : ss[v->start];
+	    }
 	}
 	return s;
     case PM_INTEGER:
@@ -2227,19 +2256,37 @@ getarrvalue(Value v)
     s = getvaluearr(v);
     if (v->start == 0 && v->end == -1)
 	return s;
-    assert(v->pm->length == arrlen(s));
-    if (v->start < 0)
-	v->start += v->pm->length;
-    if (v->end < 0)
-	v->end += v->pm->length + 1;
-    if (v->start > v->pm->length || v->start < 0)
-	s = arrdup(nular);
-    else
-	s = arrdup(s + v->start);
-    if (v->end <= v->start)
-	s[0] = NULL;
-    else if (v->end - v->start <= v->pm->length)
-	s[v->end - v->start] = NULL;
+    if (v->pm->node.flags & PM_CACHELEN) {
+	int len = arrcachelen(v->pm);
+	assert(v->pm->length == arrlen(s));
+	if (v->start < 0)
+	    v->start += v->pm->length;
+	if (v->end < 0)
+	    v->end += v->pm->length + 1;
+	if (v->start > v->pm->length || v->start < 0)
+	    s = arrdup(nular);
+	else
+	    s = arrdup(s + v->start);
+	if (v->end <= v->start)
+	    s[0] = NULL;
+	//XXX[badarrays] s just changed above but here we use the same
+	//               cached length possible cause of problems
+	else if (v->end - v->start <= v->pm->length)
+	    s[v->end - v->start] = NULL;
+    } else {
+	if (v->start < 0)
+	   v->start += arrlen(s);
+	if (v->end < 0)
+	   v->end += arrlen(s) + 1;
+	if (v->start > arrlen(s) || v->start < 0)
+	   s = arrdup(nular);
+	else
+	   s = arrdup(s + v->start);
+	if (v->end <= v->start)
+	   s[0] = NULL;
+	else if (v->end - v->start <= arrlen(s))
+	   s[v->end - v->start] = NULL;
+    }
     return s;
 }
 
@@ -2561,11 +2608,14 @@ setarrvalue(Value v, char **val)
 	char **const old = v->pm->gsu.a->getfn(v->pm);
 	char **new;
 	char **p, **q, **r; /* index variables */
-	const int pre_assignment_length = v->pm->length;
+	int pre_assignment_length = arrcachelen(v->pm);
 	int post_assignment_length;
 	int i;
 
-	assert(v->pm->length == arrlen(old));
+	if (v->pm->node.flags & PM_CACHELEN)
+	    assert(v->pm->length == arrlen(old));
+	else
+	    pre_assignment_length = arrlen(old);
 	q = old;
 
 	if ((v->flags & VALFLAG_INV) && unset(KSHARRAYS)) {
@@ -2676,8 +2726,11 @@ getaparam(char *s, int *len)
 	PM_TYPE(v->pm->node.flags) == PM_ARRAY)
     {
 	if (len) {
-	    *len = v->pm->length;
-	    assert (*len == arrlen(v->pm->gsu.a->getfn(v->pm)));
+	    if (v->pm->node.flags & PM_CACHELEN) {
+		*len = arrcachelen(v->pm);
+		assert (*len == arrlen(v->pm->gsu.a->getfn(v->pm)));
+	    } else
+		*len = arrlen(v->pm->gsu.a->getfn(v->pm));
 	}
 	//fprintf(stderr, "%i %i\n", v->pm->length, arrlen(v->pm->gsu.a->getfn(v->pm)));
 	return v->pm->gsu.a->getfn(v->pm);
@@ -2818,7 +2871,10 @@ assignsparam(char *s, char *val, int flags)
 		return v->pm; /* avoid later setstrvalue() call */
 	    case PM_ARRAY:
 	    	if (unset(KSHARRAYS)) {
-		    v->start = v->pm->length;
+		    if (v->pm->node.flags & PM_CACHELEN)
+			v->start = arrcachelen(v->pm);
+		    else
+			v->start = arrlen(v->pm->gsu.a->getfn(v->pm));
 		    v->end = v->start + 1;
 		} else {
 		    /* ksh appends scalar to first element */
@@ -2946,10 +3002,15 @@ assignaparam(char *s, char **val, int flags)
     if (flags & ASSPM_AUGMENT) {
     	if (v->start == 0 && v->end == -1) {
 	    if (PM_TYPE(v->pm->node.flags) & PM_ARRAY) {
-		assert(v->pm->length == arrlen(v->pm->gsu.a->getfn(v->pm)));
-	    	v->start = 
-		    //arrlen(v->pm->gsu.a->getfn(v->pm));
-		    v->pm->length;
+		if (v->pm->node.flags & PM_CACHELEN) {
+		    v->start = 
+			//arrlen(v->pm->gsu.a->getfn(v->pm));
+			arrcachelen(v->pm);
+		    assert(v->pm->length == arrlen(v->pm->gsu.a->getfn(v->pm)));
+		} else {
+		    v->start = 
+			arrlen(v->pm->gsu.a->getfn(v->pm));
+		}
 	    	v->end = v->start + 1;
 	    } else if (PM_TYPE(v->pm->node.flags) & PM_HASHED)
 	    	v->start = -1, v->end = 0;
@@ -2957,10 +3018,15 @@ assignaparam(char *s, char **val, int flags)
 	    if (v->end > 0)
 		v->start = v->end--;
 	    else if (PM_TYPE(v->pm->node.flags) & PM_ARRAY) {
-		assert(v->pm->length == arrlen(v->pm->gsu.a->getfn(v->pm)));
-		v->end 
-		    //= arrlen(v->pm->gsu.a->getfn(v->pm)) + v->end;
-		    += v->pm->length;
+		if (v->pm->node.flags & PM_CACHELEN) {
+		    v->end 
+			//= arrlen(v->pm->gsu.a->getfn(v->pm)) + v->end;
+			+= arrcachelen(v->pm);
+		    assert(v->pm->length == arrlen(v->pm->gsu.a->getfn(v->pm)));
+		} else {
+		    v->end 
+			= arrlen(v->pm->gsu.a->getfn(v->pm)) + v->end;
+		}
 		v->start = v->end + 1;
 	    }
 	}
diff --git a/Src/subst.c b/Src/subst.c
index 8644ea0d6..cc59c74d4 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -35,6 +35,8 @@
 /**/
 char nulstring[] = {Nularg, '\0'};
 
+int arrcachelen(Param pm);
+    
 /* Do substitutions before fork. These are:
  *  - Process substitution: <(...), >(...), =(...)
  *  - Parameter substitution
@@ -2548,7 +2550,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 		 * necessary joining of arrays until this point
 		 * to avoid the multsub() horror.
 		 */
-		int tmplen = arrlen(v->pm->gsu.a->getfn(v->pm));
+		int tmplen;
+		if (v->pm->node.flags & PM_CACHELEN)
+		    tmplen = arrcachelen(v->pm);
+		else
+		    tmplen = arrlen(v->pm->gsu.a->getfn(v->pm));
 
 		if (v->start < 0)
 		    v->start += tmplen + ((v->flags & VALFLAG_INV) ? 1 : 0);
diff --git a/Src/zsh.h b/Src/zsh.h
index 0e7b75e25..bf89272f2 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -1791,6 +1791,7 @@ struct tieddata {
 
 #define PM_KSHSTORED	(1<<17) /* function stored in ksh form              */
 #define PM_ZSHSTORED	(1<<18) /* function stored in zsh form              */
+#define PM_CACHELEN     (1<<19) /* length is cached */
 
 /* Remaining flags do not correspond directly to command line arguments */
 #define PM_LOCAL	(1<<21) /* this parameter will be made local        */
@@ -1809,7 +1810,7 @@ struct tieddata {
 #define PM_NAMEDDIR     (1<<30) /* has a corresponding nameddirtab entry    */
 
 /* The option string corresponds to the first of the variables above */
-#define TYPESET_OPTSTR "aiEFALRZlurtxUhHTkz"
+#define TYPESET_OPTSTR "aiEFALRZlurtxUhHTkzc"
 
 /* These typeset options take an optional numeric argument */
 #define TYPESET_OPTNUM "LRZiEF"