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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* D. G. Korn
* G. S. Fowler
* AT&T Research
*
* match shell file patterns -- derived from Bourne and Korn shell gmatch()
*
* sh pattern egrep RE description
* ---------- -------- -----------
* * .* 0 or more chars
* ? . any single char
* [.] [.] char class
* [!.] [^.] negated char class
* [[:.:]] [[:.:]] ctype class
* [[=.=]] [[=.=]] equivalence class
* [[...]] [[...]] collation element
* *(.) (.)* 0 or more of
* +(.) (.)+ 1 or more of
* ?(.) (.)? 0 or 1 of
* (.) (.) 1 of
* @(.) (.) 1 of
* a|b a|b a or b
* \# () subgroup back reference [1-9]
* a&b a and b
* !(.) none of
*
* \ used to escape metacharacters
*
* *, ?, (, |, &, ), [, \ must be \'d outside of [...]
* only ] must be \'d inside [...]
*
* BUG: unbalanced ) terminates top level pattern
*/
#include <ast.h>
#include <ctype.h>
#include <hashkey.h>
#ifndef isblank
#define isblank(x) ((x)==' '||(x)=='\t')
#endif
#ifndef isgraph
#define isgraph(x) (isprint(x)&&!isblank(x))
#endif
#define MAXGROUP 10
typedef struct
{
char* beg[MAXGROUP];
char* end[MAXGROUP];
char* next_s;
short groups;
} Group_t;
typedef struct
{
Group_t current;
Group_t best;
char* last_s;
char* next_p;
} Match_t;
#define mbgetchar(p) (*p++)
#ifndef isxdigit
#define isxdigit(c) ((c)>='0'&&(c)<='9'||(c)>='a'&&(c)<='f'||(c)>='A'&&(c)<='F')
#endif
#define getsource(s,e) (((s)>=(e))?0:mbgetchar(s))
#define COLL_MAX 3
/*
* gobble chars up to <sub> or ) keeping track of (...) and [...]
* sub must be one of { '|', '&', 0 }
* 0 returned if s runs out
*/
static char*
gobble(Match_t* mp, register char* s, register int sub, int* g, int clear)
{
register int p = 0;
register char* b = 0;
int c = 0;
int n;
for (;;)
switch (mbgetchar(s))
{
case '\\':
if (mbgetchar(s))
break;
/*FALLTHROUGH*/
case 0:
return 0;
case '[':
if (!b)
{
if (*s == '!')
mbgetchar(s);
b = s;
}
else if (*s == '.' || *s == '=' || *s == ':')
c = *s;
break;
case ']':
if (b)
{
if (*(s - 2) == c)
c = 0;
else if (b != (s - 1))
b = 0;
}
break;
case '(':
if (!b)
{
p++;
n = (*g)++;
if (clear)
{
if (!sub)
n++;
if (n < MAXGROUP)
mp->current.beg[n] = mp->current.end[n] = 0;
}
}
break;
case ')':
if (!b && p-- <= 0)
return sub ? 0 : s;
break;
case '|':
if (!b && !p && sub == '|')
return s;
break;
}
}
static int grpmatch(Match_t*, int, char*, register char*, char*, int);
/*
* match a single pattern
* e is the end (0) of the substring in s
* r marks the start of a repeated subgroup pattern
*/
static int
onematch(Match_t* mp, int g, char* s, char* p, char* e, char* r, int flags)
{
register int pc;
register int sc;
register int n;
register int icase;
char* olds;
char* oldp;
icase = flags & STR_ICASE;
do
{
olds = s;
sc = getsource(s, e);
if (icase && isupper(sc))
sc = tolower(sc);
oldp = p;
switch (pc = mbgetchar(p))
{
case '(':
case '*':
case '?':
case '+':
case '@':
case '!':
if (pc == '(' || *p == '(')
{
char* subp;
int oldg;
s = olds;
subp = p + (pc != '(');
oldg = g;
n = ++g;
if (g < MAXGROUP && (!r || g > mp->current.groups))
mp->current.beg[g] = mp->current.end[g] = 0;
if (!(p = gobble(mp, subp, 0, &g, !r)))
return 0;
if (pc == '*' || pc == '?' || pc == '+' && oldp == r)
{
if (onematch(mp, g, s, p, e, NiL, flags))
return 1;
if (!sc || !getsource(s, e))
{
mp->current.groups = oldg;
return 0;
}
}
if (pc == '*' || pc == '+')
{
p = oldp;
sc = n - 1;
}
else
sc = g;
pc = (pc != '!');
do
{
if (grpmatch(mp, n, olds, subp, s, flags) == pc)
{
if (n < MAXGROUP)
{
if (!mp->current.beg[n] || mp->current.beg[n] > olds)
mp->current.beg[n] = olds;
if (s > mp->current.end[n])
mp->current.end[n] = s;
}
if (onematch(mp, sc, s, p, e, oldp, flags))
{
if (p == oldp && n < MAXGROUP)
{
if (!mp->current.beg[n] || mp->current.beg[n] > olds)
mp->current.beg[n] = olds;
if (s > mp->current.end[n])
mp->current.end[n] = s;
}
return 1;
}
}
} while (s < e && mbgetchar(s));
mp->current.groups = oldg;
return 0;
}
else if (pc == '*')
{
/*
* several stars are the same as one
*/
while (*p == '*' && *(p + 1) != '(')
p++;
oldp = p;
switch (pc = mbgetchar(p))
{
case '@':
case '!':
case '+':
n = *p == '(';
break;
case '(':
case '[':
case '?':
case '*':
n = 1;
break;
case 0:
case '|':
case '&':
case ')':
mp->current.next_s = (flags & STR_MAXIMAL) ? e : olds;
mp->next_p = oldp;
mp->current.groups = g;
if (!pc && (!mp->best.next_s || (flags & STR_MAXIMAL) && mp->current.next_s > mp->best.next_s || !(flags & STR_MAXIMAL) && mp->current.next_s < mp->best.next_s))
mp->best = mp->current;
return 1;
case '\\':
if (!(pc = mbgetchar(p)))
return 0;
if (pc >= '0' && pc <= '9')
{
n = pc - '0';
if (n <= g && mp->current.beg[n])
pc = *mp->current.beg[n];
}
/*FALLTHROUGH*/
default:
if (icase && isupper(pc))
pc = tolower(pc);
n = 0;
break;
}
p = oldp;
for (;;)
{
if ((n || pc == sc) && onematch(mp, g, olds, p, e, NiL, flags))
return 1;
if (!sc)
return 0;
olds = s;
sc = getsource(s, e);
if ((flags & STR_ICASE) && isupper(sc))
sc = tolower(sc);
}
}
else if (pc != '?' && pc != sc)
return 0;
break;
case 0:
if (!(flags & STR_MAXIMAL))
sc = 0;
/*FALLTHROUGH*/
case '|':
case '&':
case ')':
if (!sc)
{
mp->current.next_s = olds;
mp->next_p = oldp;
mp->current.groups = g;
}
if (!pc && (!mp->best.next_s || (flags & STR_MAXIMAL) && olds > mp->best.next_s || !(flags & STR_MAXIMAL) && olds < mp->best.next_s))
{
mp->best = mp->current;
mp->best.next_s = olds;
mp->best.groups = g;
}
return !sc;
case '[':
{
/*UNDENT...*/
int invert;
int x;
int ok = 0;
char* range;
if (!sc)
return 0;
range = 0;
n = 0;
if (invert = *p == '!')
p++;
for (;;)
{
oldp = p;
if (!(pc = mbgetchar(p)))
return 0;
else if (pc == '[' && (*p == ':' || *p == '=' || *p == '.'))
{
x = 0;
n = mbgetchar(p);
oldp = p;
for (;;)
{
if (!(pc = mbgetchar(p)))
return 0;
if (pc == n && *p == ']')
break;
x++;
}
mbgetchar(p);
if (ok)
/*NOP*/;
else if (n == ':')
{
switch (HASHNKEY5(x, oldp[0], oldp[1], oldp[2], oldp[3], oldp[4]))
{
case HASHNKEY5(5,'a','l','n','u','m'):
if (isalnum(sc))
ok = 1;
break;
case HASHNKEY5(5,'a','l','p','h','a'):
if (isalpha(sc))
ok = 1;
break;
case HASHNKEY5(5,'b','l','a','n','k'):
if (isblank(sc))
ok = 1;
break;
case HASHNKEY5(5,'c','n','t','r','l'):
if (iscntrl(sc))
ok = 1;
break;
case HASHNKEY5(5,'d','i','g','i','t'):
if (isdigit(sc))
ok = 1;
break;
case HASHNKEY5(5,'g','r','a','p','h'):
if (isgraph(sc))
ok = 1;
break;
case HASHNKEY5(5,'l','o','w','e','r'):
if (islower(sc))
ok = 1;
break;
case HASHNKEY5(5,'p','r','i','n','t'):
if (isprint(sc))
ok = 1;
break;
case HASHNKEY5(5,'p','u','n','c','t'):
if (ispunct(sc))
ok = 1;
break;
case HASHNKEY5(5,'s','p','a','c','e'):
if (isspace(sc))
ok = 1;
break;
case HASHNKEY5(5,'u','p','p','e','r'):
if (icase ? islower(sc) : isupper(sc))
ok = 1;
break;
case HASHNKEY5(6,'x','d','i','g','i'):
if (oldp[5] == 't' && isxdigit(sc))
ok = 1;
break;
}
}
else if (range)
goto getrange;
else if (*p == '-' && *(p + 1) != ']')
{
mbgetchar(p);
range = oldp;
}
else if (isalpha(*oldp) && isalpha(*olds) && tolower(*oldp) == tolower(*olds) || sc == mbgetchar(oldp))
ok = 1;
n = 1;
}
else if (pc == ']' && n)
{
if (ok != invert)
break;
return 0;
}
else if (pc == '\\' && (oldp = p, !(pc = mbgetchar(p))))
return 0;
else if (ok)
/*NOP*/;
else if (range)
{
getrange:
if (icase && isupper(pc))
pc = tolower(pc);
x = mbgetchar(range);
if (icase && isupper(x))
x = tolower(x);
if (sc == x || sc == pc || sc > x && sc < pc)
ok = 1;
if (*p == '-' && *(p + 1) != ']')
{
mbgetchar(p);
range = oldp;
}
else
range = 0;
n = 1;
}
else if (*p == '-' && *(p + 1) != ']')
{
mbgetchar(p);
range = oldp;
n = 1;
}
else
{
if (icase && isupper(pc))
pc = tolower(pc);
if (sc == pc)
ok = 1;
n = pc;
}
}
/*...INDENT*/
}
break;
case '\\':
if (!(pc = mbgetchar(p)))
return 0;
if (pc >= '0' && pc <= '9')
{
n = pc - '0';
if (n <= g && (oldp = mp->current.beg[n]))
{
while (oldp < mp->current.end[n])
if (!*olds || *olds++ != *oldp++)
return 0;
s = olds;
break;
}
}
/*FALLTHROUGH*/
default:
if (icase && isupper(pc))
pc = tolower(pc);
if (pc != sc)
return 0;
break;
}
} while (sc);
return 0;
}
/*
* match any pattern in a group
* | and & subgroups are parsed here
*/
static int
grpmatch(Match_t* mp, int g, char* s, register char* p, char* e, int flags)
{
register char* a;
do
{
for (a = p; onematch(mp, g, s, a, e, NiL, flags); a++)
if (*(a = mp->next_p) != '&')
return 1;
} while (p = gobble(mp, p, '|', &g, 1));
return 0;
}
/*
* subgroup match
* 0 returned if no match
* otherwise number of subgroups matched returned
* match group begin offsets are even elements of sub
* match group end offsets are odd elements of sub
* the matched string is from s+sub[0] up to but not
* including s+sub[1]
*/
int
strgrpmatch(const char* b, const char* p, int* sub, int n, int flags)
{
register int i;
register char* s;
char* e;
Match_t match;
s = (char*)b;
match.last_s = e = s + strlen(s);
for (;;)
{
match.best.next_s = 0;
match.current.groups = 0;
if ((i = grpmatch(&match, 0, s, (char*)p, e, flags)) || match.best.next_s)
{
if (!i)
match.current = match.best;
match.current.groups++;
match.current.end[0] = match.current.next_s;
break;
}
if ((flags & STR_LEFT) || s >= e)
return 0;
s++;
}
if ((flags & STR_RIGHT) && match.current.next_s != e)
return 0;
if (!sub)
return 1;
match.current.beg[0] = s;
s = (char*)b;
if (n > match.current.groups)
n = match.current.groups;
for (i = 0; i < n; i++)
{
sub[i * 2] = match.current.end[i] ? match.current.beg[i] - s : 0;
sub[i * 2 + 1] = match.current.end[i] ? match.current.end[i] - s : 0;
}
return n;
}
/*
* compare the string s with the shell pattern p
* returns 1 for match 0 otherwise
*/
int
strmatch(const char* s, const char* p)
{
return strgrpmatch(s, p, NiL, 0, STR_MAXIMAL|STR_LEFT|STR_RIGHT);
}
|