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