155850Sbostic /*- 266362Sbostic * Copyright (c) 1992, 1993, 1994 Henry Spencer. 366362Sbostic * Copyright (c) 1992, 1993, 1994 461162Sbostic * The Regents of the University of California. All rights reserved. 555850Sbostic * 655850Sbostic * This code is derived from software contributed to Berkeley by 7*66406Sbostic * Henry Spencer. 855850Sbostic * 955850Sbostic * %sccs.include.redist.c% 1055850Sbostic * 11*66406Sbostic * @(#)regex2.h 8.4 (Berkeley) 03/20/94 1255850Sbostic */ 1355850Sbostic 1455850Sbostic /* 1560201Sbostic * First, the stuff that ends up in the outside-world include file 1660201Sbostic = typedef off_t regoff_t; 1760201Sbostic = typedef struct { 1860201Sbostic = int re_magic; 1960201Sbostic = size_t re_nsub; // number of parenthesized subexpressions 2066362Sbostic = const char *re_endp; // end pointer for REG_PEND 2160201Sbostic = struct re_guts *re_g; // none of your business :-) 2260201Sbostic = } regex_t; 2360201Sbostic = typedef struct { 2460201Sbostic = regoff_t rm_so; // start of match 2560201Sbostic = regoff_t rm_eo; // end of match 2660201Sbostic = } regmatch_t; 2760201Sbostic */ 2860201Sbostic /* 2955850Sbostic * internals of regex_t 3055850Sbostic */ 3155850Sbostic #define MAGIC1 ((('r'^0200)<<8) | 'e') 3255850Sbostic 3355850Sbostic /* 3455850Sbostic * The internal representation is a *strip*, a sequence of 3555850Sbostic * operators ending with an endmarker. (Some terminology etc. is a 3655850Sbostic * historical relic of earlier versions which used multiple strips.) 3755850Sbostic * Certain oddities in the representation are there to permit running 3855850Sbostic * the machinery backwards; in particular, any deviation from sequential 3955850Sbostic * flow must be marked at both its source and its destination. Some 4055850Sbostic * fine points: 4155850Sbostic * 4255850Sbostic * - OPLUS_ and O_PLUS are *inside* the loop they create. 4355850Sbostic * - OQUEST_ and O_QUEST are *outside* the bypass they create. 4455850Sbostic * - OCH_ and O_CH are *outside* the multi-way branch they create, while 4555850Sbostic * OOR1 and OOR2 are respectively the end and the beginning of one of 4655850Sbostic * the branches. Note that there is an implicit OOR2 following OCH_ 4755850Sbostic * and an implicit OOR1 preceding O_CH. 4855850Sbostic * 4955850Sbostic * In state representations, an operator's bit is on to signify a state 5055850Sbostic * immediately *preceding* "execution" of that operator. 5155850Sbostic */ 5255850Sbostic typedef unsigned long sop; /* strip operator */ 5355850Sbostic typedef long sopno; 5455850Sbostic #define OPRMASK 0xf8000000 5555850Sbostic #define OPDMASK 0x07ffffff 5660201Sbostic #define OPSHIFT ((unsigned)27) 5755850Sbostic #define OP(n) ((n)&OPRMASK) 5855850Sbostic #define OPND(n) ((n)&OPDMASK) 5955850Sbostic #define SOP(op, opnd) ((op)|(opnd)) 6055850Sbostic /* operators meaning operand */ 6155850Sbostic /* (back, fwd are offsets) */ 6255850Sbostic #define OEND (1<<OPSHIFT) /* endmarker - */ 6355850Sbostic #define OCHAR (2<<OPSHIFT) /* character unsigned char */ 6455850Sbostic #define OBOL (3<<OPSHIFT) /* left anchor - */ 6555850Sbostic #define OEOL (4<<OPSHIFT) /* right anchor - */ 6655850Sbostic #define OANY (5<<OPSHIFT) /* . - */ 6755850Sbostic #define OANYOF (6<<OPSHIFT) /* [...] set number */ 6855850Sbostic #define OBACK_ (7<<OPSHIFT) /* begin \d paren number */ 6955850Sbostic #define O_BACK (8<<OPSHIFT) /* end \d paren number */ 7055850Sbostic #define OPLUS_ (9<<OPSHIFT) /* + prefix fwd to suffix */ 7155850Sbostic #define O_PLUS (10<<OPSHIFT) /* + suffix back to prefix */ 7255850Sbostic #define OQUEST_ (11<<OPSHIFT) /* ? prefix fwd to suffix */ 7355850Sbostic #define O_QUEST (12<<OPSHIFT) /* ? suffix back to prefix */ 7455850Sbostic #define OLPAREN (13<<OPSHIFT) /* ( fwd to ) */ 7555850Sbostic #define ORPAREN (14<<OPSHIFT) /* ) back to ( */ 7655850Sbostic #define OCH_ (15<<OPSHIFT) /* begin choice fwd to OOR2 */ 7755850Sbostic #define OOR1 (16<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */ 7855850Sbostic #define OOR2 (17<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */ 7955850Sbostic #define O_CH (18<<OPSHIFT) /* end choice back to OOR1 */ 8060201Sbostic #define OBOW (19<<OPSHIFT) /* begin word - */ 8160201Sbostic #define OEOW (20<<OPSHIFT) /* end word - */ 8255850Sbostic 8355850Sbostic /* 8455850Sbostic * Structure for [] character-set representation. Character sets are 8555850Sbostic * done as bit vectors, grouped 8 to a byte vector for compactness. 8655850Sbostic * The individual set therefore has both a pointer to the byte vector 8755850Sbostic * and a mask to pick out the relevant bit of each byte. A hash code 8855850Sbostic * simplifies testing whether two sets could be identical. 8955850Sbostic * 9055850Sbostic * This will get trickier for multicharacter collating elements. As 9155850Sbostic * preliminary hooks for dealing with such things, we also carry along 9255850Sbostic * a string of multi-character elements, and decide the size of the 9355850Sbostic * vectors at run time. 9455850Sbostic */ 9555850Sbostic typedef struct { 9666362Sbostic uch *ptr; /* -> uch [csetsize] */ 9766362Sbostic uch mask; /* bit within array */ 9866362Sbostic uch hash; /* hash code */ 9955850Sbostic size_t smultis; 10060201Sbostic char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */ 10155850Sbostic } cset; 10255850Sbostic /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */ 10366362Sbostic #define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c)) 10466362Sbostic #define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c)) 10566362Sbostic #define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask) 10666362Sbostic #define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */ 10766362Sbostic #define MCsub(p, cs, cp) mcsub(p, cs, cp) 10866362Sbostic #define MCin(p, cs, cp) mcin(p, cs, cp) 10955850Sbostic 11060201Sbostic /* stuff for character categories */ 11160201Sbostic typedef unsigned char cat_t; 11260201Sbostic 11355850Sbostic /* 11455850Sbostic * main compiled-expression structure 11555850Sbostic */ 11655850Sbostic struct re_guts { 11755850Sbostic int magic; 11855850Sbostic # define MAGIC2 ((('R'^0200)<<8)|'E') 11955850Sbostic sop *strip; /* malloced area for strip */ 12055850Sbostic int csetsize; /* number of bits in a cset vector */ 12155850Sbostic int ncsets; /* number of csets in use */ 12255850Sbostic cset *sets; /* -> cset [ncsets] */ 12366362Sbostic uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */ 12455850Sbostic int cflags; /* copy of regcomp() cflags argument */ 12555850Sbostic sopno nstates; /* = number of sops */ 12655850Sbostic sopno firststate; /* the initial OEND (normally 0) */ 12755850Sbostic sopno laststate; /* the final OEND */ 12855850Sbostic int iflags; /* internal flags */ 12955850Sbostic # define USEBOL 01 /* used ^ */ 13055850Sbostic # define USEEOL 02 /* used $ */ 13155850Sbostic # define BAD 04 /* something wrong */ 13260201Sbostic int nbol; /* number of ^ used */ 13360201Sbostic int neol; /* number of $ used */ 13455850Sbostic int ncategories; /* how many character categories */ 13560201Sbostic cat_t *categories; /* ->catspace[-CHAR_MIN] */ 13655850Sbostic char *must; /* match must contain this string */ 13755850Sbostic int mlen; /* length of must */ 13855850Sbostic size_t nsub; /* copy of re_nsub */ 13955850Sbostic int backrefs; /* does it use back references? */ 14055850Sbostic sopno nplus; /* how deep does it nest +s? */ 14160201Sbostic /* catspace must be last */ 14260201Sbostic cat_t catspace[1]; /* actually [NC] */ 14355850Sbostic }; 14460201Sbostic 14560201Sbostic /* misc utilities */ 14660201Sbostic #define OUT (CHAR_MAX+1) /* a non-character value */ 14765332Sbostic #define ISWORD(c) (isalnum(c) || (c) == '_') 148