tag: show tag notes with --notes
[git/mjg.git] / wildmatch.c
blob3972e26e83a5131f746399deaa32835c7c9915cb
1 /*
2 ** Do shell-style pattern matching for ?, \, [], and * characters.
3 ** It is 8bit clean.
4 **
5 ** Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
6 ** Rich $alz is now <rsalz@bbn.com>.
7 **
8 ** Modified by Wayne Davison to special-case '/' matching, to make '**'
9 ** work differently than '*', and to fix the character-class code.
12 #include "cache.h"
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 FALSE 0
22 #define TRUE 1
24 #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
25 && *(class) == *(litmatch) \
26 && strncmp((char*)class, litmatch, len) == 0)
28 #if defined STDC_HEADERS || !defined isascii
29 # define ISASCII(c) 1
30 #else
31 # define ISASCII(c) isascii(c)
32 #endif
34 #ifdef isblank
35 # define ISBLANK(c) (ISASCII(c) && isblank(c))
36 #else
37 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
38 #endif
40 #ifdef isgraph
41 # define ISGRAPH(c) (ISASCII(c) && isgraph(c))
42 #else
43 # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
44 #endif
46 #define ISPRINT(c) (ISASCII(c) && isprint(c))
47 #define ISDIGIT(c) (ISASCII(c) && isdigit(c))
48 #define ISALNUM(c) (ISASCII(c) && isalnum(c))
49 #define ISALPHA(c) (ISASCII(c) && isalpha(c))
50 #define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
51 #define ISLOWER(c) (ISASCII(c) && islower(c))
52 #define ISPUNCT(c) (ISASCII(c) && ispunct(c))
53 #define ISSPACE(c) (ISASCII(c) && isspace(c))
54 #define ISUPPER(c) (ISASCII(c) && isupper(c))
55 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
57 /* Match pattern "p" against "text" */
58 static int dowild(const uchar *p, const uchar *text, int force_lower_case)
60 uchar p_ch;
62 for ( ; (p_ch = *p) != '\0'; text++, p++) {
63 int matched, special;
64 uchar t_ch, prev_ch;
65 if ((t_ch = *text) == '\0' && p_ch != '*')
66 return ABORT_ALL;
67 if (force_lower_case && ISUPPER(t_ch))
68 t_ch = tolower(t_ch);
69 if (force_lower_case && ISUPPER(p_ch))
70 p_ch = tolower(p_ch);
71 switch (p_ch) {
72 case '\\':
73 /* Literal match with following character. Note that the test
74 * in "default" handles the p[1] == '\0' failure case. */
75 p_ch = *++p;
76 /* FALLTHROUGH */
77 default:
78 if (t_ch != p_ch)
79 return NOMATCH;
80 continue;
81 case '?':
82 /* Match anything but '/'. */
83 if (t_ch == '/')
84 return NOMATCH;
85 continue;
86 case '*':
87 if (*++p == '*') {
88 const uchar *prev_p = p - 2;
89 while (*++p == '*') {}
90 if ((prev_p == text || *prev_p == '/') ||
91 (*p == '\0' || *p == '/' ||
92 (p[0] == '\\' && p[1] == '/'))) {
94 * Assuming we already match 'foo/' and are at
95 * <star star slash>, just assume it matches
96 * nothing and go ahead match the rest of the
97 * pattern with the remaining string. This
98 * helps make foo/<*><*>/bar (<> because
99 * otherwise it breaks C comment syntax) match
100 * both foo/bar and foo/a/bar.
102 if (p[0] == '/' &&
103 dowild(p + 1, text, force_lower_case) == MATCH)
104 return MATCH;
105 special = TRUE;
106 } else
107 return ABORT_MALFORMED;
108 } else
109 special = FALSE;
110 if (*p == '\0') {
111 /* Trailing "**" matches everything. Trailing "*" matches
112 * only if there are no more slash characters. */
113 if (!special) {
114 if (strchr((char*)text, '/') != NULL)
115 return NOMATCH;
117 return MATCH;
119 while (1) {
120 if (t_ch == '\0')
121 break;
122 if ((matched = dowild(p, text, force_lower_case)) != NOMATCH) {
123 if (!special || matched != ABORT_TO_STARSTAR)
124 return matched;
125 } else if (!special && t_ch == '/')
126 return ABORT_TO_STARSTAR;
127 t_ch = *++text;
129 return ABORT_ALL;
130 case '[':
131 p_ch = *++p;
132 #ifdef NEGATE_CLASS2
133 if (p_ch == NEGATE_CLASS2)
134 p_ch = NEGATE_CLASS;
135 #endif
136 /* Assign literal TRUE/FALSE because of "matched" comparison. */
137 special = p_ch == NEGATE_CLASS? TRUE : FALSE;
138 if (special) {
139 /* Inverted character class. */
140 p_ch = *++p;
142 prev_ch = 0;
143 matched = FALSE;
144 do {
145 if (!p_ch)
146 return ABORT_ALL;
147 if (p_ch == '\\') {
148 p_ch = *++p;
149 if (!p_ch)
150 return ABORT_ALL;
151 if (t_ch == p_ch)
152 matched = TRUE;
153 } else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') {
154 p_ch = *++p;
155 if (p_ch == '\\') {
156 p_ch = *++p;
157 if (!p_ch)
158 return ABORT_ALL;
160 if (t_ch <= p_ch && t_ch >= prev_ch)
161 matched = TRUE;
162 p_ch = 0; /* This makes "prev_ch" get set to 0. */
163 } else if (p_ch == '[' && p[1] == ':') {
164 const uchar *s;
165 int i;
166 for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
167 if (!p_ch)
168 return ABORT_ALL;
169 i = p - s - 1;
170 if (i < 0 || p[-1] != ':') {
171 /* Didn't find ":]", so treat like a normal set. */
172 p = s - 2;
173 p_ch = '[';
174 if (t_ch == p_ch)
175 matched = TRUE;
176 continue;
178 if (CC_EQ(s,i, "alnum")) {
179 if (ISALNUM(t_ch))
180 matched = TRUE;
181 } else if (CC_EQ(s,i, "alpha")) {
182 if (ISALPHA(t_ch))
183 matched = TRUE;
184 } else if (CC_EQ(s,i, "blank")) {
185 if (ISBLANK(t_ch))
186 matched = TRUE;
187 } else if (CC_EQ(s,i, "cntrl")) {
188 if (ISCNTRL(t_ch))
189 matched = TRUE;
190 } else if (CC_EQ(s,i, "digit")) {
191 if (ISDIGIT(t_ch))
192 matched = TRUE;
193 } else if (CC_EQ(s,i, "graph")) {
194 if (ISGRAPH(t_ch))
195 matched = TRUE;
196 } else if (CC_EQ(s,i, "lower")) {
197 if (ISLOWER(t_ch))
198 matched = TRUE;
199 } else if (CC_EQ(s,i, "print")) {
200 if (ISPRINT(t_ch))
201 matched = TRUE;
202 } else if (CC_EQ(s,i, "punct")) {
203 if (ISPUNCT(t_ch))
204 matched = TRUE;
205 } else if (CC_EQ(s,i, "space")) {
206 if (ISSPACE(t_ch))
207 matched = TRUE;
208 } else if (CC_EQ(s,i, "upper")) {
209 if (ISUPPER(t_ch))
210 matched = TRUE;
211 } else if (CC_EQ(s,i, "xdigit")) {
212 if (ISXDIGIT(t_ch))
213 matched = TRUE;
214 } else /* malformed [:class:] string */
215 return ABORT_ALL;
216 p_ch = 0; /* This makes "prev_ch" get set to 0. */
217 } else if (t_ch == p_ch)
218 matched = TRUE;
219 } while (prev_ch = p_ch, (p_ch = *++p) != ']');
220 if (matched == special || t_ch == '/')
221 return NOMATCH;
222 continue;
226 return *text ? NOMATCH : MATCH;
229 /* Match the "pattern" against the "text" string. */
230 int wildmatch(const char *pattern, const char *text, int flags)
232 return dowild((const uchar*)pattern, (const uchar*)text,
233 flags & FNM_CASEFOLD ? 1 :0);