1 /* $NetBSD: regexec.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */ 2 /*- 3 * Copyright (c) 1992, 1993, 1994 Henry Spencer. 4 * Copyright (c) 1992, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Henry Spencer of the University of Toronto. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)regexec.c 8.2 (Berkeley) 3/16/94 39 */ 40 41 #if defined(LIBC_SCCS) && !defined(lint) 42 static char sccsid[] = "@(#)regexec.c 8.2 (Berkeley) 3/16/94"; 43 #endif /* LIBC_SCCS and not lint */ 44 45 /* 46 * the outer shell of regexec() 47 * 48 * This file includes engine.c *twice*, after muchos fiddling with the 49 * macros that code uses. This lets the same code operate on two different 50 * representations for state sets. 51 */ 52 #include <sys/types.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <limits.h> 57 #include <ctype.h> 58 #include <regex.h> 59 60 #include "utils.h" 61 #include "regex2.h" 62 63 #ifdef notdef 64 static int nope = 0; /* for use in asserts; shuts lint up */ 65 #endif 66 67 /* macros for manipulating states, small version */ 68 #define states int 69 #define states1 int /* for later use in regexec() decision */ 70 #define CLEAR(v) ((v) = 0) 71 #define SET0(v, n) ((v) &= ~(1 << (n))) 72 #define SET1(v, n) ((v) |= 1 << (n)) 73 #define ISSET(v, n) ((v) & (1 << (n))) 74 #define ASSIGN(d, s) ((d) = (s)) 75 #define EQ(a, b) ((a) == (b)) 76 #define STATEVARS int dummy /* dummy version */ 77 #define STATESETUP(m, n) /* nothing */ 78 #define STATETEARDOWN(m) /* nothing */ 79 #define SETUP(v) ((v) = 0) 80 #define onestate int 81 #define INIT(o, n) ((o) = (unsigned)1 << (n)) 82 #define INC(o) ((o) <<= 1) 83 #define ISSTATEIN(v, o) ((v) & (o)) 84 /* some abbreviations; note that some of these know variable names! */ 85 /* do "if I'm here, I can also be there" etc without branches */ 86 #define FWD(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) << (n)) 87 #define BACK(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) >> (n)) 88 #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n))) 89 /* function names */ 90 #define SNAMES /* engine.c looks after details */ 91 92 #include "engine.c" 93 94 /* now undo things */ 95 #undef states 96 #undef CLEAR 97 #undef SET0 98 #undef SET1 99 #undef ISSET 100 #undef ASSIGN 101 #undef EQ 102 #undef STATEVARS 103 #undef STATESETUP 104 #undef STATETEARDOWN 105 #undef SETUP 106 #undef onestate 107 #undef INIT 108 #undef INC 109 #undef ISSTATEIN 110 #undef FWD 111 #undef BACK 112 #undef ISSETBACK 113 #undef SNAMES 114 115 /* macros for manipulating states, large version */ 116 #define states char * 117 #define CLEAR(v) memset(v, 0, m->g->nstates) 118 #define SET0(v, n) ((v)[n] = 0) 119 #define SET1(v, n) ((v)[n] = 1) 120 #define ISSET(v, n) ((v)[n]) 121 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates) 122 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0) 123 #define STATEVARS int vn; char *space 124 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \ 125 if ((m)->space == NULL) return(REG_ESPACE); \ 126 (m)->vn = 0; } 127 #define STATETEARDOWN(m) { free((m)->space); } 128 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates]) 129 #define onestate int 130 #define INIT(o, n) ((o) = (n)) 131 #define INC(o) ((o)++) 132 #define ISSTATEIN(v, o) ((v)[o]) 133 /* some abbreviations; note that some of these know variable names! */ 134 /* do "if I'm here, I can also be there" etc without branches */ 135 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here]) 136 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here]) 137 #define ISSETBACK(v, n) ((v)[here - (n)]) 138 /* function names */ 139 #define LNAMES /* flag */ 140 141 #include "engine.c" 142 143 /* 144 - regexec - interface for matching 145 = extern int regexec(const regex_t *, const char *, size_t, \ 146 = regmatch_t [], int); 147 = #define REG_NOTBOL 00001 148 = #define REG_NOTEOL 00002 149 = #define REG_STARTEND 00004 150 = #define REG_TRACE 00400 // tracing of execution 151 = #define REG_LARGE 01000 // force large representation 152 = #define REG_BACKR 02000 // force use of backref code 153 * 154 * We put this here so we can exploit knowledge of the state representation 155 * when choosing which matcher to call. Also, by this point the matchers 156 * have been prototyped. 157 */ 158 int /* 0 success, REG_NOMATCH failure */ 159 regexec(const regex_t *preg, const RCHAR_T *string, size_t nmatch, regmatch_t *pmatch, int eflags) 160 { 161 register struct re_guts *g = preg->re_g; 162 #ifdef REDEBUG 163 # define GOODFLAGS(f) (f) 164 #else 165 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND)) 166 #endif 167 168 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2) 169 return(REG_BADPAT); 170 assert(!(g->iflags&BAD)); 171 if (g->iflags&BAD) /* backstop for no-debug case */ 172 return(REG_BADPAT); 173 eflags = GOODFLAGS(eflags); 174 175 if (g->nstates <= (int)(CHAR_BIT*sizeof(states1)) && !(eflags®_LARGE)) 176 return(smatcher(g, (RCHAR_T *)__UNCONST(string), nmatch, pmatch, eflags)); 177 else 178 return(lmatcher(g, (RCHAR_T *)__UNCONST(string), nmatch, pmatch, eflags)); 179 } 180