G:/ScriptBasic/source/extensions/re/regexec.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
00003  * Copyright (c) 1992, 1993, 1994
00004  *      The Regents of the University of California.  All rights reserved.
00005  *
00006  * This code is derived from software contributed to Berkeley by
00007  * Henry Spencer.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 3. All advertising materials mentioning features or use of this software
00018  *    must display the following acknowledgement:
00019  *      This product includes software developed by the University of
00020  *      California, Berkeley and its contributors.
00021  * 4. Neither the name of the University nor the names of its contributors
00022  *    may be used to endorse or promote products derived from this software
00023  *    without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00026  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00031  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00032  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00034  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00035  * SUCH DAMAGE.
00036  *
00037  *      @(#)regexec.c   8.3 (Berkeley) 3/20/94
00038  */
00039 
00040 #if defined(LIBC_SCCS) && !defined(lint)
00041 static char sccsid[] = "@(#)regexec.c   8.3 (Berkeley) 3/20/94";
00042 #endif /* LIBC_SCCS and not lint */
00043 
00044 /*
00045  * the outer shell of regexec()
00046  *
00047  * This file includes engine.c *twice*, after muchos fiddling with the
00048  * macros that code uses.  This lets the same code operate on two different
00049  * representations for state sets.
00050  */
00051 #include <sys/types.h>
00052 #include <stdio.h>
00053 #include <stdlib.h>
00054 #include <string.h>
00055 #include <limits.h>
00056 #include <ctype.h>
00057 #include "regex.h"
00058 
00059 #include "utils.h"
00060 #include "regex2.h"
00061 
00062 static int nope = 0;            /* for use in asserts; shuts lint up */
00063 
00064 /* macros for manipulating states, small version */
00065 #define states  long
00066 #define states1 states          /* for later use in regexec() decision */
00067 #define CLEAR(v)        ((v) = 0)
00068 #define SET0(v, n)      ((v) &= ~(1 << (n)))
00069 #define SET1(v, n)      ((v) |= 1 << (n))
00070 #define ISSET(v, n)     ((v) & (1 << (n)))
00071 #define ASSIGN(d, s)    ((d) = (s))
00072 #define EQ(a, b)        ((a) == (b))
00073 #define STATEVARS       int dummy       /* dummy version */
00074 #define STATESETUP(m, n)        /* nothing */
00075 #define STATETEARDOWN(m)        /* nothing */
00076 #define SETUP(v)        ((v) = 0)
00077 #define onestate        int
00078 #define INIT(o, n)      ((o) = (unsigned)1 << (n))
00079 #define INC(o)  ((o) <<= 1)
00080 #define ISSTATEIN(v, o) ((v) & (o))
00081 /* some abbreviations; note that some of these know variable names! */
00082 /* do "if I'm here, I can also be there" etc without branches */
00083 #define FWD(dst, src, n)        ((dst) |= ((unsigned)(src)&(here)) << (n))
00084 #define BACK(dst, src, n)       ((dst) |= ((unsigned)(src)&(here)) >> (n))
00085 #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n)))
00086 /* function names */
00087 #define SNAMES                  /* engine.c looks after details */
00088 
00089 #include "engine.ci"
00090 
00091 /* now undo things */
00092 #undef  states
00093 #undef  CLEAR
00094 #undef  SET0
00095 #undef  SET1
00096 #undef  ISSET
00097 #undef  ASSIGN
00098 #undef  EQ
00099 #undef  STATEVARS
00100 #undef  STATESETUP
00101 #undef  STATETEARDOWN
00102 #undef  SETUP
00103 #undef  onestate
00104 #undef  INIT
00105 #undef  INC
00106 #undef  ISSTATEIN
00107 #undef  FWD
00108 #undef  BACK
00109 #undef  ISSETBACK
00110 #undef  SNAMES
00111 
00112 /* macros for manipulating states, large version */
00113 #define states  char *
00114 #define CLEAR(v)        memset(v, 0, m->g->nstates)
00115 #define SET0(v, n)      ((v)[n] = 0)
00116 #define SET1(v, n)      ((v)[n] = 1)
00117 #define ISSET(v, n)     ((v)[n])
00118 #define ASSIGN(d, s)    memcpy(d, s, m->g->nstates)
00119 #define EQ(a, b)        (memcmp(a, b, m->g->nstates) == 0)
00120 #define STATEVARS       int vn; char *space
00121 #define STATESETUP(m, nv)       { (m)->space = malloc((nv)*(m)->g->nstates); \
00122                                 if ((m)->space == NULL) return(REG_ESPACE); \
00123                                 (m)->vn = 0; }
00124 #define STATETEARDOWN(m)        { free((m)->space); }
00125 #define SETUP(v)        ((v) = &m->space[m->vn++ * m->g->nstates])
00126 #define onestate        int
00127 #define INIT(o, n)      ((o) = (n))
00128 #define INC(o)  ((o)++)
00129 #define ISSTATEIN(v, o) ((v)[o])
00130 /* some abbreviations; note that some of these know variable names! */
00131 /* do "if I'm here, I can also be there" etc without branches */
00132 #define FWD(dst, src, n)        ((dst)[here+(n)] |= (src)[here])
00133 #define BACK(dst, src, n)       ((dst)[here-(n)] |= (src)[here])
00134 #define ISSETBACK(v, n) ((v)[here - (n)])
00135 /* function names */
00136 #define LNAMES                  /* flag */
00137 
00138 #include "engine.ci"
00139 
00140 /*
00141  - regexec - interface for matching
00142  = extern int regexec(const regex_t *, const char *, size_t, \
00143  =                                      regmatch_t [], int);
00144  = #define      REG_NOTBOL      00001
00145  = #define      REG_NOTEOL      00002
00146  = #define      REG_STARTEND    00004
00147  = #define      REG_TRACE       00400   // tracing of execution
00148  = #define      REG_LARGE       01000   // force large representation
00149  = #define      REG_BACKR       02000   // force use of backref code
00150  *
00151  * We put this here so we can exploit knowledge of the state representation
00152  * when choosing which matcher to call.  Also, by this point the matchers
00153  * have been prototyped.
00154  */
00155 int     __stdcall                       /* 0 success, REG_NOMATCH failure */
00156 regexec(preg, string, nmatch, pmatch, eflags)
00157 const regex_t *preg;
00158 const char *string;
00159 size_t nmatch;
00160 regmatch_t pmatch[];
00161 int eflags;
00162 {
00163         register struct re_guts *g = preg->re_g;
00164 #ifdef REDEBUG
00165 #       define  GOODFLAGS(f)    (f)
00166 #else
00167 #       define  GOODFLAGS(f)    ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
00168 #endif
00169 
00170         if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
00171                 return(REG_BADPAT);
00172         assert(!(g->iflags&BAD));
00173         if (g->iflags&BAD)              /* backstop for no-debug case */
00174                 return(REG_BADPAT);
00175         eflags = GOODFLAGS(eflags);
00176 
00177         if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
00178                 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
00179         else
00180                 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
00181 }

Generated on Sun Mar 12 23:56:30 2006 for ScriptBasic by  doxygen 1.4.6-NO