1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin
24da2e3ebdSchin /*
25da2e3ebdSchin * D. G. Korn
26da2e3ebdSchin * G. S. Fowler
27da2e3ebdSchin * AT&T Research
28da2e3ebdSchin *
29da2e3ebdSchin * match shell file patterns
30da2e3ebdSchin * this interface is a wrapper on regex
31da2e3ebdSchin *
32da2e3ebdSchin * sh pattern egrep RE description
33da2e3ebdSchin * ---------- -------- -----------
34da2e3ebdSchin * * .* 0 or more chars
35da2e3ebdSchin * ? . any single char
36da2e3ebdSchin * [.] [.] char class
37da2e3ebdSchin * [!.] [^.] negated char class
38da2e3ebdSchin * [[:.:]] [[:.:]] ctype class
39da2e3ebdSchin * [[=.=]] [[=.=]] equivalence class
40da2e3ebdSchin * [[...]] [[...]] collation element
41da2e3ebdSchin * *(.) (.)* 0 or more of
42da2e3ebdSchin * +(.) (.)+ 1 or more of
43da2e3ebdSchin * ?(.) (.)? 0 or 1 of
44da2e3ebdSchin * (.) (.) 1 of
45da2e3ebdSchin * @(.) (.) 1 of
46da2e3ebdSchin * a|b a|b a or b
47da2e3ebdSchin * \# () subgroup back reference [1-9]
48da2e3ebdSchin * a&b a and b
49da2e3ebdSchin * !(.) none of
50da2e3ebdSchin *
51da2e3ebdSchin * \ used to escape metacharacters
52da2e3ebdSchin *
53da2e3ebdSchin * *, ?, (, |, &, ), [, \ must be \'d outside of [...]
54da2e3ebdSchin * only ] must be \'d inside [...]
55da2e3ebdSchin *
56da2e3ebdSchin */
57da2e3ebdSchin
58da2e3ebdSchin #include <ast.h>
59da2e3ebdSchin #include <regex.h>
60da2e3ebdSchin
61da2e3ebdSchin static struct State_s
62da2e3ebdSchin {
63da2e3ebdSchin regmatch_t* match;
64da2e3ebdSchin int nmatch;
65da2e3ebdSchin } matchstate;
66da2e3ebdSchin
67da2e3ebdSchin /*
68da2e3ebdSchin * subgroup match
69da2e3ebdSchin * 0 returned if no match
70da2e3ebdSchin * otherwise number of subgroups matched returned
71da2e3ebdSchin * match group begin offsets are even elements of sub
72da2e3ebdSchin * match group end offsets are odd elements of sub
73da2e3ebdSchin * the matched string is from s+sub[0] up to but not
74da2e3ebdSchin * including s+sub[1]
75da2e3ebdSchin */
76da2e3ebdSchin
77da2e3ebdSchin int
strgrpmatch(const char * b,const char * p,int * sub,int n,register int flags)78da2e3ebdSchin strgrpmatch(const char* b, const char* p, int* sub, int n, register int flags)
79da2e3ebdSchin {
80da2e3ebdSchin register regex_t* re;
81da2e3ebdSchin register int* end;
82da2e3ebdSchin register int i;
83da2e3ebdSchin register regflags_t reflags;
84da2e3ebdSchin
85da2e3ebdSchin /*
86da2e3ebdSchin * 0 and empty patterns are special
87da2e3ebdSchin */
88da2e3ebdSchin
89da2e3ebdSchin if (!p || !b)
90da2e3ebdSchin {
91da2e3ebdSchin if (!p && !b)
92da2e3ebdSchin regcache(NiL, 0, NiL);
93da2e3ebdSchin return 0;
94da2e3ebdSchin }
95da2e3ebdSchin if (!*p)
967c2fbfb3SApril Chin {
977c2fbfb3SApril Chin if (sub && n > 0)
987c2fbfb3SApril Chin sub[0] = sub[1] = 0;
99da2e3ebdSchin return *b == 0;
1007c2fbfb3SApril Chin }
101da2e3ebdSchin
102da2e3ebdSchin /*
103da2e3ebdSchin * convert flags
104da2e3ebdSchin */
105da2e3ebdSchin
106da2e3ebdSchin if (flags & REG_ADVANCE)
107da2e3ebdSchin reflags = flags & ~REG_ADVANCE;
108da2e3ebdSchin else
109da2e3ebdSchin {
110da2e3ebdSchin reflags = REG_SHELL|REG_AUGMENTED;
111da2e3ebdSchin if (!(flags & STR_MAXIMAL))
112da2e3ebdSchin reflags |= REG_MINIMAL;
113da2e3ebdSchin if (flags & STR_GROUP)
114da2e3ebdSchin reflags |= REG_SHELL_GROUP;
115da2e3ebdSchin if (flags & STR_LEFT)
116da2e3ebdSchin reflags |= REG_LEFT;
117da2e3ebdSchin if (flags & STR_RIGHT)
118da2e3ebdSchin reflags |= REG_RIGHT;
119da2e3ebdSchin if (flags & STR_ICASE)
120da2e3ebdSchin reflags |= REG_ICASE;
121da2e3ebdSchin }
122da2e3ebdSchin if (!sub || n <= 0)
123da2e3ebdSchin reflags |= REG_NOSUB;
124da2e3ebdSchin if (!(re = regcache(p, reflags, NiL)))
125da2e3ebdSchin return 0;
126da2e3ebdSchin if (n > matchstate.nmatch)
127da2e3ebdSchin {
128da2e3ebdSchin if (!(matchstate.match = newof(matchstate.match, regmatch_t, n, 0)))
129da2e3ebdSchin return 0;
130da2e3ebdSchin matchstate.nmatch = n;
131da2e3ebdSchin }
132da2e3ebdSchin if (regexec(re, b, n, matchstate.match, reflags & ~(REG_MINIMAL|REG_SHELL_GROUP|REG_LEFT|REG_RIGHT|REG_ICASE)))
133da2e3ebdSchin return 0;
134da2e3ebdSchin if (!sub || n <= 0)
135da2e3ebdSchin return 1;
136da2e3ebdSchin i = re->re_nsub;
137da2e3ebdSchin end = sub + n * 2;
138da2e3ebdSchin for (n = 0; sub < end && n <= i; n++)
139da2e3ebdSchin {
140da2e3ebdSchin *sub++ = matchstate.match[n].rm_so;
141da2e3ebdSchin *sub++ = matchstate.match[n].rm_eo;
142da2e3ebdSchin }
143da2e3ebdSchin return i + 1;
144da2e3ebdSchin }
145da2e3ebdSchin
146da2e3ebdSchin /*
147da2e3ebdSchin * compare the string s with the shell pattern p
148da2e3ebdSchin * returns 1 for match 0 otherwise
149da2e3ebdSchin */
150da2e3ebdSchin
151da2e3ebdSchin int
strmatch(const char * s,const char * p)152da2e3ebdSchin strmatch(const char* s, const char* p)
153da2e3ebdSchin {
154da2e3ebdSchin return strgrpmatch(s, p, NiL, 0, STR_MAXIMAL|STR_LEFT|STR_RIGHT);
155da2e3ebdSchin }
156da2e3ebdSchin
157da2e3ebdSchin /*
158da2e3ebdSchin * leading substring match
159da2e3ebdSchin * first char after end of substring returned
160da2e3ebdSchin * 0 returned if no match
161da2e3ebdSchin *
162da2e3ebdSchin * OBSOLETE: use strgrpmatch()
163da2e3ebdSchin */
164da2e3ebdSchin
165da2e3ebdSchin char*
strsubmatch(const char * s,const char * p,int flags)166da2e3ebdSchin strsubmatch(const char* s, const char* p, int flags)
167da2e3ebdSchin {
168da2e3ebdSchin int match[2];
169da2e3ebdSchin
170da2e3ebdSchin return strgrpmatch(s, p, match, 1, (flags ? STR_MAXIMAL : 0)|STR_LEFT) ? (char*)s + match[1] : (char*)0;
171da2e3ebdSchin }
172