queue: refactor to use pkg_list_t framework

feature/tap-sh
William Pitcock 2013-03-01 10:24:57 -06:00
parent b475859cfa
commit ae063d8922
4 changed files with 68 additions and 53 deletions

22
iter.h
View File

@ -51,6 +51,28 @@ pkg_node_insert(pkg_node_t *node, void *data, pkg_list_t *list)
list->head = node;
}
static inline void
pkg_node_insert_tail(pkg_node_t *node, void *data, pkg_list_t *list)
{
pkg_node_t *tnode;
node->data = data;
if (list->head == NULL)
{
list->head = node;
list->tail = node;
return;
}
tnode = list->tail;
node->prev = tnode;
tnode->next = node;
list->tail = node;
}
static inline void
pkg_node_delete(pkg_node_t *node, pkg_list_t *list)
{

40
main.c
View File

@ -487,8 +487,7 @@ int
main(int argc, char *argv[])
{
int ret;
pkg_queue_t *pkgq = NULL;
pkg_queue_t *pkgq_head = NULL;
pkg_list_t pkgq = PKG_LIST_INITIALIZER;
char *builddir;
char *required_pkgconfig_version = NULL;
char *required_exact_module_version = NULL;
@ -750,11 +749,7 @@ main(int argc, char *argv[])
if (argv[pkg_optind + 1] == NULL || !PKG_OPERATOR_CHAR(*(argv[pkg_optind + 1])))
{
pkgq = pkg_queue_push(pkgq, package);
if (pkgq_head == NULL)
pkgq_head = pkgq;
pkg_queue_push(&pkgq, package);
pkg_optind++;
}
else
@ -764,14 +759,11 @@ main(int argc, char *argv[])
snprintf(packagebuf, sizeof packagebuf, "%s %s %s", package, argv[pkg_optind + 1], argv[pkg_optind + 2]);
pkg_optind += 3;
pkgq = pkg_queue_push(pkgq, packagebuf);
if (pkgq_head == NULL)
pkgq_head = pkgq;
pkg_queue_push(&pkgq, packagebuf);
}
}
if (pkgq_head == NULL)
if (pkgq.head == NULL)
{
fprintf(stderr, "Please specify at least one package name on the command line.\n");
return EXIT_FAILURE;
@ -779,7 +771,7 @@ main(int argc, char *argv[])
ret = EXIT_SUCCESS;
if (!pkg_queue_validate(pkgq_head, maximum_traverse_depth, global_traverse_flags))
if (!pkg_queue_validate(&pkgq, maximum_traverse_depth, global_traverse_flags))
{
ret = EXIT_FAILURE;
goto out;
@ -788,7 +780,7 @@ main(int argc, char *argv[])
if ((want_flags & PKG_UNINSTALLED) == PKG_UNINSTALLED)
{
ret = EXIT_FAILURE;
pkg_queue_apply(pkgq_head, apply_uninstalled, maximum_traverse_depth, global_traverse_flags, &ret);
pkg_queue_apply(&pkgq, apply_uninstalled, maximum_traverse_depth, global_traverse_flags, &ret);
goto out;
}
@ -796,7 +788,7 @@ main(int argc, char *argv[])
{
want_flags &= ~(PKG_CFLAGS|PKG_LIBS);
if (!pkg_queue_apply(pkgq_head, apply_digraph, maximum_traverse_depth, global_traverse_flags, NULL))
if (!pkg_queue_apply(&pkgq, apply_digraph, maximum_traverse_depth, global_traverse_flags, NULL))
{
ret = EXIT_FAILURE;
goto out;
@ -807,7 +799,7 @@ main(int argc, char *argv[])
{
want_flags &= ~(PKG_CFLAGS|PKG_LIBS);
if (!pkg_queue_apply(pkgq_head, apply_simulate, -1, global_traverse_flags, NULL))
if (!pkg_queue_apply(&pkgq, apply_simulate, -1, global_traverse_flags, NULL))
{
ret = EXIT_FAILURE;
goto out;
@ -818,7 +810,7 @@ main(int argc, char *argv[])
{
want_flags &= ~(PKG_CFLAGS|PKG_LIBS);
if (!pkg_queue_apply(pkgq_head, apply_modversion, 2, global_traverse_flags, NULL))
if (!pkg_queue_apply(&pkgq, apply_modversion, 2, global_traverse_flags, NULL))
{
ret = EXIT_FAILURE;
goto out;
@ -829,7 +821,7 @@ main(int argc, char *argv[])
{
want_flags &= ~(PKG_CFLAGS|PKG_LIBS);
if (!pkg_queue_apply(pkgq_head, apply_variables, 2, global_traverse_flags, NULL))
if (!pkg_queue_apply(&pkgq, apply_variables, 2, global_traverse_flags, NULL))
{
ret = EXIT_FAILURE;
goto out;
@ -840,7 +832,7 @@ main(int argc, char *argv[])
{
want_flags &= ~(PKG_CFLAGS|PKG_LIBS);
if (!pkg_queue_apply(pkgq_head, apply_variable, 2, global_traverse_flags | PKGF_SKIP_ROOT_VIRTUAL, want_variable))
if (!pkg_queue_apply(&pkgq, apply_variable, 2, global_traverse_flags | PKGF_SKIP_ROOT_VIRTUAL, want_variable))
{
ret = EXIT_FAILURE;
goto out;
@ -851,7 +843,7 @@ main(int argc, char *argv[])
{
want_flags &= ~(PKG_CFLAGS|PKG_LIBS);
if (!pkg_queue_apply(pkgq_head, apply_requires, maximum_traverse_depth, global_traverse_flags, NULL))
if (!pkg_queue_apply(&pkgq, apply_requires, maximum_traverse_depth, global_traverse_flags, NULL))
{
ret = EXIT_FAILURE;
goto out;
@ -862,7 +854,7 @@ main(int argc, char *argv[])
{
want_flags &= ~(PKG_CFLAGS|PKG_LIBS);
if (!pkg_queue_apply(pkgq_head, apply_requires_private, maximum_traverse_depth, global_traverse_flags, NULL))
if (!pkg_queue_apply(&pkgq, apply_requires_private, maximum_traverse_depth, global_traverse_flags, NULL))
{
ret = EXIT_FAILURE;
goto out;
@ -873,7 +865,7 @@ main(int argc, char *argv[])
{
pkg_fragment_t *frag_list = NULL;
if (!pkg_queue_apply(pkgq_head, apply_cflags, maximum_traverse_depth, global_traverse_flags, &frag_list))
if (!pkg_queue_apply(&pkgq, apply_cflags, maximum_traverse_depth, global_traverse_flags, &frag_list))
{
ret = EXIT_FAILURE;
goto out_println;
@ -884,14 +876,14 @@ main(int argc, char *argv[])
{
pkg_fragment_t *frag_list = NULL;
if (!pkg_queue_apply(pkgq_head, apply_libs, maximum_traverse_depth, global_traverse_flags, &frag_list))
if (!pkg_queue_apply(&pkgq, apply_libs, maximum_traverse_depth, global_traverse_flags, &frag_list))
{
ret = EXIT_FAILURE;
goto out_println;
}
}
pkg_queue_free(pkgq_head);
pkg_queue_free(&pkgq);
out_println:
if (want_flags & (PKG_CFLAGS|PKG_LIBS))

15
pkg.h
View File

@ -74,11 +74,6 @@ struct pkg_tuple_ {
char *value;
};
typedef struct pkg_queue_ {
struct pkg_queue_ *prev, *next;
char *package;
} pkg_queue_t;
#define PKG_PROPF_NONE 0x0
#define PKG_PROPF_VIRTUAL 0x1
#define PKG_PROPF_CACHED 0x2
@ -186,11 +181,11 @@ void pkg_tuple_define_global(const char *kv);
extern FILE *error_msgout;
/* queue.c */
pkg_queue_t *pkg_queue_push(pkg_queue_t *parent, const char *package);
bool pkg_queue_compile(pkg_t *world, pkg_queue_t *head);
void pkg_queue_free(pkg_queue_t *head);
bool pkg_queue_apply(pkg_queue_t *head, pkg_queue_apply_func_t func, int maxdepth, unsigned int flags, void *data);
bool pkg_queue_validate(pkg_queue_t *head, int maxdepth, unsigned int flags);
void pkg_queue_push(pkg_list_t *list, const char *package);
bool pkg_queue_compile(pkg_t *world, pkg_list_t *list);
void pkg_queue_free(pkg_list_t *list);
bool pkg_queue_apply(pkg_list_t *list, pkg_queue_apply_func_t func, int maxdepth, unsigned int flags, void *data);
bool pkg_queue_validate(pkg_list_t *list, int maxdepth, unsigned int flags);
/* cache.c */
pkg_t *pkg_cache_lookup(const char *id);

44
queue.c
View File

@ -16,29 +16,33 @@
#include "pkg.h"
#include "bsdstubs.h"
pkg_queue_t *
pkg_queue_push(pkg_queue_t *parent, const char *package)
typedef struct {
pkg_node_t iter;
char *package;
} pkg_queue_t;
void
pkg_queue_push(pkg_list_t *list, const char *package)
{
pkg_queue_t *pkgq = calloc(sizeof(pkg_queue_t), 1);
pkgq->package = strdup(package);
pkgq->prev = parent;
if (pkgq->prev != NULL)
pkgq->prev->next = pkgq;
return pkgq;
pkg_node_insert_tail(&pkgq->iter, pkgq, list);
}
bool
pkg_queue_compile(pkg_t *world, pkg_queue_t *head)
pkg_queue_compile(pkg_t *world, pkg_list_t *list)
{
pkg_queue_t *pkgq;
pkg_node_t *iter;
PKG_FOREACH_LIST_ENTRY(head, pkgq)
PKG_FOREACH_LIST_ENTRY(list->head, iter)
{
pkg_queue_t *pkgq;
pkg_dependency_t *pkgdep;
pkgq = iter->data;
pkgdep = pkg_dependency_parse(world, pkgq->package);
if (pkgdep != NULL)
world->requires = pkg_dependency_append(world->requires, pkgdep);
else
@ -49,28 +53,30 @@ pkg_queue_compile(pkg_t *world, pkg_queue_t *head)
}
void
pkg_queue_free(pkg_queue_t *head)
pkg_queue_free(pkg_list_t *list)
{
pkg_queue_t *pkgq, *next_pkgq;
pkg_node_t *node, *tnode;
PKG_FOREACH_LIST_ENTRY_SAFE(head, next_pkgq, pkgq)
PKG_FOREACH_LIST_ENTRY_SAFE(list->head, tnode, node)
{
pkg_queue_t *pkgq = node->data;
free(pkgq->package);
free(pkgq);
}
}
static inline unsigned int
pkg_queue_verify(pkg_t *world, pkg_queue_t *head, int maxdepth, unsigned int flags)
pkg_queue_verify(pkg_t *world, pkg_list_t *list, int maxdepth, unsigned int flags)
{
if (!pkg_queue_compile(world, head))
if (!pkg_queue_compile(world, list))
return PKG_ERRF_DEPGRAPH_BREAK;
return pkg_verify_graph(world, maxdepth, flags);
}
bool
pkg_queue_apply(pkg_queue_t *head, pkg_queue_apply_func_t func, int maxdepth, unsigned int flags, void *data)
pkg_queue_apply(pkg_list_t *list, pkg_queue_apply_func_t func, int maxdepth, unsigned int flags, void *data)
{
pkg_t world = {
.id = "world",
@ -82,7 +88,7 @@ pkg_queue_apply(pkg_queue_t *head, pkg_queue_apply_func_t func, int maxdepth, un
if (!maxdepth)
maxdepth = -1;
if (pkg_queue_verify(&world, head, maxdepth, flags) != PKG_ERRF_OK)
if (pkg_queue_verify(&world, list, maxdepth, flags) != PKG_ERRF_OK)
return false;
if (!func(&world, data, maxdepth, flags))
@ -97,7 +103,7 @@ pkg_queue_apply(pkg_queue_t *head, pkg_queue_apply_func_t func, int maxdepth, un
}
bool
pkg_queue_validate(pkg_queue_t *head, int maxdepth, unsigned int flags)
pkg_queue_validate(pkg_list_t *list, int maxdepth, unsigned int flags)
{
bool retval = true;
pkg_t world = {
@ -110,7 +116,7 @@ pkg_queue_validate(pkg_queue_t *head, int maxdepth, unsigned int flags)
if (!maxdepth)
maxdepth = -1;
if (pkg_queue_verify(&world, head, maxdepth, flags) != PKG_ERRF_OK)
if (pkg_queue_verify(&world, list, maxdepth, flags) != PKG_ERRF_OK)
retval = false;
pkg_free(&world);