1*f46918caSnia /* $NetBSD: opattern.c,v 1.3 2021/04/10 19:49:59 nia Exp $ */
2af21abb5Sjoerg
3af21abb5Sjoerg #if HAVE_CONFIG_H
4af21abb5Sjoerg #include "config.h"
5af21abb5Sjoerg #endif
6af21abb5Sjoerg #include <nbcompat.h>
7af21abb5Sjoerg #if HAVE_SYS_CDEFS_H
8af21abb5Sjoerg #include <sys/cdefs.h>
9af21abb5Sjoerg #endif
10*f46918caSnia __RCSID("$NetBSD: opattern.c,v 1.3 2021/04/10 19:49:59 nia Exp $");
11af21abb5Sjoerg
12af21abb5Sjoerg /*
13af21abb5Sjoerg * FreeBSD install - a package for the installation and maintainance
14af21abb5Sjoerg * of non-core utilities.
15af21abb5Sjoerg *
16af21abb5Sjoerg * Redistribution and use in source and binary forms, with or without
17af21abb5Sjoerg * modification, are permitted provided that the following conditions
18af21abb5Sjoerg * are met:
19af21abb5Sjoerg * 1. Redistributions of source code must retain the above copyright
20af21abb5Sjoerg * notice, this list of conditions and the following disclaimer.
21af21abb5Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
22af21abb5Sjoerg * notice, this list of conditions and the following disclaimer in the
23af21abb5Sjoerg * documentation and/or other materials provided with the distribution.
24af21abb5Sjoerg *
25af21abb5Sjoerg * Jordan K. Hubbard
26af21abb5Sjoerg * 18 July 1993
27af21abb5Sjoerg *
28af21abb5Sjoerg * Miscellaneous string utilities.
29af21abb5Sjoerg *
30af21abb5Sjoerg */
31af21abb5Sjoerg
32af21abb5Sjoerg #if HAVE_ASSERT_H
33af21abb5Sjoerg #include <assert.h>
34af21abb5Sjoerg #endif
35af21abb5Sjoerg #if HAVE_ERR_H
36af21abb5Sjoerg #include <err.h>
37af21abb5Sjoerg #endif
38af21abb5Sjoerg #if HAVE_FNMATCH_H
39af21abb5Sjoerg #include <fnmatch.h>
40af21abb5Sjoerg #endif
41af21abb5Sjoerg #include "lib.h"
42af21abb5Sjoerg #include "dewey.h"
43af21abb5Sjoerg
44af21abb5Sjoerg /* pull in definitions and macros for resizing arrays as we go */
45af21abb5Sjoerg #include "defs.h"
46af21abb5Sjoerg
47af21abb5Sjoerg /*
48af21abb5Sjoerg * Perform alternate match on "pkg" against "pattern",
49af21abb5Sjoerg * calling pkg_match (recursively) to resolve any other patterns.
50af21abb5Sjoerg * Return 1 on match, 0 otherwise
51af21abb5Sjoerg */
52af21abb5Sjoerg static int
alternate_match(const char * pattern,const char * pkg)53af21abb5Sjoerg alternate_match(const char *pattern, const char *pkg)
54af21abb5Sjoerg {
55af21abb5Sjoerg char *sep;
56af21abb5Sjoerg char buf[MaxPathSize];
57af21abb5Sjoerg char *last;
58af21abb5Sjoerg char *alt;
59af21abb5Sjoerg char *cp;
60af21abb5Sjoerg int cnt;
61af21abb5Sjoerg int found;
62af21abb5Sjoerg
63af21abb5Sjoerg if ((sep = strchr(pattern, '{')) == (char *) NULL) {
64af21abb5Sjoerg errx(EXIT_FAILURE, "alternate_match(): '{' expected in `%s'", pattern);
65af21abb5Sjoerg }
66af21abb5Sjoerg (void) strncpy(buf, pattern, (size_t) (sep - pattern));
67af21abb5Sjoerg alt = &buf[sep - pattern];
68af21abb5Sjoerg last = (char *) NULL;
69af21abb5Sjoerg for (cnt = 0, cp = sep; *cp && last == (char *) NULL; cp++) {
70af21abb5Sjoerg if (*cp == '{') {
71af21abb5Sjoerg cnt++;
72af21abb5Sjoerg } else if (*cp == '}' && --cnt == 0 && last == (char *) NULL) {
73af21abb5Sjoerg last = cp + 1;
74af21abb5Sjoerg }
75af21abb5Sjoerg }
76af21abb5Sjoerg if (cnt != 0) {
77af21abb5Sjoerg errx(EXIT_FAILURE, "Malformed alternate `%s'", pattern);
78af21abb5Sjoerg }
79af21abb5Sjoerg for (found = 0, cp = sep + 1; *sep != '}'; cp = sep + 1) {
80af21abb5Sjoerg for (cnt = 0, sep = cp; cnt > 0 || (cnt == 0 && *sep != '}' && *sep != ','); sep++) {
81af21abb5Sjoerg if (*sep == '{') {
82af21abb5Sjoerg cnt++;
83af21abb5Sjoerg } else if (*sep == '}') {
84af21abb5Sjoerg cnt--;
85af21abb5Sjoerg }
86af21abb5Sjoerg }
87af21abb5Sjoerg (void) snprintf(alt, sizeof(buf) - (alt - buf), "%.*s%s", (int) (sep - cp), cp, last);
88af21abb5Sjoerg if (pkg_match(buf, pkg) == 1) {
89af21abb5Sjoerg found = 1;
90af21abb5Sjoerg }
91af21abb5Sjoerg }
92af21abb5Sjoerg return found;
93af21abb5Sjoerg }
94af21abb5Sjoerg
95af21abb5Sjoerg /*
96af21abb5Sjoerg * Perform glob match on "pkg" against "pattern".
97af21abb5Sjoerg * Return 1 on match, 0 otherwise
98af21abb5Sjoerg */
99af21abb5Sjoerg static int
glob_match(const char * pattern,const char * pkg)100af21abb5Sjoerg glob_match(const char *pattern, const char *pkg)
101af21abb5Sjoerg {
102af21abb5Sjoerg return fnmatch(pattern, pkg, FNM_PERIOD) == 0;
103af21abb5Sjoerg }
104af21abb5Sjoerg
105af21abb5Sjoerg /*
106af21abb5Sjoerg * Perform simple match on "pkg" against "pattern".
107af21abb5Sjoerg * Return 1 on match, 0 otherwise
108af21abb5Sjoerg */
109af21abb5Sjoerg static int
simple_match(const char * pattern,const char * pkg)110af21abb5Sjoerg simple_match(const char *pattern, const char *pkg)
111af21abb5Sjoerg {
112af21abb5Sjoerg return strcmp(pattern, pkg) == 0;
113af21abb5Sjoerg }
114af21abb5Sjoerg
115af21abb5Sjoerg /*
116d66ee6c3Sjoerg * Performs a fast check if pattern can ever match pkg.
117d66ee6c3Sjoerg * Returns 1 if a match is possible and 0 otherwise.
118d66ee6c3Sjoerg */
119d66ee6c3Sjoerg int
quick_pkg_match(const char * pattern,const char * pkg)120d66ee6c3Sjoerg quick_pkg_match(const char *pattern, const char *pkg)
121d66ee6c3Sjoerg {
122d66ee6c3Sjoerg #define simple(x) (isalnum((unsigned char)(x)) || (x) == '-')
123d66ee6c3Sjoerg if (!simple(pattern[0]))
124d66ee6c3Sjoerg return 1;
125d66ee6c3Sjoerg if (pattern[0] != pkg[0])
126d66ee6c3Sjoerg return 0;
127d66ee6c3Sjoerg
128d66ee6c3Sjoerg if (!simple(pattern[1]))
129d66ee6c3Sjoerg return 1;
130d66ee6c3Sjoerg if (pattern[1] != pkg[1])
131d66ee6c3Sjoerg return 0;
132d66ee6c3Sjoerg return 1;
133d66ee6c3Sjoerg #undef simple
134d66ee6c3Sjoerg }
135d66ee6c3Sjoerg
136d66ee6c3Sjoerg /*
137af21abb5Sjoerg * Match pkg against pattern, return 1 if matching, 0 else
138af21abb5Sjoerg */
139af21abb5Sjoerg int
pkg_match(const char * pattern,const char * pkg)140af21abb5Sjoerg pkg_match(const char *pattern, const char *pkg)
141af21abb5Sjoerg {
142d66ee6c3Sjoerg if (!quick_pkg_match(pattern, pkg))
143d66ee6c3Sjoerg return 0;
144d66ee6c3Sjoerg
145af21abb5Sjoerg if (strchr(pattern, '{') != (char *) NULL) {
146af21abb5Sjoerg /* emulate csh-type alternates */
147af21abb5Sjoerg return alternate_match(pattern, pkg);
148af21abb5Sjoerg }
149af21abb5Sjoerg if (strpbrk(pattern, "<>") != (char *) NULL) {
150af21abb5Sjoerg int ret;
151af21abb5Sjoerg
152af21abb5Sjoerg /* perform relational dewey match on version number */
153af21abb5Sjoerg ret = dewey_match(pattern, pkg);
154af21abb5Sjoerg if (ret < 0)
155af21abb5Sjoerg errx(EXIT_FAILURE, "dewey_match returned error");
156af21abb5Sjoerg return ret;
157af21abb5Sjoerg }
158af21abb5Sjoerg if (strpbrk(pattern, "*?[]") != (char *) NULL) {
159af21abb5Sjoerg /* glob match */
160af21abb5Sjoerg if (glob_match(pattern, pkg))
161af21abb5Sjoerg return 1;
162af21abb5Sjoerg }
163af21abb5Sjoerg
164af21abb5Sjoerg /* no alternate, dewey or glob match -> simple compare */
165af21abb5Sjoerg if (simple_match(pattern, pkg))
166af21abb5Sjoerg return 1;
167af21abb5Sjoerg
168af21abb5Sjoerg /* globbing patterns and simple matches may be specified with or
169af21abb5Sjoerg * without the version number, so check for both cases. */
170af21abb5Sjoerg
171af21abb5Sjoerg {
172af21abb5Sjoerg char *pattern_ver;
173af21abb5Sjoerg int retval;
174af21abb5Sjoerg
175d66ee6c3Sjoerg pattern_ver = xasprintf("%s-[0-9]*", pattern);
176af21abb5Sjoerg retval = glob_match(pattern_ver, pkg);
177af21abb5Sjoerg free(pattern_ver);
178af21abb5Sjoerg return retval;
179af21abb5Sjoerg }
180af21abb5Sjoerg }
181af21abb5Sjoerg
182af21abb5Sjoerg int
pkg_order(const char * pattern,const char * first_pkg,const char * second_pkg)183af21abb5Sjoerg pkg_order(const char *pattern, const char *first_pkg, const char *second_pkg)
184af21abb5Sjoerg {
185af21abb5Sjoerg const char *first_version;
186af21abb5Sjoerg const char *second_version;
187af21abb5Sjoerg
188af21abb5Sjoerg if (first_pkg == NULL && second_pkg == NULL)
189af21abb5Sjoerg return 0;
190af21abb5Sjoerg
191af21abb5Sjoerg if (first_pkg == NULL)
192af21abb5Sjoerg return pkg_match(pattern, second_pkg) ? 2 : 0;
193af21abb5Sjoerg if (second_pkg == NULL)
194af21abb5Sjoerg return pkg_match(pattern, first_pkg) ? 1 : 0;
195af21abb5Sjoerg
196af21abb5Sjoerg first_version = strrchr(first_pkg, '-');
197af21abb5Sjoerg second_version = strrchr(second_pkg, '-');
198af21abb5Sjoerg
199af21abb5Sjoerg if (first_version == NULL || !pkg_match(pattern, first_pkg))
200af21abb5Sjoerg return pkg_match(pattern, second_pkg) ? 2 : 0;
201af21abb5Sjoerg
202af21abb5Sjoerg if (second_version == NULL || !pkg_match(pattern, second_pkg))
203af21abb5Sjoerg return pkg_match(pattern, first_pkg) ? 1 : 0;
204af21abb5Sjoerg
205af21abb5Sjoerg if (dewey_cmp(first_version + 1, DEWEY_GT, second_version + 1))
206af21abb5Sjoerg return 1;
2079cc7e269Stron else if (dewey_cmp(first_version + 1, DEWEY_LT, second_version + 1))
2089cc7e269Stron return 2;
2099cc7e269Stron else if (strcmp(first_pkg, second_pkg) < 0)
2109cc7e269Stron return 1;
211af21abb5Sjoerg else
212af21abb5Sjoerg return 2;
213af21abb5Sjoerg }
214