158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
458f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 Henry Spencer.
558f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994
658f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved.
758f0484fSRodney W. Grimes *
858f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by
958f0484fSRodney W. Grimes * Henry Spencer.
1058f0484fSRodney W. Grimes *
1158f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
1258f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
1358f0484fSRodney W. Grimes * are met:
1458f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1658f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1758f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1858f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
2058f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
2158f0484fSRodney W. Grimes * without specific prior written permission.
2258f0484fSRodney W. Grimes *
2358f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2458f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2558f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2658f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2758f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2858f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2958f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3058f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3158f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3258f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3358f0484fSRodney W. Grimes * SUCH DAMAGE.
3458f0484fSRodney W. Grimes */
3558f0484fSRodney W. Grimes
3658f0484fSRodney W. Grimes /*
3758f0484fSRodney W. Grimes * First, the stuff that ends up in the outside-world include file
3858f0484fSRodney W. Grimes = typedef off_t regoff_t;
3958f0484fSRodney W. Grimes = typedef struct {
4058f0484fSRodney W. Grimes = int re_magic;
4158f0484fSRodney W. Grimes = size_t re_nsub; // number of parenthesized subexpressions
4258f0484fSRodney W. Grimes = const char *re_endp; // end pointer for REG_PEND
4358f0484fSRodney W. Grimes = struct re_guts *re_g; // none of your business :-)
4458f0484fSRodney W. Grimes = } regex_t;
4558f0484fSRodney W. Grimes = typedef struct {
4658f0484fSRodney W. Grimes = regoff_t rm_so; // start of match
4758f0484fSRodney W. Grimes = regoff_t rm_eo; // end of match
4858f0484fSRodney W. Grimes = } regmatch_t;
4958f0484fSRodney W. Grimes */
5058f0484fSRodney W. Grimes /*
5158f0484fSRodney W. Grimes * internals of regex_t
5258f0484fSRodney W. Grimes */
5358f0484fSRodney W. Grimes #define MAGIC1 ((('r'^0200)<<8) | 'e')
5458f0484fSRodney W. Grimes
5558f0484fSRodney W. Grimes /*
5658f0484fSRodney W. Grimes * The internal representation is a *strip*, a sequence of
5758f0484fSRodney W. Grimes * operators ending with an endmarker. (Some terminology etc. is a
5858f0484fSRodney W. Grimes * historical relic of earlier versions which used multiple strips.)
5958f0484fSRodney W. Grimes * Certain oddities in the representation are there to permit running
6058f0484fSRodney W. Grimes * the machinery backwards; in particular, any deviation from sequential
6158f0484fSRodney W. Grimes * flow must be marked at both its source and its destination. Some
6258f0484fSRodney W. Grimes * fine points:
6358f0484fSRodney W. Grimes *
6458f0484fSRodney W. Grimes * - OPLUS_ and O_PLUS are *inside* the loop they create.
6558f0484fSRodney W. Grimes * - OQUEST_ and O_QUEST are *outside* the bypass they create.
6658f0484fSRodney W. Grimes * - OCH_ and O_CH are *outside* the multi-way branch they create, while
6758f0484fSRodney W. Grimes * OOR1 and OOR2 are respectively the end and the beginning of one of
6858f0484fSRodney W. Grimes * the branches. Note that there is an implicit OOR2 following OCH_
6958f0484fSRodney W. Grimes * and an implicit OOR1 preceding O_CH.
7058f0484fSRodney W. Grimes *
7158f0484fSRodney W. Grimes * In state representations, an operator's bit is on to signify a state
7258f0484fSRodney W. Grimes * immediately *preceding* "execution" of that operator.
7358f0484fSRodney W. Grimes */
7458f0484fSRodney W. Grimes typedef unsigned long sop; /* strip operator */
758d0f9a93SPedro F. Giffuni typedef unsigned long sopno;
76cfc1614aSJohn Birrell #define OPRMASK 0xf8000000L
77cfc1614aSJohn Birrell #define OPDMASK 0x07ffffffL
7858f0484fSRodney W. Grimes #define OPSHIFT ((unsigned)27)
7958f0484fSRodney W. Grimes #define OP(n) ((n)&OPRMASK)
8058f0484fSRodney W. Grimes #define OPND(n) ((n)&OPDMASK)
8158f0484fSRodney W. Grimes #define SOP(op, opnd) ((op)|(opnd))
8258f0484fSRodney W. Grimes /* operators meaning operand */
8358f0484fSRodney W. Grimes /* (back, fwd are offsets) */
84cfc1614aSJohn Birrell #define OEND (1L<<OPSHIFT) /* endmarker - */
85e5996857STim J. Robbins #define OCHAR (2L<<OPSHIFT) /* character wide character */
86cfc1614aSJohn Birrell #define OBOL (3L<<OPSHIFT) /* left anchor - */
87cfc1614aSJohn Birrell #define OEOL (4L<<OPSHIFT) /* right anchor - */
88cfc1614aSJohn Birrell #define OANY (5L<<OPSHIFT) /* . - */
89cfc1614aSJohn Birrell #define OANYOF (6L<<OPSHIFT) /* [...] set number */
90cfc1614aSJohn Birrell #define OBACK_ (7L<<OPSHIFT) /* begin \d paren number */
91cfc1614aSJohn Birrell #define O_BACK (8L<<OPSHIFT) /* end \d paren number */
92cfc1614aSJohn Birrell #define OPLUS_ (9L<<OPSHIFT) /* + prefix fwd to suffix */
93cfc1614aSJohn Birrell #define O_PLUS (10L<<OPSHIFT) /* + suffix back to prefix */
94cfc1614aSJohn Birrell #define OQUEST_ (11L<<OPSHIFT) /* ? prefix fwd to suffix */
95cfc1614aSJohn Birrell #define O_QUEST (12L<<OPSHIFT) /* ? suffix back to prefix */
96cfc1614aSJohn Birrell #define OLPAREN (13L<<OPSHIFT) /* ( fwd to ) */
97cfc1614aSJohn Birrell #define ORPAREN (14L<<OPSHIFT) /* ) back to ( */
98cfc1614aSJohn Birrell #define OCH_ (15L<<OPSHIFT) /* begin choice fwd to OOR2 */
99cfc1614aSJohn Birrell #define OOR1 (16L<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */
100cfc1614aSJohn Birrell #define OOR2 (17L<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */
101cfc1614aSJohn Birrell #define O_CH (18L<<OPSHIFT) /* end choice back to OOR1 */
102cfc1614aSJohn Birrell #define OBOW (19L<<OPSHIFT) /* begin word - */
103cfc1614aSJohn Birrell #define OEOW (20L<<OPSHIFT) /* end word - */
104ca53e5aeSKyle Evans #define OBOS (21L<<OPSHIFT) /* begin subj. - */
105ca53e5aeSKyle Evans #define OEOS (22L<<OPSHIFT) /* end subj. - */
106*6b986646SKyle Evans #define OWBND (23L<<OPSHIFT) /* word bound - */
107*6b986646SKyle Evans #define ONWBND (24L<<OPSHIFT) /* not bound - */
10858f0484fSRodney W. Grimes
10958f0484fSRodney W. Grimes /*
110e5996857STim J. Robbins * Structures for [] character-set representation.
11158f0484fSRodney W. Grimes */
11258f0484fSRodney W. Grimes typedef struct {
113e5996857STim J. Robbins wint_t min;
114e5996857STim J. Robbins wint_t max;
115e5996857STim J. Robbins } crange;
116e5996857STim J. Robbins typedef struct {
117547bc083SYuri Pankov unsigned char bmp[NC_MAX / 8];
118e5996857STim J. Robbins wctype_t *types;
1198d0f9a93SPedro F. Giffuni unsigned int ntypes;
120e5996857STim J. Robbins wint_t *wides;
1218d0f9a93SPedro F. Giffuni unsigned int nwides;
122e5996857STim J. Robbins crange *ranges;
1238d0f9a93SPedro F. Giffuni unsigned int nranges;
124e5996857STim J. Robbins int invert;
125e5996857STim J. Robbins int icase;
12658f0484fSRodney W. Grimes } cset;
127e5996857STim J. Robbins
128e5996857STim J. Robbins static int
CHIN1(cset * cs,wint_t ch)12969053d66SStefan Farfeleder CHIN1(cset *cs, wint_t ch)
130e5996857STim J. Robbins {
1318d0f9a93SPedro F. Giffuni unsigned int i;
132e5996857STim J. Robbins
133e5996857STim J. Robbins assert(ch >= 0);
134e5996857STim J. Robbins if (ch < NC)
135e5996857STim J. Robbins return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
136e5996857STim J. Robbins cs->invert);
137547bc083SYuri Pankov for (i = 0; i < cs->nwides; i++) {
138547bc083SYuri Pankov if (cs->icase) {
139547bc083SYuri Pankov if (ch == towlower(cs->wides[i]) ||
140547bc083SYuri Pankov ch == towupper(cs->wides[i]))
141e5996857STim J. Robbins return (!cs->invert);
142547bc083SYuri Pankov } else if (ch == cs->wides[i])
143547bc083SYuri Pankov return (!cs->invert);
144547bc083SYuri Pankov }
145e5996857STim J. Robbins for (i = 0; i < cs->nranges; i++)
146e5996857STim J. Robbins if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
147e5996857STim J. Robbins return (!cs->invert);
148e5996857STim J. Robbins for (i = 0; i < cs->ntypes; i++)
149e5996857STim J. Robbins if (iswctype(ch, cs->types[i]))
150e5996857STim J. Robbins return (!cs->invert);
151e5996857STim J. Robbins return (cs->invert);
152e5996857STim J. Robbins }
153e5996857STim J. Robbins
154e5996857STim J. Robbins static __inline int
CHIN(cset * cs,wint_t ch)15569053d66SStefan Farfeleder CHIN(cset *cs, wint_t ch)
156e5996857STim J. Robbins {
157e5996857STim J. Robbins
158e5996857STim J. Robbins assert(ch >= 0);
159e5996857STim J. Robbins if (ch < NC)
160e5996857STim J. Robbins return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
161e5996857STim J. Robbins cs->invert);
162e5996857STim J. Robbins else if (cs->icase)
163e5996857STim J. Robbins return (CHIN1(cs, ch) || CHIN1(cs, towlower(ch)) ||
164e5996857STim J. Robbins CHIN1(cs, towupper(ch)));
165e5996857STim J. Robbins else
166e5996857STim J. Robbins return (CHIN1(cs, ch));
167e5996857STim J. Robbins }
16858f0484fSRodney W. Grimes
16958f0484fSRodney W. Grimes /*
17058f0484fSRodney W. Grimes * main compiled-expression structure
17158f0484fSRodney W. Grimes */
17258f0484fSRodney W. Grimes struct re_guts {
17358f0484fSRodney W. Grimes int magic;
17458f0484fSRodney W. Grimes # define MAGIC2 ((('R'^0200)<<8)|'E')
17558f0484fSRodney W. Grimes sop *strip; /* malloced area for strip */
1768d0f9a93SPedro F. Giffuni unsigned int ncsets; /* number of csets in use */
17758f0484fSRodney W. Grimes cset *sets; /* -> cset [ncsets] */
17858f0484fSRodney W. Grimes int cflags; /* copy of regcomp() cflags argument */
17958f0484fSRodney W. Grimes sopno nstates; /* = number of sops */
18058f0484fSRodney W. Grimes sopno firststate; /* the initial OEND (normally 0) */
18158f0484fSRodney W. Grimes sopno laststate; /* the final OEND */
18258f0484fSRodney W. Grimes int iflags; /* internal flags */
18358f0484fSRodney W. Grimes # define USEBOL 01 /* used ^ */
18458f0484fSRodney W. Grimes # define USEEOL 02 /* used $ */
18558f0484fSRodney W. Grimes # define BAD 04 /* something wrong */
18658f0484fSRodney W. Grimes int nbol; /* number of ^ used */
18758f0484fSRodney W. Grimes int neol; /* number of $ used */
18858f0484fSRodney W. Grimes char *must; /* match must contain this string */
189e6a886d8SDaniel C. Sobral int moffset; /* latest point at which must may be located */
1906049d9f0SDaniel C. Sobral int *charjump; /* Boyer-Moore char jump table */
1916049d9f0SDaniel C. Sobral int *matchjump; /* Boyer-Moore match jump table */
19258f0484fSRodney W. Grimes int mlen; /* length of must */
19358f0484fSRodney W. Grimes size_t nsub; /* copy of re_nsub */
19458f0484fSRodney W. Grimes int backrefs; /* does it use back references? */
19558f0484fSRodney W. Grimes sopno nplus; /* how deep does it nest +s? */
19658f0484fSRodney W. Grimes };
19758f0484fSRodney W. Grimes
19858f0484fSRodney W. Grimes /* misc utilities */
1990853006fSTim J. Robbins #define OUT (CHAR_MIN - 1) /* a non-character value */
20015ae9efaSKyle Evans #define IGN (CHAR_MIN - 2)
201e5996857STim J. Robbins #define ISWORD(c) (iswalnum((uch)(c)) || (c) == '_')
202