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