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