1 /* $NetBSD: regcomp.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Henry Spencer of the University of Toronto.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)regcomp.c 8.4 (Berkeley) 3/19/94
41 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid
[] = "@(#)regcomp.c 8.4 (Berkeley) 3/19/94";
43 #endif /* LIBC_SCCS and not lint */
45 #include <sys/types.h>
60 * parse structure, passed up and down to avoid global variables and
64 RCHAR_T
*next
; /* next character in RE */
65 RCHAR_T
*end
; /* end of string (-> NUL normally) */
66 int error
; /* has an error been seen? */
67 sop
*strip
; /* malloced strip */
68 RCHAR_T
*stripdata
; /* malloced stripdata */
69 sopno ssize
; /* malloced strip size (allocated) */
70 sopno slen
; /* malloced strip length (used) */
71 int ncsalloc
; /* number of csets allocated */
73 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
74 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
75 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
78 /* ========= begin header generated by ./mkh ========= */
83 /* === regcomp.c === */
84 static void p_ere
__P((struct parse
*p
, int stop
, size_t reclimit
));
85 static void p_ere_exp
__P((struct parse
*p
, size_t reclimit
));
86 static void p_str
__P((struct parse
*p
));
87 static void p_bre
__P((struct parse
*p
, int end1
, int end2
, size_t reclimit
));
88 static int p_simp_re
__P((struct parse
*p
, int starordinary
, size_t reclimit
));
89 static int p_count
__P((struct parse
*p
));
90 static void p_bracket
__P((struct parse
*p
));
91 static void p_b_term
__P((struct parse
*p
, cset
*cs
));
92 static void p_b_cclass
__P((struct parse
*p
, cset
*cs
));
93 static void p_b_eclass
__P((struct parse
*p
, cset
*cs
));
94 static char p_b_symbol
__P((struct parse
*p
));
95 static char p_b_coll_elem
__P((struct parse
*p
, int endc
));
96 static char othercase
__P((int ch
));
97 static void bothcases
__P((struct parse
*p
, int ch
));
98 static void ordinary
__P((struct parse
*p
, int ch
));
99 static void nonnewline
__P((struct parse
*p
));
100 static void repeat
__P((struct parse
*p
, sopno start
, int from
, int to
, size_t reclimit
));
101 static int seterr
__P((struct parse
*p
, int e
));
102 static cset
*allocset
__P((struct parse
*p
));
103 static void freeset
__P((struct parse
*p
, cset
*cs
));
104 static int freezeset
__P((struct parse
*p
, cset
*cs
));
105 static int firstch
__P((struct parse
*p
, cset
*cs
));
106 static int nch
__P((struct parse
*p
, cset
*cs
));
107 static void mcadd
__P((struct parse
*p
, cset
*cs
, const char *cp
));
109 static void mcsub
__P((cset
*cs
, char *cp
));
110 static int mcin
__P((cset
*cs
, char *cp
));
111 static char *mcfind
__P((cset
*cs
, char *cp
));
113 static void mcinvert
__P((struct parse
*p
, cset
*cs
));
114 static void mccase
__P((struct parse
*p
, cset
*cs
));
116 static int isinsets
__P((struct re_guts
*g
, int c
));
117 static int samesets
__P((struct re_guts
*g
, int c1
, int c2
));
119 static void categorize
__P((struct parse
*p
, struct re_guts
*g
));
120 static sopno dupl
__P((struct parse
*p
, sopno start
, sopno finish
));
121 static void doemit
__P((struct parse
*p
, sop op
, size_t opnd
));
122 static void doinsert
__P((struct parse
*p
, sop op
, size_t opnd
, sopno pos
));
123 static void dofwd
__P((struct parse
*p
, sopno pos
, sop value
));
124 static int enlarge
__P((struct parse
*p
, sopno size
));
125 static void stripsnug
__P((struct parse
*p
, struct re_guts
*g
));
126 static void findmust
__P((struct parse
*p
, struct re_guts
*g
));
127 static sopno pluscount
__P((struct parse
*p
, struct re_guts
*g
));
132 /* ========= end header generated by ./mkh ========= */
134 static RCHAR_T nuls
[10]; /* place to point scanner in event of error */
137 * macros for use with parse structure
138 * BEWARE: these know that the parse structure is named `p' !!!
140 #define PEEK() (*p->next)
141 #define PEEK2() (*(p->next+1))
142 #define MORE() (p->next < p->end)
143 #define MORE2() (p->next+1 < p->end)
144 #define SEE(c) (MORE() && PEEK() == (c))
145 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
146 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
147 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
148 #define NEXT() (p->next++)
149 #define NEXT2() (p->next += 2)
150 #define NEXTn(n) (p->next += (n))
151 #define GETNEXT() (*p->next++)
152 #define SETERROR(e) seterr(p, (e))
153 #define REQUIRE(co, e) ((co) || SETERROR(e))
154 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
155 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
156 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
157 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
158 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
159 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
160 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
161 #define HERE() (p->slen)
162 #define THERE() (p->slen - 1)
163 #define THERETHERE() (p->slen - 2)
164 #define DROP(n) (p->slen -= (n))
167 static int never
= 0; /* for use in asserts; shuts lint up */
169 #define never 0 /* some <assert.h>s have bugs too */
172 #define MEMLIMIT 0x8000000
174 ((p)->ncsalloc / CHAR_BIT * (p)->g->csetsize + \
175 (p)->ncsalloc * sizeof(cset) + \
176 (p)->ssize * sizeof(sop))
180 - regcomp - interface for parser and compilation
181 = extern int regcomp(regex_t *, const RCHAR_T *, int);
182 = #define REG_BASIC 0000
183 = #define REG_EXTENDED 0001
184 = #define REG_ICASE 0002
185 = #define REG_NOSUB 0004
186 = #define REG_NEWLINE 0010
187 = #define REG_NOSPEC 0020
188 = #define REG_PEND 0040
189 = #define REG_DUMP 0200
191 int /* 0 success, otherwise REG_something */
192 regcomp(regex_t
*preg
, const RCHAR_T
*pattern
, int cflags
)
195 register struct re_guts
*g
;
196 register struct parse
*p
= &pa
;
200 # define GOODFLAGS(f) (f)
202 # define GOODFLAGS(f) ((f)&~REG_DUMP)
205 cflags
= GOODFLAGS(cflags
);
206 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
209 if (cflags
®_PEND
) {
210 if (preg
->re_endp
< pattern
)
212 len
= preg
->re_endp
- pattern
;
214 len
= STRLEN(pattern
);
216 /* do the mallocs early so failure handling is easy */
217 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
218 (NC
-1)*sizeof(cat_t
));
221 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
222 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
223 if (p
->strip
== NULL
) {
227 p
->stripdata
= (RCHAR_T
*)malloc(p
->ssize
* sizeof(RCHAR_T
));
228 if (p
->stripdata
== NULL
) {
229 free((char *)p
->strip
);
237 p
->next
= (RCHAR_T
*)__UNCONST(pattern
); /* convenience; we do not modify it */
238 p
->end
= p
->next
+ len
;
241 for (i
= 0; i
< NPAREN
; i
++) {
257 g
->ncategories
= 1; /* category 0 is "everything else" */
258 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
259 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
265 g
->firststate
= THERE();
266 if (cflags
®_EXTENDED
)
268 else if (cflags
®_NOSPEC
)
271 p_bre(p
, OUT
, OUT
, 0);
273 g
->laststate
= THERE();
275 /* tidy up loose ends and fill things in */
279 g
->nplus
= pluscount(p
, g
);
281 preg
->re_nsub
= g
->nsub
;
283 preg
->re_magic
= MAGIC1
;
285 /* not debugging, so can't rely on the assert() in regexec() */
287 SETERROR(REG_ASSERT
);
290 /* win or lose, we're done */
291 if (p
->error
!= 0) /* lose */
297 - p_ere - ERE parser top level, concatenation and alternation
298 == static void p_ere(register struct parse *p, int stop, size_t reclimit);
301 p_ere(register struct parse
*p
, int stop
, size_t reclimit
)
303 /* character this ERE should end at */
306 register sopno prevback
= 0;
307 register sopno prevfwd
= 0;
309 register int first
= 1; /* is this the first alternative? */
311 if (reclimit
++ > RECLIMIT
|| p
->error
== REG_ESPACE
) {
312 p
->error
= REG_ESPACE
;
317 /* do a bunch of concatenated expressions */
319 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
320 p_ere_exp(p
, reclimit
);
321 (void)REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
324 break; /* NOTE BREAK OUT */
327 INSERT(OCH_
, conc
); /* offset is wrong */
332 ASTERN(OOR1
, prevback
);
334 AHEAD(prevfwd
); /* fix previous offset */
336 EMIT(OOR2
, 0); /* offset is very wrong */
339 if (!first
) { /* tail-end fixups */
341 ASTERN(O_CH
, prevback
);
344 assert(!MORE() || SEE(stop
));
348 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
349 == static void p_ere_exp(register struct parse *p);
352 p_ere_exp(register struct parse
*p
, size_t reclimit
)
358 register sopno subno
;
361 assert(MORE()); /* caller should have ensured this */
367 (void)REQUIRE(MORE(), REG_EPAREN
);
371 p
->pbegin
[subno
] = HERE();
372 EMIT(OLPAREN
, subno
);
374 p_ere(p
, ')', reclimit
);
375 if (subno
< NPAREN
) {
376 p
->pend
[subno
] = HERE();
377 assert(p
->pend
[subno
] != 0);
379 EMIT(ORPAREN
, subno
);
380 (void)MUSTEAT(')', REG_EPAREN
);
382 #ifndef POSIX_MISTAKE
383 case ')': /* happens only if no current unmatched ( */
385 * You may ask, why the ifndef? Because I didn't notice
386 * this until slightly too late for 1003.2, and none of the
387 * other 1003.2 regular-expression reviewers noticed it at
388 * all. So an unmatched ) is legal POSIX, at least until
389 * we can get it fixed.
391 SETERROR(REG_EPAREN
);
396 p
->g
->iflags
|= USEBOL
;
402 p
->g
->iflags
|= USEEOL
;
411 SETERROR(REG_BADRPT
);
414 if (p
->g
->cflags
®_NEWLINE
)
423 (void)REQUIRE(MORE(), REG_EESCAPE
);
427 case '{': /* okay as ordinary except if digit follows */
428 (void)REQUIRE(!MORE() || !ISDIGIT((UCHAR_T
)PEEK()), REG_BADRPT
);
438 /* we call { a repetition if followed by a digit */
439 if (!( c
== '*' || c
== '+' || c
== '?' ||
440 (c
== '{' && MORE2() && ISDIGIT((UCHAR_T
)PEEK2())) ))
441 return; /* no repetition, we're done */
444 (void)REQUIRE(!wascaret
, REG_BADRPT
);
446 case '*': /* implemented as +? */
447 /* this case does not require the (y|) trick, noKLUDGE */
450 INSERT(OQUEST_
, pos
);
451 ASTERN(O_QUEST
, pos
);
458 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
459 INSERT(OCH_
, pos
); /* offset slightly wrong */
460 ASTERN(OOR1
, pos
); /* this one's right */
461 AHEAD(pos
); /* fix the OCH_ */
462 EMIT(OOR2
, 0); /* offset very wrong... */
463 AHEAD(THERE()); /* ...so fix it */
464 ASTERN(O_CH
, THERETHERE());
469 if (ISDIGIT((UCHAR_T
)PEEK())) {
471 (void)REQUIRE(count
<= count2
, REG_BADBR
);
472 } else /* single number with comma */
474 } else /* just a single number */
476 repeat(p
, pos
, count
, count2
, 0);
477 if (!EAT('}')) { /* error heuristics */
478 while (MORE() && PEEK() != '}')
480 (void)REQUIRE(MORE(), REG_EBRACE
);
489 if (!( c
== '*' || c
== '+' || c
== '?' ||
490 (c
== '{' && MORE2() && ISDIGIT((UCHAR_T
)PEEK2())) ) )
492 SETERROR(REG_BADRPT
);
496 - p_str - string (no metacharacters) "parser"
497 == static void p_str(register struct parse *p);
500 p_str(register struct parse
*p
)
502 (void)REQUIRE(MORE(), REG_EMPTY
);
504 ordinary(p
, GETNEXT());
508 - p_bre - BRE parser top level, anchoring and concatenation
509 == static void p_bre(register struct parse *p, register int end1, \
510 == register int end2, size_t reclimit);
511 * Giving end1 as OUT essentially eliminates the end1/end2 check.
513 * This implementation is a bit of a kludge, in that a trailing $ is first
514 * taken as an ordinary character and then revised to be an anchor. The
515 * only undesirable side effect is that '$' gets included as a character
516 * category in such cases. This is fairly harmless; not worth fixing.
517 * The amount of lookahead needed to avoid this kludge is excessive.
520 p_bre(register struct parse
*p
, register int end1
, register int end2
, size_t reclimit
)
522 /* first terminating character */
523 /* second terminating character */
525 register sopno start
;
526 register int first
= 1; /* first subexpression? */
527 register int wasdollar
= 0;
529 if (reclimit
++ > RECLIMIT
|| p
->error
== REG_ESPACE
) {
530 p
->error
= REG_ESPACE
;
538 p
->g
->iflags
|= USEBOL
;
541 while (MORE() && !SEETWO(end1
, end2
)) {
542 wasdollar
= p_simp_re(p
, first
, reclimit
);
545 if (wasdollar
) { /* oops, that was a trailing anchor */
548 p
->g
->iflags
|= USEEOL
;
552 (void)REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
556 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
557 == static int p_simp_re(register struct parse *p, int starordinary, size_t reclimit);
559 static int /* was the simple RE an unbackslashed $? */
560 p_simp_re(register struct parse
*p
, int starordinary
, size_t reclimit
)
562 /* is a leading * an ordinary character? */
569 register sopno subno
;
572 pos
= HERE(); /* repetion op, if any, covers from here */
574 assert(MORE()); /* caller should have ensured this */
578 (void)REQUIRE(MORE(), REG_EESCAPE
);
579 c
= (unsigned char)GETNEXT();
582 SETERROR(REG_BADRPT
);
588 p
->pbegin
[subno
] = HERE();
589 EMIT(OLPAREN
, subno
);
590 /* the MORE here is an error heuristic */
591 if (MORE() && !SEETWO('\\', ')'))
592 p_bre(p
, '\\', ')', reclimit
);
593 if (subno
< NPAREN
) {
594 p
->pend
[subno
] = HERE();
595 assert(p
->pend
[subno
] != 0);
597 EMIT(ORPAREN
, subno
);
598 (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
600 case ')': /* should not get here -- must be user */
602 SETERROR(REG_EPAREN
);
615 if (p
->pend
[i
] != 0) {
616 assert(i
<= p
->g
->nsub
);
618 assert(p
->pbegin
[i
] != 0);
619 assert(p
->strip
[p
->pbegin
[i
]] == OLPAREN
);
620 assert(p
->strip
[p
->pend
[i
]] == ORPAREN
);
621 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
624 SETERROR(REG_ESUBREG
);
634 if (p
->g
->cflags
®_NEWLINE
)
643 (void)REQUIRE(starordinary
, REG_BADRPT
);
651 if (EAT('*')) { /* implemented as +? */
652 /* this case does not require the (y|) trick, noKLUDGE */
655 INSERT(OQUEST_
, pos
);
656 ASTERN(O_QUEST
, pos
);
657 } else if (EATTWO('\\', '{')) {
660 if (MORE() && ISDIGIT((UCHAR_T
)PEEK())) {
662 (void)REQUIRE(count
<= count2
, REG_BADBR
);
663 } else /* single number with comma */
665 } else /* just a single number */
667 repeat(p
, pos
, count
, count2
, reclimit
);
668 if (!EATTWO('\\', '}')) { /* error heuristics */
669 while (MORE() && !SEETWO('\\', '}'))
671 (void)REQUIRE(MORE(), REG_EBRACE
);
674 } else if (!backsl
&& c
== (unsigned char)'$') /* $ (but not \$) ends it */
681 - p_count - parse a repetition count
682 == static int p_count(register struct parse *p);
684 static int /* the value */
685 p_count(register struct parse
*p
)
687 register int count
= 0;
688 register int ndigits
= 0;
690 while (MORE() && ISDIGIT((UCHAR_T
)PEEK()) && count
<= DUPMAX
) {
691 count
= count
*10 + (GETNEXT() - '0');
695 (void)REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
700 - p_bracket - parse a bracketed character list
701 == static void p_bracket(register struct parse *p);
703 * Note a significant property of this code: if the allocset() did SETERROR,
704 * no set operations are done.
707 p_bracket(register struct parse
*p
)
710 register int invert
= 0;
711 static RCHAR_T bow
[] = { '[', ':', '<', ':', ']', ']' };
712 static RCHAR_T eow
[] = { '[', ':', '>', ':', ']', ']' };
718 /* Dept of Truly Sickening Special-Case Kludges */
719 if (p
->next
+ 5 < p
->end
&& MEMCMP(p
->next
, bow
, 6) == 0) {
724 if (p
->next
+ 5 < p
->end
&& MEMCMP(p
->next
, eow
, 6) == 0) {
731 invert
++; /* make note to invert set at end */
736 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
740 (void)MUSTEAT(']', REG_EBRACK
);
742 if (p
->error
!= 0) /* don't mess things up further */
745 if (p
->g
->cflags
®_ICASE
) {
749 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
750 if (CHIN(cs
, i
) && isalpha(i
)) {
755 if (cs
->multis
!= NULL
)
761 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
766 if (p
->g
->cflags
®_NEWLINE
)
768 if (cs
->multis
!= NULL
)
772 assert(cs
->multis
== NULL
); /* xxx */
774 if (nch(p
, cs
) == 1) { /* optimize singleton sets */
775 ordinary(p
, firstch(p
, cs
));
778 EMIT(OANYOF
, freezeset(p
, cs
));
782 - p_b_term - parse one term of a bracketed character list
783 == static void p_b_term(register struct parse *p, register cset *cs);
786 p_b_term(register struct parse
*p
, register cset
*cs
)
789 register char start
, finish
;
792 /* classify what we've got */
793 switch ((MORE()) ? PEEK() : '\0') {
795 c
= (MORE2()) ? PEEK2() : '\0';
798 SETERROR(REG_ERANGE
);
799 return; /* NOTE RETURN */
807 case ':': /* character class */
809 (void)REQUIRE(MORE(), REG_EBRACK
);
811 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
813 (void)REQUIRE(MORE(), REG_EBRACK
);
814 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
816 case '=': /* equivalence class */
818 (void)REQUIRE(MORE(), REG_EBRACK
);
820 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
822 (void)REQUIRE(MORE(), REG_EBRACK
);
823 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
825 default: /* symbol, ordinary character, or range */
826 /* xxx revision needed for multichar stuff */
827 start
= p_b_symbol(p
);
828 if (SEE('-') && MORE2() && PEEK2() != ']') {
834 finish
= p_b_symbol(p
);
837 /* xxx what about signed chars here... */
838 (void)REQUIRE(start
<= finish
, REG_ERANGE
);
839 for (i
= start
; i
<= finish
; i
++)
846 - p_b_cclass - parse a character-class name and deal with it
847 == static void p_b_cclass(register struct parse *p, register cset *cs);
850 p_b_cclass(register struct parse
*p
, register cset
*cs
)
852 register RCHAR_T
*sp
= p
->next
;
853 register struct cclass
*cp
;
855 register const char *u
;
858 while (MORE() && isalpha(PEEK()))
861 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
862 if (STRLEN(cp
->name
) == len
&& !MEMCMP(cp
->name
, sp
, len
))
864 if (cp
->name
== NULL
) {
865 /* oops, didn't find it */
866 SETERROR(REG_ECTYPE
);
871 while ((c
= *u
++) != '\0')
873 for (u
= cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
878 - p_b_eclass - parse an equivalence-class name and deal with it
879 == static void p_b_eclass(register struct parse *p, register cset *cs);
881 * This implementation is incomplete. xxx
884 p_b_eclass(register struct parse
*p
, register cset
*cs
)
888 c
= p_b_coll_elem(p
, '=');
893 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
894 == static char p_b_symbol(register struct parse *p);
896 static char /* value of symbol */
897 p_b_symbol(register struct parse
*p
)
901 (void)REQUIRE(MORE(), REG_EBRACK
);
902 if (!EATTWO('[', '.'))
905 /* collating symbol */
906 value
= p_b_coll_elem(p
, '.');
907 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
912 - p_b_coll_elem - parse a collating-element name and look it up
913 == static char p_b_coll_elem(register struct parse *p, int endc);
915 static char /* value of collating element */
916 p_b_coll_elem(register struct parse
*p
, int endc
)
918 /* name ended by endc,']' */
920 register RCHAR_T
*sp
= p
->next
;
921 register struct cname
*cp
;
924 while (MORE() && !SEETWO(endc
, ']'))
927 SETERROR(REG_EBRACK
);
931 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
932 if (STRLEN(cp
->name
) == len
&& MEMCMP(cp
->name
, sp
, len
))
933 return(cp
->code
); /* known name */
935 return(*sp
); /* single character */
936 SETERROR(REG_ECOLLATE
); /* neither */
941 - othercase - return the case counterpart of an alphabetic
942 == static char othercase(int ch);
944 static char /* if no counterpart, return ch */
950 else if (islower(ch
))
952 else /* peculiar, but could happen */
957 - bothcases - emit a dualcase version of a two-case character
958 == static void bothcases(register struct parse *p, int ch);
960 * Boy, is this implementation ever a kludge...
963 bothcases(register struct parse
*p
, int ch
)
965 register RCHAR_T
*oldnext
= p
->next
;
966 register RCHAR_T
*oldend
= p
->end
;
969 assert(othercase(ch
) != ch
); /* p_bracket() would recurse */
976 assert(p
->next
== bracket
+2);
982 - ordinary - emit an ordinary character
983 == static void ordinary(register struct parse *p, register int ch);
986 ordinary(register struct parse
*p
, register int ch
)
989 register cat_t *cap = p->g->categories;
992 if ((p
->g
->cflags
®_ICASE
) && isalpha(ch
) && othercase(ch
) != ch
)
995 EMIT(OCHAR
, (UCHAR_T
)ch
);
998 cap[ch] = p->g->ncategories++;
1004 - nonnewline - emit REG_NEWLINE version of OANY
1005 == static void nonnewline(register struct parse *p);
1007 * Boy, is this implementation ever a kludge...
1010 nonnewline(register struct parse
*p
)
1012 register RCHAR_T
*oldnext
= p
->next
;
1013 register RCHAR_T
*oldend
= p
->end
;
1023 assert(p
->next
== bracket
+3);
1029 - repeat - generate code for a bounded repetition, recursively if needed
1030 == static void repeat(register struct parse *p, sopno start, int from, int to, size_t reclimit);
1033 repeat(register struct parse
*p
, sopno start
, int from
, int to
, size_t reclimit
)
1035 /* operand from here to end of strip */
1036 /* repeated from this number */
1037 /* to this number of times (maybe INFINITY) */
1039 register sopno finish
;
1042 # define REP(f, t) ((f)*8 + (t))
1043 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1044 register sopno copy
;
1046 if (reclimit
++ > RECLIMIT
)
1047 p
->error
= REG_ESPACE
;
1055 switch (REP(MAP(from
), MAP(to
))) {
1056 case REP(0, 0): /* must be user doing this */
1057 DROP(finish
-start
); /* drop the operand */
1059 case REP(0, 1): /* as x{1,1}? */
1060 case REP(0, N
): /* as x{1,n}? */
1061 case REP(0, INF
): /* as x{1,}? */
1062 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1063 INSERT(OCH_
, start
); /* offset is wrong... */
1064 repeat(p
, start
+1, 1, to
, reclimit
);
1065 ASTERN(OOR1
, start
);
1066 AHEAD(start
); /* ... fix it */
1069 ASTERN(O_CH
, THERETHERE());
1071 case REP(1, 1): /* trivial case */
1074 case REP(1, N
): /* as x?x{1,n-1} */
1075 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1076 INSERT(OCH_
, start
);
1077 ASTERN(OOR1
, start
);
1079 EMIT(OOR2
, 0); /* offset very wrong... */
1080 AHEAD(THERE()); /* ...so fix it */
1081 ASTERN(O_CH
, THERETHERE());
1082 copy
= dupl(p
, start
+1, finish
+1);
1083 assert(copy
== finish
+4);
1084 repeat(p
, copy
, 1, to
-1, reclimit
);
1086 case REP(1, INF
): /* as x+ */
1087 INSERT(OPLUS_
, start
);
1088 ASTERN(O_PLUS
, start
);
1090 case REP(N
, N
): /* as xx{m-1,n-1} */
1091 copy
= dupl(p
, start
, finish
);
1092 repeat(p
, copy
, from
-1, to
-1, reclimit
);
1094 case REP(N
, INF
): /* as xx{n-1,INF} */
1095 copy
= dupl(p
, start
, finish
);
1096 repeat(p
, copy
, from
-1, to
, reclimit
);
1098 default: /* "can't happen" */
1099 SETERROR(REG_ASSERT
); /* just in case */
1105 - seterr - set an error condition
1106 == static int seterr(register struct parse *p, int e);
1108 static int /* useless but makes type checking happy */
1109 seterr(register struct parse
*p
, int e
)
1111 if (p
->error
== 0) /* keep earliest error condition */
1113 p
->next
= nuls
; /* try to bring things to a halt */
1115 return(0); /* make the return value well-defined */
1119 - allocset - allocate a set of characters for []
1120 == static cset *allocset(register struct parse *p);
1123 allocset(register struct parse
*p
)
1125 register int no
= p
->g
->ncsets
++;
1127 register size_t nbytes
;
1129 register size_t css
= (size_t)p
->g
->csetsize
;
1132 if (no
>= p
->ncsalloc
) { /* need another column of space */
1133 p
->ncsalloc
+= CHAR_BIT
;
1135 assert(nc
% CHAR_BIT
== 0);
1136 nbytes
= nc
/ CHAR_BIT
* css
;
1137 if (MEMSIZE(p
) > MEMLIMIT
)
1139 if (p
->g
->sets
== NULL
)
1140 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1142 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1144 if (p
->g
->setbits
== NULL
)
1145 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1147 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1149 /* xxx this isn't right if setbits is now NULL */
1150 for (i
= 0; i
< no
; i
++)
1151 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1153 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1154 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1159 SETERROR(REG_ESPACE
);
1160 /* caller's responsibility not to do set ops */
1165 cs
= &p
->g
->sets
[no
];
1166 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1167 cs
->mask
= 1 << ((no
) % CHAR_BIT
);
1176 - freeset - free a now-unused set
1177 == static void freeset(register struct parse *p, register cset *cs);
1180 freeset(register struct parse
*p
, register cset
*cs
)
1183 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1184 register size_t css
= (size_t)p
->g
->csetsize
;
1186 for (i
= 0; i
< css
; i
++)
1188 if (cs
== top
-1) /* recover only the easy case */
1193 - freezeset - final processing on a set of characters
1194 == static int freezeset(register struct parse *p, register cset *cs);
1196 * The main task here is merging identical sets. This is usually a waste
1197 * of time (although the hash code minimizes the overhead), but can win
1198 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1199 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1202 static int /* set number */
1203 freezeset(register struct parse
*p
, register cset
*cs
)
1205 register uch h
= cs
->hash
;
1207 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1209 register size_t css
= (size_t)p
->g
->csetsize
;
1211 /* look for an earlier one which is the same */
1212 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1213 if (cs2
->hash
== h
&& cs2
!= cs
) {
1215 for (i
= 0; i
< css
; i
++)
1216 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1222 if (cs2
< top
) { /* found one */
1227 return((int)(cs
- p
->g
->sets
));
1231 - firstch - return first character in a set (which must have at least one)
1232 == static int firstch(register struct parse *p, register cset *cs);
1234 static int /* character; there is no "none" value */
1235 firstch(register struct parse
*p
, register cset
*cs
)
1238 register size_t css
= (size_t)p
->g
->csetsize
;
1240 for (i
= 0; i
< css
; i
++)
1244 return(0); /* arbitrary */
1248 - nch - number of characters in a set
1249 == static int nch(register struct parse *p, register cset *cs);
1252 nch(register struct parse
*p
, register cset
*cs
)
1255 register size_t css
= (size_t)p
->g
->csetsize
;
1258 for (i
= 0; i
< css
; i
++)
1265 - mcadd - add a collating element to a cset
1266 == static void mcadd(register struct parse *p, register cset *cs, \
1267 == register char *cp);
1270 mcadd(register struct parse
*p
, register cset
*cs
, register const char *cp
)
1272 register size_t oldend
= cs
->smultis
;
1274 cs
->smultis
+= strlen(cp
) + 1;
1275 if (cs
->multis
== NULL
)
1276 cs
->multis
= malloc(cs
->smultis
);
1278 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1279 if (cs
->multis
== NULL
) {
1280 SETERROR(REG_ESPACE
);
1284 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1285 cs
->multis
[cs
->smultis
- 1] = '\0';
1290 - mcsub - subtract a collating element from a cset
1291 == static void mcsub(register cset *cs, register char *cp);
1294 mcsub(register cset
*cs
, register char *cp
)
1296 register char *fp
= mcfind(cs
, cp
);
1297 register size_t len
= strlen(fp
);
1300 (void) memmove(fp
, fp
+ len
+ 1,
1301 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1304 if (cs
->smultis
== 0) {
1310 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1311 assert(cs
->multis
!= NULL
);
1315 - mcin - is a collating element in a cset?
1316 == static int mcin(register cset *cs, register char *cp);
1319 mcin(register cset
*cs
, register char *cp
)
1321 return(mcfind(cs
, cp
) != NULL
);
1325 - mcfind - find a collating element in a cset
1326 == static char *mcfind(register cset *cs, register char *cp);
1329 mcfind(register cset
*cs
, register char *cp
)
1333 if (cs
->multis
== NULL
)
1335 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1336 if (strcmp(cp
, p
) == 0)
1343 - mcinvert - invert the list of collating elements in a cset
1344 == static void mcinvert(register struct parse *p, register cset *cs);
1346 * This would have to know the set of possibilities. Implementation
1350 mcinvert(register struct parse
*p
, register cset
*cs
)
1352 assert(cs
->multis
== NULL
); /* xxx */
1356 - mccase - add case counterparts of the list of collating elements in a cset
1357 == static void mccase(register struct parse *p, register cset *cs);
1359 * This would have to know the set of possibilities. Implementation
1363 mccase(register struct parse
*p
, register cset
*cs
)
1365 assert(cs
->multis
== NULL
); /* xxx */
1370 - isinsets - is this character in any sets?
1371 == static int isinsets(register struct re_guts *g, int c);
1373 static int /* predicate */
1374 isinsets(register struct re_guts
*g
, int c
)
1378 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1379 register unsigned uc
= (unsigned char)c
;
1381 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1388 - samesets - are these two characters in exactly the same sets?
1389 == static int samesets(register struct re_guts *g, int c1, int c2);
1391 static int /* predicate */
1392 samesets(register struct re_guts
*g
, int c1
, int c2
)
1396 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1397 register unsigned uc1
= (unsigned char)c1
;
1398 register unsigned uc2
= (unsigned char)c2
;
1400 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1401 if (col
[uc1
] != col
[uc2
])
1408 - categorize - sort out character categories
1409 == static void categorize(struct parse *p, register struct re_guts *g);
1412 categorize(struct parse
*p
, register struct re_guts
*g
)
1415 register cat_t
*cats
= g
->categories
;
1420 /* avoid making error situations worse */
1424 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1425 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1426 cat
= g
->ncategories
++;
1428 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1429 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1436 - dupl - emit a duplicate of a bunch of sops
1437 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1439 static sopno
/* start of duplicate */
1440 dupl(register struct parse
*p
, sopno start
, sopno finish
)
1443 /* to this less one */
1445 register sopno ret
= HERE();
1446 register sopno len
= finish
- start
;
1448 assert(finish
>= start
);
1451 if (!enlarge(p
, p
->ssize
+ len
)) /* this many unexpected additions */
1453 assert(p
->ssize
>= p
->slen
+ len
);
1454 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1455 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1456 (void) memcpy((char *)(p
->stripdata
+ p
->slen
),
1457 (char *)(p
->stripdata
+ start
), (size_t)len
*sizeof(RCHAR_T
));
1463 - doemit - emit a strip operator
1464 == static void doemit(register struct parse *p, sop op, size_t opnd);
1466 * It might seem better to implement this as a macro with a function as
1467 * hard-case backup, but it's just too big and messy unless there are
1468 * some changes to the data structures. Maybe later.
1471 doemit(register struct parse
*p
, sop op
, size_t opnd
)
1473 /* avoid making error situations worse */
1477 /* deal with oversize operands ("can't happen", more or less) */
1480 /* deal with undersized strip */
1481 if (p
->slen
>= p
->ssize
)
1482 if (!enlarge(p
, (p
->ssize
+1) / 2 * 3)) /* +50% */
1485 /* finally, it's all reduced to the easy case */
1486 p
->strip
[p
->slen
] = op
;
1487 p
->stripdata
[p
->slen
] = opnd
;
1492 - doinsert - insert a sop into the strip
1493 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1496 doinsert(register struct parse
*p
, sop op
, size_t opnd
, sopno pos
)
1503 /* avoid making error situations worse */
1508 EMIT(op
, opnd
); /* do checks, ensure space */
1509 assert(HERE() == sn
+1);
1511 d
= p
->stripdata
[sn
];
1513 /* adjust paren pointers */
1515 for (i
= 1; i
< NPAREN
; i
++) {
1516 if (p
->pbegin
[i
] >= pos
) {
1519 if (p
->pend
[i
] >= pos
) {
1524 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1525 (HERE()-pos
-1)*sizeof(sop
));
1526 memmove((char *)&p
->stripdata
[pos
+1], (char *)&p
->stripdata
[pos
],
1527 (HERE()-pos
-1)*sizeof(RCHAR_T
));
1529 p
->stripdata
[pos
] = d
;
1533 - dofwd - complete a forward reference
1534 == static void dofwd(register struct parse *p, sopno pos, sop value);
1537 dofwd(register struct parse
*p
, register sopno pos
, sop value
)
1539 /* avoid making error situations worse */
1544 p
->stripdata
[pos
] = value
;
1548 - enlarge - enlarge the strip
1549 == static int enlarge(register struct parse *p, sopno size);
1552 enlarge(register struct parse
*p
, register sopno size
)
1555 register RCHAR_T
*dp
;
1558 if (p
->ssize
>= size
)
1563 if (MEMSIZE(p
) > MEMLIMIT
)
1565 sp
= realloc(p
->strip
, p
->ssize
* sizeof(sop
));
1569 dp
= realloc(p
->stripdata
, p
->ssize
* sizeof(RCHAR_T
));
1573 SETERROR(REG_ESPACE
);
1581 - stripsnug - compact the strip
1582 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1585 stripsnug(register struct parse
*p
, register struct re_guts
*g
)
1587 g
->nstates
= p
->slen
;
1588 g
->strip
= (sop
*)realloc((char *)p
->strip
,
1589 p
->slen
* sizeof(sop
));
1590 if (g
->strip
== NULL
) {
1591 SETERROR(REG_ESPACE
);
1592 g
->strip
= p
->strip
;
1594 g
->stripdata
= (RCHAR_T
*)realloc((char *)p
->stripdata
,
1595 p
->slen
* sizeof(RCHAR_T
));
1596 if (g
->stripdata
== NULL
) {
1597 SETERROR(REG_ESPACE
);
1598 g
->stripdata
= p
->stripdata
;
1603 - findmust - fill in must and mlen with longest mandatory literal string
1604 == static void findmust(register struct parse *p, register struct re_guts *g);
1606 * This algorithm could do fancy things like analyzing the operands of |
1607 * for common subsequences. Someday. This code is simple and finds most
1608 * of the interesting cases.
1610 * Note that must and mlen got initialized during setup.
1613 findmust(struct parse
*p
, register struct re_guts
*g
)
1615 register sop
*scans
;
1616 register RCHAR_T
*scand
;
1618 RCHAR_T
*startd
= NULL
;
1619 register sop
*newstarts
= 0;
1620 register RCHAR_T
*newstartd
= NULL
;
1621 register sopno newlen
;
1624 register RCHAR_T
*cp
;
1627 /* avoid making error situations worse */
1631 /* find the longest OCHAR sequence in strip */
1633 scans
= g
->strip
+ 1;
1634 scand
= g
->stripdata
+ 1;
1639 case OCHAR
: /* sequence member */
1640 if (newlen
== 0) { /* new sequence */
1641 newstarts
= scans
- 1;
1642 newstartd
= scand
- 1;
1646 case OPLUS_
: /* things that don't break one */
1650 case OQUEST_
: /* things that must be skipped */
1659 /* assert() interferes w debug printouts */
1660 if (s
!= O_QUEST
&& s
!= O_CH
&& s
!= OOR2
) {
1664 } while (s
!= O_QUEST
&& s
!= O_CH
);
1666 default: /* things that break a sequence */
1667 if (newlen
> g
->mlen
) { /* ends one */
1675 } while (s
!= OEND
);
1677 if (g
->mlen
== 0) /* there isn't one */
1680 /* turn it into a character string */
1681 g
->must
= malloc(((size_t)g
->mlen
+ 1) * sizeof(RCHAR_T
));
1682 if (g
->must
== NULL
) { /* argh; just forget it */
1689 for (i
= g
->mlen
; i
> 0; i
--) {
1696 assert(cp
< g
->must
+ g
->mlen
);
1699 assert(cp
== g
->must
+ g
->mlen
);
1700 *cp
++ = '\0'; /* just on general principles */
1704 - pluscount - count + nesting
1705 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1707 static sopno
/* nesting depth */
1708 pluscount(struct parse
*p
, register struct re_guts
*g
)
1712 register sopno plusnest
= 0;
1713 register sopno maxnest
= 0;
1716 return(0); /* there may not be an OEND */
1718 scan
= g
->strip
+ 1;
1726 if (plusnest
> maxnest
)
1731 } while (s
!= OEND
);