fragment: refactor using pkg_list_t framework

pull/48/head
William Pitcock 2013-03-01 11:10:43 -06:00
parent 74e40fd128
commit 8330056dc1
4 changed files with 102 additions and 118 deletions

View File

@ -16,29 +16,8 @@
#include "pkg.h" #include "pkg.h"
#include "bsdstubs.h" #include "bsdstubs.h"
pkg_fragment_t * void
pkg_fragment_append(pkg_fragment_t *head, pkg_fragment_t *tail) pkg_fragment_add(pkg_list_t *list, const char *string)
{
pkg_fragment_t *node;
if (head == NULL)
return tail;
/* skip to end of list */
PKG_FOREACH_LIST_ENTRY(head, node)
{
if (node->next == NULL)
break;
}
node->next = tail;
tail->prev = node;
return head;
}
pkg_fragment_t *
pkg_fragment_add(pkg_fragment_t *head, const char *string)
{ {
pkg_fragment_t *frag; pkg_fragment_t *frag;
@ -55,77 +34,75 @@ pkg_fragment_add(pkg_fragment_t *head, const char *string)
frag->data = strdup(string); frag->data = strdup(string);
} }
return pkg_fragment_append(head, frag); pkg_node_insert_tail(&frag->iter, frag, list);
} }
static inline pkg_fragment_t * static inline pkg_fragment_t *
pkg_fragment_lookup(pkg_fragment_t *head, pkg_fragment_t *base) pkg_fragment_lookup(pkg_list_t *list, pkg_fragment_t *base)
{ {
pkg_fragment_t *node; pkg_node_t *node;
PKG_FOREACH_LIST_ENTRY(head, node) PKG_FOREACH_LIST_ENTRY(list->head, node)
{ {
if (base->type != node->type) pkg_fragment_t *frag = node->data;
if (base->type != frag->type)
continue; continue;
if (!strcmp(base->data, node->data)) if (!strcmp(base->data, frag->data))
return node; return frag;
} }
return NULL; return NULL;
} }
bool bool
pkg_fragment_exists(pkg_fragment_t *head, pkg_fragment_t *base) pkg_fragment_exists(pkg_list_t *list, pkg_fragment_t *base)
{ {
return pkg_fragment_lookup(head, base) != NULL; return pkg_fragment_lookup(list, base) != NULL;
} }
pkg_fragment_t * void
pkg_fragment_copy(pkg_fragment_t *head, pkg_fragment_t *base) pkg_fragment_copy(pkg_list_t *list, pkg_fragment_t *base)
{ {
pkg_fragment_t *frag; pkg_fragment_t *frag;
if ((frag = pkg_fragment_lookup(head, base)) != NULL) if ((frag = pkg_fragment_lookup(list, base)) != NULL)
{ pkg_fragment_delete(list, frag);
if (head == frag)
head = frag->next;
pkg_fragment_delete(frag);
}
frag = calloc(sizeof(pkg_fragment_t), 1); frag = calloc(sizeof(pkg_fragment_t), 1);
frag->type = base->type; frag->type = base->type;
frag->data = strdup(base->data); frag->data = strdup(base->data);
return pkg_fragment_append(head, frag); pkg_node_insert_tail(&frag->iter, frag, list);
} }
void void
pkg_fragment_delete(pkg_fragment_t *node) pkg_fragment_delete(pkg_list_t *list, pkg_fragment_t *node)
{ {
if (node->prev != NULL) pkg_node_delete(&node->iter, list);
node->prev->next = node->next;
if (node->next != NULL)
node->next->prev = node->prev;
free(node->data); free(node->data);
free(node); free(node);
} }
void void
pkg_fragment_free(pkg_fragment_t *head) pkg_fragment_free(pkg_list_t *list)
{ {
pkg_fragment_t *node, *next; pkg_node_t *node, *next;
PKG_FOREACH_LIST_ENTRY_SAFE(head, next, node) PKG_FOREACH_LIST_ENTRY_SAFE(list->head, next, node)
pkg_fragment_delete(node); {
pkg_fragment_t *frag = node->data;
free(frag->data);
free(frag);
}
} }
pkg_fragment_t * void
pkg_fragment_parse(pkg_fragment_t *head, pkg_list_t *vars, const char *value) pkg_fragment_parse(pkg_list_t *list, pkg_list_t *vars, const char *value)
{ {
int i, argc; int i, argc;
char **argv; char **argv;
@ -134,10 +111,8 @@ pkg_fragment_parse(pkg_fragment_t *head, pkg_list_t *vars, const char *value)
pkg_argv_split(repstr, &argc, &argv); pkg_argv_split(repstr, &argc, &argv);
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
head = pkg_fragment_add(head, argv[i]); pkg_fragment_add(list, argv[i]);
pkg_argv_free(argv); pkg_argv_free(argv);
free(repstr); free(repstr);
return head;
} }

