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