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.
75 #include <afsconfig.h>
76 #include <afs/param.h>
96 static char expbuf[ESIZE], *braslist[NBRA], *braelist[NBRA];
99 static int advance(char *, char *);
100 static int backref(int i, char *);
101 static int cclass(char *, char, int);
104 * compile the regular expression argument into a dfa
107 re_comp(const char *sp)
110 register char *ep = expbuf;
111 int cclcnt, numbra = 0;
114 char *bracketp = &bracket[0];
115 static char *retoolong = "Regular expression too long";
117 #define comerr(msg) {expbuf[0] = 0; numbra = 0; return(msg); }
119 if (sp == 0 || *sp == '\0') {
121 return ("No previous regular expression");
130 if (ep >= &expbuf[ESIZE - 10 /* fudge factor */])
132 if ((c = *sp++) == '\0') {
133 if (bracketp != bracket)
134 comerr("unmatched \\(");
148 if (lastep == 0 || *lastep == CBRA || *lastep == CKET)
163 if ((c = *sp++) == '^') {
170 if (c == '-' && ep[-1] != 0) {
171 if ((c = *sp++) == ']') {
180 if (ep >= &expbuf[ESIZE - 10 /* fudge factor */])
186 if (ep >= &expbuf[ESIZE - 10 /* fudge factor */])
188 } while ((c = *sp++) != ']');
193 if ((c = *sp++) == '(') {
195 comerr("too many \\(\\) pairs");
196 *bracketp++ = numbra;
202 if (bracketp <= bracket)
203 comerr("unmatched \\)");
208 if (c >= '1' && c < ('1' + NBRA)) {
226 cclass(char *set, char c, int af)
240 backref(int i, char *lp)
245 while (*bp++ == *lp++)
246 if (bp >= braelist[i])
252 * try to match the next thing in the dfa
255 advance(char *lp, char *ep)
257 register char *curlp;
283 if (cclass(ep, *lp++, 1)) {
290 if (cclass(ep, *lp++, 0)) {
297 braslist[(int) *ep++] = lp;
301 braelist[(int) *ep++] = lp;
305 if (braelist[i = *ep++] == 0)
307 if (backref(i, lp)) {
308 lp += braelist[i] - braslist[i];
314 if (braelist[i = *ep++] == 0)
317 ct = braelist[i] - braslist[i];
318 while (backref(i, lp))
320 while (lp >= curlp) {
321 if ((rv = advance(lp, ep)))
334 while (*lp++ == *ep);
341 while (cclass(ep, *lp++, ep[-1] == (CCL | CSTAR)));
348 if ((rv = advance(lp, ep)))
350 } while (lp > curlp);
359 * match the argument string against the compiled re
362 re_exec(const char *p1)
364 register char *p2 = expbuf;
368 for (c = 0; c < NBRA; c++) {
373 return ((advance(p1, p2)));
375 * fast check for first character
382 if ((rv = advance(p1, p2)))
391 if ((rv = advance(p1, p2)))