2 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid
[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
42 #endif /* LIBC_SCCS and not lint */
44 #include <sys/types.h>
59 * parse structure, passed up and down to avoid global variables and
63 char *next
; /* next character in RE */
64 char *end
; /* end of string (-> NUL normally) */
65 int error
; /* has an error been seen? */
66 sop
*strip
; /* malloced strip */
67 sopno ssize
; /* malloced strip size (allocated) */
68 sopno slen
; /* malloced strip length (used) */
69 int ncsalloc
; /* number of csets allocated */
71 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
72 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
73 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
76 static void p_ere(struct parse
*p
, int stop
);
77 static void p_ere_exp(struct parse
*p
);
78 static void p_str(struct parse
*p
);
79 static void p_bre(struct parse
*p
, int end1
, int end2
);
80 static int p_simp_re(struct parse
*p
, int starordinary
);
81 static int p_count(struct parse
*p
);
82 static void p_bracket(struct parse
*p
);
83 static void p_b_term(struct parse
*p
, cset
*cs
);
84 static void p_b_cclass(struct parse
*p
, cset
*cs
);
85 static void p_b_eclass(struct parse
*p
, cset
*cs
);
86 static char p_b_symbol(struct parse
*p
);
87 static char p_b_coll_elem(struct parse
*p
, int endc
);
88 static char othercase(int ch
);
89 static void bothcases(struct parse
*p
, int ch
);
90 static void ordinary(struct parse
*p
, int ch
);
91 static void nonnewline(struct parse
*p
);
92 static void repeat(struct parse
*p
, sopno start
, int from
, int to
);
93 static int seterr(struct parse
*p
, int e
);
94 static cset
*allocset(struct parse
*p
);
95 static void freeset(struct parse
*p
, cset
*cs
);
96 static int freezeset(struct parse
*p
, cset
*cs
);
97 static int firstch(struct parse
*p
, cset
*cs
);
98 static int nch(struct parse
*p
, cset
*cs
);
99 static void mcadd(struct parse
*p
, cset
*cs
, char *cp
);
100 static void mcsub(cset
*cs
, char *cp
);
101 static int mcin(cset
*cs
, char *cp
);
102 static char *mcfind(cset
*cs
, char *cp
);
103 static void mcinvert(struct parse
*p
, cset
*cs
);
104 static void mccase(struct parse
*p
, cset
*cs
);
105 static int isinsets(struct re_guts
*g
, int c
);
106 static int samesets(struct re_guts
*g
, int c1
, int c2
);
107 static void categorize(struct parse
*p
, struct re_guts
*g
);
108 static sopno
dupl(struct parse
*p
, sopno start
, sopno finish
);
109 static void doemit(struct parse
*p
, sop op
, size_t opnd
);
110 static void doinsert(struct parse
*p
, sop op
, size_t opnd
, sopno pos
);
111 static void dofwd(struct parse
*p
, sopno pos
, sop value
);
112 static void enlarge(struct parse
*p
, sopno size
);
113 static void stripsnug(struct parse
*p
, struct re_guts
*g
);
114 static void findmust(struct parse
*p
, struct re_guts
*g
);
115 static sopno
pluscount(struct parse
*p
, struct re_guts
*g
);
117 static char nuls
[10]; /* place to point scanner in event of error */
120 * macros for use with parse structure
121 * BEWARE: these know that the parse structure is named `p' !!!
123 #define PEEK() (*p->next)
124 #define PEEK2() (*(p->next+1))
125 #define MORE() (p->next < p->end)
126 #define MORE2() (p->next+1 < p->end)
127 #define SEE(c) (MORE() && PEEK() == (c))
128 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
129 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
130 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
131 #define NEXT() (p->next++)
132 #define NEXT2() (p->next += 2)
133 #define NEXTn(n) (p->next += (n))
134 #define GETNEXT() (*p->next++)
135 #define SETERROR(e) seterr(p, (e))
136 #define REQUIRE(co, e) ((co) || SETERROR(e))
137 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
138 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
139 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
140 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
141 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
142 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
143 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
144 #define HERE() (p->slen)
145 #define THERE() (p->slen - 1)
146 #define THERETHERE() (p->slen - 2)
147 #define DROP(n) (p->slen -= (n))
150 static int never
= 0; /* for use in asserts; shuts lint up */
152 #define never 0 /* some <assert.h>s have bugs too */
156 - regcomp - interface for parser and compilation
157 = extern int regcomp(regex_t *, const char *, int);
158 = #define REG_BASIC 0000
159 = #define REG_EXTENDED 0001
160 = #define REG_ICASE 0002
161 = #define REG_NOSUB 0004
162 = #define REG_NEWLINE 0010
163 = #define REG_NOSPEC 0020
164 = #define REG_PEND 0040
165 = #define REG_DUMP 0200
167 int /* 0 success, otherwise REG_something */
168 regcomp(preg
, pattern
, cflags
)
174 register struct re_guts
*g
;
175 register struct parse
*p
= &pa
;
179 # define GOODFLAGS(f) (f)
181 # define GOODFLAGS(f) ((f)&~REG_DUMP)
184 cflags
= GOODFLAGS(cflags
);
185 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
188 if (cflags
®_PEND
) {
189 if (preg
->re_endp
< pattern
)
191 len
= preg
->re_endp
- pattern
;
193 len
= strlen((char *)pattern
);
195 /* do the mallocs early so failure handling is easy */
196 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
197 (NC
-1)*sizeof(cat_t
));
200 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
201 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
203 if (p
->strip
== NULL
) {
210 p
->next
= (char *)pattern
; /* convenience; we do not modify it */
211 p
->end
= p
->next
+ len
;
214 for (i
= 0; i
< NPAREN
; i
++) {
229 g
->ncategories
= 1; /* category 0 is "everything else" */
230 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
231 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
236 g
->firststate
= THERE();
237 if (cflags
®_EXTENDED
)
239 else if (cflags
®_NOSPEC
)
244 g
->laststate
= THERE();
246 /* tidy up loose ends and fill things in */
250 g
->nplus
= pluscount(p
, g
);
252 preg
->re_nsub
= g
->nsub
;
254 preg
->re_magic
= MAGIC1
;
256 /* not debugging, so can't rely on the assert() in regexec() */
258 SETERROR(REG_ASSERT
);
261 /* win or lose, we're done */
262 if (p
->error
!= 0) /* lose */
268 - p_ere - ERE parser top level, concatenation and alternation
269 == static void p_ere(register struct parse *p, int stop);
273 register struct parse
*p
;
274 int stop
; /* character this ERE should end at */
277 register sopno prevback
;
278 register sopno prevfwd
;
280 register int first
= 1; /* is this the first alternative? */
283 /* do a bunch of concatenated expressions */
285 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
287 REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
290 break; /* NOTE BREAK OUT */
293 INSERT(OCH_
, conc
); /* offset is wrong */
298 ASTERN(OOR1
, prevback
);
300 AHEAD(prevfwd
); /* fix previous offset */
302 EMIT(OOR2
, 0); /* offset is very wrong */
305 if (!first
) { /* tail-end fixups */
307 ASTERN(O_CH
, prevback
);
310 assert(!MORE() || SEE(stop
));
314 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
315 == static void p_ere_exp(register struct parse *p);
319 register struct parse
*p
;
325 register sopno subno
;
328 assert(MORE()); /* caller should have ensured this */
334 REQUIRE(MORE(), REG_EPAREN
);
338 p
->pbegin
[subno
] = HERE();
339 EMIT(OLPAREN
, subno
);
342 if (subno
< NPAREN
) {
343 p
->pend
[subno
] = HERE();
344 assert(p
->pend
[subno
] != 0);
346 EMIT(ORPAREN
, subno
);
347 MUSTEAT(')', REG_EPAREN
);
349 #ifndef POSIX_MISTAKE
350 case ')': /* happens only if no current unmatched ( */
352 * You may ask, why the ifndef? Because I didn't notice
353 * this until slightly too late for 1003.2, and none of the
354 * other 1003.2 regular-expression reviewers noticed it at
355 * all. So an unmatched ) is legal POSIX, at least until
356 * we can get it fixed.
358 SETERROR(REG_EPAREN
);
363 p
->g
->iflags
|= USEBOL
;
369 p
->g
->iflags
|= USEEOL
;
378 SETERROR(REG_BADRPT
);
381 if (p
->g
->cflags
®_NEWLINE
)
390 REQUIRE(MORE(), REG_EESCAPE
);
394 case '{': /* okay as ordinary except if digit follows */
395 REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT
);
405 /* we call { a repetition if followed by a digit */
406 if (!( c
== '*' || c
== '+' || c
== '?' ||
407 (c
== '{' && MORE2() && isdigit(PEEK2())) ))
408 return; /* no repetition, we're done */
411 REQUIRE(!wascaret
, REG_BADRPT
);
413 case '*': /* implemented as +? */
414 /* this case does not require the (y|) trick, noKLUDGE */
417 INSERT(OQUEST_
, pos
);
418 ASTERN(O_QUEST
, pos
);
425 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
426 INSERT(OCH_
, pos
); /* offset slightly wrong */
427 ASTERN(OOR1
, pos
); /* this one's right */
428 AHEAD(pos
); /* fix the OCH_ */
429 EMIT(OOR2
, 0); /* offset very wrong... */
430 AHEAD(THERE()); /* ...so fix it */
431 ASTERN(O_CH
, THERETHERE());
436 if (isdigit(PEEK())) {
438 REQUIRE(count
<= count2
, REG_BADBR
);
439 } else /* single number with comma */
441 } else /* just a single number */
443 repeat(p
, pos
, count
, count2
);
444 if (!EAT('}')) { /* error heuristics */
445 while (MORE() && PEEK() != '}')
447 REQUIRE(MORE(), REG_EBRACE
);
456 if (!( c
== '*' || c
== '+' || c
== '?' ||
457 (c
== '{' && MORE2() && isdigit(PEEK2())) ) )
459 SETERROR(REG_BADRPT
);
463 - p_str - string (no metacharacters) "parser"
464 == static void p_str(register struct parse *p);
468 register struct parse
*p
;
470 REQUIRE(MORE(), REG_EMPTY
);
472 ordinary(p
, GETNEXT());
476 - p_bre - BRE parser top level, anchoring and concatenation
477 == static void p_bre(register struct parse *p, register int end1, \
478 == register int end2);
479 * Giving end1 as OUT essentially eliminates the end1/end2 check.
481 * This implementation is a bit of a kludge, in that a trailing $ is first
482 * taken as an ordinary character and then revised to be an anchor. The
483 * only undesirable side effect is that '$' gets included as a character
484 * category in such cases. This is fairly harmless; not worth fixing.
485 * The amount of lookahead needed to avoid this kludge is excessive.
489 register struct parse
*p
;
490 register int end1
; /* first terminating character */
491 register int end2
; /* second terminating character */
493 register sopno start
= HERE();
494 register int first
= 1; /* first subexpression? */
495 register int wasdollar
= 0;
499 p
->g
->iflags
|= USEBOL
;
502 while (MORE() && !SEETWO(end1
, end2
)) {
503 wasdollar
= p_simp_re(p
, first
);
506 if (wasdollar
) { /* oops, that was a trailing anchor */
509 p
->g
->iflags
|= USEEOL
;
513 REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
517 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
518 == static int p_simp_re(register struct parse *p, int starordinary);
520 static int /* was the simple RE an unbackslashed $? */
521 p_simp_re(p
, starordinary
)
522 register struct parse
*p
;
523 int starordinary
; /* is a leading * an ordinary character? */
530 register sopno subno
;
531 # define BACKSL (1<<CHAR_BIT)
533 pos
= HERE(); /* repetion op, if any, covers from here */
535 assert(MORE()); /* caller should have ensured this */
538 REQUIRE(MORE(), REG_EESCAPE
);
539 c
= BACKSL
| (unsigned char)GETNEXT();
543 if (p
->g
->cflags
®_NEWLINE
)
552 SETERROR(REG_BADRPT
);
558 p
->pbegin
[subno
] = HERE();
559 EMIT(OLPAREN
, subno
);
560 /* the MORE here is an error heuristic */
561 if (MORE() && !SEETWO('\\', ')'))
563 if (subno
< NPAREN
) {
564 p
->pend
[subno
] = HERE();
565 assert(p
->pend
[subno
] != 0);
567 EMIT(ORPAREN
, subno
);
568 REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
570 case BACKSL
|')': /* should not get here -- must be user */
572 SETERROR(REG_EPAREN
);
583 i
= (c
&~BACKSL
) - '0';
585 if (p
->pend
[i
] != 0) {
586 assert(i
<= p
->g
->nsub
);
588 assert(p
->pbegin
[i
] != 0);
589 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
590 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
591 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
594 SETERROR(REG_ESUBREG
);
598 REQUIRE(starordinary
, REG_BADRPT
);
601 ordinary(p
, c
&~ BACKSL
);
605 if (EAT('*')) { /* implemented as +? */
606 /* this case does not require the (y|) trick, noKLUDGE */
609 INSERT(OQUEST_
, pos
);
610 ASTERN(O_QUEST
, pos
);
611 } else if (EATTWO('\\', '{')) {
614 if (MORE() && isdigit(PEEK())) {
616 REQUIRE(count
<= count2
, REG_BADBR
);
617 } else /* single number with comma */
619 } else /* just a single number */
621 repeat(p
, pos
, count
, count2
);
622 if (!EATTWO('\\', '}')) { /* error heuristics */
623 while (MORE() && !SEETWO('\\', '}'))
625 REQUIRE(MORE(), REG_EBRACE
);
628 } else if (c
== (unsigned char)'$') /* $ (but not \$) ends it */
635 - p_count - parse a repetition count
636 == static int p_count(register struct parse *p);
638 static int /* the value */
640 register struct parse
*p
;
642 register int count
= 0;
643 register int ndigits
= 0;
645 while (MORE() && isdigit(PEEK()) && count
<= DUPMAX
) {
646 count
= count
*10 + (GETNEXT() - '0');
650 REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
655 - p_bracket - parse a bracketed character list
656 == static void p_bracket(register struct parse *p);
658 * Note a significant property of this code: if the allocset() did SETERROR,
659 * no set operations are done.
663 register struct parse
*p
;
666 register cset
*cs
= allocset(p
);
667 register int invert
= 0;
669 /* Dept of Truly Sickening Special-Case Kludges */
670 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:<:]]", 6) == 0) {
675 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:>:]]", 6) == 0) {
682 invert
++; /* make note to invert set at end */
687 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
691 MUSTEAT(']', REG_EBRACK
);
693 if (p
->error
!= 0) /* don't mess things up further */
696 if (p
->g
->cflags
®_ICASE
) {
700 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
701 if (CHIN(cs
, i
) && isalpha(i
)) {
706 if (cs
->multis
!= NULL
)
712 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
717 if (p
->g
->cflags
®_NEWLINE
)
719 if (cs
->multis
!= NULL
)
723 assert(cs
->multis
== NULL
); /* xxx */
725 if (nch(p
, cs
) == 1) { /* optimize singleton sets */
726 ordinary(p
, firstch(p
, cs
));
729 EMIT(OANYOF
, freezeset(p
, cs
));
733 - p_b_term - parse one term of a bracketed character list
734 == static void p_b_term(register struct parse *p, register cset *cs);
738 register struct parse
*p
;
742 register char start
, finish
;
745 /* classify what we've got */
746 switch ((MORE()) ? PEEK() : '\0') {
748 c
= (MORE2()) ? PEEK2() : '\0';
751 SETERROR(REG_ERANGE
);
752 return; /* NOTE RETURN */
760 case ':': /* character class */
762 REQUIRE(MORE(), REG_EBRACK
);
764 REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
766 REQUIRE(MORE(), REG_EBRACK
);
767 REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
769 case '=': /* equivalence class */
771 REQUIRE(MORE(), REG_EBRACK
);
773 REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
775 REQUIRE(MORE(), REG_EBRACK
);
776 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
778 default: /* symbol, ordinary character, or range */
779 /* xxx revision needed for multichar stuff */
780 start
= p_b_symbol(p
);
781 if (SEE('-') && MORE2() && PEEK2() != ']') {
787 finish
= p_b_symbol(p
);
790 /* xxx what about signed chars here... */
791 REQUIRE(start
<= finish
, REG_ERANGE
);
792 for (i
= start
; i
<= finish
; i
++)
799 - p_b_cclass - parse a character-class name and deal with it
800 == static void p_b_cclass(register struct parse *p, register cset *cs);
804 register struct parse
*p
;
807 register char *sp
= p
->next
;
808 register struct cclass
*cp
;
813 while (MORE() && isalpha(PEEK()))
816 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
817 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
819 if (cp
->name
== NULL
) {
820 /* oops, didn't find it */
821 SETERROR(REG_ECTYPE
);
826 while ((c
= *u
++) != '\0')
828 for (u
= cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
833 - p_b_eclass - parse an equivalence-class name and deal with it
834 == static void p_b_eclass(register struct parse *p, register cset *cs);
836 * This implementation is incomplete. xxx
840 register struct parse
*p
;
845 c
= p_b_coll_elem(p
, '=');
850 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
851 == static char p_b_symbol(register struct parse *p);
853 static char /* value of symbol */
855 register struct parse
*p
;
859 REQUIRE(MORE(), REG_EBRACK
);
860 if (!EATTWO('[', '.'))
863 /* collating symbol */
864 value
= p_b_coll_elem(p
, '.');
865 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
870 - p_b_coll_elem - parse a collating-element name and look it up
871 == static char p_b_coll_elem(register struct parse *p, int endc);
873 static char /* value of collating element */
874 p_b_coll_elem(p
, endc
)
875 register struct parse
*p
;
876 int endc
; /* name ended by endc,']' */
878 register char *sp
= p
->next
;
879 register struct cname
*cp
;
883 while (MORE() && !SEETWO(endc
, ']'))
886 SETERROR(REG_EBRACK
);
890 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
891 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
892 return(cp
->code
); /* known name */
894 return(*sp
); /* single character */
895 SETERROR(REG_ECOLLATE
); /* neither */
900 - othercase - return the case counterpart of an alphabetic
901 == static char othercase(int ch);
903 static char /* if no counterpart, return ch */
910 else if (islower(ch
))
912 else /* peculiar, but could happen */
917 - bothcases - emit a dualcase version of a two-case character
918 == static void bothcases(register struct parse *p, int ch);
920 * Boy, is this implementation ever a kludge...
924 register struct parse
*p
;
927 register char *oldnext
= p
->next
;
928 register char *oldend
= p
->end
;
931 assert(othercase(ch
) != ch
); /* p_bracket() would recurse */
938 assert(p
->next
== bracket
+2);
944 - ordinary - emit an ordinary character
945 == static void ordinary(register struct parse *p, register int ch);
949 register struct parse
*p
;
952 register cat_t
*cap
= p
->g
->categories
;
954 if ((p
->g
->cflags
®_ICASE
) && isalpha(ch
) && othercase(ch
) != ch
)
957 EMIT(OCHAR
, (unsigned char)ch
);
959 cap
[ch
] = p
->g
->ncategories
++;
964 - nonnewline - emit REG_NEWLINE version of OANY
965 == static void nonnewline(register struct parse *p);
967 * Boy, is this implementation ever a kludge...
971 register struct parse
*p
;
973 register char *oldnext
= p
->next
;
974 register char *oldend
= p
->end
;
984 assert(p
->next
== bracket
+3);
990 - repeat - generate code for a bounded repetition, recursively if needed
991 == static void repeat(register struct parse *p, sopno start, int from, int to);
994 repeat(p
, start
, from
, to
)
995 register struct parse
*p
;
996 sopno start
; /* operand from here to end of strip */
997 int from
; /* repeated from this number */
998 int to
; /* to this number of times (maybe INFINITY) */
1000 register sopno finish
= HERE();
1003 # define REP(f, t) ((f)*8 + (t))
1004 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1005 register sopno copy
;
1007 if (p
->error
!= 0) /* head off possible runaway recursion */
1012 switch (REP(MAP(from
), MAP(to
))) {
1013 case REP(0, 0): /* must be user doing this */
1014 DROP(finish
-start
); /* drop the operand */
1016 case REP(0, 1): /* as x{1,1}? */
1017 case REP(0, N
): /* as x{1,n}? */
1018 case REP(0, INF
): /* as x{1,}? */
1019 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1020 INSERT(OCH_
, start
); /* offset is wrong... */
1021 repeat(p
, start
+1, 1, to
);
1022 ASTERN(OOR1
, start
);
1023 AHEAD(start
); /* ... fix it */
1026 ASTERN(O_CH
, THERETHERE());
1028 case REP(1, 1): /* trivial case */
1031 case REP(1, N
): /* as x?x{1,n-1} */
1032 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1033 INSERT(OCH_
, start
);
1034 ASTERN(OOR1
, start
);
1036 EMIT(OOR2
, 0); /* offset very wrong... */
1037 AHEAD(THERE()); /* ...so fix it */
1038 ASTERN(O_CH
, THERETHERE());
1039 copy
= dupl(p
, start
+1, finish
+1);
1040 assert(copy
== finish
+4);
1041 repeat(p
, copy
, 1, to
-1);
1043 case REP(1, INF
): /* as x+ */
1044 INSERT(OPLUS_
, start
);
1045 ASTERN(O_PLUS
, start
);
1047 case REP(N
, N
): /* as xx{m-1,n-1} */
1048 copy
= dupl(p
, start
, finish
);
1049 repeat(p
, copy
, from
-1, to
-1);
1051 case REP(N
, INF
): /* as xx{n-1,INF} */
1052 copy
= dupl(p
, start
, finish
);
1053 repeat(p
, copy
, from
-1, to
);
1055 default: /* "can't happen" */
1056 SETERROR(REG_ASSERT
); /* just in case */
1062 - seterr - set an error condition
1063 == static int seterr(register struct parse *p, int e);
1065 static int /* useless but makes type checking happy */
1067 register struct parse
*p
;
1070 if (p
->error
== 0) /* keep earliest error condition */
1072 p
->next
= nuls
; /* try to bring things to a halt */
1074 return(0); /* make the return value well-defined */
1078 - allocset - allocate a set of characters for []
1079 == static cset *allocset(register struct parse *p);
1083 register struct parse
*p
;
1085 register int no
= p
->g
->ncsets
++;
1087 register size_t nbytes
;
1089 register size_t css
= (size_t)p
->g
->csetsize
;
1092 if (no
>= p
->ncsalloc
) { /* need another column of space */
1093 p
->ncsalloc
+= CHAR_BIT
;
1095 assert(nc
% CHAR_BIT
== 0);
1096 nbytes
= nc
/ CHAR_BIT
* css
;
1097 if (p
->g
->sets
== NULL
)
1098 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1100 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1102 if (p
->g
->setbits
== NULL
)
1103 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1105 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1107 /* xxx this isn't right if setbits is now NULL */
1108 for (i
= 0; i
< no
; i
++)
1109 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1111 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1112 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1116 SETERROR(REG_ESPACE
);
1117 /* caller's responsibility not to do set ops */
1121 assert(p
->g
->sets
!= NULL
); /* xxx */
1122 cs
= &p
->g
->sets
[no
];
1123 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1124 cs
->mask
= 1 << ((no
) % CHAR_BIT
);
1133 - freeset - free a now-unused set
1134 == static void freeset(register struct parse *p, register cset *cs);
1138 register struct parse
*p
;
1142 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1143 register size_t css
= (size_t)p
->g
->csetsize
;
1145 for (i
= 0; i
< css
; i
++)
1147 if (cs
== top
-1) /* recover only the easy case */
1152 - freezeset - final processing on a set of characters
1153 == static int freezeset(register struct parse *p, register cset *cs);
1155 * The main task here is merging identical sets. This is usually a waste
1156 * of time (although the hash code minimizes the overhead), but can win
1157 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1158 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1161 static int /* set number */
1163 register struct parse
*p
;
1166 register uch h
= cs
->hash
;
1168 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1170 register size_t css
= (size_t)p
->g
->csetsize
;
1172 /* look for an earlier one which is the same */
1173 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1174 if (cs2
->hash
== h
&& cs2
!= cs
) {
1176 for (i
= 0; i
< css
; i
++)
1177 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1183 if (cs2
< top
) { /* found one */
1188 return((int)(cs
- p
->g
->sets
));
1192 - firstch - return first character in a set (which must have at least one)
1193 == static int firstch(register struct parse *p, register cset *cs);
1195 static int /* character; there is no "none" value */
1197 register struct parse
*p
;
1201 register size_t css
= (size_t)p
->g
->csetsize
;
1203 for (i
= 0; i
< css
; i
++)
1207 return(0); /* arbitrary */
1211 - nch - number of characters in a set
1212 == static int nch(register struct parse *p, register cset *cs);
1216 register struct parse
*p
;
1220 register size_t css
= (size_t)p
->g
->csetsize
;
1223 for (i
= 0; i
< css
; i
++)
1230 - mcadd - add a collating element to a cset
1231 == static void mcadd(register struct parse *p, register cset *cs, \
1232 == register char *cp);
1236 register struct parse
*p
;
1240 register size_t oldend
= cs
->smultis
;
1242 cs
->smultis
+= strlen(cp
) + 1;
1243 if (cs
->multis
== NULL
)
1244 cs
->multis
= malloc(cs
->smultis
);
1246 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1247 if (cs
->multis
== NULL
) {
1248 SETERROR(REG_ESPACE
);
1252 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1253 cs
->multis
[cs
->smultis
- 1] = '\0';
1257 - mcsub - subtract a collating element from a cset
1258 == static void mcsub(register cset *cs, register char *cp);
1265 register char *fp
= mcfind(cs
, cp
);
1266 register size_t len
= strlen(fp
);
1269 (void) memmove(fp
, fp
+ len
+ 1,
1270 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1273 if (cs
->smultis
== 0) {
1279 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1280 assert(cs
->multis
!= NULL
);
1284 - mcin - is a collating element in a cset?
1285 == static int 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);
1306 if (cs
->multis
== NULL
)
1308 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1309 if (strcmp(cp
, p
) == 0)
1315 - mcinvert - invert the list of collating elements in a cset
1316 == static void mcinvert(register struct parse *p, register cset *cs);
1318 * This would have to know the set of possibilities. Implementation
1323 register struct parse
*p
;
1326 assert(cs
->multis
== NULL
); /* xxx */
1330 - mccase - add case counterparts of the list of collating elements in a cset
1331 == static void mccase(register struct parse *p, register cset *cs);
1333 * This would have to know the set of possibilities. Implementation
1338 register struct parse
*p
;
1341 assert(cs
->multis
== NULL
); /* xxx */
1345 - isinsets - is this character in any sets?
1346 == static int isinsets(register struct re_guts *g, int c);
1348 static int /* predicate */
1350 register struct re_guts
*g
;
1355 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1356 register unsigned uc
= (unsigned char)c
;
1358 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1365 - samesets - are these two characters in exactly the same sets?
1366 == static int samesets(register struct re_guts *g, int c1, int c2);
1368 static int /* predicate */
1370 register struct re_guts
*g
;
1376 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1377 register unsigned uc1
= (unsigned char)c1
;
1378 register unsigned uc2
= (unsigned char)c2
;
1380 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1381 if (col
[uc1
] != col
[uc2
])
1387 - categorize - sort out character categories
1388 == static void categorize(struct parse *p, register struct re_guts *g);
1393 register struct re_guts
*g
;
1395 register cat_t
*cats
= g
->categories
;
1400 /* avoid making error situations worse */
1404 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1405 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1406 cat
= g
->ncategories
++;
1408 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1409 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1415 - dupl - emit a duplicate of a bunch of sops
1416 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1418 static sopno
/* start of duplicate */
1419 dupl(p
, start
, finish
)
1420 register struct parse
*p
;
1421 sopno start
; /* from here */
1422 sopno finish
; /* to this less one */
1424 register sopno ret
= HERE();
1425 register sopno len
= finish
- start
;
1427 assert(finish
>= start
);
1430 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1431 assert(p
->ssize
>= p
->slen
+ len
);
1432 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1433 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1439 - doemit - emit a strip operator
1440 == static void doemit(register struct parse *p, sop op, size_t opnd);
1442 * It might seem better to implement this as a macro with a function as
1443 * hard-case backup, but it's just too big and messy unless there are
1444 * some changes to the data structures. Maybe later.
1448 register struct parse
*p
;
1452 /* avoid making error situations worse */
1456 /* deal with oversize operands ("can't happen", more or less) */
1457 assert(opnd
< 1<<OPSHIFT
);
1459 /* deal with undersized strip */
1460 if (p
->slen
>= p
->ssize
)
1461 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1462 assert(p
->slen
< p
->ssize
);
1464 /* finally, it's all reduced to the easy case */
1465 p
->strip
[p
->slen
++] = SOP(op
, opnd
);
1469 - doinsert - insert a sop into the strip
1470 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1473 doinsert(p
, op
, opnd
, pos
)
1474 register struct parse
*p
;
1483 /* avoid making error situations worse */
1488 EMIT(op
, opnd
); /* do checks, ensure space */
1489 assert(HERE() == sn
+1);
1492 /* adjust paren pointers */
1494 for (i
= 1; i
< NPAREN
; i
++) {
1495 if (p
->pbegin
[i
] >= pos
) {
1498 if (p
->pend
[i
] >= pos
) {
1503 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1504 (HERE()-pos
-1)*sizeof(sop
));
1509 - dofwd - complete a forward reference
1510 == static void dofwd(register struct parse *p, sopno pos, sop value);
1513 dofwd(p
, pos
, value
)
1514 register struct parse
*p
;
1518 /* avoid making error situations worse */
1522 assert(value
< 1<<OPSHIFT
);
1523 p
->strip
[pos
] = OP(p
->strip
[pos
]) | value
;
1527 - enlarge - enlarge the strip
1528 == static void enlarge(register struct parse *p, sopno size);
1532 register struct parse
*p
;
1533 register sopno size
;
1537 if (p
->ssize
>= size
)
1540 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1542 SETERROR(REG_ESPACE
);
1550 - stripsnug - compact the strip
1551 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1555 register struct parse
*p
;
1556 register struct re_guts
*g
;
1558 g
->nstates
= p
->slen
;
1559 g
->strip
= (sop
*)realloc((char *)p
->strip
, p
->slen
* sizeof(sop
));
1560 if (g
->strip
== NULL
) {
1561 SETERROR(REG_ESPACE
);
1562 g
->strip
= p
->strip
;
1567 - findmust - fill in must and mlen with longest mandatory literal string
1568 == static void findmust(register struct parse *p, register struct re_guts *g);
1570 * This algorithm could do fancy things like analyzing the operands of |
1571 * for common subsequences. Someday. This code is simple and finds most
1572 * of the interesting cases.
1574 * Note that must and mlen got initialized during setup.
1579 register struct re_guts
*g
;
1583 register sop
*newstart
;
1584 register sopno newlen
;
1589 /* avoid making error situations worse */
1593 /* find the longest OCHAR sequence in strip */
1595 scan
= g
->strip
+ 1;
1599 case OCHAR
: /* sequence member */
1600 if (newlen
== 0) /* new sequence */
1601 newstart
= scan
- 1;
1604 case OPLUS_
: /* things that don't break one */
1608 case OQUEST_
: /* things that must be skipped */
1614 /* assert() interferes w debug printouts */
1615 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1620 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1622 default: /* things that break a sequence */
1623 if (newlen
> g
->mlen
) { /* ends one */
1630 } while (OP(s
) != OEND
);
1632 if (g
->mlen
== 0) /* there isn't one */
1635 /* turn it into a character string */
1636 g
->must
= malloc((size_t)g
->mlen
+ 1);
1637 if (g
->must
== NULL
) { /* argh; just forget it */
1643 for (i
= g
->mlen
; i
> 0; i
--) {
1644 while (OP(s
= *scan
++) != OCHAR
)
1646 assert(cp
< g
->must
+ g
->mlen
);
1647 *cp
++ = (char)OPND(s
);
1649 assert(cp
== g
->must
+ g
->mlen
);
1650 *cp
++ = '\0'; /* just on general principles */
1654 - pluscount - count + nesting
1655 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1657 static sopno
/* nesting depth */
1660 register struct re_guts
*g
;
1664 register sopno plusnest
= 0;
1665 register sopno maxnest
= 0;
1668 return(0); /* there may not be an OEND */
1670 scan
= g
->strip
+ 1;
1678 if (plusnest
> maxnest
)
1683 } while (OP(s
) != OEND
);