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