158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 Henry Spencer. 358f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 458f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 558f0484fSRodney W. Grimes * 658f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 758f0484fSRodney W. Grimes * Henry Spencer. 858f0484fSRodney W. Grimes * 958f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1058f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1158f0484fSRodney W. Grimes * are met: 1258f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1458f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1658f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1758f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1858f0484fSRodney W. Grimes * must display the following acknowledgement: 1958f0484fSRodney W. Grimes * This product includes software developed by the University of 2058f0484fSRodney W. Grimes * California, Berkeley and its contributors. 2158f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2258f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2358f0484fSRodney W. Grimes * without specific prior written permission. 2458f0484fSRodney W. Grimes * 2558f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2658f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2758f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2858f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2958f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3058f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3158f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3258f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3358f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3458f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3558f0484fSRodney W. Grimes * SUCH DAMAGE. 3658f0484fSRodney W. Grimes * 3758f0484fSRodney W. Grimes * @(#)regexec.c 8.3 (Berkeley) 3/20/94 3858f0484fSRodney W. Grimes */ 3958f0484fSRodney W. Grimes 4058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 4158f0484fSRodney W. Grimes static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94"; 4258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 4358f0484fSRodney W. Grimes 4458f0484fSRodney W. Grimes /* 4558f0484fSRodney W. Grimes * the outer shell of regexec() 4658f0484fSRodney W. Grimes * 4758f0484fSRodney W. Grimes * This file includes engine.c *twice*, after muchos fiddling with the 4858f0484fSRodney W. Grimes * macros that code uses. This lets the same code operate on two different 4958f0484fSRodney W. Grimes * representations for state sets. 5058f0484fSRodney W. Grimes */ 5158f0484fSRodney W. Grimes #include <sys/types.h> 5258f0484fSRodney W. Grimes #include <stdio.h> 5358f0484fSRodney W. Grimes #include <stdlib.h> 5458f0484fSRodney W. Grimes #include <string.h> 5558f0484fSRodney W. Grimes #include <limits.h> 5658f0484fSRodney W. Grimes #include <ctype.h> 5758f0484fSRodney W. Grimes #include <regex.h> 5858f0484fSRodney W. Grimes 5958f0484fSRodney W. Grimes #include "utils.h" 6058f0484fSRodney W. Grimes #include "regex2.h" 6158f0484fSRodney W. Grimes 6258f0484fSRodney W. Grimes static int nope = 0; /* for use in asserts; shuts lint up */ 6358f0484fSRodney W. Grimes 6458f0484fSRodney W. Grimes /* macros for manipulating states, small version */ 6558f0484fSRodney W. Grimes #define states long 6658f0484fSRodney W. Grimes #define states1 states /* for later use in regexec() decision */ 6758f0484fSRodney W. Grimes #define CLEAR(v) ((v) = 0) 68cfc1614aSJohn Birrell #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n))) 69cfc1614aSJohn Birrell #define SET1(v, n) ((v) |= (unsigned long)1 << (n)) 70cfc1614aSJohn Birrell #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0) 7158f0484fSRodney W. Grimes #define ASSIGN(d, s) ((d) = (s)) 7258f0484fSRodney W. Grimes #define EQ(a, b) ((a) == (b)) 73cfc1614aSJohn Birrell #define STATEVARS long dummy /* dummy version */ 7458f0484fSRodney W. Grimes #define STATESETUP(m, n) /* nothing */ 7558f0484fSRodney W. Grimes #define STATETEARDOWN(m) /* nothing */ 7658f0484fSRodney W. Grimes #define SETUP(v) ((v) = 0) 77cfc1614aSJohn Birrell #define onestate long 78cfc1614aSJohn Birrell #define INIT(o, n) ((o) = (unsigned long)1 << (n)) 7958f0484fSRodney W. Grimes #define INC(o) ((o) <<= 1) 80cfc1614aSJohn Birrell #define ISSTATEIN(v, o) (((v) & (o)) != 0) 8158f0484fSRodney W. Grimes /* some abbreviations; note that some of these know variable names! */ 8258f0484fSRodney W. Grimes /* do "if I'm here, I can also be there" etc without branches */ 83cfc1614aSJohn Birrell #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n)) 84cfc1614aSJohn Birrell #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n)) 85cfc1614aSJohn Birrell #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0) 8658f0484fSRodney W. Grimes /* function names */ 8758f0484fSRodney W. Grimes #define SNAMES /* engine.c looks after details */ 8858f0484fSRodney W. Grimes 8958f0484fSRodney W. Grimes #include "engine.c" 9058f0484fSRodney W. Grimes 9158f0484fSRodney W. Grimes /* now undo things */ 9258f0484fSRodney W. Grimes #undef states 9358f0484fSRodney W. Grimes #undef CLEAR 9458f0484fSRodney W. Grimes #undef SET0 9558f0484fSRodney W. Grimes #undef SET1 9658f0484fSRodney W. Grimes #undef ISSET 9758f0484fSRodney W. Grimes #undef ASSIGN 9858f0484fSRodney W. Grimes #undef EQ 9958f0484fSRodney W. Grimes #undef STATEVARS 10058f0484fSRodney W. Grimes #undef STATESETUP 10158f0484fSRodney W. Grimes #undef STATETEARDOWN 10258f0484fSRodney W. Grimes #undef SETUP 10358f0484fSRodney W. Grimes #undef onestate 10458f0484fSRodney W. Grimes #undef INIT 10558f0484fSRodney W. Grimes #undef INC 10658f0484fSRodney W. Grimes #undef ISSTATEIN 10758f0484fSRodney W. Grimes #undef FWD 10858f0484fSRodney W. Grimes #undef BACK 10958f0484fSRodney W. Grimes #undef ISSETBACK 11058f0484fSRodney W. Grimes #undef SNAMES 11158f0484fSRodney W. Grimes 11258f0484fSRodney W. Grimes /* macros for manipulating states, large version */ 11358f0484fSRodney W. Grimes #define states char * 11458f0484fSRodney W. Grimes #define CLEAR(v) memset(v, 0, m->g->nstates) 11558f0484fSRodney W. Grimes #define SET0(v, n) ((v)[n] = 0) 11658f0484fSRodney W. Grimes #define SET1(v, n) ((v)[n] = 1) 11758f0484fSRodney W. Grimes #define ISSET(v, n) ((v)[n]) 11858f0484fSRodney W. Grimes #define ASSIGN(d, s) memcpy(d, s, m->g->nstates) 11958f0484fSRodney W. Grimes #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0) 120cfc1614aSJohn Birrell #define STATEVARS long vn; char *space 12158f0484fSRodney W. Grimes #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \ 12258f0484fSRodney W. Grimes if ((m)->space == NULL) return(REG_ESPACE); \ 12358f0484fSRodney W. Grimes (m)->vn = 0; } 12458f0484fSRodney W. Grimes #define STATETEARDOWN(m) { free((m)->space); } 12558f0484fSRodney W. Grimes #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates]) 126cfc1614aSJohn Birrell #define onestate long 12758f0484fSRodney W. Grimes #define INIT(o, n) ((o) = (n)) 12858f0484fSRodney W. Grimes #define INC(o) ((o)++) 12958f0484fSRodney W. Grimes #define ISSTATEIN(v, o) ((v)[o]) 13058f0484fSRodney W. Grimes /* some abbreviations; note that some of these know variable names! */ 13158f0484fSRodney W. Grimes /* do "if I'm here, I can also be there" etc without branches */ 13258f0484fSRodney W. Grimes #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here]) 13358f0484fSRodney W. Grimes #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here]) 13458f0484fSRodney W. Grimes #define ISSETBACK(v, n) ((v)[here - (n)]) 13558f0484fSRodney W. Grimes /* function names */ 13658f0484fSRodney W. Grimes #define LNAMES /* flag */ 13758f0484fSRodney W. Grimes 13858f0484fSRodney W. Grimes #include "engine.c" 13958f0484fSRodney W. Grimes 14058f0484fSRodney W. Grimes /* 14158f0484fSRodney W. Grimes - regexec - interface for matching 14258f0484fSRodney W. Grimes = extern int regexec(const regex_t *, const char *, size_t, \ 14358f0484fSRodney W. Grimes = regmatch_t [], int); 14458f0484fSRodney W. Grimes = #define REG_NOTBOL 00001 14558f0484fSRodney W. Grimes = #define REG_NOTEOL 00002 14658f0484fSRodney W. Grimes = #define REG_STARTEND 00004 14758f0484fSRodney W. Grimes = #define REG_TRACE 00400 // tracing of execution 14858f0484fSRodney W. Grimes = #define REG_LARGE 01000 // force large representation 14958f0484fSRodney W. Grimes = #define REG_BACKR 02000 // force use of backref code 15058f0484fSRodney W. Grimes * 15158f0484fSRodney W. Grimes * We put this here so we can exploit knowledge of the state representation 15258f0484fSRodney W. Grimes * when choosing which matcher to call. Also, by this point the matchers 15358f0484fSRodney W. Grimes * have been prototyped. 15458f0484fSRodney W. Grimes */ 15558f0484fSRodney W. Grimes int /* 0 success, REG_NOMATCH failure */ 15658f0484fSRodney W. Grimes regexec(preg, string, nmatch, pmatch, eflags) 15758f0484fSRodney W. Grimes const regex_t *preg; 15858f0484fSRodney W. Grimes const char *string; 15958f0484fSRodney W. Grimes size_t nmatch; 16058f0484fSRodney W. Grimes regmatch_t pmatch[]; 16158f0484fSRodney W. Grimes int eflags; 16258f0484fSRodney W. Grimes { 16358f0484fSRodney W. Grimes register struct re_guts *g = preg->re_g; 16458f0484fSRodney W. Grimes #ifdef REDEBUG 16558f0484fSRodney W. Grimes # define GOODFLAGS(f) (f) 16658f0484fSRodney W. Grimes #else 16758f0484fSRodney W. Grimes # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND)) 16858f0484fSRodney W. Grimes #endif 16958f0484fSRodney W. Grimes 17058f0484fSRodney W. Grimes if (preg->re_magic != MAGIC1 || g->magic != MAGIC2) 17158f0484fSRodney W. Grimes return(REG_BADPAT); 17258f0484fSRodney W. Grimes assert(!(g->iflags&BAD)); 17358f0484fSRodney W. Grimes if (g->iflags&BAD) /* backstop for no-debug case */ 17458f0484fSRodney W. Grimes return(REG_BADPAT); 17558f0484fSRodney W. Grimes eflags = GOODFLAGS(eflags); 17658f0484fSRodney W. Grimes 17758f0484fSRodney W. Grimes if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags®_LARGE)) 17858f0484fSRodney W. Grimes return(smatcher(g, (char *)string, nmatch, pmatch, eflags)); 17958f0484fSRodney W. Grimes else 18058f0484fSRodney W. Grimes return(lmatcher(g, (char *)string, nmatch, pmatch, eflags)); 18158f0484fSRodney W. Grimes } 182