Cleanup various type-o's in comments.
[dragonfly.git] / lib / libcr / 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  * $DragonFly: src/lib/libcr/regex/Attic/regexec.c,v 1.3 2004/03/13 19:46:55 eirikn Exp $
42  */
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 <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>
58
59 #include "utils.h"
60 #include "regex2.h"
61
62 static int nope = 0;            /* for use in asserts; shuts lint up */
63
64 /* macros for manipulating states, small version */
65 #define states  long
66 #define states1 states          /* for later use in regexec() decision */
67 #define CLEAR(v)        ((v) = 0)
68 #define SET0(v, n)      ((v) &= ~((unsigned long)1 << (n)))
69 #define SET1(v, n)      ((v) |= (unsigned long)1 << (n))
70 #define ISSET(v, n)     (((v) & ((unsigned long)1 << (n))) != 0)
71 #define ASSIGN(d, s)    ((d) = (s))
72 #define EQ(a, b)        ((a) == (b))
73 #define STATEVARS       long dummy      /* dummy version */
74 #define STATESETUP(m, n)        /* nothing */
75 #define STATETEARDOWN(m)        /* nothing */
76 #define SETUP(v)        ((v) = 0)
77 #define onestate        long
78 #define INIT(o, n)      ((o) = (unsigned long)1 << (n))
79 #define INC(o)  ((o) <<= 1)
80 #define ISSTATEIN(v, o) (((v) & (o)) != 0)
81 /* some abbreviations; note that some of these know variable names! */
82 /* do "if I'm here, I can also be there" etc without branches */
83 #define FWD(dst, src, n)        ((dst) |= ((unsigned long)(src)&(here)) << (n))
84 #define BACK(dst, src, n)       ((dst) |= ((unsigned long)(src)&(here)) >> (n))
85 #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
86 /* function names */
87 #define SNAMES                  /* engine.c looks after details */
88
89 #include "engine.c"
90
91 /* now undo things */
92 #undef  states
93 #undef  CLEAR
94 #undef  SET0
95 #undef  SET1
96 #undef  ISSET
97 #undef  ASSIGN
98 #undef  EQ
99 #undef  STATEVARS
100 #undef  STATESETUP
101 #undef  STATETEARDOWN
102 #undef  SETUP
103 #undef  onestate
104 #undef  INIT
105 #undef  INC
106 #undef  ISSTATEIN
107 #undef  FWD
108 #undef  BACK
109 #undef  ISSETBACK
110 #undef  SNAMES
111
112 /* macros for manipulating states, large version */
113 #define states  char *
114 #define CLEAR(v)        memset(v, 0, m->g->nstates)
115 #define SET0(v, n)      ((v)[n] = 0)
116 #define SET1(v, n)      ((v)[n] = 1)
117 #define ISSET(v, n)     ((v)[n])
118 #define ASSIGN(d, s)    memcpy(d, s, m->g->nstates)
119 #define EQ(a, b)        (memcmp(a, b, m->g->nstates) == 0)
120 #define STATEVARS       long vn; char *space
121 #define STATESETUP(m, nv)       { (m)->space = malloc((nv)*(m)->g->nstates); \
122                                 if ((m)->space == NULL) return(REG_ESPACE); \
123                                 (m)->vn = 0; }
124 #define STATETEARDOWN(m)        { free((m)->space); }
125 #define SETUP(v)        ((v) = &m->space[m->vn++ * m->g->nstates])
126 #define onestate        long
127 #define INIT(o, n)      ((o) = (n))
128 #define INC(o)  ((o)++)
129 #define ISSTATEIN(v, o) ((v)[o])
130 /* some abbreviations; note that some of these know variable names! */
131 /* do "if I'm here, I can also be there" etc without branches */
132 #define FWD(dst, src, n)        ((dst)[here+(n)] |= (src)[here])
133 #define BACK(dst, src, n)       ((dst)[here-(n)] |= (src)[here])
134 #define ISSETBACK(v, n) ((v)[here - (n)])
135 /* function names */
136 #define LNAMES                  /* flag */
137
138 #include "engine.c"
139
140 /*
141  - regexec - interface for matching
142  = extern int regexec(const regex_t *, const char *, size_t, \
143  =                                      regmatch_t [], int);
144  = #define      REG_NOTBOL      00001
145  = #define      REG_NOTEOL      00002
146  = #define      REG_STARTEND    00004
147  = #define      REG_TRACE       00400   // tracing of execution
148  = #define      REG_LARGE       01000   // force large representation
149  = #define      REG_BACKR       02000   // force use of backref code
150  *
151  * We put this here so we can exploit knowledge of the state representation
152  * when choosing which matcher to call.  Also, by this point the matchers
153  * have been prototyped.
154  */
155 int                             /* 0 success, REG_NOMATCH failure */
156 regexec(const regex_t *preg, const char *string, size_t nmatch,
157         regmatch_t pmatch[], int eflags)
158 {
159         struct re_guts *g = preg->re_g;
160 #ifdef REDEBUG
161 #       define  GOODFLAGS(f)    (f)
162 #else
163 #       define  GOODFLAGS(f)    ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
164 #endif
165
166         if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
167                 return(REG_BADPAT);
168         assert(!(g->iflags&BAD));
169         if (g->iflags&BAD)              /* backstop for no-debug case */
170                 return(REG_BADPAT);
171         eflags = GOODFLAGS(eflags);
172
173         if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
174                 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
175         else
176                 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
177 }