1 /* 2 * First, the stuff that ends up in the outside-world include file 3 = typedef off_t my_regoff_t; 4 = typedef struct { 5 = int re_magic; 6 = size_t re_nsub; // number of parenthesized subexpressions 7 = const char *re_endp; // end pointer for MY_REG_PEND 8 = struct re_guts *re_g; // none of your business :-) 9 = } regex_t; 10 = typedef struct { 11 = my_regoff_t rm_so; // start of match 12 = my_regoff_t rm_eo; // end of match 13 = } regmatch_t; 14 */ 15 /* 16 * internals of regex_t 17 */ 18 #ifndef __regex2_h__ 19 #define __regex2_h__ 20 21 #define MAGIC1 ((('r'^0200)<<8) | 'e') 22 23 /* 24 * The internal representation is a *strip*, a sequence of 25 * operators ending with an endmarker. (Some terminology etc. is a 26 * historical relic of earlier versions which used multiple strips.) 27 * Certain oddities in the representation are there to permit running 28 * the machinery backwards; in particular, any deviation from sequential 29 * flow must be marked at both its source and its destination. Some 30 * fine points: 31 * 32 * - OPLUS_ and O_PLUS are *inside* the loop they create. 33 * - OQUEST_ and O_QUEST are *outside* the bypass they create. 34 * - OCH_ and O_CH are *outside* the multi-way branch they create, while 35 * OOR1 and OOR2 are respectively the end and the beginning of one of 36 * the branches. Note that there is an implicit OOR2 following OCH_ 37 * and an implicit OOR1 preceding O_CH. 38 * 39 * In state representations, an operator's bit is on to signify a state 40 * immediately *preceding* "execution" of that operator. 41 */ 42 typedef unsigned long sop; /* strip operator */ 43 typedef long sopno; 44 #define OPRMASK 0xf8000000 45 #define OPDMASK 0x07ffffff 46 #define OPSHIFT ((unsigned long)27) 47 #define OP(n) ((unsigned long) ((n)&OPRMASK)) 48 #define OPND(n) ((unsigned long) ((n)&OPDMASK)) 49 #define SOP(op, opnd) (unsigned long) ((op)|(opnd)) 50 #define OSHIFT(A) ((unsigned long) (A) << OPSHIFT) 51 /* operators meaning operand */ 52 /* (back, fwd are offsets) */ 53 #define OEND OSHIFT(1) /* endmarker - */ 54 #define OCHAR OSHIFT(2) /* character unsigned char */ 55 #define OBOL OSHIFT(3) /* left anchor - */ 56 #define OEOL OSHIFT(4) /* right anchor - */ 57 #define OANY OSHIFT(5) /* . - */ 58 #define OANYOF OSHIFT(6) /* [...] set number */ 59 #define OBACK_ OSHIFT(7) /* begin \d paren number */ 60 #define O_BACK OSHIFT(8) /* end \d paren number */ 61 #define OPLUS_ OSHIFT(9) /* + prefix fwd to suffix */ 62 #define O_PLUS OSHIFT(10) /* + suffix back to prefix */ 63 #define OQUEST_ OSHIFT(11) /* ? prefix fwd to suffix */ 64 #define O_QUEST OSHIFT(12) /* ? suffix back to prefix */ 65 #define OLPAREN OSHIFT(13) /* ( fwd to ) */ 66 #define ORPAREN OSHIFT(14) /* ) back to ( */ 67 #define OCH_ OSHIFT(15) /* begin choice fwd to OOR2 */ 68 #define OOR1 OSHIFT(16) /* | pt. 1 back to OOR1 or OCH_ */ 69 #define OOR2 OSHIFT(17) /* | pt. 2 fwd to OOR2 or O_CH */ 70 #define O_CH OSHIFT(18) /* end choice back to OOR1 */ 71 #define OBOW OSHIFT(19) /* begin word - */ 72 #define OEOW OSHIFT(20) /* end word - */ 73 74 /* 75 * Structure for [] character-set representation. Character sets are 76 * done as bit vectors, grouped 8 to a byte vector for compactness. 77 * The individual set therefore has both a pointer to the byte vector 78 * and a mask to pick out the relevant bit of each byte. A hash code 79 * simplifies testing whether two sets could be identical. 80 * 81 * This will get trickier for multicharacter collating elements. As 82 * preliminary hooks for dealing with such things, we also carry along 83 * a string of multi-character elements, and decide the size of the 84 * vectors at run time. 85 */ 86 87 88 typedef struct { 89 uch *ptr; /* -> uch [csetsize] */ 90 uch mask; /* bit within array */ 91 uch hash; /* hash code */ 92 size_t smultis; 93 char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */ 94 } cset; 95 /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */ 96 #define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (uch) (c)) 97 #define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (uch)(c)) 98 #define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask) 99 #define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */ 100 #define MCsub(p, cs, cp) mcsub(p, cs, cp) 101 #define MCin(p, cs, cp) mcin(p, cs, cp) 102 103 /* stuff for character categories */ 104 typedef unsigned char cat_t; 105 106 /* 107 * main compiled-expression structure 108 */ 109 struct re_guts { 110 int magic; 111 # define MAGIC2 ((('R'^0200)<<8)|'E') 112 sop *strip; /* malloced area for strip */ 113 int csetsize; /* number of bits in a cset vector */ 114 int ncsets; /* number of csets in use */ 115 cset *sets; /* -> cset [ncsets] */ 116 uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */ 117 int cflags; /* copy of regcomp() cflags argument */ 118 sopno nstates; /* = number of sops */ 119 sopno firststate; /* the initial OEND (normally 0) */ 120 sopno laststate; /* the final OEND */ 121 int iflags; /* internal flags */ 122 # define USEBOL 01 /* used ^ */ 123 # define USEEOL 02 /* used $ */ 124 # define BAD 04 /* something wrong */ 125 int nbol; /* number of ^ used */ 126 int neol; /* number of $ used */ 127 int ncategories; /* how many character categories */ 128 cat_t *categories; /* ->catspace[-CHAR_MIN] */ 129 char *must; /* match must contain this string */ 130 int mlen; /* length of must */ 131 size_t nsub; /* copy of re_nsub */ 132 int backrefs; /* does it use back references? */ 133 sopno nplus; /* how deep does it nest +s? */ 134 /* catspace must be last */ 135 cat_t catspace[1]; /* actually [NC] */ 136 }; 137 138 /* misc utilities */ 139 #undef OUT /* May be defined in windows */ 140 #define OUT (CHAR_MAX+1) /* a non-character value */ 141 #define ISWORD(s,c) (my_isalnum(s,c) || (c) == '_') 142 143 #endif /* __regex2_h__ */ 144