2 ** Do shell-style pattern matching for ?, \, [], and * characters.
5 ** Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
6 ** Rich $alz is now <rsalz@bbn.com>.
8 ** Modified by Wayne Davison to special-case '/' matching, to make '**'
9 ** work differently than '*', and to fix the character-class code.
13 #include "wildmatch.h"
15 typedef unsigned char uchar
;
17 /* What character marks an inverted character class? */
18 #define NEGATE_CLASS '!'
19 #define NEGATE_CLASS2 '^'
21 #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
22 && *(class) == *(litmatch) \
23 && strncmp((char*)class, litmatch, len) == 0)
25 #if defined STDC_HEADERS || !defined isascii
28 # define ISASCII(c) isascii(c)
32 # define ISBLANK(c) (ISASCII(c) && isblank(c))
34 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
38 # define ISGRAPH(c) (ISASCII(c) && isgraph(c))
40 # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
43 #define ISPRINT(c) (ISASCII(c) && isprint(c))
44 #define ISDIGIT(c) (ISASCII(c) && isdigit(c))
45 #define ISALNUM(c) (ISASCII(c) && isalnum(c))
46 #define ISALPHA(c) (ISASCII(c) && isalpha(c))
47 #define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
48 #define ISLOWER(c) (ISASCII(c) && islower(c))
49 #define ISPUNCT(c) (ISASCII(c) && ispunct(c))
50 #define ISSPACE(c) (ISASCII(c) && isspace(c))
51 #define ISUPPER(c) (ISASCII(c) && isupper(c))
52 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
54 /* Match pattern "p" against "text" */
55 static int dowild(const uchar
*p
, const uchar
*text
, int force_lower_case
)
58 const uchar
*pattern
= p
;
60 for ( ; (p_ch
= *p
) != '\0'; text
++, p
++) {
61 int matched
, match_slash
, negated
;
63 if ((t_ch
= *text
) == '\0' && p_ch
!= '*')
65 if (force_lower_case
&& ISUPPER(t_ch
))
67 if (force_lower_case
&& ISUPPER(p_ch
))
71 /* Literal match with following character. Note that the test
72 * in "default" handles the p[1] == '\0' failure case. */
80 /* Match anything but '/'. */
86 const uchar
*prev_p
= p
- 2;
87 while (*++p
== '*') {}
88 if ((prev_p
< pattern
|| *prev_p
== '/') &&
89 (*p
== '\0' || *p
== '/' ||
90 (p
[0] == '\\' && p
[1] == '/'))) {
92 * Assuming we already match 'foo/' and are at
93 * <star star slash>, just assume it matches
94 * nothing and go ahead match the rest of the
95 * pattern with the remaining string. This
96 * helps make foo/<*><*>/bar (<> because
97 * otherwise it breaks C comment syntax) match
98 * both foo/bar and foo/a/bar.
101 dowild(p
+ 1, text
, force_lower_case
) == WM_MATCH
)
105 return WM_ABORT_MALFORMED
;
109 /* Trailing "**" matches everything. Trailing "*" matches
110 * only if there are no more slash characters. */
112 if (strchr((char*)text
, '/') != NULL
)
120 if ((matched
= dowild(p
, text
, force_lower_case
)) != WM_NOMATCH
) {
121 if (!match_slash
|| matched
!= WM_ABORT_TO_STARSTAR
)
123 } else if (!match_slash
&& t_ch
== '/')
124 return WM_ABORT_TO_STARSTAR
;
131 if (p_ch
== NEGATE_CLASS2
)
134 /* Assign literal 1/0 because of "matched" comparison. */
135 negated
= p_ch
== NEGATE_CLASS
? 1 : 0;
137 /* Inverted character class. */
151 } else if (p_ch
== '-' && prev_ch
&& p
[1] && p
[1] != ']') {
158 if (t_ch
<= p_ch
&& t_ch
>= prev_ch
)
160 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
161 } else if (p_ch
== '[' && p
[1] == ':') {
164 for (s
= p
+= 2; (p_ch
= *p
) && p_ch
!= ']'; p
++) {} /*SHARED ITERATOR*/
168 if (i
< 0 || p
[-1] != ':') {
169 /* Didn't find ":]", so treat like a normal set. */
176 if (CC_EQ(s
,i
, "alnum")) {
179 } else if (CC_EQ(s
,i
, "alpha")) {
182 } else if (CC_EQ(s
,i
, "blank")) {
185 } else if (CC_EQ(s
,i
, "cntrl")) {
188 } else if (CC_EQ(s
,i
, "digit")) {
191 } else if (CC_EQ(s
,i
, "graph")) {
194 } else if (CC_EQ(s
,i
, "lower")) {
197 } else if (CC_EQ(s
,i
, "print")) {
200 } else if (CC_EQ(s
,i
, "punct")) {
203 } else if (CC_EQ(s
,i
, "space")) {
206 } else if (CC_EQ(s
,i
, "upper")) {
209 } else if (CC_EQ(s
,i
, "xdigit")) {
212 } else /* malformed [:class:] string */
214 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
215 } else if (t_ch
== p_ch
)
217 } while (prev_ch
= p_ch
, (p_ch
= *++p
) != ']');
218 if (matched
== negated
|| t_ch
== '/')
224 return *text
? WM_NOMATCH
: WM_MATCH
;
227 /* Match the "pattern" against the "text" string. */
228 int wildmatch(const char *pattern
, const char *text
,
229 unsigned int flags
, struct wildopts
*wo
)
231 return dowild((const uchar
*)pattern
, (const uchar
*)text
,
232 flags
& WM_CASEFOLD
? 1 :0);