regguts.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Internal interface definitions, etc., for the reg package
  3. *
  4. * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
  5. *
  6. * Development of this software was funded, in part, by Cray Research Inc.,
  7. * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
  8. * Corporation, none of whom are responsible for the results. The author
  9. * thanks all of them.
  10. *
  11. * Redistribution and use in source and binary forms -- with or without
  12. * modification -- are permitted for any purpose, provided that
  13. * redistributions in source form retain this entire copyright notice and
  14. * indicate the origin and nature of any modifications.
  15. *
  16. * I'd appreciate being given credit for this package in the documentation of
  17. * software which uses it, but that is not a requirement.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  21. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  22. * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * Environmental customization. It should not (I hope) be necessary to alter
  32. * the file you are now reading -- regcustom.h should handle it all, given
  33. * care here and elsewhere.
  34. */
  35. #include "regcustom.h"
  36. /*
  37. * Things that regcustom.h might override.
  38. */
  39. /* assertions */
  40. #ifndef assert
  41. #ifndef REG_DEBUG
  42. #ifndef NDEBUG
  43. #define NDEBUG /* no assertions */
  44. #endif
  45. #endif /* !REG_DEBUG */
  46. #include <assert.h>
  47. #endif
  48. /* voids */
  49. #ifndef VOID
  50. #define VOID void /* for function return values */
  51. #endif
  52. #ifndef DISCARD
  53. #define DISCARD void /* for throwing values away */
  54. #endif
  55. #ifndef PVOID
  56. #define PVOID void * /* generic pointer */
  57. #endif
  58. #ifndef VS
  59. #define VS(x) ((void*)(x)) /* cast something to generic ptr */
  60. #endif
  61. #ifndef NOPARMS
  62. #define NOPARMS void /* for empty parm lists */
  63. #endif
  64. /* function-pointer declarator */
  65. #ifndef FUNCPTR
  66. #if __STDC__ >= 1
  67. #define FUNCPTR(name, args) (*name)args
  68. #else
  69. #define FUNCPTR(name, args) (*name)()
  70. #endif
  71. #endif
  72. /* memory allocation */
  73. #ifndef MALLOC
  74. #define MALLOC(n) malloc(n)
  75. #endif
  76. #ifndef REALLOC
  77. #define REALLOC(p, n) realloc(VS(p), n)
  78. #endif
  79. #ifndef FREE
  80. #define FREE(p) free(VS(p))
  81. #endif
  82. /* want size of a char in bits, and max value in bounded quantifiers */
  83. #ifndef _POSIX2_RE_DUP_MAX
  84. #define _POSIX2_RE_DUP_MAX 255 /* normally from <limits.h> */
  85. #endif
  86. /*
  87. * misc
  88. */
  89. #define NOTREACHED 0
  90. #define xxx 1
  91. #define DUPMAX _POSIX2_RE_DUP_MAX
  92. #define DUPINF (DUPMAX+1)
  93. #define REMAGIC 0xFED7 /* magic number for main struct */
  94. /*
  95. * debugging facilities
  96. */
  97. #ifdef REG_DEBUG
  98. /* FDEBUG does finite-state tracing */
  99. #define FDEBUG(arglist) { if (v->eflags&REG_FTRACE) printf arglist; }
  100. /* MDEBUG does higher-level tracing */
  101. #define MDEBUG(arglist) { if (v->eflags&REG_MTRACE) printf arglist; }
  102. #else
  103. #define FDEBUG(arglist) {}
  104. #define MDEBUG(arglist) {}
  105. #endif
  106. /*
  107. * bitmap manipulation
  108. */
  109. #define UBITS (CHAR_BIT * sizeof(unsigned))
  110. #define BSET(uv, sn) ((uv)[(sn)/UBITS] |= (unsigned)1 << ((sn)%UBITS))
  111. #define ISBSET(uv, sn) ((uv)[(sn)/UBITS] & ((unsigned)1 << ((sn)%UBITS)))
  112. /*
  113. * We dissect a chr into byts for colormap table indexing. Here we define a
  114. * byt, which will be the same as a byte on most machines... The exact size of
  115. * a byt is not critical, but about 8 bits is good, and extraction of 8-bit
  116. * chunks is sometimes especially fast.
  117. */
  118. #ifndef BYTBITS
  119. #define BYTBITS 8 /* bits in a byt */
  120. #endif
  121. #define BYTTAB (1<<BYTBITS) /* size of table with one entry per byt value */
  122. #define BYTMASK (BYTTAB-1) /* bit mask for byt */
  123. #define NBYTS ((CHRBITS+BYTBITS-1)/BYTBITS)
  124. /* the definition of GETCOLOR(), below, assumes NBYTS <= 4 */
  125. /*
  126. * As soon as possible, we map chrs into equivalence classes -- "colors" --
  127. * which are of much more manageable number.
  128. */
  129. typedef short color; /* colors of characters */
  130. typedef int pcolor; /* what color promotes to */
  131. #define MAX_COLOR SHRT_MAX /* max color value */
  132. #define COLORLESS (-1) /* impossible color */
  133. #define WHITE 0 /* default color, parent of all others */
  134. /*
  135. * A colormap is a tree -- more precisely, a DAG -- indexed at each level by a
  136. * byt of the chr, to map the chr to a color efficiently. Because lower
  137. * sections of the tree can be shared, it can exploit the usual sparseness of
  138. * such a mapping table. The tree is always NBYTS levels deep (in the past it
  139. * was shallower during construction but was "filled" to full depth at the end
  140. * of that); areas that are unaltered as yet point to "fill blocks" which are
  141. * entirely WHITE in color.
  142. */
  143. /* the tree itself */
  144. struct colors {
  145. color ccolor[BYTTAB];
  146. };
  147. struct ptrs {
  148. union tree *pptr[BYTTAB];
  149. };
  150. union tree {
  151. struct colors colors;
  152. struct ptrs ptrs;
  153. };
  154. #define tcolor colors.ccolor
  155. #define tptr ptrs.pptr
  156. /* Internal per-color descriptor structure for the color machinery */
  157. struct colordesc {
  158. uchr nchrs; /* number of chars of this color */
  159. color sub; /* open subcolor (if any); free chain ptr */
  160. #define NOSUB COLORLESS
  161. struct arc *arcs; /* color chain */
  162. int flags;
  163. #define FREECOL 01 /* currently free */
  164. #define PSEUDO 02 /* pseudocolor, no real chars */
  165. #define UNUSEDCOLOR(cd) ((cd)->flags&FREECOL)
  166. union tree *block; /* block of solid color, if any */
  167. };
  168. /*
  169. * The color map itself
  170. *
  171. * Much of the data in the colormap struct is only used at compile time.
  172. * However, the bulk of the space usage is in the "tree" structure, so it's
  173. * not clear that there's much point in converting the rest to a more compact
  174. * form when compilation is finished.
  175. */
  176. struct colormap {
  177. int magic;
  178. #define CMMAGIC 0x876
  179. struct vars *v; /* for compile error reporting */
  180. size_t ncds; /* number of colordescs */
  181. size_t max; /* highest in use */
  182. color free; /* beginning of free chain (if non-0) */
  183. struct colordesc *cd;
  184. #define CDEND(cm) (&(cm)->cd[(cm)->max + 1])
  185. #define NINLINECDS ((size_t)10)
  186. struct colordesc cdspace[NINLINECDS];
  187. union tree tree[NBYTS]; /* tree top, plus fill blocks */
  188. };
  189. /* optimization magic to do fast chr->color mapping */
  190. #define B0(c) ((c) & BYTMASK)
  191. #define B1(c) (((c)>>BYTBITS) & BYTMASK)
  192. #define B2(c) (((c)>>(2*BYTBITS)) & BYTMASK)
  193. #define B3(c) (((c)>>(3*BYTBITS)) & BYTMASK)
  194. #if NBYTS == 1
  195. #define GETCOLOR(cm, c) ((cm)->tree->tcolor[B0(c)])
  196. #endif
  197. /* beware, for NBYTS>1, GETCOLOR() is unsafe -- 2nd arg used repeatedly */
  198. #if NBYTS == 2
  199. #define GETCOLOR(cm, c) ((cm)->tree->tptr[B1(c)]->tcolor[B0(c)])
  200. #endif
  201. #if NBYTS == 4
  202. #define GETCOLOR(cm, c) ((cm)->tree->tptr[B3(c)]->tptr[B2(c)]->tptr[B1(c)]->tcolor[B0(c)])
  203. #endif
  204. /*
  205. * Interface definitions for locale-interface functions in locale.c.
  206. */
  207. /* Representation of a set of characters. */
  208. struct cvec {
  209. int nchrs; /* number of chrs */
  210. int chrspace; /* number of chrs possible */
  211. chr *chrs; /* pointer to vector of chrs */
  212. int nranges; /* number of ranges (chr pairs) */
  213. int rangespace; /* number of chrs possible */
  214. chr *ranges; /* pointer to vector of chr pairs */
  215. };
  216. /*
  217. * definitions for non-deterministic finite autmaton (NFA) internal
  218. * representation
  219. *
  220. * Having a "from" pointer within each arc may seem redundant, but it saves a
  221. * lot of hassle.
  222. */
  223. struct state;
  224. struct arc {
  225. int type; /* 0 if free, else an NFA arc type code */
  226. color co;
  227. struct state *from; /* where it's from (and contained within) */
  228. struct state *to; /* where it's to */
  229. struct arc *outchain; /* link in *from's outs chain or free chain */
  230. struct arc *outchainRev; /* back-link in *from's outs chain */
  231. #define freechain outchain /* we do not maintain "freechainRev" */
  232. struct arc *inchain; /* *to's ins chain */
  233. struct arc *inchainRev; /* back-link in *to's ins chain */
  234. struct arc *colorchain; /* color's arc chain */
  235. struct arc *colorchainRev; /* back-link in color's arc chain */
  236. };
  237. struct arcbatch { /* for bulk allocation of arcs */
  238. struct arcbatch *next;
  239. #define ABSIZE 10
  240. struct arc a[ABSIZE];
  241. };
  242. struct state {
  243. int no;
  244. #define FREESTATE (-1)
  245. char flag; /* marks special states */
  246. int nins; /* number of inarcs */
  247. struct arc *ins; /* chain of inarcs */
  248. int nouts; /* number of outarcs */
  249. struct arc *outs; /* chain of outarcs */
  250. struct arc *free; /* chain of free arcs */
  251. struct state *tmp; /* temporary for traversal algorithms */
  252. struct state *next; /* chain for traversing all */
  253. struct state *prev; /* back chain */
  254. struct arcbatch oas; /* first arcbatch, avoid malloc in easy case */
  255. int noas; /* number of arcs used in first arcbatch */
  256. };
  257. struct nfa {
  258. struct state *pre; /* pre-initial state */
  259. struct state *init; /* initial state */
  260. struct state *final; /* final state */
  261. struct state *post; /* post-final state */
  262. int nstates; /* for numbering states */
  263. struct state *states; /* state-chain header */
  264. struct state *slast; /* tail of the chain */
  265. struct state *free; /* free list */
  266. struct colormap *cm; /* the color map */
  267. color bos[2]; /* colors, if any, assigned to BOS and BOL */
  268. color eos[2]; /* colors, if any, assigned to EOS and EOL */
  269. struct vars *v; /* simplifies compile error reporting */
  270. struct nfa *parent; /* parent NFA, if any */
  271. };
  272. /*
  273. * definitions for compacted NFA
  274. *
  275. * The main space savings in a compacted NFA is from making the arcs as small
  276. * as possible. We store only the transition color and next-state number for
  277. * each arc. The list of out arcs for each state is an array beginning at
  278. * cnfa.states[statenumber], and terminated by a dummy carc struct with
  279. * co == COLORLESS.
  280. *
  281. * The non-dummy carc structs are of two types: plain arcs and LACON arcs.
  282. * Plain arcs just store the transition color number as "co". LACON arcs
  283. * store the lookahead constraint number plus cnfa.ncolors as "co". LACON
  284. * arcs can be distinguished from plain by testing for co >= cnfa.ncolors.
  285. */
  286. struct carc {
  287. color co; /* COLORLESS is list terminator */
  288. int to; /* next-state number */
  289. };
  290. struct cnfa {
  291. int nstates; /* number of states */
  292. int ncolors; /* number of colors */
  293. int flags;
  294. #define HASLACONS 01 /* uses lookahead constraints */
  295. int pre; /* setup state number */
  296. int post; /* teardown state number */
  297. color bos[2]; /* colors, if any, assigned to BOS and BOL */
  298. color eos[2]; /* colors, if any, assigned to EOS and EOL */
  299. char *stflags; /* vector of per-state flags bytes */
  300. #define CNFA_NOPROGRESS 01 /* flag bit for a no-progress state */
  301. struct carc **states; /* vector of pointers to outarc lists */
  302. /* states[n] are pointers into a single malloc'd array of arcs */
  303. struct carc *arcs; /* the area for the lists */
  304. };
  305. #define ZAPCNFA(cnfa) ((cnfa).nstates = 0)
  306. #define NULLCNFA(cnfa) ((cnfa).nstates == 0)
  307. /*
  308. * This symbol limits the transient heap space used by the regex compiler,
  309. * and thereby also the maximum complexity of NFAs that we'll deal with.
  310. * Currently we only count NFA states and arcs against this; the other
  311. * transient data is generally not large enough to notice compared to those.
  312. * Note that we do not charge anything for the final output data structures
  313. * (the compacted NFA and the colormap).
  314. */
  315. #ifndef REG_MAX_COMPILE_SPACE
  316. #define REG_MAX_COMPILE_SPACE \
  317. (100000 * sizeof(struct state) + 100000 * sizeof(struct arcbatch))
  318. #endif
  319. /*
  320. * subexpression tree
  321. *
  322. * "op" is one of:
  323. * '=' plain regex without interesting substructure (implemented as DFA)
  324. * 'b' back-reference (has no substructure either)
  325. * '(' capture node: captures the match of its single child
  326. * '.' concatenation: matches a match for left, then a match for right
  327. * '|' alternation: matches a match for left or a match for right
  328. * '*' iteration: matches some number of matches of its single child
  329. *
  330. * Note: the right child of an alternation must be another alternation or
  331. * NULL; hence, an N-way branch requires N alternation nodes, not N-1 as you
  332. * might expect. This could stand to be changed. Actually I'd rather see
  333. * a single alternation node with N children, but that will take revising
  334. * the representation of struct subre.
  335. *
  336. * Note: when a backref is directly quantified, we stick the min/max counts
  337. * into the backref rather than plastering an iteration node on top. This is
  338. * for efficiency: there is no need to search for possible division points.
  339. */
  340. struct subre {
  341. char op; /* see type codes above */
  342. char flags;
  343. #define LONGER 01 /* prefers longer match */
  344. #define SHORTER 02 /* prefers shorter match */
  345. #define MIXED 04 /* mixed preference below */
  346. #define CAP 010 /* capturing parens below */
  347. #define BACKR 020 /* back reference below */
  348. #define INUSE 0100 /* in use in final tree */
  349. #define NOPROP 03 /* bits which may not propagate up */
  350. #define LMIX(f) ((f)<<2) /* LONGER -> MIXED */
  351. #define SMIX(f) ((f)<<1) /* SHORTER -> MIXED */
  352. #define UP(f) (((f)&~NOPROP) | (LMIX(f) & SMIX(f) & MIXED))
  353. #define MESSY(f) ((f)&(MIXED|CAP|BACKR))
  354. #define PREF(f) ((f)&NOPROP)
  355. #define PREF2(f1, f2) ((PREF(f1) != 0) ? PREF(f1) : PREF(f2))
  356. #define COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2))
  357. short id; /* ID of subre (1..ntree-1) */
  358. int subno; /* subexpression number (for 'b' and '(') */
  359. short min; /* min repetitions for iteration or backref */
  360. short max; /* max repetitions for iteration or backref */
  361. struct subre *left; /* left child, if any (also freelist chain) */
  362. struct subre *right; /* right child, if any */
  363. struct state *begin; /* outarcs from here... */
  364. struct state *end; /* ...ending in inarcs here */
  365. struct cnfa cnfa; /* compacted NFA, if any */
  366. struct subre *chain; /* for bookkeeping and error cleanup */
  367. };
  368. /*
  369. * table of function pointers for generic manipulation functions. A regex_t's
  370. * re_fns points to one of these.
  371. */
  372. struct fns {
  373. void FUNCPTR(free, (regex_t *));
  374. };
  375. /*
  376. * the insides of a regex_t, hidden behind a void *
  377. */
  378. struct guts {
  379. int magic;
  380. #define GUTSMAGIC 0xFED9
  381. int cflags; /* copy of compile flags */
  382. long info; /* copy of re_info */
  383. size_t nsub; /* copy of re_nsub */
  384. struct subre *tree;
  385. struct cnfa search; /* for fast preliminary search */
  386. int ntree; /* number of subre's, plus one */
  387. struct colormap cmap;
  388. int FUNCPTR(compare, (const chr *, const chr *, size_t));
  389. struct subre *lacons; /* lookahead-constraint vector */
  390. int nlacons; /* size of lacons */
  391. };
  392. /*
  393. * Magic for allocating a variable workspace. This default version is
  394. * stack-hungry.
  395. */
  396. #ifndef AllocVars
  397. #define AllocVars(vPtr) \
  398. struct vars var; \
  399. struct vars *vPtr = &var
  400. #endif
  401. #ifndef FreeVars
  402. #define FreeVars(vPtr) ((void) 0)
  403. #endif
  404. /*
  405. * Local Variables:
  406. * mode: c
  407. * c-basic-offset: 4
  408. * fill-column: 78
  409. * End:
  410. */