Home | History | Annotate | Line # | Download | only in regex
regexec.c revision 1.20
      1  1.20  junyoung /*	$NetBSD: regexec.c,v 1.20 2007/02/09 23:44:18 junyoung Exp $	*/
      2   1.6       cgd 
      3   1.4       cgd /*-
      4   1.4       cgd  * Copyright (c) 1992, 1993, 1994
      5   1.4       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.4       cgd  *
      7   1.4       cgd  * This code is derived from software contributed to Berkeley by
      8   1.4       cgd  * Henry Spencer.
      9   1.4       cgd  *
     10   1.4       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.4       cgd  * modification, are permitted provided that the following conditions
     12   1.4       cgd  * are met:
     13   1.4       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.4       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.4       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.4       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.4       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.17       agc  *    may be used to endorse or promote products derived from this software
     20  1.17       agc  *    without specific prior written permission.
     21  1.17       agc  *
     22  1.17       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.17       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.17       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.17       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.17       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.17       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.17       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.17       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.17       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.17       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.17       agc  * SUCH DAMAGE.
     33  1.17       agc  *
     34  1.17       agc  *	@(#)regexec.c	8.3 (Berkeley) 3/20/94
     35  1.17       agc  */
     36  1.17       agc 
     37  1.17       agc /*-
     38  1.17       agc  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
     39  1.17       agc  *
     40  1.17       agc  * This code is derived from software contributed to Berkeley by
     41  1.17       agc  * Henry Spencer.
     42  1.17       agc  *
     43  1.17       agc  * Redistribution and use in source and binary forms, with or without
     44  1.17       agc  * modification, are permitted provided that the following conditions
     45  1.17       agc  * are met:
     46  1.17       agc  * 1. Redistributions of source code must retain the above copyright
     47  1.17       agc  *    notice, this list of conditions and the following disclaimer.
     48  1.17       agc  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.17       agc  *    notice, this list of conditions and the following disclaimer in the
     50  1.17       agc  *    documentation and/or other materials provided with the distribution.
     51   1.4       cgd  * 3. All advertising materials mentioning features or use of this software
     52   1.4       cgd  *    must display the following acknowledgement:
     53   1.4       cgd  *	This product includes software developed by the University of
     54   1.4       cgd  *	California, Berkeley and its contributors.
     55   1.4       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.4       cgd  *    may be used to endorse or promote products derived from this software
     57   1.4       cgd  *    without specific prior written permission.
     58   1.4       cgd  *
     59   1.4       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.4       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.4       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.4       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.4       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.4       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.4       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.4       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.4       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.4       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.4       cgd  * SUCH DAMAGE.
     70   1.4       cgd  *
     71   1.4       cgd  *	@(#)regexec.c	8.3 (Berkeley) 3/20/94
     72   1.4       cgd  */
     73   1.4       cgd 
     74   1.8  christos #include <sys/cdefs.h>
     75   1.4       cgd #if defined(LIBC_SCCS) && !defined(lint)
     76   1.6       cgd #if 0
     77   1.4       cgd static char sccsid[] = "@(#)regexec.c	8.3 (Berkeley) 3/20/94";
     78   1.6       cgd #else
     79  1.20  junyoung __RCSID("$NetBSD: regexec.c,v 1.20 2007/02/09 23:44:18 junyoung Exp $");
     80   1.6       cgd #endif
     81   1.4       cgd #endif /* LIBC_SCCS and not lint */
     82   1.4       cgd 
     83   1.1       jtc /*
     84   1.1       jtc  * the outer shell of regexec()
     85   1.1       jtc  *
     86   1.1       jtc  * This file includes engine.c *twice*, after muchos fiddling with the
     87   1.1       jtc  * macros that code uses.  This lets the same code operate on two different
     88   1.1       jtc  * representations for state sets.
     89   1.1       jtc  */
     90   1.9       jtc #include "namespace.h"
     91   1.1       jtc #include <sys/types.h>
     92  1.13     lukem 
     93  1.13     lukem #include <assert.h>
     94  1.13     lukem #include <ctype.h>
     95  1.13     lukem #include <limits.h>
     96   1.1       jtc #include <stdio.h>
     97   1.1       jtc #include <stdlib.h>
     98   1.1       jtc #include <string.h>
     99  1.20  junyoung #include <regex.h>
    100   1.9       jtc 
    101   1.9       jtc #ifdef __weak_alias
    102  1.15   mycroft __weak_alias(regexec,_regexec)
    103   1.9       jtc #endif
    104   1.1       jtc 
    105   1.1       jtc #include "utils.h"
    106   1.1       jtc #include "regex2.h"
    107   1.1       jtc 
    108   1.1       jtc /* macros for manipulating states, small version */
    109  1.12  drochner #define	states	unsigned long
    110  1.12  drochner #define	states1	unsigned long	/* for later use in regexec() decision */
    111   1.1       jtc #define	CLEAR(v)	((v) = 0)
    112  1.12  drochner #define	SET0(v, n)	((v) &= ~((unsigned long)1 << (n)))
    113  1.12  drochner #define	SET1(v, n)	((v) |= (unsigned long)1 << (n))
    114  1.12  drochner #define	ISSET(v, n)	(((v) & ((unsigned long)1 << (n))) != 0)
    115   1.1       jtc #define	ASSIGN(d, s)	((d) = (s))
    116   1.1       jtc #define	EQ(a, b)	((a) == (b))
    117  1.12  drochner #define	STATEVARS	int dummy	/* dummy version */
    118   1.1       jtc #define	STATESETUP(m, n)	/* nothing */
    119   1.1       jtc #define	STATETEARDOWN(m)	/* nothing */
    120   1.1       jtc #define	SETUP(v)	((v) = 0)
    121  1.12  drochner #define	onestate	unsigned long
    122  1.12  drochner #define	INIT(o, n)	((o) = (unsigned long)1 << (n))
    123   1.7       cgd #define	INC(o)	((o) <<= 1)
    124   1.5       cgd #define	ISSTATEIN(v, o)	(((v) & (o)) != 0)
    125   1.1       jtc /* some abbreviations; note that some of these know variable names! */
    126   1.1       jtc /* do "if I'm here, I can also be there" etc without branches */
    127  1.12  drochner #define	FWD(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) << (n))
    128  1.12  drochner #define	BACK(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) >> (n))
    129  1.12  drochner #define	ISSETBACK(v, n)	(((v) & ((unsigned long)here >> (n))) != 0)
    130   1.1       jtc /* function names */
    131   1.1       jtc #define SNAMES			/* engine.c looks after details */
    132   1.1       jtc 
    133   1.1       jtc #include "engine.c"
    134   1.1       jtc 
    135   1.1       jtc /* now undo things */
    136   1.1       jtc #undef	states
    137   1.1       jtc #undef	CLEAR
    138   1.1       jtc #undef	SET0
    139   1.1       jtc #undef	SET1
    140   1.1       jtc #undef	ISSET
    141   1.1       jtc #undef	ASSIGN
    142   1.1       jtc #undef	EQ
    143   1.1       jtc #undef	STATEVARS
    144   1.1       jtc #undef	STATESETUP
    145   1.1       jtc #undef	STATETEARDOWN
    146   1.1       jtc #undef	SETUP
    147   1.1       jtc #undef	onestate
    148   1.1       jtc #undef	INIT
    149   1.1       jtc #undef	INC
    150   1.1       jtc #undef	ISSTATEIN
    151   1.1       jtc #undef	FWD
    152   1.1       jtc #undef	BACK
    153   1.1       jtc #undef	ISSETBACK
    154   1.1       jtc #undef	SNAMES
    155   1.1       jtc 
    156   1.1       jtc /* macros for manipulating states, large version */
    157   1.1       jtc #define	states	char *
    158  1.11  christos #define	CLEAR(v)	memset(v, 0, (size_t)m->g->nstates)
    159   1.1       jtc #define	SET0(v, n)	((v)[n] = 0)
    160   1.1       jtc #define	SET1(v, n)	((v)[n] = 1)
    161   1.1       jtc #define	ISSET(v, n)	((v)[n])
    162  1.11  christos #define	ASSIGN(d, s)	memcpy(d, s, (size_t)m->g->nstates)
    163  1.11  christos #define	EQ(a, b)	(memcmp(a, b, (size_t)m->g->nstates) == 0)
    164  1.12  drochner #define	STATEVARS	int vn; char *space
    165  1.16  christos #define	STATESETUP(m, nv) \
    166  1.16  christos     if (((m)->space = malloc((size_t)((nv)*(m)->g->nstates))) == NULL) \
    167  1.16  christos 	return(REG_ESPACE); \
    168  1.16  christos     else \
    169  1.16  christos 	(m)->vn = 0
    170  1.16  christos 
    171  1.16  christos #define	STATETEARDOWN(m)	{ free((m)->space); m->space = NULL; }
    172  1.11  christos #define	SETUP(v)	((v) = &m->space[(size_t)(m->vn++ * m->g->nstates)])
    173  1.12  drochner #define	onestate	int
    174   1.1       jtc #define	INIT(o, n)	((o) = (n))
    175   1.1       jtc #define	INC(o)	((o)++)
    176   1.1       jtc #define	ISSTATEIN(v, o)	((v)[o])
    177   1.1       jtc /* some abbreviations; note that some of these know variable names! */
    178   1.1       jtc /* do "if I'm here, I can also be there" etc without branches */
    179   1.1       jtc #define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
    180   1.1       jtc #define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
    181   1.1       jtc #define	ISSETBACK(v, n)	((v)[here - (n)])
    182   1.1       jtc /* function names */
    183   1.1       jtc #define	LNAMES			/* flag */
    184   1.1       jtc 
    185   1.1       jtc #include "engine.c"
    186   1.1       jtc 
    187   1.1       jtc /*
    188   1.1       jtc  - regexec - interface for matching
    189   1.2       jtc  = extern int regexec(const regex_t *, const char *, size_t, \
    190   1.2       jtc  =					regmatch_t [], int);
    191   1.1       jtc  = #define	REG_NOTBOL	00001
    192   1.1       jtc  = #define	REG_NOTEOL	00002
    193   1.1       jtc  = #define	REG_STARTEND	00004
    194   1.1       jtc  = #define	REG_TRACE	00400	// tracing of execution
    195   1.1       jtc  = #define	REG_LARGE	01000	// force large representation
    196   1.1       jtc  = #define	REG_BACKR	02000	// force use of backref code
    197   1.1       jtc  *
    198   1.1       jtc  * We put this here so we can exploit knowledge of the state representation
    199   1.1       jtc  * when choosing which matcher to call.  Also, by this point the matchers
    200   1.1       jtc  * have been prototyped.
    201   1.1       jtc  */
    202   1.1       jtc int				/* 0 success, REG_NOMATCH failure */
    203  1.19  junyoung regexec(
    204  1.19  junyoung     const regex_t *preg,
    205  1.19  junyoung     const char *string,
    206  1.19  junyoung     size_t nmatch,
    207  1.19  junyoung     regmatch_t pmatch[],
    208  1.19  junyoung     int eflags)
    209   1.1       jtc {
    210  1.10     perry 	struct re_guts *g = preg->re_g;
    211  1.11  christos 	char *s;
    212   1.1       jtc #ifdef REDEBUG
    213   1.1       jtc #	define	GOODFLAGS(f)	(f)
    214   1.1       jtc #else
    215   1.1       jtc #	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
    216  1.13     lukem #endif
    217  1.13     lukem 
    218  1.13     lukem 	_DIAGASSERT(preg != NULL);
    219  1.13     lukem 	_DIAGASSERT(string != NULL);
    220   1.1       jtc 
    221   1.1       jtc 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
    222   1.1       jtc 		return(REG_BADPAT);
    223   1.1       jtc 	assert(!(g->iflags&BAD));
    224   1.1       jtc 	if (g->iflags&BAD)		/* backstop for no-debug case */
    225   1.1       jtc 		return(REG_BADPAT);
    226   1.3       jtc 	eflags = GOODFLAGS(eflags);
    227   1.1       jtc 
    228  1.18  christos 	s = __UNCONST(string);
    229  1.11  christos 
    230   1.1       jtc 	if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
    231  1.11  christos 		return(smatcher(g, s, nmatch, pmatch, eflags));
    232   1.1       jtc 	else
    233  1.11  christos 		return(lmatcher(g, s, nmatch, pmatch, eflags));
    234   1.1       jtc }
    235