1*55851Sbostic /*- 2*55851Sbostic * Copyright (c) 1992 Henry Spencer. 3*55851Sbostic * Copyright (c) 1992 The Regents of the University of California. 4*55851Sbostic * All rights reserved. 5*55851Sbostic * 6*55851Sbostic * This code is derived from software contributed to Berkeley by 7*55851Sbostic * Henry Spencer of the University of Toronto. 8*55851Sbostic * 9*55851Sbostic * %sccs.include.redist.c% 10*55851Sbostic * 11*55851Sbostic * @(#)regexec.c 5.1 (Berkeley) 08/06/92 12*55851Sbostic */ 13*55851Sbostic 14*55851Sbostic #if defined(LIBC_SCCS) && !defined(lint) 15*55851Sbostic static char sccsid[] = "@(#)regexec.c 5.1 (Berkeley) 08/06/92"; 16*55851Sbostic #endif /* LIBC_SCCS and not lint */ 17*55851Sbostic 18*55851Sbostic #include <sys/types.h> 19*55851Sbostic 20*55851Sbostic #include <stdio.h> 21*55851Sbostic #include <stdlib.h> 22*55851Sbostic #include <string.h> 23*55851Sbostic #include <assert.h> 24*55851Sbostic #include <limits.h> 25*55851Sbostic #include <regex.h> 26*55851Sbostic 27*55851Sbostic #include "utils.h" 28*55851Sbostic #include "regex2.h" 29*55851Sbostic 30*55851Sbostic /* 31*55851Sbostic - regexec - interface for matching 32*55851Sbostic */ 33*55851Sbostic int /* 0 success, REG_NOMATCH failure */ 34*55851Sbostic regexec(preg, string, nmatch, pmatch, eflags) 35*55851Sbostic const regex_t *preg; 36*55851Sbostic const char *string; 37*55851Sbostic size_t nmatch; 38*55851Sbostic regmatch_t pmatch[]; 39*55851Sbostic int eflags; 40*55851Sbostic { 41*55851Sbostic register struct re_guts *g = preg->re_g; 42*55851Sbostic STATIC int smatcher(); 43*55851Sbostic STATIC int lmatcher(); 44*55851Sbostic #ifndef NDEBUG 45*55851Sbostic # define GOODFLAGS(f) (f) 46*55851Sbostic #else 47*55851Sbostic # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND)) 48*55851Sbostic #endif 49*55851Sbostic 50*55851Sbostic if (preg->re_magic != MAGIC1 || g->magic != MAGIC2) 51*55851Sbostic return(REG_BADPAT); 52*55851Sbostic assert(!(g->iflags&BAD)); 53*55851Sbostic if (g->iflags&BAD) /* backstop for NDEBUG case */ 54*55851Sbostic return(REG_BADPAT); 55*55851Sbostic eflags = GOODFLAGS(eflags); /* xxx should we complain? */ 56*55851Sbostic 57*55851Sbostic if (g->nstates <= 32 && !(eflags®_LARGE)) 58*55851Sbostic return(smatcher(g, string, nmatch, pmatch, eflags)); 59*55851Sbostic else 60*55851Sbostic return(lmatcher(g, string, nmatch, pmatch, eflags)); 61*55851Sbostic } 62*55851Sbostic 63*55851Sbostic /* macros for manipulating states, small version */ 64*55851Sbostic #define states long 65*55851Sbostic #define CLEAR(v) ((v) = 0) 66*55851Sbostic #define SET0(v, n) ((v) &= ~(1 << (n))) 67*55851Sbostic #define SET1(v, n) ((v) |= 1 << (n)) 68*55851Sbostic #define ISSET(v, n) ((v) & (1 << (n))) 69*55851Sbostic #define ASSIGN(d, s) ((d) = (s)) 70*55851Sbostic #define EQ(a, b) ((a) == (b)) 71*55851Sbostic #define STATEVARS int dummy /* dummy version */ 72*55851Sbostic #define STATESETUP(m, n) /* nothing */ 73*55851Sbostic #define STATETEARDOWN(m) /* nothing */ 74*55851Sbostic #define SETUP(v) /* nothing */ 75*55851Sbostic #define onestate int 76*55851Sbostic #define INIT(o, n) ((o) = 1 << (n)) 77*55851Sbostic #define INC(o) ((o) <<= 1) 78*55851Sbostic #define ISSTATEIN(v, o) ((v) & (o)) 79*55851Sbostic /* some abbreviations; note that some of these know variable names! */ 80*55851Sbostic /* do "if I'm here, I can also be there" etc without branches */ 81*55851Sbostic #define FWD(dst, src, n) ((dst) |= ((src)&(here)) << (n)) 82*55851Sbostic #define BACK(dst, src, n) ((dst) |= ((src)&(here)) >> (n)) 83*55851Sbostic #define ISSETBACK(v, n) ((v) & (here >> (n))) 84*55851Sbostic /* function names */ 85*55851Sbostic #define SNAMES /* engine.c looks after details */ 86*55851Sbostic 87*55851Sbostic #include "engine.c" 88*55851Sbostic 89*55851Sbostic /* now undo things */ 90*55851Sbostic #undef states 91*55851Sbostic #undef CLEAR 92*55851Sbostic #undef SET0 93*55851Sbostic #undef SET1 94*55851Sbostic #undef ISSET 95*55851Sbostic #undef ASSIGN 96*55851Sbostic #undef EQ 97*55851Sbostic #undef STATEVARS 98*55851Sbostic #undef STATESETUP 99*55851Sbostic #undef STATETEARDOWN 100*55851Sbostic #undef SETUP 101*55851Sbostic #undef onestate 102*55851Sbostic #undef INIT 103*55851Sbostic #undef INC 104*55851Sbostic #undef ISSTATEIN 105*55851Sbostic #undef FWD 106*55851Sbostic #undef BACK 107*55851Sbostic #undef ISSETBACK 108*55851Sbostic #undef SNAMES 109*55851Sbostic 110*55851Sbostic /* macros for manipulating states, large version */ 111*55851Sbostic #define states char * 112*55851Sbostic #define CLEAR(v) memset(v, 0, m->g->nstates) 113*55851Sbostic #define SET0(v, n) ((v)[n] = 0) 114*55851Sbostic #define SET1(v, n) ((v)[n] = 1) 115*55851Sbostic #define ISSET(v, n) ((v)[n]) 116*55851Sbostic #define ASSIGN(d, s) memcpy(d, s, m->g->nstates) 117*55851Sbostic #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0) 118*55851Sbostic #define STATEVARS int vn; char *space 119*55851Sbostic #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \ 120*55851Sbostic if ((m)->space == NULL) return(REG_ESPACE); \ 121*55851Sbostic (m)->vn = 0; } 122*55851Sbostic #define STATETEARDOWN(m) { free((m)->space); } 123*55851Sbostic #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates]) 124*55851Sbostic #define onestate int 125*55851Sbostic #define INIT(o, n) ((o) = (n)) 126*55851Sbostic #define INC(o) ((o)++) 127*55851Sbostic #define ISSTATEIN(v, o) ((v)[o]) 128*55851Sbostic /* some abbreviations; note that some of these know variable names! */ 129*55851Sbostic /* do "if I'm here, I can also be there" etc without branches */ 130*55851Sbostic #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here]) 131*55851Sbostic #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here]) 132*55851Sbostic #define ISSETBACK(v, n) ((v)[here - (n)]) 133*55851Sbostic /* function names */ 134*55851Sbostic #define LNAMES /* flag */ 135*55851Sbostic 136*55851Sbostic #include "engine.c" 137