XBPS Library API 20240111
The X Binary Package System
dewey.c
1/* $NetBSD: dewey.c,v 1.1.1.3 2009/03/08 14:51:37 joerg Exp $ */
2
3/*
4 * Copyright © 2002 Alistair G. Crooks. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <strings.h>
35#include <ctype.h>
36
37#include "xbps_api_impl.h"
38
39#define PKG_PATTERN_MAX 1024
40
41#ifndef MIN
42#define MIN(a,b) (((a) < (b)) ? (a) : (b))
43#endif
44#ifndef MAX
45#define MAX(a,b) (((a) > (b)) ? (a) : (b))
46#endif
47
48enum {
49 DEWEY_LT,
50 DEWEY_LE,
51 DEWEY_EQ,
52 DEWEY_GE,
53 DEWEY_GT,
54 DEWEY_NE
55};
56
57/* do not modify these values, or things will NOT work */
58enum {
59 Alpha = -3,
60 Beta = -2,
61 RC = -1,
62 Dot = 0,
63 Patch = 1
64};
65
66/* this struct defines a version number */
67typedef struct arr_t {
68 unsigned c; /* # of version numbers */
69 unsigned size; /* size of array */
70 int *v; /* array of decimal numbers */
71 int revision; /* any "_" suffix */
72} arr_t;
73
74/* this struct describes a test */
75typedef struct test_t {
76 const char *s; /* string representation */
77 unsigned len; /* length of string */
78 int t; /* enumerated type of test */
79} test_t;
80
81
82/* the tests that are recognised. */
83const test_t tests[] = {
84 { "<=", 2, DEWEY_LE },
85 { "<", 1, DEWEY_LT },
86 { ">=", 2, DEWEY_GE },
87 { ">", 1, DEWEY_GT },
88 { "==", 2, DEWEY_EQ },
89 { "!=", 2, DEWEY_NE },
90 { NULL, 0, 0 }
91};
92
93const test_t modifiers[] = {
94 { "alpha", 5, Alpha },
95 { "beta", 4, Beta },
96 { "pre", 3, RC },
97 { "rc", 2, RC },
98 { "pl", 2, Dot },
99 { ".", 1, Dot },
100 { NULL, 0, 0 }
101};
102
103
104
105/* locate the test in the tests array */
106static int
107dewey_mktest(int *op, const char *test)
108{
109 const test_t *tp;
110
111 for (tp = tests ; tp->s ; tp++) {
112 if (strncasecmp(test, tp->s, tp->len) == 0) {
113 *op = tp->t;
114 return tp->len;
115 }
116 }
117 return -1;
118}
119
120/*
121 * make a component of a version number.
122 * '.' encodes as Dot which is '0'
123 * 'pl' encodes as 'patch level', or 'Dot', which is 0.
124 * 'alpha' encodes as 'alpha version', or Alpha, which is -3.
125 * 'beta' encodes as 'beta version', or Beta, which is -2.
126 * 'rc' encodes as 'release candidate', or RC, which is -1.
127 * '_' encodes as 'xbps revision', which is used after all other tests
128 */
129static int
130mkcomponent(arr_t *ap, const char *num)
131{
132 static const char alphas[] = "abcdefghijklmnopqrstuvwxyz";
133 const test_t *modp;
134 int n;
135 const char *cp;
136
137 if (ap->c == ap->size) {
138 if (ap->size == 0) {
139 ap->size = 62;
140 ap->v = malloc(ap->size * sizeof(int));
141 assert(ap->v != NULL);
142 } else {
143 ap->size *= 2;
144 ap->v = realloc(ap->v, ap->size * sizeof(int));
145 assert(ap->v != NULL);
146 }
147 }
148 if (isdigit((unsigned char)*num)) {
149 for (cp = num, n = 0 ; isdigit((unsigned char)*num) ; num++) {
150 n = (n * 10) + (*num - '0');
151 }
152 ap->v[ap->c++] = n;
153 return (int)(num - cp);
154 }
155 for (modp = modifiers ; modp->s ; modp++) {
156 if (strncasecmp(num, modp->s, modp->len) == 0) {
157 ap->v[ap->c++] = modp->t;
158 return modp->len;
159 }
160 }
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');
164 }
165 ap->revision = n;
166 return (int)(num - cp);
167 }
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) {
172 ap->size *= 2;
173 ap->v = realloc(ap->v, ap->size * sizeof(int));
174 assert(ap->v != NULL);
175 }
176 ap->v[ap->c++] = (int)(cp - alphas) + 1;
177 return 1;
178 }
179 return 1;
180}
181
182/* make a version number string into an array of comparable ints */
183static int
184mkversion(arr_t *ap, const char *num)
185{
186 ap->c = 0;
187 ap->size = 0;
188 ap->v = NULL;
189 ap->revision = 0;
190
191 while (*num) {
192 num += mkcomponent(ap, num);
193 }
194 return 1;
195}
196
197static void
198freeversion(arr_t *ap)
199{
200 free(ap->v);
201 ap->v = NULL;
202 ap->c = 0;
203 ap->size = 0;
204}
205
206#define DIGIT(v, c, n) (((n) < (c)) ? v[n] : 0)
207
208/* compare the result against the test we were expecting */
209static int
210result(int cmp, int tst)
211{
212 switch(tst) {
213 case DEWEY_LT:
214 return cmp < 0;
215 case DEWEY_LE:
216 return cmp <= 0;
217 case DEWEY_GT:
218 return cmp > 0;
219 case DEWEY_GE:
220 return cmp >= 0;
221 case DEWEY_EQ:
222 return cmp == 0;
223 case DEWEY_NE:
224 return cmp != 0;
225 default:
226 return 0;
227 }
228}
229
230/* do the test on the 2 vectors */
231static int
232vtest(arr_t *lhs, int tst, arr_t *rhs)
233{
234 int cmp;
235 unsigned int c, i;
236
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);
240 }
241 }
242 return result(lhs->revision - rhs->revision, tst);
243}
244
245/*
246 * Compare two dewey decimal numbers.
247 */
248static int
249dewey_cmp(const char *lhs, int op, const char *rhs)
250{
251 arr_t right;
252 arr_t left;
253 int retval;
254
255 if (!mkversion(&left, lhs))
256 return 0;
257 if (!mkversion(&right, rhs)) {
258 freeversion(&left);
259 return 0;
260 }
261 retval = vtest(&left, op, &right);
262 freeversion(&left);
263 freeversion(&right);
264 return retval;
265}
266
267/*
268 * Returns -1, 0 or 1 depending on if the version components of
269 * pkg1 is less than, equal to or greater than pkg2. No comparison
270 * comparison of the basenames is done.
271 */
272int
273xbps_cmpver(const char *pkg1, const char *pkg2)
274{
275 if (dewey_cmp(pkg1, DEWEY_LT, pkg2))
276 return -1;
277 else if (dewey_cmp(pkg1, DEWEY_GT, pkg2))
278 return 1;
279 else
280 return 0;
281}
282
283/*
284 * Perform dewey match on "pkg" against "pattern".
285 * Return 1 on match, 0 on non-match, -1 on error.
286 */
287int HIDDEN
288dewey_match(const char *pattern, const char *pkg)
289{
290 const char *version;
291 const char *sep, *sep2;
292 int op, op2;
293 int n;
294
295 /* compare names */
296 if ((version=strrchr(pkg, '-')) == NULL) {
297 return 0;
298 }
299 if ((sep = strpbrk(pattern, "<>")) == NULL)
300 return -1;
301 /* compare name lengths */
302 if ((sep-pattern != version-pkg) ||
303 strncmp(pkg, pattern, (size_t)(version-pkg)) != 0)
304 return 0;
305 version++;
306
307 /* extract comparison operator */
308 if ((n = dewey_mktest(&op, sep)) < 0) {
309 return 0;
310 }
311 /* skip operator */
312 sep += n;
313
314 /* if greater than, look for less than */
315 sep2 = NULL;
316 if (op == DEWEY_GT || op == DEWEY_GE) {
317 if ((sep2 = strchr(sep, '<')) != NULL) {
318 if ((n = dewey_mktest(&op2, sep2)) < 0) {
319 return 0;
320 }
321 /* compare upper limit */
322 if (!dewey_cmp(version, op2, sep2+n))
323 return 0;
324 }
325 }
326
327 /* compare only pattern / lower limit */
328 if (sep2) {
329 char ver[PKG_PATTERN_MAX];
330
331 xbps_strlcpy(ver, sep, MIN((ssize_t)sizeof(ver), sep2-sep+1));
332 if (dewey_cmp(version, op, ver))
333 return 1;
334 } else {
335 if (dewey_cmp(version, op, sep))
336 return 1;
337 }
338
339 return 0;
340}
341
size_t xbps_strlcpy(char *dst, const char *src, size_t dstsize)
Definition util.c:575
int xbps_cmpver(const char *pkg1, const char *pkg2)
Definition dewey.c:273