1/*- 2 * This code is derived from OpenBSD's libc/regex, original license follows: 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. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)regexec.c 8.3 (Berkeley) 3/20/94 36 */ 37 38/* 39 * the outer shell of llvm_regexec() 40 * 41 * This file includes engine.inc *twice*, after muchos fiddling with the 42 * macros that code uses. This lets the same code operate on two different 43 * representations for state sets. 44 */ 45#include <sys/types.h> 46#include <stdio.h> 47#include <stdlib.h> 48#include <string.h> 49#include <limits.h> 50#include <ctype.h> 51#include "regex_impl.h" 52 53#include "regutils.h" 54#include "regex2.h" 55 56/* macros for manipulating states, small version */ 57/* FIXME: 'states' is assumed as 'long' on small version. */ 58#define states1 long /* for later use in llvm_regexec() decision */ 59#define states states1 60#define CLEAR(v) ((v) = 0) 61#define SET0(v, n) ((v) &= ~((unsigned long)1 << (n))) 62#define SET1(v, n) ((v) |= (unsigned long)1 << (n)) 63#define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0) 64#define ASSIGN(d, s) ((d) = (s)) 65#define EQ(a, b) ((a) == (b)) 66#define STATEVARS long dummy /* dummy version */ 67#define STATESETUP(m, n) /* nothing */ 68#define STATETEARDOWN(m) /* nothing */ 69#define SETUP(v) ((v) = 0) 70#define onestate long 71#define INIT(o, n) ((o) = (unsigned long)1 << (n)) 72#define INC(o) ((o) = (unsigned long)(o) << 1) 73#define ISSTATEIN(v, o) (((v) & (o)) != 0) 74/* some abbreviations; note that some of these know variable names! */ 75/* do "if I'm here, I can also be there" etc without branches */ 76#define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n)) 77#define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n)) 78#define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0) 79/* function names */ 80#define SNAMES /* engine.inc looks after details */ 81 82#include "regengine.inc" 83 84/* now undo things */ 85#undef states 86#undef CLEAR 87#undef SET0 88#undef SET1 89#undef ISSET 90#undef ASSIGN 91#undef EQ 92#undef STATEVARS 93#undef STATESETUP 94#undef STATETEARDOWN 95#undef SETUP 96#undef onestate 97#undef INIT 98#undef INC 99#undef ISSTATEIN 100#undef FWD 101#undef BACK 102#undef ISSETBACK 103#undef SNAMES 104 105/* macros for manipulating states, large version */ 106#define states char * 107#define CLEAR(v) memset(v, 0, m->g->nstates) 108#define SET0(v, n) ((v)[n] = 0) 109#define SET1(v, n) ((v)[n] = 1) 110#define ISSET(v, n) ((v)[n]) 111#define ASSIGN(d, s) memmove(d, s, m->g->nstates) 112#define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0) 113#define STATEVARS long vn; char *space 114#define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \ 115 if ((m)->space == NULL) return(REG_ESPACE); \ 116 (m)->vn = 0; } 117#define STATETEARDOWN(m) { free((m)->space); } 118#define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates]) 119#define onestate long 120#define INIT(o, n) ((o) = (n)) 121#define INC(o) ((o)++) 122#define ISSTATEIN(v, o) ((v)[o]) 123/* some abbreviations; note that some of these know variable names! */ 124/* do "if I'm here, I can also be there" etc without branches */ 125#define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here]) 126#define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here]) 127#define ISSETBACK(v, n) ((v)[here - (n)]) 128/* function names */ 129#define LNAMES /* flag */ 130 131#include "regengine.inc" 132 133/* 134 - llvm_regexec - interface for matching 135 * 136 * We put this here so we can exploit knowledge of the state representation 137 * when choosing which matcher to call. Also, by this point the matchers 138 * have been prototyped. 139 */ 140int /* 0 success, REG_NOMATCH failure */ 141llvm_regexec(const llvm_regex_t *preg, const char *string, size_t nmatch, 142 llvm_regmatch_t pmatch[], int eflags) 143{ 144 struct re_guts *g = preg->re_g; 145#ifdef REDEBUG 146# define GOODFLAGS(f) (f) 147#else 148# define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND)) 149#endif 150 151 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2) 152 return(REG_BADPAT); 153 assert(!(g->iflags®EX_BAD)); 154 if (g->iflags®EX_BAD) /* backstop for no-debug case */ 155 return(REG_BADPAT); 156 eflags = GOODFLAGS(eflags); 157 158 if (g->nstates <= (long)(CHAR_BIT*sizeof(states1)) && !(eflags®_LARGE)) 159 return(smatcher(g, string, nmatch, pmatch, eflags)); 160 else 161 return(lmatcher(g, string, nmatch, pmatch, eflags)); 162} 163