regex2.h revision 92991
177218Sphk/*- 277218Sphk * Copyright (c) 1992, 1993, 1994 Henry Spencer. 377218Sphk * Copyright (c) 1992, 1993, 1994 477218Sphk * The Regents of the University of California. All rights reserved. 577218Sphk * 677218Sphk * This code is derived from software contributed to Berkeley by 777218Sphk * Henry Spencer. 877218Sphk * 977218Sphk * Redistribution and use in source and binary forms, with or without 1077218Sphk * modification, are permitted provided that the following conditions 1177218Sphk * are met: 1291454Sbrooks * 1. Redistributions of source code must retain the above copyright 1391454Sbrooks * notice, this list of conditions and the following disclaimer. 1477218Sphk * 2. Redistributions in binary form must reproduce the above copyright 1577218Sphk * notice, this list of conditions and the following disclaimer in the 1677218Sphk * documentation and/or other materials provided with the distribution. 1777218Sphk * 3. All advertising materials mentioning features or use of this software 1877218Sphk * must display the following acknowledgement: 1977218Sphk * This product includes software developed by the University of 2077218Sphk * California, Berkeley and its contributors. 2177218Sphk * 4. Neither the name of the University nor the names of its contributors 2277218Sphk * may be used to endorse or promote products derived from this software 2377218Sphk * without specific prior written permission. 2477218Sphk * 2577218Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2677218Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2777218Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2877218Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2977218Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3077218Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3177218Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3277218Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3377218Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3477218Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3577218Sphk * SUCH DAMAGE. 3677218Sphk * 3777218Sphk * @(#)regex2.h 8.4 (Berkeley) 3/20/94 3877218Sphk * $FreeBSD: head/lib/libc/regex/regex2.h 92991 2002-03-22 23:42:05Z obrien $ 3977218Sphk */ 4077218Sphk 4177218Sphk/* 4277218Sphk * First, the stuff that ends up in the outside-world include file 4377218Sphk = typedef off_t regoff_t; 4477218Sphk = typedef struct { 4577218Sphk = int re_magic; 4677218Sphk = size_t re_nsub; // number of parenthesized subexpressions 4777218Sphk = const char *re_endp; // end pointer for REG_PEND 4877218Sphk = struct re_guts *re_g; // none of your business :-) 4977218Sphk = } regex_t; 5077218Sphk = typedef struct { 5177218Sphk = regoff_t rm_so; // start of match 5277218Sphk = regoff_t rm_eo; // end of match 5377218Sphk = } regmatch_t; 5477218Sphk */ 5577218Sphk/* 5677218Sphk * internals of regex_t 5777218Sphk */ 5877218Sphk#define MAGIC1 ((('r'^0200)<<8) | 'e') 5977218Sphk 6077218Sphk/* 6177218Sphk * The internal representation is a *strip*, a sequence of 6277218Sphk * operators ending with an endmarker. (Some terminology etc. is a 6377218Sphk * historical relic of earlier versions which used multiple strips.) 6477218Sphk * Certain oddities in the representation are there to permit running 6577218Sphk * the machinery backwards; in particular, any deviation from sequential 6677218Sphk * flow must be marked at both its source and its destination. Some 6777218Sphk * fine points: 6877218Sphk * 6977218Sphk * - OPLUS_ and O_PLUS are *inside* the loop they create. 7077218Sphk * - OQUEST_ and O_QUEST are *outside* the bypass they create. 7177218Sphk * - OCH_ and O_CH are *outside* the multi-way branch they create, while 7277218Sphk * OOR1 and OOR2 are respectively the end and the beginning of one of 7377218Sphk * the branches. Note that there is an implicit OOR2 following OCH_ 7477218Sphk * and an implicit OOR1 preceding O_CH. 7577218Sphk * 7677218Sphk * In state representations, an operator's bit is on to signify a state 77138593Ssam * immediately *preceding* "execution" of that operator. 7877218Sphk */ 79138593Ssamtypedef unsigned long sop; /* strip operator */ 80116957Ssamtypedef long sopno; 8177218Sphk#define OPRMASK 0xf8000000L 82187801Ssam#define OPDMASK 0x07ffffffL 8377218Sphk#define OPSHIFT ((unsigned)27) 8477218Sphk#define OP(n) ((n)&OPRMASK) 8577218Sphk#define OPND(n) ((n)&OPDMASK) 8677218Sphk#define SOP(op, opnd) ((op)|(opnd)) 87146873Sjhb/* operators meaning operand */ 8877218Sphk/* (back, fwd are offsets) */ 8977218Sphk#define OEND (1L<<OPSHIFT) /* endmarker - */ 9077218Sphk#define OCHAR (2L<<OPSHIFT) /* character unsigned char */ 9177218Sphk#define OBOL (3L<<OPSHIFT) /* left anchor - */ 92155931Ssam#define OEOL (4L<<OPSHIFT) /* right anchor - */ 93173275Ssam#define OANY (5L<<OPSHIFT) /* . - */ 9477218Sphk#define OANYOF (6L<<OPSHIFT) /* [...] set number */ 9577218Sphk#define OBACK_ (7L<<OPSHIFT) /* begin \d paren number */ 96178354Ssam#define O_BACK (8L<<OPSHIFT) /* end \d paren number */ 9777218Sphk#define OPLUS_ (9L<<OPSHIFT) /* + prefix fwd to suffix */ 98178354Ssam#define O_PLUS (10L<<OPSHIFT) /* + suffix back to prefix */ 99178354Ssam#define OQUEST_ (11L<<OPSHIFT) /* ? prefix fwd to suffix */ 100178354Ssam#define O_QUEST (12L<<OPSHIFT) /* ? suffix back to prefix */ 101178354Ssam#define OLPAREN (13L<<OPSHIFT) /* ( fwd to ) */ 102178354Ssam#define ORPAREN (14L<<OPSHIFT) /* ) back to ( */ 103178354Ssam#define OCH_ (15L<<OPSHIFT) /* begin choice fwd to OOR2 */ 104178354Ssam#define OOR1 (16L<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */ 105178354Ssam#define OOR2 (17L<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */ 106178354Ssam#define O_CH (18L<<OPSHIFT) /* end choice back to OOR1 */ 107178354Ssam#define OBOW (19L<<OPSHIFT) /* begin word - */ 108178354Ssam#define OEOW (20L<<OPSHIFT) /* end word - */ 109178354Ssam 110178354Ssam/* 111178354Ssam * Structure for [] character-set representation. Character sets are 112178354Ssam * done as bit vectors, grouped 8 to a byte vector for compactness. 113178354Ssam * The individual set therefore has both a pointer to the byte vector 114178354Ssam * and a mask to pick out the relevant bit of each byte. A hash code 115178354Ssam * simplifies testing whether two sets could be identical. 116183261Ssam * 117183261Ssam * This will get trickier for multicharacter collating elements. As 118183261Ssam * preliminary hooks for dealing with such things, we also carry along 119183261Ssam * a string of multi-character elements, and decide the size of the 120183261Ssam * vectors at run time. 121178354Ssam */ 122178354Ssamtypedef struct { 123187801Ssam uch *ptr; /* -> uch [csetsize] */ 124187801Ssam uch mask; /* bit within array */ 125173275Ssam short hash; /* hash code */ 126173275Ssam size_t smultis; 127173275Ssam char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */ 128173275Ssam} cset; 129173275Ssam/* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */ 130173275Ssam#define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (uch)(c)) 131173275Ssam#define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (uch)(c)) 132173275Ssam#define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask) 133178354Ssam#define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */ 134178354Ssam#define MCsub(p, cs, cp) mcsub(p, cs, cp) 135178354Ssam#define MCin(p, cs, cp) mcin(p, cs, cp) 136173275Ssam 137173275Ssam/* stuff for character categories */ 138178354Ssamtypedef unsigned char cat_t; 139173275Ssam 140173275Ssam/* 141173275Ssam * main compiled-expression structure 14277218Sphk */ 14377218Sphkstruct re_guts { 14477218Sphk int magic; 145178354Ssam# define MAGIC2 ((('R'^0200)<<8)|'E') 146178354Ssam sop *strip; /* malloced area for strip */ 147178354Ssam int csetsize; /* number of bits in a cset vector */ 148178354Ssam int ncsets; /* number of csets in use */ 149178354Ssam cset *sets; /* -> cset [ncsets] */ 15077218Sphk uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */ 151187801Ssam int cflags; /* copy of regcomp() cflags argument */ 152178354Ssam sopno nstates; /* = number of sops */ 153178354Ssam sopno firststate; /* the initial OEND (normally 0) */ 154178354Ssam sopno laststate; /* the final OEND */ 155178354Ssam int iflags; /* internal flags */ 156178354Ssam# define USEBOL 01 /* used ^ */ 157178354Ssam# define USEEOL 02 /* used $ */ 158173275Ssam# define BAD 04 /* something wrong */ 159173275Ssam int nbol; /* number of ^ used */ 160178354Ssam int neol; /* number of $ used */ 161173275Ssam int ncategories; /* how many character categories */ 162173275Ssam cat_t *categories; /* ->catspace[-CHAR_MIN] */ 163170531Ssam char *must; /* match must contain this string */ 164173275Ssam int moffset; /* latest point at which must may be located */ 165173275Ssam int *charjump; /* Boyer-Moore char jump table */ 166173275Ssam int *matchjump; /* Boyer-Moore match jump table */ 167173275Ssam int mlen; /* length of must */ 168173275Ssam size_t nsub; /* copy of re_nsub */ 169173275Ssam int backrefs; /* does it use back references? */ 170173275Ssam sopno nplus; /* how deep does it nest +s? */ 171173275Ssam /* catspace must be last */ 172173275Ssam cat_t catspace[1]; /* actually [NC] */ 173173275Ssam}; 174170531Ssam 175170531Ssam/* misc utilities */ 176170531Ssam#define OUT (CHAR_MAX+1) /* a non-character value */ 177170531Ssam#define ISWORD(c) (isalnum((uch)(c)) || (c) == '_') 178170531Ssam