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