9764be96b6fef91cd588c76025f6b745a60054d6
[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. 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  * @(#)regexec.c        8.3 (Berkeley) 3/20/94
40  */
41
42 /*
43  * the outer shell of regexec()
44  *
45  * This file includes engine.c *twice*, after muchos fiddling with the
46  * macros that code uses.  This lets the same code operate on two different
47  * representations for state sets.
48  */
49 #include <sys/types.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <limits.h>
54 #include <ctype.h>
55 #include <regex.h>
56
57 #include "utils.h"
58 #include "regex2.h"
59
60 static int nope = 0;            /* for use in asserts; shuts lint up */
61
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 */
86
87 #include "engine.c"
88
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
109
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 */
135
136 #include "engine.c"
137
138 /*
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
148  *
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.
152  */
153 int                             /* 0 success, REG_NOMATCH failure */
154 regexec(preg, string, nmatch, pmatch, eflags)
155 const regex_t *preg;
156 const char *string;
157 size_t nmatch;
158 regmatch_t pmatch[];
159 int eflags;
160 {
161         register struct re_guts *g = preg->re_g;
162 #ifdef REDEBUG
163 #       define  GOODFLAGS(f)    (f)
164 #else
165 #       define  GOODFLAGS(f)    ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
166 #endif
167
168         if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
169                 return(REG_BADPAT);
170         assert(!(g->iflags&BAD));
171         if (g->iflags&BAD)              /* backstop for no-debug case */
172                 return(REG_BADPAT);
173         eflags = GOODFLAGS(eflags);
174
175         if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
176                 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
177         else
178                 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
179 }