37#include "xbps_api_impl.h"
39#define PKG_PATTERN_MAX 1024
42#define MIN(a,b) (((a) < (b)) ? (a) : (b))
45#define MAX(a,b) (((a) > (b)) ? (a) : (b))
75typedef struct test_t {
83const test_t tests[] = {
84 {
"<=", 2, DEWEY_LE },
86 {
">=", 2, DEWEY_GE },
88 {
"==", 2, DEWEY_EQ },
89 {
"!=", 2, DEWEY_NE },
93const test_t modifiers[] = {
94 {
"alpha", 5, Alpha },
107dewey_mktest(
int *op,
const char *test)
111 for (tp = tests ; tp->s ; tp++) {
112 if (strncasecmp(test, tp->s, tp->len) == 0) {
130mkcomponent(arr_t *ap,
const char *num)
132 static const char alphas[] =
"abcdefghijklmnopqrstuvwxyz";
137 if (ap->c == ap->size) {
140 ap->v = malloc(ap->size *
sizeof(
int));
141 assert(ap->v != NULL);
144 ap->v = realloc(ap->v, ap->size *
sizeof(
int));
145 assert(ap->v != NULL);
148 if (isdigit((
unsigned char)*num)) {
149 for (cp = num, n = 0 ; isdigit((
unsigned char)*num) ; num++) {
150 n = (n * 10) + (*num -
'0');
153 return (
int)(num - cp);
155 for (modp = modifiers ; modp->s ; modp++) {
156 if (strncasecmp(num, modp->s, modp->len) == 0) {
157 ap->v[ap->c++] = modp->t;
161 if (strncasecmp(num,
"_", 1) == 0) {
162 for (cp = num, num += 1, n = 0 ; isdigit((
unsigned char)*num) ; num++) {
163 n = (n * 10) + (*num -
'0');
166 return (
int)(num - cp);
168 if (isalpha((
unsigned char)*num)) {
169 ap->v[ap->c++] = Dot;
170 cp = strchr(alphas, tolower((
unsigned char)*num));
171 if (ap->c == ap->size) {
173 ap->v = realloc(ap->v, ap->size *
sizeof(
int));
174 assert(ap->v != NULL);
176 ap->v[ap->c++] = (int)(cp - alphas) + 1;
184mkversion(arr_t *ap,
const char *num)
192 num += mkcomponent(ap, num);
198freeversion(arr_t *ap)
206#define DIGIT(v, c, n) (((n) < (c)) ? v[n] : 0)
210result(
int cmp,
int tst)
232vtest(arr_t *lhs,
int tst, arr_t *rhs)
237 for (i = 0, c = MAX(lhs->c, rhs->c) ; i < c ; i++) {
238 if ((cmp = DIGIT(lhs->v, lhs->c, i) - DIGIT(rhs->v, rhs->c, i)) != 0) {
239 return result(cmp, tst);
242 return result(lhs->revision - rhs->revision, tst);
249dewey_cmp(
const char *lhs,
int op,
const char *rhs)
255 if (!mkversion(&left, lhs))
257 if (!mkversion(&right, rhs)) {
261 retval = vtest(&left, op, &right);
275 if (dewey_cmp(pkg1, DEWEY_LT, pkg2))
277 else if (dewey_cmp(pkg1, DEWEY_GT, pkg2))
288dewey_match(
const char *pattern,
const char *pkg)
291 const char *sep, *sep2;
296 if ((version=strrchr(pkg,
'-')) == NULL) {
299 if ((sep = strpbrk(pattern,
"<>")) == NULL)
302 if ((sep-pattern != version-pkg) ||
303 strncmp(pkg, pattern, (
size_t)(version-pkg)) != 0)
308 if ((n = dewey_mktest(&op, sep)) < 0) {
316 if (op == DEWEY_GT || op == DEWEY_GE) {
317 if ((sep2 = strchr(sep,
'<')) != NULL) {
318 if ((n = dewey_mktest(&op2, sep2)) < 0) {
322 if (!dewey_cmp(version, op2, sep2+n))
329 char ver[PKG_PATTERN_MAX];
331 xbps_strlcpy(ver, sep, MIN((ssize_t)
sizeof(ver), sep2-sep+1));
332 if (dewey_cmp(version, op, ver))
335 if (dewey_cmp(version, op, sep))
size_t xbps_strlcpy(char *dst, const char *src, size_t dstsize)
int xbps_cmpver(const char *pkg1, const char *pkg2)