about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--Src/Modules/parameter.c38
-rw-r--r--Src/Zle/compcore.c21
-rw-r--r--Src/Zle/computil.c19
-rw-r--r--Src/linklist.c68
-rw-r--r--Src/loop.c10
-rw-r--r--Src/module.c4
-rw-r--r--Src/parse.c2
-rw-r--r--Src/subst.c9
9 files changed, 84 insertions, 93 deletions
diff --git a/ChangeLog b/ChangeLog
index c13f1431b..d3e15ac5e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-06-27  Peter Stephenson  <pws@csr.com>
+
+	* 23670: Src/linklist.c, Src/loop.c, Src/module.c, Src/parse.c,
+	Src/subst.c, Src/Modules/parameter.c, Src/Zle/compcore.c,
+	Src/Zle/computil.c: Rationalise some linked list functions.
+
 2007-06-26  Peter Stephenson  <p.w.stephenson@ntlworld.com>
 
 	* 23606: Src/mkbltnmlst.sh, Src/module.c, Test/V01zmodload.ztst:
diff --git a/Src/Modules/parameter.c b/Src/Modules/parameter.c
index bf21b5cda..9112e64f1 100644
--- a/Src/Modules/parameter.c
+++ b/Src/Modules/parameter.c
@@ -793,19 +793,6 @@ modpmparamscan(HashNode hn, UNUSED(int dummy))
 }
 
 /**/
