Home | History | Annotate | Line # | Download | only in regex
regexec.c revision 1.1
      1  1.1  jtc /*
      2  1.1  jtc  * the outer shell of regexec()
      3  1.1  jtc  *
      4  1.1  jtc  * This file includes engine.c *twice*, after muchos fiddling with the
      5  1.1  jtc  * macros that code uses.  This lets the same code operate on two different
      6  1.1  jtc  * representations for state sets.
      7  1.1  jtc  */
      8  1.1  jtc #include <sys/types.h>
      9  1.1  jtc #include <stdio.h>
     10  1.1  jtc #include <stdlib.h>
     11  1.1  jtc #include <string.h>
     12  1.1  jtc #include <limits.h>
     13  1.1  jtc #include <ctype.h>
     14  1.1  jtc #include <regex.h>
     15  1.1  jtc 
     16  1.1  jtc #include "utils.h"
     17  1.1  jtc #include "regex2.h"
     18  1.1  jtc 
     19  1.1  jtc static int nope = 0;		/* for use in asserts; shuts lint up */
     20  1.1  jtc 
     21  1.1  jtc /* macros for manipulating states, small version */
     22  1.1  jtc #define	states	long
     23  1.1  jtc #define	states1	states		/* for later use in regexec() decision */
     24  1.1  jtc #define	CLEAR(v)	((v) = 0)
     25  1.1  jtc #define	SET0(v, n)	((v) &= ~(1 << (n)))
     26  1.1  jtc #define	SET1(v, n)	((v) |= 1 << (n))
     27  1.1  jtc #define	ISSET(v, n)	((v) & (1 << (n)))
     28  1.1  jtc #define	ASSIGN(d, s)	((d) = (s))
     29  1.1  jtc #define	EQ(a, b)	((a) == (b))
     30  1.1  jtc #define	STATEVARS	int dummy	/* dummy version */
     31  1.1  jtc #define	STATESETUP(m, n)	/* nothing */
     32  1.1  jtc #define	STATETEARDOWN(m)	/* nothing */
     33  1.1  jtc #define	SETUP(v)	((v) = 0)
     34  1.1  jtc #define	onestate	int
     35  1.1  jtc #define	INIT(o, n)	((o) = (unsigned)1 << (n))
     36  1.1  jtc #define	INC(o)	((o) <<= 1)
     37  1.1  jtc #define	ISSTATEIN(v, o)	((v) & (o))
     38  1.1  jtc /* some abbreviations; note that some of these know variable names! */
     39  1.1  jtc /* do "if I'm here, I can also be there" etc without branches */
     40  1.1  jtc #define	FWD(dst, src, n)	((dst) |= ((unsigned)(src)&(here)) << (n))
     41  1.1  jtc #define	BACK(dst, src, n)	((dst) |= ((unsigned)(src)&(here)) >> (n))
     42  1.1  jtc #define	ISSETBACK(v, n)	((v) & ((unsigned)here >> (n)))
     43  1.1  jtc /* function names */
     44  1.1  jtc #define SNAMES			/* engine.c looks after details */
     45  1.1  jtc 
     46  1.1  jtc #include "engine.c"
     47  1.1  jtc 
     48  1.1  jtc /* now undo things */
     49  1.1  jtc #undef	states
     50  1.1  jtc #undef	CLEAR
     51  1.1  jtc #undef	SET0
     52  1.1  jtc #undef	SET1
     53  1.1  jtc #undef	ISSET
     54  1.1  jtc #undef	ASSIGN
     55  1.1  jtc #undef	EQ
     56  1.1  jtc #undef	STATEVARS
     57  1.1  jtc #undef	STATESETUP
     58  1.1  jtc #undef	STATETEARDOWN
     59  1.1  jtc #undef	SETUP
     60  1.1  jtc #undef	onestate
     61  1.1  jtc #undef	INIT
     62  1.1  jtc #undef	INC
     63  1.1  jtc #undef	ISSTATEIN
     64  1.1  jtc #undef	FWD
     65  1.1  jtc #undef	BACK
     66  1.1  jtc #undef	ISSETBACK
     67  1.1  jtc #undef	SNAMES
     68  1.1  jtc 
     69  1.1  jtc /* macros for manipulating states, large version */
     70  1.1  jtc #define	states	char *
     71  1.1  jtc #define	CLEAR(v)	memset(v, 0, m->g->nstates)
     72  1.1  jtc #define	SET0(v, n)	((v)[n] = 0)
     73  1.1  jtc #define	SET1(v, n)	((v)[n] = 1)
     74  1.1  jtc #define	ISSET(v, n)	((v)[n])
     75  1.1  jtc #define	ASSIGN(d, s)	memcpy(d, s, m->g->nstates)
     76  1.1  jtc #define	EQ(a, b)	(memcmp(a, b, m->g->nstates) == 0)
     77  1.1  jtc #define	STATEVARS	int vn; char *space
     78  1.1  jtc #define	STATESETUP(m, nv)	{ (m)->space = malloc((nv)*(m)->g->nstates); \
     79  1.1  jtc 				if ((m)->space == NULL) return(REG_ESPACE); \
     80  1.1  jtc 				(m)->vn = 0; }
     81  1.1  jtc #define	STATETEARDOWN(m)	{ free((m)->space); }
     82  1.1  jtc #define	SETUP(v)	((v) = &m->space[m->vn++ * m->g->nstates])
     83  1.1  jtc #define	onestate	int
     84  1.1  jtc #define	INIT(o, n)	((o) = (n))
     85  1.1  jtc #define	INC(o)	((o)++)
     86  1.1  jtc #define	ISSTATEIN(v, o)	((v)[o])
     87  1.1  jtc /* some abbreviations; note that some of these know variable names! */
     88  1.1  jtc /* do "if I'm here, I can also be there" etc without branches */
     89  1.1  jtc #define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
     90  1.1  jtc #define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
     91  1.1  jtc #define	ISSETBACK(v, n)	((v)[here - (n)])
     92  1.1  jtc /* function names */
     93  1.1  jtc #define	LNAMES			/* flag */
     94  1.1  jtc 
     95  1.1  jtc #include "engine.c"
     96  1.1  jtc 
     97  1.1  jtc /*
     98  1.1  jtc  - regexec - interface for matching
     99  1.1  jtc  = extern int regexec(const regex_t *preg, const char *string, size_t nmatch, \
    100  1.1  jtc  =					regmatch_t pmatch[], int eflags);
    101  1.1  jtc  = #define	REG_NOTBOL	00001
    102  1.1  jtc  = #define	REG_NOTEOL	00002
    103  1.1  jtc  = #define	REG_STARTEND	00004
    104  1.1  jtc  = #define	REG_TRACE	00400	// tracing of execution
    105  1.1  jtc  = #define	REG_LARGE	01000	// force large representation
    106  1.1  jtc  = #define	REG_BACKR	02000	// force use of backref code
    107  1.1  jtc  *
    108  1.1  jtc  * We put this here so we can exploit knowledge of the state representation
    109  1.1  jtc  * when choosing which matcher to call.  Also, by this point the matchers
    110  1.1  jtc  * have been prototyped.
    111  1.1  jtc  */
    112  1.1  jtc int				/* 0 success, REG_NOMATCH failure */
    113  1.1  jtc regexec(preg, string, nmatch, pmatch, eflags)
    114  1.1  jtc const regex_t *preg;
    115  1.1  jtc const char *string;
    116  1.1  jtc size_t nmatch;
    117  1.1  jtc regmatch_t pmatch[];
    118  1.1  jtc int eflags;
    119  1.1  jtc {
    120  1.1  jtc 	register struct re_guts *g = preg->re_g;
    121  1.1  jtc #ifdef REDEBUG
    122  1.1  jtc #	define	GOODFLAGS(f)	(f)
    123  1.1  jtc #else
    124  1.1  jtc #	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
    125  1.1  jtc #endif
    126  1.1  jtc 
    127  1.1  jtc 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
    128  1.1  jtc 		return(REG_BADPAT);
    129  1.1  jtc 	assert(!(g->iflags&BAD));
    130  1.1  jtc 	if (g->iflags&BAD)		/* backstop for no-debug case */
    131  1.1  jtc 		return(REG_BADPAT);
    132  1.1  jtc 	if (eflags != GOODFLAGS(eflags))
    133  1.1  jtc 		return(REG_INVARG);
    134  1.1  jtc 
    135  1.1  jtc 	if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
    136  1.1  jtc 		return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
    137  1.1  jtc 	else
    138  1.1  jtc 		return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
    139  1.1  jtc }
    140