#include <config.h>
#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#include "sudo.h"
#include "parse.h"
#include "redblack.h"
#include <gram.h>
struct rbtree *aliases;
unsigned int alias_seqno;
int
alias_compare(v1, v2)
const void *v1, *v2;
{
const struct alias *a1 = (const struct alias *)v1;
const struct alias *a2 = (const struct alias *)v2;
int res;
if (v1 == NULL)
res = -1;
else if (v2 == NULL)
res = 1;
else if ((res = strcmp(a1->name, a2->name)) == 0)
res = a1->type - a2->type;
return res;
}
struct alias *
alias_find(name, type)
char *name;
int type;
{
struct alias key;
struct rbnode *node;
struct alias *a = NULL;
key.name = name;
key.type = type;
if ((node = rbfind(aliases, &key)) != NULL) {
a = node->data;
if (a->seqno == alias_seqno) {
errno = ELOOP;
return NULL;
}
a->seqno = alias_seqno;
} else {
errno = ENOENT;
}
return a;
}
char *
alias_add(name, type, members)
char *name;
int type;
struct member *members;
{
static char errbuf[512];
struct alias *a;
a = ecalloc(1, sizeof(*a));
a->name = name;
a->type = type;
list2tq(&a->members, members);
if (rbinsert(aliases, a)) {
snprintf(errbuf, sizeof(errbuf), "Alias `%s' already defined", name);
alias_free(a);
return errbuf;
}
return NULL;
}
void
alias_apply(func, cookie)
int (*func) __P((void *, void *));
void *cookie;
{
rbapply(aliases, func, cookie, inorder);
}
int
no_aliases()
{
return rbisempty(aliases);
}
void
alias_free(v)
void *v;
{
struct alias *a = (struct alias *)v;
struct member *m;
struct sudo_command *c;
void *next;
efree(a->name);
for (m = a->members.first; m != NULL; m = next) {
next = m->next;
if (m->type == COMMAND) {
c = (struct sudo_command *) m->name;
efree(c->cmnd);
efree(c->args);
}
efree(m->name);
efree(m);
}
efree(a);
}
struct alias *
alias_remove(name, type)
char *name;
int type;
{
struct rbnode *node;
struct alias key;
key.name = name;
key.type = type;
if ((node = rbfind(aliases, &key)) == NULL) {
errno = ENOENT;
return NULL;
}
return rbdelete(aliases, node);
}
void
init_aliases()
{
if (aliases != NULL)
rbdestroy(aliases, alias_free);
aliases = rbcreate(alias_compare);
}