2011-07-25 02:03:17 +00:00
|
|
|
/*
|
|
|
|
* parse.c
|
|
|
|
* Parser for .pc file.
|
|
|
|
*
|
2012-04-30 06:06:13 +00:00
|
|
|
* Copyright (c) 2011, 2012 William Pitcock <nenolod@dereferenced.org>.
|
2011-07-25 02:03:17 +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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pkg.h"
|
2012-04-30 06:06:13 +00:00
|
|
|
#include "bsdstubs.h"
|
2011-07-25 02:03:17 +00:00
|
|
|
|
2012-05-03 19:25:33 +00:00
|
|
|
static pkg_fragment_t *
|
2012-05-03 19:35:56 +00:00
|
|
|
parse_fragment_list(pkg_t *pkg, const char *string)
|
2012-05-03 19:25:33 +00:00
|
|
|
{
|
|
|
|
int i, argc;
|
|
|
|
char **argv;
|
2012-05-07 01:12:11 +00:00
|
|
|
char *repstr = pkg_tuple_parse(pkg->vars, string);
|
2012-05-03 19:35:56 +00:00
|
|
|
pkg_fragment_t *head = NULL;
|
2012-05-03 19:25:33 +00:00
|
|
|
|
2012-05-07 03:21:11 +00:00
|
|
|
pkg_argv_split(repstr, &argc, &argv);
|
2012-05-03 19:25:33 +00:00
|
|
|
|
2012-05-03 19:35:56 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
2012-05-03 19:25:33 +00:00
|
|
|
head = pkg_fragment_add(head, argv[i]);
|
|
|
|
|
2012-05-07 03:21:11 +00:00
|
|
|
pkg_argv_free(argv);
|
2012-05-03 19:25:33 +00:00
|
|
|
free(repstr);
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
/*
|
|
|
|
* parse_deplist(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;
|
|
|
|
|
2011-07-27 00:51:04 +00:00
|
|
|
#define DEBUG_PARSE 0
|
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
static pkg_dependency_t *
|
|
|
|
pkg_dependency_add(pkg_dependency_t *head, const char *package, const char *version, pkg_comparator_t compare)
|
|
|
|
{
|
|
|
|
pkg_dependency_t *dep;
|
|
|
|
|
|
|
|
dep = calloc(sizeof(pkg_dependency_t), 1);
|
|
|
|
dep->package = strdup(package);
|
|
|
|
|
2011-07-27 00:51:04 +00:00
|
|
|
if (version != NULL)
|
2011-07-25 04:19:53 +00:00
|
|
|
dep->version = strdup(version);
|
|
|
|
|
|
|
|
dep->compare = compare;
|
|
|
|
|
|
|
|
dep->prev = head;
|
|
|
|
if (dep->prev != NULL)
|
|
|
|
dep->prev->next = dep;
|
|
|
|
|
2011-07-27 00:51:04 +00:00
|
|
|
#if DEBUG_PARSE
|
|
|
|
fprintf(stderr, "--> %s %d %s\n", dep->package, dep->compare, dep->version);
|
|
|
|
#endif
|
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
return dep;
|
|
|
|
}
|
|
|
|
|
2011-07-25 21:53:12 +00:00
|
|
|
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 */
|
|
|
|
foreach_list_entry(head, node)
|
|
|
|
{
|
|
|
|
if (node->next == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->next = tail;
|
|
|
|
tail->prev = node;
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2012-05-07 01:26:24 +00:00
|
|
|
void
|
|
|
|
pkg_dependency_free(pkg_dependency_t *head)
|
|
|
|
{
|
|
|
|
pkg_dependency_t *node, *next;
|
|
|
|
|
|
|
|
foreach_list_entry_safe(head, next, node)
|
|
|
|
{
|
|
|
|
if (node->package != NULL)
|
|
|
|
free(node->package);
|
|
|
|
|
|
|
|
if (node->version != NULL)
|
|
|
|
free(node->version);
|
|
|
|
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-25 05:38:23 +00:00
|
|
|
pkg_dependency_t *
|
2011-07-25 04:19:53 +00:00
|
|
|
parse_deplist(pkg_t *pkg, const char *depends)
|
|
|
|
{
|
|
|
|
parse_state_t state = OUTSIDE_MODULE;
|
|
|
|
pkg_dependency_t *deplist = NULL;
|
|
|
|
pkg_dependency_t *deplist_head = NULL;
|
|
|
|
pkg_comparator_t compare = PKG_ANY;
|
2011-07-27 00:26:56 +00:00
|
|
|
char buf[BUFSIZ];
|
2012-05-07 01:12:11 +00:00
|
|
|
char *kvdepends = pkg_tuple_parse(pkg->vars, depends);
|
2011-07-27 00:26:56 +00:00
|
|
|
char *start = buf;
|
|
|
|
char *ptr = buf;
|
2011-07-25 04:19:53 +00:00
|
|
|
char *vstart = NULL;
|
2012-05-02 09:38:54 +00:00
|
|
|
char *package = NULL, *version = NULL;
|
2011-07-25 04:19:53 +00:00
|
|
|
|
2012-04-30 06:06:13 +00:00
|
|
|
strlcpy(buf, kvdepends, sizeof buf);
|
|
|
|
strlcat(buf, " ", sizeof buf);
|
2011-07-27 00:26:56 +00:00
|
|
|
free(kvdepends);
|
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case OUTSIDE_MODULE:
|
2012-05-06 02:26:15 +00:00
|
|
|
if (!PKG_MODULE_SEPARATOR(*ptr))
|
2011-07-25 04:19:53 +00:00
|
|
|
state = INSIDE_MODULE_NAME;
|
2011-07-27 00:38:26 +00:00
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case INSIDE_MODULE_NAME:
|
|
|
|
if (isspace(*ptr))
|
|
|
|
{
|
|
|
|
const char *sptr = ptr;
|
|
|
|
|
|
|
|
while (*sptr && isspace(*sptr))
|
|
|
|
sptr++;
|
|
|
|
|
|
|
|
if (*sptr == '\0')
|
|
|
|
state = OUTSIDE_MODULE;
|
2012-05-06 02:26:15 +00:00
|
|
|
else if (PKG_MODULE_SEPARATOR(*sptr))
|
2011-07-25 04:19:53 +00:00
|
|
|
state = OUTSIDE_MODULE;
|
2012-05-06 02:26:15 +00:00
|
|
|
else if (PKG_OPERATOR_CHAR(*sptr))
|
2011-07-25 04:19:53 +00:00
|
|
|
state = BEFORE_OPERATOR;
|
|
|
|
else
|
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
}
|
2012-05-06 02:26:15 +00:00
|
|
|
else if (PKG_MODULE_SEPARATOR(*ptr))
|
2011-07-25 04:19:53 +00:00
|
|
|
state = OUTSIDE_MODULE;
|
2011-07-25 06:02:53 +00:00
|
|
|
else if (*(ptr + 1) == '\0')
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
}
|
2011-07-25 04:19:53 +00:00
|
|
|
|
|
|
|
if (state != INSIDE_MODULE_NAME && start != ptr)
|
|
|
|
{
|
|
|
|
char *iter = start;
|
|
|
|
|
2012-05-06 02:26:15 +00:00
|
|
|
while (PKG_MODULE_SEPARATOR(*iter))
|
2011-07-25 04:19:53 +00:00
|
|
|
iter++;
|
|
|
|
|
|
|
|
package = strndup(iter, ptr - iter);
|
2011-07-25 06:02:53 +00:00
|
|
|
#if DEBUG_PARSE
|
|
|
|
fprintf(stderr, "Found package: %s\n", package);
|
|
|
|
#endif
|
2011-07-25 04:19:53 +00:00
|
|
|
start = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == OUTSIDE_MODULE)
|
|
|
|
{
|
2011-07-27 00:51:04 +00:00
|
|
|
deplist = pkg_dependency_add(deplist, package, NULL, compare);
|
2011-07-25 04:19:53 +00:00
|
|
|
|
|
|
|
if (deplist_head == NULL)
|
|
|
|
deplist_head = deplist;
|
|
|
|
|
2011-07-27 00:26:56 +00:00
|
|
|
if (package != NULL)
|
|
|
|
{
|
|
|
|
free(package);
|
|
|
|
package = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version != NULL)
|
|
|
|
{
|
|
|
|
free(version);
|
|
|
|
version = NULL;
|
|
|
|
}
|
2011-07-27 00:51:04 +00:00
|
|
|
|
|
|
|
compare = PKG_ANY;
|
2011-07-25 04:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BEFORE_OPERATOR:
|
2012-05-06 02:26:15 +00:00
|
|
|
if (PKG_OPERATOR_CHAR(*ptr))
|
2011-07-27 00:38:26 +00:00
|
|
|
{
|
|
|
|
switch(*ptr)
|
|
|
|
{
|
|
|
|
case '=':
|
|
|
|
compare = PKG_EQUAL;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
compare = PKG_GREATER_THAN;
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
compare = PKG_LESS_THAN;
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
compare = PKG_NOT_EQUAL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
state = INSIDE_OPERATOR;
|
2011-07-27 00:38:26 +00:00
|
|
|
}
|
2011-07-25 06:02:53 +00:00
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case INSIDE_OPERATOR:
|
2012-05-06 02:26:15 +00:00
|
|
|
if (!PKG_OPERATOR_CHAR(*ptr))
|
2011-07-25 04:19:53 +00:00
|
|
|
state = AFTER_OPERATOR;
|
2011-07-27 00:38:26 +00:00
|
|
|
else if (*ptr == '=')
|
|
|
|
{
|
|
|
|
switch(compare)
|
|
|
|
{
|
|
|
|
case PKG_LESS_THAN:
|
|
|
|
compare = PKG_LESS_THAN_EQUAL;
|
|
|
|
break;
|
|
|
|
case PKG_GREATER_THAN:
|
|
|
|
compare = PKG_GREATER_THAN_EQUAL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-07-25 06:02:53 +00:00
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AFTER_OPERATOR:
|
2011-07-27 00:51:04 +00:00
|
|
|
#if DEBUG_PARSE
|
|
|
|
fprintf(stderr, "Found op: %d\n", compare);
|
|
|
|
#endif
|
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
if (!isspace(*ptr))
|
|
|
|
{
|
|
|
|
vstart = ptr;
|
|
|
|
state = INSIDE_VERSION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSIDE_VERSION:
|
2012-05-06 02:26:15 +00:00
|
|
|
if (PKG_MODULE_SEPARATOR(*ptr) || *(ptr + 1) == '\0')
|
2011-07-25 04:19:53 +00:00
|
|
|
{
|
2011-07-27 00:51:04 +00:00
|
|
|
version = strndup(vstart, (ptr - vstart));
|
2011-07-25 04:19:53 +00:00
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
|
2011-07-25 06:02:53 +00:00
|
|
|
#if DEBUG_PARSE
|
|
|
|
fprintf(stderr, "Found version: %s\n", version);
|
|
|
|
#endif
|
2011-07-27 00:51:04 +00:00
|
|
|
deplist = pkg_dependency_add(deplist, package, version, compare);
|
2011-07-25 04:19:53 +00:00
|
|
|
|
|
|
|
if (deplist_head == NULL)
|
|
|
|
deplist_head = deplist;
|
|
|
|
|
2011-07-27 00:26:56 +00:00
|
|
|
if (package != NULL)
|
|
|
|
{
|
|
|
|
free(package);
|
|
|
|
package = NULL;
|
|
|
|
}
|
2011-07-25 04:19:53 +00:00
|
|
|
|
2011-07-27 00:26:56 +00:00
|
|
|
if (version != NULL)
|
|
|
|
{
|
|
|
|
free(version);
|
|
|
|
version = NULL;
|
|
|
|
}
|
2011-07-27 00:51:04 +00:00
|
|
|
|
|
|
|
compare = PKG_ANY;
|
2011-07-25 04:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == OUTSIDE_MODULE)
|
|
|
|
start = ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return deplist_head;
|
|
|
|
}
|
|
|
|
|
2011-07-25 02:03:17 +00:00
|
|
|
/*
|
|
|
|
* parse_file(filename)
|
|
|
|
*
|
|
|
|
* Parse a .pc file into a pkg_t object structure.
|
|
|
|
*/
|
|
|
|
pkg_t *
|
2012-01-16 01:03:54 +00:00
|
|
|
parse_file(const char *filename, FILE *f)
|
2011-07-25 02:03:17 +00:00
|
|
|
{
|
|
|
|
pkg_t *pkg;
|
|
|
|
char readbuf[BUFSIZ];
|
|
|
|
|
|
|
|
pkg = calloc(sizeof(pkg_t), 1);
|
|
|
|
pkg->filename = strdup(filename);
|
|
|
|
|
2012-05-04 03:23:03 +00:00
|
|
|
while (pkg_fgetline(readbuf, BUFSIZ, f) != NULL)
|
2011-07-25 02:03:17 +00:00
|
|
|
{
|
2011-07-25 04:19:53 +00:00
|
|
|
char op, *p, *key = NULL, *value = NULL;
|
2011-07-25 02:03:17 +00:00
|
|
|
|
|
|
|
readbuf[strlen(readbuf) - 1] = '\0';
|
|
|
|
|
|
|
|
p = readbuf;
|
|
|
|
while (*p && (isalpha(*p) || isdigit(*p) || *p == '_' || *p == '.'))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
key = strndup(readbuf, p - readbuf);
|
|
|
|
if (!isalpha(*key) && !isdigit(*p))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
while (*p && isspace(*p))
|
|
|
|
p++;
|
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
op = *p++;
|
|
|
|
|
|
|
|
while (*p && isspace(*p))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
value = strdup(p);
|
2011-07-25 02:03:17 +00:00
|
|
|
|
2011-07-25 04:19:53 +00:00
|
|
|
switch (op)
|
2011-07-25 02:03:17 +00:00
|
|
|
{
|
|
|
|
case ':':
|
|
|
|
if (!strcasecmp(key, "Name"))
|
2012-05-07 01:12:11 +00:00
|
|
|
pkg->realname = pkg_tuple_parse(pkg->vars, value);
|
2011-07-25 04:19:53 +00:00
|
|
|
else if (!strcasecmp(key, "Description"))
|
2012-05-07 01:12:11 +00:00
|
|
|
pkg->description = pkg_tuple_parse(pkg->vars, value);
|
2011-07-25 04:19:53 +00:00
|
|
|
else if (!strcasecmp(key, "Version"))
|
2012-05-07 01:12:11 +00:00
|
|
|
pkg->version = pkg_tuple_parse(pkg->vars, value);
|
2011-07-25 04:19:53 +00:00
|
|
|
else if (!strcasecmp(key, "CFLAGS"))
|
2012-05-03 19:35:56 +00:00
|
|
|
pkg->cflags = parse_fragment_list(pkg, value);
|
2011-07-25 04:19:53 +00:00
|
|
|
else if (!strcasecmp(key, "LIBS"))
|
2012-05-03 19:35:56 +00:00
|
|
|
pkg->libs = parse_fragment_list(pkg, value);
|
2012-05-02 21:53:02 +00:00
|
|
|
else if (!strcasecmp(key, "LIBS.private"))
|
2012-05-03 19:35:56 +00:00
|
|
|
pkg->libs_private = parse_fragment_list(pkg, value);
|
2011-07-25 04:19:53 +00:00
|
|
|
else if (!strcasecmp(key, "Requires"))
|
|
|
|
pkg->requires = parse_deplist(pkg, value);
|
2012-05-02 21:37:32 +00:00
|
|
|
else if (!strcasecmp(key, "Requires.private"))
|
|
|
|
pkg->requires_private = parse_deplist(pkg, value);
|
2011-07-25 04:19:53 +00:00
|
|
|
else if (!strcasecmp(key, "Conflicts"))
|
|
|
|
pkg->conflicts = parse_deplist(pkg, value);
|
2011-07-25 02:03:17 +00:00
|
|
|
break;
|
|
|
|
case '=':
|
2012-05-07 01:06:48 +00:00
|
|
|
pkg->vars = pkg_tuple_add(pkg->vars, key, value);
|
2011-07-25 02:03:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (key)
|
|
|
|
free(key);
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
return pkg;
|
|
|
|
}
|