nuke NEW, DISPOSE, RENEW, and COPYFROMTO macros; (rl_complete): call

rl_list_possib instead of doing the same


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@5886 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Johan Danielsson
1999-04-08 13:42:09 +00:00
parent 98c988dd10
commit afd8b4de9d

View File

@@ -29,9 +29,7 @@ RCSID("$Id$");
** strcmp-like sorting predicate for qsort. ** strcmp-like sorting predicate for qsort.
*/ */
static int static int
compare(p1, p2) compare(const void *p1, const void *p2)
const void *p1;
const void *p2;
{ {
const char **v1; const char **v1;
const char **v2; const char **v2;
@@ -46,10 +44,7 @@ compare(p1, p2)
** Ignore . and .. . ** Ignore . and .. .
*/ */
static int static int
FindMatches(dir, file, avp) FindMatches(char *dir, char *file, char ***avp)
char *dir;
char *file;
char ***avp;
{ {
char **av; char **av;
char **new; char **new;
@@ -73,18 +68,18 @@ FindMatches(dir, file, avp)
continue; continue;
if ((ac % MEM_INC) == 0) { if ((ac % MEM_INC) == 0) {
if ((new = NEW(char*, ac + MEM_INC)) == NULL) if ((new = malloc(sizeof(char*) * (ac + MEM_INC))) == NULL)
break; break;
if (ac) { if (ac) {
COPYFROMTO(new, av, ac * sizeof (char **)); memcpy(new, av, ac * sizeof (char **));
DISPOSE(av); free(av);
} }
*avp = av = new; *avp = av = new;
} }
if ((av[ac] = strdup(p)) == NULL) { if ((av[ac] = strdup(p)) == NULL) {
if (ac == 0) if (ac == 0)
DISPOSE(av); free(av);
break; break;
} }
ac++; ac++;
@@ -100,11 +95,7 @@ FindMatches(dir, file, avp)
/* /*
** Split a pathname into allocated directory and trailing filename parts. ** Split a pathname into allocated directory and trailing filename parts.
*/ */
static int static int SplitPath(char *path, char **dirpart, char **filepart)
SplitPath(path, dirpart, filepart)
char *path;
char **dirpart;
char **filepart;
{ {
static char DOT[] = "."; static char DOT[] = ".";
char *dpart; char *dpart;
@@ -114,7 +105,7 @@ SplitPath(path, dirpart, filepart)
if ((dpart = strdup(DOT)) == NULL) if ((dpart = strdup(DOT)) == NULL)
return -1; return -1;
if ((fpart = strdup(path)) == NULL) { if ((fpart = strdup(path)) == NULL) {
DISPOSE(dpart); free(dpart);
return -1; return -1;
} }
} }
@@ -123,7 +114,7 @@ SplitPath(path, dirpart, filepart)
return -1; return -1;
dpart[fpart - path] = '\0'; dpart[fpart - path] = '\0';
if ((fpart = strdup(++fpart)) == NULL) { if ((fpart = strdup(++fpart)) == NULL) {
DISPOSE(dpart); free(dpart);
return -1; return -1;
} }
} }
@@ -136,14 +127,11 @@ SplitPath(path, dirpart, filepart)
** Attempt to complete the pathname, returning an allocated copy. ** Attempt to complete the pathname, returning an allocated copy.
** Fill in *unique if we completed it, or set it to 0 if ambiguous. ** Fill in *unique if we completed it, or set it to 0 if ambiguous.
*/ */
char *
rl_complete(pathname, unique) static char *
char *pathname; rl_complete_filename(char *pathname, int *unique)
int *unique;
{ {
char **av; char **av;
char *dir;
char *file;
char *new; char *new;
char *p; char *p;
size_t ac; size_t ac;
@@ -152,27 +140,24 @@ rl_complete(pathname, unique)
size_t j; size_t j;
size_t len; size_t len;
if (SplitPath(pathname, &dir, &file) < 0) ac = rl_list_possib(pathname, &av);
return NULL;
if ((ac = FindMatches(dir, file, &av)) == 0) {
DISPOSE(dir);
DISPOSE(file);
return NULL;
}
p = NULL; p = NULL;
len = strlen(file);
if (ac == 1) { if (ac == 1) {
char *s = strrchr(pathname, '/');
/* Exactly one match -- finish it off. */ /* Exactly one match -- finish it off. */
*unique = 1; *unique = 1;
if(s == NULL)
len = strlen(pathname);
else
len = strlen(s + 1);
j = strlen(av[0]) - len + 2; j = strlen(av[0]) - len + 2;
if ((p = NEW(char, j + 1)) != NULL) { if ((p = malloc(j + 1)) != NULL) {
COPYFROMTO(p, av[0] + len, j); memcpy(p, av[0] + len, j);
if ((new = NEW(char, strlen(dir) + strlen(av[0]) + 2)) != NULL) { asprintf(&new, "%s%s", pathname, p);
snprintf (new, sizeof(new), if(new != NULL) {
"%s/%s", dir, av[0]);
rl_add_slash(new, p); rl_add_slash(new, p);
DISPOSE(new); free(new);
} }
} }
} }
@@ -187,8 +172,8 @@ rl_complete(pathname, unique)
breakout: breakout:
if (i > len) { if (i > len) {
j = i - len + 1; j = i - len + 1;
if ((p = NEW(char, j)) != NULL) { if ((p = malloc(j)) != NULL) {
COPYFROMTO(p, av[0] + len, j); memcpy(p, av[0] + len, j);
p[j - 1] = '\0'; p[j - 1] = '\0';
} }
} }
@@ -196,21 +181,34 @@ rl_complete(pathname, unique)
} }
/* Clean up and return. */ /* Clean up and return. */
DISPOSE(dir);
DISPOSE(file);
for (i = 0; i < ac; i++) for (i = 0; i < ac; i++)
DISPOSE(av[i]); free(av[i]);
DISPOSE(av); free(av);
return p; return p;
} }
static rl_complete_func_t complete_func = rl_complete_filename;
char *
rl_complete(char *pathname, int *unique)
{
return (*complete_func)(pathname, unique);
}
rl_complete_func_t
rl_set_complete_func(rl_complete_func_t func)
{
rl_complete_func_t old = complete_func;
complete_func = func;
return old;
}
/* /*
** Return all possible completions. ** Return all possible completions.
*/ */
int static int
rl_list_possib(pathname, avp) rl_list_possib_filename(char *pathname, char ***avp)
char *pathname;
char ***avp;
{ {
char *dir; char *dir;
char *file; char *file;
@@ -219,7 +217,23 @@ rl_list_possib(pathname, avp)
if (SplitPath(pathname, &dir, &file) < 0) if (SplitPath(pathname, &dir, &file) < 0)
return 0; return 0;
ac = FindMatches(dir, file, avp); ac = FindMatches(dir, file, avp);
DISPOSE(dir); free(dir);
DISPOSE(file); free(file);
return ac; return ac;
} }
static rl_list_possib_func_t list_possib_func = rl_list_possib_filename;
int
rl_list_possib(char *pathname, char ***avp)
{
return (*list_possib_func)(pathname, avp);
}
rl_list_possib_func_t
rl_set_list_possib_func(rl_list_possib_func_t func)
{
rl_list_possib_func_t old = list_possib_func;
list_possib_func = func;
return old;
}