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