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
|
|
|
*/
|
|
|
|
|
2015-09-06 14:35:08 +00:00
|
|
|
#include <libpkgconf/libpkgconf.h>
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2012-05-07 04:10:41 +00:00
|
|
|
void
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_add_global(pkgconf_client_t *client, const char *key, const char *value)
|
2012-05-07 04:10:41 +00:00
|
|
|
{
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_add(client, &client->global_vars, key, value, false);
|
2012-05-07 04:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_find_global(const pkgconf_client_t *client, const char *key)
|
2012-05-07 04:10:41 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_t *node;
|
2012-05-07 04:10:41 +00:00
|
|
|
|
2016-12-01 21:05:03 +00:00
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(client->global_vars.head, node)
|
2012-05-07 04:10:41 +00:00
|
|
|
{
|
2015-09-06 15:41:40 +00:00
|
|
|
pkgconf_tuple_t *tuple = node->data;
|
2013-03-01 16:45:55 +00:00
|
|
|
|
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
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_free_global(pkgconf_client_t *client)
|
2012-05-07 04:10:41 +00:00
|
|
|
{
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_free(&client->global_vars);
|
2012-05-07 04:10:41 +00:00
|
|
|
}
|
|
|
|
|
2012-05-07 04:32:08 +00:00
|
|
|
void
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_define_global(pkgconf_client_t *client, const char *kv)
|
2012-05-07 04:32:08 +00:00
|
|
|
{
|
|
|
|
char *workbuf = strdup(kv);
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
value = strchr(workbuf, '=');
|
|
|
|
if (value == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
*value++ = '\0';
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_add_global(client, workbuf, value);
|
2012-05-07 04:32:08 +00:00
|
|
|
out:
|
|
|
|
free(workbuf);
|
|
|
|
}
|
|
|
|
|
2016-12-10 02:58:15 +00:00
|
|
|
static void
|
|
|
|
pkgconf_tuple_find_delete(pkgconf_list_t *list, const char *key)
|
|
|
|
{
|
|
|
|
pkgconf_node_t *node, *next;
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY_SAFE(list->head, next, node)
|
|
|
|
{
|
|
|
|
pkgconf_tuple_t *tuple = node->data;
|
|
|
|
|
|
|
|
if (!strcmp(tuple->key, key))
|
|
|
|
{
|
|
|
|
pkgconf_tuple_free_entry(tuple, list);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 15:41:40 +00:00
|
|
|
pkgconf_tuple_t *
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_add(const pkgconf_client_t *client, pkgconf_list_t *list, const char *key, const char *value, bool parse)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2015-09-06 15:41:40 +00:00
|
|
|
pkgconf_tuple_t *tuple = calloc(sizeof(pkgconf_tuple_t), 1);
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2016-12-10 02:58:15 +00:00
|
|
|
pkgconf_tuple_find_delete(list, key);
|
|
|
|
|
2012-05-07 01:18:21 +00:00
|
|
|
tuple->key = strdup(key);
|
2015-12-07 23:02:14 +00:00
|
|
|
if (parse)
|
2016-12-01 21:05:03 +00:00
|
|
|
tuple->value = pkgconf_tuple_parse(client, list, value);
|
2015-12-07 23:02:14 +00:00
|
|
|
else
|
|
|
|
tuple->value = strdup(value);
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_insert(&tuple->iter, tuple, list);
|
2012-05-07 01:18:21 +00:00
|
|
|
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_find(const pkgconf_client_t *client, pkgconf_list_t *list, const char *key)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_t *node;
|
2012-05-07 04:12:43 +00:00
|
|
|
char *res;
|
|
|
|
|
2016-12-01 21:05:03 +00:00
|
|
|
if ((res = pkgconf_tuple_find_global(client, key)) != NULL)
|
2012-05-07 04:12:43 +00:00
|
|
|
return res;
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2015-09-06 15:41:40 +00:00
|
|
|
pkgconf_tuple_t *tuple = node->data;
|
2013-03-01 16:45:55 +00:00
|
|
|
|
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 *
|
2016-12-01 21:05:03 +00:00
|
|
|
pkgconf_tuple_parse(const pkgconf_client_t *client, pkgconf_list_t *vars, const char *value)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2015-09-06 16:29:56 +00:00
|
|
|
char buf[PKGCONF_BUFSIZE];
|
2012-05-07 01:18:21 +00:00
|
|
|
const char *ptr;
|
|
|
|
char *bptr = buf;
|
|
|
|
|
2016-12-10 03:32:55 +00:00
|
|
|
if (*value == '/' && client->sysroot_dir != NULL && strncmp(value, client->sysroot_dir, strlen(client->sysroot_dir)))
|
|
|
|
bptr += strlcpy(buf, client->sysroot_dir, sizeof buf);
|
|
|
|
|
2015-09-06 16:29:56 +00:00
|
|
|
for (ptr = value; *ptr != '\0' && bptr - buf < PKGCONF_BUFSIZE; ptr++)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2014-07-02 04:44:49 +00:00
|
|
|
if (*ptr != '$' || (*ptr == '$' && *(ptr + 1) != '{'))
|
2012-05-07 01:18:21 +00:00
|
|
|
*bptr++ = *ptr;
|
|
|
|
else if (*(ptr + 1) == '{')
|
|
|
|
{
|
2015-09-06 16:29:56 +00:00
|
|
|
static char varname[PKGCONF_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);
|
2016-12-01 21:05:03 +00:00
|
|
|
kv = pkgconf_tuple_find_global(client, varname);
|
2012-05-07 01:18:21 +00:00
|
|
|
if (kv != NULL)
|
|
|
|
{
|
2015-12-07 23:02:14 +00:00
|
|
|
strncpy(bptr, kv, PKGCONF_BUFSIZE - (bptr - buf));
|
|
|
|
bptr += strlen(kv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-01 21:05:03 +00:00
|
|
|
kv = pkgconf_tuple_find(client, vars, varname);
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2015-12-07 23:02:14 +00:00
|
|
|
if (kv != NULL)
|
|
|
|
{
|
2016-12-01 21:05:03 +00:00
|
|
|
parsekv = pkgconf_tuple_parse(client, vars, kv);
|
2015-12-07 23:02:14 +00:00
|
|
|
|
|
|
|
strncpy(bptr, parsekv, PKGCONF_BUFSIZE - (bptr - buf));
|
|
|
|
bptr += strlen(parsekv);
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2015-12-07 23:02:14 +00:00
|
|
|
free(parsekv);
|
|
|
|
}
|
2012-05-07 01:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*bptr = '\0';
|
|
|
|
|
|
|
|
return strdup(buf);
|
|
|
|
}
|
|
|
|
|
2016-12-10 02:50:33 +00:00
|
|
|
void
|
|
|
|
pkgconf_tuple_free_entry(pkgconf_tuple_t *tuple, pkgconf_list_t *list)
|
|
|
|
{
|
|
|
|
pkgconf_node_delete(&tuple->iter, list);
|
|
|
|
|
|
|
|
free(tuple->key);
|
|
|
|
free(tuple->value);
|
|
|
|
free(tuple);
|
|
|
|
}
|
|
|
|
|
2012-05-07 01:18:21 +00:00
|
|
|
void
|
2015-09-06 15:41:40 +00:00
|
|
|
pkgconf_tuple_free(pkgconf_list_t *list)
|
2012-05-07 01:18:21 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_t *node, *next;
|
2012-05-07 01:18:21 +00:00
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
PKGCONF_FOREACH_LIST_ENTRY_SAFE(list->head, next, node)
|
2016-12-10 02:50:33 +00:00
|
|
|
pkgconf_tuple_free_entry(node->data, list);
|
2012-05-07 01:18:21 +00:00
|
|
|
}
|