parse: replace strdup_parse with pkg_tuple_parse().

pull/15/head
William Pitcock 2012-05-06 20:12:11 -05:00
parent 4ea25d56f4
commit 30fe1fae39
3 changed files with 7 additions and 58 deletions

2
main.c
View File

@ -172,7 +172,7 @@ print_variable(pkg_t *pkg, void *unused)
variable_tmpl = pkg_tuple_find(pkg->vars, want_variable); variable_tmpl = pkg_tuple_find(pkg->vars, want_variable);
if (variable_tmpl != NULL) if (variable_tmpl != NULL)
{ {
char *var = strdup_parse(pkg, variable_tmpl); char *var = pkg_tuple_parse(pkg->vars, variable_tmpl);
printf("%s", var); printf("%s", var);

61
parse.c
View File

@ -24,63 +24,12 @@
#include "pkg.h" #include "pkg.h"
#include "bsdstubs.h" #include "bsdstubs.h"
char *
strdup_parse(pkg_t *pkg, const char *value)
{
char buf[BUFSIZ];
const char *ptr;
char *bptr = buf;
for (ptr = value; *ptr != '\0' && bptr - buf < BUFSIZ; ptr++)
{
if (*ptr != '$')
*bptr++ = *ptr;
else if (*(ptr + 1) == '{')
{
static char varname[BUFSIZ];
char *vptr = varname;
const char *pptr;
char *kv, *parsekv;
*vptr = '\0';
for (pptr = ptr + 2; *pptr != '\0'; pptr++)
{
if (*pptr != '}')
*vptr++ = *pptr;
else
{
*vptr = '\0';
break;
}
}
ptr += (pptr - ptr);
kv = pkg_tuple_find(pkg->vars, varname);
if (kv != NULL)
{
parsekv = strdup_parse(pkg, kv);
strncpy(bptr, parsekv, BUFSIZ - (bptr - buf));
bptr += strlen(parsekv);
free(parsekv);
}
}
}
*bptr = '\0';
return strdup(buf);
}
static pkg_fragment_t * static pkg_fragment_t *
parse_fragment_list(pkg_t *pkg, const char *string) parse_fragment_list(pkg_t *pkg, const char *string)
{ {
int i, argc; int i, argc;
char **argv; char **argv;
char *repstr = strdup_parse(pkg, string); char *repstr = pkg_tuple_parse(pkg->vars, string);
pkg_fragment_t *head = NULL; pkg_fragment_t *head = NULL;
argv_split(repstr, &argc, &argv); argv_split(repstr, &argc, &argv);
@ -166,7 +115,7 @@ parse_deplist(pkg_t *pkg, const char *depends)
pkg_dependency_t *deplist_head = NULL; pkg_dependency_t *deplist_head = NULL;
pkg_comparator_t compare = PKG_ANY; pkg_comparator_t compare = PKG_ANY;
char buf[BUFSIZ]; char buf[BUFSIZ];
char *kvdepends = strdup_parse(pkg, depends); char *kvdepends = pkg_tuple_parse(pkg->vars, depends);
char *start = buf; char *start = buf;
char *ptr = buf; char *ptr = buf;
char *vstart = NULL; char *vstart = NULL;
@ -389,11 +338,11 @@ parse_file(const char *filename, FILE *f)
{ {
case ':': case ':':
if (!strcasecmp(key, "Name")) if (!strcasecmp(key, "Name"))
pkg->realname = strdup_parse(pkg, value); pkg->realname = pkg_tuple_parse(pkg->vars, value);
else if (!strcasecmp(key, "Description")) else if (!strcasecmp(key, "Description"))
pkg->description = strdup_parse(pkg, value); pkg->description = pkg_tuple_parse(pkg->vars, value);
else if (!strcasecmp(key, "Version")) else if (!strcasecmp(key, "Version"))
pkg->version = strdup_parse(pkg, value); pkg->version = pkg_tuple_parse(pkg->vars, value);
else if (!strcasecmp(key, "CFLAGS")) else if (!strcasecmp(key, "CFLAGS"))
pkg->cflags = parse_fragment_list(pkg, value); pkg->cflags = parse_fragment_list(pkg, value);
else if (!strcasecmp(key, "LIBS")) else if (!strcasecmp(key, "LIBS"))

2
pkg.h
View File

@ -137,7 +137,6 @@ const char *pkg_get_comparator(pkg_dependency_t *pkgdep);
pkg_t *parse_file(const char *path, FILE *f); pkg_t *parse_file(const char *path, FILE *f);
pkg_dependency_t *parse_deplist(pkg_t *pkg, const char *depends); pkg_dependency_t *parse_deplist(pkg_t *pkg, const char *depends);
pkg_dependency_t *pkg_dependency_append(pkg_dependency_t *head, pkg_dependency_t *tail); pkg_dependency_t *pkg_dependency_append(pkg_dependency_t *head, pkg_dependency_t *tail);
char *strdup_parse(pkg_t *pkg, const char *value);
/* argvsplit.c */ /* argvsplit.c */
int argv_split(const char *src, int *argc, char ***argv); int argv_split(const char *src, int *argc, char ***argv);
@ -155,5 +154,6 @@ 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_tuple_t *parent, const char *key, const char *value);
char *pkg_tuple_find(pkg_tuple_t *head, const char *key); char *pkg_tuple_find(pkg_tuple_t *head, const char *key);
char *pkg_tuple_parse(pkg_tuple_t *vars, const char *value);
#endif #endif