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