Bug Summary

File:lib/Support/regcomp.c
Warning:line 1666, column 10
Dereference of null pointer

Annotated Source Code

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