41
main.c
View File

@ -96,12 +96,14 @@ print_list_entry(const pkg_t *entry)
} }
static void static void
print_cflags(pkg_fragment_t *list) print_cflags(pkg_list_t *list)
{ {
pkg_fragment_t *frag; pkg_node_t *node;
PKG_FOREACH_LIST_ENTRY(list, frag) PKG_FOREACH_LIST_ENTRY(list->head, node)
{ {
pkg_fragment_t *frag = node->data;
if ((want_flags & PKG_CFLAGS_ONLY_I) == PKG_CFLAGS_ONLY_I && frag->type != 'I') if ((want_flags & PKG_CFLAGS_ONLY_I) == PKG_CFLAGS_ONLY_I && frag->type != 'I')
continue; continue;
else if ((want_flags & PKG_CFLAGS_ONLY_OTHER) == PKG_CFLAGS_ONLY_OTHER && frag->type == 'I') else if ((want_flags & PKG_CFLAGS_ONLY_OTHER) == PKG_CFLAGS_ONLY_OTHER && frag->type == 'I')
@ -112,12 +114,14 @@ print_cflags(pkg_fragment_t *list)
} }
static void static void
print_libs(pkg_fragment_t *list) print_libs(pkg_list_t *list)
{ {
pkg_fragment_t *frag; pkg_node_t *node;
PKG_FOREACH_LIST_ENTRY(list, frag) PKG_FOREACH_LIST_ENTRY(list->head, node)
{ {
pkg_fragment_t *frag = node->data;
if ((want_flags & PKG_LIBS_ONLY_LDPATH) == PKG_LIBS_ONLY_LDPATH && frag->type != 'L') if ((want_flags & PKG_LIBS_ONLY_LDPATH) == PKG_LIBS_ONLY_LDPATH && frag->type != 'L')
continue; continue;
else if ((want_flags & PKG_LIBS_ONLY_LIBNAME) == PKG_LIBS_ONLY_LIBNAME && frag->type != 'l') else if ((want_flags & PKG_LIBS_ONLY_LIBNAME) == PKG_LIBS_ONLY_LIBNAME && frag->type != 'l')
@ -293,19 +297,19 @@ apply_variable(pkg_t *world, void *variable, int maxdepth, unsigned int flags)
static bool static bool
apply_cflags(pkg_t *world, void *list_head, int maxdepth, unsigned int flags) apply_cflags(pkg_t *world, void *list_head, int maxdepth, unsigned int flags)
{ {
pkg_fragment_t **head = list_head; pkg_list_t *list = list_head;
int eflag; int eflag;
eflag = pkg_cflags(world, head, maxdepth, flags | PKGF_SEARCH_PRIVATE); eflag = pkg_cflags(world, list, maxdepth, flags | PKGF_SEARCH_PRIVATE);
if (eflag != PKG_ERRF_OK) if (eflag != PKG_ERRF_OK)
return false; return false;
if (*head == NULL) if (list->head == NULL)
return true; return true;
print_cflags(*head); print_cflags(list);
pkg_fragment_free(*head); pkg_fragment_free(list);
return true; return true;
} }
@ -313,19 +317,20 @@ apply_cflags(pkg_t *world, void *list_head, int maxdepth, unsigned int flags)
static bool static bool
apply_libs(pkg_t *world, void *list_head, int maxdepth, unsigned int flags) apply_libs(pkg_t *world, void *list_head, int maxdepth, unsigned int flags)
{ {
pkg_fragment_t **head = list_head; pkg_list_t *list = list_head;
int eflag; int eflag;
eflag = pkg_libs(world, head, maxdepth, flags); eflag = pkg_libs(world, list, maxdepth, flags);
if (eflag != PKG_ERRF_OK) if (eflag != PKG_ERRF_OK)
return false; return false;
if (*head == NULL) if (list->head == NULL)
return true; return true;
print_libs(*head); print_libs(list);
pkg_fragment_free(list);
pkg_fragment_free(*head);
return true; return true;
} }
@ -867,7 +872,7 @@ main(int argc, char *argv[])
if ((want_flags & PKG_CFLAGS) == PKG_CFLAGS) if ((want_flags & PKG_CFLAGS) == PKG_CFLAGS)
{ {
pkg_fragment_t *frag_list = NULL; pkg_list_t frag_list = PKG_LIST_INITIALIZER;
if (!pkg_queue_apply(&pkgq, 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))
{ {
@ -878,7 +883,7 @@ main(int argc, char *argv[])
if ((want_flags & PKG_LIBS) == PKG_LIBS) if ((want_flags & PKG_LIBS) == PKG_LIBS)
{ {
pkg_fragment_t *frag_list = NULL; pkg_list_t frag_list = PKG_LIST_INITIALIZER;
if (!pkg_queue_apply(&pkgq, 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))
{ {

65
pkg.c
View File

@ -206,11 +206,11 @@ pkg_new_from_file(const char *filename, FILE *f, unsigned int flags)
else if (!strcasecmp(key, "Version")) else if (!strcasecmp(key, "Version"))
pkg->version = pkg_tuple_parse(&pkg->vars, value); pkg->version = pkg_tuple_parse(&pkg->vars, value);
else if (!strcasecmp(key, "CFLAGS")) else if (!strcasecmp(key, "CFLAGS"))
pkg->cflags = pkg_fragment_parse(pkg->cflags, &pkg->vars, value); pkg_fragment_parse(&pkg->cflags, &pkg->vars, value);
else if (!strcasecmp(key, "LIBS")) else if (!strcasecmp(key, "LIBS"))
pkg->libs = pkg_fragment_parse(pkg->libs, &pkg->vars, value); pkg_fragment_parse(&pkg->libs, &pkg->vars, value);
else if (!strcasecmp(key, "LIBS.private")) else if (!strcasecmp(key, "LIBS.private"))
pkg->libs_private = pkg_fragment_parse(pkg->libs_private, &pkg->vars, value); pkg_fragment_parse(&pkg->libs_private, &pkg->vars, value);
else if (!strcasecmp(key, "Requires")) else if (!strcasecmp(key, "Requires"))
pkg->requires = pkg_dependency_parse(pkg, value); pkg->requires = pkg_dependency_parse(pkg, value);
else if (!strcasecmp(key, "Requires.private")) else if (!strcasecmp(key, "Requires.private"))
@ -253,9 +253,9 @@ pkg_free(pkg_t *pkg)
pkg_dependency_free(pkg->requires_private); pkg_dependency_free(pkg->requires_private);
pkg_dependency_free(pkg->conflicts); pkg_dependency_free(pkg->conflicts);
pkg_fragment_free(pkg->cflags); pkg_fragment_free(&pkg->cflags);
pkg_fragment_free(pkg->libs); pkg_fragment_free(&pkg->libs);
pkg_fragment_free(pkg->libs_private); pkg_fragment_free(&pkg->libs_private);
pkg_tuple_free(&pkg->vars); pkg_tuple_free(&pkg->vars);
@ -962,25 +962,26 @@ pkg_traverse(pkg_t *root,
static void static void
pkg_cflags_collect(pkg_t *pkg, void *data, unsigned int flags) pkg_cflags_collect(pkg_t *pkg, void *data, unsigned int flags)
{ {
pkg_fragment_t **list = data; pkg_list_t *list = data;
pkg_fragment_t *frag; pkg_node_t *node;
(void) flags; (void) flags;
PKG_FOREACH_LIST_ENTRY(pkg->cflags, frag) PKG_FOREACH_LIST_ENTRY(pkg->cflags.head, node)
*list = pkg_fragment_copy(*list, frag); {
pkg_fragment_t *frag = node->data;
pkg_fragment_copy(list, frag);
}
} }
int int
pkg_cflags(pkg_t *root, pkg_fragment_t **list, int maxdepth, unsigned int flags) pkg_cflags(pkg_t *root, pkg_list_t *list, int maxdepth, unsigned int flags)
{ {
int eflag; int eflag;
eflag = pkg_traverse(root, pkg_cflags_collect, list, maxdepth, flags); eflag = pkg_traverse(root, pkg_cflags_collect, list, maxdepth, flags);
if (eflag != PKG_ERRF_OK) if (eflag != PKG_ERRF_OK)
{ pkg_fragment_free(list);
pkg_fragment_free(*list);
*list = NULL;
}
return eflag; return eflag;
} }
@ -988,27 +989,35 @@ pkg_cflags(pkg_t *root, pkg_fragment_t **list, int maxdepth, unsigned int flags)
static void static void
pkg_libs_collect(pkg_t *pkg, void *data, unsigned int flags) pkg_libs_collect(pkg_t *pkg, void *data, unsigned int flags)
{ {
pkg_fragment_t **list = data; pkg_list_t *list = data;
pkg_fragment_t *frag; pkg_node_t *node;
(void) flags; (void) flags;
PKG_FOREACH_LIST_ENTRY(pkg->libs, frag) PKG_FOREACH_LIST_ENTRY(pkg->libs.head, node)
*list = pkg_fragment_copy(*list, frag); {
pkg_fragment_t *frag = node->data;
pkg_fragment_copy(list, frag);
}
} }
static void static void
pkg_libs_private_collect(pkg_t *pkg, void *data, unsigned int flags) pkg_libs_private_collect(pkg_t *pkg, void *data, unsigned int flags)
{ {
pkg_fragment_t **list = data; pkg_list_t *list = data;
pkg_fragment_t *frag; pkg_node_t *node;
(void) flags; (void) flags;
PKG_FOREACH_LIST_ENTRY(pkg->libs_private, frag) PKG_FOREACH_LIST_ENTRY(pkg->libs_private.head, node)
*list = pkg_fragment_copy(*list, frag); {
pkg_fragment_t *frag = node->data;
pkg_fragment_copy(list, frag);
}
} }
int int
pkg_libs(pkg_t *root, pkg_fragment_t **list, int maxdepth, unsigned int flags) pkg_libs(pkg_t *root, pkg_list_t *list, int maxdepth, unsigned int flags)
{ {
int eflag; int eflag;
@ -1016,8 +1025,7 @@ pkg_libs(pkg_t *root, pkg_fragment_t **list, int maxdepth, unsigned int flags)
if (eflag != PKG_ERRF_OK) if (eflag != PKG_ERRF_OK)
{ {
pkg_fragment_free(*list); pkg_fragment_free(list);
*list = NULL;
return eflag; return eflag;
} }
@ -1025,10 +1033,7 @@ pkg_libs(pkg_t *root, pkg_fragment_t **list, int maxdepth, unsigned int flags)
{ {
eflag = pkg_traverse(root, pkg_libs_private_collect, list, maxdepth, flags); eflag = pkg_traverse(root, pkg_libs_private_collect, list, maxdepth, flags);
if (eflag != PKG_ERRF_OK) if (eflag != PKG_ERRF_OK)
{ pkg_fragment_free(list);
pkg_fragment_free(*list);
*list = NULL;
}
} }
return eflag; return eflag;

25
pkg.h
View File

@ -52,7 +52,7 @@ typedef struct pkg_fragment_ pkg_fragment_t;
#define PKG_MAX(a,b) (((a) > (b)) ? (a) : (b)) #define PKG_MAX(a,b) (((a) > (b)) ? (a) : (b))
struct pkg_fragment_ { struct pkg_fragment_ {
struct pkg_fragment_ *prev, *next; pkg_node_t iter;
char type; char type;
char *data; char *data;
@ -91,9 +91,9 @@ struct pkg_ {
char *url; char *url;
char *pc_filedir; char *pc_filedir;
pkg_fragment_t *libs; pkg_list_t libs;
pkg_fragment_t *libs_private; pkg_list_t libs_private;
pkg_fragment_t *cflags; pkg_list_t cflags;
pkg_dependency_t *requires; pkg_dependency_t *requires;
pkg_dependency_t *requires_private; pkg_dependency_t *requires_private;
@ -141,8 +141,8 @@ unsigned int pkg_verify_graph(pkg_t *root, int depth, unsigned int flags);
int pkg_compare_version(const char *a, const char *b); int pkg_compare_version(const char *a, const char *b);
pkg_t *pkg_verify_dependency(pkg_dependency_t *pkgdep, unsigned int flags, unsigned int *eflags); pkg_t *pkg_verify_dependency(pkg_dependency_t *pkgdep, unsigned int flags, unsigned int *eflags);
const char *pkg_get_comparator(pkg_dependency_t *pkgdep); const char *pkg_get_comparator(pkg_dependency_t *pkgdep);
int pkg_cflags(pkg_t *root, pkg_fragment_t **list, int maxdepth, unsigned int flags); int pkg_cflags(pkg_t *root, pkg_list_t *list, int maxdepth, unsigned int flags);
int pkg_libs(pkg_t *root, pkg_fragment_t **list, int maxdepth, unsigned int flags); int pkg_libs(pkg_t *root, pkg_list_t *list, int maxdepth, unsigned int flags);
pkg_comparator_t pkg_comparator_lookup_by_name(const char *name); pkg_comparator_t pkg_comparator_lookup_by_name(const char *name);
/* parse.c */ /* parse.c */
@ -157,13 +157,12 @@ int pkg_argv_split(const char *src, int *argc, char ***argv);
void pkg_argv_free(char **argv); void pkg_argv_free(char **argv);
/* fragment.c */ /* fragment.c */
pkg_fragment_t *pkg_fragment_parse(pkg_fragment_t *head, pkg_list_t *vars, const char *value); void pkg_fragment_parse(pkg_list_t *list, pkg_list_t *vars, const char *value);
pkg_fragment_t *pkg_fragment_append(pkg_fragment_t *head, pkg_fragment_t *tail); void pkg_fragment_add(pkg_list_t *list, const char *string);
pkg_fragment_t *pkg_fragment_add(pkg_fragment_t *head, const char *string); void pkg_fragment_copy(pkg_list_t *list, pkg_fragment_t *base);
pkg_fragment_t *pkg_fragment_copy(pkg_fragment_t *head, pkg_fragment_t *base); void pkg_fragment_delete(pkg_list_t *list, pkg_fragment_t *node);
void pkg_fragment_delete(pkg_fragment_t *node); bool pkg_fragment_exists(pkg_list_t *head, pkg_fragment_t *base);
bool pkg_fragment_exists(pkg_fragment_t *head, pkg_fragment_t *base); void pkg_fragment_free(pkg_list_t *list);
void pkg_fragment_free(pkg_fragment_t *head);
/* fileio.c */ /* fileio.c */
char *pkg_fgetline(char *line, size_t size, FILE *stream); char *pkg_fgetline(char *line, size_t size, FILE *stream);