Add regular expression matching support, based on OpenBSD regexec()/regcomp()
[oota-llvm.git] / lib / Support / regexec.c
1 /*-
2  * This code is derived from OpenBSD's libc/regex, original license follows:
3  *
4  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5  * Copyright (c) 1992, 1993, 1994
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Henry Spencer.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      @(#)regexec.c   8.3 (Berkeley) 3/20/94
36  */
37
38 /*
39  * the outer shell of llvm_regexec()
40  *
41  * This file includes engine.inc *twice*, after muchos fiddling with the
42  * macros that code uses.  This lets the same code operate on two different
43  * representations for state sets.
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_impl.h"
52
53 #include "regutils.h"
54 #include "regex2.h"
55
56 /* macros for manipulating states, small version */
57 #define states  long
58 #define states1 states          /* for later use in llvm_regexec() decision */
59 #define CLEAR(v)        ((v) = 0)
60 #define SET0(v, n)      ((v) &= ~((unsigned long)1 << (n)))
61 #define SET1(v, n)      ((v) |= (unsigned long)1 << (n))
62 #define ISSET(v, n)     (((v) & ((unsigned long)1 << (n))) != 0)
63 #define ASSIGN(d, s)    ((d) = (s))
64 #define EQ(a, b)        ((a) == (b))
65 #define STATEVARS       long dummy      /* dummy version */
66 #define STATESETUP(m, n)        /* nothing */
67 #define STATETEARDOWN(m)        /* nothing */
68 #define SETUP(v)        ((v) = 0)
69 #define onestate        long
70 #define INIT(o, n)      ((o) = (unsigned long)1 << (n))
71 #define INC(o)          ((o) <<= 1)
72 #define ISSTATEIN(v, o) (((v) & (o)) != 0)
73 /* some abbreviations; note that some of these know variable names! */
74 /* do "if I'm here, I can also be there" etc without branches */
75 #define FWD(dst, src, n)        ((dst) |= ((unsigned long)(src)&(here)) << (n))
76 #define BACK(dst, src, n)       ((dst) |= ((unsigned long)(src)&(here)) >> (n))
77 #define ISSETBACK(v, n)         (((v) & ((unsigned long)here >> (n))) != 0)
78 /* function names */
79 #define SNAMES                  /* engine.inc looks after details */
80
81 #include "regengine.inc"
82
83 /* now undo things */
84 #undef  states
85 #undef  CLEAR
86 #undef  SET0
87 #undef  SET1
88 #undef  ISSET
89 #undef  ASSIGN
90 #undef  EQ
91 #undef  STATEVARS
92 #undef  STATESETUP
93 #undef  STATETEARDOWN
94 #undef  SETUP
95 #undef  onestate
96 #undef  INIT
97 #undef  INC
98 #undef  ISSTATEIN
99 #undef  FWD
100 #undef  BACK
101 #undef  ISSETBACK
102 #undef  SNAMES
103
104 /* macros for manipulating states, large version */
105 #define states  char *
106 #define CLEAR(v)        memset(v, 0, m->g->nstates)
107 #define SET0(v, n)      ((v)[n] = 0)
108 #define SET1(v, n)      ((v)[n] = 1)
109 #define ISSET(v, n)     ((v)[n])
110 #define ASSIGN(d, s)    memmove(d, s, m->g->nstates)
111 #define EQ(a, b)        (memcmp(a, b, m->g->nstates) == 0)
112 #define STATEVARS       long vn; char *space
113 #define STATESETUP(m, nv)       { (m)->space = malloc((nv)*(m)->g->nstates); \
114                                 if ((m)->space == NULL) return(REG_ESPACE); \
115                                 (m)->vn = 0; }
116 #define STATETEARDOWN(m)        { free((m)->space); }
117 #define SETUP(v)        ((v) = &m->space[m->vn++ * m->g->nstates])
118 #define onestate        long
119 #define INIT(o, n)      ((o) = (n))
120 #define INC(o)  ((o)++)
121 #define ISSTATEIN(v, o) ((v)[o])
122 /* some abbreviations; note that some of these know variable names! */
123 /* do "if I'm here, I can also be there" etc without branches */
124 #define FWD(dst, src, n)        ((dst)[here+(n)] |= (src)[here])
125 #define BACK(dst, src, n)       ((dst)[here-(n)] |= (src)[here])
126 #define ISSETBACK(v, n) ((v)[here - (n)])
127 /* function names */
128 #define LNAMES                  /* flag */
129
130 #include "regengine.inc"
131
132 /*
133  - llvm_regexec - interface for matching
134  *
135  * We put this here so we can exploit knowledge of the state representation
136  * when choosing which matcher to call.  Also, by this point the matchers
137  * have been prototyped.
138  */
139 int                             /* 0 success, REG_NOMATCH failure */
140 llvm_regexec(const llvm_regex_t *preg, const char *string, size_t nmatch,
141              llvm_regmatch_t pmatch[], int eflags)
142 {
143         struct re_guts *g = preg->re_g;
144 #ifdef REDEBUG
145 #       define  GOODFLAGS(f)    (f)
146 #else
147 #       define  GOODFLAGS(f)    ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
148 #endif
149
150         if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
151                 return(REG_BADPAT);
152         assert(!(g->iflags&REGEX_BAD));
153         if (g->iflags&REGEX_BAD)                /* backstop for no-debug case */
154                 return(REG_BADPAT);
155         eflags = GOODFLAGS(eflags);
156
157         if (g->nstates <= (long)(CHAR_BIT*sizeof(states1)) && !(eflags&REG_LARGE))
158                 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
159         else
160                 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
161 }