1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/* $OpenBSD: rmatch.c,v 1.3 2022/12/26 19:16:02 jmc Exp $ */
/*
* Copyright (c) 2021 Claudio Jeker <claudio@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Guido van Rossum.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "charclass.h"
#include "extern.h"
#define RANGE_MATCH 1
#define RANGE_NOMATCH 0
#define RANGE_ERROR (-1)
static int
classmatch(const char *pattern, char test, const char **ep)
{
const char *mismatch = pattern;
const struct cclass *cc;
const char *colon;
size_t len;
int rval = RANGE_NOMATCH;
if (*pattern++ != ':') {
*ep = mismatch;
return RANGE_ERROR;
}
if ((colon = strchr(pattern, ':')) == NULL || colon[1] != ']') {
*ep = mismatch;
return RANGE_ERROR;
}
*ep = colon + 2;
len = (size_t)(colon - pattern);
for (cc = cclasses; cc->name != NULL; cc++) {
if (!strncmp(pattern, cc->name, len) && cc->name[len] == '\0') {
if (cc->isctype((unsigned char)test))
rval = RANGE_MATCH;
return rval;
}
}
/* invalid character class, treat as normal text */
*ep = mismatch;
return RANGE_ERROR;
}
static int
rangematch(const char **pp, char test)
{
const char *pattern = *pp;
int negate, ok;
char c, c2;
/*
* A bracket expression starting with an unquoted circumflex
* character produces unspecified results (IEEE 1003.2-1992,
* 3.13.2). This implementation treats it like '!', for
* consistency with the regular expression syntax.
* J.T. Conklin (conklin@ngai.kaleida.com)
*/
if ((negate = (*pattern == '!' || *pattern == '^')))
++pattern;
/*
* A right bracket shall lose its special meaning and represent
* itself in a bracket expression if it occurs first in the list.
* -- POSIX.2 2.8.3.2
*/
ok = 0;
c = *pattern++;
do {
if (c == '[') {
switch (classmatch(pattern, test, &pattern)) {
case RANGE_MATCH:
ok = 1;
continue;
case RANGE_NOMATCH:
continue;
default:
/* invalid character class, treat literally. */
break;
}
}
if (c == '\\')
c = *pattern++;
if (c == '\0')
return RANGE_ERROR;
/* patterns can not match on '/' */
if (c == '/')
return RANGE_NOMATCH;
if (*pattern == '-'
&& (c2 = *(pattern + 1)) != '\0' && c2 != ']') {
pattern += 2;
if (c2 == '\\')
c2 = *pattern++;
if (c2 == '\0')
return RANGE_ERROR;
if (c <= test && test <= c2)
ok = 1;
} else if (c == test)
ok = 1;
} while ((c = *pattern++) != ']');
*pp = pattern;
return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
}
/*
* Single character match, advances pattern as much as needed.
* Return 0 on match and !0 (aka 1) on mismatch.
* When matched pp is advanced to the end of the pattern matched.
*/
static int
matchchar(const char **pp, const char in)
{
const char *pattern = *pp;
char c;
int rv = 0;
switch (c = *pattern++) {
case '?':
if (in == '\0')
rv = 1;
if (in == '/')
rv = 1;
break;
case '[':
if (in == '\0')
rv = 1;
if (in == '/')
rv = 1;
if (rv == 1)
break;
switch (rangematch(&pattern, in)) {
case RANGE_ERROR:
/* not a good range, treat as normal text */
goto normal;
case RANGE_MATCH:
break;
case RANGE_NOMATCH:
rv = 1;
}
break;
case '\\':
if ((c = *pattern++) == '\0') {
c = '\\';
--pattern;
}
/* FALLTHROUGH */
default:
normal:
if (c != in)
rv = 1;
break;
}
*pp = pattern;
return rv;
}
/*
* Do a substring match. If wild is set then the pattern started with a '*'.
* The match will go until '*', '/' or '\0' is encountered in pattern or
* the input string is consumed up to end.
* The pattern and string handles pp and ss are updated only on success.
*/
static int
matchsub(const char **pp, const char **ss, const char *end, int wild)
{
const char *pattern = *pp;
const char *p = pattern;
const char *string = *ss;
size_t matchlen;
/* first calculate how many characters the submatch will consume */
for (matchlen = 0; *p != '\0'; matchlen++) {
if (p[0] == '*')
break;
/* '/' acts as barrier */
if (p[0] == '/' || (p[0] == '\\' && p[1] == '/')) {
if (wild) {
/* match needs to match up to end of segment */
if (string > end - matchlen)
return 1;
string = end - matchlen;
wild = 0;
}
break;
}
/*
* skip forward one character in pattern by doing a
* dummy lookup.
*/
matchchar(&p, ' ');
}
/* not enough char to match */
if (string > end - matchlen)
return 1;
if (*p == '\0') {
if (wild) {
/* match needs to match up to end of segment */
string = end - matchlen;
wild = 0;
}
}
while (*pattern != '\0' && *pattern != '*') {
/* eat possible escape char before '/' */
if (pattern[0] == '\\' && pattern[1] == '/')
pattern++;
if (pattern[0] == '/')
break;
/* check if there are still characters available to compare */
if (string >= end)
return 1;
/* Compare one char at a time. */
if (!matchchar(&pattern, *string++))
continue;
if (wild) {
/* skip forward one char and restart match */
string = ++*ss;
pattern = *pp;
/* can it still match? */
if (string > end - matchlen)
return 1;
} else {
/* failed match */
return 1;
}
}
*pp = pattern;
*ss = string;
return 0;
}
/*
* File matching with the addition of the special '**'.
* Returns 0 on match and !0 for strings that do not match pattern.
*/
int
rmatch(const char *pattern, const char *string, int leading_dir)
{
const char *segend, *segnext, *mismatch = NULL;
int wild, starstar;
while (*pattern && *string) {
/* handle leading '/' first */
if (pattern[0] == '\\' && pattern[1] == '/')
pattern++;
if (*string == '/' && *pattern == '/') {
string++;
pattern++;
}
/* match to the next '/' in string */
segend = strchr(string, '/');
if (segend == NULL)
segend = strchr(string, '\0');
while (*pattern) {
/*
* Check for '*' and '**'. For '*' reduce '*' and '?'
* sequences into n-'?' and trailing '*'.
* For '**' this optimisation can not be done
* since '**???/' will match 'a/aa/aaa/' but not
* 'a/aa/aa/' still additional '*' will be reduced.
*/
wild = 0;
starstar = 0;
for ( ; *pattern == '*' || *pattern == '?'; pattern++) {
if (pattern[0] == '*') {
if (pattern[1] == '*') {
starstar = 1;
pattern++;
}
wild = 1;
} else if (!starstar) { /* pattern[0] == '?' */
if (string < segend && *string != '/')
string++;
else
/* no match possible */
return 1;
} else
break;
}
/* pattern ends in '**' so it is a match */
if (starstar && *pattern == '\0')
return 0;
if (starstar) {
segnext = segend;
mismatch = pattern;
}
while (string < segend) {
if (matchsub(&pattern, &string, segend, wild)) {
failed_match:
/*
* failed to match, if starstar retry
* with the next segment.
*/
if (mismatch) {
pattern = mismatch;
wild = 1;
string = segnext;
if (*string == '/')
string++;
segend = strchr(string, '/');
if (!segend)
segend = strchr(string,
'\0');
segnext = segend;
if (string < segend)
continue;
}
/* no match possible */
return 1;
}
break;
}
/* at end of string segment, eat up any extra '*' */
if (string >= segend && *pattern != '*')
break;
}
if (*string != '\0' && *string != '/')
goto failed_match;
if (*pattern != '\0' && *pattern != '/')
goto failed_match;
}
/* if both pattern and string are consumed it was a match */
if (*pattern == '\0' && *string == '\0')
return 0;
/* if leading_dir is set then string can also be '/' for success */
if (leading_dir && *pattern == '\0' && *string == '/')
return 0;
/* else failure */
return 1;
}
|