TRE: Add DragonFly README files
[dragonfly.git] / lib / libc / regex / regex2.h
1 /*-
2  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3  * Copyright (c) 1992, 1993, 1994
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Henry Spencer.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)regex2.h    8.4 (Berkeley) 3/20/94
34  * $FreeBSD: src/lib/libc/regex/regex2.h,v 1.11 2007/01/09 00:28:04 imp Exp $
35  * $DragonFly: src/lib/libc/regex/regex2.h,v 1.2 2003/06/17 04:26:44 dillon Exp $
36  */
37
38 /*
39  * First, the stuff that ends up in the outside-world include file
40  = typedef off_t regoff_t;
41  = typedef struct {
42  =      int re_magic;
43  =      size_t re_nsub;         // number of parenthesized subexpressions
44  =      const char *re_endp;    // end pointer for REG_PEND
45  =      struct re_guts *re_g;   // none of your business :-)
46  = } regex_t;
47  = typedef struct {
48  =      regoff_t rm_so;         // start of match
49  =      regoff_t rm_eo;         // end of match
50  = } regmatch_t;
51  */
52 /*
53  * internals of regex_t
54  */
55 #define MAGIC1  ((('r'^0200)<<8) | 'e')
56
57 /*
58  * The internal representation is a *strip*, a sequence of
59  * operators ending with an endmarker.  (Some terminology etc. is a
60  * historical relic of earlier versions which used multiple strips.)
61  * Certain oddities in the representation are there to permit running
62  * the machinery backwards; in particular, any deviation from sequential
63  * flow must be marked at both its source and its destination.  Some
64  * fine points:
65  *
66  * - OPLUS_ and O_PLUS are *inside* the loop they create.
67  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
68  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
69  *   OOR1 and OOR2 are respectively the end and the beginning of one of
70  *   the branches.  Note that there is an implicit OOR2 following OCH_
71  *   and an implicit OOR1 preceding O_CH.
72  *
73  * In state representations, an operator's bit is on to signify a state
74  * immediately *preceding* "execution" of that operator.
75  */
76 typedef unsigned long sop;      /* strip operator */
77 typedef long sopno;
78 #define OPRMASK 0xf8000000L
79 #define OPDMASK 0x07ffffffL
80 #define OPSHIFT ((unsigned)27)
81 #define OP(n)   ((n)&OPRMASK)
82 #define OPND(n) ((n)&OPDMASK)
83 #define SOP(op, opnd)   ((op)|(opnd))
84 /* operators                       meaning      operand                 */
85 /*                                              (back, fwd are offsets) */
86 #define OEND    (1L<<OPSHIFT)   /* endmarker    -                       */
87 #define OCHAR   (2L<<OPSHIFT)   /* character    wide character          */
88 #define OBOL    (3L<<OPSHIFT)   /* left anchor  -                       */
89 #define OEOL    (4L<<OPSHIFT)   /* right anchor -                       */
90 #define OANY    (5L<<OPSHIFT)   /* .            -                       */
91 #define OANYOF  (6L<<OPSHIFT)   /* [...]        set number              */
92 #define OBACK_  (7L<<OPSHIFT)   /* begin \d     paren number            */
93 #define O_BACK  (8L<<OPSHIFT)   /* end \d       paren number            */
94 #define OPLUS_  (9L<<OPSHIFT)   /* + prefix     fwd to suffix           */
95 #define O_PLUS  (10L<<OPSHIFT)  /* + suffix     back to prefix          */
96 #define OQUEST_ (11L<<OPSHIFT)  /* ? prefix     fwd to suffix           */
97 #define O_QUEST (12L<<OPSHIFT)  /* ? suffix     back to prefix          */
98 #define OLPAREN (13L<<OPSHIFT)  /* (            fwd to )                */
99 #define ORPAREN (14L<<OPSHIFT)  /* )            back to (               */
100 #define OCH_    (15L<<OPSHIFT)  /* begin choice fwd to OOR2             */
101 #define OOR1    (16L<<OPSHIFT)  /* | pt. 1      back to OOR1 or OCH_    */
102 #define OOR2    (17L<<OPSHIFT)  /* | pt. 2      fwd to OOR2 or O_CH     */
103 #define O_CH    (18L<<OPSHIFT)  /* end choice   back to OOR1            */
104 #define OBOW    (19L<<OPSHIFT)  /* begin word   -                       */
105 #define OEOW    (20L<<OPSHIFT)  /* end word     -                       */
106
107 /*
108  * Structures for [] character-set representation.
109  */
110 typedef struct {
111         wint_t          min;
112         wint_t          max;
113 } crange;
114 typedef struct {
115         unsigned char   bmp[NC / 8];
116         wctype_t        *types;
117         int             ntypes;
118         wint_t          *wides;
119         int             nwides;
120         crange          *ranges;
121         int             nranges;
122         int             invert;
123         int             icase;
124 } cset;
125
126 static int
127 CHIN1(cset *cs, wint_t ch)
128 {
129         int i;
130
131         assert(ch >= 0);
132         if (ch < NC)
133                 return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
134                     cs->invert);
135         for (i = 0; i < cs->nwides; i++)
136                 if (ch == cs->wides[i])
137                         return (!cs->invert);
138         for (i = 0; i < cs->nranges; i++)
139                 if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
140                         return (!cs->invert);
141         for (i = 0; i < cs->ntypes; i++)
142                 if (iswctype(ch, cs->types[i]))
143                         return (!cs->invert);
144         return (cs->invert);
145 }
146
147 static __inline int
148 CHIN(cset *cs, wint_t ch)
149 {
150
151         assert(ch >= 0);
152         if (ch < NC)
153                 return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
154                     cs->invert);
155         else if (cs->icase)
156                 return (CHIN1(cs, ch) || CHIN1(cs, towlower(ch)) ||
157                     CHIN1(cs, towupper(ch)));
158         else
159                 return (CHIN1(cs, ch));
160 }
161
162 /*
163  * main compiled-expression structure
164  */
165 struct re_guts {
166         int magic;
167 #               define  MAGIC2  ((('R'^0200)<<8)|'E')
168         sop *strip;             /* malloced area for strip */
169         int ncsets;             /* number of csets in use */
170         cset *sets;             /* -> cset [ncsets] */
171         int cflags;             /* copy of regcomp() cflags argument */
172         sopno nstates;          /* = number of sops */
173         sopno firststate;       /* the initial OEND (normally 0) */
174         sopno laststate;        /* the final OEND */
175         int iflags;             /* internal flags */
176 #               define  USEBOL  01      /* used ^ */
177 #               define  USEEOL  02      /* used $ */
178 #               define  BAD     04      /* something wrong */
179         int nbol;               /* number of ^ used */
180         int neol;               /* number of $ used */
181         char *must;             /* match must contain this string */
182         int moffset;            /* latest point at which must may be located */
183         int *charjump;          /* Boyer-Moore char jump table */
184         int *matchjump;         /* Boyer-Moore match jump table */
185         int mlen;               /* length of must */
186         size_t nsub;            /* copy of re_nsub */
187         int backrefs;           /* does it use back references? */
188         sopno nplus;            /* how deep does it nest +s? */
189 };
190
191 /* misc utilities */
192 #define OUT     (CHAR_MIN - 1)  /* a non-character value */
193 #define ISWORD(c)       (iswalnum((uch)(c)) || (c) == '_')