MFC CAM fixes for the 2.0 release.
[dragonfly.git] / lib / libc / regex / regexec.c
blobd7567fc98330cdeff701273abe6346991cb0c5db
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.
6 * This code is derived from software contributed to Berkeley by
7 * Henry Spencer.
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.
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.
37 * @(#)regexec.c 8.3 (Berkeley) 3/20/94
39 * @(#)regexec.c 8.3 (Berkeley) 3/20/94
41 * $DragonFly: src/lib/libc/regex/regexec.c,v 1.4 2005/11/20 09:18:37 swildner Exp $
45 * the outer shell of regexec()
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.
51 #include <sys/types.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <limits.h>
56 #include <ctype.h>
57 #include <regex.h>
59 #include "utils.h"
60 #include "regex2.h"
62 /* macros for manipulating states, small version */
63 #define states long
64 #define states1 states /* for later use in regexec() decision */
65 #define CLEAR(v) ((v) = 0)
66 #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
67 #define SET1(v, n) ((v) |= (unsigned long)1 << (n))
68 #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
69 #define ASSIGN(d, s) ((d) = (s))
70 #define EQ(a, b) ((a) == (b))
71 #define STATEVARS long dummy /* dummy version */
72 #define STATESETUP(m, n) /* nothing */
73 #define STATETEARDOWN(m) /* nothing */
74 #define SETUP(v) ((v) = 0)
75 #define onestate long
76 #define INIT(o, n) ((o) = (unsigned long)1 << (n))
77 #define INC(o) ((o) <<= 1)
78 #define ISSTATEIN(v, o) (((v) & (o)) != 0)
79 /* some abbreviations; note that some of these know variable names! */
80 /* do "if I'm here, I can also be there" etc without branches */
81 #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
82 #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
83 #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
84 /* function names */
85 #define SNAMES /* engine.c looks after details */
87 #include "engine.c"
89 /* now undo things */
90 #undef states
91 #undef CLEAR
92 #undef SET0
93 #undef SET1
94 #undef ISSET
95 #undef ASSIGN
96 #undef EQ
97 #undef STATEVARS
98 #undef STATESETUP
99 #undef STATETEARDOWN
100 #undef SETUP
101 #undef onestate
102 #undef INIT
103 #undef INC
104 #undef ISSTATEIN
105 #undef FWD
106 #undef BACK
107 #undef ISSETBACK
108 #undef SNAMES
110 /* macros for manipulating states, large version */
111 #define states char *
112 #define CLEAR(v) memset(v, 0, m->g->nstates)
113 #define SET0(v, n) ((v)[n] = 0)
114 #define SET1(v, n) ((v)[n] = 1)
115 #define ISSET(v, n) ((v)[n])
116 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
117 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
118 #define STATEVARS long vn; char *space
119 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
120 if ((m)->space == NULL) return(REG_ESPACE); \
121 (m)->vn = 0; }
122 #define STATETEARDOWN(m) { free((m)->space); }
123 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
124 #define onestate long
125 #define INIT(o, n) ((o) = (n))
126 #define INC(o) ((o)++)
127 #define ISSTATEIN(v, o) ((v)[o])
128 /* some abbreviations; note that some of these know variable names! */
129 /* do "if I'm here, I can also be there" etc without branches */
130 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
131 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
132 #define ISSETBACK(v, n) ((v)[here - (n)])
133 /* function names */
134 #define LNAMES /* flag */
136 #include "engine.c"
139 - regexec - interface for matching
140 = extern int regexec(const regex_t *, const char *, size_t, \
141 = regmatch_t [], int);
142 = #define REG_NOTBOL 00001
143 = #define REG_NOTEOL 00002
144 = #define REG_STARTEND 00004
145 = #define REG_TRACE 00400 // tracing of execution
146 = #define REG_LARGE 01000 // force large representation
147 = #define REG_BACKR 02000 // force use of backref code
149 * We put this here so we can exploit knowledge of the state representation
150 * when choosing which matcher to call. Also, by this point the matchers
151 * have been prototyped.
153 int /* 0 success, REG_NOMATCH failure */
154 regexec(const regex_t *preg, const char *string, size_t nmatch,
155 regmatch_t pmatch[], int eflags)
157 struct re_guts *g = preg->re_g;
158 #ifdef REDEBUG
159 # define GOODFLAGS(f) (f)
160 #else
161 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
162 #endif
164 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
165 return(REG_BADPAT);
166 assert(!(g->iflags&BAD));
167 if (g->iflags&BAD) /* backstop for no-debug case */
168 return(REG_BADPAT);
169 eflags = GOODFLAGS(eflags);
171 if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
172 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
173 else
174 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));