tuple: refactor to use pkg_list_t framework

feature/tap-sh
William Pitcock 2013-03-01 10:45:55 -06:00
parent ae063d8922
commit 74e40fd128
6 changed files with 66 additions and 50 deletions

View File

@ -249,7 +249,7 @@ pkg_dependency_t *
pkg_dependency_parse(pkg_t *pkg, const char *depends)
{
pkg_dependency_t *list = NULL;
char *kvdepends = pkg_tuple_parse(pkg->vars, depends);
char *kvdepends = pkg_tuple_parse(&pkg->vars, depends);
list = pkg_dependency_parse_str(list, kvdepends);
free(kvdepends);

View File

@ -125,7 +125,7 @@ pkg_fragment_free(pkg_fragment_t *head)
}
pkg_fragment_t *
pkg_fragment_parse(pkg_fragment_t *head, pkg_tuple_t *vars, const char *value)
pkg_fragment_parse(pkg_fragment_t *head, pkg_list_t *vars, const char *value)
{
int i, argc;
char **argv;

12
main.c
View File

@ -142,12 +142,16 @@ print_modversion(pkg_t *pkg, void *unused, unsigned int flags)
static void
print_variables(pkg_t *pkg, void *unused, unsigned int flags)
{
pkg_tuple_t *node;
pkg_node_t *node;
(void) unused;
(void) flags;
PKG_FOREACH_LIST_ENTRY(pkg->vars, node)
printf("%s\n", node->key);
PKG_FOREACH_LIST_ENTRY(pkg->vars.head, node)
{
pkg_tuple_t *tuple = node->data;
printf("%s\n", tuple->key);
}
}
static void
@ -253,7 +257,7 @@ print_variable(pkg_t *pkg, void *data, unsigned int flags)
const char *var;
(void) flags;
var = pkg_tuple_find(pkg->vars, req->variable);
var = pkg_tuple_find(&pkg->vars, req->variable);
if (var != NULL)
{
if (*(req->buf) == '\0')

29
pkg.c
View File

@ -154,7 +154,7 @@ pkg_new_from_file(const char *filename, FILE *f, unsigned int flags)
pkg = calloc(sizeof(pkg_t), 1);
pkg->filename = strdup(filename);
pkg->vars = pkg_tuple_add(pkg->vars, "pcfiledir", pkg_get_parent_dir(pkg));
pkg_tuple_add(&pkg->vars, "pcfiledir", pkg_get_parent_dir(pkg));
/* make module id */
if ((idptr = strrchr(pkg->filename, PKG_DIR_SEP_S)) != NULL)
@ -200,17 +200,17 @@ pkg_new_from_file(const char *filename, FILE *f, unsigned int flags)
{
case ':':
if (!strcasecmp(key, "Name"))
pkg->realname = pkg_tuple_parse(pkg->vars, value);
pkg->realname = pkg_tuple_parse(&pkg->vars, value);
else if (!strcasecmp(key, "Description"))
pkg->description = pkg_tuple_parse(pkg->vars, value);
pkg->description = pkg_tuple_parse(&pkg->vars, value);
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"))
pkg->cflags = pkg_fragment_parse(pkg->cflags, pkg->vars, value);
pkg->cflags = pkg_fragment_parse(pkg->cflags, &pkg->vars, value);
else if (!strcasecmp(key, "LIBS"))
pkg->libs = pkg_fragment_parse(pkg->libs, pkg->vars, value);
pkg->libs = pkg_fragment_parse(pkg->libs, &pkg->vars, value);
else if (!strcasecmp(key, "LIBS.private"))
pkg->libs_private = pkg_fragment_parse(pkg->libs_private, pkg->vars, value);
pkg->libs_private = pkg_fragment_parse(pkg->libs_private, &pkg->vars, value);
else if (!strcasecmp(key, "Requires"))
pkg->requires = pkg_dependency_parse(pkg, value);
else if (!strcasecmp(key, "Requires.private"))
@ -220,7 +220,7 @@ pkg_new_from_file(const char *filename, FILE *f, unsigned int flags)
break;
case '=':
if (!(flags & PKGF_MUNGE_SYSROOT_PREFIX) || strcasecmp(key, "prefix"))
pkg->vars = pkg_tuple_add(pkg->vars, key, value);
pkg_tuple_add(&pkg->vars, key, value);
else
{
char mungebuf[PKG_BUFSIZE];
@ -229,7 +229,7 @@ pkg_new_from_file(const char *filename, FILE *f, unsigned int flags)
strlcpy(mungebuf, sysroot_dir, sizeof mungebuf);
strlcat(mungebuf, value, sizeof mungebuf);
pkg->vars = pkg_tuple_add(pkg->vars, key, mungebuf);
pkg_tuple_add(&pkg->vars, key, mungebuf);
}
break;
default:
@ -257,7 +257,7 @@ pkg_free(pkg_t *pkg)
pkg_fragment_free(pkg->libs);
pkg_fragment_free(pkg->libs_private);
pkg_tuple_free(pkg->vars);
pkg_tuple_free(&pkg->vars);
if (pkg->id != NULL)
free(pkg->id);
@ -629,10 +629,17 @@ static pkg_t pkg_config_virtual = {
.url = PACKAGE_BUGREPORT,
.version = PKG_PKGCONFIG_VERSION_EQUIV,
.flags = PKG_PROPF_VIRTUAL,
.vars = &(pkg_tuple_t){
.vars = {
.head = &(pkg_node_t){
.prev = NULL,
.next = NULL,
.data = &(pkg_tuple_t){
.key = "pc_path",
.value = PKG_DEFAULT_PATH,
},
},
.tail = NULL,
},
};
typedef bool (*pkg_vercmp_res_func_t)(pkg_t *pkg, pkg_dependency_t *pkgdep);

15
pkg.h
View File

@ -68,7 +68,7 @@ struct pkg_dependency_ {
};
struct pkg_tuple_ {
struct pkg_tuple_ *prev, *next;
pkg_node_t iter;
char *key;
char *value;
@ -98,7 +98,8 @@ struct pkg_ {
pkg_dependency_t *requires;
pkg_dependency_t *requires_private;
pkg_dependency_t *conflicts;
pkg_tuple_t *vars;
pkg_list_t vars;
bool uninstalled;
@ -156,7 +157,7 @@ int pkg_argv_split(const char *src, int *argc, char ***argv);
void pkg_argv_free(char **argv);
/* fragment.c */
pkg_fragment_t *pkg_fragment_parse(pkg_fragment_t *head, pkg_tuple_t *vars, const char *value);
pkg_fragment_t *pkg_fragment_parse(pkg_fragment_t *head, pkg_list_t *vars, const char *value);
pkg_fragment_t *pkg_fragment_append(pkg_fragment_t *head, pkg_fragment_t *tail);
pkg_fragment_t *pkg_fragment_add(pkg_fragment_t *head, const char *string);
pkg_fragment_t *pkg_fragment_copy(pkg_fragment_t *head, pkg_fragment_t *base);
@ -168,10 +169,10 @@ void pkg_fragment_free(pkg_fragment_t *head);
char *pkg_fgetline(char *line, size_t size, FILE *stream);
/* tuple.c */
pkg_tuple_t *pkg_tuple_add(pkg_tuple_t *parent, const char *key, const char *value);
char *pkg_tuple_find(pkg_tuple_t *head, const char *key);
char *pkg_tuple_parse(pkg_tuple_t *vars, const char *value);
void pkg_tuple_free(pkg_tuple_t *head);
pkg_tuple_t *pkg_tuple_add(pkg_list_t *parent, const char *key, const char *value);
char *pkg_tuple_find(pkg_list_t *list, const char *key);
char *pkg_tuple_parse(pkg_list_t *list, const char *value);
void pkg_tuple_free(pkg_list_t *list);
void pkg_tuple_add_global(const char *key, const char *value);
char *pkg_tuple_find_global(const char *key);
void pkg_tuple_free_global(void);

52
tuple.c
View File

@ -16,23 +16,25 @@
#include "pkg.h"
#include "bsdstubs.h"
static pkg_tuple_t *pkg_global_var;
static pkg_list_t pkg_global_var = PKG_LIST_INITIALIZER;
void
pkg_tuple_add_global(const char *key, const char *value)
{
pkg_global_var = pkg_tuple_add(pkg_global_var, key, value);
pkg_tuple_add(&pkg_global_var, key, value);
}
char *
pkg_tuple_find_global(const char *key)
{
pkg_tuple_t *node;
pkg_node_t *node;
PKG_FOREACH_LIST_ENTRY(pkg_global_var, node)
PKG_FOREACH_LIST_ENTRY(pkg_global_var.head, node)
{
if (!strcasecmp(node->key, key))
return node->value;
pkg_tuple_t *tuple = node->data;
if (!strcasecmp(tuple->key, key))
return tuple->value;
}
return NULL;
@ -41,7 +43,7 @@ pkg_tuple_find_global(const char *key)
void
pkg_tuple_free_global(void)
{
pkg_tuple_free(pkg_global_var);
pkg_tuple_free(&pkg_global_var);
}
void
@ -61,40 +63,40 @@ out:
}
pkg_tuple_t *
pkg_tuple_add(pkg_tuple_t *parent, const char *key, const char *value)
pkg_tuple_add(pkg_list_t *list, const char *key, const char *value)
{
pkg_tuple_t *tuple = calloc(sizeof(pkg_tuple_t), 1);
tuple->key = strdup(key);
tuple->value = pkg_tuple_parse(parent, value);
tuple->value = pkg_tuple_parse(list, value);
tuple->next = parent;
if (tuple->next != NULL)
tuple->next->prev = tuple;
pkg_node_insert(&tuple->iter, tuple, list);
return tuple;
}
char *
pkg_tuple_find(pkg_tuple_t *head, const char *key)
pkg_tuple_find(pkg_list_t *list, const char *key)
{
pkg_tuple_t *node;
pkg_node_t *node;
char *res;
if ((res = pkg_tuple_find_global(key)) != NULL)
return res;
PKG_FOREACH_LIST_ENTRY(head, node)
PKG_FOREACH_LIST_ENTRY(list->head, node)
{
if (!strcasecmp(node->key, key))
return node->value;
pkg_tuple_t *tuple = node->data;
if (!strcasecmp(tuple->key, key))
return tuple->value;
}
return NULL;
}
char *
pkg_tuple_parse(pkg_tuple_t *vars, const char *value)
pkg_tuple_parse(pkg_list_t *vars, const char *value)
{
char buf[PKG_BUFSIZE];
const char *ptr;
@ -145,14 +147,16 @@ pkg_tuple_parse(pkg_tuple_t *vars, const char *value)
}
void
pkg_tuple_free(pkg_tuple_t *head)
pkg_tuple_free(pkg_list_t *list)
{
pkg_tuple_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)
{
free(node->key);
free(node->value);
free(node);
pkg_tuple_t *tuple = node->data;
free(tuple->key);
free(tuple->value);
free(tuple);
}
}