1*6545ccd2Stb /* $OpenBSD: regex2.h,v 1.12 2021/01/03 17:07:58 tb Exp $ */
2df930be7Sderaadt
3df930be7Sderaadt /*-
4df930be7Sderaadt * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5df930be7Sderaadt * Copyright (c) 1992, 1993, 1994
6df930be7Sderaadt * The Regents of the University of California. All rights reserved.
7df930be7Sderaadt *
8df930be7Sderaadt * This code is derived from software contributed to Berkeley by
9df930be7Sderaadt * Henry Spencer.
10df930be7Sderaadt *
11df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
12df930be7Sderaadt * modification, are permitted provided that the following conditions
13df930be7Sderaadt * are met:
14df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
15df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
16df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
17df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
18df930be7Sderaadt * documentation and/or other materials provided with the distribution.
196580fee3Smillert * 3. Neither the name of the University nor the names of its contributors
20df930be7Sderaadt * may be used to endorse or promote products derived from this software
21df930be7Sderaadt * without specific prior written permission.
22df930be7Sderaadt *
23df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df930be7Sderaadt * SUCH DAMAGE.
34442a6afdSmillert *
35442a6afdSmillert * @(#)regex2.h 8.4 (Berkeley) 3/20/94
36df930be7Sderaadt */
37df930be7Sderaadt
38df930be7Sderaadt /*
39df930be7Sderaadt * internals of regex_t
40df930be7Sderaadt */
41df930be7Sderaadt #define MAGIC1 ((('r'^0200)<<8) | 'e')
42df930be7Sderaadt
43df930be7Sderaadt /*
44df930be7Sderaadt * The internal representation is a *strip*, a sequence of
45df930be7Sderaadt * operators ending with an endmarker. (Some terminology etc. is a
46df930be7Sderaadt * historical relic of earlier versions which used multiple strips.)
47df930be7Sderaadt * Certain oddities in the representation are there to permit running
48df930be7Sderaadt * the machinery backwards; in particular, any deviation from sequential
49df930be7Sderaadt * flow must be marked at both its source and its destination. Some
50df930be7Sderaadt * fine points:
51df930be7Sderaadt *
52df930be7Sderaadt * - OPLUS_ and O_PLUS are *inside* the loop they create.
53df930be7Sderaadt * - OQUEST_ and O_QUEST are *outside* the bypass they create.
54df930be7Sderaadt * - OCH_ and O_CH are *outside* the multi-way branch they create, while
55df930be7Sderaadt * OOR1 and OOR2 are respectively the end and the beginning of one of
56df930be7Sderaadt * the branches. Note that there is an implicit OOR2 following OCH_
57df930be7Sderaadt * and an implicit OOR1 preceding O_CH.
58df930be7Sderaadt *
59df930be7Sderaadt * In state representations, an operator's bit is on to signify a state
60df930be7Sderaadt * immediately *preceding* "execution" of that operator.
61df930be7Sderaadt */
62442a6afdSmillert typedef unsigned long sop; /* strip operator */
63442a6afdSmillert typedef long sopno;
64621b3d91Stholo #define OPRMASK 0xf8000000LU
65621b3d91Stholo #define OPDMASK 0x07ffffffLU
66df930be7Sderaadt #define OPSHIFT ((unsigned)27)
67df930be7Sderaadt #define OP(n) ((n)&OPRMASK)
68df930be7Sderaadt #define OPND(n) ((n)&OPDMASK)
69df930be7Sderaadt #define SOP(op, opnd) ((op)|(opnd))
70df930be7Sderaadt /* operators meaning operand */
71df930be7Sderaadt /* (back, fwd are offsets) */
72621b3d91Stholo #define OEND (1LU<<OPSHIFT) /* endmarker - */
73621b3d91Stholo #define OCHAR (2LU<<OPSHIFT) /* character unsigned char */
74621b3d91Stholo #define OBOL (3LU<<OPSHIFT) /* left anchor - */
75621b3d91Stholo #define OEOL (4LU<<OPSHIFT) /* right anchor - */
76621b3d91Stholo #define OANY (5LU<<OPSHIFT) /* . - */
77621b3d91Stholo #define OANYOF (6LU<<OPSHIFT) /* [...] set number */
78621b3d91Stholo #define OBACK_ (7LU<<OPSHIFT) /* begin \d paren number */
79621b3d91Stholo #define O_BACK (8LU<<OPSHIFT) /* end \d paren number */
80621b3d91Stholo #define OPLUS_ (9LU<<OPSHIFT) /* + prefix fwd to suffix */
81621b3d91Stholo #define O_PLUS (10LU<<OPSHIFT) /* + suffix back to prefix */
82621b3d91Stholo #define OQUEST_ (11LU<<OPSHIFT) /* ? prefix fwd to suffix */
83621b3d91Stholo #define O_QUEST (12LU<<OPSHIFT) /* ? suffix back to prefix */
84621b3d91Stholo #define OLPAREN (13LU<<OPSHIFT) /* ( fwd to ) */
85621b3d91Stholo #define ORPAREN (14LU<<OPSHIFT) /* ) back to ( */
86621b3d91Stholo #define OCH_ (15LU<<OPSHIFT) /* begin choice fwd to OOR2 */
87621b3d91Stholo #define OOR1 (16LU<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */
88621b3d91Stholo #define OOR2 (17LU<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */
89621b3d91Stholo #define O_CH (18LU<<OPSHIFT) /* end choice back to OOR1 */
90621b3d91Stholo #define OBOW (19LU<<OPSHIFT) /* begin word - */
91621b3d91Stholo #define OEOW (20LU<<OPSHIFT) /* end word - */
92df930be7Sderaadt
93df930be7Sderaadt /*
94df930be7Sderaadt * Structure for [] character-set representation. Character sets are
95df930be7Sderaadt * done as bit vectors, grouped 8 to a byte vector for compactness.
96df930be7Sderaadt * The individual set therefore has both a pointer to the byte vector
97df930be7Sderaadt * and a mask to pick out the relevant bit of each byte. A hash code
98df930be7Sderaadt * simplifies testing whether two sets could be identical.
99df930be7Sderaadt *
100df930be7Sderaadt * This will get trickier for multicharacter collating elements. As
101df930be7Sderaadt * preliminary hooks for dealing with such things, we also carry along
102df930be7Sderaadt * a string of multi-character elements, and decide the size of the
103df930be7Sderaadt * vectors at run time.
104df930be7Sderaadt */
105df930be7Sderaadt typedef struct {
106df930be7Sderaadt uch *ptr; /* -> uch [csetsize] */
107df930be7Sderaadt uch mask; /* bit within array */
108df930be7Sderaadt uch hash; /* hash code */
109df930be7Sderaadt } cset;
110745d0dd7Stb
111745d0dd7Stb static inline void
CHadd(cset * cs,char c)112745d0dd7Stb CHadd(cset *cs, char c)
113745d0dd7Stb {
114745d0dd7Stb cs->ptr[(uch)c] |= cs->mask;
115745d0dd7Stb cs->hash += c;
116745d0dd7Stb }
117745d0dd7Stb
118745d0dd7Stb static inline void
CHsub(cset * cs,char c)119745d0dd7Stb CHsub(cset *cs, char c)
120745d0dd7Stb {
121745d0dd7Stb cs->ptr[(uch)c] &= ~cs->mask;
122745d0dd7Stb cs->hash -= c;
123745d0dd7Stb }
124745d0dd7Stb
125*6545ccd2Stb static inline int
CHIN(const cset * cs,char c)126745d0dd7Stb CHIN(const cset *cs, char c)
127745d0dd7Stb {
128*6545ccd2Stb return (cs->ptr[(uch)c] & cs->mask) != 0;
129745d0dd7Stb }
130df930be7Sderaadt
131df930be7Sderaadt /*
132df930be7Sderaadt * main compiled-expression structure
133df930be7Sderaadt */
134df930be7Sderaadt struct re_guts {
135df930be7Sderaadt int magic;
136df930be7Sderaadt # define MAGIC2 ((('R'^0200)<<8)|'E')
137df930be7Sderaadt sop *strip; /* malloced area for strip */
138df930be7Sderaadt int csetsize; /* number of bits in a cset vector */
139df930be7Sderaadt int ncsets; /* number of csets in use */
140df930be7Sderaadt cset *sets; /* -> cset [ncsets] */
141df930be7Sderaadt uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */
142df930be7Sderaadt int cflags; /* copy of regcomp() cflags argument */
143df930be7Sderaadt sopno nstates; /* = number of sops */
144df930be7Sderaadt sopno firststate; /* the initial OEND (normally 0) */
145df930be7Sderaadt sopno laststate; /* the final OEND */
146df930be7Sderaadt int iflags; /* internal flags */
147df930be7Sderaadt # define USEBOL 01 /* used ^ */
148df930be7Sderaadt # define USEEOL 02 /* used $ */
149df930be7Sderaadt # define BAD 04 /* something wrong */
150df930be7Sderaadt int nbol; /* number of ^ used */
151df930be7Sderaadt int neol; /* number of $ used */
152df930be7Sderaadt char *must; /* match must contain this string */
153df930be7Sderaadt int mlen; /* length of must */
154df930be7Sderaadt size_t nsub; /* copy of re_nsub */
155df930be7Sderaadt int backrefs; /* does it use back references? */
156df930be7Sderaadt sopno nplus; /* how deep does it nest +s? */
157df930be7Sderaadt };
158df930be7Sderaadt
159df930be7Sderaadt /* misc utilities */
160df930be7Sderaadt #define OUT (CHAR_MAX+1) /* a non-character value */
161df930be7Sderaadt #define ISWORD(c) (isalnum(c) || (c) == '_')
162