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 * routines to do regular expression matching
17 * ... returns 0 if the string s was compiled successfully,
18 * a pointer to an error message otherwise.
19 * If passed 0 or a null string returns without changing
20 * the currently compiled re (see note 11 below).
24 * ... returns 1 if the string s matches the last compiled regular
26 * 0 if the string s failed to match the last compiled
27 * regular expression, and
28 * -1 if the compiled regular expression was invalid
29 * (indicating an internal error).
31 * The strings passed to both re_comp and re_exec may have trailing or
32 * embedded newline characters; they are terminated by nulls.
34 * The identity of the author of these routines is lost in antiquity;
35 * this is essentially the same as the re code in the original V6 ed.
37 * The regular expressions recognized are described below. This description
38 * is essentially the same as that for ed.
40 * A regular expression specifies a set of strings of characters.
41 * A member of this set of strings is said to be matched by
42 * the regular expression. In the following specification for
43 * regular expressions the word `character' means any character but NUL.
45 * 1. Any character except a special character matches itself.
46 * Special characters are the regular expression delimiter plus
47 * \ [ . and sometimes ^ * $.
48 * 2. A . matches any character.
49 * 3. A \ followed by any character except a digit or ( )
50 * matches that character.
51 * 4. A nonempty string s bracketed [s] (or [^s]) matches any
52 * character in (or not in) s. In s, \ has no special meaning,
53 * and ] may only appear as the first letter. A substring
54 * a-b, with a and b in ascending ASCII order, stands for
55 * the inclusive range of ASCII characters.
56 * 5. A regular expression of form 1-4 followed by * matches a
57 * sequence of 0 or more matches of the regular expression.
58 * 6. A regular expression, x, of form 1-8, bracketed \(x\)
59 * matches what x matches.
60 * 7. A \ followed by a digit n matches a copy of the string that the
61 * bracketed regular expression beginning with the nth \( matched.
62 * 8. A regular expression of form 1-8, x, followed by a regular
63 * expression of form 1-7, y matches a match for x followed by
64 * a match for y, with the x match being as long as possible
65 * while still permitting a y match.
66 * 9. A regular expression of form 1-8 preceded by ^ (or followed
67 * by $), is constrained to matches that begin at the left
68 * (or end at the right) end of a line.
69 * 10. A regular expression of form 1-9 picks out the longest among
70 * the leftmost matches in a line.
71 * 11. An empty regular expression stands for a copy of the last
72 * regular expression encountered.
93 static char expbuf[ESIZE], *braslist[NBRA], *braelist[NBRA];
101 * compile the regular expression argument into a dfa
108 register char *ep = expbuf;
109 int cclcnt, numbra = 0;
112 char *bracketp = &bracket[0];
113 static char *retoolong = "Regular expression too long";
115 #define comerr(msg) {expbuf[0] = 0; numbra = 0; return(msg); }
117 if (sp == 0 || *sp == '\0') {
119 return ("No previous regular expression");
128 if (ep >= &expbuf[ESIZE])
130 if ((c = *sp++) == '\0') {
131 if (bracketp != bracket)
132 comerr("unmatched \\(");
146 if (lastep == 0 || *lastep == CBRA || *lastep == CKET)
161 if ((c = *sp++) == '^') {
168 if (c == '-' && ep[-1] != 0) {
169 if ((c = *sp++) == ']') {
178 if (ep >= &expbuf[ESIZE])
184 if (ep >= &expbuf[ESIZE])
186 } while ((c = *sp++) != ']');
191 if ((c = *sp++) == '(') {
193 comerr("too many \\(\\) pairs");
194 *bracketp++ = numbra;
200 if (bracketp <= bracket)
201 comerr("unmatched \\)");
206 if (c >= '1' && c < ('1' + NBRA)) {
225 register char *set, c;
247 while (*bp++ == *lp++)
248 if (bp >= braelist[i])
254 * try to match the next thing in the dfa
258 register char *lp, *ep;
260 register char *curlp;
286 if (cclass(ep, *lp++, 1)) {
293 if (cclass(ep, *lp++, 0)) {
300 braslist[*ep++] = lp;
304 braelist[*ep++] = lp;
308 if (braelist[i = *ep++] == 0)
310 if (backref(i, lp)) {
311 lp += braelist[i] - braslist[i];
317 if (braelist[i = *ep++] == 0)
320 ct = braelist[i] - braslist[i];
321 while (backref(i, lp))
323 while (lp >= curlp) {
324 if (rv = advance(lp, ep))
337 while (*lp++ == *ep);
344 while (cclass(ep, *lp++, ep[-1] == (CCL | CSTAR)));
351 if (rv = advance(lp, ep))
353 } while (lp > curlp);
362 * match the argument string against the compiled re
368 register char *p2 = expbuf;
372 for (c = 0; c < NBRA; c++) {
377 return ((advance(p1, p2)));
379 * fast check for first character
386 if (rv = advance(p1, p2))
395 if (rv = advance(p1, p2))