158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 Henry Spencer. 358f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 458f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 558f0484fSRodney W. Grimes * 658f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 758f0484fSRodney W. Grimes * Henry Spencer. 858f0484fSRodney W. Grimes * 958f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1058f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1158f0484fSRodney W. Grimes * are met: 1258f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1458f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1658f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1758f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1858f0484fSRodney W. Grimes * must display the following acknowledgement: 1958f0484fSRodney W. Grimes * This product includes software developed by the University of 2058f0484fSRodney W. Grimes * California, Berkeley and its contributors. 2158f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2258f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2358f0484fSRodney W. Grimes * without specific prior written permission. 2458f0484fSRodney W. Grimes * 2558f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2658f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2758f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2858f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2958f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3058f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3158f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3258f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3358f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3458f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3558f0484fSRodney W. Grimes * SUCH DAMAGE. 3658f0484fSRodney W. Grimes * 3758f0484fSRodney W. Grimes * @(#)regex2.h 8.4 (Berkeley) 3/20/94 386049d9f0SDaniel C. Sobral * $FreeBSD$ 3958f0484fSRodney W. Grimes */ 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes /* 4258f0484fSRodney W. Grimes * First, the stuff that ends up in the outside-world include file 4358f0484fSRodney W. Grimes = typedef off_t regoff_t; 4458f0484fSRodney W. Grimes = typedef struct { 4558f0484fSRodney W. Grimes = int re_magic; 4658f0484fSRodney W. Grimes = size_t re_nsub; // number of parenthesized subexpressions 4758f0484fSRodney W. Grimes = const char *re_endp; // end pointer for REG_PEND 4858f0484fSRodney W. Grimes = struct re_guts *re_g; // none of your business :-) 4958f0484fSRodney W. Grimes = } regex_t; 5058f0484fSRodney W. Grimes = typedef struct { 5158f0484fSRodney W. Grimes = regoff_t rm_so; // start of match 5258f0484fSRodney W. Grimes = regoff_t rm_eo; // end of match 5358f0484fSRodney W. Grimes = } regmatch_t; 5458f0484fSRodney W. Grimes */ 5558f0484fSRodney W. Grimes /* 5658f0484fSRodney W. Grimes * internals of regex_t 5758f0484fSRodney W. Grimes */ 5858f0484fSRodney W. Grimes #define MAGIC1 ((('r'^0200)<<8) | 'e') 5958f0484fSRodney W. Grimes 6058f0484fSRodney W. Grimes /* 6158f0484fSRodney W. Grimes * The internal representation is a *strip*, a sequence of 6258f0484fSRodney W. Grimes * operators ending with an endmarker. (Some terminology etc. is a 6358f0484fSRodney W. Grimes * historical relic of earlier versions which used multiple strips.) 6458f0484fSRodney W. Grimes * Certain oddities in the representation are there to permit running 6558f0484fSRodney W. Grimes * the machinery backwards; in particular, any deviation from sequential 6658f0484fSRodney W. Grimes * flow must be marked at both its source and its destination. Some 6758f0484fSRodney W. Grimes * fine points: 6858f0484fSRodney W. Grimes * 6958f0484fSRodney W. Grimes * - OPLUS_ and O_PLUS are *inside* the loop they create. 7058f0484fSRodney W. Grimes * - OQUEST_ and O_QUEST are *outside* the bypass they create. 7158f0484fSRodney W. Grimes * - OCH_ and O_CH are *outside* the multi-way branch they create, while 7258f0484fSRodney W. Grimes * OOR1 and OOR2 are respectively the end and the beginning of one of 7358f0484fSRodney W. Grimes * the branches. Note that there is an implicit OOR2 following OCH_ 7458f0484fSRodney W. Grimes * and an implicit OOR1 preceding O_CH. 7558f0484fSRodney W. Grimes * 7658f0484fSRodney W. Grimes * In state representations, an operator's bit is on to signify a state 7758f0484fSRodney W. Grimes * immediately *preceding* "execution" of that operator. 7858f0484fSRodney W. Grimes */ 7958f0484fSRodney W. Grimes typedef unsigned long sop; /* strip operator */ 8058f0484fSRodney W. Grimes typedef long sopno; 81cfc1614aSJohn Birrell #define OPRMASK 0xf8000000L 82cfc1614aSJohn Birrell #define OPDMASK 0x07ffffffL 8358f0484fSRodney W. Grimes #define OPSHIFT ((unsigned)27) 8458f0484fSRodney W. Grimes #define OP(n) ((n)&OPRMASK) 8558f0484fSRodney W. Grimes #define OPND(n) ((n)&OPDMASK) 8658f0484fSRodney W. Grimes #define SOP(op, opnd) ((op)|(opnd)) 8758f0484fSRodney W. Grimes /* operators meaning operand */ 8858f0484fSRodney W. Grimes /* (back, fwd are offsets) */ 89cfc1614aSJohn Birrell #define OEND (1L<<OPSHIFT) /* endmarker - */ 90e5996857STim J. Robbins #define OCHAR (2L<<OPSHIFT) /* character wide character */ 91cfc1614aSJohn Birrell #define OBOL (3L<<OPSHIFT) /* left anchor - */ 92cfc1614aSJohn Birrell #define OEOL (4L<<OPSHIFT) /* right anchor - */ 93cfc1614aSJohn Birrell #define OANY (5L<<OPSHIFT) /* . - */ 94cfc1614aSJohn Birrell #define OANYOF (6L<<OPSHIFT) /* [...] set number */ 95cfc1614aSJohn Birrell #define OBACK_ (7L<<OPSHIFT) /* begin \d paren number */ 96cfc1614aSJohn Birrell #define O_BACK (8L<<OPSHIFT) /* end \d paren number */ 97cfc1614aSJohn Birrell #define OPLUS_ (9L<<OPSHIFT) /* + prefix fwd to suffix */ 98cfc1614aSJohn Birrell #define O_PLUS (10L<<OPSHIFT) /* + suffix back to prefix */ 99cfc1614aSJohn Birrell #define OQUEST_ (11L<<OPSHIFT) /* ? prefix fwd to suffix */ 100cfc1614aSJohn Birrell #define O_QUEST (12L<<OPSHIFT) /* ? suffix back to prefix */ 101cfc1614aSJohn Birrell #define OLPAREN (13L<<OPSHIFT) /* ( fwd to ) */ 102cfc1614aSJohn Birrell #define ORPAREN (14L<<OPSHIFT) /* ) back to ( */ 103cfc1614aSJohn Birrell #define OCH_ (15L<<OPSHIFT) /* begin choice fwd to OOR2 */ 104cfc1614aSJohn Birrell #define OOR1 (16L<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */ 105cfc1614aSJohn Birrell #define OOR2 (17L<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */ 106cfc1614aSJohn Birrell #define O_CH (18L<<OPSHIFT) /* end choice back to OOR1 */ 107cfc1614aSJohn Birrell #define OBOW (19L<<OPSHIFT) /* begin word - */ 108cfc1614aSJohn Birrell #define OEOW (20L<<OPSHIFT) /* end word - */ 10958f0484fSRodney W. Grimes 11058f0484fSRodney W. Grimes /* 111e5996857STim J. Robbins * Structures for [] character-set representation. 11258f0484fSRodney W. Grimes */ 11358f0484fSRodney W. Grimes typedef struct { 114e5996857STim J. Robbins wint_t min; 115e5996857STim J. Robbins wint_t max; 116e5996857STim J. Robbins } crange; 117e5996857STim J. Robbins typedef struct { 118e5996857STim J. Robbins unsigned char bmp[NC / 8]; 119e5996857STim J. Robbins wctype_t *types; 120e5996857STim J. Robbins int ntypes; 121e5996857STim J. Robbins wint_t *wides; 122e5996857STim J. Robbins int nwides; 123e5996857STim J. Robbins crange *ranges; 124e5996857STim J. Robbins int nranges; 125e5996857STim J. Robbins int invert; 126e5996857STim J. Robbins int icase; 12758f0484fSRodney W. Grimes } cset; 128e5996857STim J. Robbins 129e5996857STim J. Robbins static int 130e5996857STim J. Robbins CHIN1(cs, ch) 131e5996857STim J. Robbins cset *cs; 132e5996857STim J. Robbins wint_t ch; 133e5996857STim J. Robbins { 134e5996857STim J. Robbins int i; 135e5996857STim J. Robbins 136e5996857STim J. Robbins assert(ch >= 0); 137e5996857STim J. Robbins if (ch < NC) 138e5996857STim J. Robbins return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^ 139e5996857STim J. Robbins cs->invert); 140e5996857STim J. Robbins for (i = 0; i < cs->nwides; i++) 141e5996857STim J. Robbins if (ch == cs->wides[i]) 142e5996857STim J. Robbins return (!cs->invert); 143e5996857STim J. Robbins for (i = 0; i < cs->nranges; i++) 144e5996857STim J. Robbins if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max) 145e5996857STim J. Robbins return (!cs->invert); 146e5996857STim J. Robbins for (i = 0; i < cs->ntypes; i++) 147e5996857STim J. Robbins if (iswctype(ch, cs->types[i])) 148e5996857STim J. Robbins return (!cs->invert); 149e5996857STim J. Robbins return (cs->invert); 150e5996857STim J. Robbins } 151e5996857STim J. Robbins 152e5996857STim J. Robbins static __inline int 153e5996857STim J. Robbins CHIN(cs, ch) 154e5996857STim J. Robbins cset *cs; 155e5996857STim J. Robbins 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 */ 17658f0484fSRodney W. Grimes 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 */ 200e5996857STim J. Robbins #define ISWORD(c) (iswalnum((uch)(c)) || (c) == '_') 201