regcomp.c revision 1.22 1 1.22 christos /* $NetBSD: regcomp.c,v 1.22 2006/03/19 02:48:30 christos Exp $ */
2 1.6 cgd
3 1.5 cgd /*-
4 1.5 cgd * Copyright (c) 1992, 1993, 1994
5 1.5 cgd * The Regents of the University of California. All rights reserved.
6 1.5 cgd *
7 1.5 cgd * This code is derived from software contributed to Berkeley by
8 1.5 cgd * Henry Spencer.
9 1.5 cgd *
10 1.5 cgd * Redistribution and use in source and binary forms, with or without
11 1.5 cgd * modification, are permitted provided that the following conditions
12 1.5 cgd * are met:
13 1.5 cgd * 1. Redistributions of source code must retain the above copyright
14 1.5 cgd * notice, this list of conditions and the following disclaimer.
15 1.5 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.5 cgd * notice, this list of conditions and the following disclaimer in the
17 1.5 cgd * documentation and/or other materials provided with the distribution.
18 1.18 agc * 3. Neither the name of the University nor the names of its contributors
19 1.18 agc * may be used to endorse or promote products derived from this software
20 1.18 agc * without specific prior written permission.
21 1.18 agc *
22 1.18 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.18 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.18 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.18 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.18 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.18 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.18 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.18 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.18 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.18 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.18 agc * SUCH DAMAGE.
33 1.18 agc *
34 1.18 agc * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
35 1.18 agc */
36 1.18 agc
37 1.18 agc /*-
38 1.18 agc * Copyright (c) 1992, 1993, 1994 Henry Spencer.
39 1.18 agc *
40 1.18 agc * This code is derived from software contributed to Berkeley by
41 1.18 agc * Henry Spencer.
42 1.18 agc *
43 1.18 agc * Redistribution and use in source and binary forms, with or without
44 1.18 agc * modification, are permitted provided that the following conditions
45 1.18 agc * are met:
46 1.18 agc * 1. Redistributions of source code must retain the above copyright
47 1.18 agc * notice, this list of conditions and the following disclaimer.
48 1.18 agc * 2. Redistributions in binary form must reproduce the above copyright
49 1.18 agc * notice, this list of conditions and the following disclaimer in the
50 1.18 agc * documentation and/or other materials provided with the distribution.
51 1.5 cgd * 3. All advertising materials mentioning features or use of this software
52 1.5 cgd * must display the following acknowledgement:
53 1.5 cgd * This product includes software developed by the University of
54 1.5 cgd * California, Berkeley and its contributors.
55 1.5 cgd * 4. Neither the name of the University nor the names of its contributors
56 1.5 cgd * may be used to endorse or promote products derived from this software
57 1.5 cgd * without specific prior written permission.
58 1.5 cgd *
59 1.5 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.5 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.5 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.5 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.5 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.5 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.5 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.5 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.5 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.5 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.5 cgd * SUCH DAMAGE.
70 1.5 cgd *
71 1.5 cgd * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
72 1.5 cgd */
73 1.5 cgd
74 1.7 christos #include <sys/cdefs.h>
75 1.5 cgd #if defined(LIBC_SCCS) && !defined(lint)
76 1.6 cgd #if 0
77 1.5 cgd static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
78 1.6 cgd #else
79 1.22 christos __RCSID("$NetBSD: regcomp.c,v 1.22 2006/03/19 02:48:30 christos Exp $");
80 1.6 cgd #endif
81 1.5 cgd #endif /* LIBC_SCCS and not lint */
82 1.5 cgd
83 1.8 jtc #include "namespace.h"
84 1.1 jtc #include <sys/types.h>
85 1.14 lukem
86 1.14 lukem #include <assert.h>
87 1.1 jtc #include <ctype.h>
88 1.1 jtc #include <limits.h>
89 1.14 lukem #include <regex.h>
90 1.14 lukem #include <stdio.h>
91 1.1 jtc #include <stdlib.h>
92 1.14 lukem #include <string.h>
93 1.8 jtc
94 1.8 jtc #ifdef __weak_alias
95 1.16 mycroft __weak_alias(regcomp,_regcomp)
96 1.8 jtc #endif
97 1.1 jtc
98 1.1 jtc #include "utils.h"
99 1.1 jtc #include "regex2.h"
100 1.1 jtc
101 1.1 jtc #include "cclass.h"
102 1.1 jtc #include "cname.h"
103 1.1 jtc
104 1.1 jtc /*
105 1.1 jtc * parse structure, passed up and down to avoid global variables and
106 1.1 jtc * other clumsinesses
107 1.1 jtc */
108 1.1 jtc struct parse {
109 1.21 yamt const char *next; /* next character in RE */
110 1.21 yamt const char *end; /* end of string (-> NUL normally) */
111 1.1 jtc int error; /* has an error been seen? */
112 1.1 jtc sop *strip; /* malloced strip */
113 1.1 jtc sopno ssize; /* malloced strip size (allocated) */
114 1.1 jtc sopno slen; /* malloced strip length (used) */
115 1.1 jtc int ncsalloc; /* number of csets allocated */
116 1.1 jtc struct re_guts *g;
117 1.1 jtc # define NPAREN 10 /* we need to remember () 1-9 for back refs */
118 1.1 jtc sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
119 1.1 jtc sopno pend[NPAREN]; /* -> ) ([0] unused) */
120 1.1 jtc };
121 1.1 jtc
122 1.5 cgd /* ========= begin header generated by ./mkh ========= */
123 1.5 cgd #ifdef __cplusplus
124 1.5 cgd extern "C" {
125 1.5 cgd #endif
126 1.5 cgd
127 1.5 cgd /* === regcomp.c === */
128 1.5 cgd static void p_ere __P((struct parse *p, int stop));
129 1.5 cgd static void p_ere_exp __P((struct parse *p));
130 1.5 cgd static void p_str __P((struct parse *p));
131 1.5 cgd static void p_bre __P((struct parse *p, int end1, int end2));
132 1.5 cgd static int p_simp_re __P((struct parse *p, int starordinary));
133 1.5 cgd static int p_count __P((struct parse *p));
134 1.5 cgd static void p_bracket __P((struct parse *p));
135 1.5 cgd static void p_b_term __P((struct parse *p, cset *cs));
136 1.5 cgd static void p_b_cclass __P((struct parse *p, cset *cs));
137 1.5 cgd static void p_b_eclass __P((struct parse *p, cset *cs));
138 1.5 cgd static char p_b_symbol __P((struct parse *p));
139 1.5 cgd static char p_b_coll_elem __P((struct parse *p, int endc));
140 1.19 jdolecek static int othercase __P((int ch));
141 1.5 cgd static void bothcases __P((struct parse *p, int ch));
142 1.5 cgd static void ordinary __P((struct parse *p, int ch));
143 1.5 cgd static void nonnewline __P((struct parse *p));
144 1.5 cgd static void repeat __P((struct parse *p, sopno start, int from, int to));
145 1.5 cgd static int seterr __P((struct parse *p, int e));
146 1.5 cgd static cset *allocset __P((struct parse *p));
147 1.5 cgd static void freeset __P((struct parse *p, cset *cs));
148 1.5 cgd static int freezeset __P((struct parse *p, cset *cs));
149 1.5 cgd static int firstch __P((struct parse *p, cset *cs));
150 1.5 cgd static int nch __P((struct parse *p, cset *cs));
151 1.10 mycroft static void mcadd __P((struct parse *p, cset *cs, const char *cp));
152 1.7 christos #if 0
153 1.5 cgd static void mcsub __P((cset *cs, char *cp));
154 1.5 cgd static int mcin __P((cset *cs, char *cp));
155 1.5 cgd static char *mcfind __P((cset *cs, char *cp));
156 1.7 christos #endif
157 1.5 cgd static void mcinvert __P((struct parse *p, cset *cs));
158 1.5 cgd static void mccase __P((struct parse *p, cset *cs));
159 1.5 cgd static int isinsets __P((struct re_guts *g, int c));
160 1.5 cgd static int samesets __P((struct re_guts *g, int c1, int c2));
161 1.5 cgd static void categorize __P((struct parse *p, struct re_guts *g));
162 1.5 cgd static sopno dupl __P((struct parse *p, sopno start, sopno finish));
163 1.12 drochner static void doemit __P((struct parse *p, sop op, sopno opnd));
164 1.12 drochner static void doinsert __P((struct parse *p, sop op, sopno opnd, sopno pos));
165 1.12 drochner static void dofwd __P((struct parse *p, sopno pos, sopno value));
166 1.5 cgd static void enlarge __P((struct parse *p, sopno size));
167 1.5 cgd static void stripsnug __P((struct parse *p, struct re_guts *g));
168 1.5 cgd static void findmust __P((struct parse *p, struct re_guts *g));
169 1.5 cgd static sopno pluscount __P((struct parse *p, struct re_guts *g));
170 1.5 cgd
171 1.5 cgd #ifdef __cplusplus
172 1.5 cgd }
173 1.5 cgd #endif
174 1.5 cgd /* ========= end header generated by ./mkh ========= */
175 1.1 jtc
176 1.1 jtc static char nuls[10]; /* place to point scanner in event of error */
177 1.1 jtc
178 1.1 jtc /*
179 1.1 jtc * macros for use with parse structure
180 1.1 jtc * BEWARE: these know that the parse structure is named `p' !!!
181 1.1 jtc */
182 1.1 jtc #define PEEK() (*p->next)
183 1.1 jtc #define PEEK2() (*(p->next+1))
184 1.1 jtc #define MORE() (p->next < p->end)
185 1.1 jtc #define MORE2() (p->next+1 < p->end)
186 1.1 jtc #define SEE(c) (MORE() && PEEK() == (c))
187 1.1 jtc #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
188 1.1 jtc #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
189 1.1 jtc #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
190 1.1 jtc #define NEXT() (p->next++)
191 1.1 jtc #define NEXT2() (p->next += 2)
192 1.1 jtc #define NEXTn(n) (p->next += (n))
193 1.1 jtc #define GETNEXT() (*p->next++)
194 1.1 jtc #define SETERROR(e) seterr(p, (e))
195 1.7 christos #define REQUIRE(co, e) (void) ((co) || SETERROR(e))
196 1.1 jtc #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
197 1.7 christos #define MUSTEAT(c, e) (void) (REQUIRE(MORE() && GETNEXT() == (c), e))
198 1.1 jtc #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
199 1.12 drochner #define EMIT(op, sopnd) doemit(p, (sop)(op), sopnd)
200 1.12 drochner #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
201 1.12 drochner #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
202 1.1 jtc #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
203 1.1 jtc #define HERE() (p->slen)
204 1.1 jtc #define THERE() (p->slen - 1)
205 1.4 jtc #define THERETHERE() (p->slen - 2)
206 1.1 jtc #define DROP(n) (p->slen -= (n))
207 1.1 jtc
208 1.1 jtc #ifndef NDEBUG
209 1.1 jtc static int never = 0; /* for use in asserts; shuts lint up */
210 1.3 jtc #else
211 1.3 jtc #define never 0 /* some <assert.h>s have bugs too */
212 1.1 jtc #endif
213 1.1 jtc
214 1.1 jtc /*
215 1.1 jtc - regcomp - interface for parser and compilation
216 1.2 jtc = extern int regcomp(regex_t *, const char *, int);
217 1.1 jtc = #define REG_BASIC 0000
218 1.1 jtc = #define REG_EXTENDED 0001
219 1.1 jtc = #define REG_ICASE 0002
220 1.1 jtc = #define REG_NOSUB 0004
221 1.1 jtc = #define REG_NEWLINE 0010
222 1.1 jtc = #define REG_NOSPEC 0020
223 1.1 jtc = #define REG_PEND 0040
224 1.1 jtc = #define REG_DUMP 0200
225 1.1 jtc */
226 1.1 jtc int /* 0 success, otherwise REG_something */
227 1.1 jtc regcomp(preg, pattern, cflags)
228 1.1 jtc regex_t *preg;
229 1.1 jtc const char *pattern;
230 1.1 jtc int cflags;
231 1.1 jtc {
232 1.1 jtc struct parse pa;
233 1.9 perry struct re_guts *g;
234 1.9 perry struct parse *p = &pa;
235 1.9 perry int i;
236 1.9 perry size_t len;
237 1.3 jtc #ifdef REDEBUG
238 1.3 jtc # define GOODFLAGS(f) (f)
239 1.3 jtc #else
240 1.3 jtc # define GOODFLAGS(f) ((f)&~REG_DUMP)
241 1.3 jtc #endif
242 1.1 jtc
243 1.14 lukem _DIAGASSERT(preg != NULL);
244 1.14 lukem _DIAGASSERT(pattern != NULL);
245 1.14 lukem
246 1.3 jtc cflags = GOODFLAGS(cflags);
247 1.1 jtc if ((cflags®_EXTENDED) && (cflags®_NOSPEC))
248 1.1 jtc return(REG_INVARG);
249 1.1 jtc
250 1.1 jtc if (cflags®_PEND) {
251 1.1 jtc if (preg->re_endp < pattern)
252 1.1 jtc return(REG_INVARG);
253 1.1 jtc len = preg->re_endp - pattern;
254 1.1 jtc } else
255 1.11 christos len = strlen(pattern);
256 1.1 jtc
257 1.1 jtc /* do the mallocs early so failure handling is easy */
258 1.1 jtc g = (struct re_guts *)malloc(sizeof(struct re_guts) +
259 1.1 jtc (NC-1)*sizeof(cat_t));
260 1.1 jtc if (g == NULL)
261 1.1 jtc return(REG_ESPACE);
262 1.1 jtc p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
263 1.1 jtc p->strip = (sop *)malloc(p->ssize * sizeof(sop));
264 1.1 jtc p->slen = 0;
265 1.1 jtc if (p->strip == NULL) {
266 1.11 christos free(g);
267 1.1 jtc return(REG_ESPACE);
268 1.1 jtc }
269 1.1 jtc
270 1.1 jtc /* set things up */
271 1.1 jtc p->g = g;
272 1.21 yamt p->next = pattern;
273 1.1 jtc p->end = p->next + len;
274 1.1 jtc p->error = 0;
275 1.1 jtc p->ncsalloc = 0;
276 1.1 jtc for (i = 0; i < NPAREN; i++) {
277 1.1 jtc p->pbegin[i] = 0;
278 1.1 jtc p->pend[i] = 0;
279 1.1 jtc }
280 1.1 jtc g->csetsize = NC;
281 1.1 jtc g->sets = NULL;
282 1.1 jtc g->setbits = NULL;
283 1.1 jtc g->ncsets = 0;
284 1.1 jtc g->cflags = cflags;
285 1.1 jtc g->iflags = 0;
286 1.1 jtc g->nbol = 0;
287 1.1 jtc g->neol = 0;
288 1.1 jtc g->must = NULL;
289 1.1 jtc g->mlen = 0;
290 1.1 jtc g->nsub = 0;
291 1.1 jtc g->ncategories = 1; /* category 0 is "everything else" */
292 1.1 jtc g->categories = &g->catspace[-(CHAR_MIN)];
293 1.1 jtc (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
294 1.1 jtc g->backrefs = 0;
295 1.1 jtc
296 1.1 jtc /* do it */
297 1.1 jtc EMIT(OEND, 0);
298 1.1 jtc g->firststate = THERE();
299 1.1 jtc if (cflags®_EXTENDED)
300 1.1 jtc p_ere(p, OUT);
301 1.1 jtc else if (cflags®_NOSPEC)
302 1.1 jtc p_str(p);
303 1.1 jtc else
304 1.1 jtc p_bre(p, OUT, OUT);
305 1.1 jtc EMIT(OEND, 0);
306 1.1 jtc g->laststate = THERE();
307 1.1 jtc
308 1.22 christos if (g->setbits == NULL) {
309 1.22 christos SETERROR(REG_INVARG);
310 1.22 christos goto out;
311 1.22 christos }
312 1.1 jtc /* tidy up loose ends and fill things in */
313 1.1 jtc categorize(p, g);
314 1.1 jtc stripsnug(p, g);
315 1.1 jtc findmust(p, g);
316 1.1 jtc g->nplus = pluscount(p, g);
317 1.1 jtc g->magic = MAGIC2;
318 1.1 jtc preg->re_nsub = g->nsub;
319 1.1 jtc preg->re_g = g;
320 1.1 jtc preg->re_magic = MAGIC1;
321 1.1 jtc #ifndef REDEBUG
322 1.1 jtc /* not debugging, so can't rely on the assert() in regexec() */
323 1.1 jtc if (g->iflags&BAD)
324 1.1 jtc SETERROR(REG_ASSERT);
325 1.1 jtc #endif
326 1.1 jtc
327 1.22 christos out:
328 1.1 jtc /* win or lose, we're done */
329 1.1 jtc if (p->error != 0) /* lose */
330 1.1 jtc regfree(preg);
331 1.1 jtc return(p->error);
332 1.1 jtc }
333 1.1 jtc
334 1.1 jtc /*
335 1.1 jtc - p_ere - ERE parser top level, concatenation and alternation
336 1.9 perry == static void p_ere(struct parse *p, int stop);
337 1.1 jtc */
338 1.1 jtc static void
339 1.1 jtc p_ere(p, stop)
340 1.9 perry struct parse *p;
341 1.1 jtc int stop; /* character this ERE should end at */
342 1.1 jtc {
343 1.9 perry char c;
344 1.9 perry sopno prevback = 0; /* pacify gcc */
345 1.9 perry sopno prevfwd = 0; /* pacify gcc */
346 1.9 perry sopno conc;
347 1.9 perry int first = 1; /* is this the first alternative? */
348 1.1 jtc
349 1.14 lukem _DIAGASSERT(p != NULL);
350 1.14 lukem
351 1.1 jtc for (;;) {
352 1.1 jtc /* do a bunch of concatenated expressions */
353 1.1 jtc conc = HERE();
354 1.1 jtc while (MORE() && (c = PEEK()) != '|' && c != stop)
355 1.1 jtc p_ere_exp(p);
356 1.1 jtc REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
357 1.1 jtc
358 1.1 jtc if (!EAT('|'))
359 1.1 jtc break; /* NOTE BREAK OUT */
360 1.1 jtc
361 1.1 jtc if (first) {
362 1.1 jtc INSERT(OCH_, conc); /* offset is wrong */
363 1.1 jtc prevfwd = conc;
364 1.1 jtc prevback = conc;
365 1.1 jtc first = 0;
366 1.1 jtc }
367 1.1 jtc ASTERN(OOR1, prevback);
368 1.1 jtc prevback = THERE();
369 1.1 jtc AHEAD(prevfwd); /* fix previous offset */
370 1.1 jtc prevfwd = HERE();
371 1.1 jtc EMIT(OOR2, 0); /* offset is very wrong */
372 1.1 jtc }
373 1.1 jtc
374 1.1 jtc if (!first) { /* tail-end fixups */
375 1.1 jtc AHEAD(prevfwd);
376 1.1 jtc ASTERN(O_CH, prevback);
377 1.1 jtc }
378 1.1 jtc
379 1.1 jtc assert(!MORE() || SEE(stop));
380 1.1 jtc }
381 1.1 jtc
382 1.1 jtc /*
383 1.1 jtc - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
384 1.9 perry == static void p_ere_exp(struct parse *p);
385 1.1 jtc */
386 1.1 jtc static void
387 1.1 jtc p_ere_exp(p)
388 1.9 perry struct parse *p;
389 1.1 jtc {
390 1.9 perry char c;
391 1.9 perry sopno pos;
392 1.9 perry int count;
393 1.9 perry int count2;
394 1.9 perry sopno subno;
395 1.1 jtc int wascaret = 0;
396 1.1 jtc
397 1.14 lukem _DIAGASSERT(p != NULL);
398 1.14 lukem
399 1.1 jtc assert(MORE()); /* caller should have ensured this */
400 1.1 jtc c = GETNEXT();
401 1.1 jtc
402 1.1 jtc pos = HERE();
403 1.1 jtc switch (c) {
404 1.1 jtc case '(':
405 1.1 jtc REQUIRE(MORE(), REG_EPAREN);
406 1.1 jtc p->g->nsub++;
407 1.1 jtc subno = p->g->nsub;
408 1.1 jtc if (subno < NPAREN)
409 1.1 jtc p->pbegin[subno] = HERE();
410 1.1 jtc EMIT(OLPAREN, subno);
411 1.1 jtc if (!SEE(')'))
412 1.1 jtc p_ere(p, ')');
413 1.1 jtc if (subno < NPAREN) {
414 1.1 jtc p->pend[subno] = HERE();
415 1.1 jtc assert(p->pend[subno] != 0);
416 1.1 jtc }
417 1.1 jtc EMIT(ORPAREN, subno);
418 1.1 jtc MUSTEAT(')', REG_EPAREN);
419 1.1 jtc break;
420 1.1 jtc #ifndef POSIX_MISTAKE
421 1.1 jtc case ')': /* happens only if no current unmatched ( */
422 1.1 jtc /*
423 1.1 jtc * You may ask, why the ifndef? Because I didn't notice
424 1.1 jtc * this until slightly too late for 1003.2, and none of the
425 1.1 jtc * other 1003.2 regular-expression reviewers noticed it at
426 1.1 jtc * all. So an unmatched ) is legal POSIX, at least until
427 1.1 jtc * we can get it fixed.
428 1.1 jtc */
429 1.1 jtc SETERROR(REG_EPAREN);
430 1.1 jtc break;
431 1.1 jtc #endif
432 1.1 jtc case '^':
433 1.1 jtc EMIT(OBOL, 0);
434 1.1 jtc p->g->iflags |= USEBOL;
435 1.1 jtc p->g->nbol++;
436 1.1 jtc wascaret = 1;
437 1.1 jtc break;
438 1.1 jtc case '$':
439 1.1 jtc EMIT(OEOL, 0);
440 1.1 jtc p->g->iflags |= USEEOL;
441 1.1 jtc p->g->neol++;
442 1.1 jtc break;
443 1.1 jtc case '|':
444 1.1 jtc SETERROR(REG_EMPTY);
445 1.1 jtc break;
446 1.1 jtc case '*':
447 1.1 jtc case '+':
448 1.1 jtc case '?':
449 1.1 jtc SETERROR(REG_BADRPT);
450 1.1 jtc break;
451 1.1 jtc case '.':
452 1.1 jtc if (p->g->cflags®_NEWLINE)
453 1.1 jtc nonnewline(p);
454 1.1 jtc else
455 1.1 jtc EMIT(OANY, 0);
456 1.1 jtc break;
457 1.1 jtc case '[':
458 1.1 jtc p_bracket(p);
459 1.1 jtc break;
460 1.1 jtc case '\\':
461 1.1 jtc REQUIRE(MORE(), REG_EESCAPE);
462 1.1 jtc c = GETNEXT();
463 1.1 jtc ordinary(p, c);
464 1.1 jtc break;
465 1.1 jtc case '{': /* okay as ordinary except if digit follows */
466 1.17 itohy REQUIRE(!MORE() || !isdigit((unsigned char)PEEK()), REG_BADRPT);
467 1.1 jtc /* FALLTHROUGH */
468 1.1 jtc default:
469 1.1 jtc ordinary(p, c);
470 1.1 jtc break;
471 1.1 jtc }
472 1.1 jtc
473 1.1 jtc if (!MORE())
474 1.1 jtc return;
475 1.1 jtc c = PEEK();
476 1.1 jtc /* we call { a repetition if followed by a digit */
477 1.1 jtc if (!( c == '*' || c == '+' || c == '?' ||
478 1.17 itohy (c == '{' && MORE2() && isdigit((unsigned char)PEEK2())) ))
479 1.1 jtc return; /* no repetition, we're done */
480 1.1 jtc NEXT();
481 1.1 jtc
482 1.1 jtc REQUIRE(!wascaret, REG_BADRPT);
483 1.1 jtc switch (c) {
484 1.1 jtc case '*': /* implemented as +? */
485 1.4 jtc /* this case does not require the (y|) trick, noKLUDGE */
486 1.1 jtc INSERT(OPLUS_, pos);
487 1.1 jtc ASTERN(O_PLUS, pos);
488 1.1 jtc INSERT(OQUEST_, pos);
489 1.1 jtc ASTERN(O_QUEST, pos);
490 1.1 jtc break;
491 1.1 jtc case '+':
492 1.1 jtc INSERT(OPLUS_, pos);
493 1.1 jtc ASTERN(O_PLUS, pos);
494 1.1 jtc break;
495 1.1 jtc case '?':
496 1.4 jtc /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
497 1.4 jtc INSERT(OCH_, pos); /* offset slightly wrong */
498 1.4 jtc ASTERN(OOR1, pos); /* this one's right */
499 1.4 jtc AHEAD(pos); /* fix the OCH_ */
500 1.4 jtc EMIT(OOR2, 0); /* offset very wrong... */
501 1.4 jtc AHEAD(THERE()); /* ...so fix it */
502 1.4 jtc ASTERN(O_CH, THERETHERE());
503 1.1 jtc break;
504 1.1 jtc case '{':
505 1.1 jtc count = p_count(p);
506 1.1 jtc if (EAT(',')) {
507 1.17 itohy if (isdigit((unsigned char)PEEK())) {
508 1.1 jtc count2 = p_count(p);
509 1.1 jtc REQUIRE(count <= count2, REG_BADBR);
510 1.1 jtc } else /* single number with comma */
511 1.1 jtc count2 = INFINITY;
512 1.1 jtc } else /* just a single number */
513 1.1 jtc count2 = count;
514 1.1 jtc repeat(p, pos, count, count2);
515 1.1 jtc if (!EAT('}')) { /* error heuristics */
516 1.1 jtc while (MORE() && PEEK() != '}')
517 1.1 jtc NEXT();
518 1.1 jtc REQUIRE(MORE(), REG_EBRACE);
519 1.1 jtc SETERROR(REG_BADBR);
520 1.1 jtc }
521 1.1 jtc break;
522 1.1 jtc }
523 1.1 jtc
524 1.1 jtc if (!MORE())
525 1.1 jtc return;
526 1.1 jtc c = PEEK();
527 1.1 jtc if (!( c == '*' || c == '+' || c == '?' ||
528 1.17 itohy (c == '{' && MORE2() && isdigit((unsigned char)PEEK2())) ) )
529 1.1 jtc return;
530 1.1 jtc SETERROR(REG_BADRPT);
531 1.1 jtc }
532 1.1 jtc
533 1.1 jtc /*
534 1.1 jtc - p_str - string (no metacharacters) "parser"
535 1.9 perry == static void p_str(struct parse *p);
536 1.1 jtc */
537 1.1 jtc static void
538 1.1 jtc p_str(p)
539 1.9 perry struct parse *p;
540 1.1 jtc {
541 1.14 lukem
542 1.14 lukem _DIAGASSERT(p != NULL);
543 1.14 lukem
544 1.1 jtc REQUIRE(MORE(), REG_EMPTY);
545 1.1 jtc while (MORE())
546 1.1 jtc ordinary(p, GETNEXT());
547 1.1 jtc }
548 1.1 jtc
549 1.1 jtc /*
550 1.1 jtc - p_bre - BRE parser top level, anchoring and concatenation
551 1.9 perry == static void p_bre(struct parse *p, int end1, \
552 1.9 perry == int end2);
553 1.1 jtc * Giving end1 as OUT essentially eliminates the end1/end2 check.
554 1.1 jtc *
555 1.1 jtc * This implementation is a bit of a kludge, in that a trailing $ is first
556 1.1 jtc * taken as an ordinary character and then revised to be an anchor. The
557 1.1 jtc * only undesirable side effect is that '$' gets included as a character
558 1.1 jtc * category in such cases. This is fairly harmless; not worth fixing.
559 1.1 jtc * The amount of lookahead needed to avoid this kludge is excessive.
560 1.1 jtc */
561 1.1 jtc static void
562 1.1 jtc p_bre(p, end1, end2)
563 1.9 perry struct parse *p;
564 1.9 perry int end1; /* first terminating character */
565 1.9 perry int end2; /* second terminating character */
566 1.9 perry {
567 1.14 lukem sopno start;
568 1.9 perry int first = 1; /* first subexpression? */
569 1.9 perry int wasdollar = 0;
570 1.1 jtc
571 1.14 lukem _DIAGASSERT(p != NULL);
572 1.14 lukem
573 1.14 lukem start = HERE();
574 1.14 lukem
575 1.1 jtc if (EAT('^')) {
576 1.1 jtc EMIT(OBOL, 0);
577 1.1 jtc p->g->iflags |= USEBOL;
578 1.1 jtc p->g->nbol++;
579 1.1 jtc }
580 1.1 jtc while (MORE() && !SEETWO(end1, end2)) {
581 1.1 jtc wasdollar = p_simp_re(p, first);
582 1.1 jtc first = 0;
583 1.1 jtc }
584 1.1 jtc if (wasdollar) { /* oops, that was a trailing anchor */
585 1.1 jtc DROP(1);
586 1.1 jtc EMIT(OEOL, 0);
587 1.1 jtc p->g->iflags |= USEEOL;
588 1.1 jtc p->g->neol++;
589 1.1 jtc }
590 1.1 jtc
591 1.1 jtc REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
592 1.1 jtc }
593 1.1 jtc
594 1.1 jtc /*
595 1.1 jtc - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
596 1.9 perry == static int p_simp_re(struct parse *p, int starordinary);
597 1.1 jtc */
598 1.1 jtc static int /* was the simple RE an unbackslashed $? */
599 1.1 jtc p_simp_re(p, starordinary)
600 1.9 perry struct parse *p;
601 1.1 jtc int starordinary; /* is a leading * an ordinary character? */
602 1.1 jtc {
603 1.9 perry int c;
604 1.9 perry int count;
605 1.9 perry int count2;
606 1.9 perry sopno pos;
607 1.9 perry int i;
608 1.9 perry sopno subno;
609 1.1 jtc # define BACKSL (1<<CHAR_BIT)
610 1.1 jtc
611 1.14 lukem _DIAGASSERT(p != NULL);
612 1.14 lukem
613 1.1 jtc pos = HERE(); /* repetion op, if any, covers from here */
614 1.1 jtc
615 1.1 jtc assert(MORE()); /* caller should have ensured this */
616 1.1 jtc c = GETNEXT();
617 1.1 jtc if (c == '\\') {
618 1.1 jtc REQUIRE(MORE(), REG_EESCAPE);
619 1.1 jtc c = BACKSL | (unsigned char)GETNEXT();
620 1.1 jtc }
621 1.1 jtc switch (c) {
622 1.1 jtc case '.':
623 1.1 jtc if (p->g->cflags®_NEWLINE)
624 1.1 jtc nonnewline(p);
625 1.1 jtc else
626 1.1 jtc EMIT(OANY, 0);
627 1.1 jtc break;
628 1.1 jtc case '[':
629 1.1 jtc p_bracket(p);
630 1.1 jtc break;
631 1.1 jtc case BACKSL|'{':
632 1.1 jtc SETERROR(REG_BADRPT);
633 1.1 jtc break;
634 1.1 jtc case BACKSL|'(':
635 1.1 jtc p->g->nsub++;
636 1.1 jtc subno = p->g->nsub;
637 1.1 jtc if (subno < NPAREN)
638 1.1 jtc p->pbegin[subno] = HERE();
639 1.1 jtc EMIT(OLPAREN, subno);
640 1.1 jtc /* the MORE here is an error heuristic */
641 1.1 jtc if (MORE() && !SEETWO('\\', ')'))
642 1.1 jtc p_bre(p, '\\', ')');
643 1.1 jtc if (subno < NPAREN) {
644 1.1 jtc p->pend[subno] = HERE();
645 1.1 jtc assert(p->pend[subno] != 0);
646 1.1 jtc }
647 1.1 jtc EMIT(ORPAREN, subno);
648 1.1 jtc REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
649 1.1 jtc break;
650 1.1 jtc case BACKSL|')': /* should not get here -- must be user */
651 1.1 jtc case BACKSL|'}':
652 1.1 jtc SETERROR(REG_EPAREN);
653 1.1 jtc break;
654 1.1 jtc case BACKSL|'1':
655 1.1 jtc case BACKSL|'2':
656 1.1 jtc case BACKSL|'3':
657 1.1 jtc case BACKSL|'4':
658 1.1 jtc case BACKSL|'5':
659 1.1 jtc case BACKSL|'6':
660 1.1 jtc case BACKSL|'7':
661 1.1 jtc case BACKSL|'8':
662 1.1 jtc case BACKSL|'9':
663 1.1 jtc i = (c&~BACKSL) - '0';
664 1.1 jtc assert(i < NPAREN);
665 1.1 jtc if (p->pend[i] != 0) {
666 1.1 jtc assert(i <= p->g->nsub);
667 1.1 jtc EMIT(OBACK_, i);
668 1.1 jtc assert(p->pbegin[i] != 0);
669 1.1 jtc assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
670 1.1 jtc assert(OP(p->strip[p->pend[i]]) == ORPAREN);
671 1.1 jtc (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
672 1.1 jtc EMIT(O_BACK, i);
673 1.1 jtc } else
674 1.1 jtc SETERROR(REG_ESUBREG);
675 1.1 jtc p->g->backrefs = 1;
676 1.1 jtc break;
677 1.1 jtc case '*':
678 1.1 jtc REQUIRE(starordinary, REG_BADRPT);
679 1.1 jtc /* FALLTHROUGH */
680 1.1 jtc default:
681 1.1 jtc ordinary(p, c &~ BACKSL);
682 1.1 jtc break;
683 1.1 jtc }
684 1.1 jtc
685 1.1 jtc if (EAT('*')) { /* implemented as +? */
686 1.4 jtc /* this case does not require the (y|) trick, noKLUDGE */
687 1.1 jtc INSERT(OPLUS_, pos);
688 1.1 jtc ASTERN(O_PLUS, pos);
689 1.1 jtc INSERT(OQUEST_, pos);
690 1.1 jtc ASTERN(O_QUEST, pos);
691 1.1 jtc } else if (EATTWO('\\', '{')) {
692 1.1 jtc count = p_count(p);
693 1.1 jtc if (EAT(',')) {
694 1.17 itohy if (MORE() && isdigit((unsigned char)PEEK())) {
695 1.1 jtc count2 = p_count(p);
696 1.1 jtc REQUIRE(count <= count2, REG_BADBR);
697 1.1 jtc } else /* single number with comma */
698 1.1 jtc count2 = INFINITY;
699 1.1 jtc } else /* just a single number */
700 1.1 jtc count2 = count;
701 1.1 jtc repeat(p, pos, count, count2);
702 1.1 jtc if (!EATTWO('\\', '}')) { /* error heuristics */
703 1.1 jtc while (MORE() && !SEETWO('\\', '}'))
704 1.1 jtc NEXT();
705 1.1 jtc REQUIRE(MORE(), REG_EBRACE);
706 1.1 jtc SETERROR(REG_BADBR);
707 1.1 jtc }
708 1.1 jtc } else if (c == (unsigned char)'$') /* $ (but not \$) ends it */
709 1.1 jtc return(1);
710 1.1 jtc
711 1.1 jtc return(0);
712 1.1 jtc }
713 1.1 jtc
714 1.1 jtc /*
715 1.1 jtc - p_count - parse a repetition count
716 1.9 perry == static int p_count(struct parse *p);
717 1.1 jtc */
718 1.1 jtc static int /* the value */
719 1.1 jtc p_count(p)
720 1.9 perry struct parse *p;
721 1.1 jtc {
722 1.9 perry int count = 0;
723 1.9 perry int ndigits = 0;
724 1.1 jtc
725 1.14 lukem _DIAGASSERT(p != NULL);
726 1.14 lukem
727 1.17 itohy while (MORE() && isdigit((unsigned char)PEEK()) && count <= DUPMAX) {
728 1.1 jtc count = count*10 + (GETNEXT() - '0');
729 1.1 jtc ndigits++;
730 1.1 jtc }
731 1.1 jtc
732 1.1 jtc REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
733 1.1 jtc return(count);
734 1.1 jtc }
735 1.1 jtc
736 1.1 jtc /*
737 1.1 jtc - p_bracket - parse a bracketed character list
738 1.9 perry == static void p_bracket(struct parse *p);
739 1.1 jtc *
740 1.1 jtc * Note a significant property of this code: if the allocset() did SETERROR,
741 1.1 jtc * no set operations are done.
742 1.1 jtc */
743 1.1 jtc static void
744 1.1 jtc p_bracket(p)
745 1.9 perry struct parse *p;
746 1.1 jtc {
747 1.14 lukem cset *cs;
748 1.9 perry int invert = 0;
749 1.1 jtc
750 1.14 lukem _DIAGASSERT(p != NULL);
751 1.14 lukem
752 1.14 lukem cs = allocset(p);
753 1.14 lukem
754 1.1 jtc /* Dept of Truly Sickening Special-Case Kludges */
755 1.13 drochner if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]",
756 1.13 drochner (size_t)6) == 0) {
757 1.1 jtc EMIT(OBOW, 0);
758 1.1 jtc NEXTn(6);
759 1.1 jtc return;
760 1.1 jtc }
761 1.13 drochner if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]",
762 1.13 drochner (size_t)6) == 0) {
763 1.1 jtc EMIT(OEOW, 0);
764 1.1 jtc NEXTn(6);
765 1.1 jtc return;
766 1.1 jtc }
767 1.1 jtc
768 1.1 jtc if (EAT('^'))
769 1.1 jtc invert++; /* make note to invert set at end */
770 1.1 jtc if (EAT(']'))
771 1.1 jtc CHadd(cs, ']');
772 1.1 jtc else if (EAT('-'))
773 1.1 jtc CHadd(cs, '-');
774 1.1 jtc while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
775 1.1 jtc p_b_term(p, cs);
776 1.1 jtc if (EAT('-'))
777 1.1 jtc CHadd(cs, '-');
778 1.1 jtc MUSTEAT(']', REG_EBRACK);
779 1.1 jtc
780 1.1 jtc if (p->error != 0) /* don't mess things up further */
781 1.1 jtc return;
782 1.1 jtc
783 1.1 jtc if (p->g->cflags®_ICASE) {
784 1.9 perry int i;
785 1.9 perry int ci;
786 1.1 jtc
787 1.1 jtc for (i = p->g->csetsize - 1; i >= 0; i--)
788 1.1 jtc if (CHIN(cs, i) && isalpha(i)) {
789 1.1 jtc ci = othercase(i);
790 1.1 jtc if (ci != i)
791 1.1 jtc CHadd(cs, ci);
792 1.1 jtc }
793 1.1 jtc if (cs->multis != NULL)
794 1.2 jtc mccase(p, cs);
795 1.1 jtc }
796 1.1 jtc if (invert) {
797 1.9 perry int i;
798 1.1 jtc
799 1.1 jtc for (i = p->g->csetsize - 1; i >= 0; i--)
800 1.1 jtc if (CHIN(cs, i))
801 1.1 jtc CHsub(cs, i);
802 1.1 jtc else
803 1.1 jtc CHadd(cs, i);
804 1.1 jtc if (p->g->cflags®_NEWLINE)
805 1.1 jtc CHsub(cs, '\n');
806 1.1 jtc if (cs->multis != NULL)
807 1.2 jtc mcinvert(p, cs);
808 1.1 jtc }
809 1.1 jtc
810 1.1 jtc assert(cs->multis == NULL); /* xxx */
811 1.1 jtc
812 1.1 jtc if (nch(p, cs) == 1) { /* optimize singleton sets */
813 1.1 jtc ordinary(p, firstch(p, cs));
814 1.1 jtc freeset(p, cs);
815 1.1 jtc } else
816 1.1 jtc EMIT(OANYOF, freezeset(p, cs));
817 1.1 jtc }
818 1.1 jtc
819 1.1 jtc /*
820 1.1 jtc - p_b_term - parse one term of a bracketed character list
821 1.9 perry == static void p_b_term(struct parse *p, cset *cs);
822 1.1 jtc */
823 1.1 jtc static void
824 1.1 jtc p_b_term(p, cs)
825 1.9 perry struct parse *p;
826 1.9 perry cset *cs;
827 1.1 jtc {
828 1.9 perry char c;
829 1.9 perry char start, finish;
830 1.9 perry int i;
831 1.1 jtc
832 1.14 lukem _DIAGASSERT(p != NULL);
833 1.14 lukem _DIAGASSERT(cs != NULL);
834 1.14 lukem
835 1.1 jtc /* classify what we've got */
836 1.1 jtc switch ((MORE()) ? PEEK() : '\0') {
837 1.1 jtc case '[':
838 1.1 jtc c = (MORE2()) ? PEEK2() : '\0';
839 1.1 jtc break;
840 1.11 christos
841 1.1 jtc case '-':
842 1.1 jtc SETERROR(REG_ERANGE);
843 1.1 jtc return; /* NOTE RETURN */
844 1.11 christos
845 1.1 jtc default:
846 1.1 jtc c = '\0';
847 1.1 jtc break;
848 1.1 jtc }
849 1.1 jtc
850 1.1 jtc switch (c) {
851 1.1 jtc case ':': /* character class */
852 1.1 jtc NEXT2();
853 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
854 1.1 jtc c = PEEK();
855 1.1 jtc REQUIRE(c != '-' && c != ']', REG_ECTYPE);
856 1.1 jtc p_b_cclass(p, cs);
857 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
858 1.1 jtc REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
859 1.1 jtc break;
860 1.1 jtc case '=': /* equivalence class */
861 1.1 jtc NEXT2();
862 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
863 1.1 jtc c = PEEK();
864 1.1 jtc REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
865 1.1 jtc p_b_eclass(p, cs);
866 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
867 1.1 jtc REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
868 1.1 jtc break;
869 1.1 jtc default: /* symbol, ordinary character, or range */
870 1.1 jtc /* xxx revision needed for multichar stuff */
871 1.1 jtc start = p_b_symbol(p);
872 1.1 jtc if (SEE('-') && MORE2() && PEEK2() != ']') {
873 1.1 jtc /* range */
874 1.1 jtc NEXT();
875 1.1 jtc if (EAT('-'))
876 1.1 jtc finish = '-';
877 1.1 jtc else
878 1.1 jtc finish = p_b_symbol(p);
879 1.1 jtc } else
880 1.1 jtc finish = start;
881 1.1 jtc /* xxx what about signed chars here... */
882 1.1 jtc REQUIRE(start <= finish, REG_ERANGE);
883 1.1 jtc for (i = start; i <= finish; i++)
884 1.1 jtc CHadd(cs, i);
885 1.1 jtc break;
886 1.1 jtc }
887 1.1 jtc }
888 1.1 jtc
889 1.1 jtc /*
890 1.1 jtc - p_b_cclass - parse a character-class name and deal with it
891 1.9 perry == static void p_b_cclass(struct parse *p, cset *cs);
892 1.1 jtc */
893 1.1 jtc static void
894 1.1 jtc p_b_cclass(p, cs)
895 1.9 perry struct parse *p;
896 1.9 perry cset *cs;
897 1.1 jtc {
898 1.21 yamt const char *sp;
899 1.10 mycroft const struct cclass *cp;
900 1.9 perry size_t len;
901 1.10 mycroft const char *u;
902 1.9 perry char c;
903 1.1 jtc
904 1.14 lukem _DIAGASSERT(p != NULL);
905 1.14 lukem _DIAGASSERT(cs != NULL);
906 1.14 lukem
907 1.14 lukem sp = p->next;
908 1.14 lukem
909 1.17 itohy while (MORE() && isalpha((unsigned char)PEEK()))
910 1.1 jtc NEXT();
911 1.1 jtc len = p->next - sp;
912 1.1 jtc for (cp = cclasses; cp->name != NULL; cp++)
913 1.1 jtc if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
914 1.1 jtc break;
915 1.1 jtc if (cp->name == NULL) {
916 1.1 jtc /* oops, didn't find it */
917 1.1 jtc SETERROR(REG_ECTYPE);
918 1.1 jtc return;
919 1.1 jtc }
920 1.1 jtc
921 1.1 jtc u = cp->chars;
922 1.1 jtc while ((c = *u++) != '\0')
923 1.1 jtc CHadd(cs, c);
924 1.1 jtc for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
925 1.2 jtc MCadd(p, cs, u);
926 1.1 jtc }
927 1.1 jtc
928 1.1 jtc /*
929 1.1 jtc - p_b_eclass - parse an equivalence-class name and deal with it
930 1.9 perry == static void p_b_eclass(struct parse *p, cset *cs);
931 1.1 jtc *
932 1.1 jtc * This implementation is incomplete. xxx
933 1.1 jtc */
934 1.1 jtc static void
935 1.1 jtc p_b_eclass(p, cs)
936 1.9 perry struct parse *p;
937 1.9 perry cset *cs;
938 1.1 jtc {
939 1.9 perry char c;
940 1.1 jtc
941 1.14 lukem _DIAGASSERT(p != NULL);
942 1.14 lukem _DIAGASSERT(cs != NULL);
943 1.14 lukem
944 1.1 jtc c = p_b_coll_elem(p, '=');
945 1.1 jtc CHadd(cs, c);
946 1.1 jtc }
947 1.1 jtc
948 1.1 jtc /*
949 1.1 jtc - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
950 1.9 perry == static char p_b_symbol(struct parse *p);
951 1.1 jtc */
952 1.1 jtc static char /* value of symbol */
953 1.1 jtc p_b_symbol(p)
954 1.9 perry struct parse *p;
955 1.1 jtc {
956 1.9 perry char value;
957 1.1 jtc
958 1.14 lukem _DIAGASSERT(p != NULL);
959 1.14 lukem
960 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
961 1.1 jtc if (!EATTWO('[', '.'))
962 1.1 jtc return(GETNEXT());
963 1.1 jtc
964 1.1 jtc /* collating symbol */
965 1.1 jtc value = p_b_coll_elem(p, '.');
966 1.1 jtc REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
967 1.1 jtc return(value);
968 1.1 jtc }
969 1.1 jtc
970 1.1 jtc /*
971 1.1 jtc - p_b_coll_elem - parse a collating-element name and look it up
972 1.9 perry == static char p_b_coll_elem(struct parse *p, int endc);
973 1.1 jtc */
974 1.1 jtc static char /* value of collating element */
975 1.1 jtc p_b_coll_elem(p, endc)
976 1.9 perry struct parse *p;
977 1.1 jtc int endc; /* name ended by endc,']' */
978 1.1 jtc {
979 1.21 yamt const char *sp;
980 1.10 mycroft const struct cname *cp;
981 1.11 christos size_t len;
982 1.1 jtc
983 1.14 lukem _DIAGASSERT(p != NULL);
984 1.14 lukem
985 1.14 lukem sp = p->next;
986 1.14 lukem
987 1.1 jtc while (MORE() && !SEETWO(endc, ']'))
988 1.1 jtc NEXT();
989 1.1 jtc if (!MORE()) {
990 1.1 jtc SETERROR(REG_EBRACK);
991 1.1 jtc return(0);
992 1.1 jtc }
993 1.1 jtc len = p->next - sp;
994 1.1 jtc for (cp = cnames; cp->name != NULL; cp++)
995 1.1 jtc if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
996 1.1 jtc return(cp->code); /* known name */
997 1.1 jtc if (len == 1)
998 1.1 jtc return(*sp); /* single character */
999 1.1 jtc SETERROR(REG_ECOLLATE); /* neither */
1000 1.1 jtc return(0);
1001 1.1 jtc }
1002 1.1 jtc
1003 1.1 jtc /*
1004 1.1 jtc - othercase - return the case counterpart of an alphabetic
1005 1.19 jdolecek == static int othercase(int ch);
1006 1.1 jtc */
1007 1.19 jdolecek static int /* if no counterpart, return ch */
1008 1.1 jtc othercase(ch)
1009 1.1 jtc int ch;
1010 1.1 jtc {
1011 1.1 jtc assert(isalpha(ch));
1012 1.1 jtc if (isupper(ch))
1013 1.1 jtc return(tolower(ch));
1014 1.1 jtc else if (islower(ch))
1015 1.1 jtc return(toupper(ch));
1016 1.1 jtc else /* peculiar, but could happen */
1017 1.1 jtc return(ch);
1018 1.1 jtc }
1019 1.1 jtc
1020 1.1 jtc /*
1021 1.1 jtc - bothcases - emit a dualcase version of a two-case character
1022 1.9 perry == static void bothcases(struct parse *p, int ch);
1023 1.1 jtc *
1024 1.1 jtc * Boy, is this implementation ever a kludge...
1025 1.1 jtc */
1026 1.1 jtc static void
1027 1.1 jtc bothcases(p, ch)
1028 1.9 perry struct parse *p;
1029 1.1 jtc int ch;
1030 1.1 jtc {
1031 1.21 yamt const char *oldnext;
1032 1.21 yamt const char *oldend;
1033 1.1 jtc char bracket[3];
1034 1.1 jtc
1035 1.14 lukem _DIAGASSERT(p != NULL);
1036 1.14 lukem
1037 1.14 lukem oldnext = p->next;
1038 1.14 lukem oldend = p->end;
1039 1.14 lukem
1040 1.1 jtc assert(othercase(ch) != ch); /* p_bracket() would recurse */
1041 1.1 jtc p->next = bracket;
1042 1.1 jtc p->end = bracket+2;
1043 1.1 jtc bracket[0] = ch;
1044 1.1 jtc bracket[1] = ']';
1045 1.1 jtc bracket[2] = '\0';
1046 1.1 jtc p_bracket(p);
1047 1.1 jtc assert(p->next == bracket+2);
1048 1.1 jtc p->next = oldnext;
1049 1.1 jtc p->end = oldend;
1050 1.1 jtc }
1051 1.1 jtc
1052 1.1 jtc /*
1053 1.1 jtc - ordinary - emit an ordinary character
1054 1.9 perry == static void ordinary(struct parse *p, int ch);
1055 1.1 jtc */
1056 1.1 jtc static void
1057 1.1 jtc ordinary(p, ch)
1058 1.9 perry struct parse *p;
1059 1.9 perry int ch;
1060 1.1 jtc {
1061 1.14 lukem cat_t *cap;
1062 1.1 jtc
1063 1.14 lukem _DIAGASSERT(p != NULL);
1064 1.14 lukem
1065 1.14 lukem cap = p->g->categories;
1066 1.17 itohy if ((p->g->cflags®_ICASE) && isalpha((unsigned char) ch)
1067 1.17 itohy && othercase((unsigned char) ch) != (unsigned char) ch)
1068 1.17 itohy bothcases(p, (unsigned char) ch);
1069 1.1 jtc else {
1070 1.1 jtc EMIT(OCHAR, (unsigned char)ch);
1071 1.1 jtc if (cap[ch] == 0)
1072 1.1 jtc cap[ch] = p->g->ncategories++;
1073 1.1 jtc }
1074 1.1 jtc }
1075 1.1 jtc
1076 1.1 jtc /*
1077 1.1 jtc - nonnewline - emit REG_NEWLINE version of OANY
1078 1.9 perry == static void nonnewline(struct parse *p);
1079 1.1 jtc *
1080 1.1 jtc * Boy, is this implementation ever a kludge...
1081 1.1 jtc */
1082 1.1 jtc static void
1083 1.1 jtc nonnewline(p)
1084 1.9 perry struct parse *p;
1085 1.1 jtc {
1086 1.21 yamt const char *oldnext;
1087 1.21 yamt const char *oldend;
1088 1.1 jtc char bracket[4];
1089 1.1 jtc
1090 1.14 lukem _DIAGASSERT(p != NULL);
1091 1.14 lukem
1092 1.14 lukem oldnext = p->next;
1093 1.14 lukem oldend = p->end;
1094 1.14 lukem
1095 1.1 jtc p->next = bracket;
1096 1.1 jtc p->end = bracket+3;
1097 1.1 jtc bracket[0] = '^';
1098 1.1 jtc bracket[1] = '\n';
1099 1.1 jtc bracket[2] = ']';
1100 1.1 jtc bracket[3] = '\0';
1101 1.1 jtc p_bracket(p);
1102 1.1 jtc assert(p->next == bracket+3);
1103 1.1 jtc p->next = oldnext;
1104 1.1 jtc p->end = oldend;
1105 1.1 jtc }
1106 1.1 jtc
1107 1.1 jtc /*
1108 1.1 jtc - repeat - generate code for a bounded repetition, recursively if needed
1109 1.9 perry == static void repeat(struct parse *p, sopno start, int from, int to);
1110 1.1 jtc */
1111 1.1 jtc static void
1112 1.1 jtc repeat(p, start, from, to)
1113 1.9 perry struct parse *p;
1114 1.1 jtc sopno start; /* operand from here to end of strip */
1115 1.1 jtc int from; /* repeated from this number */
1116 1.1 jtc int to; /* to this number of times (maybe INFINITY) */
1117 1.1 jtc {
1118 1.14 lukem sopno finish;
1119 1.1 jtc # define N 2
1120 1.1 jtc # define INF 3
1121 1.1 jtc # define REP(f, t) ((f)*8 + (t))
1122 1.1 jtc # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1123 1.9 perry sopno copy;
1124 1.1 jtc
1125 1.14 lukem _DIAGASSERT(p != NULL);
1126 1.14 lukem
1127 1.14 lukem finish = HERE();
1128 1.14 lukem
1129 1.1 jtc if (p->error != 0) /* head off possible runaway recursion */
1130 1.1 jtc return;
1131 1.1 jtc
1132 1.1 jtc assert(from <= to);
1133 1.1 jtc
1134 1.1 jtc switch (REP(MAP(from), MAP(to))) {
1135 1.1 jtc case REP(0, 0): /* must be user doing this */
1136 1.1 jtc DROP(finish-start); /* drop the operand */
1137 1.1 jtc break;
1138 1.1 jtc case REP(0, 1): /* as x{1,1}? */
1139 1.1 jtc case REP(0, N): /* as x{1,n}? */
1140 1.1 jtc case REP(0, INF): /* as x{1,}? */
1141 1.4 jtc /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1142 1.4 jtc INSERT(OCH_, start); /* offset is wrong... */
1143 1.1 jtc repeat(p, start+1, 1, to);
1144 1.4 jtc ASTERN(OOR1, start);
1145 1.1 jtc AHEAD(start); /* ... fix it */
1146 1.4 jtc EMIT(OOR2, 0);
1147 1.4 jtc AHEAD(THERE());
1148 1.4 jtc ASTERN(O_CH, THERETHERE());
1149 1.1 jtc break;
1150 1.1 jtc case REP(1, 1): /* trivial case */
1151 1.1 jtc /* done */
1152 1.1 jtc break;
1153 1.1 jtc case REP(1, N): /* as x?x{1,n-1} */
1154 1.4 jtc /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1155 1.4 jtc INSERT(OCH_, start);
1156 1.4 jtc ASTERN(OOR1, start);
1157 1.4 jtc AHEAD(start);
1158 1.4 jtc EMIT(OOR2, 0); /* offset very wrong... */
1159 1.4 jtc AHEAD(THERE()); /* ...so fix it */
1160 1.4 jtc ASTERN(O_CH, THERETHERE());
1161 1.1 jtc copy = dupl(p, start+1, finish+1);
1162 1.4 jtc assert(copy == finish+4);
1163 1.1 jtc repeat(p, copy, 1, to-1);
1164 1.1 jtc break;
1165 1.1 jtc case REP(1, INF): /* as x+ */
1166 1.1 jtc INSERT(OPLUS_, start);
1167 1.1 jtc ASTERN(O_PLUS, start);
1168 1.1 jtc break;
1169 1.1 jtc case REP(N, N): /* as xx{m-1,n-1} */
1170 1.1 jtc copy = dupl(p, start, finish);
1171 1.1 jtc repeat(p, copy, from-1, to-1);
1172 1.1 jtc break;
1173 1.1 jtc case REP(N, INF): /* as xx{n-1,INF} */
1174 1.1 jtc copy = dupl(p, start, finish);
1175 1.1 jtc repeat(p, copy, from-1, to);
1176 1.1 jtc break;
1177 1.1 jtc default: /* "can't happen" */
1178 1.1 jtc SETERROR(REG_ASSERT); /* just in case */
1179 1.1 jtc break;
1180 1.1 jtc }
1181 1.1 jtc }
1182 1.1 jtc
1183 1.1 jtc /*
1184 1.1 jtc - seterr - set an error condition
1185 1.9 perry == static int seterr(struct parse *p, int e);
1186 1.1 jtc */
1187 1.1 jtc static int /* useless but makes type checking happy */
1188 1.1 jtc seterr(p, e)
1189 1.9 perry struct parse *p;
1190 1.1 jtc int e;
1191 1.1 jtc {
1192 1.14 lukem
1193 1.14 lukem _DIAGASSERT(p != NULL);
1194 1.14 lukem
1195 1.1 jtc if (p->error == 0) /* keep earliest error condition */
1196 1.1 jtc p->error = e;
1197 1.1 jtc p->next = nuls; /* try to bring things to a halt */
1198 1.1 jtc p->end = nuls;
1199 1.1 jtc return(0); /* make the return value well-defined */
1200 1.1 jtc }
1201 1.1 jtc
1202 1.1 jtc /*
1203 1.1 jtc - allocset - allocate a set of characters for []
1204 1.9 perry == static cset *allocset(struct parse *p);
1205 1.1 jtc */
1206 1.1 jtc static cset *
1207 1.1 jtc allocset(p)
1208 1.9 perry struct parse *p;
1209 1.1 jtc {
1210 1.14 lukem int no;
1211 1.9 perry size_t nc;
1212 1.9 perry size_t nbytes;
1213 1.9 perry cset *cs;
1214 1.14 lukem size_t css;
1215 1.9 perry int i;
1216 1.1 jtc
1217 1.14 lukem _DIAGASSERT(p != NULL);
1218 1.14 lukem
1219 1.14 lukem no = p->g->ncsets++;
1220 1.14 lukem css = (size_t)p->g->csetsize;
1221 1.1 jtc if (no >= p->ncsalloc) { /* need another column of space */
1222 1.1 jtc p->ncsalloc += CHAR_BIT;
1223 1.1 jtc nc = p->ncsalloc;
1224 1.1 jtc assert(nc % CHAR_BIT == 0);
1225 1.1 jtc nbytes = nc / CHAR_BIT * css;
1226 1.1 jtc if (p->g->sets == NULL)
1227 1.11 christos p->g->sets = malloc(nc * sizeof(cset));
1228 1.1 jtc else
1229 1.11 christos p->g->sets = realloc(p->g->sets, nc * sizeof(cset));
1230 1.1 jtc if (p->g->setbits == NULL)
1231 1.11 christos p->g->setbits = malloc(nbytes);
1232 1.2 jtc else {
1233 1.11 christos p->g->setbits = realloc(p->g->setbits, nbytes);
1234 1.2 jtc /* xxx this isn't right if setbits is now NULL */
1235 1.2 jtc for (i = 0; i < no; i++)
1236 1.2 jtc p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1237 1.2 jtc }
1238 1.1 jtc if (p->g->sets != NULL && p->g->setbits != NULL)
1239 1.1 jtc (void) memset((char *)p->g->setbits + (nbytes - css),
1240 1.1 jtc 0, css);
1241 1.1 jtc else {
1242 1.1 jtc no = 0;
1243 1.1 jtc SETERROR(REG_ESPACE);
1244 1.1 jtc /* caller's responsibility not to do set ops */
1245 1.1 jtc }
1246 1.1 jtc }
1247 1.1 jtc
1248 1.1 jtc assert(p->g->sets != NULL); /* xxx */
1249 1.1 jtc cs = &p->g->sets[no];
1250 1.1 jtc cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1251 1.1 jtc cs->mask = 1 << ((no) % CHAR_BIT);
1252 1.1 jtc cs->hash = 0;
1253 1.1 jtc cs->smultis = 0;
1254 1.1 jtc cs->multis = NULL;
1255 1.1 jtc
1256 1.1 jtc return(cs);
1257 1.1 jtc }
1258 1.1 jtc
1259 1.1 jtc /*
1260 1.1 jtc - freeset - free a now-unused set
1261 1.9 perry == static void freeset(struct parse *p, cset *cs);
1262 1.1 jtc */
1263 1.1 jtc static void
1264 1.1 jtc freeset(p, cs)
1265 1.9 perry struct parse *p;
1266 1.9 perry cset *cs;
1267 1.1 jtc {
1268 1.9 perry int i;
1269 1.14 lukem cset *top;
1270 1.14 lukem size_t css;
1271 1.14 lukem
1272 1.14 lukem _DIAGASSERT(p != NULL);
1273 1.14 lukem _DIAGASSERT(cs != NULL);
1274 1.14 lukem
1275 1.14 lukem top = &p->g->sets[p->g->ncsets];
1276 1.14 lukem css = (size_t)p->g->csetsize;
1277 1.1 jtc
1278 1.1 jtc for (i = 0; i < css; i++)
1279 1.1 jtc CHsub(cs, i);
1280 1.1 jtc if (cs == top-1) /* recover only the easy case */
1281 1.1 jtc p->g->ncsets--;
1282 1.1 jtc }
1283 1.1 jtc
1284 1.1 jtc /*
1285 1.1 jtc - freezeset - final processing on a set of characters
1286 1.9 perry == static int freezeset(struct parse *p, cset *cs);
1287 1.1 jtc *
1288 1.1 jtc * The main task here is merging identical sets. This is usually a waste
1289 1.1 jtc * of time (although the hash code minimizes the overhead), but can win
1290 1.1 jtc * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1291 1.1 jtc * is done using addition rather than xor -- all ASCII [aA] sets xor to
1292 1.1 jtc * the same value!
1293 1.1 jtc */
1294 1.1 jtc static int /* set number */
1295 1.1 jtc freezeset(p, cs)
1296 1.9 perry struct parse *p;
1297 1.9 perry cset *cs;
1298 1.1 jtc {
1299 1.14 lukem uch h;
1300 1.9 perry int i;
1301 1.14 lukem cset *top;
1302 1.9 perry cset *cs2;
1303 1.14 lukem size_t css;
1304 1.14 lukem
1305 1.14 lukem _DIAGASSERT(p != NULL);
1306 1.14 lukem _DIAGASSERT(cs != NULL);
1307 1.14 lukem
1308 1.14 lukem h = cs->hash;
1309 1.14 lukem top = &p->g->sets[p->g->ncsets];
1310 1.14 lukem css = (size_t)p->g->csetsize;
1311 1.1 jtc
1312 1.1 jtc /* look for an earlier one which is the same */
1313 1.1 jtc for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1314 1.1 jtc if (cs2->hash == h && cs2 != cs) {
1315 1.1 jtc /* maybe */
1316 1.1 jtc for (i = 0; i < css; i++)
1317 1.1 jtc if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1318 1.1 jtc break; /* no */
1319 1.1 jtc if (i == css)
1320 1.1 jtc break; /* yes */
1321 1.1 jtc }
1322 1.1 jtc
1323 1.1 jtc if (cs2 < top) { /* found one */
1324 1.1 jtc freeset(p, cs);
1325 1.1 jtc cs = cs2;
1326 1.1 jtc }
1327 1.1 jtc
1328 1.1 jtc return((int)(cs - p->g->sets));
1329 1.1 jtc }
1330 1.1 jtc
1331 1.1 jtc /*
1332 1.1 jtc - firstch - return first character in a set (which must have at least one)
1333 1.9 perry == static int firstch(struct parse *p, cset *cs);
1334 1.1 jtc */
1335 1.1 jtc static int /* character; there is no "none" value */
1336 1.1 jtc firstch(p, cs)
1337 1.9 perry struct parse *p;
1338 1.9 perry cset *cs;
1339 1.1 jtc {
1340 1.9 perry int i;
1341 1.14 lukem size_t css;
1342 1.14 lukem
1343 1.14 lukem _DIAGASSERT(p != NULL);
1344 1.14 lukem _DIAGASSERT(cs != NULL);
1345 1.14 lukem
1346 1.14 lukem css = (size_t)p->g->csetsize;
1347 1.1 jtc
1348 1.1 jtc for (i = 0; i < css; i++)
1349 1.1 jtc if (CHIN(cs, i))
1350 1.1 jtc return((char)i);
1351 1.1 jtc assert(never);
1352 1.1 jtc return(0); /* arbitrary */
1353 1.1 jtc }
1354 1.1 jtc
1355 1.1 jtc /*
1356 1.1 jtc - nch - number of characters in a set
1357 1.9 perry == static int nch(struct parse *p, cset *cs);
1358 1.1 jtc */
1359 1.1 jtc static int
1360 1.1 jtc nch(p, cs)
1361 1.9 perry struct parse *p;
1362 1.9 perry cset *cs;
1363 1.1 jtc {
1364 1.9 perry int i;
1365 1.14 lukem size_t css;
1366 1.9 perry int n = 0;
1367 1.1 jtc
1368 1.14 lukem _DIAGASSERT(p != NULL);
1369 1.14 lukem _DIAGASSERT(cs != NULL);
1370 1.14 lukem
1371 1.14 lukem css = (size_t)p->g->csetsize;
1372 1.14 lukem
1373 1.1 jtc for (i = 0; i < css; i++)
1374 1.1 jtc if (CHIN(cs, i))
1375 1.1 jtc n++;
1376 1.1 jtc return(n);
1377 1.1 jtc }
1378 1.1 jtc
1379 1.1 jtc /*
1380 1.1 jtc - mcadd - add a collating element to a cset
1381 1.9 perry == static void mcadd(struct parse *p, cset *cs, \
1382 1.9 perry == char *cp);
1383 1.1 jtc */
1384 1.1 jtc static void
1385 1.1 jtc mcadd(p, cs, cp)
1386 1.9 perry struct parse *p;
1387 1.9 perry cset *cs;
1388 1.10 mycroft const char *cp;
1389 1.1 jtc {
1390 1.14 lukem size_t oldend;
1391 1.14 lukem
1392 1.14 lukem _DIAGASSERT(p != NULL);
1393 1.14 lukem _DIAGASSERT(cs != NULL);
1394 1.14 lukem _DIAGASSERT(cp != NULL);
1395 1.14 lukem
1396 1.14 lukem oldend = cs->smultis;
1397 1.1 jtc
1398 1.1 jtc cs->smultis += strlen(cp) + 1;
1399 1.1 jtc if (cs->multis == NULL)
1400 1.1 jtc cs->multis = malloc(cs->smultis);
1401 1.1 jtc else
1402 1.1 jtc cs->multis = realloc(cs->multis, cs->smultis);
1403 1.1 jtc if (cs->multis == NULL) {
1404 1.1 jtc SETERROR(REG_ESPACE);
1405 1.1 jtc return;
1406 1.1 jtc }
1407 1.1 jtc
1408 1.1 jtc (void) strcpy(cs->multis + oldend - 1, cp);
1409 1.1 jtc cs->multis[cs->smultis - 1] = '\0';
1410 1.1 jtc }
1411 1.1 jtc
1412 1.7 christos #if 0
1413 1.1 jtc /*
1414 1.1 jtc - mcsub - subtract a collating element from a cset
1415 1.9 perry == static void mcsub(cset *cs, char *cp);
1416 1.1 jtc */
1417 1.1 jtc static void
1418 1.1 jtc mcsub(cs, cp)
1419 1.9 perry cset *cs;
1420 1.9 perry char *cp;
1421 1.1 jtc {
1422 1.14 lukem char *fp;
1423 1.14 lukem size_t len;
1424 1.14 lukem
1425 1.14 lukem _DIAGASSERT(cs != NULL);
1426 1.14 lukem _DIAGASSERT(cp != NULL);
1427 1.14 lukem
1428 1.14 lukem fp = mcfind(cs, cp);
1429 1.14 lukem len = strlen(fp);
1430 1.1 jtc
1431 1.1 jtc assert(fp != NULL);
1432 1.1 jtc (void) memmove(fp, fp + len + 1,
1433 1.1 jtc cs->smultis - (fp + len + 1 - cs->multis));
1434 1.1 jtc cs->smultis -= len;
1435 1.1 jtc
1436 1.1 jtc if (cs->smultis == 0) {
1437 1.1 jtc free(cs->multis);
1438 1.1 jtc cs->multis = NULL;
1439 1.1 jtc return;
1440 1.1 jtc }
1441 1.1 jtc
1442 1.1 jtc cs->multis = realloc(cs->multis, cs->smultis);
1443 1.1 jtc assert(cs->multis != NULL);
1444 1.1 jtc }
1445 1.1 jtc
1446 1.1 jtc /*
1447 1.1 jtc - mcin - is a collating element in a cset?
1448 1.9 perry == static int mcin(cset *cs, char *cp);
1449 1.1 jtc */
1450 1.1 jtc static int
1451 1.1 jtc mcin(cs, cp)
1452 1.9 perry cset *cs;
1453 1.9 perry char *cp;
1454 1.1 jtc {
1455 1.14 lukem
1456 1.14 lukem _DIAGASSERT(cs != NULL);
1457 1.14 lukem _DIAGASSERT(cp != NULL);
1458 1.14 lukem
1459 1.1 jtc return(mcfind(cs, cp) != NULL);
1460 1.1 jtc }
1461 1.1 jtc
1462 1.1 jtc /*
1463 1.1 jtc - mcfind - find a collating element in a cset
1464 1.9 perry == static char *mcfind(cset *cs, char *cp);
1465 1.1 jtc */
1466 1.1 jtc static char *
1467 1.1 jtc mcfind(cs, cp)
1468 1.9 perry cset *cs;
1469 1.9 perry char *cp;
1470 1.1 jtc {
1471 1.9 perry char *p;
1472 1.1 jtc
1473 1.14 lukem _DIAGASSERT(cs != NULL);
1474 1.14 lukem _DIAGASSERT(cp != NULL);
1475 1.14 lukem
1476 1.1 jtc if (cs->multis == NULL)
1477 1.1 jtc return(NULL);
1478 1.1 jtc for (p = cs->multis; *p != '\0'; p += strlen(p) + 1)
1479 1.1 jtc if (strcmp(cp, p) == 0)
1480 1.1 jtc return(p);
1481 1.1 jtc return(NULL);
1482 1.1 jtc }
1483 1.7 christos #endif
1484 1.1 jtc
1485 1.1 jtc /*
1486 1.1 jtc - mcinvert - invert the list of collating elements in a cset
1487 1.9 perry == static void mcinvert(struct parse *p, cset *cs);
1488 1.1 jtc *
1489 1.1 jtc * This would have to know the set of possibilities. Implementation
1490 1.1 jtc * is deferred.
1491 1.1 jtc */
1492 1.11 christos /* ARGSUSED */
1493 1.1 jtc static void
1494 1.2 jtc mcinvert(p, cs)
1495 1.9 perry struct parse *p;
1496 1.9 perry cset *cs;
1497 1.1 jtc {
1498 1.14 lukem
1499 1.14 lukem _DIAGASSERT(p != NULL);
1500 1.14 lukem _DIAGASSERT(cs != NULL);
1501 1.14 lukem
1502 1.1 jtc assert(cs->multis == NULL); /* xxx */
1503 1.1 jtc }
1504 1.1 jtc
1505 1.1 jtc /*
1506 1.1 jtc - mccase - add case counterparts of the list of collating elements in a cset
1507 1.9 perry == static void mccase(struct parse *p, cset *cs);
1508 1.1 jtc *
1509 1.1 jtc * This would have to know the set of possibilities. Implementation
1510 1.1 jtc * is deferred.
1511 1.1 jtc */
1512 1.11 christos /* ARGSUSED */
1513 1.1 jtc static void
1514 1.2 jtc mccase(p, cs)
1515 1.9 perry struct parse *p;
1516 1.9 perry cset *cs;
1517 1.1 jtc {
1518 1.14 lukem
1519 1.14 lukem _DIAGASSERT(p != NULL);
1520 1.14 lukem _DIAGASSERT(cs != NULL);
1521 1.14 lukem
1522 1.1 jtc assert(cs->multis == NULL); /* xxx */
1523 1.1 jtc }
1524 1.1 jtc
1525 1.1 jtc /*
1526 1.1 jtc - isinsets - is this character in any sets?
1527 1.9 perry == static int isinsets(struct re_guts *g, int c);
1528 1.1 jtc */
1529 1.1 jtc static int /* predicate */
1530 1.1 jtc isinsets(g, c)
1531 1.9 perry struct re_guts *g;
1532 1.1 jtc int c;
1533 1.1 jtc {
1534 1.9 perry uch *col;
1535 1.9 perry int i;
1536 1.14 lukem int ncols;
1537 1.9 perry unsigned uc = (unsigned char)c;
1538 1.1 jtc
1539 1.14 lukem _DIAGASSERT(g != NULL);
1540 1.14 lukem
1541 1.14 lukem ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1542 1.14 lukem
1543 1.1 jtc for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1544 1.1 jtc if (col[uc] != 0)
1545 1.1 jtc return(1);
1546 1.1 jtc return(0);
1547 1.1 jtc }
1548 1.1 jtc
1549 1.1 jtc /*
1550 1.1 jtc - samesets - are these two characters in exactly the same sets?
1551 1.9 perry == static int samesets(struct re_guts *g, int c1, int c2);
1552 1.1 jtc */
1553 1.1 jtc static int /* predicate */
1554 1.1 jtc samesets(g, c1, c2)
1555 1.9 perry struct re_guts *g;
1556 1.1 jtc int c1;
1557 1.1 jtc int c2;
1558 1.1 jtc {
1559 1.9 perry uch *col;
1560 1.9 perry int i;
1561 1.14 lukem int ncols;
1562 1.9 perry unsigned uc1 = (unsigned char)c1;
1563 1.9 perry unsigned uc2 = (unsigned char)c2;
1564 1.1 jtc
1565 1.14 lukem _DIAGASSERT(g != NULL);
1566 1.14 lukem
1567 1.14 lukem ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1568 1.14 lukem
1569 1.1 jtc for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1570 1.1 jtc if (col[uc1] != col[uc2])
1571 1.1 jtc return(0);
1572 1.1 jtc return(1);
1573 1.1 jtc }
1574 1.1 jtc
1575 1.1 jtc /*
1576 1.1 jtc - categorize - sort out character categories
1577 1.9 perry == static void categorize(struct parse *p, struct re_guts *g);
1578 1.1 jtc */
1579 1.1 jtc static void
1580 1.1 jtc categorize(p, g)
1581 1.1 jtc struct parse *p;
1582 1.9 perry struct re_guts *g;
1583 1.1 jtc {
1584 1.14 lukem cat_t *cats;
1585 1.9 perry int c;
1586 1.9 perry int c2;
1587 1.9 perry cat_t cat;
1588 1.1 jtc
1589 1.14 lukem _DIAGASSERT(p != NULL);
1590 1.14 lukem _DIAGASSERT(g != NULL);
1591 1.14 lukem
1592 1.14 lukem cats = g->categories;
1593 1.14 lukem
1594 1.1 jtc /* avoid making error situations worse */
1595 1.1 jtc if (p->error != 0)
1596 1.1 jtc return;
1597 1.1 jtc
1598 1.1 jtc for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1599 1.1 jtc if (cats[c] == 0 && isinsets(g, c)) {
1600 1.1 jtc cat = g->ncategories++;
1601 1.1 jtc cats[c] = cat;
1602 1.1 jtc for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1603 1.1 jtc if (cats[c2] == 0 && samesets(g, c, c2))
1604 1.1 jtc cats[c2] = cat;
1605 1.1 jtc }
1606 1.1 jtc }
1607 1.1 jtc
1608 1.1 jtc /*
1609 1.1 jtc - dupl - emit a duplicate of a bunch of sops
1610 1.9 perry == static sopno dupl(struct parse *p, sopno start, sopno finish);
1611 1.1 jtc */
1612 1.1 jtc static sopno /* start of duplicate */
1613 1.1 jtc dupl(p, start, finish)
1614 1.9 perry struct parse *p;
1615 1.1 jtc sopno start; /* from here */
1616 1.1 jtc sopno finish; /* to this less one */
1617 1.1 jtc {
1618 1.14 lukem sopno ret;
1619 1.9 perry sopno len = finish - start;
1620 1.1 jtc
1621 1.14 lukem _DIAGASSERT(p != NULL);
1622 1.14 lukem
1623 1.14 lukem ret = HERE();
1624 1.14 lukem
1625 1.1 jtc assert(finish >= start);
1626 1.1 jtc if (len == 0)
1627 1.1 jtc return(ret);
1628 1.1 jtc enlarge(p, p->ssize + len); /* this many unexpected additions */
1629 1.1 jtc assert(p->ssize >= p->slen + len);
1630 1.12 drochner (void)memcpy(p->strip + p->slen, p->strip + start,
1631 1.11 christos (size_t)len * sizeof(sop));
1632 1.1 jtc p->slen += len;
1633 1.1 jtc return(ret);
1634 1.1 jtc }
1635 1.1 jtc
1636 1.1 jtc /*
1637 1.1 jtc - doemit - emit a strip operator
1638 1.9 perry == static void doemit(struct parse *p, sop op, size_t opnd);
1639 1.1 jtc *
1640 1.1 jtc * It might seem better to implement this as a macro with a function as
1641 1.1 jtc * hard-case backup, but it's just too big and messy unless there are
1642 1.1 jtc * some changes to the data structures. Maybe later.
1643 1.1 jtc */
1644 1.1 jtc static void
1645 1.1 jtc doemit(p, op, opnd)
1646 1.9 perry struct parse *p;
1647 1.1 jtc sop op;
1648 1.12 drochner sopno opnd;
1649 1.1 jtc {
1650 1.14 lukem
1651 1.14 lukem _DIAGASSERT(p != NULL);
1652 1.14 lukem
1653 1.1 jtc /* avoid making error situations worse */
1654 1.1 jtc if (p->error != 0)
1655 1.1 jtc return;
1656 1.1 jtc
1657 1.1 jtc /* deal with oversize operands ("can't happen", more or less) */
1658 1.1 jtc assert(opnd < 1<<OPSHIFT);
1659 1.1 jtc
1660 1.1 jtc /* deal with undersized strip */
1661 1.1 jtc if (p->slen >= p->ssize)
1662 1.1 jtc enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1663 1.1 jtc assert(p->slen < p->ssize);
1664 1.1 jtc
1665 1.1 jtc /* finally, it's all reduced to the easy case */
1666 1.1 jtc p->strip[p->slen++] = SOP(op, opnd);
1667 1.1 jtc }
1668 1.1 jtc
1669 1.1 jtc /*
1670 1.1 jtc - doinsert - insert a sop into the strip
1671 1.9 perry == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
1672 1.1 jtc */
1673 1.1 jtc static void
1674 1.1 jtc doinsert(p, op, opnd, pos)
1675 1.9 perry struct parse *p;
1676 1.1 jtc sop op;
1677 1.12 drochner sopno opnd;
1678 1.1 jtc sopno pos;
1679 1.1 jtc {
1680 1.9 perry sopno sn;
1681 1.9 perry sop s;
1682 1.9 perry int i;
1683 1.1 jtc
1684 1.14 lukem _DIAGASSERT(p != NULL);
1685 1.14 lukem
1686 1.1 jtc /* avoid making error situations worse */
1687 1.1 jtc if (p->error != 0)
1688 1.1 jtc return;
1689 1.1 jtc
1690 1.1 jtc sn = HERE();
1691 1.1 jtc EMIT(op, opnd); /* do checks, ensure space */
1692 1.1 jtc assert(HERE() == sn+1);
1693 1.1 jtc s = p->strip[sn];
1694 1.1 jtc
1695 1.1 jtc /* adjust paren pointers */
1696 1.1 jtc assert(pos > 0);
1697 1.1 jtc for (i = 1; i < NPAREN; i++) {
1698 1.1 jtc if (p->pbegin[i] >= pos) {
1699 1.1 jtc p->pbegin[i]++;
1700 1.1 jtc }
1701 1.1 jtc if (p->pend[i] >= pos) {
1702 1.1 jtc p->pend[i]++;
1703 1.1 jtc }
1704 1.1 jtc }
1705 1.1 jtc
1706 1.11 christos memmove(&p->strip[pos+1], &p->strip[pos], (HERE()-pos-1)*sizeof(sop));
1707 1.1 jtc p->strip[pos] = s;
1708 1.1 jtc }
1709 1.1 jtc
1710 1.1 jtc /*
1711 1.1 jtc - dofwd - complete a forward reference
1712 1.9 perry == static void dofwd(struct parse *p, sopno pos, sop value);
1713 1.1 jtc */
1714 1.1 jtc static void
1715 1.1 jtc dofwd(p, pos, value)
1716 1.9 perry struct parse *p;
1717 1.9 perry sopno pos;
1718 1.12 drochner sopno value;
1719 1.1 jtc {
1720 1.14 lukem
1721 1.14 lukem _DIAGASSERT(p != NULL);
1722 1.14 lukem
1723 1.1 jtc /* avoid making error situations worse */
1724 1.1 jtc if (p->error != 0)
1725 1.1 jtc return;
1726 1.1 jtc
1727 1.1 jtc assert(value < 1<<OPSHIFT);
1728 1.1 jtc p->strip[pos] = OP(p->strip[pos]) | value;
1729 1.1 jtc }
1730 1.1 jtc
1731 1.1 jtc /*
1732 1.1 jtc - enlarge - enlarge the strip
1733 1.9 perry == static void enlarge(struct parse *p, sopno size);
1734 1.1 jtc */
1735 1.1 jtc static void
1736 1.1 jtc enlarge(p, size)
1737 1.9 perry struct parse *p;
1738 1.9 perry sopno size;
1739 1.1 jtc {
1740 1.9 perry sop *sp;
1741 1.1 jtc
1742 1.14 lukem _DIAGASSERT(p != NULL);
1743 1.14 lukem
1744 1.1 jtc if (p->ssize >= size)
1745 1.1 jtc return;
1746 1.1 jtc
1747 1.1 jtc sp = (sop *)realloc(p->strip, size*sizeof(sop));
1748 1.1 jtc if (sp == NULL) {
1749 1.1 jtc SETERROR(REG_ESPACE);
1750 1.1 jtc return;
1751 1.1 jtc }
1752 1.1 jtc p->strip = sp;
1753 1.1 jtc p->ssize = size;
1754 1.1 jtc }
1755 1.1 jtc
1756 1.1 jtc /*
1757 1.1 jtc - stripsnug - compact the strip
1758 1.9 perry == static void stripsnug(struct parse *p, struct re_guts *g);
1759 1.1 jtc */
1760 1.1 jtc static void
1761 1.1 jtc stripsnug(p, g)
1762 1.9 perry struct parse *p;
1763 1.9 perry struct re_guts *g;
1764 1.1 jtc {
1765 1.14 lukem
1766 1.14 lukem _DIAGASSERT(p != NULL);
1767 1.14 lukem _DIAGASSERT(g != NULL);
1768 1.14 lukem
1769 1.1 jtc g->nstates = p->slen;
1770 1.11 christos g->strip = realloc(p->strip, p->slen * sizeof(sop));
1771 1.1 jtc if (g->strip == NULL) {
1772 1.1 jtc SETERROR(REG_ESPACE);
1773 1.1 jtc g->strip = p->strip;
1774 1.1 jtc }
1775 1.1 jtc }
1776 1.1 jtc
1777 1.1 jtc /*
1778 1.1 jtc - findmust - fill in must and mlen with longest mandatory literal string
1779 1.9 perry == static void findmust(struct parse *p, struct re_guts *g);
1780 1.1 jtc *
1781 1.1 jtc * This algorithm could do fancy things like analyzing the operands of |
1782 1.1 jtc * for common subsequences. Someday. This code is simple and finds most
1783 1.1 jtc * of the interesting cases.
1784 1.1 jtc *
1785 1.1 jtc * Note that must and mlen got initialized during setup.
1786 1.1 jtc */
1787 1.1 jtc static void
1788 1.1 jtc findmust(p, g)
1789 1.1 jtc struct parse *p;
1790 1.9 perry struct re_guts *g;
1791 1.1 jtc {
1792 1.9 perry sop *scan;
1793 1.7 christos sop *start = NULL;
1794 1.9 perry sop *newstart = NULL;
1795 1.9 perry sopno newlen;
1796 1.9 perry sop s;
1797 1.9 perry char *cp;
1798 1.9 perry sopno i;
1799 1.1 jtc
1800 1.14 lukem _DIAGASSERT(p != NULL);
1801 1.14 lukem _DIAGASSERT(g != NULL);
1802 1.14 lukem
1803 1.1 jtc /* avoid making error situations worse */
1804 1.1 jtc if (p->error != 0)
1805 1.1 jtc return;
1806 1.1 jtc
1807 1.1 jtc /* find the longest OCHAR sequence in strip */
1808 1.1 jtc newlen = 0;
1809 1.1 jtc scan = g->strip + 1;
1810 1.1 jtc do {
1811 1.1 jtc s = *scan++;
1812 1.1 jtc switch (OP(s)) {
1813 1.1 jtc case OCHAR: /* sequence member */
1814 1.1 jtc if (newlen == 0) /* new sequence */
1815 1.1 jtc newstart = scan - 1;
1816 1.1 jtc newlen++;
1817 1.1 jtc break;
1818 1.1 jtc case OPLUS_: /* things that don't break one */
1819 1.1 jtc case OLPAREN:
1820 1.1 jtc case ORPAREN:
1821 1.1 jtc break;
1822 1.1 jtc case OQUEST_: /* things that must be skipped */
1823 1.1 jtc case OCH_:
1824 1.1 jtc scan--;
1825 1.1 jtc do {
1826 1.1 jtc scan += OPND(s);
1827 1.1 jtc s = *scan;
1828 1.1 jtc /* assert() interferes w debug printouts */
1829 1.1 jtc if (OP(s) != O_QUEST && OP(s) != O_CH &&
1830 1.1 jtc OP(s) != OOR2) {
1831 1.1 jtc g->iflags |= BAD;
1832 1.1 jtc return;
1833 1.1 jtc }
1834 1.1 jtc } while (OP(s) != O_QUEST && OP(s) != O_CH);
1835 1.11 christos /* FALLTHROUGH */
1836 1.1 jtc default: /* things that break a sequence */
1837 1.1 jtc if (newlen > g->mlen) { /* ends one */
1838 1.1 jtc start = newstart;
1839 1.1 jtc g->mlen = newlen;
1840 1.1 jtc }
1841 1.1 jtc newlen = 0;
1842 1.1 jtc break;
1843 1.1 jtc }
1844 1.1 jtc } while (OP(s) != OEND);
1845 1.1 jtc
1846 1.1 jtc if (g->mlen == 0) /* there isn't one */
1847 1.1 jtc return;
1848 1.1 jtc
1849 1.1 jtc /* turn it into a character string */
1850 1.1 jtc g->must = malloc((size_t)g->mlen + 1);
1851 1.1 jtc if (g->must == NULL) { /* argh; just forget it */
1852 1.1 jtc g->mlen = 0;
1853 1.1 jtc return;
1854 1.1 jtc }
1855 1.1 jtc cp = g->must;
1856 1.1 jtc scan = start;
1857 1.1 jtc for (i = g->mlen; i > 0; i--) {
1858 1.1 jtc while (OP(s = *scan++) != OCHAR)
1859 1.1 jtc continue;
1860 1.2 jtc assert(cp < g->must + g->mlen);
1861 1.1 jtc *cp++ = (char)OPND(s);
1862 1.1 jtc }
1863 1.2 jtc assert(cp == g->must + g->mlen);
1864 1.1 jtc *cp++ = '\0'; /* just on general principles */
1865 1.1 jtc }
1866 1.1 jtc
1867 1.1 jtc /*
1868 1.1 jtc - pluscount - count + nesting
1869 1.9 perry == static sopno pluscount(struct parse *p, struct re_guts *g);
1870 1.1 jtc */
1871 1.1 jtc static sopno /* nesting depth */
1872 1.1 jtc pluscount(p, g)
1873 1.1 jtc struct parse *p;
1874 1.9 perry struct re_guts *g;
1875 1.1 jtc {
1876 1.9 perry sop *scan;
1877 1.9 perry sop s;
1878 1.9 perry sopno plusnest = 0;
1879 1.9 perry sopno maxnest = 0;
1880 1.14 lukem
1881 1.14 lukem _DIAGASSERT(p != NULL);
1882 1.14 lukem _DIAGASSERT(g != NULL);
1883 1.1 jtc
1884 1.1 jtc if (p->error != 0)
1885 1.1 jtc return(0); /* there may not be an OEND */
1886 1.1 jtc
1887 1.1 jtc scan = g->strip + 1;
1888 1.1 jtc do {
1889 1.1 jtc s = *scan++;
1890 1.1 jtc switch (OP(s)) {
1891 1.1 jtc case OPLUS_:
1892 1.1 jtc plusnest++;
1893 1.1 jtc break;
1894 1.1 jtc case O_PLUS:
1895 1.1 jtc if (plusnest > maxnest)
1896 1.1 jtc maxnest = plusnest;
1897 1.1 jtc plusnest--;
1898 1.1 jtc break;
1899 1.1 jtc }
1900 1.1 jtc } while (OP(s) != OEND);
1901 1.1 jtc if (plusnest != 0)
1902 1.1 jtc g->iflags |= BAD;
1903 1.1 jtc return(maxnest);
1904 1.1 jtc }
1905