Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: zstyle is badly broken as of 20060817



On Sat, 19 Aug 2006 11:11:55 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> schaefer<532> zstyle ':completion:*' list-prompt ''
> schaefer<533> zstyle -L | grep list-prompt 
> zstyle ':completion:*' list-prompt ''
> schaefer<534> zstyle -d ':completion:*' list-prompt
> schaefer<535> zstyle | grep list-prompt
> list-prompt
> schaefer<536> zstyle -L | grep list-prompt
> schaefer<537> 
> 
> So the list-prompt style is still there even after supposedly being deleted,
> but zstyle -L does not list it (and completion behaves as if it is still
> set, which is how I even noticed).

Your subject line implies this is something new, but it isn't.  When the
last pattern for a style is deleted the style hangs around, with the
effects you see.  You'd find it was cosmetic, although confusing and
potentially memory hogging, in that there were no contexts given for the
style.  This fixes it.

> schaefer<545> zstyle -s ":completion:${curcontext}:default" list-prompt tmp
> schaefer<546> echo $?
> 0

I didn't seem to get this effect.  It would have to be a different bug,
since searching the list of context patterns appears to be correctly
terminated even if it's empty.

Index: Src/Modules/zutil.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/zutil.c,v
retrieving revision 1.18
diff -u -r1.18 zutil.c
--- Src/Modules/zutil.c	16 Aug 2006 09:06:40 -0000	1.18
+++ Src/Modules/zutil.c	20 Aug 2006 16:44:11 -0000
@@ -58,9 +58,27 @@
 
 /* Memory stuff. */
 
+/*
+ * Free the information for one of the patterns associated with
+ * a style.
+ *
+ * If the style s is passed, prev is the previous pattern in the list,
+ * found when scanning.  We use this to update the list of patterns.
+ * If this results in their being no remaining patterns, the style
+ * itself is removed from the list of styles.  This isn't optimised,
+ * since it's not a very frequent operation; we simply scan down the list
+ * to find the previous entry.
+ */
 static void
-freestypat(Stypat p)
+freestypat(Stypat p, Style s, Stypat prev)
 {
+    if (s) {
+	if (prev)
+	    prev->next = p->next;
+	else
+	    s->pats = p->next;
+    }
+
     zsfree(p->pat);
     freepatprog(p->prog);
     if (p->vals)
@@ -68,6 +86,20 @@
     if (p->eval)
 	freeeprog(p->eval);
     zfree(p, sizeof(*p));
+
+    if (s && !s->pats) {
+	/* No patterns left, free style */
+	if (s == zstyles) {
+	    zstyles = s->next;
+	} else {
+	    Style s2;
+	    for (s2 = zstyles; s2->next != s; s2 = s2->next)
+		;
+	    s2->next = s->next;
+	}
+	zsfree(s->name);
+	zfree(s, sizeof(*s));
+    }
 }
 
 static void
@@ -80,7 +112,7 @@
 	sn = s->next;
 	for (p = s->pats; p; p = pn) {
 	    pn = p->next;
-	    freestypat(p);
+	    freestypat(p, NULL, NULL);
 	}
 	zsfree(s->name);
 	zfree(s, sizeof(*s));
@@ -96,8 +128,9 @@
     Style s;
 
     for (s = zstyles; s; s = s->next)
-	if (!strcmp(name, s->name))
+	if (!strcmp(name, s->name)) {
 	    return s;
+	}
 
     return NULL;
 }
@@ -397,27 +430,22 @@
 			    for (q = NULL, p = s->pats; p;
 				 q = p, p = p->next) {
 				if (!strcmp(p->pat, pat)) {
-				    if (q)
-					q->next = p->next;
-				    else
-					s->pats = p->next;
-				    freestypat(p);
+				    freestypat(p, s, q);
 				    break;
 				}
 			    }
 			}
 		    }
 		} else {
+		    Style next;
 		    Stypat p, q;
 
-		    for (s = zstyles; s; s = s->next) {
+		    /* careful! style itself may be deleted */
+		    for (s = zstyles; s; s = next) {
+			next = s->next;
 			for (q = NULL, p = s->pats; p; q = p, p = p->next) {
 			    if (!strcmp(p->pat, args[1])) {
-				if (q)
-				    q->next = p->next;
-				else
-				    s->pats = p->next;
-				freestypat(p);
+				freestypat(p, s, q);
 				break;
 			    }
 			}

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author