sh: introduce a function to iterate over all aliases

Currently the data structure holding alias information is opaque for
consumers outside alias.c and there is no way to iterate over all
aliases, which will become needed by a future commit.

The new function "iteralias" takes a null pointer to return the first
alias or an existing alias to return the next one, unless there is
no alias to return, in which case it returns a null pointer.

I slightly changed the static function hashalias so that it returns the
index into the array holding link heads, and not the link head directly.
In this form it's easier to use by iteralias and the slight adjustment
in the three existing callers doesn't look too bad.

Differential Revision:	https://reviews.freebsd.org/D40619
This commit is contained in:
Piotr Pawel Stefaniak 2023-04-29 22:23:59 +02:00
parent 63b6e661d2
commit 2fc4a84ed8
2 changed files with 24 additions and 6 deletions

View file

@ -53,7 +53,7 @@ static int aliases;
static void setalias(const char *, const char *);
static int unalias(const char *);
static struct alias **hashalias(const char *);
static size_t hashalias(const char *);
static
void
@ -62,7 +62,7 @@ setalias(const char *name, const char *val)
struct alias *ap, **app;
unalias(name);
app = hashalias(name);
app = &atab[hashalias(name)];
INTOFF;
ap = ckmalloc(sizeof (struct alias));
ap->name = savestr(name);
@ -87,7 +87,7 @@ unalias(const char *name)
{
struct alias *ap, **app;
app = hashalias(name);
app = &atab[hashalias(name)];
for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
if (equal(name, ap->name)) {
@ -145,7 +145,7 @@ lookupalias(const char *name, int check)
if (aliases == 0)
return (NULL);
for (ap = *hashalias(name); ap; ap = ap->next) {
for (ap = atab[hashalias(name)]; ap; ap = ap->next) {
if (equal(name, ap->name)) {
if (check && (ap->flag & ALIASINUSE))
return (NULL);
@ -242,7 +242,7 @@ unaliascmd(int argc __unused, char **argv __unused)
return (i);
}
static struct alias **
static size_t
hashalias(const char *p)
{
unsigned int hashval;
@ -250,5 +250,22 @@ hashalias(const char *p)
hashval = (unsigned char)*p << 4;
while (*p)
hashval+= *p++;
return &atab[hashval % ATABSIZE];
return (hashval % ATABSIZE);
}
const struct alias *
iteralias(const struct alias *index)
{
size_t i = 0;
if (index != NULL) {
if (index->next != NULL)
return (index->next);
i = hashalias(index->name) + 1;
}
for (; i < ATABSIZE; i++)
if (atab[i] != NULL)
return (atab[i]);
return (NULL);
}

View file

@ -42,3 +42,4 @@ struct alias {
};
struct alias *lookupalias(const char *, int);
const struct alias *iteralias(const struct alias *);