Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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