Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / lib / libc / regex / 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. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)regexec.c   8.3 (Berkeley) 3/20/94
34  * $FreeBSD: src/lib/libc/regex/regexec.c,v 1.8 2007/06/11 03:05:54 delphij Exp $
35  * $DragonFly: src/lib/libc/regex/regexec.c,v 1.4 2005/11/20 09:18:37 swildner Exp $
36  */
37
38 /*
39  * the outer shell of regexec()
40  *
41  * This file includes engine.c three times, after muchos fiddling with the
42  * macros that code uses.  This lets the same code operate on two different
43  * representations for state sets and characters.
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.h>
52 #include <wchar.h>
53 #include <wctype.h>
54
55 #include "utils.h"
56 #include "regex2.h"
57
58 static int nope __unused = 0;   /* for use in asserts; shuts lint up */
59
60 static __inline size_t
61 xmbrtowc(wint_t *wi, const char *s, size_t n, mbstate_t *mbs, wint_t dummy)
62 {
63         size_t nr;
64         wchar_t wc;
65
66         nr = mbrtowc(&wc, s, n, mbs);
67         if (wi != NULL)
68                 *wi = wc;
69         if (nr == 0)
70                 return (1);
71         else if (nr == (size_t)-1 || nr == (size_t)-2) {
72                 memset(mbs, 0, sizeof(*mbs));
73                 if (wi != NULL)
74                         *wi = dummy;
75                 return (1);
76         } else
77                 return (nr);
78 }
79
80 static __inline size_t
81 xmbrtowc_dummy(wint_t *wi,
82                 const char *s,
83                 size_t n __unused,
84                 mbstate_t *mbs __unused,
85                 wint_t dummy __unused)
86 {
87
88         if (wi != NULL)
89                 *wi = (unsigned char)*s;
90         return (1);
91 }
92
93 /* macros for manipulating states, small version */
94 #define states  long
95 #define states1 states          /* for later use in regexec() decision */
96 #define CLEAR(v)        ((v) = 0)
97 #define SET0(v, n)      ((v) &= ~((unsigned long)1 << (n)))
98 #define SET1(v, n)      ((v) |= (unsigned long)1 << (n))
99 #define ISSET(v, n)     (((v) & ((unsigned long)1 << (n))) != 0)
100 #define ASSIGN(d, s)    ((d) = (s))
101 #define EQ(a, b)        ((a) == (b))
102 #define STATEVARS       long dummy      /* dummy version */
103 #define STATESETUP(m, n)        /* nothing */
104 #define STATETEARDOWN(m)        /* nothing */
105 #define SETUP(v)        ((v) = 0)
106 #define onestate        long
107 #define INIT(o, n)      ((o) = (unsigned long)1 << (n))
108 #define INC(o)  ((o) <<= 1)
109 #define ISSTATEIN(v, o) (((v) & (o)) != 0)
110 /* some abbreviations; note that some of these know variable names! */
111 /* do "if I'm here, I can also be there" etc without branches */
112 #define FWD(dst, src, n)        ((dst) |= ((unsigned long)(src)&(here)) << (n))
113 #define BACK(dst, src, n)       ((dst) |= ((unsigned long)(src)&(here)) >> (n))
114 #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
115 /* no multibyte support */
116 #define XMBRTOWC        xmbrtowc_dummy
117 #define ZAPSTATE(mbs)   ((void)(mbs))
118 /* function names */
119 #define SNAMES                  /* engine.c looks after details */
120
121 #include "engine.c"
122
123 /* now undo things */
124 #undef  states
125 #undef  CLEAR
126 #undef  SET0
127 #undef  SET1
128 #undef  ISSET
129 #undef  ASSIGN
130 #undef  EQ
131 #undef  STATEVARS
132 #undef  STATESETUP
133 #undef  STATETEARDOWN
134 #undef  SETUP
135 #undef  onestate
136 #undef  INIT
137 #undef  INC
138 #undef  ISSTATEIN
139 #undef  FWD
140 #undef  BACK
141 #undef  ISSETBACK
142 #undef  SNAMES
143 #undef  XMBRTOWC
144 #undef  ZAPSTATE
145
146 /* macros for manipulating states, large version */
147 #define states  char *
148 #define CLEAR(v)        memset(v, 0, m->g->nstates)
149 #define SET0(v, n)      ((v)[n] = 0)
150 #define SET1(v, n)      ((v)[n] = 1)
151 #define ISSET(v, n)     ((v)[n])
152 #define ASSIGN(d, s)    memcpy(d, s, m->g->nstates)
153 #define EQ(a, b)        (memcmp(a, b, m->g->nstates) == 0)
154 #define STATEVARS       long vn; char *space
155 #define STATESETUP(m, nv)       { (m)->space = malloc((nv)*(m)->g->nstates); \
156                                 if ((m)->space == NULL) return(REG_ESPACE); \
157                                 (m)->vn = 0; }
158 #define STATETEARDOWN(m)        { free((m)->space); }
159 #define SETUP(v)        ((v) = &m->space[m->vn++ * m->g->nstates])
160 #define onestate        long
161 #define INIT(o, n)      ((o) = (n))
162 #define INC(o)  ((o)++)
163 #define ISSTATEIN(v, o) ((v)[o])
164 /* some abbreviations; note that some of these know variable names! */
165 /* do "if I'm here, I can also be there" etc without branches */
166 #define FWD(dst, src, n)        ((dst)[here+(n)] |= (src)[here])
167 #define BACK(dst, src, n)       ((dst)[here-(n)] |= (src)[here])
168 #define ISSETBACK(v, n) ((v)[here - (n)])
169 /* no multibyte support */
170 #define XMBRTOWC        xmbrtowc_dummy
171 #define ZAPSTATE(mbs)   ((void)(mbs))
172 /* function names */
173 #define LNAMES                  /* flag */
174
175 #include "engine.c"
176
177 /* multibyte character & large states version */
178 #undef  LNAMES
179 #undef  XMBRTOWC
180 #undef  ZAPSTATE
181 #define XMBRTOWC        xmbrtowc
182 #define ZAPSTATE(mbs)   memset((mbs), 0, sizeof(*(mbs)))
183 #define MNAMES
184
185 #include "engine.c"
186
187 /*
188  - regexec - interface for matching
189  = extern int regexec(const regex_t *, const char *, size_t, \
190  =                                      regmatch_t [], int);
191  = #define      REG_NOTBOL      00001
192  = #define      REG_NOTEOL      00002
193  = #define      REG_STARTEND    00004
194  = #define      REG_TRACE       00400   // tracing of execution
195  = #define      REG_LARGE       01000   // force large representation
196  = #define      REG_BACKR       02000   // force use of backref code
197  *
198  * We put this here so we can exploit knowledge of the state representation
199  * when choosing which matcher to call.  Also, by this point the matchers
200  * have been prototyped.
201  */
202 int                             /* 0 success, REG_NOMATCH failure */
203 regexec(const regex_t * __restrict preg,
204         const char * __restrict string,
205         size_t nmatch,
206         regmatch_t pmatch[__restrict],
207         int eflags)
208 {
209         struct re_guts *g = preg->re_g;
210 #ifdef REDEBUG
211 #       define  GOODFLAGS(f)    (f)
212 #else
213 #       define  GOODFLAGS(f)    ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
214 #endif
215
216         if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
217                 return(REG_BADPAT);
218         assert(!(g->iflags&BAD));
219         if (g->iflags&BAD)              /* backstop for no-debug case */
220                 return(REG_BADPAT);
221         eflags = GOODFLAGS(eflags);
222
223         if (MB_CUR_MAX > 1)
224                 return(mmatcher(g, (char *)string, nmatch, pmatch, eflags));
225         else if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
226                 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
227         else
228                 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
229 }