1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-09-03 10:21:46 +02:00

23670: rationalise some linked list functions

This commit is contained in:
Peter Stephenson 2007-06-27 13:56:10 +00:00
parent 4be5febd94
commit 4d52b7ebe6
9 changed files with 90 additions and 99 deletions

View file

@ -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:

View file

@ -792,19 +792,6 @@ modpmparamscan(HashNode hn, UNUSED(int dummy))
modpmfound = 1;
}
/**/
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. */

View file

@ -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). */

View file

@ -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;

View file

@ -273,18 +273,10 @@ newsizedlist(int size)
return list;
}
/**/
mod_export int
listcontains(LinkList list, void *dat)
{
LinkNode node;
for (node = firstnode(list); node; incnode(node))
if (getdata(node) == dat)
return 1;
return 0;
}
/*
* Return the node whose data is the pointer "dat", else NULL.
* Can be used as a boolean test.
*/
/**/
mod_export LinkNode
@ -298,3 +290,67 @@ linknodebydatum(LinkList list, void *dat)
return NULL;
}
/*
* Return the node whose data matches the string "dat", else NULL.
*/
/**/
mod_export LinkNode
linknodebystring(LinkList list, char *dat)
{
LinkNode node;
for (node = firstnode(list); node; incnode(node))
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;
}

View file

@ -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++;

View file

@ -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;
}

View file

@ -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)) {

View file

@ -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;
}