6b5e5868e7ebf1aff3a5abd7d0c4ef0e5fbf3648Garrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 1992, 1993, 1994 Henry Spencer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 1992, 1993, 1994
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * The Regents of the University of California. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This code is derived from software contributed to Berkeley by
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Henry Spencer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Redistribution and use in source and binary forms, with or without
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * modification, are permitted provided that the following conditions
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * documentation and/or other materials provided with the distribution.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 4. Neither the name of the University nor the names of its contributors
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * may be used to endorse or promote products derived from this software
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * without specific prior written permission.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * SUCH DAMAGE.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * the outer shell of regexec()
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This file includes engine.c three times, after muchos fiddling with the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * macros that code uses. This lets the same code operate on two different
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * representations for state sets and characters.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* we want _NOTE, but not NOTE (which collides with our own use) */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorexmbrtowc(wint_t *wi, const char *s, size_t n, mbstate_t *mbs, wint_t dummy)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore else if (nr == (size_t)-1 || nr == (size_t)-2) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorexmbrtowc_dummy(wint_t *wi, const char *s, size_t n, mbstate_t *mbs,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *wi = (unsigned char)*s;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* macros for manipulating states, small version */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define states1 states /* for later use in regexec() decision */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define SET1(v, n) ((v) |= (unsigned long)1 << (n))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define STATEVARS long dummy /* dummy version */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define INIT(o, n) ((o) = (unsigned long)1 << (n))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* some abbreviations; note that some of these know variable names! */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* do "if I'm here, I can also be there" etc without branches */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* no multibyte support */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* function names */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define SNAMES /* engine.c looks after details */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* now undo things */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* macros for manipulating states, large version */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define CLEAR(v) (void) memset(v, 0, m->g->nstates)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define ASSIGN(d, s) (void) memcpy(d, s, m->g->nstates)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define STATETEARDOWN(m) { free((m)->space); }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* some abbreviations; note that some of these know variable names! */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* do "if I'm here, I can also be there" etc without branches */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* no multibyte support */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* function names */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* multibyte character & large states version */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define ZAPSTATE(mbs) (void) memset((mbs), 0, sizeof (*(mbs)))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * regexec - interface for matching
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * We put this here so we can exploit knowledge of the state representation
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * when choosing which matcher to call. Also, by this point the matchers
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * have been prototyped.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amoreint /* 0 success, REG_NOMATCH failure */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore const char *_RESTRICT_KYWD string, size_t nmatch,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore regmatch_t pmatch[_RESTRICT_KYWD], int eflags)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (g->iflags&BAD) /* backstop for no-debug case */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (mmatcher(g, (char *)string, nmatch, pmatch, eflags));
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore else if (g->nstates <= CHAR_BIT*sizeof (states1) && !(eflags®_LARGE))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore else if (g->nstates <= CHAR_BIT*sizeof (states1))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (smatcher(g, (char *)string, nmatch, pmatch, eflags));
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (lmatcher(g, (char *)string, nmatch, pmatch, eflags));