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