Added a comment to a shared iterator to avoid a warning from IBM's checker.
[rsync/rsync.git] / lib / wildmatch.c
CommitLineData
446ee5b1
WD
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**
7a1f46b6
WD
8** Modified by Wayne Davison to special-case '/' matching, to make '**'
9** work differently than '*', and to fix the character-class code.
446ee5b1
WD
10*/
11
12#include "rsync.h"
13
14/* What character marks an inverted character class? */
e11c4251
WD
15#define NEGATE_CLASS '!'
16#define NEGATE_CLASS2 '^'
446ee5b1 17
710faea9
WD
18#define FALSE 0
19#define TRUE 1
20b2e9ce
WD
20#define ABORT_ALL -1
21#define ABORT_TO_STARSTAR -2
22
5bb92000
WD
23#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
24 && *(class) == *(litmatch) \
136c5c5e 25 && strncmp((char*)class, litmatch, len) == 0)
e11c4251 26
f2ac84c3
WD
27#if defined STDC_HEADERS || !defined isascii
28# define ISASCII(c) 1
29#else
30# define ISASCII(c) isascii(c)
31#endif
32
33#ifdef isblank
34# define ISBLANK(c) (ISASCII(c) && isblank(c))
35#else
36# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
37#endif
38
39#ifdef isgraph
40# define ISGRAPH(c) (ISASCII(c) && isgraph(c))
41#else
42# define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
43#endif
44
45#define ISPRINT(c) (ISASCII(c) && isprint(c))
46#define ISDIGIT(c) (ISASCII(c) && isdigit(c))
47#define ISALNUM(c) (ISASCII(c) && isalnum(c))
48#define ISALPHA(c) (ISASCII(c) && isalpha(c))
49#define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
50#define ISLOWER(c) (ISASCII(c) && islower(c))
51#define ISPUNCT(c) (ISASCII(c) && ispunct(c))
52#define ISSPACE(c) (ISASCII(c) && isspace(c))
53#define ISUPPER(c) (ISASCII(c) && isupper(c))
54#define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
55
d5c973cc
WD
56#ifdef WILD_TEST_ITERATIONS
57int wildmatch_iteration_count;
20b2e9ce 58#endif
446ee5b1 59
8e744636
WD
60static int force_lower_case = 0;
61
390621a7
WD
62/* Match pattern "p" against the a virtually-joined string consisting
63 * of "text" and any strings in array "a". */
64static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
446ee5b1 65{
8e744636 66 uchar p_ch;
446ee5b1 67
d5c973cc
WD
68#ifdef WILD_TEST_ITERATIONS
69 wildmatch_iteration_count++;
20b2e9ce
WD
70#endif
71
8e744636
WD
72 for ( ; (p_ch = *p) != '\0'; text++, p++) {
73 int matched, special;
74 uchar t_ch, prev_ch;
390621a7
WD
75 while ((t_ch = *text) == '\0') {
76 if (*a == NULL) {
77 if (p_ch != '*')
78 return ABORT_ALL;
79 break;
80 }
81 text = *a++;
82 }
8e744636
WD
83 if (force_lower_case && ISUPPER(t_ch))
84 t_ch = tolower(t_ch);
85 switch (p_ch) {
446ee5b1
WD
86 case '\\':
87 /* Literal match with following character. Note that the test
88 * in "default" handles the p[1] == '\0' failure case. */
8e744636 89 p_ch = *++p;
446ee5b1
WD
90 /* FALLTHROUGH */
91 default:
8e744636 92 if (t_ch != p_ch)
710faea9 93 return FALSE;
446ee5b1
WD
94 continue;
95 case '?':
96 /* Match anything but '/'. */
8e744636 97 if (t_ch == '/')
710faea9 98 return FALSE;
446ee5b1
WD
99 continue;
100 case '*':
101 if (*++p == '*') {
102 while (*++p == '*') {}
710faea9 103 special = TRUE;
e725abcf 104 } else
710faea9 105 special = FALSE;
446ee5b1 106 if (*p == '\0') {
710faea9
WD
107 /* Trailing "**" matches everything. Trailing "*" matches
108 * only if there are no more slash characters. */
390621a7
WD
109 if (!special) {
110 do {
111 if (strchr((char*)text, '/') != NULL)
112 return FALSE;
113 } while ((text = *a++) != NULL);
114 }
115 return TRUE;
446ee5b1 116 }
390621a7
WD
117 while (1) {
118 if (t_ch == '\0') {
119 if ((text = *a++) == NULL)
120 break;
121 t_ch = *text;
122 continue;
123 }
124 if ((matched = dowild(p, text, a)) != FALSE) {
20b2e9ce
WD
125 if (!special || matched != ABORT_TO_STARSTAR)
126 return matched;
8e744636 127 } else if (!special && t_ch == '/')
20b2e9ce 128 return ABORT_TO_STARSTAR;
390621a7 129 t_ch = *++text;
446ee5b1 130 }
20b2e9ce 131 return ABORT_ALL;
446ee5b1 132 case '[':
8e744636 133 p_ch = *++p;
e11c4251 134#ifdef NEGATE_CLASS2
8e744636
WD
135 if (p_ch == NEGATE_CLASS2)
136 p_ch = NEGATE_CLASS;
e11c4251 137#endif
c9a59880 138 /* Assign literal TRUE/FALSE because of "matched" comparison. */
8e744636 139 special = p_ch == NEGATE_CLASS? TRUE : FALSE;
446ee5b1
WD
140 if (special) {
141 /* Inverted character class. */
8e744636 142 p_ch = *++p;
446ee5b1 143 }
8e744636 144 prev_ch = 0;
710faea9 145 matched = FALSE;
c9a59880 146 do {
8e744636 147 if (!p_ch)
5bb92000 148 return ABORT_ALL;
8e744636
WD
149 if (p_ch == '\\') {
150 p_ch = *++p;
151 if (!p_ch)
5bb92000 152 return ABORT_ALL;
8e744636 153 if (t_ch == p_ch)
e11c4251 154 matched = TRUE;
8e744636
WD
155 } else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') {
156 p_ch = *++p;
157 if (p_ch == '\\') {
158 p_ch = *++p;
159 if (!p_ch)
5bb92000 160 return ABORT_ALL;
e11c4251 161 }
8e744636 162 if (t_ch <= p_ch && t_ch >= prev_ch)
e11c4251 163 matched = TRUE;
8e744636
WD
164 p_ch = 0; /* This makes "prev_ch" get set to 0. */
165 } else if (p_ch == '[' && p[1] == ':') {
5f238db2 166 const uchar *s;
e11c4251 167 int i;
3d0a159d 168 for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
8e744636 169 if (!p_ch)
5bb92000 170 return ABORT_ALL;
fc96552d
WD
171 i = p - s - 1;
172 if (i < 0 || p[-1] != ':') {
173 /* Didn't find ":]", so treat like a normal set. */
174 p = s - 2;
8e744636
WD
175 p_ch = '[';
176 if (t_ch == p_ch)
fc96552d
WD
177 matched = TRUE;
178 continue;
179 }
5bb92000 180 if (CC_EQ(s,i, "alnum")) {
8e744636 181 if (ISALNUM(t_ch))
5bb92000 182 matched = TRUE;
e725abcf 183 } else if (CC_EQ(s,i, "alpha")) {
8e744636 184 if (ISALPHA(t_ch))
5bb92000 185 matched = TRUE;
e725abcf 186 } else if (CC_EQ(s,i, "blank")) {
8e744636 187 if (ISBLANK(t_ch))
5bb92000 188 matched = TRUE;
e725abcf 189 } else if (CC_EQ(s,i, "cntrl")) {
8e744636 190 if (ISCNTRL(t_ch))
5bb92000 191 matched = TRUE;
e725abcf 192 } else if (CC_EQ(s,i, "digit")) {
8e744636 193 if (ISDIGIT(t_ch))
5bb92000 194 matched = TRUE;
e725abcf 195 } else if (CC_EQ(s,i, "graph")) {
8e744636 196 if (ISGRAPH(t_ch))
5bb92000 197 matched = TRUE;
e725abcf 198 } else if (CC_EQ(s,i, "lower")) {
8e744636 199 if (ISLOWER(t_ch))
5bb92000 200 matched = TRUE;
e725abcf 201 } else if (CC_EQ(s,i, "print")) {
8e744636 202 if (ISPRINT(t_ch))
5bb92000 203 matched = TRUE;
e725abcf 204 } else if (CC_EQ(s,i, "punct")) {
8e744636 205 if (ISPUNCT(t_ch))
5bb92000 206 matched = TRUE;
e725abcf 207 } else if (CC_EQ(s,i, "space")) {
8e744636 208 if (ISSPACE(t_ch))
5bb92000 209 matched = TRUE;
e725abcf 210 } else if (CC_EQ(s,i, "upper")) {
8e744636 211 if (ISUPPER(t_ch))
5bb92000 212 matched = TRUE;
e725abcf 213 } else if (CC_EQ(s,i, "xdigit")) {
8e744636 214 if (ISXDIGIT(t_ch))
5bb92000 215 matched = TRUE;
e725abcf 216 } else /* malformed [:class:] string */
5bb92000 217 return ABORT_ALL;
8e744636
WD
218 p_ch = 0; /* This makes "prev_ch" get set to 0. */
219 } else if (t_ch == p_ch)
710faea9 220 matched = TRUE;
8e744636
WD
221 } while (prev_ch = p_ch, (p_ch = *++p) != ']');
222 if (matched == special || t_ch == '/')
710faea9 223 return FALSE;
446ee5b1
WD
224 continue;
225 }
226 }
227
390621a7
WD
228 do {
229 if (*text)
230 return FALSE;
231 } while ((text = *a++) != NULL);
232
233 return TRUE;
234}
235
236/* Match literal string "s" against the a virtually-joined string consisting
237 * of "text" and any strings in array "a". */
238static int doliteral(const uchar *s, const uchar *text, const uchar*const *a)
239{
240 for ( ; *s != '\0'; text++, s++) {
241 while (*text == '\0') {
242 if ((text = *a++) == NULL)
243 return FALSE;
244 }
245 if (*text != *s)
246 return FALSE;
247 }
248
249 do {
250 if (*text)
251 return FALSE;
252 } while ((text = *a++) != NULL);
253
254 return TRUE;
255}
256
257/* Return the last "count" path elements from the concatenated string.
258 * We return a string pointer to the start of the string, and update the
259 * array pointer-pointer to point to any remaining string elements. */
260static const uchar *trailing_N_elements(const uchar*const **a_ptr, int count)
261{
262 const uchar*const *a = *a_ptr;
263 const uchar*const *first_a = a;
264
265 while (*a)
266 a++;
267
268 while (a != first_a) {
269 const uchar *s = *--a;
270 s += strlen((char*)s);
271 while (--s >= *a) {
272 if (*s == '/' && !--count) {
273 *a_ptr = a+1;
274 return s+1;
275 }
276 }
277 }
278
279 if (count == 1) {
280 *a_ptr = a+1;
281 return *a;
282 }
283
284 return NULL;
446ee5b1 285}
710faea9 286
e725abcf
WD
287/* Match the "pattern" against the "text" string. */
288int wildmatch(const char *pattern, const char *text)
710faea9 289{
390621a7 290 static const uchar *nomore[1]; /* A NULL pointer. */
d5c973cc
WD
291#ifdef WILD_TEST_ITERATIONS
292 wildmatch_iteration_count = 0;
20b2e9ce 293#endif
390621a7 294 return dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
710faea9 295}
8e744636
WD
296
297/* Match the "pattern" against the forced-to-lower-case "text" string. */
298int iwildmatch(const char *pattern, const char *text)
299{
390621a7 300 static const uchar *nomore[1]; /* A NULL pointer. */
8e744636
WD
301 int ret;
302#ifdef WILD_TEST_ITERATIONS
303 wildmatch_iteration_count = 0;
304#endif
305 force_lower_case = 1;
390621a7 306 ret = dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
8e744636
WD
307 force_lower_case = 0;
308 return ret;
309}
390621a7
WD
310
311/* Match pattern "p" against the a virtually-joined string consisting
312 * of all the pointers in array "texts" (which has a NULL pointer at the
313 * end). The int "where" can be 0 (normal matching), > 0 (match only
314 * the trailing N slash-separated filename components of "texts"), or < 0
315 * (match the "pattern" at the start or after any slash in "texts"). */
316int wildmatch_array(const char *pattern, const char*const *texts, int where)
317{
318 const uchar *p = (const uchar*)pattern;
319 const uchar*const *a = (const uchar*const*)texts;
320 const uchar *text;
321 int matched;
322
323#ifdef WILD_TEST_ITERATIONS
324 wildmatch_iteration_count = 0;
325#endif
326
327 if (where > 0)
328 text = trailing_N_elements(&a, where);
329 else
330 text = *a++;
331 if (!text)
332 return FALSE;
333
334 if ((matched = dowild(p, text, a)) != TRUE && where < 0
335 && matched != ABORT_ALL) {
336 while (1) {
337 if (*text == '\0') {
338 if ((text = (uchar*)*a++) == NULL)
339 return FALSE;
340 continue;
341 }
342 if (*text++ == '/' && (matched = dowild(p, text, a)) != FALSE
343 && matched != ABORT_TO_STARSTAR)
344 break;
345 }
346 }
347 return matched == TRUE;
348}
349
350/* Match literal string "s" against the a virtually-joined string consisting
351 * of all the pointers in array "texts" (which has a NULL pointer at the
352 * end). The int "where" can be 0 (normal matching), or > 0 (match
353 * only the trailing N slash-separated filename components of "texts"). */
354int litmatch_array(const char *string, const char*const *texts, int where)
355{
356 const uchar *s = (const uchar*)string;
357 const uchar*const *a = (const uchar* const*)texts;
358 const uchar *text;
359
360 if (where > 0)
361 text = trailing_N_elements(&a, where);
362 else
363 text = *a++;
364 if (!text)
365 return FALSE;
366
367 return doliteral(s, text, a) == TRUE;
368}