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_parse(pkg_t *pkg, const char *depends)
{ {
pkg_dependency_t *list = NULL; 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); list = pkg_dependency_parse_str(list, kvdepends);
free(kvdepends); free(kvdepends);

View File

@ -125,7 +125,7 @@ pkg_fragment_free(pkg_fragment_t *head)
} }
pkg_fragment_t * 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; int i, argc;
char **argv; char **argv;

12
main.c
View File

@ -142,12 +142,16 @@ print_modversion(pkg_t *pkg, void *unused, unsigned int flags)
static void static void
print_variables(pkg_t *pkg, void *unused, unsigned int flags) print_variables(pkg_t *pkg, void *unused, unsigned int flags)
{ {
pkg_tuple_t *node; pkg_node_t *node;
(void) unused; (void) unused;
(void) flags; (void) flags;
PKG_FOREACH_LIST_ENTRY(pkg->vars, node) PKG_FOREACH_LIST_ENTRY(pkg->vars.head, node)
printf("%s\n", node->key); {
pkg_tuple_t *tuple = node->data;
printf("%s\n", tuple->key);
}
} }
static void static void
@ -253,7 +257,7 @@ print_variable(pkg_t *pkg, void *data, unsigned int flags)
const char *var; const char *var;
(void) flags; (void) flags;
var = pkg_tuple_find(pkg->vars, req->variable); var = pkg_tuple_find(&pkg->vars, req->variable);
if (var != NULL) if (var != NULL)
{ {
if (*(req->buf) == '\0') 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 = calloc(sizeof(pkg_t), 1);
pkg->filename = strdup(filename); 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 */ /* make module id */
if ((idptr = strrchr(pkg->filename, PKG_DIR_SEP_S)) != NULL) 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 ':': case ':':
if (!strcasecmp(key, "Name")) 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")) 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")) 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->cflags = 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->libs = 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->libs_private = 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"))
@ -220,7 +220,7 @@ pkg_new_from_file(const char *filename, FILE *f, unsigned int flags)
break; break;
case '=': case '=':
if (!(flags & PKGF_MUNGE_SYSROOT_PREFIX) || strcasecmp(key, "prefix")) 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 else
{ {
char mungebuf[PKG_BUFSIZE]; 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); strlcpy(mungebuf, sysroot_dir, sizeof mungebuf);
strlcat(mungebuf, value, sizeof mungebuf); strlcat(mungebuf, value, sizeof mungebuf);
pkg->vars = pkg_tuple_add(pkg->vars, key, mungebuf); pkg_tuple_add(&pkg->vars, key, mungebuf);
} }
break; break;
default: default:
@ -257,7 +257,7 @@ pkg_free(pkg_t *pkg)
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);
if (pkg->id != NULL) if (pkg->id != NULL)
free(pkg->id); free(pkg->id);
@ -629,10 +629,17 @@ static pkg_t pkg_config_virtual = {
.url = PACKAGE_BUGREPORT, .url = PACKAGE_BUGREPORT,
.version = PKG_PKGCONFIG_VERSION_EQUIV, .version = PKG_PKGCONFIG_VERSION_EQUIV,
.flags = PKG_PROPF_VIRTUAL, .flags = PKG_PROPF_VIRTUAL,
.vars = &(pkg_tuple_t){ .vars = {
.head = &(pkg_node_t){
.prev = NULL,
.next = NULL,
.data = &(pkg_tuple_t){
.key = "pc_path", .key = "pc_path",
.value = PKG_DEFAULT_PATH, .value = PKG_DEFAULT_PATH,
}, },
},
.tail = NULL,
},
}; };
typedef bool (*pkg_vercmp_res_func_t)(pkg_t *pkg, pkg_dependency_t *pkgdep); 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_ {
struct pkg_tuple_ *prev, *next; pkg_node_t iter;
char *key; char *key;
char *value; char *value;
@ -98,7 +98,8 @@ struct pkg_ {
pkg_dependency_t *requires; pkg_dependency_t *requires;
pkg_dependency_t *requires_private; pkg_dependency_t *requires_private;
pkg_dependency_t *conflicts; pkg_dependency_t *conflicts;
pkg_tuple_t *vars;
pkg_list_t vars;
bool uninstalled; bool uninstalled;
@ -156,7 +157,7 @@ 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_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_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_add(pkg_fragment_t *head, const char *string);
pkg_fragment_t *pkg_fragment_copy(pkg_fragment_t *head, pkg_fragment_t *base); 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); char *pkg_fgetline(char *line, size_t size, FILE *stream);
/* tuple.c */ /* tuple.c */
pkg_tuple_t *pkg_tuple_add(pkg_tuple_t *parent, const char *key, const char *value); pkg_tuple_t *pkg_tuple_add(pkg_list_t *parent, const char *key, const char *value);
char *pkg_tuple_find(pkg_tuple_t *head, const char *key); char *pkg_tuple_find(pkg_list_t *list, const char *key);
char *pkg_tuple_parse(pkg_tuple_t *vars, const char *value); char *pkg_tuple_parse(pkg_list_t *list, const char *value);
void pkg_tuple_free(pkg_tuple_t *head); void pkg_tuple_free(pkg_list_t *list);
void pkg_tuple_add_global(const char *key, const char *value); void pkg_tuple_add_global(const char *key, const char *value);
char *pkg_tuple_find_global(const char *key); char *pkg_tuple_find_global(const char *key);
void pkg_tuple_free_global(void); void pkg_tuple_free_global(void);

52
tuple.c
View File

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