1 /* $NetBSD: regcomp.c,v 1.5 2014/01/26 21:47:00 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 #include <sys/cdefs.h>
43 #if defined(LIBC_SCCS) && !defined(lint)
44 static char sccsid
[] = "@(#)regcomp.c 8.4 (Berkeley) 3/19/94";
45 #endif /* LIBC_SCCS and not lint */
47 __RCSID("$NetBSD: regcomp.c,v 1.5 2014/01/26 21:47:00 christos Exp $");
50 #include <sys/types.h>
65 * parse structure, passed up and down to avoid global variables and
69 RCHAR_T
*next
; /* next character in RE */
70 RCHAR_T
*end
; /* end of string (-> NUL normally) */
71 int error
; /* has an error been seen? */
72 sop
*strip
; /* malloced strip */
73 RCHAR_T
*stripdata
; /* malloced stripdata */
74 sopno ssize
; /* malloced strip size (allocated) */
75 sopno slen
; /* malloced strip length (used) */
76 int ncsalloc
; /* number of csets allocated */
78 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
79 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
80 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
83 /* ========= begin header generated by ./mkh ========= */
88 /* === regcomp.c === */
89 static void p_ere
__P((struct parse
*p
, int stop
, size_t reclimit
));
90 static void p_ere_exp
__P((struct parse
*p
, size_t reclimit
));
91 static void p_str
__P((struct parse
*p
));
92 static void p_bre
__P((struct parse
*p
, int end1
, int end2
, size_t reclimit
));
93 static int p_simp_re
__P((struct parse
*p
, int starordinary
, size_t reclimit
));
94 static int p_count
__P((struct parse
*p
));
95 static void p_bracket
__P((struct parse
*p
));
96 static void p_b_term
__P((struct parse
*p
, cset
*cs
));
97 static void p_b_cclass
__P((struct parse
*p
, cset
*cs
));
98 static void p_b_eclass
__P((struct parse
*p
, cset
*cs
));
99 static char p_b_symbol
__P((struct parse
*p
));
100 static char p_b_coll_elem
__P((struct parse
*p
, int endc
));
101 static char othercase
__P((int ch
));
102 static void bothcases
__P((struct parse
*p
, int ch
));
103 static void ordinary
__P((struct parse
*p
, int ch
));
104 static void nonnewline
__P((struct parse
*p
));
105 static void repeat
__P((struct parse
*p
, sopno start
, int from
, int to
, size_t reclimit
));
106 static int seterr
__P((struct parse
*p
, int e
));
107 static cset
*allocset
__P((struct parse
*p
));
108 static void freeset
__P((struct parse
*p
, cset
*cs
));
109 static int freezeset
__P((struct parse
*p
, cset
*cs
));
110 static int firstch
__P((struct parse
*p
, cset
*cs
));
111 static int nch
__P((struct parse
*p
, cset
*cs
));
112 static void mcadd
__P((struct parse
*p
, cset
*cs
, const char *cp
));
114 static void mcsub
__P((cset
*cs
, char *cp
));
115 static int mcin
__P((cset
*cs
, char *cp
));
116 static char *mcfind
__P((cset
*cs
, char *cp
));
118 static void mcinvert
__P((struct parse
*p
, cset
*cs
));
119 static void mccase
__P((struct parse
*p
, cset
*cs
));
121 static int isinsets
__P((struct re_guts
*g
, int c
));
122 static int samesets
__P((struct re_guts
*g
, int c1
, int c2
));
124 static void categorize
__P((struct parse
*p
, struct re_guts
*g
));
125 static sopno dupl
__P((struct parse
*p
, sopno start
, sopno finish
));
126 static void doemit
__P((struct parse
*p
, sop op
, size_t opnd
));
127 static void doinsert
__P((struct parse
*p
, sop op
, size_t opnd
, sopno pos
));
128 static void dofwd
__P((struct parse
*p
, sopno pos
, sop value
));
129 static int enlarge
__P((struct parse
*p
, sopno size
));
130 static void stripsnug
__P((struct parse
*p
, struct re_guts
*g
));
131 static void findmust
__P((struct parse
*p
, struct re_guts
*g
));
132 static sopno pluscount
__P((struct parse
*p
, struct re_guts
*g
));
137 /* ========= end header generated by ./mkh ========= */
139 static RCHAR_T nuls
[10]; /* place to point scanner in event of error */
142 * macros for use with parse structure
143 * BEWARE: these know that the parse structure is named `p' !!!
145 #define PEEK() (*p->next)
146 #define PEEK2() (*(p->next+1))
147 #define MORE() (p->next < p->end)
148 #define MORE2() (p->next+1 < p->end)
149 #define SEE(c) (MORE() && PEEK() == (c))
150 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
151 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
152 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
153 #define NEXT() (p->next++)
154 #define NEXT2() (p->next += 2)
155 #define NEXTn(n) (p->next += (n))
156 #define GETNEXT() (*p->next++)
157 #define SETERROR(e) seterr(p, (e))
158 #define REQUIRE(co, e) ((co) || SETERROR(e))
159 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
160 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
161 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
162 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
163 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
164 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
165 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
166 #define HERE() (p->slen)
167 #define THERE() (p->slen - 1)
168 #define THERETHERE() (p->slen - 2)
169 #define DROP(n) (p->slen -= (n))
172 static int never
= 0; /* for use in asserts; shuts lint up */
174 #define never 0 /* some <assert.h>s have bugs too */
177 #define MEMLIMIT 0x8000000
179 ((p)->ncsalloc / CHAR_BIT * (p)->g->csetsize + \
180 (p)->ncsalloc * sizeof(cset) + \
181 (p)->ssize * sizeof(sop))
185 - regcomp - interface for parser and compilation
186 = extern int regcomp(regex_t *, const RCHAR_T *, int);
187 = #define REG_BASIC 0000
188 = #define REG_EXTENDED 0001
189 = #define REG_ICASE 0002
190 = #define REG_NOSUB 0004
191 = #define REG_NEWLINE 0010
192 = #define REG_NOSPEC 0020
193 = #define REG_PEND 0040
194 = #define REG_DUMP 0200
196 int /* 0 success, otherwise REG_something */
197 regcomp(regex_t
*preg
, const RCHAR_T
*pattern
, int cflags
)
201 struct parse
*p
= &pa
;
205 # define GOODFLAGS(f) (f)
207 # define GOODFLAGS(f) ((f)&~REG_DUMP)
210 cflags
= GOODFLAGS(cflags
);
211 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
214 if (cflags
®_PEND
) {
215 if (preg
->re_endp
< pattern
)
217 len
= preg
->re_endp
- pattern
;
219 len
= STRLEN(pattern
);
221 /* do the mallocs early so failure handling is easy */
222 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
223 (NC
-1)*sizeof(cat_t
));
226 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
227 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
228 if (p
->strip
== NULL
) {
232 p
->stripdata
= (RCHAR_T
*)malloc(p
->ssize
* sizeof(RCHAR_T
));
233 if (p
->stripdata
== NULL
) {
234 free((char *)p
->strip
);
242 p
->next
= (RCHAR_T
*)__UNCONST(pattern
); /* convenience; we do not modify it */
243 p
->end
= p
->next
+ len
;
246 for (i
= 0; i
< NPAREN
; i
++) {
262 g
->ncategories
= 1; /* category 0 is "everything else" */
263 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
264 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
270 g
->firststate
= THERE();
271 if (cflags
®_EXTENDED
)
273 else if (cflags
®_NOSPEC
)
276 p_bre(p
, OUT
, OUT
, 0);
278 g
->laststate
= THERE();
280 /* tidy up loose ends and fill things in */
284 g
->nplus
= pluscount(p
, g
);
286 preg
->re_nsub
= g
->nsub
;
288 preg
->re_magic
= MAGIC1
;
290 /* not debugging, so can't rely on the assert() in regexec() */
292 SETERROR(REG_ASSERT
);
295 /* win or lose, we're done */
296 if (p
->error
!= 0) /* lose */
302 - p_ere - ERE parser top level, concatenation and alternation
303 == static void p_ere(struct parse *p, int stop, size_t reclimit);
306 p_ere(struct parse
*p
, int stop
, size_t reclimit
)
308 /* character this ERE should end at */
314 int first
= 1; /* is this the first alternative? */
316 if (reclimit
++ > RECLIMIT
|| p
->error
== REG_ESPACE
) {
317 p
->error
= REG_ESPACE
;
322 /* do a bunch of concatenated expressions */
324 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
325 p_ere_exp(p
, reclimit
);
326 (void)REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
329 break; /* NOTE BREAK OUT */
332 INSERT(OCH_
, conc
); /* offset is wrong */
337 ASTERN(OOR1
, prevback
);
339 AHEAD(prevfwd
); /* fix previous offset */
341 EMIT(OOR2
, 0); /* offset is very wrong */
344 if (!first
) { /* tail-end fixups */
346 ASTERN(O_CH
, prevback
);
349 assert(!MORE() || SEE(stop
));
353 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
354 == static void p_ere_exp(struct parse *p);
357 p_ere_exp(struct parse
*p
, size_t reclimit
)
366 assert(MORE()); /* caller should have ensured this */
372 (void)REQUIRE(MORE(), REG_EPAREN
);
376 p
->pbegin
[subno
] = HERE();
377 EMIT(OLPAREN
, subno
);
379 p_ere(p
, ')', reclimit
);
380 if (subno
< NPAREN
) {
381 p
->pend
[subno
] = HERE();
382 assert(p
->pend
[subno
] != 0);
384 EMIT(ORPAREN
, subno
);
385 (void)MUSTEAT(')', REG_EPAREN
);
387 #ifndef POSIX_MISTAKE
388 case ')': /* happens only if no current unmatched ( */
390 * You may ask, why the ifndef? Because I didn't notice
391 * this until slightly too late for 1003.2, and none of the
392 * other 1003.2 regular-expression reviewers noticed it at
393 * all. So an unmatched ) is legal POSIX, at least until
394 * we can get it fixed.
396 SETERROR(REG_EPAREN
);
401 p
->g
->iflags
|= USEBOL
;
407 p
->g
->iflags
|= USEEOL
;
416 SETERROR(REG_BADRPT
);
419 if (p
->g
->cflags
®_NEWLINE
)
428 (void)REQUIRE(MORE(), REG_EESCAPE
);
432 case '{': /* okay as ordinary except if digit follows */
433 (void)REQUIRE(!MORE() || !ISDIGIT((UCHAR_T
)PEEK()), REG_BADRPT
);
443 /* we call { a repetition if followed by a digit */
444 if (!( c
== '*' || c
== '+' || c
== '?' ||
445 (c
== '{' && MORE2() && ISDIGIT((UCHAR_T
)PEEK2())) ))
446 return; /* no repetition, we're done */
449 (void)REQUIRE(!wascaret
, REG_BADRPT
);
451 case '*': /* implemented as +? */
452 /* this case does not require the (y|) trick, noKLUDGE */
455 INSERT(OQUEST_
, pos
);
456 ASTERN(O_QUEST
, pos
);
463 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
464 INSERT(OCH_
, pos
); /* offset slightly wrong */
465 ASTERN(OOR1
, pos
); /* this one's right */
466 AHEAD(pos
); /* fix the OCH_ */
467 EMIT(OOR2
, 0); /* offset very wrong... */
468 AHEAD(THERE()); /* ...so fix it */
469 ASTERN(O_CH
, THERETHERE());
474 if (ISDIGIT((UCHAR_T
)PEEK())) {
476 (void)REQUIRE(count
<= count2
, REG_BADBR
);
477 } else /* single number with comma */
479 } else /* just a single number */
481 repeat(p
, pos
, count
, count2
, 0);
482 if (!EAT('}')) { /* error heuristics */
483 while (MORE() && PEEK() != '}')
485 (void)REQUIRE(MORE(), REG_EBRACE
);
494 if (!( c
== '*' || c
== '+' || c
== '?' ||
495 (c
== '{' && MORE2() && ISDIGIT((UCHAR_T
)PEEK2())) ) )
497 SETERROR(REG_BADRPT
);
501 - p_str - string (no metacharacters) "parser"
502 == static void p_str(struct parse *p);
505 p_str(struct parse
*p
)
507 (void)REQUIRE(MORE(), REG_EMPTY
);
509 ordinary(p
, GETNEXT());
513 - p_bre - BRE parser top level, anchoring and concatenation
514 == static void p_bre(struct parse *p, int end1, \
515 == int end2, size_t reclimit);
516 * Giving end1 as OUT essentially eliminates the end1/end2 check.
518 * This implementation is a bit of a kludge, in that a trailing $ is first
519 * taken as an ordinary character and then revised to be an anchor. The
520 * only undesirable side effect is that '$' gets included as a character
521 * category in such cases. This is fairly harmless; not worth fixing.
522 * The amount of lookahead needed to avoid this kludge is excessive.
525 p_bre(struct parse
*p
, int end1
, int end2
, size_t reclimit
)
527 /* first terminating character */
528 /* second terminating character */
531 int first
= 1; /* first subexpression? */
534 if (reclimit
++ > RECLIMIT
|| p
->error
== REG_ESPACE
) {
535 p
->error
= REG_ESPACE
;
543 p
->g
->iflags
|= USEBOL
;
546 while (MORE() && !SEETWO(end1
, end2
)) {
547 wasdollar
= p_simp_re(p
, first
, reclimit
);
550 if (wasdollar
) { /* oops, that was a trailing anchor */
553 p
->g
->iflags
|= USEEOL
;
557 (void)REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
561 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
562 == static int p_simp_re(struct parse *p, int starordinary, size_t reclimit);
564 static int /* was the simple RE an unbackslashed $? */
565 p_simp_re(struct parse
*p
, int starordinary
, size_t reclimit
)
567 /* is a leading * an ordinary character? */
577 pos
= HERE(); /* repetion op, if any, covers from here */
579 assert(MORE()); /* caller should have ensured this */
583 (void)REQUIRE(MORE(), REG_EESCAPE
);
584 c
= (unsigned char)GETNEXT();
587 SETERROR(REG_BADRPT
);
593 p
->pbegin
[subno
] = HERE();
594 EMIT(OLPAREN
, subno
);
595 /* the MORE here is an error heuristic */
596 if (MORE() && !SEETWO('\\', ')'))
597 p_bre(p
, '\\', ')', reclimit
);
598 if (subno
< NPAREN
) {
599 p
->pend
[subno
] = HERE();
600 assert(p
->pend
[subno
] != 0);
602 EMIT(ORPAREN
, subno
);
603 (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
605 case ')': /* should not get here -- must be user */
607 SETERROR(REG_EPAREN
);
620 if (p
->pend
[i
] != 0) {
621 assert(i
<= p
->g
->nsub
);
623 assert(p
->pbegin
[i
] != 0);
624 assert(p
->strip
[p
->pbegin
[i
]] == OLPAREN
);
625 assert(p
->strip
[p
->pend
[i
]] == ORPAREN
);
626 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
629 SETERROR(REG_ESUBREG
);
639 if (p
->g
->cflags
®_NEWLINE
)
648 (void)REQUIRE(starordinary
, REG_BADRPT
);
656 if (EAT('*')) { /* implemented as +? */
657 /* this case does not require the (y|) trick, noKLUDGE */
660 INSERT(OQUEST_
, pos
);
661 ASTERN(O_QUEST
, pos
);
662 } else if (EATTWO('\\', '{')) {
665 if (MORE() && ISDIGIT((UCHAR_T
)PEEK())) {
667 (void)REQUIRE(count
<= count2
, REG_BADBR
);
668 } else /* single number with comma */
670 } else /* just a single number */
672 repeat(p
, pos
, count
, count2
, reclimit
);
673 if (!EATTWO('\\', '}')) { /* error heuristics */
674 while (MORE() && !SEETWO('\\', '}'))
676 (void)REQUIRE(MORE(), REG_EBRACE
);
679 } else if (!backsl
&& c
== (unsigned char)'$') /* $ (but not \$) ends it */
686 - p_count - parse a repetition count
687 == static int p_count(struct parse *p);
689 static int /* the value */
690 p_count(struct parse
*p
)
695 while (MORE() && ISDIGIT((UCHAR_T
)PEEK()) && count
<= DUPMAX
) {
696 count
= count
*10 + (GETNEXT() - '0');
700 (void)REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
705 - p_bracket - parse a bracketed character list
706 == static void p_bracket(struct parse *p);
708 * Note a significant property of this code: if the allocset() did SETERROR,
709 * no set operations are done.
712 p_bracket(struct parse
*p
)
716 static RCHAR_T bow
[] = { '[', ':', '<', ':', ']', ']' };
717 static RCHAR_T eow
[] = { '[', ':', '>', ':', ']', ']' };
723 /* Dept of Truly Sickening Special-Case Kludges */
724 if (p
->next
+ 5 < p
->end
&& MEMCMP(p
->next
, bow
, 6) == 0) {
729 if (p
->next
+ 5 < p
->end
&& MEMCMP(p
->next
, eow
, 6) == 0) {
736 invert
++; /* make note to invert set at end */
741 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
745 (void)MUSTEAT(']', REG_EBRACK
);
747 if (p
->error
!= 0) /* don't mess things up further */
750 if (p
->g
->cflags
®_ICASE
) {
754 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
755 if (CHIN(cs
, i
) && isalpha(i
)) {
760 if (cs
->multis
!= NULL
)
766 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
771 if (p
->g
->cflags
®_NEWLINE
)
773 if (cs
->multis
!= NULL
)
777 assert(cs
->multis
== NULL
); /* xxx */
779 if (nch(p
, cs
) == 1) { /* optimize singleton sets */
780 ordinary(p
, firstch(p
, cs
));
783 EMIT(OANYOF
, freezeset(p
, cs
));
787 - p_b_term - parse one term of a bracketed character list
788 == static void p_b_term(struct parse *p, cset *cs);
791 p_b_term(struct parse
*p
, cset
*cs
)
797 /* classify what we've got */
798 switch ((MORE()) ? PEEK() : '\0') {
800 c
= (MORE2()) ? PEEK2() : '\0';
803 SETERROR(REG_ERANGE
);
804 return; /* NOTE RETURN */
812 case ':': /* character class */
814 (void)REQUIRE(MORE(), REG_EBRACK
);
816 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
818 (void)REQUIRE(MORE(), REG_EBRACK
);
819 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
821 case '=': /* equivalence class */
823 (void)REQUIRE(MORE(), REG_EBRACK
);
825 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
827 (void)REQUIRE(MORE(), REG_EBRACK
);
828 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
830 default: /* symbol, ordinary character, or range */
831 /* xxx revision needed for multichar stuff */
832 start
= p_b_symbol(p
);
833 if (SEE('-') && MORE2() && PEEK2() != ']') {
839 finish
= p_b_symbol(p
);
842 /* xxx what about signed chars here... */
843 (void)REQUIRE(start
<= finish
, REG_ERANGE
);
844 for (i
= start
; i
<= finish
; i
++)
851 - p_b_cclass - parse a character-class name and deal with it
852 == static void p_b_cclass(struct parse *p, cset *cs);
855 p_b_cclass(struct parse
*p
, cset
*cs
)
857 RCHAR_T
*sp
= p
->next
;
863 while (MORE() && isalpha(PEEK()))
866 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
867 if (STRLEN(cp
->name
) == len
&& !MEMCMP(cp
->name
, sp
, len
))
869 if (cp
->name
== NULL
) {
870 /* oops, didn't find it */
871 SETERROR(REG_ECTYPE
);
876 while ((c
= *u
++) != '\0')
878 for (u
= cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
883 - p_b_eclass - parse an equivalence-class name and deal with it
884 == static void p_b_eclass(struct parse *p, cset *cs);
886 * This implementation is incomplete. xxx
889 p_b_eclass(struct parse
*p
, cset
*cs
)
893 c
= p_b_coll_elem(p
, '=');
898 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
899 == static char p_b_symbol(struct parse *p);
901 static char /* value of symbol */
902 p_b_symbol(struct parse
*p
)
906 (void)REQUIRE(MORE(), REG_EBRACK
);
907 if (!EATTWO('[', '.'))
910 /* collating symbol */
911 value
= p_b_coll_elem(p
, '.');
912 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
917 - p_b_coll_elem - parse a collating-element name and look it up
918 == static char p_b_coll_elem(struct parse *p, int endc);
920 static char /* value of collating element */
921 p_b_coll_elem(struct parse
*p
, int endc
)
923 /* name ended by endc,']' */
925 RCHAR_T
*sp
= p
->next
;
929 while (MORE() && !SEETWO(endc
, ']'))
932 SETERROR(REG_EBRACK
);
936 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
937 if (STRLEN(cp
->name
) == len
&& MEMCMP(cp
->name
, sp
, len
))
938 return(cp
->code
); /* known name */
940 return(*sp
); /* single character */
941 SETERROR(REG_ECOLLATE
); /* neither */
946 - othercase - return the case counterpart of an alphabetic
947 == static char othercase(int ch);
949 static char /* if no counterpart, return ch */
955 else if (islower(ch
))
957 else /* peculiar, but could happen */
962 - bothcases - emit a dualcase version of a two-case character
963 == static void bothcases(struct parse *p, int ch);
965 * Boy, is this implementation ever a kludge...
968 bothcases(struct parse
*p
, int ch
)
970 RCHAR_T
*oldnext
= p
->next
;
971 RCHAR_T
*oldend
= p
->end
;
974 assert(othercase(ch
) != ch
); /* p_bracket() would recurse */
981 assert(p
->next
== bracket
+2);
987 - ordinary - emit an ordinary character
988 == static void ordinary(struct parse *p, int ch);
991 ordinary(struct parse
*p
, int ch
)
994 cat_t *cap = p->g->categories;
997 if ((p
->g
->cflags
®_ICASE
) && isalpha(ch
) && othercase(ch
) != ch
)
1000 EMIT(OCHAR
, (UCHAR_T
)ch
);
1003 cap[ch] = p->g->ncategories++;
1009 - nonnewline - emit REG_NEWLINE version of OANY
1010 == static void nonnewline(struct parse *p);
1012 * Boy, is this implementation ever a kludge...
1015 nonnewline(struct parse
*p
)
1017 RCHAR_T
*oldnext
= p
->next
;
1018 RCHAR_T
*oldend
= p
->end
;
1028 assert(p
->next
== bracket
+3);
1034 - repeat - generate code for a bounded repetition, recursively if needed
1035 == static void repeat(struct parse *p, sopno start, int from, int to, size_t reclimit);
1038 repeat(struct parse
*p
, sopno start
, int from
, int to
, size_t reclimit
)
1040 /* operand from here to end of strip */
1041 /* repeated from this number */
1042 /* to this number of times (maybe INFINITY) */
1047 # define REP(f, t) ((f)*8 + (t))
1048 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1051 if (reclimit
++ > RECLIMIT
)
1052 p
->error
= REG_ESPACE
;
1060 switch (REP(MAP(from
), MAP(to
))) {
1061 case REP(0, 0): /* must be user doing this */
1062 DROP(finish
-start
); /* drop the operand */
1064 case REP(0, 1): /* as x{1,1}? */
1065 case REP(0, N
): /* as x{1,n}? */
1066 case REP(0, INF
): /* as x{1,}? */
1067 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1068 INSERT(OCH_
, start
); /* offset is wrong... */
1069 repeat(p
, start
+1, 1, to
, reclimit
);
1070 ASTERN(OOR1
, start
);
1071 AHEAD(start
); /* ... fix it */
1074 ASTERN(O_CH
, THERETHERE());
1076 case REP(1, 1): /* trivial case */
1079 case REP(1, N
): /* as x?x{1,n-1} */
1080 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1081 INSERT(OCH_
, start
);
1082 ASTERN(OOR1
, start
);
1084 EMIT(OOR2
, 0); /* offset very wrong... */
1085 AHEAD(THERE()); /* ...so fix it */
1086 ASTERN(O_CH
, THERETHERE());
1087 copy
= dupl(p
, start
+1, finish
+1);
1088 assert(copy
== finish
+4);
1089 repeat(p
, copy
, 1, to
-1, reclimit
);
1091 case REP(1, INF
): /* as x+ */
1092 INSERT(OPLUS_
, start
);
1093 ASTERN(O_PLUS
, start
);
1095 case REP(N
, N
): /* as xx{m-1,n-1} */
1096 copy
= dupl(p
, start
, finish
);
1097 repeat(p
, copy
, from
-1, to
-1, reclimit
);
1099 case REP(N
, INF
): /* as xx{n-1,INF} */
1100 copy
= dupl(p
, start
, finish
);
1101 repeat(p
, copy
, from
-1, to
, reclimit
);
1103 default: /* "can't happen" */
1104 SETERROR(REG_ASSERT
); /* just in case */
1110 - seterr - set an error condition
1111 == static int seterr(struct parse *p, int e);
1113 static int /* useless but makes type checking happy */
1114 seterr(struct parse
*p
, int e
)
1116 if (p
->error
== 0) /* keep earliest error condition */
1118 p
->next
= nuls
; /* try to bring things to a halt */
1120 return(0); /* make the return value well-defined */
1124 - allocset - allocate a set of characters for []
1125 == static cset *allocset(struct parse *p);
1128 allocset(struct parse
*p
)
1130 int no
= p
->g
->ncsets
++;
1134 size_t css
= (size_t)p
->g
->csetsize
;
1137 if (no
>= p
->ncsalloc
) { /* need another column of space */
1138 p
->ncsalloc
+= CHAR_BIT
;
1140 assert(nc
% CHAR_BIT
== 0);
1141 nbytes
= nc
/ CHAR_BIT
* css
;
1142 if (MEMSIZE(p
) > MEMLIMIT
)
1144 if (p
->g
->sets
== NULL
)
1145 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1147 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1149 if (p
->g
->setbits
== NULL
)
1150 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1152 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1154 /* xxx this isn't right if setbits is now NULL */
1155 for (i
= 0; i
< no
; i
++)
1156 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1158 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1159 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1164 SETERROR(REG_ESPACE
);
1165 /* caller's responsibility not to do set ops */
1170 cs
= &p
->g
->sets
[no
];
1171 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1172 cs
->mask
= 1 << ((no
) % CHAR_BIT
);
1181 - freeset - free a now-unused set
1182 == static void freeset(struct parse *p, cset *cs);
1185 freeset(struct parse
*p
, cset
*cs
)
1188 cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1189 size_t css
= (size_t)p
->g
->csetsize
;
1191 for (i
= 0; i
< css
; i
++)
1193 if (cs
== top
-1) /* recover only the easy case */
1198 - freezeset - final processing on a set of characters
1199 == static int freezeset(struct parse *p, cset *cs);
1201 * The main task here is merging identical sets. This is usually a waste
1202 * of time (although the hash code minimizes the overhead), but can win
1203 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1204 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1207 static int /* set number */
1208 freezeset(struct parse
*p
, cset
*cs
)
1212 cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1214 size_t css
= (size_t)p
->g
->csetsize
;
1216 /* look for an earlier one which is the same */
1217 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1218 if (cs2
->hash
== h
&& cs2
!= cs
) {
1220 for (i
= 0; i
< css
; i
++)
1221 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1227 if (cs2
< top
) { /* found one */
1232 return((int)(cs
- p
->g
->sets
));
1236 - firstch - return first character in a set (which must have at least one)
1237 == static int firstch(struct parse *p, cset *cs);
1239 static int /* character; there is no "none" value */
1240 firstch(struct parse
*p
, cset
*cs
)
1243 size_t css
= (size_t)p
->g
->csetsize
;
1245 for (i
= 0; i
< css
; i
++)
1249 return(0); /* arbitrary */
1253 - nch - number of characters in a set
1254 == static int nch(struct parse *p, cset *cs);
1257 nch(struct parse
*p
, cset
*cs
)
1260 size_t css
= (size_t)p
->g
->csetsize
;
1263 for (i
= 0; i
< css
; i
++)
1270 - mcadd - add a collating element to a cset
1271 == static void mcadd(struct parse *p, cset *cs, \
1275 mcadd(struct parse
*p
, cset
*cs
, const char *cp
)
1277 size_t oldend
= cs
->smultis
;
1279 cs
->smultis
+= strlen(cp
) + 1;
1280 if (cs
->multis
== NULL
)
1281 cs
->multis
= malloc(cs
->smultis
);
1283 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1284 if (cs
->multis
== NULL
) {
1285 SETERROR(REG_ESPACE
);
1289 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1290 cs
->multis
[cs
->smultis
- 1] = '\0';
1295 - mcsub - subtract a collating element from a cset
1296 == static void mcsub(cset *cs, char *cp);
1299 mcsub(cset
*cs
, char *cp
)
1301 char *fp
= mcfind(cs
, cp
);
1302 size_t len
= strlen(fp
);
1305 (void) memmove(fp
, fp
+ len
+ 1,
1306 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1309 if (cs
->smultis
== 0) {
1315 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1316 assert(cs
->multis
!= NULL
);
1320 - mcin - is a collating element in a cset?
1321 == static int mcin(cset *cs, char *cp);
1324 mcin(cset
*cs
, char *cp
)
1326 return(mcfind(cs
, cp
) != NULL
);
1330 - mcfind - find a collating element in a cset
1331 == static char *mcfind(cset *cs, char *cp);
1334 mcfind(cset
*cs
, char *cp
)
1338 if (cs
->multis
== NULL
)
1340 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1341 if (strcmp(cp
, p
) == 0)
1348 - mcinvert - invert the list of collating elements in a cset
1349 == static void mcinvert(struct parse *p, cset *cs);
1351 * This would have to know the set of possibilities. Implementation
1355 mcinvert(struct parse
*p
, cset
*cs
)
1357 assert(cs
->multis
== NULL
); /* xxx */
1361 - mccase - add case counterparts of the list of collating elements in a cset
1362 == static void mccase(struct parse *p, cset *cs);
1364 * This would have to know the set of possibilities. Implementation
1368 mccase(struct parse
*p
, cset
*cs
)
1370 assert(cs
->multis
== NULL
); /* xxx */
1375 - isinsets - is this character in any sets?
1376 == static int isinsets(struct re_guts *g, int c);
1378 static int /* predicate */
1379 isinsets(struct re_guts
*g
, int c
)
1383 int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1384 unsigned uc
= (unsigned char)c
;
1386 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1393 - samesets - are these two characters in exactly the same sets?
1394 == static int samesets(struct re_guts *g, int c1, int c2);
1396 static int /* predicate */
1397 samesets(struct re_guts
*g
, int c1
, int c2
)
1401 int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1402 unsigned uc1
= (unsigned char)c1
;
1403 unsigned uc2
= (unsigned char)c2
;
1405 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1406 if (col
[uc1
] != col
[uc2
])
1413 - categorize - sort out character categories
1414 == static void categorize(struct parse *p, struct re_guts *g);
1417 categorize(struct parse
*p
, struct re_guts
*g
)
1420 cat_t
*cats
= g
->categories
;
1425 /* avoid making error situations worse */
1429 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1430 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1431 cat
= g
->ncategories
++;
1433 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1434 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1441 - dupl - emit a duplicate of a bunch of sops
1442 == static sopno dupl(struct parse *p, sopno start, sopno finish);
1444 static sopno
/* start of duplicate */
1445 dupl(struct parse
*p
, sopno start
, sopno finish
)
1448 /* to this less one */
1451 sopno len
= finish
- start
;
1453 assert(finish
>= start
);
1456 if (!enlarge(p
, p
->ssize
+ len
)) /* this many unexpected additions */
1458 assert(p
->ssize
>= p
->slen
+ len
);
1459 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1460 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1461 (void) memcpy((char *)(p
->stripdata
+ p
->slen
),
1462 (char *)(p
->stripdata
+ start
), (size_t)len
*sizeof(RCHAR_T
));
1468 - doemit - emit a strip operator
1469 == static void doemit(struct parse *p, sop op, size_t opnd);
1471 * It might seem better to implement this as a macro with a function as
1472 * hard-case backup, but it's just too big and messy unless there are
1473 * some changes to the data structures. Maybe later.
1476 doemit(struct parse
*p
, sop op
, size_t opnd
)
1478 /* avoid making error situations worse */
1482 /* deal with oversize operands ("can't happen", more or less) */
1485 /* deal with undersized strip */
1486 if (p
->slen
>= p
->ssize
)
1487 if (!enlarge(p
, (p
->ssize
+1) / 2 * 3)) /* +50% */
1490 /* finally, it's all reduced to the easy case */
1491 p
->strip
[p
->slen
] = op
;
1492 p
->stripdata
[p
->slen
] = opnd
;
1497 - doinsert - insert a sop into the strip
1498 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
1501 doinsert(struct parse
*p
, sop op
, size_t opnd
, sopno pos
)
1508 /* avoid making error situations worse */
1513 EMIT(op
, opnd
); /* do checks, ensure space */
1514 assert(HERE() == sn
+1);
1516 d
= p
->stripdata
[sn
];
1518 /* adjust paren pointers */
1520 for (i
= 1; i
< NPAREN
; i
++) {
1521 if (p
->pbegin
[i
] >= pos
) {
1524 if (p
->pend
[i
] >= pos
) {
1529 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1530 (HERE()-pos
-1)*sizeof(sop
));
1531 memmove((char *)&p
->stripdata
[pos
+1], (char *)&p
->stripdata
[pos
],
1532 (HERE()-pos
-1)*sizeof(RCHAR_T
));
1534 p
->stripdata
[pos
] = d
;
1538 - dofwd - complete a forward reference
1539 == static void dofwd(struct parse *p, sopno pos, sop value);
1542 dofwd(struct parse
*p
, sopno pos
, sop value
)
1544 /* avoid making error situations worse */
1549 p
->stripdata
[pos
] = value
;
1553 - enlarge - enlarge the strip
1554 == static int enlarge(struct parse *p, sopno size);
1557 enlarge(struct parse
*p
, sopno size
)
1563 if (p
->ssize
>= size
)
1568 if (MEMSIZE(p
) > MEMLIMIT
)
1570 sp
= realloc(p
->strip
, p
->ssize
* sizeof(sop
));
1574 dp
= realloc(p
->stripdata
, p
->ssize
* sizeof(RCHAR_T
));
1578 SETERROR(REG_ESPACE
);
1586 - stripsnug - compact the strip
1587 == static void stripsnug(struct parse *p, struct re_guts *g);
1590 stripsnug(struct parse
*p
, struct re_guts
*g
)
1592 g
->nstates
= p
->slen
;
1593 g
->strip
= (sop
*)realloc((char *)p
->strip
,
1594 p
->slen
* sizeof(sop
));
1595 if (g
->strip
== NULL
) {
1596 SETERROR(REG_ESPACE
);
1597 g
->strip
= p
->strip
;
1599 g
->stripdata
= (RCHAR_T
*)realloc((char *)p
->stripdata
,
1600 p
->slen
* sizeof(RCHAR_T
));
1601 if (g
->stripdata
== NULL
) {
1602 SETERROR(REG_ESPACE
);
1603 g
->stripdata
= p
->stripdata
;
1608 - findmust - fill in must and mlen with longest mandatory literal string
1609 == static void findmust(struct parse *p, struct re_guts *g);
1611 * This algorithm could do fancy things like analyzing the operands of |
1612 * for common subsequences. Someday. This code is simple and finds most
1613 * of the interesting cases.
1615 * Note that must and mlen got initialized during setup.
1618 findmust(struct parse
*p
, struct re_guts
*g
)
1623 RCHAR_T
*startd
= NULL
;
1625 RCHAR_T
*newstartd
= NULL
;
1632 /* avoid making error situations worse */
1636 /* find the longest OCHAR sequence in strip */
1638 scans
= g
->strip
+ 1;
1639 scand
= g
->stripdata
+ 1;
1644 case OCHAR
: /* sequence member */
1645 if (newlen
== 0) { /* new sequence */
1646 newstarts
= scans
- 1;
1647 newstartd
= scand
- 1;
1651 case OPLUS_
: /* things that don't break one */
1655 case OQUEST_
: /* things that must be skipped */
1664 /* assert() interferes w debug printouts */
1665 if (s
!= O_QUEST
&& s
!= O_CH
&& s
!= OOR2
) {
1669 } while (s
!= O_QUEST
&& s
!= O_CH
);
1671 default: /* things that break a sequence */
1672 if (newlen
> g
->mlen
) { /* ends one */
1680 } while (s
!= OEND
);
1682 if (g
->mlen
== 0) /* there isn't one */
1685 /* turn it into a character string */
1686 g
->must
= malloc(((size_t)g
->mlen
+ 1) * sizeof(RCHAR_T
));
1687 if (g
->must
== NULL
) { /* argh; just forget it */
1694 for (i
= g
->mlen
; i
> 0; i
--) {
1701 assert(cp
< g
->must
+ g
->mlen
);
1704 assert(cp
== g
->must
+ g
->mlen
);
1705 *cp
++ = '\0'; /* just on general principles */
1709 - pluscount - count + nesting
1710 == static sopno pluscount(struct parse *p, struct re_guts *g);
1712 static sopno
/* nesting depth */
1713 pluscount(struct parse
*p
, struct re_guts
*g
)
1721 return(0); /* there may not be an OEND */
1723 scan
= g
->strip
+ 1;
1731 if (plusnest
> maxnest
)
1736 } while (s
!= OEND
);