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 static char sccsid[] = "@(#)regex.c 1.1 86/09/24 SMI"; /* from UCB 4.1 80/12/21 */
15 * routines to do regular expression matching
21 * ... returns 0 if the string s was compiled successfully,
22 * a pointer to an error message otherwise.
23 * If passed 0 or a null string returns without changing
24 * the currently compiled re (see note 11 below).
28 * ... returns 1 if the string s matches the last compiled regular
30 * 0 if the string s failed to match the last compiled
31 * regular expression, and
32 * -1 if the compiled regular expression was invalid
33 * (indicating an internal error).
35 * The strings passed to both re_comp and re_exec may have trailing or
36 * embedded newline characters; they are terminated by nulls.
38 * The identity of the author of these routines is lost in antiquity;
39 * this is essentially the same as the re code in the original V6 ed.
41 * The regular expressions recognized are described below. This description
42 * is essentially the same as that for ed.
44 * A regular expression specifies a set of strings of characters.
45 * A member of this set of strings is said to be matched by
46 * the regular expression. In the following specification for
47 * regular expressions the word `character' means any character but NUL.
49 * 1. Any character except a special character matches itself.
50 * Special characters are the regular expression delimiter plus
51 * \ [ . and sometimes ^ * $.
52 * 2. A . matches any character.
53 * 3. A \ followed by any character except a digit or ( )
54 * matches that character.
55 * 4. A nonempty string s bracketed [s] (or [^s]) matches any
56 * character in (or not in) s. In s, \ has no special meaning,
57 * and ] may only appear as the first letter. A substring
58 * a-b, with a and b in ascending ASCII order, stands for
59 * the inclusive range of ASCII characters.
60 * 5. A regular expression of form 1-4 followed by * matches a
61 * sequence of 0 or more matches of the regular expression.
62 * 6. A regular expression, x, of form 1-8, bracketed \(x\)
63 * matches what x matches.
64 * 7. A \ followed by a digit n matches a copy of the string that the
65 * bracketed regular expression beginning with the nth \( matched.
66 * 8. A regular expression of form 1-8, x, followed by a regular
67 * expression of form 1-7, y matches a match for x followed by
68 * a match for y, with the x match being as long as possible
69 * while still permitting a y match.
70 * 9. A regular expression of form 1-8 preceded by ^ (or followed
71 * by $), is constrained to matches that begin at the left
72 * (or end at the right) end of a line.
73 * 10. A regular expression of form 1-9 picks out the longest among
74 * the leftmost matches in a line.
75 * 11. An empty regular expression stands for a copy of the last
76 * regular expression encountered.
97 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");
129 if (ep >= &expbuf[ESIZE])
131 if ((c = *sp++) == '\0') {
132 if (bracketp != bracket)
133 comerr("unmatched \\(");
147 if (lastep == 0 || *lastep == CBRA || *lastep == CKET)
162 if ((c = *sp++) == '^') {
169 if (c == '-' && ep [-1] != 0) {
170 if ((c = *sp++) == ']') {
179 if (ep >= &expbuf[ESIZE])
185 if (ep >= &expbuf[ESIZE])
187 } while ((c = *sp++) != ']');
192 if ((c = *sp++) == '(') {
194 comerr("too many \\(\\) pairs");
195 *bracketp++ = numbra;
201 if (bracketp <= bracket)
202 comerr("unmatched \\)");
207 if (c >= '1' && c < ('1' + NBRA)) {
225 * match the argument string against the compiled re
231 register char *p2 = expbuf;
235 for (c = 0; c < NBRA; c++) {
240 return((advance(p1, p2)));
242 * fast check for first character
249 if (rv = advance(p1, p2))
258 if (rv = advance(p1, p2))
265 * try to match the next thing in the dfa
269 register char *lp, *ep;
271 register char *curlp;
297 if (cclass(ep, *lp++, 1)) {
304 if (cclass(ep, *lp++, 0)) {
311 braslist[*ep++] = lp;
315 braelist[*ep++] = lp;
319 if (braelist[i = *ep++] == 0)
321 if (backref(i, lp)) {
322 lp += braelist[i] - braslist[i];
328 if (braelist[i = *ep++] == 0)
331 ct = braelist[i] - braslist[i];
332 while (backref(i, lp))
334 while (lp >= curlp) {
335 if (rv = advance(lp, ep))
357 while (cclass(ep, *lp++, ep[-1] == (CCL|CSTAR)))
365 if (rv = advance(lp, ep))
367 } while (lp > curlp);
383 while (*bp++ == *lp++)
384 if (bp >= braelist[i])
391 register char *set, c;