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];
97 * compile the regular expression argument into a dfa
104 register char *ep = expbuf;
105 int cclcnt, numbra = 0;
108 char *bracketp = &bracket[0];
109 static char *retoolong = "Regular expression too long";
111 #define comerr(msg) {expbuf[0] = 0; numbra = 0; return(msg); }
113 if (sp == 0 || *sp == '\0') {
115 return("No previous regular expression");
125 if (ep >= &expbuf[ESIZE])
127 if ((c = *sp++) == '\0') {
128 if (bracketp != bracket)
129 comerr("unmatched \\(");
143 if (lastep == 0 || *lastep == CBRA || *lastep == CKET)
158 if ((c = *sp++) == '^') {
165 if (c == '-' && ep [-1] != 0) {
166 if ((c = *sp++) == ']') {
175 if (ep >= &expbuf[ESIZE])
181 if (ep >= &expbuf[ESIZE])
183 } while ((c = *sp++) != ']');
188 if ((c = *sp++) == '(') {
190 comerr("too many \\(\\) pairs");
191 *bracketp++ = numbra;
197 if (bracketp <= bracket)
198 comerr("unmatched \\)");
203 if (c >= '1' && c < ('1' + NBRA)) {
221 * match the argument string against the compiled re
227 register char *p2 = expbuf;
231 for (c = 0; c < NBRA; c++) {
236 return((advance(p1, p2)));
238 * fast check for first character
245 if (rv = advance(p1, p2))
254 if (rv = advance(p1, p2))
261 * try to match the next thing in the dfa
265 register char *lp, *ep;
267 register char *curlp;
293 if (cclass(ep, *lp++, 1)) {
300 if (cclass(ep, *lp++, 0)) {
307 braslist[*ep++] = lp;
311 braelist[*ep++] = lp;
315 if (braelist[i = *ep++] == 0)
317 if (backref(i, lp)) {
318 lp += braelist[i] - braslist[i];
324 if (braelist[i = *ep++] == 0)
327 ct = braelist[i] - braslist[i];
328 while (backref(i, lp))
330 while (lp >= curlp) {
331 if (rv = advance(lp, ep))
353 while (cclass(ep, *lp++, ep[-1] == (CCL|CSTAR)))
361 if (rv = advance(lp, ep))
363 } while (lp > curlp);
379 while (*bp++ == *lp++)
380 if (bp >= braelist[i])
387 register char *set, c;