wildmatch: remove static variable force_lower_case
[git/git.git] / wildmatch.c
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.
10 */
11
12 #include "cache.h"
13 #include "wildmatch.h"
14
15 typedef unsigned char uchar;
16
17 /* What character marks an inverted character class? */
18 #define NEGATE_CLASS '!'
19 #define NEGATE_CLASS2 '^'
20
21 #define FALSE 0
22 #define TRUE 1
23
24 #define NOMATCH 1
25 #define MATCH 0
26 #define ABORT_ALL -1
27 #define ABORT_TO_STARSTAR -2
28
29 #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
30 && *(class) == *(litmatch) \
31 && strncmp((char*)class, litmatch, len) == 0)
32
33 #if defined STDC_HEADERS || !defined isascii
34 # define ISASCII(c) 1
35 #else
36 # define ISASCII(c) isascii(c)
37 #endif
38
39 #ifdef isblank
40 # define ISBLANK(c) (ISASCII(c) && isblank(c))
41 #else
42 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
43 #endif
44
45 #ifdef isgraph
46 # define ISGRAPH(c) (ISASCII(c) && isgraph(c))
47 #else
48 # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
49 #endif
50
51 #define ISPRINT(c) (ISASCII(c) && isprint(c))
52 #define ISDIGIT(c) (ISASCII(c) && isdigit(c))
53 #define ISALNUM(c) (ISASCII(c) && isalnum(c))
54 #define ISALPHA(c) (ISASCII(c) && isalpha(c))
55 #define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
56 #define ISLOWER(c) (ISASCII(c) && islower(c))
57 #define ISPUNCT(c) (ISASCII(c) && ispunct(c))
58 #define ISSPACE(c) (ISASCII(c) && isspace(c))
59 #define ISUPPER(c) (ISASCII(c) && isupper(c))
60 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
61
62 /* Match pattern "p" against "text" */
63 static int dowild(const uchar *p, const uchar *text, int force_lower_case)
64 {
65 uchar p_ch;
66
67 for ( ; (p_ch = *p) != '\0'; text++, p++) {
68 int matched, special;
69 uchar t_ch, prev_ch;
70 if ((t_ch = *text) == '\0' && p_ch != '*')
71 return ABORT_ALL;
72 if (force_lower_case && ISUPPER(t_ch))
73 t_ch = tolower(t_ch);
74 switch (p_ch) {
75 case '\\':
76 /* Literal match with following character. Note that the test
77 * in "default" handles the p[1] == '\0' failure case. */
78 p_ch = *++p;
79 /* FALLTHROUGH */
80 default:
81 if (t_ch != p_ch)
82 return NOMATCH;
83 continue;
84 case '?':
85 /* Match anything but '/'. */
86 if (t_ch == '/')
87 return NOMATCH;
88 continue;
89 case '*':
90 if (*++p == '*') {
91 while (*++p == '*') {}
92 special = TRUE;
93 } else
94 special = FALSE;
95 if (*p == '\0') {
96 /* Trailing "**" matches everything. Trailing "*" matches
97 * only if there are no more slash characters. */
98 if (!special) {
99 if (strchr((char*)text, '/') != NULL)
100 return NOMATCH;
101 }
102 return MATCH;
103 }
104 while (1) {
105 if (t_ch == '\0')
106 break;
107 if ((matched = dowild(p, text, force_lower_case)) != NOMATCH) {
108 if (!special || matched != ABORT_TO_STARSTAR)
109 return matched;
110 } else if (!special && t_ch == '/')
111 return ABORT_TO_STARSTAR;
112 t_ch = *++text;
113 }
114 return ABORT_ALL;
115 case '[':
116 p_ch = *++p;
117 #ifdef NEGATE_CLASS2
118 if (p_ch == NEGATE_CLASS2)
119 p_ch = NEGATE_CLASS;
120 #endif
121 /* Assign literal TRUE/FALSE because of "matched" comparison. */
122 special = p_ch == NEGATE_CLASS? TRUE : FALSE;
123 if (special) {
124 /* Inverted character class. */
125 p_ch = *++p;
126 }
127 prev_ch = 0;
128 matched = FALSE;
129 do {
130 if (!p_ch)
131 return ABORT_ALL;
132 if (p_ch == '\\') {
133 p_ch = *++p;
134 if (!p_ch)
135 return ABORT_ALL;
136 if (t_ch == p_ch)
137 matched = TRUE;
138 } else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') {
139 p_ch = *++p;
140 if (p_ch == '\\') {
141 p_ch = *++p;
142 if (!p_ch)
143 return ABORT_ALL;
144 }
145 if (t_ch <= p_ch && t_ch >= prev_ch)
146 matched = TRUE;
147 p_ch = 0; /* This makes "prev_ch" get set to 0. */
148 } else if (p_ch == '[' && p[1] == ':') {
149 const uchar *s;
150 int i;
151 for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
152 if (!p_ch)
153 return ABORT_ALL;
154 i = p - s - 1;
155 if (i < 0 || p[-1] != ':') {
156 /* Didn't find ":]", so treat like a normal set. */
157 p = s - 2;
158 p_ch = '[';
159 if (t_ch == p_ch)
160 matched = TRUE;
161 continue;
162 }
163 if (CC_EQ(s,i, "alnum")) {
164 if (ISALNUM(t_ch))
165 matched = TRUE;
166 } else if (CC_EQ(s,i, "alpha")) {
167 if (ISALPHA(t_ch))
168 matched = TRUE;
169 } else if (CC_EQ(s,i, "blank")) {
170 if (ISBLANK(t_ch))
171 matched = TRUE;
172 } else if (CC_EQ(s,i, "cntrl")) {
173 if (ISCNTRL(t_ch))
174 matched = TRUE;
175 } else if (CC_EQ(s,i, "digit")) {
176 if (ISDIGIT(t_ch))
177 matched = TRUE;
178 } else if (CC_EQ(s,i, "graph")) {
179 if (ISGRAPH(t_ch))
180 matched = TRUE;
181 } else if (CC_EQ(s,i, "lower")) {
182 if (ISLOWER(t_ch))
183 matched = TRUE;
184 } else if (CC_EQ(s,i, "print")) {
185 if (ISPRINT(t_ch))
186 matched = TRUE;
187 } else if (CC_EQ(s,i, "punct")) {
188 if (ISPUNCT(t_ch))
189 matched = TRUE;
190 } else if (CC_EQ(s,i, "space")) {
191 if (ISSPACE(t_ch))
192 matched = TRUE;
193 } else if (CC_EQ(s,i, "upper")) {
194 if (ISUPPER(t_ch))
195 matched = TRUE;
196 } else if (CC_EQ(s,i, "xdigit")) {
197 if (ISXDIGIT(t_ch))
198 matched = TRUE;
199 } else /* malformed [:class:] string */
200 return ABORT_ALL;
201 p_ch = 0; /* This makes "prev_ch" get set to 0. */
202 } else if (t_ch == p_ch)
203 matched = TRUE;
204 } while (prev_ch = p_ch, (p_ch = *++p) != ']');
205 if (matched == special || t_ch == '/')
206 return NOMATCH;
207 continue;
208 }
209 }
210
211 return *text ? NOMATCH : MATCH;
212 }
213
214 /* Match the "pattern" against the "text" string. */
215 int wildmatch(const char *pattern, const char *text, int flags)
216 {
217 return dowild((const uchar*)pattern, (const uchar*)text,
218 flags & FNM_CASEFOLD ? 1 :0);
219 }