158f0484fSRodney W. Grimes /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro 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 * @(#)regex2.h 8.4 (Berkeley) 3/20/94 366049d9f0SDaniel C. Sobral * $FreeBSD$ 3758f0484fSRodney W. Grimes */ 3858f0484fSRodney W. Grimes 3958f0484fSRodney W. Grimes /* 4058f0484fSRodney W. Grimes * First, the stuff that ends up in the outside-world include file 4158f0484fSRodney W. Grimes = typedef off_t regoff_t; 4258f0484fSRodney W. Grimes = typedef struct { 4358f0484fSRodney W. Grimes = int re_magic; 4458f0484fSRodney W. Grimes = size_t re_nsub; // number of parenthesized subexpressions 4558f0484fSRodney W. Grimes = const char *re_endp; // end pointer for REG_PEND 4658f0484fSRodney W. Grimes = struct re_guts *re_g; // none of your business :-) 4758f0484fSRodney W. Grimes = } regex_t; 4858f0484fSRodney W. Grimes = typedef struct { 4958f0484fSRodney W. Grimes = regoff_t rm_so; // start of match 5058f0484fSRodney W. Grimes = regoff_t rm_eo; // end of match 5158f0484fSRodney W. Grimes = } regmatch_t; 5258f0484fSRodney W. Grimes */ 5358f0484fSRodney W. Grimes /* 5458f0484fSRodney W. Grimes * internals of regex_t 5558f0484fSRodney W. Grimes */ 5658f0484fSRodney W. Grimes #define MAGIC1 ((('r'^0200)<<8) | 'e') 5758f0484fSRodney W. Grimes 5858f0484fSRodney W. Grimes /* 5958f0484fSRodney W. Grimes * The internal representation is a *strip*, a sequence of 6058f0484fSRodney W. Grimes * operators ending with an endmarker. (Some terminology etc. is a 6158f0484fSRodney W. Grimes * historical relic of earlier versions which used multiple strips.) 6258f0484fSRodney W. Grimes * Certain oddities in the representation are there to permit running 6358f0484fSRodney W. Grimes * the machinery backwards; in particular, any deviation from sequential 6458f0484fSRodney W. Grimes * flow must be marked at both its source and its destination. Some 6558f0484fSRodney W. Grimes * fine points: 6658f0484fSRodney W. Grimes * 6758f0484fSRodney W. Grimes * - OPLUS_ and O_PLUS are *inside* the loop they create. 6858f0484fSRodney W. Grimes * - OQUEST_ and O_QUEST are *outside* the bypass they create. 6958f0484fSRodney W. Grimes * - OCH_ and O_CH are *outside* the multi-way branch they create, while 7058f0484fSRodney W. Grimes * OOR1 and OOR2 are respectively the end and the beginning of one of 7158f0484fSRodney W. Grimes * the branches. Note that there is an implicit OOR2 following OCH_ 7258f0484fSRodney W. Grimes * and an implicit OOR1 preceding O_CH. 7358f0484fSRodney W. Grimes * 7458f0484fSRodney W. Grimes * In state representations, an operator's bit is on to signify a state 7558f0484fSRodney W. Grimes * immediately *preceding* "execution" of that operator. 7658f0484fSRodney W. Grimes */ 7758f0484fSRodney W. Grimes typedef unsigned long sop; /* strip operator */ 788d0f9a93SPedro F. Giffuni typedef unsigned long sopno; 79cfc1614aSJohn Birrell #define OPRMASK 0xf8000000L 80cfc1614aSJohn Birrell #define OPDMASK 0x07ffffffL 8158f0484fSRodney W. Grimes #define OPSHIFT ((unsigned)27) 8258f0484fSRodney W. Grimes #define OP(n) ((n)&OPRMASK) 8358f0484fSRodney W. Grimes #define OPND(n) ((n)&OPDMASK) 8458f0484fSRodney W. Grimes #define SOP(op, opnd) ((op)|(opnd)) 8558f0484fSRodney W. Grimes /* operators meaning operand */ 8658f0484fSRodney W. Grimes /* (back, fwd are offsets) */ 87cfc1614aSJohn Birrell #define OEND (1L<<OPSHIFT) /* endmarker - */ 88e5996857STim J. Robbins #define OCHAR (2L<<OPSHIFT) /* character wide character */ 89cfc1614aSJohn Birrell #define OBOL (3L<<OPSHIFT) /* left anchor - */ 90cfc1614aSJohn Birrell #define OEOL (4L<<OPSHIFT) /* right anchor - */ 91cfc1614aSJohn Birrell #define OANY (5L<<OPSHIFT) /* . - */ 92cfc1614aSJohn Birrell #define OANYOF (6L<<OPSHIFT) /* [...] set number */ 93cfc1614aSJohn Birrell #define OBACK_ (7L<<OPSHIFT) /* begin \d paren number */ 94cfc1614aSJohn Birrell #define O_BACK (8L<<OPSHIFT) /* end \d paren number */ 95cfc1614aSJohn Birrell #define OPLUS_ (9L<<OPSHIFT) /* + prefix fwd to suffix */ 96cfc1614aSJohn Birrell #define O_PLUS (10L<<OPSHIFT) /* + suffix back to prefix */ 97cfc1614aSJohn Birrell #define OQUEST_ (11L<<OPSHIFT) /* ? prefix fwd to suffix */ 98cfc1614aSJohn Birrell #define O_QUEST (12L<<OPSHIFT) /* ? suffix back to prefix */ 99cfc1614aSJohn Birrell #define OLPAREN (13L<<OPSHIFT) /* ( fwd to ) */ 100cfc1614aSJohn Birrell #define ORPAREN (14L<<OPSHIFT) /* ) back to ( */ 101cfc1614aSJohn Birrell #define OCH_ (15L<<OPSHIFT) /* begin choice fwd to OOR2 */ 102cfc1614aSJohn Birrell #define OOR1 (16L<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */ 103cfc1614aSJohn Birrell #define OOR2 (17L<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */ 104cfc1614aSJohn Birrell #define O_CH (18L<<OPSHIFT) /* end choice back to OOR1 */ 105cfc1614aSJohn Birrell #define OBOW (19L<<OPSHIFT) /* begin word - */ 106cfc1614aSJohn Birrell #define OEOW (20L<<OPSHIFT) /* end word - */ 10758f0484fSRodney W. Grimes 10858f0484fSRodney W. Grimes /* 109e5996857STim J. Robbins * Structures for [] character-set representation. 11058f0484fSRodney W. Grimes */ 11158f0484fSRodney W. Grimes typedef struct { 112e5996857STim J. Robbins wint_t min; 113e5996857STim J. Robbins wint_t max; 114e5996857STim J. Robbins } crange; 115e5996857STim J. Robbins typedef struct { 116e5996857STim J. Robbins unsigned char bmp[NC / 8]; 117e5996857STim J. Robbins wctype_t *types; 1188d0f9a93SPedro F. Giffuni unsigned int ntypes; 119e5996857STim J. Robbins wint_t *wides; 1208d0f9a93SPedro F. Giffuni unsigned int nwides; 121e5996857STim J. Robbins crange *ranges; 1228d0f9a93SPedro F. Giffuni unsigned int nranges; 123e5996857STim J. Robbins int invert; 124e5996857STim J. Robbins int icase; 12558f0484fSRodney W. Grimes } cset; 126e5996857STim J. Robbins 127e5996857STim J. Robbins static int 12869053d66SStefan Farfeleder CHIN1(cset *cs, wint_t ch) 129e5996857STim J. Robbins { 1308d0f9a93SPedro F. Giffuni unsigned int i; 131e5996857STim J. Robbins 132e5996857STim J. Robbins assert(ch >= 0); 133e5996857STim J. Robbins if (ch < NC) 134e5996857STim J. Robbins return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^ 135e5996857STim J. Robbins cs->invert); 136e5996857STim J. Robbins for (i = 0; i < cs->nwides; i++) 137e5996857STim J. Robbins if (ch == cs->wides[i]) 138e5996857STim J. Robbins return (!cs->invert); 139e5996857STim J. Robbins for (i = 0; i < cs->nranges; i++) 140e5996857STim J. Robbins if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max) 141e5996857STim J. Robbins return (!cs->invert); 142e5996857STim J. Robbins for (i = 0; i < cs->ntypes; i++) 143e5996857STim J. Robbins if (iswctype(ch, cs->types[i])) 144e5996857STim J. Robbins return (!cs->invert); 145e5996857STim J. Robbins return (cs->invert); 146e5996857STim J. Robbins } 147e5996857STim J. Robbins 148e5996857STim J. Robbins static __inline int 14969053d66SStefan Farfeleder CHIN(cset *cs, wint_t ch) 150e5996857STim J. Robbins { 151e5996857STim J. Robbins 152e5996857STim J. Robbins assert(ch >= 0); 153e5996857STim J. Robbins if (ch < NC) 154e5996857STim J. Robbins return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^ 155e5996857STim J. Robbins cs->invert); 156e5996857STim J. Robbins else if (cs->icase) 157e5996857STim J. Robbins return (CHIN1(cs, ch) || CHIN1(cs, towlower(ch)) || 158e5996857STim J. Robbins CHIN1(cs, towupper(ch))); 159e5996857STim J. Robbins else 160e5996857STim J. Robbins return (CHIN1(cs, ch)); 161e5996857STim J. Robbins } 16258f0484fSRodney W. Grimes 16358f0484fSRodney W. Grimes /* 16458f0484fSRodney W. Grimes * main compiled-expression structure 16558f0484fSRodney W. Grimes */ 16658f0484fSRodney W. Grimes struct re_guts { 16758f0484fSRodney W. Grimes int magic; 16858f0484fSRodney W. Grimes # define MAGIC2 ((('R'^0200)<<8)|'E') 16958f0484fSRodney W. Grimes sop *strip; /* malloced area for strip */ 1708d0f9a93SPedro F. Giffuni unsigned int ncsets; /* number of csets in use */ 17158f0484fSRodney W. Grimes cset *sets; /* -> cset [ncsets] */ 17258f0484fSRodney W. Grimes int cflags; /* copy of regcomp() cflags argument */ 17358f0484fSRodney W. Grimes sopno nstates; /* = number of sops */ 17458f0484fSRodney W. Grimes sopno firststate; /* the initial OEND (normally 0) */ 17558f0484fSRodney W. Grimes sopno laststate; /* the final OEND */ 17658f0484fSRodney W. Grimes int iflags; /* internal flags */ 17758f0484fSRodney W. Grimes # define USEBOL 01 /* used ^ */ 17858f0484fSRodney W. Grimes # define USEEOL 02 /* used $ */ 17958f0484fSRodney W. Grimes # define BAD 04 /* something wrong */ 18058f0484fSRodney W. Grimes int nbol; /* number of ^ used */ 18158f0484fSRodney W. Grimes int neol; /* number of $ used */ 18258f0484fSRodney W. Grimes char *must; /* match must contain this string */ 183e6a886d8SDaniel C. Sobral int moffset; /* latest point at which must may be located */ 1846049d9f0SDaniel C. Sobral int *charjump; /* Boyer-Moore char jump table */ 1856049d9f0SDaniel C. Sobral int *matchjump; /* Boyer-Moore match jump table */ 18658f0484fSRodney W. Grimes int mlen; /* length of must */ 18758f0484fSRodney W. Grimes size_t nsub; /* copy of re_nsub */ 18858f0484fSRodney W. Grimes int backrefs; /* does it use back references? */ 18958f0484fSRodney W. Grimes sopno nplus; /* how deep does it nest +s? */ 19058f0484fSRodney W. Grimes }; 19158f0484fSRodney W. Grimes 19258f0484fSRodney W. Grimes /* misc utilities */ 1930853006fSTim J. Robbins #define OUT (CHAR_MIN - 1) /* a non-character value */ 19415ae9efaSKyle Evans #define IGN (CHAR_MIN - 2) 195e5996857STim J. Robbins #define ISWORD(c) (iswalnum((uch)(c)) || (c) == '_') 196