2012-05-07 03:32:53 +00:00
|
|
|
/*
|
|
|
|
* dependency.c
|
|
|
|
* dependency parsing and management
|
|
|
|
*
|
2013-02-24 14:21:36 +00:00
|
|
|
* Copyright (c) 2011, 2012, 2013 pkgconf authors (see AUTHORS).
|
2012-05-07 03:32:53 +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 03:32:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pkg.h"
|
|
|
|
#include "bsdstubs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pkg_dependency_parse(pkg, depends)
|
|
|
|
*
|
|
|
|
* Add requirements to a .pc file.
|
|
|
|
* Commas are counted as whitespace, as to allow:
|
|
|
|
* @SUBSTVAR@, zlib
|
|
|
|
* getting substituted to:
|
|
|
|
* , zlib.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
OUTSIDE_MODULE = 0,
|
|
|
|
INSIDE_MODULE_NAME = 1,
|
|
|
|
BEFORE_OPERATOR = 2,
|
|
|
|
INSIDE_OPERATOR = 3,
|
|
|
|
AFTER_OPERATOR = 4,
|
|
|
|
INSIDE_VERSION = 5
|
|
|
|
} parse_state_t;
|
|
|
|
|
|
|
|
#define DEBUG_PARSE 0
|
|
|
|
|
|
|
|
static inline pkg_dependency_t *
|
2012-08-14 15:13:28 +00:00
|
|
|
pkg_dependency_add(pkg_dependency_t *head, const char *package, size_t package_sz, const char *version, size_t version_sz, pkg_comparator_t compare)
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
|
|
|
pkg_dependency_t *dep;
|
|
|
|
|
|
|
|
dep = calloc(sizeof(pkg_dependency_t), 1);
|
2012-08-14 15:13:28 +00:00
|
|
|
dep->package = strndup(package, package_sz);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2012-08-14 15:13:28 +00:00
|
|
|
if (version_sz != 0)
|
|
|
|
dep->version = strndup(version, version_sz);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
|
|
|
dep->compare = compare;
|
|
|
|
|
|
|
|
dep->prev = head;
|
|
|
|
if (dep->prev != NULL)
|
|
|
|
dep->prev->next = dep;
|
|
|
|
|
|
|
|
#if DEBUG_PARSE
|
2012-07-02 02:21:31 +00:00
|
|
|
fprintf(error_msgout, "--> %s %d %s\n", dep->package, dep->compare, dep->version);
|
2012-05-07 03:32:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return dep;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg_dependency_t *
|
|
|
|
pkg_dependency_append(pkg_dependency_t *head, pkg_dependency_t *tail)
|
|
|
|
{
|
|
|
|
pkg_dependency_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-07 03:32:53 +00:00
|
|
|
{
|
|
|
|
if (node->next == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->next = tail;
|
|
|
|
tail->prev = node;
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pkg_dependency_free(pkg_dependency_t *head)
|
|
|
|
{
|
|
|
|
pkg_dependency_t *node, *next;
|
|
|
|
|
2012-05-07 03:55:00 +00:00
|
|
|
PKG_FOREACH_LIST_ENTRY_SAFE(head, next, node)
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
|
|
|
if (node->package != NULL)
|
|
|
|
free(node->package);
|
|
|
|
|
|
|
|
if (node->version != NULL)
|
|
|
|
free(node->version);
|
|
|
|
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg_dependency_t *
|
2012-07-02 02:44:30 +00:00
|
|
|
pkg_dependency_parse_str(pkg_dependency_t *deplist_head, const char *depends)
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
|
|
|
parse_state_t state = OUTSIDE_MODULE;
|
|
|
|
pkg_dependency_t *deplist = NULL;
|
|
|
|
pkg_comparator_t compare = PKG_ANY;
|
2013-02-24 13:41:18 +00:00
|
|
|
char cmpname[PKG_BUFSIZE];
|
2012-07-21 19:38:13 +00:00
|
|
|
char buf[PKG_BUFSIZE];
|
2012-08-19 19:43:15 +00:00
|
|
|
size_t package_sz = 0, version_sz = 0;
|
2012-05-07 03:32:53 +00:00
|
|
|
char *start = buf;
|
|
|
|
char *ptr = buf;
|
|
|
|
char *vstart = NULL;
|
2012-08-14 15:13:28 +00:00
|
|
|
char *package = NULL, *version = NULL;
|
2013-02-24 13:41:18 +00:00
|
|
|
char *cnameptr = cmpname;
|
|
|
|
|
|
|
|
memset(cmpname, '\0', sizeof cmpname);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2012-07-02 02:44:30 +00:00
|
|
|
strlcpy(buf, depends, sizeof buf);
|
2012-05-07 03:32:53 +00:00
|
|
|
strlcat(buf, " ", sizeof buf);
|
|
|
|
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case OUTSIDE_MODULE:
|
|
|
|
if (!PKG_MODULE_SEPARATOR(*ptr))
|
|
|
|
state = INSIDE_MODULE_NAME;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSIDE_MODULE_NAME:
|
|
|
|
if (isspace(*ptr))
|
|
|
|
{
|
|
|
|
const char *sptr = ptr;
|
|
|
|
|
|
|
|
while (*sptr && isspace(*sptr))
|
|
|
|
sptr++;
|
|
|
|
|
|
|
|
if (*sptr == '\0')
|
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
else if (PKG_MODULE_SEPARATOR(*sptr))
|
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
else if (PKG_OPERATOR_CHAR(*sptr))
|
|
|
|
state = BEFORE_OPERATOR;
|
|
|
|
else
|
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
}
|
|
|
|
else if (PKG_MODULE_SEPARATOR(*ptr))
|
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
else if (*(ptr + 1) == '\0')
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state != INSIDE_MODULE_NAME && start != ptr)
|
|
|
|
{
|
|
|
|
char *iter = start;
|
|
|
|
|
|
|
|
while (PKG_MODULE_SEPARATOR(*iter))
|
|
|
|
iter++;
|
|
|
|
|
2012-08-14 15:13:28 +00:00
|
|
|
package = iter;
|
|
|
|
package_sz = ptr - iter;
|
2012-05-07 03:32:53 +00:00
|
|
|
#if DEBUG_PARSE
|
2012-07-02 02:21:31 +00:00
|
|
|
fprintf(error_msgout, "Found package: %s\n", package);
|
2012-05-07 03:32:53 +00:00
|
|
|
#endif
|
|
|
|
start = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == OUTSIDE_MODULE)
|
|
|
|
{
|
2012-08-14 15:13:28 +00:00
|
|
|
deplist = pkg_dependency_add(deplist, package, package_sz, NULL, 0, compare);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
|
|
|
if (deplist_head == NULL)
|
|
|
|
deplist_head = deplist;
|
|
|
|
|
|
|
|
compare = PKG_ANY;
|
2012-08-19 19:43:15 +00:00
|
|
|
package_sz = 0;
|
|
|
|
version_sz = 0;
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BEFORE_OPERATOR:
|
|
|
|
if (PKG_OPERATOR_CHAR(*ptr))
|
|
|
|
{
|
|
|
|
state = INSIDE_OPERATOR;
|
2013-02-24 13:41:18 +00:00
|
|
|
*cnameptr++ = *ptr;
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSIDE_OPERATOR:
|
|
|
|
if (!PKG_OPERATOR_CHAR(*ptr))
|
|
|
|
{
|
2013-02-24 13:41:18 +00:00
|
|
|
state = AFTER_OPERATOR;
|
|
|
|
compare = pkg_comparator_lookup_by_name(cmpname);
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
2013-02-24 13:41:18 +00:00
|
|
|
else
|
|
|
|
*cnameptr++ = *ptr;
|
2012-05-07 03:32:53 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AFTER_OPERATOR:
|
|
|
|
#if DEBUG_PARSE
|
2012-07-02 02:21:31 +00:00
|
|
|
fprintf(error_msgout, "Found op: %d\n", compare);
|
2012-05-07 03:32:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!isspace(*ptr))
|
|
|
|
{
|
|
|
|
vstart = ptr;
|
|
|
|
state = INSIDE_VERSION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSIDE_VERSION:
|
|
|
|
if (PKG_MODULE_SEPARATOR(*ptr) || *(ptr + 1) == '\0')
|
|
|
|
{
|
2012-08-14 15:13:28 +00:00
|
|
|
version = vstart;
|
|
|
|
version_sz = ptr - vstart;
|
2012-05-07 03:32:53 +00:00
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
|
|
|
|
#if DEBUG_PARSE
|
2012-07-02 02:21:31 +00:00
|
|
|
fprintf(error_msgout, "Found version: %s\n", version);
|
2012-05-07 03:32:53 +00:00
|
|
|
#endif
|
2012-08-14 15:13:28 +00:00
|
|
|
deplist = pkg_dependency_add(deplist, package, package_sz, version, version_sz, compare);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
|
|
|
if (deplist_head == NULL)
|
|
|
|
deplist_head = deplist;
|
|
|
|
|
|
|
|
compare = PKG_ANY;
|
2012-08-19 19:43:15 +00:00
|
|
|
package_sz = 0;
|
|
|
|
version_sz = 0;
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == OUTSIDE_MODULE)
|
|
|
|
start = ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return deplist_head;
|
|
|
|
}
|
2012-07-02 02:44:30 +00:00
|
|
|
|
|
|
|
pkg_dependency_t *
|
|
|
|
pkg_dependency_parse(pkg_t *pkg, const char *depends)
|
|
|
|
{
|
|
|
|
pkg_dependency_t *list = NULL;
|
2013-03-01 16:45:55 +00:00
|
|
|
char *kvdepends = pkg_tuple_parse(&pkg->vars, depends);
|
2012-07-02 02:44:30 +00:00
|
|
|
|
|
|
|
list = pkg_dependency_parse_str(list, kvdepends);
|
|
|
|
free(kvdepends);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|