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