1 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
2 * confused with the original package (see point 3 below). Thanks, Henry!
5 /* Additional note: this code is very heavily munged from Henry's version
6 * in places. In some spots I've traded clarity for efficiency, so don't
7 * blame Henry for some of the lack of readability.
10 /* $RCSfile: regexec.c,v $$Revision: 4.0.1.4 $$Date: 92/06/08 15:25:50 $
13 * Revision 4.0.1.4 92/06/08 15:25:50 lwall
14 * patch20: pattern modifiers i and g didn't interact right
15 * patch20: in some cases $` and $' didn't get set by match
16 * patch20: /x{0}/ was wrongly interpreted as /x{0,}/
18 * Revision 4.0.1.3 91/11/05 18:23:55 lwall
19 * patch11: prepared for ctype implementations that don't define isascii()
20 * patch11: initial .* in pattern had dependency on value of $*
22 * Revision 4.0.1.2 91/06/07 11:50:33 lwall
23 * patch4: new copyright notice
24 * patch4: // wouldn't use previous pattern if it started with a null character
26 * Revision 4.0.1.1 91/04/12 09:07:39 lwall
27 * patch1: regexec only allocated space for 9 subexpresssions
29 * Revision 4.0 91/03/20 01:39:16 lwall
35 * regcomp and regexec -- regsub and regerror are not used in perl
37 * Copyright (c) 1986 by University of Toronto.
38 * Written by Henry Spencer. Not derived from licensed software.
40 * Permission is granted to anyone to use this software for any
41 * purpose on any computer system, and to redistribute it freely,
42 * subject to the following restrictions:
44 * 1. The author is not responsible for the consequences of use of
45 * this software, no matter how awful, even if they arise
48 * 2. The origin of this software must not be misrepresented, either
49 * by explicit claim or by omission.
51 * 3. Altered versions must be plainly marked as such, and must not
52 * be misrepresented as being the original software.
54 **** Alterations to Henry's code are...
56 **** Copyright (c) 1991, Larry Wall
58 **** You may distribute under the terms of either the GNU General Public
59 **** License or the Artistic License, as specified in the README file.
61 * Beware that some of this code is subtly aware of the way operator
62 * precedence is structured in regular expressions. Serious changes in
63 * regular-expression syntax might require a total rethink.
82 * Global work variables for regexec().
84 static char *regprecomp;
85 static char *reginput; /* String-input pointer. */
86 static char regprev; /* char before regbol, \n if none */
87 static char *regbol; /* Beginning of input, for ^ check. */
88 static char *regeol; /* End of input, for $ check. */
89 static char **regstartp; /* Pointer to startp array. */
90 static char **regendp; /* Ditto for endp. */
91 static char *reglastparen; /* Similarly for lastparen. */
94 static int regmyp_size = 0;
95 static char **regmystartp = Null(char**);
96 static char **regmyendp = Null(char**);
102 STATIC int regmatch();
103 STATIC int regrepeat();
105 extern int multiline;
108 - regexec - match a regexp against a string
111 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
112 register regexp *prog;
114 register char *strend; /* pointer to null at end of string */
115 char *strbeg; /* real beginning of string */
116 int minend; /* end of match must be at least minend after stringarg */
118 int safebase; /* no need to remember string in subbase */
123 register char *string = stringarg;
125 int minlen = 0; /* must match at least this many chars */
126 int dontbother = 0; /* how many characters not to try at end */
129 if (prog == NULL || string == NULL) {
130 fatal("NULL regexp parameter");
134 if (string == strbeg) /* is ^ valid at stringarg? */
137 regprev = stringarg[-1];
138 if (!multiline && regprev == '\n')
139 regprev = '\0'; /* force ^ to NOT match */
141 regprecomp = prog->precomp;
142 /* Check validity of program. */
143 if (UCHARAT(prog->program) != MAGIC) {
144 FAIL("corrupted regexp program");
147 if (prog->do_folding) {
149 New(1101,c,i+1,char);
150 Copy(string, c, i+1, char);
153 for (s = string; s < strend; s++)
158 /* If there is a "must appear" string, look for it. */
160 if (prog->regmust != Nullstr &&
161 (!(prog->reganch & ROPT_ANCH)
162 || (multiline && prog->regback >= 0)) ) {
163 if (stringarg == strbeg && screamer) {
164 if (screamfirst[prog->regmust->str_rare] >= 0)
165 s = screaminstr(screamer,prog->regmust);
171 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
175 ++prog->regmust->str_u.str_useful; /* hooray */
176 goto phooey; /* not present */
178 else if (prog->regback >= 0) {
182 minlen = prog->regback + prog->regmust->str_cur;
184 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
185 str_free(prog->regmust);
186 prog->regmust = Nullstr; /* disable regmust */
191 minlen = prog->regmust->str_cur;
195 /* Mark beginning of line for ^ . */
198 /* Mark end of line for $ (and such) */
201 /* see how far we have to get to not match where we matched before */
202 regtill = string+minend;
204 /* Allocate our backreference arrays */
205 if ( regmyp_size < prog->nparens + 1 ) {
206 /* Allocate or enlarge the arrays */
207 regmyp_size = prog->nparens + 1;
208 if ( regmyp_size < 10 ) regmyp_size = 10; /* minimum */
210 /* reallocate larger */
211 Renew(regmystartp,regmyp_size,char*);
212 Renew(regmyendp, regmyp_size,char*);
215 /* Initial allocation */
216 New(1102,regmystartp,regmyp_size,char*);
217 New(1102,regmyendp, regmyp_size,char*);
222 /* Simplest case: anchored match need be tried only once. */
223 /* [unless multiline is set] */
224 if (prog->reganch & ROPT_ANCH) {
225 if (regtry(prog, string))
227 else if (multiline || (prog->reganch & ROPT_IMPLICIT)) {
229 dontbother = minlen - 1;
230 strend -= dontbother;
231 /* for multiline we only have to try after newlines */
236 if (s < strend && regtry(prog, s))
244 /* Messy cases: unanchored match. */
245 if (prog->regstart) {
246 if (prog->reganch & ROPT_SKIP) { /* we have /x+whatever/ */
247 /* it must be a one character string */
248 i = prog->regstart->str_ptr[0];
254 while (s < strend && *s == i)
260 else if (prog->regstart->str_pok == 3) {
261 /* We know what string it must start with. */
263 while ((s = fbminstr((unsigned char*)s,
264 (unsigned char*)strend, prog->regstart)) != NULL)
275 c = prog->regstart->str_ptr;
276 while ((s = ninstr(s, strend,
277 c, c + prog->regstart->str_cur )) != NULL) {
286 if (c = prog->regstclass) {
287 int doevery = (prog->reganch & ROPT_SKIP) == 0;
290 dontbother = minlen - 1;
291 strend -= dontbother; /* don't bother with what can't match */
293 /* We know what class it must start with. */
299 if (!(c[i >> 3] & (1 << (i&7)))) {
300 if (tmp && regtry(prog, s))
312 dontbother++,strend--;
318 tmp = isALNUM(regprev); /* assume not alphanumeric */
321 if (tmp != isALNUM(i)) {
328 if ((minlen || tmp) && regtry(prog,s))
333 dontbother++,strend--;
339 tmp = isALNUM(regprev); /* assume not alphanumeric */
342 if (tmp != isALNUM(i))
344 else if (regtry(prog, s))
348 if ((minlen || !tmp) && regtry(prog,s))
355 if (tmp && regtry(prog, s))
369 if (tmp && regtry(prog, s))
382 if (tmp && regtry(prog, s))
395 if (tmp && regtry(prog, s))
408 if (tmp && regtry(prog, s))
421 if (tmp && regtry(prog, s))
435 dontbother = minlen - 1;
436 strend -= dontbother;
437 /* We don't know much -- general case. */
441 } while (s++ < strend);
448 prog->subbeg = strbeg;
449 prog->subend = strend;
450 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
451 strend += dontbother; /* uncheat */
452 if (safebase) /* no need for $digit later */
454 else if (strbeg != prog->subbase) {
455 i = strend - string + (stringarg - strbeg);
456 s = nsavestr(strbeg,i); /* so $digit will work later */
458 Safefree(prog->subbase);
459 prog->subbeg = prog->subbase = s;
463 i = strend - string + (stringarg - strbeg);
464 prog->subbeg = s = prog->subbase;
467 s += (stringarg - strbeg);
468 for (i = 0; i <= prog->nparens; i++) {
470 prog->startp[i] = s + (prog->startp[i] - string);
471 prog->endp[i] = s + (prog->endp[i] - string);
474 if (prog->do_folding)
480 if (prog->do_folding)
486 - regtry - try match at specific point
488 static int /* 0 failure, 1 success */
498 regstartp = prog->startp;
499 regendp = prog->endp;
500 reglastparen = &prog->lastparen;
506 for (i = prog->nparens; i >= 0; i--) {
511 if (regmatch(prog->program + 1) && reginput >= regtill) {
512 prog->startp[0] = string;
513 prog->endp[0] = reginput;
520 - regmatch - main matching routine
522 * Conceptually the strategy is simple: check to see whether the current
523 * node matches, call self recursively to see whether the rest matches,
524 * and then act accordingly. In practice we make some effort to avoid
525 * recursion, in particular by going through "ordinary" nodes (that don't
526 * need to know whether the rest of the match failed) by a loop instead of
529 /* [lwall] I've hoisted the register declarations to the outer block in order to
530 * maybe save a little bit of pushing and popping on the stack. It also takes
531 * advantage of machines that use a register save mask on subroutine entry.
533 static int /* 0 failure, 1 success */
537 register char *scan; /* Current node. */
538 char *next; /* Next node. */
539 register int nextchar;
540 register int n; /* no or next */
541 register int ln; /* len or last */
542 register char *s; /* operand or save */
543 register char *locinput = reginput;
545 nextchar = *locinput;
548 if (scan != NULL && regnarrate)
549 fprintf(stderr, "%s(\n", regprop(scan));
551 while (scan != NULL) {
554 fprintf(stderr, "%s...\n", regprop(scan));
558 next = scan + NEXT(scan);
562 next = regnext(scan);
567 if (locinput == regbol ? regprev == '\n' :
568 ((nextchar || locinput < regeol) &&
569 locinput[-1] == '\n') )
571 /* regtill = regbol; */
576 if ((nextchar || locinput < regeol) && nextchar != '\n')
578 if (!multiline && regeol - locinput > 1)
580 /* regtill = regbol; */
583 if ((nextchar == '\0' && locinput >= regeol) ||
586 nextchar = *++locinput;
591 /* Inline the first character, for speed. */
594 if (regeol - locinput < ln)
596 if (ln > 1 && bcmp(s, locinput, ln) != 0)
599 nextchar = *locinput;
604 nextchar = UCHARAT(locinput);
605 if (s[nextchar >> 3] & (1 << (nextchar&7)))
607 if (!nextchar && locinput >= regeol)
609 nextchar = *++locinput;
614 if (!isALNUM(nextchar))
616 nextchar = *++locinput;
619 if (!nextchar && locinput >= regeol)
621 if (isALNUM(nextchar))
623 nextchar = *++locinput;
627 if (locinput == regbol) /* was last char in word? */
628 ln = isALNUM(regprev);
630 ln = isALNUM(locinput[-1]);
631 n = isALNUM(nextchar); /* is next char in word? */
632 if ((ln == n) == (OP(scan) == BOUND))
636 if (!nextchar && locinput >= regeol)
638 if (!isSPACE(nextchar))
640 nextchar = *++locinput;
645 if (isSPACE(nextchar))
647 nextchar = *++locinput;
650 if (!isDIGIT(nextchar))
652 nextchar = *++locinput;
655 if (!nextchar && locinput >= regeol)
657 if (isDIGIT(nextchar))
659 nextchar = *++locinput;
662 n = ARG1(scan); /* which paren pair */
668 if (s == regmyendp[n])
670 /* Inline the first character, for speed. */
673 ln = regmyendp[n] - s;
674 if (locinput + ln > regeol)
676 if (ln > 1 && bcmp(s, locinput, ln) != 0)
679 nextchar = *locinput;
687 n = ARG1(scan); /* which paren pair */
690 regmystartp[n] = locinput; /* for REF */
691 if (regmatch(next)) {
693 * Don't set startp if some later
694 * invocation of the same parentheses
697 if (regstartp[n] == NULL)
698 regstartp[n] = locinput;
704 n = ARG1(scan); /* which paren pair */
707 regmyendp[n] = locinput; /* for REF */
708 if (regmatch(next)) {
710 * Don't set endp if some later
711 * invocation of the same parentheses
714 if (regendp[n] == NULL) {
715 regendp[n] = locinput;
716 if (n > *reglastparen)
725 if (OP(next) != BRANCH) /* No choice. */
726 next = NEXTOPER(scan); /* Avoid recursion. */
730 if (regmatch(NEXTOPER(scan)))
739 scan = regnext(scan);
741 } while (scan != NULL && OP(scan) == BRANCH);
748 ln = ARG1(scan); /* min to match */
749 n = ARG2(scan); /* max to match */
750 scan = NEXTOPER(scan) + 4;
755 scan = NEXTOPER(scan);
759 * Lookahead to avoid useless match attempts
760 * when we know what character comes next.
764 scan = NEXTOPER(scan);
766 if (OP(next) == EXACTLY)
767 nextchar = *(OPERAND(next)+1);
771 n = regrepeat(scan, n);
772 if (!multiline && OP(next) == EOL && ln < n)
773 ln = n; /* why back off? */
775 /* If it could work, try it. */
776 if (nextchar == -1000 || *reginput == nextchar)
779 /* Couldn't or didn't -- back up. */
781 reginput = locinput + n;
785 reginput = locinput; /* put where regtry can find it */
786 return(1); /* Success! */
788 printf("%x %d\n",scan,scan[1]);
789 FAIL("regexp memory corruption");
796 * We get here only if there's trouble -- normally "case END" is
797 * the terminating point.
799 FAIL("corrupted regexp pointers");
807 - regrepeat - repeatedly match something simple, report how many
810 * [This routine now assumes that it will only match on things of length 1.
811 * That was true before, but now we assume scan - reginput is the count,
812 * rather than incrementing count on every character.]
822 register char *loceol = regeol;
825 if (max != 32767 && max < loceol - scan)
830 while (scan < loceol && *scan != '\n')
833 case EXACTLY: /* length of string is 1 */
835 while (scan < loceol && *opnd == *scan)
840 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
846 while (scan < loceol && isALNUM(*scan))
850 while (scan < loceol && !isALNUM(*scan))
854 while (scan < loceol && isSPACE(*scan))
858 while (scan < loceol && !isSPACE(*scan))
862 while (scan < loceol && isDIGIT(*scan))
866 while (scan < loceol && !isDIGIT(*scan))
869 default: /* Oh dear. Called inappropriately. */
870 FAIL("internal regexp foulup");
881 - regnext - dig the "next" pointer out of a node
883 * [Note, when REGALIGN is defined there are two places in regmatch()
884 * that bypass this code for speed.]