2012-05-03 20:22:19 +00:00
|
|
|
/*
|
|
|
|
* fragment.c
|
|
|
|
* Management of fragment lists.
|
|
|
|
*
|
2013-12-27 11:41:23 +00:00
|
|
|
* Copyright (c) 2012, 2013, 2014 pkgconf authors (see AUTHORS).
|
2012-05-03 20:22:19 +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-03 20:22:19 +00:00
|
|
|
*/
|
|
|
|
|
2017-09-18 04:38:25 +00:00
|
|
|
#include <libpkgconf/stdinc.h>
|
2015-09-06 14:35:08 +00:00
|
|
|
#include <libpkgconf/libpkgconf.h>
|
2012-05-03 20:22:19 +00:00
|
|
|
|
2016-12-16 00:27:57 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* libpkgconf `fragment` module
|
|
|
|
* ============================
|
|
|
|
*
|
|
|
|
* The `fragment` module provides low-level management and rendering of fragment lists. A
|
|
|
|
* `fragment list` contains various `fragments` of text (such as ``-I /usr/include``) in a matter
|
|
|
|
* which is composable, mergeable and reorderable.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 22:05:04 +00:00
|
|
|
struct pkgconf_fragment_check {
|
|
|
|
char *token;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
pkgconf_fragment_is_unmergeable(const char *string)
|
|
|
|
{
|
2017-09-09 01:09:12 +00:00
|
|
|
static const struct pkgconf_fragment_check check_fragments[] = {
|
2016-05-19 22:05:04 +00:00
|
|
|
{"-framework", 10},
|
|
|
|
{"-isystem", 8},
|
2016-12-30 08:47:42 +00:00
|
|
|
{"-idirafter", 10},
|
2017-05-10 00:30:02 +00:00
|
|
|
{"-pthread", 8},
|
2017-05-10 00:34:01 +00:00
|
|
|
{"-Wa,", 4},
|
2017-05-10 00:30:02 +00:00
|
|
|
{"-Wl,", 4},
|
2017-05-10 00:34:01 +00:00
|
|
|
{"-Wp,", 4},
|
|
|
|
{"-trigraphs", 10},
|
|
|
|
{"-pedantic", 9},
|
|
|
|
{"-ansi", 5},
|
|
|
|
{"-std=", 5},
|
|
|
|
{"-stdlib=", 8},
|
2017-05-10 00:37:23 +00:00
|
|
|
{"-include", 8},
|
2017-05-10 00:38:57 +00:00
|
|
|
{"-nostdinc", 9},
|
|
|
|
{"-nostdlibinc", 12},
|
|
|
|
{"-nobuiltininc", 13},
|
2016-05-19 22:05:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (*string != '-')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < PKGCONF_ARRAY_SIZE(check_fragments); i++)
|
|
|
|
if (!strncmp(string, check_fragments[i].token, check_fragments[i].len))
|
|
|
|
return true;
|
|
|
|
|
2017-01-07 21:04:34 +00:00
|
|
|
/* only one pair of {-flag, arg} may be merged together */
|
|
|
|
if (strchr(string, ' ') != NULL)
|
|
|
|
return false;
|
|
|
|
|
2016-05-19 22:05:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:39:42 +00:00
|
|
|
static inline bool
|
|
|
|
pkgconf_fragment_should_munge(const char *string, const char *sysroot_dir)
|
|
|
|
{
|
|
|
|
if (*string != '/')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (sysroot_dir != NULL && strncmp(sysroot_dir, string, strlen(sysroot_dir)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-19 22:05:04 +00:00
|
|
|
static inline bool
|
|
|
|
pkgconf_fragment_is_special(const char *string)
|
|
|
|
{
|
|
|
|
if (*string != '-')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!strncmp(string, "-lib:", 5))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return pkgconf_fragment_is_unmergeable(string);
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:51:02 +00:00
|
|
|
static inline void
|
2022-07-26 18:00:22 +00:00
|
|
|
pkgconf_fragment_munge(const pkgconf_client_t *client, char *buf, size_t buflen, const char *source, const char *sysroot_dir, unsigned int flags)
|
2016-08-24 23:51:02 +00:00
|
|
|
{
|
|
|
|
*buf = '\0';
|
|
|
|
|
2022-07-26 18:00:22 +00:00
|
|
|
if (!(flags & PKGCONF_PKG_PROPF_UNINSTALLED) || (client->flags & PKGCONF_PKG_PKGF_PKGCONF1_SYSROOT_RULES))
|
|
|
|
{
|
|
|
|
if (sysroot_dir == NULL)
|
|
|
|
sysroot_dir = pkgconf_tuple_find_global(client, "pc_sysrootdir");
|
2016-08-24 23:51:02 +00:00
|
|
|
|
2022-07-26 18:00:22 +00:00
|
|
|
if (sysroot_dir != NULL && pkgconf_fragment_should_munge(source, sysroot_dir))
|
|
|
|
pkgconf_strlcat(buf, sysroot_dir, buflen);
|
|
|
|
}
|
2016-08-24 23:51:02 +00:00
|
|
|
|
2016-12-10 22:19:40 +00:00
|
|
|
pkgconf_strlcat(buf, source, buflen);
|
2017-01-14 02:06:48 +00:00
|
|
|
|
2017-02-03 19:17:21 +00:00
|
|
|
if (*buf == '/' && !(client->flags & PKGCONF_PKG_PKGF_DONT_RELOCATE_PATHS))
|
2017-01-14 02:06:48 +00:00
|
|
|
pkgconf_path_relocate(buf, buflen);
|
2016-08-24 23:51:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:23:51 +00:00
|
|
|
static inline char *
|
2022-07-26 18:00:22 +00:00
|
|
|
pkgconf_fragment_copy_munged(const pkgconf_client_t *client, const char *source, unsigned int flags)
|
2014-07-09 06:23:51 +00:00
|
|
|
{
|
2017-10-16 17:56:19 +00:00
|
|
|
char mungebuf[PKGCONF_ITEM_SIZE];
|
2022-07-26 18:00:22 +00:00
|
|
|
pkgconf_fragment_munge(client, mungebuf, sizeof mungebuf, source, client->sysroot_dir, flags);
|
2014-07-09 06:23:51 +00:00
|
|
|
return strdup(mungebuf);
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:27:57 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
2022-07-26 18:00:22 +00:00
|
|
|
* .. c:function:: void pkgconf_fragment_add(const pkgconf_client_t *client, pkgconf_list_t *list, const char *string, unsigned int flags)
|
2016-12-16 00:27:57 +00:00
|
|
|
*
|
|
|
|
* Adds a `fragment` of text to a `fragment list`, possibly modifying the fragment if a sysroot is set.
|
|
|
|
*
|
|
|
|
* :param pkgconf_client_t* client: The pkgconf client being accessed.
|
|
|
|
* :param pkgconf_list_t* list: The fragment list.
|
|
|
|
* :param char* string: The string of text to add as a fragment to the fragment list.
|
2022-07-26 18:00:22 +00:00
|
|
|
* :param uint flags: Parsing-related flags for the package.
|
2016-12-16 00:27:57 +00:00
|
|
|
* :return: nothing
|
|
|
|
*/
|
2013-03-01 17:10:43 +00:00
|
|
|
void
|
2022-07-26 18:00:22 +00:00
|
|
|
pkgconf_fragment_add(const pkgconf_client_t *client, pkgconf_list_t *list, const char *string, unsigned int flags)
|
2012-05-03 20:22:19 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *frag;
|
2012-05-03 20:22:19 +00:00
|
|
|
|
2016-10-25 18:57:42 +00:00
|
|
|
if (*string == '\0')
|
|
|
|
return;
|
|
|
|
|
2020-05-29 17:35:42 +00:00
|
|
|
if (strlen(string) > 1 && !pkgconf_fragment_is_special(string))
|
2012-05-03 20:22:19 +00:00
|
|
|
{
|
2023-11-05 22:17:02 +00:00
|
|
|
frag = calloc(1, sizeof(pkgconf_fragment_t));
|
2013-12-27 11:37:24 +00:00
|
|
|
|
2012-05-03 20:22:19 +00:00
|
|
|
frag->type = *(string + 1);
|
2022-07-26 18:00:22 +00:00
|
|
|
frag->data = pkgconf_fragment_copy_munged(client, string + 2, flags);
|
2017-02-25 21:53:50 +00:00
|
|
|
|
|
|
|
PKGCONF_TRACE(client, "added fragment {%c, '%s'} to list @%p", frag->type, frag->data, list);
|
2012-05-03 20:22:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-16 17:56:19 +00:00
|
|
|
char mungebuf[PKGCONF_ITEM_SIZE];
|
2016-08-24 23:51:02 +00:00
|
|
|
|
2020-05-24 20:40:47 +00:00
|
|
|
if (list->tail != NULL && list->tail->data != NULL &&
|
|
|
|
!(client->flags & PKGCONF_PKG_PKGF_DONT_MERGE_SPECIAL_FRAGMENTS))
|
2013-12-27 11:37:24 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *parent = list->tail->data;
|
2013-12-27 11:37:24 +00:00
|
|
|
|
2017-01-07 21:04:34 +00:00
|
|
|
/* only attempt to merge 'special' fragments together */
|
2022-08-16 18:46:43 +00:00
|
|
|
if (!parent->type && parent->data != NULL && pkgconf_fragment_is_unmergeable(parent->data))
|
2013-12-27 11:37:24 +00:00
|
|
|
{
|
2016-08-24 23:51:02 +00:00
|
|
|
size_t len;
|
2013-12-27 11:37:24 +00:00
|
|
|
char *newdata;
|
|
|
|
|
2022-07-26 18:00:22 +00:00
|
|
|
pkgconf_fragment_munge(client, mungebuf, sizeof mungebuf, string, NULL, flags);
|
2016-08-24 23:51:02 +00:00
|
|
|
|
|
|
|
len = strlen(parent->data) + strlen(mungebuf) + 2;
|
2013-12-27 11:37:24 +00:00
|
|
|
newdata = malloc(len);
|
2016-08-24 23:51:02 +00:00
|
|
|
|
2016-12-10 22:19:40 +00:00
|
|
|
pkgconf_strlcpy(newdata, parent->data, len);
|
|
|
|
pkgconf_strlcat(newdata, " ", len);
|
|
|
|
pkgconf_strlcat(newdata, mungebuf, len);
|
2013-12-27 11:37:24 +00:00
|
|
|
|
2017-02-25 21:53:50 +00:00
|
|
|
PKGCONF_TRACE(client, "merging '%s' to '%s' to form fragment {'%s'} in list @%p", mungebuf, parent->data, newdata, list);
|
|
|
|
|
2013-12-27 11:37:24 +00:00
|
|
|
free(parent->data);
|
|
|
|
parent->data = newdata;
|
2017-12-10 06:39:39 +00:00
|
|
|
parent->merged = true;
|
2013-12-27 11:37:24 +00:00
|
|
|
|
2017-01-07 21:04:34 +00:00
|
|
|
/* use a copy operation to force a dedup */
|
|
|
|
pkgconf_node_delete(&parent->iter, list);
|
2017-01-20 00:23:25 +00:00
|
|
|
pkgconf_fragment_copy(client, list, parent, false);
|
2017-01-07 21:04:34 +00:00
|
|
|
|
|
|
|
/* the fragment list now (maybe) has the copied node, so free the original */
|
|
|
|
free(parent->data);
|
|
|
|
free(parent);
|
|
|
|
|
2013-12-27 11:37:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-05 22:17:02 +00:00
|
|
|
frag = calloc(1, sizeof(pkgconf_fragment_t));
|
2013-12-27 11:37:24 +00:00
|
|
|
|
2012-05-03 20:22:19 +00:00
|
|
|
frag->type = 0;
|
|
|
|
frag->data = strdup(string);
|
2017-02-25 21:53:50 +00:00
|
|
|
|
|
|
|
PKGCONF_TRACE(client, "created special fragment {'%s'} in list @%p", frag->data, list);
|
2012-05-03 20:22:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_insert_tail(&frag->iter, frag, list);
|
2012-05-03 20:22:19 +00:00
|
|
|
}
|
2012-05-03 20:27:19 +00:00
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
static inline pkgconf_fragment_t *
|
2016-12-10 04:06:42 +00:00
|
|
|
pkgconf_fragment_lookup(pkgconf_list_t *list, const pkgconf_fragment_t *base)
|
2012-05-03 20:43:09 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_t *node;
|
2012-05-03 20:43:09 +00:00
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
PKGCONF_FOREACH_LIST_ENTRY_REVERSE(list->tail, node)
|
2012-05-03 20:43:09 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *frag = node->data;
|
2013-03-01 17:10:43 +00:00
|
|
|
|
|
|
|
if (base->type != frag->type)
|
2012-05-03 20:43:09 +00:00
|
|
|
continue;
|
|
|
|
|
2013-03-01 17:10:43 +00:00
|
|
|
if (!strcmp(base->data, frag->data))
|
|
|
|
return frag;
|
2012-05-03 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 22:06:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-15 22:29:07 +00:00
|
|
|
static inline bool
|
2016-12-10 04:06:42 +00:00
|
|
|
pkgconf_fragment_can_merge_back(const pkgconf_fragment_t *base, unsigned int flags, bool is_private)
|
2013-03-15 22:29:07 +00:00
|
|
|
{
|
2015-05-22 04:06:38 +00:00
|
|
|
(void) flags;
|
|
|
|
|
|
|
|
if (base->type == 'l')
|
|
|
|
{
|
|
|
|
if (is_private)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-23 03:44:33 +00:00
|
|
|
|
2013-03-15 22:29:07 +00:00
|
|
|
if (base->type == 'F')
|
|
|
|
return false;
|
2015-07-19 20:49:36 +00:00
|
|
|
if (base->type == 'L')
|
|
|
|
return false;
|
2015-07-19 21:00:35 +00:00
|
|
|
if (base->type == 'I')
|
|
|
|
return false;
|
2013-03-15 22:29:07 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
2016-12-10 04:06:42 +00:00
|
|
|
pkgconf_fragment_can_merge(const pkgconf_fragment_t *base, unsigned int flags, bool is_private)
|
2013-03-15 22:29:07 +00:00
|
|
|
{
|
2013-08-23 03:44:33 +00:00
|
|
|
(void) flags;
|
|
|
|
|
2015-05-22 04:06:38 +00:00
|
|
|
if (is_private)
|
|
|
|
return false;
|
|
|
|
|
2016-05-19 22:05:04 +00:00
|
|
|
return pkgconf_fragment_is_unmergeable(base->data);
|
2013-03-15 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
static inline pkgconf_fragment_t *
|
2016-12-10 04:06:42 +00:00
|
|
|
pkgconf_fragment_exists(pkgconf_list_t *list, const pkgconf_fragment_t *base, unsigned int flags, bool is_private)
|
2012-08-09 22:06:09 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
if (!pkgconf_fragment_can_merge_back(base, flags, is_private))
|
2013-03-15 22:29:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
if (!pkgconf_fragment_can_merge(base, flags, is_private))
|
2013-03-15 22:29:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
return pkgconf_fragment_lookup(list, base);
|
2012-05-03 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 18:39:10 +00:00
|
|
|
static inline bool
|
|
|
|
pkgconf_fragment_should_merge(const pkgconf_fragment_t *base)
|
|
|
|
{
|
|
|
|
const pkgconf_fragment_t *parent;
|
|
|
|
|
|
|
|
/* if we are the first fragment, that means the next fragment is the same, so it's always safe. */
|
|
|
|
if (base->iter.prev == NULL)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* this really shouldn't ever happen, but handle it */
|
|
|
|
parent = base->iter.prev->data;
|
|
|
|
if (parent == NULL)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
switch (parent->type)
|
|
|
|
{
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
case 'I':
|
|
|
|
return true;
|
|
|
|
default:
|
2017-01-07 21:04:34 +00:00
|
|
|
return !base->type || parent->type == base->type;
|
2016-12-30 18:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 02:06:17 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: bool pkgconf_fragment_has_system_dir(const pkgconf_client_t *client, const pkgconf_fragment_t *frag)
|
|
|
|
*
|
|
|
|
* Checks if a `fragment` contains a `system path`. System paths are detected at compile time and optionally overridden by
|
|
|
|
* the ``PKG_CONFIG_SYSTEM_INCLUDE_PATH`` and ``PKG_CONFIG_SYSTEM_LIBRARY_PATH`` environment variables.
|
|
|
|
*
|
|
|
|
* :param pkgconf_client_t* client: The pkgconf client object the fragment belongs to.
|
|
|
|
* :param pkgconf_fragment_t* frag: The fragment being checked.
|
|
|
|
* :return: true if the fragment contains a system path, else false
|
|
|
|
* :rtype: bool
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
pkgconf_fragment_has_system_dir(const pkgconf_client_t *client, const pkgconf_fragment_t *frag)
|
|
|
|
{
|
|
|
|
const pkgconf_list_t *check_paths = NULL;
|
|
|
|
|
|
|
|
switch (frag->type)
|
|
|
|
{
|
|
|
|
case 'L':
|
|
|
|
check_paths = &client->filter_libdirs;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
check_paths = &client->filter_includedirs;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkgconf_path_match_list(frag->data, check_paths);
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:27:57 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
2017-01-20 00:23:25 +00:00
|
|
|
* .. c:function:: void pkgconf_fragment_copy(const pkgconf_client_t *client, pkgconf_list_t *list, const pkgconf_fragment_t *base, bool is_private)
|
2016-12-16 00:27:57 +00:00
|
|
|
*
|
|
|
|
* Copies a `fragment` to another `fragment list`, possibly removing a previous copy of the `fragment`
|
|
|
|
* in a process known as `mergeback`.
|
|
|
|
*
|
2017-01-20 00:23:25 +00:00
|
|
|
* :param pkgconf_client_t* client: The pkgconf client being accessed.
|
2016-12-16 00:27:57 +00:00
|
|
|
* :param pkgconf_list_t* list: The list the fragment is being added to.
|
|
|
|
* :param pkgconf_fragment_t* base: The fragment being copied.
|
|
|
|
* :param bool is_private: Whether the fragment list is a `private` fragment list (static linking).
|
|
|
|
* :return: nothing
|
|
|
|
*/
|
2013-03-01 17:10:43 +00:00
|
|
|
void
|
2017-01-20 00:23:25 +00:00
|
|
|
pkgconf_fragment_copy(const pkgconf_client_t *client, pkgconf_list_t *list, const pkgconf_fragment_t *base, bool is_private)
|
2012-05-03 20:31:33 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *frag;
|
2012-05-03 20:31:33 +00:00
|
|
|
|
2017-01-20 00:23:25 +00:00
|
|
|
if ((frag = pkgconf_fragment_exists(list, base, client->flags, is_private)) != NULL)
|
2016-12-30 18:39:10 +00:00
|
|
|
{
|
|
|
|
if (pkgconf_fragment_should_merge(frag))
|
|
|
|
pkgconf_fragment_delete(list, frag);
|
|
|
|
}
|
2017-01-20 00:23:25 +00:00
|
|
|
else if (!is_private && !pkgconf_fragment_can_merge_back(base, client->flags, is_private) && (pkgconf_fragment_lookup(list, base) != NULL))
|
2013-03-15 22:29:07 +00:00
|
|
|
return;
|
2012-05-03 20:43:09 +00:00
|
|
|
|
2023-11-05 22:17:02 +00:00
|
|
|
frag = calloc(1, sizeof(pkgconf_fragment_t));
|
2012-05-03 20:31:33 +00:00
|
|
|
|
|
|
|
frag->type = base->type;
|
2017-12-12 00:25:55 +00:00
|
|
|
frag->merged = base->merged;
|
|
|
|
if (base->data != NULL)
|
|
|
|
frag->data = strdup(base->data);
|
2012-05-03 20:31:33 +00:00
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_insert_tail(&frag->iter, frag, list);
|
2012-05-03 20:31:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 19:48:02 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: void pkgconf_fragment_copy_list(const pkgconf_client_t *client, pkgconf_list_t *list, const pkgconf_list_t *base)
|
|
|
|
*
|
|
|
|
* Copies a `fragment list` to another `fragment list`, possibly removing a previous copy of the fragments
|
|
|
|
* in a process known as `mergeback`.
|
|
|
|
*
|
|
|
|
* :param pkgconf_client_t* client: The pkgconf client being accessed.
|
|
|
|
* :param pkgconf_list_t* list: The list the fragments are being added to.
|
|
|
|
* :param pkgconf_list_t* base: The list the fragments are being copied from.
|
|
|
|
* :return: nothing
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pkgconf_fragment_copy_list(const pkgconf_client_t *client, pkgconf_list_t *list, const pkgconf_list_t *base)
|
|
|
|
{
|
|
|
|
pkgconf_node_t *node;
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(base->head, node)
|
|
|
|
{
|
|
|
|
pkgconf_fragment_t *frag = node->data;
|
|
|
|
|
|
|
|
pkgconf_fragment_copy(client, list, frag, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:27:57 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
2017-01-20 00:23:25 +00:00
|
|
|
* .. c:function:: void pkgconf_fragment_filter(const pkgconf_client_t *client, pkgconf_list_t *dest, pkgconf_list_t *src, pkgconf_fragment_filter_func_t filter_func)
|
2016-12-16 00:27:57 +00:00
|
|
|
*
|
|
|
|
* Copies a `fragment list` to another `fragment list` which match a user-specified filtering function.
|
|
|
|
*
|
|
|
|
* :param pkgconf_client_t* client: The pkgconf client being accessed.
|
|
|
|
* :param pkgconf_list_t* dest: The destination list.
|
|
|
|
* :param pkgconf_list_t* src: The source list.
|
|
|
|
* :param pkgconf_fragment_filter_func_t filter_func: The filter function to use.
|
2017-01-19 02:25:19 +00:00
|
|
|
* :param void* data: Optional data to pass to the filter function.
|
2016-12-16 00:27:57 +00:00
|
|
|
* :return: nothing
|
|
|
|
*/
|
2016-12-10 04:18:59 +00:00
|
|
|
void
|
2017-01-20 00:23:25 +00:00
|
|
|
pkgconf_fragment_filter(const pkgconf_client_t *client, pkgconf_list_t *dest, pkgconf_list_t *src, pkgconf_fragment_filter_func_t filter_func, void *data)
|
2016-12-10 04:18:59 +00:00
|
|
|
{
|
|
|
|
pkgconf_node_t *node;
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(src->head, node)
|
|
|
|
{
|
|
|
|
pkgconf_fragment_t *frag = node->data;
|
|
|
|
|
2017-01-20 00:23:25 +00:00
|
|
|
if (filter_func(client, frag, data))
|
|
|
|
pkgconf_fragment_copy(client, dest, frag, true);
|
2016-12-10 04:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 21:13:14 +00:00
|
|
|
static inline char *
|
|
|
|
fragment_quote(const pkgconf_fragment_t *frag)
|
2017-02-25 21:32:49 +00:00
|
|
|
{
|
2018-01-22 21:13:14 +00:00
|
|
|
const char *src = frag->data;
|
|
|
|
ssize_t outlen = strlen(src) + 10;
|
|
|
|
char *out, *dst;
|
2017-12-12 00:25:55 +00:00
|
|
|
|
|
|
|
if (frag->data == NULL)
|
2018-01-22 21:13:14 +00:00
|
|
|
return NULL;
|
|
|
|
|
2023-11-05 22:17:02 +00:00
|
|
|
out = dst = calloc(1, outlen);
|
2017-02-25 21:32:49 +00:00
|
|
|
|
2018-01-22 21:13:14 +00:00
|
|
|
for (; *src; src++)
|
2017-02-25 21:32:49 +00:00
|
|
|
{
|
2017-03-29 23:00:57 +00:00
|
|
|
if (((*src < ' ') ||
|
2017-12-12 00:25:55 +00:00
|
|
|
(*src >= (' ' + (frag->merged ? 1 : 0)) && *src < '$') ||
|
2017-02-25 21:32:49 +00:00
|
|
|
(*src > '$' && *src < '(') ||
|
|
|
|
(*src > ')' && *src < '+') ||
|
|
|
|
(*src > ':' && *src < '=') ||
|
|
|
|
(*src > '=' && *src < '@') ||
|
2020-05-30 18:39:43 +00:00
|
|
|
(*src > 'Z' && *src < '\\') ||
|
|
|
|
#ifndef _WIN32
|
|
|
|
(*src == '\\') ||
|
|
|
|
#endif
|
|
|
|
(*src > '\\' && *src < '^') ||
|
2017-02-25 21:32:49 +00:00
|
|
|
(*src == '`') ||
|
|
|
|
(*src > 'z' && *src < '~') ||
|
2017-12-15 04:41:14 +00:00
|
|
|
(*src > '~')))
|
2018-01-22 21:13:14 +00:00
|
|
|
*dst++ = '\\';
|
|
|
|
|
|
|
|
*dst++ = *src;
|
|
|
|
|
|
|
|
if ((ptrdiff_t)(dst - out) + 2 > outlen)
|
|
|
|
{
|
2020-05-30 21:48:40 +00:00
|
|
|
ptrdiff_t offset = dst - out;
|
2018-01-22 21:13:14 +00:00
|
|
|
outlen *= 2;
|
|
|
|
out = realloc(out, outlen);
|
2020-05-30 21:48:40 +00:00
|
|
|
dst = out + offset;
|
2018-01-22 21:13:14 +00:00
|
|
|
}
|
2017-02-25 21:32:49 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 21:13:14 +00:00
|
|
|
*dst = 0;
|
|
|
|
return out;
|
2017-02-25 21:32:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 00:32:43 +00:00
|
|
|
static inline size_t
|
2017-12-12 00:25:55 +00:00
|
|
|
pkgconf_fragment_len(const pkgconf_fragment_t *frag)
|
2016-12-16 00:32:43 +00:00
|
|
|
{
|
|
|
|
size_t len = 1;
|
|
|
|
|
|
|
|
if (frag->type)
|
|
|
|
len += 2;
|
|
|
|
|
|
|
|
if (frag->data != NULL)
|
2017-02-25 21:32:49 +00:00
|
|
|
{
|
2018-01-22 21:13:14 +00:00
|
|
|
char *quoted = fragment_quote(frag);
|
|
|
|
len += strlen(quoted);
|
|
|
|
free(quoted);
|
2017-02-25 21:32:49 +00:00
|
|
|
}
|
2016-12-16 00:32:43 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2017-12-08 01:13:35 +00:00
|
|
|
static size_t
|
|
|
|
fragment_render_len(const pkgconf_list_t *list, bool escape)
|
2016-12-10 05:07:15 +00:00
|
|
|
{
|
2017-12-12 00:25:55 +00:00
|
|
|
(void) escape;
|
|
|
|
|
2016-12-10 21:47:21 +00:00
|
|
|
size_t out = 1; /* trailing nul */
|
2016-12-10 05:07:15 +00:00
|
|
|
pkgconf_node_t *node;
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
|
|
|
|
{
|
|
|
|
const pkgconf_fragment_t *frag = node->data;
|
2017-12-12 00:25:55 +00:00
|
|
|
out += pkgconf_fragment_len(frag);
|
2016-12-10 05:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-12-08 01:13:35 +00:00
|
|
|
static void
|
|
|
|
fragment_render_buf(const pkgconf_list_t *list, char *buf, size_t buflen, bool escape)
|
2016-12-10 05:07:15 +00:00
|
|
|
{
|
2017-12-12 00:25:55 +00:00
|
|
|
(void) escape;
|
|
|
|
|
2016-12-10 05:07:15 +00:00
|
|
|
pkgconf_node_t *node;
|
|
|
|
char *bptr = buf;
|
|
|
|
|
|
|
|
memset(buf, 0, buflen);
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
|
|
|
|
{
|
|
|
|
const pkgconf_fragment_t *frag = node->data;
|
2016-12-16 00:32:43 +00:00
|
|
|
size_t buf_remaining = buflen - (bptr - buf);
|
2018-01-22 21:13:14 +00:00
|
|
|
char *quoted = fragment_quote(frag);
|
2016-12-16 00:32:43 +00:00
|
|
|
|
2018-01-22 21:13:14 +00:00
|
|
|
if (strlen(quoted) > buf_remaining)
|
|
|
|
{
|
|
|
|
free(quoted);
|
2016-12-16 00:32:43 +00:00
|
|
|
break;
|
2018-01-22 21:13:14 +00:00
|
|
|
}
|
2017-12-12 00:25:55 +00:00
|
|
|
|
2016-12-10 05:07:15 +00:00
|
|
|
if (frag->type)
|
|
|
|
{
|
|
|
|
*bptr++ = '-';
|
|
|
|
*bptr++ = frag->type;
|
|
|
|
}
|
|
|
|
|
2018-01-22 21:13:14 +00:00
|
|
|
if (quoted != NULL)
|
|
|
|
{
|
|
|
|
bptr += pkgconf_strlcpy(bptr, quoted, buf_remaining);
|
|
|
|
free(quoted);
|
|
|
|
}
|
2016-12-10 05:07:15 +00:00
|
|
|
|
2023-01-21 20:17:33 +00:00
|
|
|
if (node->next != NULL)
|
|
|
|
*bptr++ = ' ';
|
2016-12-10 05:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*bptr = '\0';
|
|
|
|
}
|
|
|
|
|
2017-12-08 01:13:35 +00:00
|
|
|
static const pkgconf_fragment_render_ops_t default_render_ops = {
|
|
|
|
.render_len = fragment_render_len,
|
|
|
|
.render_buf = fragment_render_buf
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: size_t pkgconf_fragment_render_len(const pkgconf_list_t *list, bool escape, const pkgconf_fragment_render_ops_t *ops)
|
|
|
|
*
|
|
|
|
* Calculates the required memory to store a `fragment list` when rendered as a string.
|
|
|
|
*
|
|
|
|
* :param pkgconf_list_t* list: The `fragment list` being rendered.
|
2017-12-10 06:36:20 +00:00
|
|
|
* :param bool escape: Whether or not to escape special shell characters (deprecated).
|
2017-12-08 01:13:35 +00:00
|
|
|
* :param pkgconf_fragment_render_ops_t* ops: An optional ops structure to use for custom renderers, else ``NULL``.
|
|
|
|
* :return: the amount of bytes required to represent the `fragment list` when rendered
|
|
|
|
* :rtype: size_t
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
pkgconf_fragment_render_len(const pkgconf_list_t *list, bool escape, const pkgconf_fragment_render_ops_t *ops)
|
|
|
|
{
|
2017-12-10 06:36:20 +00:00
|
|
|
(void) escape;
|
|
|
|
|
2017-12-08 01:13:35 +00:00
|
|
|
ops = ops != NULL ? ops : &default_render_ops;
|
2017-12-10 06:36:20 +00:00
|
|
|
return ops->render_len(list, true);
|
2017-12-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: void pkgconf_fragment_render_buf(const pkgconf_list_t *list, char *buf, size_t buflen, bool escape, const pkgconf_fragment_render_ops_t *ops)
|
|
|
|
*
|
|
|
|
* Renders a `fragment list` into a buffer.
|
|
|
|
*
|
|
|
|
* :param pkgconf_list_t* list: The `fragment list` being rendered.
|
|
|
|
* :param char* buf: The buffer to render the fragment list into.
|
|
|
|
* :param size_t buflen: The length of the buffer.
|
2017-12-10 06:36:20 +00:00
|
|
|
* :param bool escape: Whether or not to escape special shell characters (deprecated).
|
2017-12-08 01:13:35 +00:00
|
|
|
* :param pkgconf_fragment_render_ops_t* ops: An optional ops structure to use for custom renderers, else ``NULL``.
|
|
|
|
* :return: nothing
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pkgconf_fragment_render_buf(const pkgconf_list_t *list, char *buf, size_t buflen, bool escape, const pkgconf_fragment_render_ops_t *ops)
|
|
|
|
{
|
2017-12-10 06:36:20 +00:00
|
|
|
(void) escape;
|
|
|
|
|
2017-12-08 01:13:35 +00:00
|
|
|
ops = ops != NULL ? ops : &default_render_ops;
|
2017-12-13 17:18:25 +00:00
|
|
|
ops->render_buf(list, buf, buflen, true);
|
2017-12-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 00:27:57 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: char *pkgconf_fragment_render(const pkgconf_list_t *list)
|
|
|
|
*
|
|
|
|
* Allocate memory and render a `fragment list` into it.
|
|
|
|
*
|
|
|
|
* :param pkgconf_list_t* list: The `fragment list` being rendered.
|
2017-12-10 06:36:20 +00:00
|
|
|
* :param bool escape: Whether or not to escape special shell characters (deprecated).
|
2017-12-08 01:13:35 +00:00
|
|
|
* :param pkgconf_fragment_render_ops_t* ops: An optional ops structure to use for custom renderers, else ``NULL``.
|
2016-12-16 00:27:57 +00:00
|
|
|
* :return: An allocated string containing the rendered `fragment list`.
|
|
|
|
* :rtype: char *
|
|
|
|
*/
|
2016-12-10 05:07:15 +00:00
|
|
|
char *
|
2017-12-08 01:13:35 +00:00
|
|
|
pkgconf_fragment_render(const pkgconf_list_t *list, bool escape, const pkgconf_fragment_render_ops_t *ops)
|
2016-12-10 05:07:15 +00:00
|
|
|
{
|
2017-12-10 06:36:20 +00:00
|
|
|
(void) escape;
|
|
|
|
|
|
|
|
size_t buflen = pkgconf_fragment_render_len(list, true, ops);
|
2016-12-10 05:07:15 +00:00
|
|
|
char *buf = calloc(1, buflen);
|
|
|
|
|
2017-12-10 06:36:20 +00:00
|
|
|
pkgconf_fragment_render_buf(list, buf, buflen, true, ops);
|
2016-12-10 05:07:15 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:27:57 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: void pkgconf_fragment_delete(pkgconf_list_t *list, pkgconf_fragment_t *node)
|
|
|
|
*
|
|
|
|
* Delete a `fragment node` from a `fragment list`.
|
|
|
|
*
|
|
|
|
* :param pkgconf_list_t* list: The `fragment list` to delete from.
|
|
|
|
* :param pkgconf_fragment_t* node: The `fragment node` to delete.
|
|
|
|
* :return: nothing
|
|
|
|
*/
|
2012-05-03 20:27:19 +00:00
|
|
|
void
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_delete(pkgconf_list_t *list, pkgconf_fragment_t *node)
|
2012-05-03 20:27:19 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_delete(&node->iter, list);
|
2012-05-03 20:27:19 +00:00
|
|
|
|
|
|
|
free(node->data);
|
|
|
|
free(node);
|
|
|
|
}
|
2012-05-07 01:26:09 +00:00
|
|
|
|
2016-12-16 00:27:57 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: void pkgconf_fragment_free(pkgconf_list_t *list)
|
|
|
|
*
|
|
|
|
* Delete an entire `fragment list`.
|
|
|
|
*
|
|
|
|
* :param pkgconf_list_t* list: The `fragment list` to delete.
|
|
|
|
* :return: nothing
|
|
|
|
*/
|
2012-05-07 01:26:09 +00:00
|
|
|
void
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_free(pkgconf_list_t *list)
|
2012-05-07 01:26:09 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_t *node, *next;
|
2012-05-07 01:26:09 +00:00
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
PKGCONF_FOREACH_LIST_ENTRY_SAFE(list->head, next, node)
|
2013-03-01 17:10:43 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *frag = node->data;
|
2013-03-01 17:10:43 +00:00
|
|
|
|
|
|
|
free(frag->data);
|
|
|
|
free(frag);
|
|
|
|
}
|
2012-05-07 01:26:09 +00:00
|
|
|
}
|
2012-05-07 03:29:59 +00:00
|
|
|
|
2016-12-16 00:27:57 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
2017-09-20 02:48:53 +00:00
|
|
|
* .. c:function:: bool pkgconf_fragment_parse(const pkgconf_client_t *client, pkgconf_list_t *list, pkgconf_list_t *vars, const char *value)
|
2016-12-16 00:27:57 +00:00
|
|
|
*
|
|
|
|
* Parse a string into a `fragment list`.
|
|
|
|
*
|
|
|
|
* :param pkgconf_client_t* client: The pkgconf client being accessed.
|
|
|
|
* :param pkgconf_list_t* list: The `fragment list` to add the fragment entries to.
|
|
|
|
* :param pkgconf_list_t* vars: A list of variables to use for variable substitution.
|
2022-07-26 17:08:48 +00:00
|
|
|
* :param uint flags: Any parsing flags to be aware of.
|
2016-12-16 00:27:57 +00:00
|
|
|
* :param char* value: The string to parse into fragments.
|
2017-09-20 02:48:53 +00:00
|
|
|
* :return: true on success, false on parse error
|
2016-12-16 00:27:57 +00:00
|
|
|
*/
|
2017-09-20 02:48:53 +00:00
|
|
|
bool
|
2022-07-26 17:08:48 +00:00
|
|
|
pkgconf_fragment_parse(const pkgconf_client_t *client, pkgconf_list_t *list, pkgconf_list_t *vars, const char *value, unsigned int flags)
|
2012-05-07 03:29:59 +00:00
|
|
|
{
|
2017-09-20 02:34:24 +00:00
|
|
|
int i, ret, argc;
|
2012-05-07 03:29:59 +00:00
|
|
|
char **argv;
|
2022-07-26 17:08:48 +00:00
|
|
|
char *repstr = pkgconf_tuple_parse(client, vars, value, flags);
|
2012-05-07 03:29:59 +00:00
|
|
|
|
2017-09-23 05:11:37 +00:00
|
|
|
PKGCONF_TRACE(client, "post-subst: [%s] -> [%s]", value, repstr);
|
|
|
|
|
2017-09-20 02:34:24 +00:00
|
|
|
ret = pkgconf_argv_split(repstr, &argc, &argv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
PKGCONF_TRACE(client, "unable to parse fragment string [%s]", repstr);
|
|
|
|
free(repstr);
|
2017-09-20 02:48:53 +00:00
|
|
|
return false;
|
2017-09-20 02:34:24 +00:00
|
|
|
}
|
2012-05-07 03:29:59 +00:00
|
|
|
|
2017-09-20 02:48:53 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
2022-07-26 18:00:22 +00:00
|
|
|
PKGCONF_TRACE(client, "processing %s", argv[i]);
|
|
|
|
|
2017-09-20 02:48:53 +00:00
|
|
|
if (argv[i] == NULL)
|
|
|
|
{
|
|
|
|
PKGCONF_TRACE(client, "parsed fragment string is inconsistent: argc = %d while argv[%d] == NULL", argc, i);
|
|
|
|
pkgconf_argv_free(argv);
|
|
|
|
free(repstr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:00:22 +00:00
|
|
|
pkgconf_fragment_add(client, list, argv[i], flags);
|
2017-09-20 02:48:53 +00:00
|
|
|
}
|
2012-05-07 03:29:59 +00:00
|
|
|
|
2015-09-06 15:48:24 +00:00
|
|
|
pkgconf_argv_free(argv);
|
2012-05-07 03:29:59 +00:00
|
|
|
free(repstr);
|
2017-09-20 02:48:53 +00:00
|
|
|
|
|
|
|
return true;
|
2012-05-07 03:29:59 +00:00
|
|
|
}
|