Imported Upstream version 2.5.1
[debian/amanda] / regex-src / regexec.c
1 /*
2  * the outer shell of regexec()
3  *
4  * This file includes engine.c *twice*, after muchos fiddling with the
5  * macros that code uses.  This lets the same code operate on two different
6  * representations for state sets.
7  */
8 #include "amanda.h"
9 #include <regex.h>
10 #include "utils.h"
11 #include "regex2.h"
12
13 #undef ISSET
14
15 /* macros for manipulating states, small version */
16 #define states  unsigned long
17 #define states1 states          /* for later use in regexec() decision */
18 #define CLEAR(v)        (((v) = 0), (void)(v))
19 #define SET0(v, n)      ((v) &= ~(MAKE_UNSIGNED_LONG(1) << (n)))
20 #define SET1(v, n)      ((v) |= (MAKE_UNSIGNED_LONG(1)) << (n))
21 #define ISSET(v, n)     ((v) & ((MAKE_UNSIGNED_LONG(1)) << (n)))
22 #define ASSIGN(d, s)    ((d) = (s), (void)(d))
23 #define EQ(a, b)        ((a) == (b))
24 #define STATEVARS       int dummy       /* dummy version */
25 #define STATESETUP(m, n)        /* nothing */
26 #define STATETEARDOWN(m)        /* nothing */
27 #define SETUP(v)        ((v) = 0)
28 #define onestate        sopno
29 #define INIT(o, n)      ((o) = (sopno)1 << (n))
30 #define INC(o)  ((o) <<= 1)
31 #define ISSTATEIN(v, o) ((v) & (o))
32 /* some abbreviations; note that some of these know variable names! */
33 /* do "if I'm here, I can also be there" etc without branches */
34 #define FWD(dst, src, n)        ((dst) |= ((unsigned long)(src)&(here)) << (n))
35 #define BACK(dst, src, n)       ((dst) |= ((unsigned long)(src)&(here)) >> (n))
36 #define ISSETBACK(v, n) ((v) & ((long)here >> (n)))
37 /* function names */
38 #define SNAMES                  /* engine.c looks after details */
39
40 #include "engine.c"
41
42 /* now undo things */
43 #undef  states
44 #undef  CLEAR
45 #undef  SET0
46 #undef  SET1
47 #undef  ISSET
48 #undef  ASSIGN
49 #undef  EQ
50 #undef  STATEVARS
51 #undef  STATESETUP
52 #undef  STATETEARDOWN
53 #undef  SETUP
54 #undef  onestate
55 #undef  INIT
56 #undef  INC
57 #undef  ISSTATEIN
58 #undef  FWD
59 #undef  BACK
60 #undef  ISSETBACK
61 #undef  SNAMES
62
63 /* macros for manipulating states, large version */
64 #define states  char *
65 #define CLEAR(v)        memset((v), 0, (size_t)m->g->nstates)
66 #define SET0(v, n)      ((v)[n] = 0)
67 #define SET1(v, n)      ((v)[n] = 1)
68 #define ISSET(v, n)     ((v)[n])
69 #define ASSIGN(d, s)    memcpy((d), (s), (size_t)m->g->nstates)
70 #define EQ(a, b)        (memcmp((a), (b), (size_t)m->g->nstates) == 0)
71 #define STATEVARS       int vn; char *space
72 #define STATESETUP(m, nv) do {                                          \
73             (m)->space = malloc((size_t)(nv)*(size_t)(m)->g->nstates);  \
74             if ((m)->space == NULL)                                     \
75                 return(REG_ESPACE);                                     \
76             (m)->vn = 0;                                                \
77 } while (0);
78
79 #define STATETEARDOWN(m)        { free((m)->space); }
80 #define SETUP(v)        ((v) = &m->space[m->vn++ * m->g->nstates])
81 #define onestate        int
82 #define INIT(o, n)      ((o) = (n))
83 #define INC(o)  ((o)++)
84 #define ISSTATEIN(v, o) ((v)[o])
85 /* some abbreviations; note that some of these know variable names! */
86 /* do "if I'm here, I can also be there" etc without branches */
87 #define FWD(dst, src, n)        ((dst)[here+(n)] |= (src)[here])
88 #define BACK(dst, src, n)       ((dst)[here-(n)] |= (src)[here])
89 #define ISSETBACK(v, n) ((v)[here - (n)])
90 /* function names */
91 #define LNAMES                  /* flag */
92
93 #include "engine.c"
94
95 /*
96  - regexec - interface for matching
97  = int regexec(regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
98  = #define      REG_NOTBOL      00001
99  = #define      REG_NOTEOL      00002
100  = #define      REG_STARTEND    00004
101  = #define      REG_TRACE       00400   // tracing of execution
102  = #define      REG_LARGE       01000   // force large representation
103  = #define      REG_BACKR       02000   // force use of backref code
104  *
105  * We put this here so we can exploit knowledge of the state representation
106  * when choosing which matcher to call.  Also, by this point the matchers
107  * have been prototyped.
108  */
109 int                             /* 0 success, REG_NOMATCH failure */
110 regexec(
111     regex_t *preg,
112     const char *string,
113     size_t nmatch,
114     regmatch_t pmatch[],
115     int eflags)
116 {
117         /*@ignore@*/
118         register struct re_guts *g = preg->re_g;
119 #ifdef REDEBUG
120 #       define  GOODFLAGS(f)    (f)
121 #else
122 #       define  GOODFLAGS(f)    ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
123 #endif
124
125         if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
126                 return(REG_BADPAT);
127         /*@end@*/
128         assert(!(g->iflags&BAD));
129         if (g->iflags&BAD)              /* backstop for no-debug case */
130                 return(REG_BADPAT);
131         eflags = GOODFLAGS(eflags);
132
133         if ((g->nstates <= ((sopno)(CHAR_BIT * SIZEOF(states1))) &&
134             !(eflags & REG_LARGE)))
135                 return(smatcher(g, string, nmatch, pmatch, eflags));
136         return(lmatcher(g, string, nmatch, pmatch, eflags));
137 }