1 /* $NetBSD: regex2.h,v 1.2 2013/11/22 15:52:06 christos Exp $ */ 2 /*- 3 * Copyright (c) 1992, 1993, 1994 Henry Spencer. 4 * Copyright (c) 1992, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Henry Spencer of the University of Toronto. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)regex2.h 8.3 (Berkeley) 3/16/94 39 */ 40 41 /* 42 * First, the stuff that ends up in the outside-world include file 43 = typedef off_t regoff_t; 44 = typedef struct { 45 = int re_magic; 46 = size_t re_nsub; // number of parenthesized subexpressions 47 = const char *re_endp; // end pointer for REG_PEND 48 = struct re_guts *re_g; // none of your business :-) 49 = } regex_t; 50 = typedef struct { 51 = regoff_t rm_so; // start of match 52 = regoff_t rm_eo; // end of match 53 = } regmatch_t; 54 */ 55 /* 56 * internals of regex_t 57 */ 58 #define MAGIC1 ((('r'^0200)<<8) | 'e') 59 60 /* 61 * The internal representation is a *strip*, a sequence of 62 * operators ending with an endmarker. (Some terminology etc. is a 63 * historical relic of earlier versions which used multiple strips.) 64 * Certain oddities in the representation are there to permit running 65 * the machinery backwards; in particular, any deviation from sequential 66 * flow must be marked at both its source and its destination. Some 67 * fine points: 68 * 69 * - OPLUS_ and O_PLUS are *inside* the loop they create. 70 * - OQUEST_ and O_QUEST are *outside* the bypass they create. 71 * - OCH_ and O_CH are *outside* the multi-way branch they create, while 72 * OOR1 and OOR2 are respectively the end and the beginning of one of 73 * the branches. Note that there is an implicit OOR2 following OCH_ 74 * and an implicit OOR1 preceding O_CH. 75 * 76 * In state representations, an operator's bit is on to signify a state 77 * immediately *preceding* "execution" of that operator. 78 */ 79 typedef char sop; /* strip operator */ 80 typedef size_t sopno; 81 /* operators meaning operand */ 82 /* (back, fwd are offsets) */ 83 #define OEND (1) /* endmarker - */ 84 #define OCHAR (2) /* character unsigned char */ 85 #define OBOL (3) /* left anchor - */ 86 #define OEOL (4) /* right anchor - */ 87 #define OANY (5) /* . - */ 88 #define OANYOF (6) /* [...] set number */ 89 #define OBACK_ (7) /* begin \d paren number */ 90 #define O_BACK (8) /* end \d paren number */ 91 #define OPLUS_ (9) /* + prefix fwd to suffix */ 92 #define O_PLUS (10) /* + suffix back to prefix */ 93 #define OQUEST_ (11) /* ? prefix fwd to suffix */ 94 #define O_QUEST (12) /* ? suffix back to prefix */ 95 #define OLPAREN (13) /* ( fwd to ) */ 96 #define ORPAREN (14) /* ) back to ( */ 97 #define OCH_ (15) /* begin choice fwd to OOR2 */ 98 #define OOR1 (16) /* | pt. 1 back to OOR1 or OCH_ */ 99 #define OOR2 (17) /* | pt. 2 fwd to OOR2 or O_CH */ 100 #define O_CH (18) /* end choice back to OOR1 */ 101 #define OBOW (19) /* begin word - */ 102 #define OEOW (20) /* end word - */ 103 104 /* 105 * Structure for [] character-set representation. Character sets are 106 * done as bit vectors, grouped 8 to a byte vector for compactness. 107 * The individual set therefore has both a pointer to the byte vector 108 * and a mask to pick out the relevant bit of each byte. A hash code 109 * simplifies testing whether two sets could be identical. 110 * 111 * This will get trickier for multicharacter collating elements. As 112 * preliminary hooks for dealing with such things, we also carry along 113 * a string of multi-character elements, and decide the size of the 114 * vectors at run time. 115 */ 116 typedef struct { 117 uch *ptr; /* -> uch [csetsize] */ 118 uch mask; /* bit within array */ 119 uch hash; /* hash code */ 120 size_t smultis; 121 char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */ 122 } cset; 123 /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */ 124 #define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c)) 125 #define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c)) 126 #define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask) 127 #define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */ 128 #define MCsub(p, cs, cp) mcsub(p, cs, cp) 129 #define MCin(p, cs, cp) mcin(p, cs, cp) 130 131 /* stuff for character categories */ 132 typedef RCHAR_T cat_t; 133 134 /* 135 * main compiled-expression structure 136 */ 137 struct re_guts { 138 int magic; 139 # define MAGIC2 ((('R'^0200)<<8)|'E') 140 sop *strip; /* malloced area for strip */ 141 RCHAR_T *stripdata; /* malloced area for stripdata */ 142 size_t csetsize; /* number of bits in a cset vector */ 143 size_t ncsets; /* number of csets in use */ 144 cset *sets; /* -> cset [ncsets] */ 145 uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */ 146 int cflags; /* copy of regcomp() cflags argument */ 147 sopno nstates; /* = number of sops */ 148 sopno firststate; /* the initial OEND (normally 0) */ 149 sopno laststate; /* the final OEND */ 150 int iflags; /* internal flags */ 151 # define USEBOL 01 /* used ^ */ 152 # define USEEOL 02 /* used $ */ 153 # define BAD 04 /* something wrong */ 154 size_t nbol; /* number of ^ used */ 155 size_t neol; /* number of $ used */ 156 #if 0 157 size_t ncategories; /* how many character categories */ 158 cat_t *categories; /* ->catspace[-CHAR_MIN] */ 159 #endif 160 RCHAR_T *must; /* match must contain this string */ 161 size_t mlen; /* length of must */ 162 size_t nsub; /* copy of re_nsub */ 163 int backrefs; /* does it use back references? */ 164 sopno nplus; /* how deep does it nest +s? */ 165 /* catspace must be last */ 166 #if 0 167 cat_t catspace[1]; /* actually [NC] */ 168 #endif 169 }; 170 171 /* misc utilities */ 172 #define OUT REOF /* a non-character value */ 173 #define ISWORD(c) ((c) == '_' || (ISGRAPH((UCHAR_T)c) && !ISPUNCT((UCHAR_T)c))) 174