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