brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.7 KiB · bd5e72d Raw
163 lines · c
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, 19946 *	The Regents of the University of California.  All rights reserved.7 *8 * This code is derived from software contributed to Berkeley by9 * Henry Spencer.10 *11 * Redistribution and use in source and binary forms, with or without12 * modification, are permitted provided that the following conditions13 * are met:14 * 1. Redistributions of source code must retain the above copyright15 *    notice, this list of conditions and the following disclaimer.16 * 2. Redistributions in binary form must reproduce the above copyright17 *    notice, this list of conditions and the following disclaimer in the18 *    documentation and/or other materials provided with the distribution.19 * 3. Neither the name of the University nor the names of its contributors20 *    may be used to endorse or promote products derived from this software21 *    without specific prior written permission.22 *23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF33 * SUCH DAMAGE.34 *35 *	@(#)regexec.c	8.3 (Berkeley) 3/20/9436 */37 38/*39 * the outer shell of llvm_regexec()40 *41 * This file includes engine.inc *twice*, after muchos fiddling with the42 * macros that code uses.  This lets the same code operate on two different43 * 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	states160#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	long71#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	states86#undef	CLEAR87#undef	SET088#undef	SET189#undef	ISSET90#undef	ASSIGN91#undef	EQ92#undef	STATEVARS93#undef	STATESETUP94#undef	STATETEARDOWN95#undef	SETUP96#undef	onestate97#undef	INIT98#undef	INC99#undef	ISSTATEIN100#undef	FWD101#undef	BACK102#undef	ISSETBACK103#undef	SNAMES104 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 *space114#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	long120#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 matching135 *136 * We put this here so we can exploit knowledge of the state representation137 * when choosing which matcher to call.  Also, by this point the matchers138 * 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 REDEBUG146#	define	GOODFLAGS(f)	(f)147#else148#	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))149#endif150 151	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)152		return(REG_BADPAT);153	assert(!(g->iflags&REGEX_BAD));154	if (g->iflags&REGEX_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&REG_LARGE))159		return(smatcher(g, string, nmatch, pmatch, eflags));160	else161		return(lmatcher(g, string, nmatch, pmatch, eflags));162}163