regexec.c 5.9 KB
Newer Older
1 2 3
/*-
 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
 * Copyright (c) 1992, 1993, 1994
4
 *		The Regents of the University of California.  All rights reserved.
5 6 7 8 9 10 11 12
 *
 * This code is derived from software contributed to Berkeley by
 * Henry Spencer.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
13
 *	  notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15 16
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
17
 * 3. All advertising materials mentioning features or use of this software
18 19 20
 *	  must display the following acknowledgement:
 *		This product includes software developed by the University of
 *		California, Berkeley and its contributors.
21
 * 4. Neither the name of the University nor the names of its contributors
22 23
 *	  may be used to endorse or promote products derived from this software
 *	  without specific prior written permission.
24 25 26 27
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28
 * ARE DISCLAIMED.	IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 30 31 32 33 34 35 36
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
37
 *		@(#)regexec.c	8.3 (Berkeley) 3/20/94
38 39
 */

40 41
#include "postgres.h"

42 43 44 45 46 47 48 49 50
/*
 * the outer shell of regexec()
 *
 * This file includes engine.c *twice*, after muchos fiddling with the
 * macros that code uses.  This lets the same code operate on two different
 * representations for state sets.
 */
#include <limits.h>
#include <ctype.h>
B
Bruce Momjian 已提交
51
#include <assert.h>
52

53 54 55
#include "regex/regex.h"
#include "regex/utils.h"
#include "regex/regex2.h"
56

57
static int	nope = 0;			/* for use in asserts; shuts lint up */
58 59

/* macros for manipulating states, small version */
60 61 62
#define states	long
#define states1 states			/* for later use in regexec() decision */
#define CLEAR(v)		((v) = 0)
63 64 65
#define SET0(v, n)		((v) &= ~(1L << (n)))
#define SET1(v, n)		((v) |= (1L << (n)))
#define ISSET(v, n)		((v) & (1L << (n)))
66 67 68 69 70 71
#define ASSIGN(d, s)	((d) = (s))
#define EQ(a, b)		((a) == (b))
#define STATEVARS		int dummy		/* dummy version */
#define STATESETUP(m, n)		/* nothing */
#define STATETEARDOWN(m)		/* nothing */
#define SETUP(v)		((v) = 0)
72 73 74
#define onestate		long
#define INIT(o, n)		((o) = (1L << (n)))
#define INC(o)			((o) <<= 1)
B
Bruce Momjian 已提交
75
#define ISSTATEIN(v, o) ((v) & (o))
76 77
/* some abbreviations; note that some of these know variable names! */
/* do "if I'm here, I can also be there" etc without branches */
78 79 80
#define FWD(dst, src, n)		((dst) |= ((src) & (here)) << (n))
#define BACK(dst, src, n)		((dst) |= ((src) & (here)) >> (n))
#define ISSETBACK(v, n) ((v) & (here >> (n)))
81
/* function names */
82
#define SNAMES					/* engine.c looks after details */
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

#include "engine.c"

/* now undo things */
#undef	states
#undef	CLEAR
#undef	SET0
#undef	SET1
#undef	ISSET
#undef	ASSIGN
#undef	EQ
#undef	STATEVARS
#undef	STATESETUP
#undef	STATETEARDOWN
#undef	SETUP
#undef	onestate
#undef	INIT
#undef	INC
#undef	ISSTATEIN
#undef	FWD
#undef	BACK
#undef	ISSETBACK
#undef	SNAMES

/* macros for manipulating states, large version */
108 109 110 111 112 113 114 115
#define states	char *
#define CLEAR(v)		memset(v, 0, m->g->nstates)
#define SET0(v, n)		((v)[n] = 0)
#define SET1(v, n)		((v)[n] = 1)
#define ISSET(v, n)		((v)[n])
#define ASSIGN(d, s)	memcpy(d, s, m->g->nstates)
#define EQ(a, b)		(memcmp(a, b, m->g->nstates) == 0)
#define STATEVARS		int vn; char *space
116 117 118 119 120 121 122 123 124 125 126 127
#define STATESETUP(m, nv) \
do { \
	(m)->space = malloc((nv)*(m)->g->nstates); \
	if ((m)->space == NULL) \
		return(REG_ESPACE); \
	(m)->vn = 0; \
} while (0)

#define STATETEARDOWN(m) \
do { \
	free((m)->space); \
} while (0)
128

129 130 131
#define SETUP(v)		((v) = &m->space[m->vn++ * m->g->nstates])
#define onestate		int
#define INIT(o, n)		((o) = (n))
132
#define INC(o)			((o)++)
133
#define ISSTATEIN(v, o) ((v)[o])
134 135
/* some abbreviations; note that some of these know variable names! */
/* do "if I'm here, I can also be there" etc without branches */
136 137 138
#define FWD(dst, src, n)		((dst)[here+(n)] |= (src)[here])
#define BACK(dst, src, n)		((dst)[here-(n)] |= (src)[here])
#define ISSETBACK(v, n) ((v)[here - (n)])
139
/* function names */
140
#define LNAMES					/* flag */
141 142 143 144

#include "engine.c"

/*
145
 * regexec - interface for matching
146 147
 *
 * We put this here so we can exploit knowledge of the state representation
148
 * when choosing which matcher to call.
149
 */
150
int								/* 0 success, REG_NOMATCH failure */
151 152
pg_regexec(const regex_t *preg, const char *string, size_t nmatch,
		   regmatch_t *pmatch, int eflags)
153
{
154
	struct re_guts *g = preg->re_g;
155

156 157
	pg_wchar   *str;
	int			sts;
M
Marc G. Fournier 已提交
158

159
#ifdef REDEBUG
160
#define  GOODFLAGS(f)	 (f)
161
#else
162
#define  GOODFLAGS(f)	 ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
163 164 165
#endif

	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
166
		return REG_BADPAT;
167 168
	assert(!(g->iflags & BAD));
	if (g->iflags & BAD)		/* backstop for no-debug case */
169
		return REG_BADPAT;
170 171
	eflags = GOODFLAGS(eflags);

172 173 174 175
	str = (pg_wchar *) malloc((strlen(string) + 1) * sizeof(pg_wchar));
	if (!str)
		return (REG_ESPACE);
	(void) pg_mb2wchar((unsigned char *) string, str);
176
	if (g->nstates <= CHAR_BIT * sizeof(states1) && !(eflags & REG_LARGE))
177
		sts = smatcher(g, str, nmatch, pmatch, eflags);
178
	else
179 180 181
		sts = lmatcher(g, str, nmatch, pmatch, eflags);
	free((char *) str);
	return (sts);
182
}