1 /* $NetBSD: regexec.c,v 1.5 2014/01/26 21:47:00 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 #include <sys/cdefs.h> 42 #if 0 43 #if defined(LIBC_SCCS) && !defined(lint) 44 static char sccsid[] = "@(#)regexec.c 8.2 (Berkeley) 3/16/94"; 45 #endif /* LIBC_SCCS and not lint */ 46 #else 47 __RCSID("$NetBSD: regexec.c,v 1.5 2014/01/26 21:47:00 christos Exp $"); 48 #endif 49 50 /* 51 * the outer shell of regexec() 52 * 53 * This file includes engine.c *twice*, after muchos fiddling with the 54 * macros that code uses. This lets the same code operate on two different 55 * representations for state sets. 56 */ 57 #include <sys/types.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <limits.h> 62 #include <ctype.h> 63 #include <regex.h> 64 65 #include "utils.h" 66 #include "regex2.h" 67 68 #ifdef notdef 69 static int nope = 0; /* for use in asserts; shuts lint up */ 70 #endif 71 72 /* macros for manipulating states, small version */ 73 #define states int 74 #define states1 int /* for later use in regexec() decision */ 75 #define CLEAR(v) ((v) = 0) 76 #define SET0(v, n) ((v) &= ~(1 << (n))) 77 #define SET1(v, n) ((v) |= 1 << (n)) 78 #define ISSET(v, n) ((v) & (1 << (n))) 79 #define ASSIGN(d, s) ((d) = (s)) 80 #define EQ(a, b) ((a) == (b)) 81 #define STATEVARS int dummy /* dummy version */ 82 #define STATESETUP(m, n) /* nothing */ 83 #define STATETEARDOWN(m) /* nothing */ 84 #define SETUP(v) ((v) = 0) 85 #define onestate int 86 #define INIT(o, n) ((o) = (unsigned)1 << (n)) 87 #define INC(o) ((o) <<= 1) 88 #define ISSTATEIN(v, o) ((v) & (o)) 89 /* some abbreviations; note that some of these know variable names! */ 90 /* do "if I'm here, I can also be there" etc without branches */ 91 #define FWD(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) << (n)) 92 #define BACK(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) >> (n)) 93 #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n))) 94 /* function names */ 95 #define SNAMES /* engine.c looks after details */ 96 97 #include "engine.c" 98 99 /* now undo things */ 100 #undef states 101 #undef CLEAR 102 #undef SET0 103 #undef SET1 104 #undef ISSET 105 #undef ASSIGN 106 #undef EQ 107 #undef STATEVARS 108 #undef STATESETUP 109 #undef STATETEARDOWN 110 #undef SETUP 111 #undef onestate 112 #undef INIT 113 #undef INC 114 #undef ISSTATEIN 115 #undef FWD 116 #undef BACK 117 #undef ISSETBACK 118 #undef SNAMES 119 120 /* macros for manipulating states, large version */ 121 #define states char * 122 #define CLEAR(v) memset(v, 0, m->g->nstates) 123 #define SET0(v, n) ((v)[n] = 0) 124 #define SET1(v, n) ((v)[n] = 1) 125 #define ISSET(v, n) ((v)[n]) 126 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates) 127 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0) 128 #define STATEVARS int vn; char *space 129 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \ 130 if ((m)->space == NULL) return(REG_ESPACE); \ 131 (m)->vn = 0; } 132 #define STATETEARDOWN(m) { free((m)->space); } 133 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates]) 134 #define onestate int 135 #define INIT(o, n) ((o) = (n)) 136 #define INC(o) ((o)++) 137 #define ISSTATEIN(v, o) ((v)[o]) 138 /* some abbreviations; note that some of these know variable names! */ 139 /* do "if I'm here, I can also be there" etc without branches */ 140 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here]) 141 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here]) 142 #define ISSETBACK(v, n) ((v)[here - (n)]) 143 /* function names */ 144 #define LNAMES /* flag */ 145 146 #include "engine.c" 147 148 /* 149 - regexec - interface for matching 150 = extern int regexec(const regex_t *, const char *, size_t, \ 151 = regmatch_t [], int); 152 = #define REG_NOTBOL 00001 153 = #define REG_NOTEOL 00002 154 = #define REG_STARTEND 00004 155 = #define REG_TRACE 00400 // tracing of execution 156 = #define REG_LARGE 01000 // force large representation 157 = #define REG_BACKR 02000 // force use of backref code 158 * 159 * We put this here so we can exploit knowledge of the state representation 160 * when choosing which matcher to call. Also, by this point the matchers 161 * have been prototyped. 162 */ 163 int /* 0 success, REG_NOMATCH failure */ 164 regexec(const regex_t *preg, const RCHAR_T *string, size_t nmatch, regmatch_t *pmatch, int eflags) 165 { 166 struct re_guts *g = preg->re_g; 167 #ifdef REDEBUG 168 # define GOODFLAGS(f) (f) 169 #else 170 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND)) 171 #endif 172 173 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2) 174 return(REG_BADPAT); 175 assert(!(g->iflags&BAD)); 176 if (g->iflags&BAD) /* backstop for no-debug case */ 177 return(REG_BADPAT); 178 eflags = GOODFLAGS(eflags); 179 180 if (g->nstates <= (int)(CHAR_BIT*sizeof(states1)) && !(eflags®_LARGE)) 181 return(smatcher(g, (RCHAR_T *)__UNCONST(string), nmatch, pmatch, eflags)); 182 else 183 return(lmatcher(g, (RCHAR_T *)__UNCONST(string), nmatch, pmatch, eflags)); 184 } 185