Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / pkg_install / dist / lib / opattern.c
blob3861f3250aee3bef6fc02d8ad6c30f4601e896fb
1 /* $NetBSD: opattern.c,v 1.5 2009/02/02 12:35:01 joerg Exp $ */
3 #if HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 #include <nbcompat.h>
7 #if HAVE_SYS_CDEFS_H
8 #include <sys/cdefs.h>
9 #endif
10 __RCSID("$NetBSD: opattern.c,v 1.5 2009/02/02 12:35:01 joerg Exp $");
13 * FreeBSD install - a package for the installation and maintainance
14 * of non-core utilities.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * Jordan K. Hubbard
26 * 18 July 1993
28 * Miscellaneous string utilities.
32 #if HAVE_ASSERT_H
33 #include <assert.h>
34 #endif
35 #if HAVE_ERR_H
36 #include <err.h>
37 #endif
38 #if HAVE_FNMATCH_H
39 #include <fnmatch.h>
40 #endif
41 #include "lib.h"
42 #include "dewey.h"
44 /* pull in definitions and macros for resizing arrays as we go */
45 #include "defs.h"
48 * Perform alternate match on "pkg" against "pattern",
49 * calling pkg_match (recursively) to resolve any other patterns.
50 * Return 1 on match, 0 otherwise
52 static int
53 alternate_match(const char *pattern, const char *pkg)
55 char *sep;
56 char buf[MaxPathSize];
57 char *last;
58 char *alt;
59 char *cp;
60 int cnt;
61 int found;
63 if ((sep = strchr(pattern, '{')) == (char *) NULL) {
64 errx(EXIT_FAILURE, "alternate_match(): '{' expected in `%s'", pattern);
66 (void) strncpy(buf, pattern, (size_t) (sep - pattern));
67 alt = &buf[sep - pattern];
68 last = (char *) NULL;
69 for (cnt = 0, cp = sep; *cp && last == (char *) NULL; cp++) {
70 if (*cp == '{') {
71 cnt++;
72 } else if (*cp == '}' && --cnt == 0 && last == (char *) NULL) {
73 last = cp + 1;
76 if (cnt != 0) {
77 errx(EXIT_FAILURE, "Malformed alternate `%s'", pattern);
79 for (found = 0, cp = sep + 1; *sep != '}'; cp = sep + 1) {
80 for (cnt = 0, sep = cp; cnt > 0 || (cnt == 0 && *sep != '}' && *sep != ','); sep++) {
81 if (*sep == '{') {
82 cnt++;
83 } else if (*sep == '}') {
84 cnt--;
87 (void) snprintf(alt, sizeof(buf) - (alt - buf), "%.*s%s", (int) (sep - cp), cp, last);
88 if (pkg_match(buf, pkg) == 1) {
89 found = 1;
92 return found;
96 * Perform glob match on "pkg" against "pattern".
97 * Return 1 on match, 0 otherwise
99 static int
100 glob_match(const char *pattern, const char *pkg)
102 return fnmatch(pattern, pkg, FNM_PERIOD) == 0;
106 * Perform simple match on "pkg" against "pattern".
107 * Return 1 on match, 0 otherwise
109 static int
110 simple_match(const char *pattern, const char *pkg)
112 return strcmp(pattern, pkg) == 0;
116 * Performs a fast check if pattern can ever match pkg.
117 * Returns 1 if a match is possible and 0 otherwise.
120 quick_pkg_match(const char *pattern, const char *pkg)
122 #define simple(x) (isalnum((unsigned char)(x)) || (x) == '-')
123 if (!simple(pattern[0]))
124 return 1;
125 if (pattern[0] != pkg[0])
126 return 0;
128 if (!simple(pattern[1]))
129 return 1;
130 if (pattern[1] != pkg[1])
131 return 0;
132 return 1;
133 #undef simple
137 * Match pkg against pattern, return 1 if matching, 0 else
140 pkg_match(const char *pattern, const char *pkg)
142 if (!quick_pkg_match(pattern, pkg))
143 return 0;
145 if (strchr(pattern, '{') != (char *) NULL) {
146 /* emulate csh-type alternates */
147 return alternate_match(pattern, pkg);
149 if (strpbrk(pattern, "<>") != (char *) NULL) {
150 int ret;
152 /* perform relational dewey match on version number */
153 ret = dewey_match(pattern, pkg);
154 if (ret < 0)
155 errx(EXIT_FAILURE, "dewey_match returned error");
156 return ret;
158 if (strpbrk(pattern, "*?[]") != (char *) NULL) {
159 /* glob match */
160 if (glob_match(pattern, pkg))
161 return 1;
164 /* no alternate, dewey or glob match -> simple compare */
165 if (simple_match(pattern, pkg))
166 return 1;
168 /* globbing patterns and simple matches may be specified with or
169 * without the version number, so check for both cases. */
172 char *pattern_ver;
173 int retval;
175 pattern_ver = xasprintf("%s-[0-9]*", pattern);
176 retval = glob_match(pattern_ver, pkg);
177 free(pattern_ver);
178 return retval;
183 pkg_order(const char *pattern, const char *first_pkg, const char *second_pkg)
185 const char *first_version;
186 const char *second_version;
188 if (first_pkg == NULL && second_pkg == NULL)
189 return 0;
191 if (first_pkg == NULL)
192 return pkg_match(pattern, second_pkg) ? 2 : 0;
193 if (second_pkg == NULL)
194 return pkg_match(pattern, first_pkg) ? 1 : 0;
196 first_version = strrchr(first_pkg, '-');
197 second_version = strrchr(second_pkg, '-');
199 if (first_version == NULL || !pkg_match(pattern, first_pkg))
200 return pkg_match(pattern, second_pkg) ? 2 : 0;
202 if (second_version == NULL || !pkg_match(pattern, second_pkg))
203 return pkg_match(pattern, first_pkg) ? 1 : 0;
205 if (dewey_cmp(first_version + 1, DEWEY_GT, second_version + 1))
206 return 1;
207 else
208 return 2;