2012-05-07 01:18:21 +00:00
|
|
|
/*
|
|
|
|
* tuple.c
|
|
|
|
* management of key->value tuples
|
|
|
|
*
|
2012-07-26 02:09:31 +00:00
|
|
|
* Copyright (c) 2011, 2012 pkgconf authors (see AUTHORS).
|
2012-05-07 01:18:21 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2012-07-20 19:29:58 +00:00
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
2012-05-07 01:18:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pkg.h"
|
|
|
|
#include "bsdstubs.h"
|
|
|
|
|
2013-03-01 16:45:55 +00:00
|
|
|
static pkg_list_t pkg_global_var = PKG_LIST_INITIALIZER;
|
2012-05-07 04:10:41 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
pkg_tuple_add_global(const char *key, const char *value)
|
|
|
|
{
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_tuple_add(&pkg_global_var, key, value);
|
2012-05-07 04:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
pkg_tuple_find_global(const char *key)
|
|
|
|
{
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_node_t *node;
|
2012-05-07 04:10:41 +00:00
|
|
|
|
2013-03-01 16:45:55 +00:00
|
|
|
PKG_FOREACH_LIST_ENTRY(pkg_global_var.head, node)
|
2012-05-07 04:10:41 +00:00
|
|
|
{
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_tuple_t *tuple = node->data;
|
|
|
|
|
2014-02-10 23:53:47 +00:00
|
|
|
if (!strcmp(tuple->key, key))
|
2013-03-01 16:45:55 +00:00
|
|
|
return tuple->value;
|
2012-05-07 04:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pkg_tuple_free_global(void)
|
|
|
|
{
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_tuple_free(&pkg_global_var);
|
2012-05-07 04:10:41 +00:00
|
|
|
}
|
|
|
|
|
2012-05-07 04:32:08 +00:00
|
|
|
void
|
|
|
|
pkg_tuple_define_global(const char *kv)
|
|
|
|
{
|
|
|
|
char *workbuf = strdup(kv);
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
value = strchr(workbuf, '=');
|
|
|
|
if (value == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
*value++ = '\0';
|
|
|
|
pkg_tuple_add_global(workbuf, value);
|
|
|
|
out:
|
|
|
|
free(workbuf);
|
|
|
|
}
|
|
|
|
|
2012-05-07 01:18:21 +00:00
|
|
|
pkg_tuple_t *
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_tuple_add(pkg_list_t *list, const char *key, const char *value)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
|
|
|
pkg_tuple_t *tuple = calloc(sizeof(pkg_tuple_t), 1);
|
|
|
|
|
|
|
|
tuple->key = strdup(key);
|
2013-03-01 16:45:55 +00:00
|
|
|
tuple->value = pkg_tuple_parse(list, value);
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_node_insert(&tuple->iter, tuple, list);
|
2012-05-07 01:18:21 +00:00
|
|
|
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_tuple_find(pkg_list_t *list, const char *key)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_node_t *node;
|
2012-05-07 04:12:43 +00:00
|
|
|
char *res;
|
|
|
|
|
|
|
|
if ((res = pkg_tuple_find_global(key)) != NULL)
|
|
|
|
return res;
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2013-03-01 16:45:55 +00:00
|
|
|
PKG_FOREACH_LIST_ENTRY(list->head, node)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_tuple_t *tuple = node->data;
|
|
|
|
|
2014-02-10 23:53:47 +00:00
|
|
|
if (!strcmp(tuple->key, key))
|
2013-03-01 16:45:55 +00:00
|
|
|
return tuple->value;
|
2012-05-07 01:18:21 +00:00
|
|
|
}
|
|
|
|
|
2012-05-07 04:12:43 +00:00
|
|
|
return NULL;
|
2012-05-07 01:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_tuple_parse(pkg_list_t *vars, const char *value)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2012-07-21 19:38:13 +00:00
|
|
|
char buf[PKG_BUFSIZE];
|
2012-05-07 01:18:21 +00:00
|
|
|
const char *ptr;
|
|
|
|
char *bptr = buf;
|
|
|
|
|
2012-07-21 19:38:13 +00:00
|
|
|
for (ptr = value; *ptr != '\0' && bptr - buf < PKG_BUFSIZE; ptr++)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
|
|
|
if (*ptr != '$')
|
|
|
|
*bptr++ = *ptr;
|
|
|
|
else if (*(ptr + 1) == '{')
|
|
|
|
{
|
2012-07-21 19:38:13 +00:00
|
|
|
static char varname[PKG_BUFSIZE];
|
2012-05-07 01:18:21 +00:00
|
|
|
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(vars, varname);
|
|
|
|
|
|
|
|
if (kv != NULL)
|
|
|
|
{
|
|
|
|
parsekv = pkg_tuple_parse(vars, kv);
|
|
|
|
|
2012-07-21 19:38:13 +00:00
|
|
|
strncpy(bptr, parsekv, PKG_BUFSIZE - (bptr - buf));
|
2012-05-07 01:18:21 +00:00
|
|
|
bptr += strlen(parsekv);
|
|
|
|
|
|
|
|
free(parsekv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*bptr = '\0';
|
|
|
|
|
|
|
|
return strdup(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_tuple_free(pkg_list_t *list)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_node_t *node, *next;
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2013-03-01 16:45:55 +00:00
|
|
|
PKG_FOREACH_LIST_ENTRY_SAFE(list->head, next, node)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2013-03-01 16:45:55 +00:00
|
|
|
pkg_tuple_t *tuple = node->data;
|
|
|
|
|
|
|
|
free(tuple->key);
|
|
|
|
free(tuple->value);
|
|
|
|
free(tuple);
|
2012-05-07 01:18:21 +00:00
|
|
|
}
|
|
|
|
}
|