1 /* $NetBSD: regcomp.c,v 1.4 2009/01/18 03:45:50 lukem Exp $ */
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.
8 * This code is derived from software contributed to Berkeley by
9 * Henry Spencer of the University of Toronto.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)regcomp.c 8.4 (Berkeley) 3/19/94
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid
[] = "@(#)regcomp.c 8.4 (Berkeley) 3/19/94";
44 #endif /* LIBC_SCCS and not lint */
46 #include <sys/types.h>
61 * parse structure, passed up and down to avoid global variables and
65 RCHAR_T
*next
; /* next character in RE */
66 RCHAR_T
*end
; /* end of string (-> NUL normally) */
67 int error
; /* has an error been seen? */
68 sop
*strip
; /* malloced strip */
69 RCHAR_T
*stripdata
; /* malloced stripdata */
70 sopno ssize
; /* malloced strip size (allocated) */
71 sopno slen
; /* malloced strip length (used) */
72 int ncsalloc
; /* number of csets allocated */
74 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
75 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
76 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
79 /* ========= begin header generated by ./mkh ========= */
84 /* === regcomp.c === */
85 static void p_ere
__P((struct parse
*p
, int stop
));
86 static void p_ere_exp
__P((struct parse
*p
));
87 static void p_str
__P((struct parse
*p
));
88 static void p_bre
__P((struct parse
*p
, int end1
, int end2
));
89 static int p_simp_re
__P((struct parse
*p
, int starordinary
));
90 static int p_count
__P((struct parse
*p
));
91 static void p_bracket
__P((struct parse
*p
));
92 static void p_b_term
__P((struct parse
*p
, cset
*cs
));
93 static void p_b_cclass
__P((struct parse
*p
, cset
*cs
));
94 static void p_b_eclass
__P((struct parse
*p
, cset
*cs
));
95 static char p_b_symbol
__P((struct parse
*p
));
96 static char p_b_coll_elem
__P((struct parse
*p
, int endc
));
97 static char othercase
__P((int ch
));
98 static void bothcases
__P((struct parse
*p
, int ch
));
99 static void ordinary
__P((struct parse
*p
, int ch
));
100 static void nonnewline
__P((struct parse
*p
));
101 static void repeat
__P((struct parse
*p
, sopno start
, int from
, int to
));
102 static int seterr
__P((struct parse
*p
, int e
));
103 static cset
*allocset
__P((struct parse
*p
));
104 static void freeset
__P((struct parse
*p
, cset
*cs
));
105 static int freezeset
__P((struct parse
*p
, cset
*cs
));
106 static int firstch
__P((struct parse
*p
, cset
*cs
));
107 static int nch
__P((struct parse
*p
, cset
*cs
));
108 static void mcadd
__P((struct parse
*p
, cset
*cs
, const char *cp
));
110 static void mcsub
__P((cset
*cs
, char *cp
));
111 static int mcin
__P((cset
*cs
, char *cp
));
112 static char *mcfind
__P((cset
*cs
, char *cp
));
114 static void mcinvert
__P((struct parse
*p
, cset
*cs
));
115 static void mccase
__P((struct parse
*p
, cset
*cs
));
117 static int isinsets
__P((struct re_guts
*g
, int c
));
118 static int samesets
__P((struct re_guts
*g
, int c1
, int c2
));
120 static void categorize
__P((struct parse
*p
, struct re_guts
*g
));
121 static sopno dupl
__P((struct parse
*p
, sopno start
, sopno finish
));
122 static void doemit
__P((struct parse
*p
, sop op
, size_t opnd
));
123 static void doinsert
__P((struct parse
*p
, sop op
, size_t opnd
, sopno pos
));
124 static void dofwd
__P((struct parse
*p
, sopno pos
, sop value
));
125 static void enlarge
__P((struct parse
*p
, sopno size
));
126 static void stripsnug
__P((struct parse
*p
, struct re_guts
*g
));
127 static void findmust
__P((struct parse
*p
, struct re_guts
*g
));
128 static sopno pluscount
__P((struct parse
*p
, struct re_guts
*g
));
133 /* ========= end header generated by ./mkh ========= */
135 static RCHAR_T nuls
[10]; /* place to point scanner in event of error */
138 * macros for use with parse structure
139 * BEWARE: these know that the parse structure is named `p' !!!
141 #define PEEK() (*p->next)
142 #define PEEK2() (*(p->next+1))
143 #define MORE() (p->next < p->end)
144 #define MORE2() (p->next+1 < p->end)
145 #define SEE(c) (MORE() && PEEK() == (c))
146 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
147 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
148 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
149 #define NEXT() (p->next++)
150 #define NEXT2() (p->next += 2)
151 #define NEXTn(n) (p->next += (n))
152 #define GETNEXT() (*p->next++)
153 #define SETERROR(e) seterr(p, (e))
154 #define REQUIRE(co, e) ((co) || SETERROR(e))
155 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
156 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
157 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
158 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
159 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
160 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
161 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
162 #define HERE() (p->slen)
163 #define THERE() (p->slen - 1)
164 #define THERETHERE() (p->slen - 2)
165 #define DROP(n) (p->slen -= (n))
168 static int never
= 0; /* for use in asserts; shuts lint up */
170 #define never 0 /* some <assert.h>s have bugs too */
174 - regcomp - interface for parser and compilation
175 = extern int regcomp(regex_t *, const RCHAR_T *, int);
176 = #define REG_BASIC 0000
177 = #define REG_EXTENDED 0001
178 = #define REG_ICASE 0002
179 = #define REG_NOSUB 0004
180 = #define REG_NEWLINE 0010
181 = #define REG_NOSPEC 0020
182 = #define REG_PEND 0040
183 = #define REG_DUMP 0200
185 int /* 0 success, otherwise REG_something */
186 regcomp(regex_t
*preg
, const RCHAR_T
*pattern
, int cflags
)
189 register struct re_guts
*g
;
190 register struct parse
*p
= &pa
;
194 # define GOODFLAGS(f) (f)
196 # define GOODFLAGS(f) ((f)&~REG_DUMP)
199 cflags
= GOODFLAGS(cflags
);
200 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
203 if (cflags
®_PEND
) {
204 if (preg
->re_endp
< pattern
)
206 len
= preg
->re_endp
- pattern
;
208 len
= STRLEN(pattern
);
210 /* do the mallocs early so failure handling is easy */
211 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
212 (NC
-1)*sizeof(cat_t
));
215 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
216 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
217 if (p
->strip
== NULL
) {
221 p
->stripdata
= (RCHAR_T
*)malloc(p
->ssize
* sizeof(RCHAR_T
));
222 if (p
->stripdata
== NULL
) {
223 free((char *)p
->strip
);
231 p
->next
= (RCHAR_T
*)__UNCONST(pattern
); /* convenience; we do not modify it */
232 p
->end
= p
->next
+ len
;
235 for (i
= 0; i
< NPAREN
; i
++) {
251 g
->ncategories
= 1; /* category 0 is "everything else" */
252 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
253 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
259 g
->firststate
= THERE();
260 if (cflags
®_EXTENDED
)
262 else if (cflags
®_NOSPEC
)
267 g
->laststate
= THERE();
269 /* tidy up loose ends and fill things in */
273 g
->nplus
= pluscount(p
, g
);
275 preg
->re_nsub
= g
->nsub
;
277 preg
->re_magic
= MAGIC1
;
279 /* not debugging, so can't rely on the assert() in regexec() */
281 SETERROR(REG_ASSERT
);
284 /* win or lose, we're done */
285 if (p
->error
!= 0) /* lose */
291 - p_ere - ERE parser top level, concatenation and alternation
292 == static void p_ere(register struct parse *p, int stop);
295 p_ere(register struct parse
*p
, int stop
)
297 /* character this ERE should end at */
300 register sopno prevback
= 0;
301 register sopno prevfwd
= 0;
303 register int first
= 1; /* is this the first alternative? */
306 /* do a bunch of concatenated expressions */
308 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
310 (void)REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
313 break; /* NOTE BREAK OUT */
316 INSERT(OCH_
, conc
); /* offset is wrong */
321 ASTERN(OOR1
, prevback
);
323 AHEAD(prevfwd
); /* fix previous offset */
325 EMIT(OOR2
, 0); /* offset is very wrong */
328 if (!first
) { /* tail-end fixups */
330 ASTERN(O_CH
, prevback
);
333 assert(!MORE() || SEE(stop
));
337 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
338 == static void p_ere_exp(register struct parse *p);
341 p_ere_exp(register struct parse
*p
)
347 register sopno subno
;
350 assert(MORE()); /* caller should have ensured this */
356 (void)REQUIRE(MORE(), REG_EPAREN
);
360 p
->pbegin
[subno
] = HERE();
361 EMIT(OLPAREN
, subno
);
364 if (subno
< NPAREN
) {
365 p
->pend
[subno
] = HERE();
366 assert(p
->pend
[subno
] != 0);
368 EMIT(ORPAREN
, subno
);
369 (void)MUSTEAT(')', REG_EPAREN
);
371 #ifndef POSIX_MISTAKE
372 case ')': /* happens only if no current unmatched ( */
374 * You may ask, why the ifndef? Because I didn't notice
375 * this until slightly too late for 1003.2, and none of the
376 * other 1003.2 regular-expression reviewers noticed it at
377 * all. So an unmatched ) is legal POSIX, at least until
378 * we can get it fixed.
380 SETERROR(REG_EPAREN
);
385 p
->g
->iflags
|= USEBOL
;
391 p
->g
->iflags
|= USEEOL
;
400 SETERROR(REG_BADRPT
);
403 if (p
->g
->cflags
®_NEWLINE
)
412 (void)REQUIRE(MORE(), REG_EESCAPE
);
416 case '{': /* okay as ordinary except if digit follows */
417 (void)REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT
);
427 /* we call { a repetition if followed by a digit */
428 if (!( c
== '*' || c
== '+' || c
== '?' ||
429 (c
== '{' && MORE2() && isdigit(PEEK2())) ))
430 return; /* no repetition, we're done */
433 (void)REQUIRE(!wascaret
, REG_BADRPT
);
435 case '*': /* implemented as +? */
436 /* this case does not require the (y|) trick, noKLUDGE */
439 INSERT(OQUEST_
, pos
);
440 ASTERN(O_QUEST
, pos
);
447 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
448 INSERT(OCH_
, pos
); /* offset slightly wrong */
449 ASTERN(OOR1
, pos
); /* this one's right */
450 AHEAD(pos
); /* fix the OCH_ */
451 EMIT(OOR2
, 0); /* offset very wrong... */
452 AHEAD(THERE()); /* ...so fix it */
453 ASTERN(O_CH
, THERETHERE());
458 if (isdigit(PEEK())) {
460 (void)REQUIRE(count
<= count2
, REG_BADBR
);
461 } else /* single number with comma */
463 } else /* just a single number */
465 repeat(p
, pos
, count
, count2
);
466 if (!EAT('}')) { /* error heuristics */
467 while (MORE() && PEEK() != '}')
469 (void)REQUIRE(MORE(), REG_EBRACE
);
478 if (!( c
== '*' || c
== '+' || c
== '?' ||
479 (c
== '{' && MORE2() && isdigit(PEEK2())) ) )
481 SETERROR(REG_BADRPT
);
485 - p_str - string (no metacharacters) "parser"
486 == static void p_str(register struct parse *p);
489 p_str(register struct parse
*p
)
491 (void)REQUIRE(MORE(), REG_EMPTY
);
493 ordinary(p
, GETNEXT());
497 - p_bre - BRE parser top level, anchoring and concatenation
498 == static void p_bre(register struct parse *p, register int end1, \
499 == register int end2);
500 * Giving end1 as OUT essentially eliminates the end1/end2 check.
502 * This implementation is a bit of a kludge, in that a trailing $ is first
503 * taken as an ordinary character and then revised to be an anchor. The
504 * only undesirable side effect is that '$' gets included as a character
505 * category in such cases. This is fairly harmless; not worth fixing.
506 * The amount of lookahead needed to avoid this kludge is excessive.
509 p_bre(register struct parse
*p
, register int end1
, register int end2
)
511 /* first terminating character */
512 /* second terminating character */
514 register sopno start
= HERE();
515 register int first
= 1; /* first subexpression? */
516 register int wasdollar
= 0;
520 p
->g
->iflags
|= USEBOL
;
523 while (MORE() && !SEETWO(end1
, end2
)) {
524 wasdollar
= p_simp_re(p
, first
);
527 if (wasdollar
) { /* oops, that was a trailing anchor */
530 p
->g
->iflags
|= USEEOL
;
534 (void)REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
538 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
539 == static int p_simp_re(register struct parse *p, int starordinary);
541 static int /* was the simple RE an unbackslashed $? */
542 p_simp_re(register struct parse
*p
, int starordinary
)
544 /* is a leading * an ordinary character? */
551 register sopno subno
;
554 pos
= HERE(); /* repetion op, if any, covers from here */
556 assert(MORE()); /* caller should have ensured this */
560 (void)REQUIRE(MORE(), REG_EESCAPE
);
561 c
= (unsigned char)GETNEXT();
564 SETERROR(REG_BADRPT
);
570 p
->pbegin
[subno
] = HERE();
571 EMIT(OLPAREN
, subno
);
572 /* the MORE here is an error heuristic */
573 if (MORE() && !SEETWO('\\', ')'))
575 if (subno
< NPAREN
) {
576 p
->pend
[subno
] = HERE();
577 assert(p
->pend
[subno
] != 0);
579 EMIT(ORPAREN
, subno
);
580 (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
582 case ')': /* should not get here -- must be user */
584 SETERROR(REG_EPAREN
);
597 if (p
->pend
[i
] != 0) {
598 assert(i
<= p
->g
->nsub
);
600 assert(p
->pbegin
[i
] != 0);
601 assert(p
->strip
[p
->pbegin
[i
]] == OLPAREN
);
602 assert(p
->strip
[p
->pend
[i
]] == ORPAREN
);
603 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
606 SETERROR(REG_ESUBREG
);
616 if (p
->g
->cflags
®_NEWLINE
)
625 (void)REQUIRE(starordinary
, REG_BADRPT
);
633 if (EAT('*')) { /* implemented as +? */
634 /* this case does not require the (y|) trick, noKLUDGE */
637 INSERT(OQUEST_
, pos
);
638 ASTERN(O_QUEST
, pos
);
639 } else if (EATTWO('\\', '{')) {
642 if (MORE() && isdigit(PEEK())) {
644 (void)REQUIRE(count
<= count2
, REG_BADBR
);
645 } else /* single number with comma */
647 } else /* just a single number */
649 repeat(p
, pos
, count
, count2
);
650 if (!EATTWO('\\', '}')) { /* error heuristics */
651 while (MORE() && !SEETWO('\\', '}'))
653 (void)REQUIRE(MORE(), REG_EBRACE
);
656 } else if (!backsl
&& c
== (unsigned char)'$') /* $ (but not \$) ends it */
663 - p_count - parse a repetition count
664 == static int p_count(register struct parse *p);
666 static int /* the value */
667 p_count(register struct parse
*p
)
669 register int count
= 0;
670 register int ndigits
= 0;
672 while (MORE() && isdigit(PEEK()) && count
<= DUPMAX
) {
673 count
= count
*10 + (GETNEXT() - '0');
677 (void)REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
682 - p_bracket - parse a bracketed character list
683 == static void p_bracket(register struct parse *p);
685 * Note a significant property of this code: if the allocset() did SETERROR,
686 * no set operations are done.
689 p_bracket(register struct parse
*p
)
691 register cset
*cs
= allocset(p
);
692 register int invert
= 0;
693 static RCHAR_T bow
[] = { '[', ':', '<', ':', ']', ']' };
694 static RCHAR_T eow
[] = { '[', ':', '>', ':', ']', ']' };
696 /* Dept of Truly Sickening Special-Case Kludges */
697 if (p
->next
+ 5 < p
->end
&& MEMCMP(p
->next
, bow
, 6) == 0) {
702 if (p
->next
+ 5 < p
->end
&& MEMCMP(p
->next
, eow
, 6) == 0) {
709 invert
++; /* make note to invert set at end */
714 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
718 (void)MUSTEAT(']', REG_EBRACK
);
720 if (p
->error
!= 0) /* don't mess things up further */
723 if (p
->g
->cflags
®_ICASE
) {
727 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
728 if (CHIN(cs
, i
) && isalpha(i
)) {
733 if (cs
->multis
!= NULL
)
739 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
744 if (p
->g
->cflags
®_NEWLINE
)
746 if (cs
->multis
!= NULL
)
750 assert(cs
->multis
== NULL
); /* xxx */
752 if (nch(p
, cs
) == 1) { /* optimize singleton sets */
753 ordinary(p
, firstch(p
, cs
));
756 EMIT(OANYOF
, freezeset(p
, cs
));
760 - p_b_term - parse one term of a bracketed character list
761 == static void p_b_term(register struct parse *p, register cset *cs);
764 p_b_term(register struct parse
*p
, register cset
*cs
)
767 register char start
, finish
;
770 /* classify what we've got */
771 switch ((MORE()) ? PEEK() : '\0') {
773 c
= (MORE2()) ? PEEK2() : '\0';
776 SETERROR(REG_ERANGE
);
777 return; /* NOTE RETURN */
785 case ':': /* character class */
787 (void)REQUIRE(MORE(), REG_EBRACK
);
789 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
791 (void)REQUIRE(MORE(), REG_EBRACK
);
792 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
794 case '=': /* equivalence class */
796 (void)REQUIRE(MORE(), REG_EBRACK
);
798 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
800 (void)REQUIRE(MORE(), REG_EBRACK
);
801 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
803 default: /* symbol, ordinary character, or range */
804 /* xxx revision needed for multichar stuff */
805 start
= p_b_symbol(p
);
806 if (SEE('-') && MORE2() && PEEK2() != ']') {
812 finish
= p_b_symbol(p
);
815 /* xxx what about signed chars here... */
816 (void)REQUIRE(start
<= finish
, REG_ERANGE
);
817 for (i
= start
; i
<= finish
; i
++)
824 - p_b_cclass - parse a character-class name and deal with it
825 == static void p_b_cclass(register struct parse *p, register cset *cs);
828 p_b_cclass(register struct parse
*p
, register cset
*cs
)
830 register RCHAR_T
*sp
= p
->next
;
831 register struct cclass
*cp
;
833 register const char *u
;
836 while (MORE() && isalpha(PEEK()))
839 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
840 if (STRLEN(cp
->name
) == len
&& MEMCMP(cp
->name
, sp
, len
))
842 if (cp
->name
== NULL
) {
843 /* oops, didn't find it */
844 SETERROR(REG_ECTYPE
);
849 while ((c
= *u
++) != '\0')
851 for (u
= cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
856 - p_b_eclass - parse an equivalence-class name and deal with it
857 == static void p_b_eclass(register struct parse *p, register cset *cs);
859 * This implementation is incomplete. xxx
862 p_b_eclass(register struct parse
*p
, register cset
*cs
)
866 c
= p_b_coll_elem(p
, '=');
871 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
872 == static char p_b_symbol(register struct parse *p);
874 static char /* value of symbol */
875 p_b_symbol(register struct parse
*p
)
879 (void)REQUIRE(MORE(), REG_EBRACK
);
880 if (!EATTWO('[', '.'))
883 /* collating symbol */
884 value
= p_b_coll_elem(p
, '.');
885 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
890 - p_b_coll_elem - parse a collating-element name and look it up
891 == static char p_b_coll_elem(register struct parse *p, int endc);
893 static char /* value of collating element */
894 p_b_coll_elem(register struct parse
*p
, int endc
)
896 /* name ended by endc,']' */
898 register RCHAR_T
*sp
= p
->next
;
899 register struct cname
*cp
;
902 while (MORE() && !SEETWO(endc
, ']'))
905 SETERROR(REG_EBRACK
);
909 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
910 if (STRLEN(cp
->name
) == len
&& MEMCMP(cp
->name
, sp
, len
))
911 return(cp
->code
); /* known name */
913 return(*sp
); /* single character */
914 SETERROR(REG_ECOLLATE
); /* neither */
919 - othercase - return the case counterpart of an alphabetic
920 == static char othercase(int ch);
922 static char /* if no counterpart, return ch */
928 else if (islower(ch
))
930 else /* peculiar, but could happen */
935 - bothcases - emit a dualcase version of a two-case character
936 == static void bothcases(register struct parse *p, int ch);
938 * Boy, is this implementation ever a kludge...
941 bothcases(register struct parse
*p
, int ch
)
943 register RCHAR_T
*oldnext
= p
->next
;
944 register RCHAR_T
*oldend
= p
->end
;
947 assert(othercase(ch
) != ch
); /* p_bracket() would recurse */
954 assert(p
->next
== bracket
+2);
960 - ordinary - emit an ordinary character
961 == static void ordinary(register struct parse *p, register int ch);
964 ordinary(register struct parse
*p
, register int ch
)
967 register cat_t *cap = p->g->categories;
970 if ((p
->g
->cflags
®_ICASE
) && isalpha(ch
) && othercase(ch
) != ch
)
973 EMIT(OCHAR
, (UCHAR_T
)ch
);
976 cap[ch] = p->g->ncategories++;
982 - nonnewline - emit REG_NEWLINE version of OANY
983 == static void nonnewline(register struct parse *p);
985 * Boy, is this implementation ever a kludge...
988 nonnewline(register struct parse
*p
)
990 register RCHAR_T
*oldnext
= p
->next
;
991 register RCHAR_T
*oldend
= p
->end
;
1001 assert(p
->next
== bracket
+3);
1007 - repeat - generate code for a bounded repetition, recursively if needed
1008 == static void repeat(register struct parse *p, sopno start, int from, int to);
1011 repeat(register struct parse
*p
, sopno start
, int from
, int to
)
1013 /* operand from here to end of strip */
1014 /* repeated from this number */
1015 /* to this number of times (maybe INFINITY) */
1017 register sopno finish
= HERE();
1020 # define REP(f, t) ((f)*8 + (t))
1021 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1022 register sopno copy
;
1024 if (p
->error
!= 0) /* head off possible runaway recursion */
1029 switch (REP(MAP(from
), MAP(to
))) {
1030 case REP(0, 0): /* must be user doing this */
1031 DROP(finish
-start
); /* drop the operand */
1033 case REP(0, 1): /* as x{1,1}? */
1034 case REP(0, N
): /* as x{1,n}? */
1035 case REP(0, INF
): /* as x{1,}? */
1036 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1037 INSERT(OCH_
, start
); /* offset is wrong... */
1038 repeat(p
, start
+1, 1, to
);
1039 ASTERN(OOR1
, start
);
1040 AHEAD(start
); /* ... fix it */
1043 ASTERN(O_CH
, THERETHERE());
1045 case REP(1, 1): /* trivial case */
1048 case REP(1, N
): /* as x?x{1,n-1} */
1049 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1050 INSERT(OCH_
, start
);
1051 ASTERN(OOR1
, start
);
1053 EMIT(OOR2
, 0); /* offset very wrong... */
1054 AHEAD(THERE()); /* ...so fix it */
1055 ASTERN(O_CH
, THERETHERE());
1056 copy
= dupl(p
, start
+1, finish
+1);
1057 assert(copy
== finish
+4);
1058 repeat(p
, copy
, 1, to
-1);
1060 case REP(1, INF
): /* as x+ */
1061 INSERT(OPLUS_
, start
);
1062 ASTERN(O_PLUS
, start
);
1064 case REP(N
, N
): /* as xx{m-1,n-1} */
1065 copy
= dupl(p
, start
, finish
);
1066 repeat(p
, copy
, from
-1, to
-1);
1068 case REP(N
, INF
): /* as xx{n-1,INF} */
1069 copy
= dupl(p
, start
, finish
);
1070 repeat(p
, copy
, from
-1, to
);
1072 default: /* "can't happen" */
1073 SETERROR(REG_ASSERT
); /* just in case */
1079 - seterr - set an error condition
1080 == static int seterr(register struct parse *p, int e);
1082 static int /* useless but makes type checking happy */
1083 seterr(register struct parse
*p
, int e
)
1085 if (p
->error
== 0) /* keep earliest error condition */
1087 p
->next
= nuls
; /* try to bring things to a halt */
1089 return(0); /* make the return value well-defined */
1093 - allocset - allocate a set of characters for []
1094 == static cset *allocset(register struct parse *p);
1097 allocset(register struct parse
*p
)
1099 register int no
= p
->g
->ncsets
++;
1101 register size_t nbytes
;
1103 register size_t css
= (size_t)p
->g
->csetsize
;
1106 if (no
>= p
->ncsalloc
) { /* need another column of space */
1107 p
->ncsalloc
+= CHAR_BIT
;
1109 assert(nc
% CHAR_BIT
== 0);
1110 nbytes
= nc
/ CHAR_BIT
* css
;
1111 if (p
->g
->sets
== NULL
)
1112 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1114 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1116 if (p
->g
->setbits
== NULL
)
1117 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1119 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1121 /* xxx this isn't right if setbits is now NULL */
1122 for (i
= 0; i
< no
; i
++)
1123 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1125 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1126 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1130 SETERROR(REG_ESPACE
);
1131 /* caller's responsibility not to do set ops */
1135 assert(p
->g
->sets
!= NULL
); /* xxx */
1136 cs
= &p
->g
->sets
[no
];
1137 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1138 cs
->mask
= 1 << ((no
) % CHAR_BIT
);
1147 - freeset - free a now-unused set
1148 == static void freeset(register struct parse *p, register cset *cs);
1151 freeset(register struct parse
*p
, register cset
*cs
)
1154 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1155 register size_t css
= (size_t)p
->g
->csetsize
;
1157 for (i
= 0; i
< css
; i
++)
1159 if (cs
== top
-1) /* recover only the easy case */
1164 - freezeset - final processing on a set of characters
1165 == static int freezeset(register struct parse *p, register cset *cs);
1167 * The main task here is merging identical sets. This is usually a waste
1168 * of time (although the hash code minimizes the overhead), but can win
1169 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1170 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1173 static int /* set number */
1174 freezeset(register struct parse
*p
, register cset
*cs
)
1176 register uch h
= cs
->hash
;
1178 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1180 register size_t css
= (size_t)p
->g
->csetsize
;
1182 /* look for an earlier one which is the same */
1183 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1184 if (cs2
->hash
== h
&& cs2
!= cs
) {
1186 for (i
= 0; i
< css
; i
++)
1187 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1193 if (cs2
< top
) { /* found one */
1198 return((int)(cs
- p
->g
->sets
));
1202 - firstch - return first character in a set (which must have at least one)
1203 == static int firstch(register struct parse *p, register cset *cs);
1205 static int /* character; there is no "none" value */
1206 firstch(register struct parse
*p
, register cset
*cs
)
1209 register size_t css
= (size_t)p
->g
->csetsize
;
1211 for (i
= 0; i
< css
; i
++)
1215 return(0); /* arbitrary */
1219 - nch - number of characters in a set
1220 == static int nch(register struct parse *p, register cset *cs);
1223 nch(register struct parse
*p
, register cset
*cs
)
1226 register size_t css
= (size_t)p
->g
->csetsize
;
1229 for (i
= 0; i
< css
; i
++)
1236 - mcadd - add a collating element to a cset
1237 == static void mcadd(register struct parse *p, register cset *cs, \
1238 == register char *cp);
1241 mcadd(register struct parse
*p
, register cset
*cs
, register const char *cp
)
1243 register size_t oldend
= cs
->smultis
;
1245 cs
->smultis
+= strlen(cp
) + 1;
1246 if (cs
->multis
== NULL
)
1247 cs
->multis
= malloc(cs
->smultis
);
1249 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1250 if (cs
->multis
== NULL
) {
1251 SETERROR(REG_ESPACE
);
1255 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1256 cs
->multis
[cs
->smultis
- 1] = '\0';
1261 - mcsub - subtract a collating element from a cset
1262 == static void mcsub(register cset *cs, register char *cp);
1265 mcsub(register cset
*cs
, register char *cp
)
1267 register char *fp
= mcfind(cs
, cp
);
1268 register size_t len
= strlen(fp
);
1271 (void) memmove(fp
, fp
+ len
+ 1,
1272 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1275 if (cs
->smultis
== 0) {
1281 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1282 assert(cs
->multis
!= NULL
);
1286 - mcin - is a collating element in a cset?
1287 == static int mcin(register cset *cs, register char *cp);
1290 mcin(register cset
*cs
, register char *cp
)
1292 return(mcfind(cs
, cp
) != NULL
);
1296 - mcfind - find a collating element in a cset
1297 == static char *mcfind(register cset *cs, register char *cp);
1300 mcfind(register cset
*cs
, register char *cp
)
1304 if (cs
->multis
== NULL
)
1306 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1307 if (strcmp(cp
, p
) == 0)
1314 - mcinvert - invert the list of collating elements in a cset
1315 == static void mcinvert(register struct parse *p, register cset *cs);
1317 * This would have to know the set of possibilities. Implementation
1321 mcinvert(register struct parse
*p
, register cset
*cs
)
1323 assert(cs
->multis
== NULL
); /* xxx */
1327 - mccase - add case counterparts of the list of collating elements in a cset
1328 == static void mccase(register struct parse *p, register cset *cs);
1330 * This would have to know the set of possibilities. Implementation
1334 mccase(register struct parse
*p
, register cset
*cs
)
1336 assert(cs
->multis
== NULL
); /* xxx */
1341 - isinsets - is this character in any sets?
1342 == static int isinsets(register struct re_guts *g, int c);
1344 static int /* predicate */
1345 isinsets(register struct re_guts
*g
, int c
)
1349 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1350 register unsigned uc
= (unsigned char)c
;
1352 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1359 - samesets - are these two characters in exactly the same sets?
1360 == static int samesets(register struct re_guts *g, int c1, int c2);
1362 static int /* predicate */
1363 samesets(register struct re_guts
*g
, int c1
, int c2
)
1367 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1368 register unsigned uc1
= (unsigned char)c1
;
1369 register unsigned uc2
= (unsigned char)c2
;
1371 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1372 if (col
[uc1
] != col
[uc2
])
1379 - categorize - sort out character categories
1380 == static void categorize(struct parse *p, register struct re_guts *g);
1383 categorize(struct parse
*p
, register struct re_guts
*g
)
1386 register cat_t
*cats
= g
->categories
;
1391 /* avoid making error situations worse */
1395 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1396 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1397 cat
= g
->ncategories
++;
1399 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1400 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1407 - dupl - emit a duplicate of a bunch of sops
1408 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1410 static sopno
/* start of duplicate */
1411 dupl(register struct parse
*p
, sopno start
, sopno finish
)
1414 /* to this less one */
1416 register sopno ret
= HERE();
1417 register sopno len
= finish
- start
;
1419 assert(finish
>= start
);
1422 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1423 assert(p
->ssize
>= p
->slen
+ len
);
1424 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1425 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1426 (void) memcpy((char *)(p
->stripdata
+ p
->slen
),
1427 (char *)(p
->stripdata
+ start
), (size_t)len
*sizeof(RCHAR_T
));
1433 - doemit - emit a strip operator
1434 == static void doemit(register struct parse *p, sop op, size_t opnd);
1436 * It might seem better to implement this as a macro with a function as
1437 * hard-case backup, but it's just too big and messy unless there are
1438 * some changes to the data structures. Maybe later.
1441 doemit(register struct parse
*p
, sop op
, size_t opnd
)
1443 /* avoid making error situations worse */
1447 /* deal with oversize operands ("can't happen", more or less) */
1450 /* deal with undersized strip */
1451 if (p
->slen
>= p
->ssize
)
1452 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1453 assert(p
->slen
< p
->ssize
);
1455 /* finally, it's all reduced to the easy case */
1456 p
->strip
[p
->slen
] = op
;
1457 p
->stripdata
[p
->slen
] = opnd
;
1462 - doinsert - insert a sop into the strip
1463 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1466 doinsert(register struct parse
*p
, sop op
, size_t opnd
, sopno pos
)
1473 /* avoid making error situations worse */
1478 EMIT(op
, opnd
); /* do checks, ensure space */
1479 assert(HERE() == sn
+1);
1481 d
= p
->stripdata
[sn
];
1483 /* adjust paren pointers */
1485 for (i
= 1; i
< NPAREN
; i
++) {
1486 if (p
->pbegin
[i
] >= pos
) {
1489 if (p
->pend
[i
] >= pos
) {
1494 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1495 (HERE()-pos
-1)*sizeof(sop
));
1496 memmove((char *)&p
->stripdata
[pos
+1], (char *)&p
->stripdata
[pos
],
1497 (HERE()-pos
-1)*sizeof(RCHAR_T
));
1499 p
->stripdata
[pos
] = d
;
1503 - dofwd - complete a forward reference
1504 == static void dofwd(register struct parse *p, sopno pos, sop value);
1507 dofwd(register struct parse
*p
, register sopno pos
, sop value
)
1509 /* avoid making error situations worse */
1514 p
->stripdata
[pos
] = value
;
1518 - enlarge - enlarge the strip
1519 == static void enlarge(register struct parse *p, sopno size);
1522 enlarge(register struct parse
*p
, register sopno size
)
1525 register RCHAR_T
*dp
;
1527 if (p
->ssize
>= size
)
1530 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1532 SETERROR(REG_ESPACE
);
1536 dp
= (RCHAR_T
*)realloc(p
->stripdata
, size
*sizeof(RCHAR_T
));
1538 SETERROR(REG_ESPACE
);
1546 - stripsnug - compact the strip
1547 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1550 stripsnug(register struct parse
*p
, register struct re_guts
*g
)
1552 g
->nstates
= p
->slen
;
1553 g
->strip
= (sop
*)realloc((char *)p
->strip
,
1554 p
->slen
* sizeof(sop
));
1555 if (g
->strip
== NULL
) {
1556 SETERROR(REG_ESPACE
);
1557 g
->strip
= p
->strip
;
1559 g
->stripdata
= (RCHAR_T
*)realloc((char *)p
->stripdata
,
1560 p
->slen
* sizeof(RCHAR_T
));
1561 if (g
->stripdata
== NULL
) {
1562 SETERROR(REG_ESPACE
);
1563 g
->stripdata
= p
->stripdata
;
1568 - findmust - fill in must and mlen with longest mandatory literal string
1569 == static void findmust(register struct parse *p, register struct re_guts *g);
1571 * This algorithm could do fancy things like analyzing the operands of |
1572 * for common subsequences. Someday. This code is simple and finds most
1573 * of the interesting cases.
1575 * Note that must and mlen got initialized during setup.
1578 findmust(struct parse
*p
, register struct re_guts
*g
)
1580 register sop
*scans
;
1581 register RCHAR_T
*scand
;
1583 RCHAR_T
*startd
= NULL
;
1584 register sop
*newstarts
= 0;
1585 register RCHAR_T
*newstartd
= NULL
;
1586 register sopno newlen
;
1589 register RCHAR_T
*cp
;
1592 /* avoid making error situations worse */
1596 /* find the longest OCHAR sequence in strip */
1598 scans
= g
->strip
+ 1;
1599 scand
= g
->stripdata
+ 1;
1604 case OCHAR
: /* sequence member */
1605 if (newlen
== 0) { /* new sequence */
1606 newstarts
= scans
- 1;
1607 newstartd
= scand
- 1;
1611 case OPLUS_
: /* things that don't break one */
1615 case OQUEST_
: /* things that must be skipped */
1624 /* assert() interferes w debug printouts */
1625 if (s
!= O_QUEST
&& s
!= O_CH
&& s
!= OOR2
) {
1629 } while (s
!= O_QUEST
&& s
!= O_CH
);
1631 default: /* things that break a sequence */
1632 if (newlen
> g
->mlen
) { /* ends one */
1640 } while (s
!= OEND
);
1642 if (g
->mlen
== 0) /* there isn't one */
1645 /* turn it into a character string */
1646 g
->must
= malloc(((size_t)g
->mlen
+ 1) * sizeof(RCHAR_T
));
1647 if (g
->must
== NULL
) { /* argh; just forget it */
1654 for (i
= g
->mlen
; i
> 0; i
--) {
1661 assert(cp
< g
->must
+ g
->mlen
);
1664 assert(cp
== g
->must
+ g
->mlen
);
1665 *cp
++ = '\0'; /* just on general principles */
1669 - pluscount - count + nesting
1670 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1672 static sopno
/* nesting depth */
1673 pluscount(struct parse
*p
, register struct re_guts
*g
)
1677 register sopno plusnest
= 0;
1678 register sopno maxnest
= 0;
1681 return(0); /* there may not be an OEND */
1683 scan
= g
->strip
+ 1;
1691 if (plusnest
> maxnest
)
1696 } while (s
!= OEND
);