-static int
-findmodnode(LinkList l, char *nam)
-{
-    LinkNode node;
-
-    for (node = firstnode(l); node; incnode(node))
-	if (!strcmp(nam, (char *) getdata(node)))
-	    return 1;
-
-    return 0;
-}
-
-/**/
 static HashNode
 getpmmodule(UNUSED(HashTable ht), char *name)
 {
@@ -889,14 +876,14 @@ scanpmmodules(UNUSED(HashTable ht), ScanFunc func, int flags)
     for (i = 0; i < builtintab->hsize; i++)
 	for (hn = builtintab->nodes[i]; hn; hn = hn->next) {
 	    if (!(((Builtin) hn)->node.flags & BINF_ADDED) &&
-		!findmodnode(done, ((Builtin) hn)->optstr)) {
+		!linknodebystring(done, ((Builtin) hn)->optstr)) {
 		pm.node.nam = ((Builtin) hn)->optstr;
 		addlinknode(done, pm.node.nam);
 		func(&pm.node, flags);
 	    }
 	}
     for (p = condtab; p; p = p->next)
-	if (p->module && !findmodnode(done, p->module)) {
+	if (p->module && !linknodebystring(done, p->module)) {
 	    pm.node.nam = p->module;
 	    addlinknode(done, pm.node.nam);
 	    func(&pm.node, flags);
@@ -904,7 +891,7 @@ scanpmmodules(UNUSED(HashTable ht), ScanFunc func, int flags)
     for (i = 0; i < realparamtab->hsize; i++)
 	for (hn = realparamtab->nodes[i]; hn; hn = hn->next) {
 	    if ((((Param) hn)->node.flags & PM_AUTOLOAD) &&
-		!findmodnode(done, ((Param) hn)->u.str)) {
+		!linknodebystring(done, ((Param) hn)->u.str)) {
 		pm.node.nam = ((Param) hn)->u.str;
 		addlinknode(done, pm.node.nam);
 		func(&pm.node, flags);
@@ -934,15 +921,7 @@ dirssetfn(UNUSED(Param pm), char **x)
 static char **
 dirsgetfn(UNUSED(Param pm))
 {
-    int l = countlinknodes(dirstack);
-    char **ret = (char **) zhalloc((l + 1) * sizeof(char *)), **p;
-    LinkNode n;
-
-    for (n = firstnode(dirstack), p = ret; n; incnode(n), p++)
-	*p = dupstring((char *) getdata(n));
-    *p = NULL;
-
-    return ret;
+    return hlinklist2array(dirstack, 1);
 }
 
 /* Functions for the history special parameter. */
@@ -1012,7 +991,7 @@ scanpmhistory(UNUSED(HashTable ht), ScanFunc func, int flags)
 static char **
 histwgetfn(UNUSED(Param pm))
 {
-    char **ret, **p, *h, *e, sav;
+    char *h, *e, sav;
     LinkList l = newlinklist(), ll;
     LinkNode n;
     int i = addhistnum(curhist, -1, HIST_FOREIGN), iw;
@@ -1033,13 +1012,8 @@ histwgetfn(UNUSED(Param pm))
 	}
 	he = up_histent(he);
     }
-    ret = (char **) zhalloc((countlinknodes(l) + 1) * sizeof(char *));
-
-    for (p = ret, n = firstnode(l); n; incnode(n), p++)
-	*p = (char *) getdata(n);
-    *p = NULL;
 
-    return ret;
+    return hlinklist2array(l, 0);
 }
 
 /* Functions for the jobtexts special parameter. */
diff --git a/Src/Zle/compcore.c b/Src/Zle/compcore.c
index 3e4f690f3..348054e86 100644
--- a/Src/Zle/compcore.c
+++ b/Src/Zle/compcore.c
@@ -644,7 +644,7 @@ callcompfunc(char *s, char *fn)
 	if (compredirs)
 	    freearray(compredirs);
         if (rdstrs)
-            compredirs = bld_list_array(rdstrs);
+            compredirs = zlinklist2array(rdstrs);
         else
             compredirs = (char **) zshcalloc(sizeof(char *));
 
@@ -1852,30 +1852,13 @@ set_comp_sep(void)
     return 0;
 }
 
-/* This builds an array from a list of strings. */
-
-/**/
-mod_export char **
-bld_list_array(LinkList l)
-{
-    char **a, **p;
-    LinkNode n;
-
-    a = (char **) zalloc((countlinknodes(l) + 1) * sizeof(char *));
-    for (p = a, n = firstnode(l); n; incnode(n))
-	*p++ = ztrdup((char *) getdata(n));
-    *p = NULL;
-
-    return a;
-}
-
 /* This stores the strings from the list in an array. */
 
 /**/
 mod_export void
 set_list_array(char *name, LinkList l)
 {
-    setaparam(name, bld_list_array(l));
+    setaparam(name, zlinklist2array(l));
 }
 
 /* Get the words from a variable or a (list of words). */
diff --git a/Src/Zle/computil.c b/Src/Zle/computil.c
index 0d8cf5364..b9df1e418 100644
--- a/Src/Zle/computil.c
+++ b/Src/Zle/computil.c
@@ -3406,16 +3406,9 @@ bin_compvalues(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
         /* Again, as for comparguments.  This returns the values and their
          * arguments as an array which will be stored in val_args in _values. */
 	if (cv_laststate.vals) {
-	    char **ret, **p;
-	    LinkNode n;
-
-	    ret = (char **) zalloc((countlinknodes(cv_laststate.vals) + 1) *
-				   sizeof(char *));
-
-	    for (n = firstnode(cv_laststate.vals), p = ret; n; incnode(n), p++)
-		*p = ztrdup((char *) getdata(n));
-	    *p = NULL;
+	    char **ret;
 
+	    ret = zlinklist2array(cv_laststate.vals);
 	    sethparam(args[1], ret);
 
 	    return 0;
@@ -3738,7 +3731,6 @@ bin_comptry(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 	if (!strcmp(*args, "-m")) {
 	    char *s, *p, *q, *c, **all = comptags[lasttaglevel]->all;
 	    LinkList list = newlinklist();
-	    LinkNode node;
 	    int num = 0;
 	    Ctset set;
 
@@ -3833,16 +3825,11 @@ bin_comptry(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 		    }
 		}
 		if (num) {
-		    char **a;
 		    Ctset l;
 
 		    set = (Ctset) zalloc(sizeof(*set));
 
-		    a = set->tags = (char **) zalloc((num + 1) * sizeof(char *));
-		    for (node = firstnode(list); node; incnode(node))
-			*a++ = ztrdup((char *) getdata(node));
-
-		    *a = NULL;
+		    set->tags = zlinklist2array(list);
 		    set->next = NULL;
 		    set->ptr = NULL;
 		    set->tag = NULL;
diff --git a/Src/linklist.c b/Src/linklist.c
index 26b2c5e0e..b51d88161 100644
--- a/Src/linklist.c
+++ b/Src/linklist.c
@@ -273,28 +273,84 @@ newsizedlist(int size)
     return list;
 }
 
+/*
+ * Return the node whose data is the pointer "dat", else NULL.
+ * Can be used as a boolean test.
+ */
+
 /**/
-mod_export int
-listcontains(LinkList list, void *dat)
+mod_export LinkNode
+linknodebydatum(LinkList list, void *dat)
 {
     LinkNode node;
 
     for (node = firstnode(list); node; incnode(node))
 	if (getdata(node) == dat)
-	    return 1;
+	    return node;
 
-    return 0;
+    return NULL;
 }
 
+/*
+ * Return the node whose data matches the string "dat", else NULL.
+ */
+
 /**/
 mod_export LinkNode
-linknodebydatum(LinkList list, void *dat)
+linknodebystring(LinkList list, char *dat)
 {
     LinkNode node;
 
     for (node = firstnode(list); node; incnode(node))
-	if (getdata(node) == dat)
+	if (!strcmp((char *)getdata(node), dat))
 	    return node;
 
     return NULL;
 }
+
+/*
+ * Convert a linked list whose data elements are strings to
+ * an array.  Memory is off the heap and the elements of the
+ * array are the same elements as the linked list data if copy is
+ * 0, else copied onto the heap.
+ */
+
+/**/
+mod_export char **
+hlinklist2array(LinkList list, int copy)
+{
+    int l = countlinknodes(list);
+    char **ret = (char **) zhalloc((l + 1) * sizeof(char *)), **p;
+    LinkNode n;
+
+    for (n = firstnode(list), p = ret; n; incnode(n), p++) {
+	*p = (char *) getdata(n);
+	if (copy)
+	    *p = dupstring(*p);
+    }
+    *p = NULL;
+
+    return ret;
+}
+
+/*
+ * Convert a linked list whose data elements are strings to
+ * an array.  The result is a permanently allocated, freearrayable
+ * array.
+ */
+
+/**/
+mod_export char **
+zlinklist2array(LinkList list)
+{
+    int l = countlinknodes(list);
+    char **ret = (char **) zalloc((l + 1) * sizeof(char *)), **p;
+    LinkNode n;
+
+    for (n = firstnode(list), p = ret; n; incnode(n), p++) {
+	*p = ztrdup((char *) getdata(n));
+    }
+    *p = NULL;
+
+    return ret;
+}
diff --git a/Src/loop.c b/Src/loop.c
index b20e379b8..9e19f3f93 100644
--- a/Src/loop.c
+++ b/Src/loop.c
@@ -311,20 +311,14 @@ size_t
 selectlist(LinkList l, size_t start)
 {
     size_t longest = 1, fct, fw = 0, colsz, t0, t1, ct;
-    LinkNode n;
     char **arr, **ap;
 
     trashzleptr();
-    ct = countlinknodes(l);
-    ap = arr = (char **) zhalloc((countlinknodes(l) + 1) * sizeof(char **));
-
-    for (n = (LinkNode) firstnode(l); n; incnode(n))
-	*ap++ = (char *)getdata(n);
-    *ap = NULL;
+    arr = hlinklist2array(l, 0);
     for (ap = arr; *ap; ap++)
 	if (strlen(*ap) > longest)
 	    longest = strlen(*ap);
-    t0 = ct;
+    t0 = ct = ap - arr;
     longest++;
     while (t0)
 	t0 /= 10, longest++;
diff --git a/Src/module.c b/Src/module.c
index f5f500315..c82d77373 100644
--- a/Src/module.c
+++ b/Src/module.c
@@ -2623,9 +2623,7 @@ unload_module(Module m, LinkNode node)
 	}
 	if(!m->deps) {
 	    if (!node) {
-		for (node = firstnode(modules); node; incnode(node))
-		    if (m == (Module) getdata(node))
-			break;
+		node = linknodebydatum(modules, m);
 		if (!node)
 		    return 1;
 	    }
diff --git a/Src/parse.c b/Src/parse.c
index 4fc592075..30f3abe50 100644
--- a/Src/parse.c
+++ b/Src/parse.c
@@ -2933,7 +2933,7 @@ build_cur_dump(char *nam, char *dump, char **names, int match, int map,
 	    }
 	    for (i = 0; i < shfunctab->hsize; i++)
 		for (hn = shfunctab->nodes[i]; hn; hn = hn->next)
-		    if (!listcontains(lnames, hn->nam) &&
+		    if (!linknodebydatum(lnames, hn->nam) &&
 			pattry(pprog, hn->nam) &&
 			cur_add_func(nam, (Shfunc) hn, lnames, progs,
 				     &hlen, &tlen, what)) {
diff --git a/Src/subst.c b/Src/subst.c
index 0cb8d48bf..6ce723acd 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2973,14 +2973,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 	else if (!nextnode(firstnode(list)))
 	    val = getdata(firstnode(list));
 	else {
-	    char **ap;
-	    LinkNode node;
-
-	    aval = ap = (char **) zhalloc((countlinknodes(list) + 1) *
-					  sizeof(char *));
-	    for (node = firstnode(list); node; incnode(node))
-		*ap++ = (char *) getdata(node);
-	    *ap = NULL;
+	    aval = hlinklist2array(list, 0);
 	    isarr = 2;
 	    l->list.flags |= LF_ARRAY;
 	}