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.1 $$Date: 91/04/12 09:07:39 $
13 * Revision 4.0.1.1 91/04/12 09:07:39 lwall
14 * patch1: regexec only allocated space for 9 subexpresssions
16 * Revision 4.0 91/03/20 01:39:16 lwall
22 * regcomp and regexec -- regsub and regerror are not used in perl
24 * Copyright (c) 1986 by University of Toronto.
25 * Written by Henry Spencer. Not derived from licensed software.
27 * Permission is granted to anyone to use this software for any
28 * purpose on any computer system, and to redistribute it freely,
29 * subject to the following restrictions:
31 * 1. The author is not responsible for the consequences of use of
32 * this software, no matter how awful, even if they arise
35 * 2. The origin of this software must not be misrepresented, either
36 * by explicit claim or by omission.
38 * 3. Altered versions must be plainly marked as such, and must not
39 * be misrepresented as being the original software.
41 **** Alterations to Henry's code are...
43 **** Copyright (c) 1989, Larry Wall
45 **** You may distribute under the terms of the GNU General Public License
46 **** as specified in the README file that comes with the perl 3.0 kit.
48 * Beware that some of this code is subtly aware of the way operator
49 * precedence is structured in regular expressions. Serious changes in
50 * regular-expression syntax might require a total rethink.
64 #define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
65 #define isSPACE(c) (isascii(c) && isspace(c))
66 #define isDIGIT(c) (isascii(c) && isdigit(c))
67 #define isUPPER(c) (isascii(c) && isupper(c))
74 * Global work variables for regexec().
76 static char *regprecomp;
77 static char *reginput; /* String-input pointer. */
78 static char regprev; /* char before regbol, \n if none */
79 static char *regbol; /* Beginning of input, for ^ check. */
80 static char *regeol; /* End of input, for $ check. */
81 static char **regstartp; /* Pointer to startp array. */
82 static char **regendp; /* Ditto for endp. */
83 static char *reglastparen; /* Similarly for lastparen. */
86 static int regmyp_size = 0;
87 static char **regmystartp = Null(char**);
88 static char **regmyendp = Null(char**);
94 STATIC int regmatch();
95 STATIC int regrepeat();
100 - regexec - match a regexp against a string
103 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
104 register regexp *prog;
106 register char *strend; /* pointer to null at end of string */
107 char *strbeg; /* real beginning of string */
108 int minend; /* end of match must be at least minend after stringarg */
110 int safebase; /* no need to remember string in subbase */
115 register char *string = stringarg;
117 int minlen = 0; /* must match at least this many chars */
118 int dontbother = 0; /* how many characters not to try at end */
121 if (prog == NULL || string == NULL) {
122 fatal("NULL regexp parameter");
126 if (string == strbeg) /* is ^ valid at stringarg? */
129 regprev = stringarg[-1];
130 if (!multiline && regprev == '\n')
131 regprev = '\0'; /* force ^ to NOT match */
133 regprecomp = prog->precomp;
134 /* Check validity of program. */
135 if (UCHARAT(prog->program) != MAGIC) {
136 FAIL("corrupted regexp program");
139 if (prog->do_folding) {
142 New(1101,c,i+1,char);
143 (void)bcopy(string, c, i+1);
146 for (s = string; s < strend; s++)
151 /* If there is a "must appear" string, look for it. */
153 if (prog->regmust != Nullstr &&
154 (!(prog->reganch & 1) || (multiline && prog->regback >= 0)) ) {
155 if (stringarg == strbeg && screamer) {
156 if (screamfirst[prog->regmust->str_rare] >= 0)
157 s = screaminstr(screamer,prog->regmust);
163 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
167 ++prog->regmust->str_u.str_useful; /* hooray */
168 goto phooey; /* not present */
170 else if (prog->regback >= 0) {
174 minlen = prog->regback + prog->regmust->str_cur;
176 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
177 str_free(prog->regmust);
178 prog->regmust = Nullstr; /* disable regmust */
183 minlen = prog->regmust->str_cur;
187 /* Mark beginning of line for ^ . */
190 /* Mark end of line for $ (and such) */
193 /* see how far we have to get to not match where we matched before */
194 regtill = string+minend;
196 /* Allocate our backreference arrays */
197 if ( regmyp_size < prog->nparens + 1 ) {
198 /* Allocate or enlarge the arrays */
199 regmyp_size = prog->nparens + 1;
200 if ( regmyp_size < 10 ) regmyp_size = 10; /* minimum */
202 /* reallocate larger */
203 Renew(regmystartp,regmyp_size,char*);
204 Renew(regmyendp, regmyp_size,char*);
207 /* Initial allocation */
208 New(1102,regmystartp,regmyp_size,char*);
209 New(1102,regmyendp, regmyp_size,char*);
214 /* Simplest case: anchored match need be tried only once. */
215 /* [unless multiline is set] */
216 if (prog->reganch & 1) {
217 if (regtry(prog, string))
219 else if (multiline) {
221 dontbother = minlen - 1;
222 strend -= dontbother;
223 /* for multiline we only have to try after newlines */
228 if (s < strend && regtry(prog, s))
236 /* Messy cases: unanchored match. */
237 if (prog->regstart) {
238 if (prog->reganch & 2) { /* we have /x+whatever/ */
239 /* it must be a one character string */
240 i = prog->regstart->str_ptr[0];
246 while (s < strend && *s == i)
252 else if (prog->regstart->str_pok == 3) {
253 /* We know what string it must start with. */
255 while ((s = fbminstr((unsigned char*)s,
256 (unsigned char*)strend, prog->regstart)) != NULL)
267 c = prog->regstart->str_ptr;
268 while ((s = ninstr(s, strend,
269 c, c + prog->regstart->str_cur )) != NULL) {
277 if (c = prog->regstclass) {
278 int doevery = (prog->reganch & 2) == 0;
281 dontbother = minlen - 1;
282 strend -= dontbother; /* don't bother with what can't match */
284 /* We know what class it must start with. */
290 if (!(c[i >> 3] & (1 << (i&7)))) {
291 if (tmp && regtry(prog, s))
303 dontbother++,strend--;
309 tmp = isALNUM(regprev); /* assume not alphanumeric */
312 if (tmp != isALNUM(i)) {
319 if ((minlen || tmp) && regtry(prog,s))
324 dontbother++,strend--;
330 tmp = isALNUM(regprev); /* assume not alphanumeric */
333 if (tmp != isALNUM(i))
335 else if (regtry(prog, s))
339 if ((minlen || !tmp) && regtry(prog,s))
346 if (tmp && regtry(prog, s))
360 if (tmp && regtry(prog, s))
373 if (tmp && regtry(prog, s))
386 if (tmp && regtry(prog, s))
399 if (tmp && regtry(prog, s))
412 if (tmp && regtry(prog, s))
426 dontbother = minlen - 1;
427 strend -= dontbother;
428 /* We don't know much -- general case. */
432 } while (s++ < strend);
439 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
440 strend += dontbother; /* uncheat */
441 if (safebase) /* no need for $digit later */
443 else if (strbeg != prog->subbase) {
444 i = strend - string + (stringarg - strbeg);
445 s = nsavestr(strbeg,i); /* so $digit will work later */
447 Safefree(prog->subbase);
453 s += (stringarg - strbeg);
454 for (i = 0; i <= prog->nparens; i++) {
456 prog->startp[i] = s + (prog->startp[i] - string);
457 prog->endp[i] = s + (prog->endp[i] - string);
460 if (prog->do_folding)
466 if (prog->do_folding)
472 - regtry - try match at specific point
474 static int /* 0 failure, 1 success */
484 regstartp = prog->startp;
485 regendp = prog->endp;
486 reglastparen = &prog->lastparen;
492 for (i = prog->nparens; i >= 0; i--) {
497 if (regmatch(prog->program + 1) && reginput >= regtill) {
498 prog->startp[0] = string;
499 prog->endp[0] = reginput;
506 - regmatch - main matching routine
508 * Conceptually the strategy is simple: check to see whether the current
509 * node matches, call self recursively to see whether the rest matches,
510 * and then act accordingly. In practice we make some effort to avoid
511 * recursion, in particular by going through "ordinary" nodes (that don't
512 * need to know whether the rest of the match failed) by a loop instead of
515 /* [lwall] I've hoisted the register declarations to the outer block in order to
516 * maybe save a little bit of pushing and popping on the stack. It also takes
517 * advantage of machines that use a register save mask on subroutine entry.
519 static int /* 0 failure, 1 success */
523 register char *scan; /* Current node. */
524 char *next; /* Next node. */
525 register int nextchar;
526 register int n; /* no or next */
527 register int ln; /* len or last */
528 register char *s; /* operand or save */
529 register char *locinput = reginput;
531 nextchar = *locinput;
534 if (scan != NULL && regnarrate)
535 fprintf(stderr, "%s(\n", regprop(scan));
537 while (scan != NULL) {
540 fprintf(stderr, "%s...\n", regprop(scan));
544 next = scan + NEXT(scan);
548 next = regnext(scan);
553 if (locinput == regbol ? regprev == '\n' :
554 ((nextchar || locinput < regeol) &&
555 locinput[-1] == '\n') )
557 /* regtill = regbol; */
562 if ((nextchar || locinput < regeol) && nextchar != '\n')
564 if (!multiline && regeol - locinput > 1)
566 /* regtill = regbol; */
569 if ((nextchar == '\0' && locinput >= regeol) ||
572 nextchar = *++locinput;
577 /* Inline the first character, for speed. */
580 if (regeol - locinput < ln)
582 if (ln > 1 && bcmp(s, locinput, ln) != 0)
585 nextchar = *locinput;
590 nextchar = UCHARAT(locinput);
591 if (s[nextchar >> 3] & (1 << (nextchar&7)))
593 if (!nextchar && locinput >= regeol)
595 nextchar = *++locinput;
600 if (!isALNUM(nextchar))
602 nextchar = *++locinput;
605 if (!nextchar && locinput >= regeol)
607 if (isALNUM(nextchar))
609 nextchar = *++locinput;
613 if (locinput == regbol) /* was last char in word? */
614 ln = isALNUM(regprev);
616 ln = isALNUM(locinput[-1]);
617 n = isALNUM(nextchar); /* is next char in word? */
618 if ((ln == n) == (OP(scan) == BOUND))
622 if (!nextchar && locinput >= regeol)
624 if (!isSPACE(nextchar))
626 nextchar = *++locinput;
631 if (isSPACE(nextchar))
633 nextchar = *++locinput;
636 if (!isDIGIT(nextchar))
638 nextchar = *++locinput;
641 if (!nextchar && locinput >= regeol)
643 if (isDIGIT(nextchar))
645 nextchar = *++locinput;
648 n = ARG1(scan); /* which paren pair */
654 if (s == regmyendp[n])
656 /* Inline the first character, for speed. */
659 ln = regmyendp[n] - s;
660 if (locinput + ln > regeol)
662 if (ln > 1 && bcmp(s, locinput, ln) != 0)
665 nextchar = *locinput;
673 n = ARG1(scan); /* which paren pair */
676 regmystartp[n] = locinput; /* for REF */
677 if (regmatch(next)) {
679 * Don't set startp if some later
680 * invocation of the same parentheses
683 if (regstartp[n] == NULL)
684 regstartp[n] = locinput;
690 n = ARG1(scan); /* which paren pair */
693 regmyendp[n] = locinput; /* for REF */
694 if (regmatch(next)) {
696 * Don't set endp if some later
697 * invocation of the same parentheses
700 if (regendp[n] == NULL) {
701 regendp[n] = locinput;
702 if (n > *reglastparen)
711 if (OP(next) != BRANCH) /* No choice. */
712 next = NEXTOPER(scan); /* Avoid recursion. */
716 if (regmatch(NEXTOPER(scan)))
724 scan = regnext(scan);
726 } while (scan != NULL && OP(scan) == BRANCH);
733 ln = ARG1(scan); /* min to match */
734 n = ARG2(scan); /* max to match */
735 scan = NEXTOPER(scan) + 4;
740 scan = NEXTOPER(scan);
744 * Lookahead to avoid useless match attempts
745 * when we know what character comes next.
749 scan = NEXTOPER(scan);
751 if (OP(next) == EXACTLY)
752 nextchar = *(OPERAND(next)+1);
756 n = regrepeat(scan, n);
757 if (!multiline && OP(next) == EOL && ln < n)
758 ln = n; /* why back off? */
760 /* If it could work, try it. */
761 if (nextchar == -1000 || *reginput == nextchar)
764 /* Couldn't or didn't -- back up. */
766 reginput = locinput + n;
770 reginput = locinput; /* put where regtry can find it */
771 return(1); /* Success! */
773 printf("%x %d\n",scan,scan[1]);
774 FAIL("regexp memory corruption");
781 * We get here only if there's trouble -- normally "case END" is
782 * the terminating point.
784 FAIL("corrupted regexp pointers");
792 - regrepeat - repeatedly match something simple, report how many
795 * [This routine now assumes that it will only match on things of length 1.
796 * That was true before, but now we assume scan - reginput is the count,
797 * rather than incrementing count on every character.]
807 register char *loceol = regeol;
810 if (max && max < loceol - scan)
815 while (scan < loceol && *scan != '\n')
818 case EXACTLY: /* length of string is 1 */
820 while (scan < loceol && *opnd == *scan)
825 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
831 while (scan < loceol && isALNUM(*scan))
835 while (scan < loceol && !isALNUM(*scan))
839 while (scan < loceol && isSPACE(*scan))
843 while (scan < loceol && !isSPACE(*scan))
847 while (scan < loceol && isDIGIT(*scan))
851 while (scan < loceol && !isDIGIT(*scan))
854 default: /* Oh dear. Called inappropriately. */
855 FAIL("internal regexp foulup");
866 - regnext - dig the "next" pointer out of a node
868 * [Note, when REGALIGN is defined there are two places in regmatch()
869 * that bypass this code for speed.]