37 #include "xbps_api_impl.h"
42 #define PKG_PATTERN_MAX 1024
45 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
48 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
70 typedef struct arr_t {
78 typedef struct test_t {
86 const test_t tests[] = {
87 {
"<=", 2, DEWEY_LE },
89 {
">=", 2, DEWEY_GE },
91 {
"==", 2, DEWEY_EQ },
92 {
"!=", 2, DEWEY_NE },
96 const test_t modifiers[] = {
97 {
"alpha", 5, Alpha },
110 dewey_mktest(
int *op,
const char *test)
114 for (tp = tests ; tp->s ; tp++) {
115 if (strncasecmp(test, tp->s, tp->len) == 0) {
133 mkcomponent(arr_t *ap,
const char *num)
135 static const char alphas[] =
"abcdefghijklmnopqrstuvwxyz";
140 if (ap->c == ap->size) {
143 ap->v = malloc(ap->size *
sizeof(
int));
144 assert(ap->v != NULL);
147 ap->v = realloc(ap->v, ap->size *
sizeof(
int));
148 assert(ap->v != NULL);
151 if (isdigit((
unsigned char)*num)) {
152 for (cp = num, n = 0 ; isdigit((
unsigned char)*num) ; num++) {
153 n = (n * 10) + (*num -
'0');
156 return (
int)(num - cp);
158 for (modp = modifiers ; modp->s ; modp++) {
159 if (strncasecmp(num, modp->s, modp->len) == 0) {
160 ap->v[ap->c++] = modp->t;
164 if (strncasecmp(num,
"_", 1) == 0) {
165 for (cp = num, num += 1, n = 0 ; isdigit((
unsigned char)*num) ; num++) {
166 n = (n * 10) + (*num -
'0');
169 return (
int)(num - cp);
171 if (isalpha((
unsigned char)*num)) {
172 ap->v[ap->c++] = Dot;
173 cp = strchr(alphas, tolower((
unsigned char)*num));
174 if (ap->c == ap->size) {
176 ap->v = realloc(ap->v, ap->size *
sizeof(
int));
177 assert(ap->v != NULL);
179 ap->v[ap->c++] = (int)(cp - alphas) + 1;
187 mkversion(arr_t *ap,
const char *num)
195 num += mkcomponent(ap, num);
201 freeversion(arr_t *ap)
209 #define DIGIT(v, c, n) (((n) < (c)) ? v[n] : 0)
213 result(
int cmp,
int tst)
235 vtest(arr_t *lhs,
int tst, arr_t *rhs)
240 for (i = 0, c = MAX(lhs->c, rhs->c) ; i < c ; i++) {
241 if ((cmp = DIGIT(lhs->v, lhs->c, i) - DIGIT(rhs->v, rhs->c, i)) != 0) {
242 return result(cmp, tst);
245 return result(lhs->revision - rhs->revision, tst);
252 dewey_cmp(
const char *lhs,
int op,
const char *rhs)
258 if (!mkversion(&left, lhs))
260 if (!mkversion(&right, rhs)) {
264 retval = vtest(&left, op, &right);
278 if (dewey_cmp(pkg1, DEWEY_LT, pkg2))
280 else if (dewey_cmp(pkg1, DEWEY_GT, pkg2))
291 dewey_match(
const char *pattern,
const char *pkg)
294 const char *sep, *sep2;
299 if ((version=strrchr(pkg,
'-')) == NULL) {
302 if ((sep = strpbrk(pattern,
"<>")) == NULL)
305 if ((sep-pattern != version-pkg) ||
306 strncmp(pkg, pattern, (
size_t)(version-pkg)) != 0)
311 if ((n = dewey_mktest(&op, sep)) < 0) {
319 if (op == DEWEY_GT || op == DEWEY_GE) {
320 if ((sep2 = strchr(sep,
'<')) != NULL) {
321 if ((n = dewey_mktest(&op2, sep2)) < 0) {
325 if (!dewey_cmp(version, op2, sep2+n))
332 char ver[PKG_PATTERN_MAX];
334 strlcpy(ver, sep, MIN((ssize_t)
sizeof(ver), sep2-sep+1));
335 if (dewey_cmp(version, op, ver))
338 if (dewey_cmp(version, op, sep))