2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * regex.c -- regular expression patter matching functions
15 #include <afsconfig.h>
16 #include <afs/param.h>
22 * routines to do regular expression matching
28 * ... returns 0 if the string s was compiled successfully,
29 * a pointer to an error message otherwise.
30 * If passed 0 or a null string returns without changing
31 * the currently compiled re (see note 11 below).
35 * ... returns 1 if the string s matches the last compiled regular
37 * 0 if the string s failed to match the last compiled
38 * regular expression, and
39 * -1 if the compiled regular expression was invalid
40 * (indicating an internal error).
42 * The strings passed to both re_comp and re_exec may have trailing or
43 * embedded newline characters; they are terminated by nulls.
45 * The identity of the author of these routines is lost in antiquity;
46 * this is essentially the same as the re code in the original V6 ed.
48 * The regular expressions recognized are described below. This description
49 * is essentially the same as that for ed.
51 * A regular expression specifies a set of strings of characters.
52 * A member of this set of strings is said to be matched by
53 * the regular expression. In the following specification for
54 * regular expressions the word `character' means any character but NUL.
56 * 1. Any character except a special character matches itself.
57 * Special characters are the regular expression delimiter plus
58 * \ [ . and sometimes ^ * $.
59 * 2. A . matches any character.
60 * 3. A \ followed by any character except a digit or ( )
61 * matches that character.
62 * 4. A nonempty string s bracketed [s] (or [^s]) matches any
63 * character in (or not in) s. In s, \ has no special meaning,
64 * and ] may only appear as the first letter. A substring
65 * a-b, with a and b in ascending ASCII order, stands for
66 * the inclusive range of ASCII characters.
67 * 5. A regular expression of form 1-4 followed by * matches a
68 * sequence of 0 or more matches of the regular expression.
69 * 6. A regular expression, x, of form 1-8, bracketed \(x\)
70 * matches what x matches.
71 * 7. A \ followed by a digit n matches a copy of the string that the
72 * bracketed regular expression beginning with the nth \( matched.
73 * 8. A regular expression of form 1-8, x, followed by a regular
74 * expression of form 1-7, y matches a match for x followed by
75 * a match for y, with the x match being as long as possible
76 * while still permitting a y match.
77 * 9. A regular expression of form 1-8 preceded by ^ (or followed
78 * by $), is constrained to matches that begin at the left
79 * (or end at the right) end of a line.
80 * 10. A regular expression of form 1-9 picks out the longest among
81 * the leftmost matches in a line.
82 * 11. An empty regular expression stands for a copy of the last
83 * regular expression encountered.
104 static char expbuf[ESIZE], *braslist[NBRA], *braelist[NBRA];
110 static int advance(register char *lp, register char *ep);
111 static int backref(register int i, register char *lp);
112 static int cclass(register char *set, register char c, int af);
116 * compile the regular expression argument into a dfa
119 re_comp(register char *sp)
122 register char *ep = expbuf;
123 int cclcnt, numbra = 0;
126 char *bracketp = &bracket[0];
127 static char *retoolong = "Regular expression too long";
129 #define comperr(msg) {expbuf[0] = 0; numbra = 0; return(msg); }
131 if (sp == 0 || *sp == '\0') {
133 return ("No previous regular expression");
142 if (ep >= &expbuf[ESIZE - 10 /* fudge factor */])
144 if ((c = *sp++) == '\0') {
145 if (bracketp != bracket)
146 comperr("unmatched \\(");
160 if (lastep == 0 || *lastep == CBRA || *lastep == CKET)
175 if ((c = *sp++) == '^') {
181 comperr("missing ]");
182 if (c == '-' && ep[-1] != 0) {
183 if ((c = *sp++) == ']') {
192 if (ep >= &expbuf[ESIZE - 10 /* fudge factor */])
198 if (ep >= &expbuf[ESIZE - 10 /* fudge factor */])
200 } while ((c = *sp++) != ']');
205 if ((c = *sp++) == '(') {
207 comperr("too many \\(\\) pairs");
208 *bracketp++ = numbra;
214 if (bracketp <= bracket)
215 comperr("unmatched \\)");
220 if (c >= '1' && c < ('1' + NBRA)) {
238 * match the argument string against the compiled re
241 re_exec(register char *p1)
243 register char *p2 = expbuf;
247 for (c = 0; c < NBRA; c++) {
252 return ((advance(p1, p2)));
254 * fast check for first character
261 if (rv = advance(p1, p2))
270 if (rv = advance(p1, p2))
277 * try to match the next thing in the dfa
280 advance(register char *lp, register char *ep)
282 register char *curlp;
308 if (cclass(ep, *lp++, 1)) {
315 if (cclass(ep, *lp++, 0)) {
322 braslist[*ep++] = lp;
326 braelist[*ep++] = lp;
330 if (braelist[i = *ep++] == 0)
332 if (backref(i, lp)) {
333 lp += braelist[i] - braslist[i];
339 if (braelist[i = *ep++] == 0)
342 ct = braelist[i] - braslist[i];
343 while (backref(i, lp))
345 while (lp >= curlp) {
346 if (rv = advance(lp, ep))
359 while (*lp++ == *ep);
366 while (cclass(ep, *lp++, ep[-1] == (CCL | CSTAR)));
373 if (rv = advance(lp, ep))
375 } while (lp > curlp);
384 backref(register int i, register char *lp)
389 while (*bp++ == *lp++)
390 if (bp >= braelist[i])
396 cclass(register char *set, register char c, int af)