2012-05-03 20:22:19 +00:00
|
|
|
/*
|
|
|
|
* fragment.c
|
|
|
|
* Management of fragment lists.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pkg.h"
|
|
|
|
#include "bsdstubs.h"
|
|
|
|
|
|
|
|
pkg_fragment_t *
|
|
|
|
pkg_fragment_append(pkg_fragment_t *head, pkg_fragment_t *tail)
|
|
|
|
{
|
|
|
|
pkg_fragment_t *node;
|
|
|
|
|
|
|
|
if (head == NULL)
|
|
|
|
return tail;
|
|
|
|
|
|
|
|
/* skip to end of list */
|
2012-05-07 03:55:00 +00:00
|
|
|
PKG_FOREACH_LIST_ENTRY(head, node)
|
2012-05-03 20:22:19 +00:00
|
|
|
{
|
|
|
|
if (node->next == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->next = tail;
|
|
|
|
tail->prev = node;
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg_fragment_t *
|
|
|
|
pkg_fragment_add(pkg_fragment_t *head, const char *string)
|
|
|
|
{
|
|
|
|
pkg_fragment_t *frag;
|
|
|
|
|
|
|
|
frag = calloc(sizeof(pkg_fragment_t), 1);
|
|
|
|
|
|
|
|
if (*string == '-' && strncmp(string, "-lib:", 5))
|
|
|
|
{
|
|
|
|
frag->type = *(string + 1);
|
|
|
|
frag->data = strdup(string + 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
frag->type = 0;
|
|
|
|
frag->data = strdup(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkg_fragment_append(head, frag);
|
|
|
|
}
|
2012-05-03 20:27:19 +00:00
|
|
|
|
2012-05-03 20:43:09 +00:00
|
|
|
bool
|
|
|
|
pkg_fragment_exists(pkg_fragment_t *head, pkg_fragment_t *base)
|
|
|
|
{
|
|
|
|
pkg_fragment_t *node;
|
|
|
|
|
2012-05-07 03:55:00 +00:00
|
|
|
PKG_FOREACH_LIST_ENTRY(head, node)
|
2012-05-03 20:43:09 +00:00
|
|
|
{
|
|
|
|
if (base->type != node->type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(base->data, node->data))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-03 20:31:33 +00:00
|
|
|
pkg_fragment_t *
|
|
|
|
pkg_fragment_copy(pkg_fragment_t *head, pkg_fragment_t *base)
|
|
|
|
{
|
|
|
|
pkg_fragment_t *frag;
|
|
|
|
|
2012-05-03 20:43:09 +00:00
|
|
|
if (pkg_fragment_exists(head, base))
|
|
|
|
return head;
|
|
|
|
|
2012-05-03 20:31:33 +00:00
|
|
|
frag = calloc(sizeof(pkg_fragment_t), 1);
|
|
|
|
|
|
|
|
frag->type = base->type;
|
|
|
|
frag->data = strdup(base->data);
|
|
|
|
|
|
|
|
return pkg_fragment_append(head, frag);
|
|
|
|
}
|
|
|
|
|
2012-05-03 20:27:19 +00:00
|
|
|
void
|
|
|
|
pkg_fragment_delete(pkg_fragment_t *node)
|
|
|
|
{
|
|
|
|
if (node->prev != NULL)
|
|
|
|
node->prev->next = node->next;
|
|
|
|
|
|
|
|
if (node->next != NULL)
|
|
|
|
node->next->prev = node->prev;
|
|
|
|
|
|
|
|
free(node->data);
|
|
|
|
free(node);
|
|
|
|
}
|
2012-05-07 01:26:09 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
pkg_fragment_free(pkg_fragment_t *head)
|
|
|
|
{
|
|
|
|
pkg_fragment_t *node, *next;
|
|
|
|
|
2012-05-07 03:55:00 +00:00
|
|
|
PKG_FOREACH_LIST_ENTRY_SAFE(head, next, node)
|
2012-05-07 01:26:09 +00:00
|
|
|
pkg_fragment_delete(node);
|
|
|
|
}
|
2012-05-07 03:29:59 +00:00
|
|
|
|
|
|
|
pkg_fragment_t *
|
|
|
|
pkg_fragment_parse(pkg_fragment_t *head, pkg_tuple_t *vars, const char *value)
|
|
|
|
{
|
|
|
|
int i, argc;
|
|
|
|
char **argv;
|
|
|
|
char *repstr = pkg_tuple_parse(vars, value);
|
|
|
|
|
|
|
|
pkg_argv_split(repstr, &argc, &argv);
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
head = pkg_fragment_add(head, argv[i]);
|
|
|
|
|
|
|
|
pkg_argv_free(argv);
|
|
|
|
free(repstr);
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|