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