Print this page
8993 sync regcomp(3C) with upstream
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libc/port/regex/regex2.h
+++ new/usr/src/lib/libc/port/regex/regex2.h
1 1 /*
2 2 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3 3 * Copyright (c) 1992, 1993, 1994
4 4 * The Regents of the University of California. All rights reserved.
5 5 *
6 6 * This code is derived from software contributed to Berkeley by
7 7 * Henry Spencer.
8 8 *
9 9 * Redistribution and use in source and binary forms, with or without
10 10 * modification, are permitted provided that the following conditions
11 11 * are met:
12 12 * 1. Redistributions of source code must retain the above copyright
13 13 * notice, this list of conditions and the following disclaimer.
14 14 * 2. Redistributions in binary form must reproduce the above copyright
15 15 * notice, this list of conditions and the following disclaimer in the
16 16 * documentation and/or other materials provided with the distribution.
17 17 * 3. Neither the name of the University nor the names of its contributors
18 18 * may be used to endorse or promote products derived from this software
19 19 * without specific prior written permission.
20 20 *
21 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 31 * SUCH DAMAGE.
32 32 */
33 33
34 34 /*
35 35 * First, the stuff that ends up in the outside-world include file
36 36 * typedef off_t regoff_t;
37 37 * typedef struct {
38 38 * int re_magic;
39 39 * size_t re_nsub; // number of parenthesized subexpressions
40 40 * const char *re_endp; // end pointer for REG_PEND
41 41 * struct re_guts *re_g; // none of your business :-)
42 42 * } regex_t;
43 43 * typedef struct {
44 44 * regoff_t rm_so; // start of match
45 45 * regoff_t rm_eo; // end of match
46 46 * } regmatch_t;
47 47 */
48 48 /*
49 49 * internals of regex_t
50 50 */
51 51 #define MAGIC1 ((('r'^0200)<<8) | 'e')
52 52
53 53 /*
54 54 * The internal representation is a *strip*, a sequence of
55 55 * operators ending with an endmarker. (Some terminology etc. is a
56 56 * historical relic of earlier versions which used multiple strips.)
57 57 * Certain oddities in the representation are there to permit running
58 58 * the machinery backwards; in particular, any deviation from sequential
59 59 * flow must be marked at both its source and its destination. Some
60 60 * fine points:
61 61 *
62 62 * - OPLUS_ and O_PLUS are *inside* the loop they create.
63 63 * - OQUEST_ and O_QUEST are *outside* the bypass they create.
64 64 * - OCH_ and O_CH are *outside* the multi-way branch they create, while
65 65 * OOR1 and OOR2 are respectively the end and the beginning of one of
66 66 * the branches. Note that there is an implicit OOR2 following OCH_
67 67 * and an implicit OOR1 preceding O_CH.
68 68 *
69 69 * In state representations, an operator's bit is on to signify a state
70 70 * immediately *preceding* "execution" of that operator.
71 71 */
72 72 typedef unsigned int sop; /* strip operator */
73 73 typedef unsigned int sopno;
74 74 #define OPRMASK 0xf8000000U
75 75 #define OPDMASK 0x07ffffffU
76 76 #define OPSHIFT ((unsigned)27)
77 77 #define OP(n) ((n)&OPRMASK)
78 78 #define OPND(n) ((n)&OPDMASK)
79 79 #define SOP(op, opnd) ((op)|(opnd))
80 80 /* operators meaning operand */
81 81 /* (back, fwd are offsets) */
82 82 #define OEND (1U<<OPSHIFT) /* endmarker - */
83 83 #define OCHAR (2U<<OPSHIFT) /* character wide character */
84 84 #define OBOL (3U<<OPSHIFT) /* left anchor - */
85 85 #define OEOL (4U<<OPSHIFT) /* right anchor - */
86 86 #define OANY (5U<<OPSHIFT) /* . - */
87 87 #define OANYOF (6U<<OPSHIFT) /* [...] set number */
88 88 #define OBACK_ (7U<<OPSHIFT) /* begin \d paren number */
89 89 #define O_BACK (8U<<OPSHIFT) /* end \d paren number */
90 90 #define OPLUS_ (9U<<OPSHIFT) /* + prefix fwd to suffix */
91 91 #define O_PLUS (10U<<OPSHIFT) /* + suffix back to prefix */
92 92 #define OQUEST_ (11U<<OPSHIFT) /* ? prefix fwd to suffix */
93 93 #define O_QUEST (12U<<OPSHIFT) /* ? suffix back to prefix */
94 94 #define OLPAREN (13U<<OPSHIFT) /* ( fwd to ) */
95 95 #define ORPAREN (14U<<OPSHIFT) /* ) back to ( */
96 96 #define OCH_ (15U<<OPSHIFT) /* begin choice fwd to OOR2 */
97 97 #define OOR1 (16U<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */
98 98 #define OOR2 (17U<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */
99 99 #define O_CH (18U<<OPSHIFT) /* end choice back to OOR1 */
100 100 #define OBOW (19U<<OPSHIFT) /* begin word - */
101 101 #define OEOW (20U<<OPSHIFT) /* end word - */
102 102
103 103 /*
104 104 * Structures for [] character-set representation.
105 105 */
106 106 typedef struct {
107 107 wint_t min;
108 108 wint_t max;
109 109 } crange;
110 110 typedef struct {
111 111 unsigned char bmp[NC / 8];
112 112 wctype_t *types;
113 113 unsigned int ntypes;
114 114 wint_t *wides;
115 115 unsigned int nwides;
116 116 crange *ranges;
117 117 unsigned int nranges;
118 118 int invert;
119 119 int icase;
120 120 } cset;
121 121
122 122 static int
123 123 CHIN1(cset *cs, wint_t ch)
124 124 {
125 125 unsigned int i;
126 126
127 127 assert(ch >= 0);
128 128 if (ch < NC)
129 129 return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
130 130 cs->invert);
131 131 for (i = 0; i < cs->nwides; i++)
132 132 if (ch == cs->wides[i])
133 133 return (!cs->invert);
134 134 for (i = 0; i < cs->nranges; i++)
135 135 if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
136 136 return (!cs->invert);
137 137 for (i = 0; i < cs->ntypes; i++)
138 138 if (iswctype(ch, cs->types[i]))
139 139 return (!cs->invert);
140 140 return (cs->invert);
141 141 }
142 142
143 143 static int
144 144 CHIN(cset *cs, wint_t ch)
145 145 {
146 146
147 147 assert(ch >= 0);
148 148 if (ch < NC)
149 149 return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
150 150 cs->invert);
151 151 else if (cs->icase)
152 152 return (CHIN1(cs, ch) || CHIN1(cs, towlower(ch)) ||
153 153 CHIN1(cs, towupper(ch)));
154 154 else
155 155 return (CHIN1(cs, ch));
156 156 }
157 157
158 158 /*
159 159 * main compiled-expression structure
160 160 */
161 161 struct re_guts {
162 162 int magic;
163 163 #define MAGIC2 ((('R'^0200)<<8)|'E')
164 164 sop *strip; /* malloced area for strip */
165 165 unsigned int ncsets; /* number of csets in use */
166 166 cset *sets; /* -> cset [ncsets] */
167 167 int cflags; /* copy of regcomp() cflags argument */
168 168 sopno nstates; /* = number of sops */
169 169 sopno firststate; /* the initial OEND (normally 0) */
170 170 sopno laststate; /* the final OEND */
171 171 int iflags; /* internal flags */
172 172 #define USEBOL 01 /* used ^ */
173 173 #define USEEOL 02 /* used $ */
174 174 #define BAD 04 /* something wrong */
175 175 int nbol; /* number of ^ used */
176 176 int neol; /* number of $ used */
177 177 char *must; /* match must contain this string */
178 178 int moffset; /* latest point at which must may be located */
↓ open down ↓ |
178 lines elided |
↑ open up ↑ |
179 179 int *charjump; /* Boyer-Moore char jump table */
180 180 int *matchjump; /* Boyer-Moore match jump table */
181 181 int mlen; /* length of must */
182 182 size_t nsub; /* copy of re_nsub */
183 183 int backrefs; /* does it use back references? */
184 184 sopno nplus; /* how deep does it nest +s? */
185 185 };
186 186
187 187 /* misc utilities */
188 188 #define OUT (CHAR_MIN - 1) /* a non-character value */
189 +#define IGN (CHAR_MIN - 2)
189 190 #define ISWORD(c) (iswalnum((uch)(c)) || (c) == '_')
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX