- Use "uchar" instead of "unsigned char".
[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
5f238db2 60static int domatch(const uchar *p, const uchar *text)
446ee5b1
WD
61{
62 int matched, special;
5f238db2 63 uchar ch, prev;
446ee5b1 64
d5c973cc
WD
65#ifdef WILD_TEST_ITERATIONS
66 wildmatch_iteration_count++;
20b2e9ce
WD
67#endif
68
446ee5b1
WD
69 for ( ; (ch = *p) != '\0'; text++, p++) {
70 if (*text == '\0' && ch != '*')
710faea9 71 return FALSE;
446ee5b1
WD
72 switch (ch) {
73 case '\\':
74 /* Literal match with following character. Note that the test
75 * in "default" handles the p[1] == '\0' failure case. */
76 ch = *++p;
77 /* FALLTHROUGH */
78 default:
79 if (*text != ch)
710faea9 80 return FALSE;
446ee5b1
WD
81 continue;
82 case '?':
83 /* Match anything but '/'. */
84 if (*text == '/')
710faea9 85 return FALSE;
446ee5b1
WD
86 continue;
87 case '*':
88 if (*++p == '*') {
89 while (*++p == '*') {}
710faea9 90 special = TRUE;
446ee5b1
WD
91 }
92 else
710faea9 93 special = FALSE;
446ee5b1 94 if (*p == '\0') {
710faea9
WD
95 /* Trailing "**" matches everything. Trailing "*" matches
96 * only if there are no more slash characters. */
136c5c5e 97 return special? TRUE : strchr((char*)text, '/') == NULL;
446ee5b1
WD
98 }
99 for ( ; *text; text++) {
20b2e9ce
WD
100 if ((matched = domatch(p, text)) != FALSE) {
101 if (!special || matched != ABORT_TO_STARSTAR)
102 return matched;
103 }
104 else if (!special && *text == '/')
105 return ABORT_TO_STARSTAR;
446ee5b1 106 }
20b2e9ce 107 return ABORT_ALL;
446ee5b1 108 case '[':
c9a59880 109 ch = *++p;
e11c4251
WD
110#ifdef NEGATE_CLASS2
111 if (ch == NEGATE_CLASS2)
112 ch = NEGATE_CLASS;
113#endif
c9a59880
WD
114 /* Assign literal TRUE/FALSE because of "matched" comparison. */
115 special = ch == NEGATE_CLASS? TRUE : FALSE;
446ee5b1
WD
116 if (special) {
117 /* Inverted character class. */
c9a59880 118 ch = *++p;
446ee5b1
WD
119 }
120 prev = 0;
710faea9 121 matched = FALSE;
c9a59880 122 do {
446ee5b1 123 if (!ch)
5bb92000 124 return ABORT_ALL;
e11c4251
WD
125 if (ch == '\\') {
126 ch = *++p;
127 if (!ch)
5bb92000 128 return ABORT_ALL;
e11c4251
WD
129 if (*text == ch)
130 matched = TRUE;
131 }
132 else if (ch == '-' && prev && p[1] && p[1] != ']') {
133 ch = *++p;
134 if (ch == '\\') {
135 ch = *++p;
136 if (!ch)
5bb92000 137 return ABORT_ALL;
e11c4251
WD
138 }
139 if (*text <= ch && *text >= prev)
140 matched = TRUE;
141 ch = 0; /* This makes "prev" get set to 0. */
142 }
143 else if (ch == '[' && p[1] == ':') {
5f238db2 144 const uchar *s;
e11c4251 145 int i;
5f238db2 146 for (s = p += 2; (ch = *p) && ch != ']'; p++) {}
e11c4251 147 if (!ch)
5bb92000 148 return ABORT_ALL;
fc96552d
WD
149 i = p - s - 1;
150 if (i < 0 || p[-1] != ':') {
151 /* Didn't find ":]", so treat like a normal set. */
152 p = s - 2;
153 ch = '[';
154 if (*text == ch)
155 matched = TRUE;
156 continue;
157 }
5bb92000
WD
158 if (CC_EQ(s,i, "alnum")) {
159 if (ISALNUM(*text))
160 matched = TRUE;
161 }
162 else if (CC_EQ(s,i, "alpha")) {
163 if (ISALPHA(*text))
164 matched = TRUE;
165 }
166 else if (CC_EQ(s,i, "blank")) {
167 if (ISBLANK(*text))
168 matched = TRUE;
169 }
170 else if (CC_EQ(s,i, "cntrl")) {
171 if (ISCNTRL(*text))
172 matched = TRUE;
173 }
174 else if (CC_EQ(s,i, "digit")) {
175 if (ISDIGIT(*text))
176 matched = TRUE;
177 }
178 else if (CC_EQ(s,i, "graph")) {
179 if (ISGRAPH(*text))
180 matched = TRUE;
181 }
182 else if (CC_EQ(s,i, "lower")) {
183 if (ISLOWER(*text))
184 matched = TRUE;
185 }
186 else if (CC_EQ(s,i, "print")) {
187 if (ISPRINT(*text))
188 matched = TRUE;
189 }
190 else if (CC_EQ(s,i, "punct")) {
191 if (ISPUNCT(*text))
192 matched = TRUE;
193 }
194 else if (CC_EQ(s,i, "space")) {
195 if (ISSPACE(*text))
196 matched = TRUE;
197 }
198 else if (CC_EQ(s,i, "upper")) {
199 if (ISUPPER(*text))
200 matched = TRUE;
201 }
202 else if (CC_EQ(s,i, "xdigit")) {
203 if (ISXDIGIT(*text))
204 matched = TRUE;
205 }
206 else /* malformed [:class:] string */
207 return ABORT_ALL;
7a1f46b6 208 ch = 0; /* This makes "prev" get set to 0. */
446ee5b1
WD
209 }
210 else if (*text == ch)
710faea9 211 matched = TRUE;
c9a59880 212 } while (prev = ch, (ch = *++p) != ']');
d811b689 213 if (matched == special || *text == '/')
710faea9 214 return FALSE;
446ee5b1
WD
215 continue;
216 }
217 }
218
219 return *text == '\0';
220}
710faea9 221
e11c4251
WD
222/* Find the pattern (p) in the text string (t). */
223int wildmatch(const char *p, const char *t)
710faea9 224{
d5c973cc
WD
225#ifdef WILD_TEST_ITERATIONS
226 wildmatch_iteration_count = 0;
20b2e9ce 227#endif
5f238db2 228 return domatch((const uchar*)p, (const uchar*)t) == TRUE;
710faea9 229}