[BACK]Return to regexec.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / regex

Annotation of src/lib/libc/regex/regexec.c, Revision 1.15

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

CVSweb <webmaster@jp.NetBSD.org>