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