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 /* ========= begin header generated by ./mkh ========= */
81 /* === regcomp.c === */
82 static void p_ere
__P((struct parse
*p
, int stop
));
83 static void p_ere_exp
__P((struct parse
*p
));
84 static void p_str
__P((struct parse
*p
));
85 static void p_bre
__P((struct parse
*p
, int end1
, int end2
));
86 static int p_simp_re
__P((struct parse
*p
, int starordinary
));
87 static int p_count
__P((struct parse
*p
));
88 static void p_bracket
__P((struct parse
*p
));
89 static void p_b_term
__P((struct parse
*p
, cset
*cs
));
90 static void p_b_cclass
__P((struct parse
*p
, cset
*cs
));
91 static void p_b_eclass
__P((struct parse
*p
, cset
*cs
));
92 static char p_b_symbol
__P((struct parse
*p
));
93 static char p_b_coll_elem
__P((struct parse
*p
, int endc
));
94 static char othercase
__P((int ch
));
95 static void bothcases
__P((struct parse
*p
, int ch
));
96 static void ordinary
__P((struct parse
*p
, int ch
));
97 static void nonnewline
__P((struct parse
*p
));
98 static void repeat
__P((struct parse
*p
, sopno start
, int from
, int to
));
99 static int seterr
__P((struct parse
*p
, int e
));
100 static cset
*allocset
__P((struct parse
*p
));
101 static void freeset
__P((struct parse
*p
, cset
*cs
));
102 static int freezeset
__P((struct parse
*p
, cset
*cs
));
103 static int firstch
__P((struct parse
*p
, cset
*cs
));
104 static int nch
__P((struct parse
*p
, cset
*cs
));
105 static void mcadd
__P((struct parse
*p
, cset
*cs
, char *cp
));
106 static void mcsub
__P((cset
*cs
, char *cp
));
107 static int mcin
__P((cset
*cs
, char *cp
));
108 static char *mcfind
__P((cset
*cs
, char *cp
));
109 static void mcinvert
__P((struct parse
*p
, cset
*cs
));
110 static void mccase
__P((struct parse
*p
, cset
*cs
));
111 static int isinsets
__P((struct re_guts
*g
, int c
));
112 static int samesets
__P((struct re_guts
*g
, int c1
, int c2
));
113 static void categorize
__P((struct parse
*p
, struct re_guts
*g
));
114 static sopno dupl
__P((struct parse
*p
, sopno start
, sopno finish
));
115 static void doemit
__P((struct parse
*p
, sop op
, size_t opnd
));
116 static void doinsert
__P((struct parse
*p
, sop op
, size_t opnd
, sopno pos
));
117 static void dofwd
__P((struct parse
*p
, sopno pos
, sop value
));
118 static void enlarge
__P((struct parse
*p
, sopno size
));
119 static void stripsnug
__P((struct parse
*p
, struct re_guts
*g
));
120 static void findmust
__P((struct parse
*p
, struct re_guts
*g
));
121 static sopno pluscount
__P((struct parse
*p
, struct re_guts
*g
));
126 /* ========= end header generated by ./mkh ========= */
128 static char nuls
[10]; /* place to point scanner in event of error */
131 * macros for use with parse structure
132 * BEWARE: these know that the parse structure is named `p' !!!
134 #define PEEK() (*p->next)
135 #define PEEK2() (*(p->next+1))
136 #define MORE() (p->next < p->end)
137 #define MORE2() (p->next+1 < p->end)
138 #define SEE(c) (MORE() && PEEK() == (c))
139 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
140 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
141 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
142 #define NEXT() (p->next++)
143 #define NEXT2() (p->next += 2)
144 #define NEXTn(n) (p->next += (n))
145 #define GETNEXT() (*p->next++)
146 #define SETERROR(e) seterr(p, (e))
147 #define REQUIRE(co, e) ((void) ((co) || SETERROR(e)))
148 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
149 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
150 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
151 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
152 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
153 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
154 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
155 #define HERE() (p->slen)
156 #define THERE() (p->slen - 1)
157 #define THERETHERE() (p->slen - 2)
158 #define DROP(n) (p->slen -= (n))
161 static int never
= 0; /* for use in asserts; shuts lint up */
163 #define never 0 /* some <assert.h>s have bugs too */
167 - regcomp - interface for parser and compilation
168 = extern int regcomp(regex_t *, const char *, int);
169 = #define REG_BASIC 0000
170 = #define REG_EXTENDED 0001
171 = #define REG_ICASE 0002
172 = #define REG_NOSUB 0004
173 = #define REG_NEWLINE 0010
174 = #define REG_NOSPEC 0020
175 = #define REG_PEND 0040
176 = #define REG_DUMP 0200
178 int __stdcall
/* 0 success, otherwise REG_something */
179 regcomp(regex_t
*preg
, const char *pattern
, int cflags
)
182 register struct re_guts
*g
;
183 register struct parse
*p
= &pa
;
187 # define GOODFLAGS(f) (f)
189 # define GOODFLAGS(f) ((f)&~REG_DUMP)
192 cflags
= GOODFLAGS(cflags
);
193 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
196 if (cflags
®_PEND
) {
197 if (preg
->re_endp
< pattern
)
199 len
= preg
->re_endp
- pattern
;
201 len
= strlen((char *)pattern
);
203 /* do the mallocs early so failure handling is easy */
204 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
205 (NC
-1)*sizeof(cat_t
));
208 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
209 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
211 if (p
->strip
== NULL
) {
218 p
->next
= (char *)pattern
; /* convenience; we do not modify it */
219 p
->end
= p
->next
+ len
;
222 for (i
= 0; i
< NPAREN
; i
++) {
237 g
->ncategories
= 1; /* category 0 is "everything else" */
238 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
239 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
244 g
->firststate
= THERE();
245 if (cflags
®_EXTENDED
)
247 else if (cflags
®_NOSPEC
)
252 g
->laststate
= THERE();
254 /* tidy up loose ends and fill things in */
258 g
->nplus
= pluscount(p
, g
);
260 preg
->re_nsub
= g
->nsub
;
262 preg
->re_magic
= MAGIC1
;
264 /* not debugging, so can't rely on the assert() in regexec() */
266 SETERROR(REG_ASSERT
);
269 /* win or lose, we're done */
270 if (p
->error
!= 0) /* lose */
276 - p_ere - ERE parser top level, concatenation and alternation
277 == static void p_ere(register struct parse *p, int stop);
280 p_ere(struct parse
*p
,
281 int stop
) /* character this ERE should end at */
284 register sopno prevback
;
285 register sopno prevfwd
;
287 register int first
= 1; /* is this the first alternative? */
290 /* do a bunch of concatenated expressions */
292 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
294 REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
297 break; /* NOTE BREAK OUT */
300 INSERT(OCH_
, conc
); /* offset is wrong */
305 ASTERN(OOR1
, prevback
);
307 AHEAD(prevfwd
); /* fix previous offset */
309 EMIT(OOR2
, 0); /* offset is very wrong */
312 if (!first
) { /* tail-end fixups */
314 ASTERN(O_CH
, prevback
);
317 assert(!MORE() || SEE(stop
));
321 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
322 == static void p_ere_exp(register struct parse *p);
325 p_ere_exp(struct parse
*p
)
331 register sopno subno
;
334 assert(MORE()); /* caller should have ensured this */
340 REQUIRE(MORE(), REG_EPAREN
);
344 p
->pbegin
[subno
] = HERE();
345 EMIT(OLPAREN
, subno
);
348 if (subno
< NPAREN
) {
349 p
->pend
[subno
] = HERE();
350 assert(p
->pend
[subno
] != 0);
352 EMIT(ORPAREN
, subno
);
353 MUSTEAT(')', REG_EPAREN
);
355 #ifndef POSIX_MISTAKE
356 case ')': /* happens only if no current unmatched ( */
358 * You may ask, why the ifndef? Because I didn't notice
359 * this until slightly too late for 1003.2, and none of the
360 * other 1003.2 regular-expression reviewers noticed it at
361 * all. So an unmatched ) is legal POSIX, at least until
362 * we can get it fixed.
364 SETERROR(REG_EPAREN
);
369 p
->g
->iflags
|= USEBOL
;
375 p
->g
->iflags
|= USEEOL
;
384 SETERROR(REG_BADRPT
);
387 if (p
->g
->cflags
®_NEWLINE
)
396 REQUIRE(MORE(), REG_EESCAPE
);
400 case '{': /* okay as ordinary except if digit follows */
401 REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT
);
411 /* we call { a repetition if followed by a digit */
412 if (!( c
== '*' || c
== '+' || c
== '?' ||
413 (c
== '{' && MORE2() && isdigit(PEEK2())) ))
414 return; /* no repetition, we're done */
417 REQUIRE(!wascaret
, REG_BADRPT
);
419 case '*': /* implemented as +? */
420 /* this case does not require the (y|) trick, noKLUDGE */
423 INSERT(OQUEST_
, pos
);
424 ASTERN(O_QUEST
, pos
);
431 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
432 INSERT(OCH_
, pos
); /* offset slightly wrong */
433 ASTERN(OOR1
, pos
); /* this one's right */
434 AHEAD(pos
); /* fix the OCH_ */
435 EMIT(OOR2
, 0); /* offset very wrong... */
436 AHEAD(THERE()); /* ...so fix it */
437 ASTERN(O_CH
, THERETHERE());
442 if (isdigit(PEEK())) {
444 REQUIRE(count
<= count2
, REG_BADBR
);
445 } else /* single number with comma */
447 } else /* just a single number */
449 repeat(p
, pos
, count
, count2
);
450 if (!EAT('}')) { /* error heuristics */
451 while (MORE() && PEEK() != '}')
453 REQUIRE(MORE(), REG_EBRACE
);
462 if (!( c
== '*' || c
== '+' || c
== '?' ||
463 (c
== '{' && MORE2() && isdigit(PEEK2())) ) )
465 SETERROR(REG_BADRPT
);
469 - p_str - string (no metacharacters) "parser"
470 == static void p_str(register struct parse *p);
473 p_str(struct parse
*p
)
475 REQUIRE(MORE(), REG_EMPTY
);
477 ordinary(p
, GETNEXT());
481 - p_bre - BRE parser top level, anchoring and concatenation
482 == static void p_bre(register struct parse *p, register int end1, \
483 == register int end2);
484 * Giving end1 as OUT essentially eliminates the end1/end2 check.
486 * This implementation is a bit of a kludge, in that a trailing $ is first
487 * taken as an ordinary character and then revised to be an anchor. The
488 * only undesirable side effect is that '$' gets included as a character
489 * category in such cases. This is fairly harmless; not worth fixing.
490 * The amount of lookahead needed to avoid this kludge is excessive.
493 p_bre(struct parse
*p
,
494 int end1
, /* first terminating character */
495 int end2
) /* second terminating character */
497 register sopno start
= HERE();
498 register int first
= 1; /* first subexpression? */
499 register int wasdollar
= 0;
503 p
->g
->iflags
|= USEBOL
;
506 while (MORE() && !SEETWO(end1
, end2
)) {
507 wasdollar
= p_simp_re(p
, first
);
510 if (wasdollar
) { /* oops, that was a trailing anchor */
513 p
->g
->iflags
|= USEEOL
;
517 REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
521 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
522 == static int p_simp_re(register struct parse *p, int starordinary);
524 static int /* was the simple RE an unbackslashed $? */
525 p_simp_re(struct parse
*p
,
526 int starordinary
) /* is a leading * an ordinary character? */
533 register sopno subno
;
534 # define BACKSL (1<<CHAR_BIT)
536 pos
= HERE(); /* repetion op, if any, covers from here */
538 assert(MORE()); /* caller should have ensured this */
541 REQUIRE(MORE(), REG_EESCAPE
);
542 c
= BACKSL
| (unsigned char)GETNEXT();
546 if (p
->g
->cflags
®_NEWLINE
)
555 SETERROR(REG_BADRPT
);
561 p
->pbegin
[subno
] = HERE();
562 EMIT(OLPAREN
, subno
);
563 /* the MORE here is an error heuristic */
564 if (MORE() && !SEETWO('\\', ')'))
566 if (subno
< NPAREN
) {
567 p
->pend
[subno
] = HERE();
568 assert(p
->pend
[subno
] != 0);
570 EMIT(ORPAREN
, subno
);
571 REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
573 case BACKSL
|')': /* should not get here -- must be user */
575 SETERROR(REG_EPAREN
);
586 i
= (c
&~BACKSL
) - '0';
588 if (p
->pend
[i
] != 0) {
589 assert(i
<= p
->g
->nsub
);
591 assert(p
->pbegin
[i
] != 0);
592 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
593 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
594 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
597 SETERROR(REG_ESUBREG
);
601 REQUIRE(starordinary
, REG_BADRPT
);
604 ordinary(p
, c
&~ BACKSL
);
608 if (EAT('*')) { /* implemented as +? */
609 /* this case does not require the (y|) trick, noKLUDGE */
612 INSERT(OQUEST_
, pos
);
613 ASTERN(O_QUEST
, pos
);
614 } else if (EATTWO('\\', '{')) {
617 if (MORE() && isdigit(PEEK())) {
619 REQUIRE(count
<= count2
, REG_BADBR
);
620 } else /* single number with comma */
622 } else /* just a single number */
624 repeat(p
, pos
, count
, count2
);
625 if (!EATTWO('\\', '}')) { /* error heuristics */
626 while (MORE() && !SEETWO('\\', '}'))
628 REQUIRE(MORE(), REG_EBRACE
);
631 } else if (c
== (unsigned char)'$') /* $ (but not \$) ends it */
638 - p_count - parse a repetition count
639 == static int p_count(register struct parse *p);
641 static int /* the value */
642 p_count(struct parse
*p
)
644 register int count
= 0;
645 register int ndigits
= 0;
647 while (MORE() && isdigit(PEEK()) && count
<= DUPMAX
) {
648 count
= count
*10 + (GETNEXT() - '0');
652 REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
657 - p_bracket - parse a bracketed character list
658 == static void p_bracket(register struct parse *p);
660 * Note a significant property of this code: if the allocset() did SETERROR,
661 * no set operations are done.
664 p_bracket(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);
737 p_b_term(struct parse
*p
, cset
*cs
)
740 register char start
, finish
;
743 /* classify what we've got */
744 switch ((MORE()) ? PEEK() : '\0') {
746 c
= (MORE2()) ? PEEK2() : '\0';
749 SETERROR(REG_ERANGE
);
750 return; /* NOTE RETURN */
758 case ':': /* character class */
760 REQUIRE(MORE(), REG_EBRACK
);
762 REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
764 REQUIRE(MORE(), REG_EBRACK
);
765 REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
767 case '=': /* equivalence class */
769 REQUIRE(MORE(), REG_EBRACK
);
771 REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
773 REQUIRE(MORE(), REG_EBRACK
);
774 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
776 default: /* symbol, ordinary character, or range */
777 /* xxx revision needed for multichar stuff */
778 start
= p_b_symbol(p
);
779 if (SEE('-') && MORE2() && PEEK2() != ']') {
785 finish
= p_b_symbol(p
);
788 /* xxx what about signed chars here... */
789 REQUIRE(start
<= finish
, REG_ERANGE
);
790 for (i
= start
; i
<= finish
; i
++)
797 - p_b_cclass - parse a character-class name and deal with it
798 == static void p_b_cclass(register struct parse *p, register cset *cs);
801 p_b_cclass(struct parse
*p
, cset
*cs
)
803 register char *sp
= p
->next
;
804 register struct cclass
*cp
;
809 while (MORE() && isalpha(PEEK()))
812 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
813 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
815 if (cp
->name
== NULL
) {
816 /* oops, didn't find it */
817 SETERROR(REG_ECTYPE
);
822 while ((c
= *u
++) != '\0')
824 for (u
= cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
829 - p_b_eclass - parse an equivalence-class name and deal with it
830 == static void p_b_eclass(register struct parse *p, register cset *cs);
832 * This implementation is incomplete. xxx
835 p_b_eclass(struct parse
*p
, cset
*cs
)
839 c
= p_b_coll_elem(p
, '=');
844 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
845 == static char p_b_symbol(register struct parse *p);
847 static char /* value of symbol */
848 p_b_symbol(struct parse
*p
)
852 REQUIRE(MORE(), REG_EBRACK
);
853 if (!EATTWO('[', '.'))
856 /* collating symbol */
857 value
= p_b_coll_elem(p
, '.');
858 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
863 - p_b_coll_elem - parse a collating-element name and look it up
864 == static char p_b_coll_elem(register struct parse *p, int endc);
866 static char /* value of collating element */
867 p_b_coll_elem(struct parse
*p
,
868 int endc
) /* name ended by endc,']' */
870 register char *sp
= p
->next
;
871 register struct cname
*cp
;
874 while (MORE() && !SEETWO(endc
, ']'))
877 SETERROR(REG_EBRACK
);
881 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
882 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
883 return(cp
->code
); /* known name */
885 return(*sp
); /* single character */
886 SETERROR(REG_ECOLLATE
); /* neither */
891 - othercase - return the case counterpart of an alphabetic
892 == static char othercase(int ch);
894 static char /* if no counterpart, return ch */
900 else if (islower(ch
))
902 else /* peculiar, but could happen */
907 - bothcases - emit a dualcase version of a two-case character
908 == static void bothcases(register struct parse *p, int ch);
910 * Boy, is this implementation ever a kludge...
913 bothcases(struct parse
*p
, int ch
)
915 register char *oldnext
= p
->next
;
916 register char *oldend
= p
->end
;
919 assert(othercase(ch
) != ch
); /* p_bracket() would recurse */
926 assert(p
->next
== bracket
+2);
932 - ordinary - emit an ordinary character
933 == static void ordinary(register struct parse *p, register int ch);
936 ordinary(struct parse
*p
, int ch
)
938 register cat_t
*cap
= p
->g
->categories
;
940 if ((p
->g
->cflags
®_ICASE
) && isalpha(ch
) && othercase(ch
) != ch
)
943 EMIT(OCHAR
, (unsigned char)ch
);
945 cap
[ch
] = p
->g
->ncategories
++;
950 - nonnewline - emit REG_NEWLINE version of OANY
951 == static void nonnewline(register struct parse *p);
953 * Boy, is this implementation ever a kludge...
956 nonnewline(struct parse
*p
)
958 register char *oldnext
= p
->next
;
959 register char *oldend
= p
->end
;
969 assert(p
->next
== bracket
+3);
975 - repeat - generate code for a bounded repetition, recursively if needed
976 == static void repeat(register struct parse *p, sopno start, int from, int to);
979 repeat(struct parse
*p
,
980 sopno start
, /* operand from here to end of strip */
981 int from
, /* repeated from this number */
982 int to
) /* to this number of times (maybe INFINITY) */
984 register sopno finish
= HERE();
987 # define REP(f, t) ((f)*8 + (t))
988 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
991 if (p
->error
!= 0) /* head off possible runaway recursion */
996 switch (REP(MAP(from
), MAP(to
))) {
997 case REP(0, 0): /* must be user doing this */
998 DROP(finish
-start
); /* drop the operand */
1000 case REP(0, 1): /* as x{1,1}? */
1001 case REP(0, N
): /* as x{1,n}? */
1002 case REP(0, INF
): /* as x{1,}? */
1003 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1004 INSERT(OCH_
, start
); /* offset is wrong... */
1005 repeat(p
, start
+1, 1, to
);
1006 ASTERN(OOR1
, start
);
1007 AHEAD(start
); /* ... fix it */
1010 ASTERN(O_CH
, THERETHERE());
1012 case REP(1, 1): /* trivial case */
1015 case REP(1, N
): /* as x?x{1,n-1} */
1016 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1017 INSERT(OCH_
, start
);
1018 ASTERN(OOR1
, start
);
1020 EMIT(OOR2
, 0); /* offset very wrong... */
1021 AHEAD(THERE()); /* ...so fix it */
1022 ASTERN(O_CH
, THERETHERE());
1023 copy
= dupl(p
, start
+1, finish
+1);
1024 assert(copy
== finish
+4);
1025 repeat(p
, copy
, 1, to
-1);
1027 case REP(1, INF
): /* as x+ */
1028 INSERT(OPLUS_
, start
);
1029 ASTERN(O_PLUS
, start
);
1031 case REP(N
, N
): /* as xx{m-1,n-1} */
1032 copy
= dupl(p
, start
, finish
);
1033 repeat(p
, copy
, from
-1, to
-1);
1035 case REP(N
, INF
): /* as xx{n-1,INF} */
1036 copy
= dupl(p
, start
, finish
);
1037 repeat(p
, copy
, from
-1, to
);
1039 default: /* "can't happen" */
1040 SETERROR(REG_ASSERT
); /* just in case */
1046 - seterr - set an error condition
1047 == static int seterr(register struct parse *p, int e);
1049 static int /* useless but makes type checking happy */
1050 seterr(struct parse
*p
, int e
)
1052 if (p
->error
== 0) /* keep earliest error condition */
1054 p
->next
= nuls
; /* try to bring things to a halt */
1056 return(0); /* make the return value well-defined */
1060 - allocset - allocate a set of characters for []
1061 == static cset *allocset(register struct parse *p);
1064 allocset(struct parse
*p
)
1066 register int no
= p
->g
->ncsets
++;
1068 register size_t nbytes
;
1070 register size_t css
= (size_t)p
->g
->csetsize
;
1073 if (no
>= p
->ncsalloc
) { /* need another column of space */
1074 p
->ncsalloc
+= CHAR_BIT
;
1076 assert(nc
% CHAR_BIT
== 0);
1077 nbytes
= nc
/ CHAR_BIT
* css
;
1078 if (p
->g
->sets
== NULL
)
1079 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1081 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1083 if (p
->g
->setbits
== NULL
)
1084 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1086 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1088 /* xxx this isn't right if setbits is now NULL */
1089 for (i
= 0; i
< no
; i
++)
1090 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1092 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1093 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1097 SETERROR(REG_ESPACE
);
1098 /* caller's responsibility not to do set ops */
1102 assert(p
->g
->sets
!= NULL
); /* xxx */
1103 cs
= &p
->g
->sets
[no
];
1104 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1105 cs
->mask
= 1 << ((no
) % CHAR_BIT
);
1114 - freeset - free a now-unused set
1115 == static void freeset(register struct parse *p, register cset *cs);
1118 freeset(struct parse
*p
, cset
*cs
)
1121 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1122 register size_t css
= (size_t)p
->g
->csetsize
;
1124 for (i
= 0; i
< css
; i
++)
1126 if (cs
== top
-1) /* recover only the easy case */
1131 - freezeset - final processing on a set of characters
1132 == static int freezeset(register struct parse *p, register cset *cs);
1134 * The main task here is merging identical sets. This is usually a waste
1135 * of time (although the hash code minimizes the overhead), but can win
1136 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1137 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1140 static int /* set number */
1141 freezeset(struct parse
*p
, cset
*cs
)
1143 register uch h
= cs
->hash
;
1145 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1147 register size_t css
= (size_t)p
->g
->csetsize
;
1149 /* look for an earlier one which is the same */
1150 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1151 if (cs2
->hash
== h
&& cs2
!= cs
) {
1153 for (i
= 0; i
< css
; i
++)
1154 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1160 if (cs2
< top
) { /* found one */
1165 return((int)(cs
- p
->g
->sets
));
1169 - firstch - return first character in a set (which must have at least one)
1170 == static int firstch(register struct parse *p, register cset *cs);
1172 static int /* character; there is no "none" value */
1173 firstch(struct parse
*p
, cset
*cs
)
1176 register size_t css
= (size_t)p
->g
->csetsize
;
1178 for (i
= 0; i
< css
; i
++)
1182 return(0); /* arbitrary */
1186 - nch - number of characters in a set
1187 == static int nch(register struct parse *p, register cset *cs);
1190 nch(struct parse
*p
, cset
*cs
)
1193 register size_t css
= (size_t)p
->g
->csetsize
;
1196 for (i
= 0; i
< css
; i
++)
1203 - mcadd - add a collating element to a cset
1204 == static void mcadd(register struct parse *p, register cset *cs, \
1205 == register char *cp);
1208 mcadd(struct parse
*p
, cset
*cs
, char *cp
)
1210 register size_t oldend
= cs
->smultis
;
1212 cs
->smultis
+= strlen(cp
) + 1;
1213 if (cs
->multis
== NULL
)
1214 cs
->multis
= malloc(cs
->smultis
);
1216 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1217 if (cs
->multis
== NULL
) {
1218 SETERROR(REG_ESPACE
);
1222 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1223 cs
->multis
[cs
->smultis
- 1] = '\0';
1227 - mcsub - subtract a collating element from a cset
1228 == static void mcsub(register cset *cs, register char *cp);
1231 mcsub(cset
*cs
, char *cp
)
1233 register char *fp
= mcfind(cs
, cp
);
1234 register size_t len
= strlen(fp
);
1237 (void) memmove(fp
, fp
+ len
+ 1,
1238 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1241 if (cs
->smultis
== 0) {
1247 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1248 assert(cs
->multis
!= NULL
);
1252 - mcin - is a collating element in a cset?
1253 == static int mcin(register cset *cs, register char *cp);
1256 mcin(cset
*cs
, char *cp
)
1258 return(mcfind(cs
, cp
) != NULL
);
1262 - mcfind - find a collating element in a cset
1263 == static char *mcfind(register cset *cs, register char *cp);
1266 mcfind(cset
*cs
, char *cp
)
1270 if (cs
->multis
== NULL
)
1272 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1273 if (strcmp(cp
, p
) == 0)
1279 - mcinvert - invert the list of collating elements in a cset
1280 == static void mcinvert(register struct parse *p, register cset *cs);
1282 * This would have to know the set of possibilities. Implementation
1286 mcinvert(struct parse
*p
, cset
*cs
)
1288 assert(cs
->multis
== NULL
); /* xxx */
1292 - mccase - add case counterparts of the list of collating elements in a cset
1293 == static void mccase(register struct parse *p, register cset *cs);
1295 * This would have to know the set of possibilities. Implementation
1299 mccase(struct parse
*p
, cset
*cs
)
1301 assert(cs
->multis
== NULL
); /* xxx */
1305 - isinsets - is this character in any sets?
1306 == static int isinsets(register struct re_guts *g, int c);
1308 static int /* predicate */
1309 isinsets(struct re_guts
*g
, int c
)
1313 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1314 register unsigned uc
= (unsigned char)c
;
1316 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1323 - samesets - are these two characters in exactly the same sets?
1324 == static int samesets(register struct re_guts *g, int c1, int c2);
1326 static int /* predicate */
1327 samesets(struct re_guts
*g
, int c1
, int c2
)
1331 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1332 register unsigned uc1
= (unsigned char)c1
;
1333 register unsigned uc2
= (unsigned char)c2
;
1335 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1336 if (col
[uc1
] != col
[uc2
])
1342 - categorize - sort out character categories
1343 == static void categorize(struct parse *p, register struct re_guts *g);
1346 categorize(struct parse
*p
, struct re_guts
*g
)
1348 register cat_t
*cats
= g
->categories
;
1353 /* avoid making error situations worse */
1357 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1358 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1359 cat
= g
->ncategories
++;
1361 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1362 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1368 - dupl - emit a duplicate of a bunch of sops
1369 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1371 static sopno
/* start of duplicate */
1372 dupl(struct parse
*p
,
1373 sopno start
, /* from here */
1374 sopno finish
) /* to this less one */
1376 register sopno ret
= HERE();
1377 register sopno len
= finish
- start
;
1379 assert(finish
>= start
);
1382 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1383 assert(p
->ssize
>= p
->slen
+ len
);
1384 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1385 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1391 - doemit - emit a strip operator
1392 == static void doemit(register struct parse *p, sop op, size_t opnd);
1394 * It might seem better to implement this as a macro with a function as
1395 * hard-case backup, but it's just too big and messy unless there are
1396 * some changes to the data structures. Maybe later.
1399 doemit(struct parse
*p
, sop op
, size_t opnd
)
1401 /* avoid making error situations worse */
1405 /* deal with oversize operands ("can't happen", more or less) */
1406 assert(opnd
< 1<<OPSHIFT
);
1408 /* deal with undersized strip */
1409 if (p
->slen
>= p
->ssize
)
1410 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1411 assert(p
->slen
< p
->ssize
);
1413 /* finally, it's all reduced to the easy case */
1414 p
->strip
[p
->slen
++] = SOP(op
, opnd
);
1418 - doinsert - insert a sop into the strip
1419 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1422 doinsert(struct parse
*p
, sop op
, size_t opnd
, sopno pos
)
1428 /* avoid making error situations worse */
1433 EMIT(op
, opnd
); /* do checks, ensure space */
1434 assert(HERE() == sn
+1);
1437 /* adjust paren pointers */
1439 for (i
= 1; i
< NPAREN
; i
++) {
1440 if (p
->pbegin
[i
] >= pos
) {
1443 if (p
->pend
[i
] >= pos
) {
1448 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1449 (HERE()-pos
-1)*sizeof(sop
));
1454 - dofwd - complete a forward reference
1455 == static void dofwd(register struct parse *p, sopno pos, sop value);
1458 dofwd(struct parse
*p
, sopno pos
, sop value
)
1460 /* avoid making error situations worse */
1464 assert(value
< 1<<OPSHIFT
);
1465 p
->strip
[pos
] = OP(p
->strip
[pos
]) | value
;
1469 - enlarge - enlarge the strip
1470 == static void enlarge(register struct parse *p, sopno size);
1473 enlarge(struct parse
*p
, sopno size
)
1477 if (p
->ssize
>= size
)
1480 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1482 SETERROR(REG_ESPACE
);
1490 - stripsnug - compact the strip
1491 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1494 stripsnug(struct parse
*p
, struct re_guts
*g
)
1496 g
->nstates
= p
->slen
;
1497 g
->strip
= (sop
*)realloc((char *)p
->strip
, p
->slen
* sizeof(sop
));
1498 if (g
->strip
== NULL
) {
1499 SETERROR(REG_ESPACE
);
1500 g
->strip
= p
->strip
;
1505 - findmust - fill in must and mlen with longest mandatory literal string
1506 == static void findmust(register struct parse *p, register struct re_guts *g);
1508 * This algorithm could do fancy things like analyzing the operands of |
1509 * for common subsequences. Someday. This code is simple and finds most
1510 * of the interesting cases.
1512 * Note that must and mlen got initialized during setup.
1515 findmust(struct parse
*p
, struct re_guts
*g
)
1519 register sop
*newstart
;
1520 register sopno newlen
;
1525 /* avoid making error situations worse */
1529 /* find the longest OCHAR sequence in strip */
1531 scan
= g
->strip
+ 1;
1535 case OCHAR
: /* sequence member */
1536 if (newlen
== 0) /* new sequence */
1537 newstart
= scan
- 1;
1540 case OPLUS_
: /* things that don't break one */
1544 case OQUEST_
: /* things that must be skipped */
1550 /* assert() interferes w debug printouts */
1551 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1556 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1558 default: /* things that break a sequence */
1559 if (newlen
> g
->mlen
) { /* ends one */
1566 } while (OP(s
) != OEND
);
1568 if (g
->mlen
== 0) /* there isn't one */
1571 /* turn it into a character string */
1572 g
->must
= malloc((size_t)g
->mlen
+ 1);
1573 if (g
->must
== NULL
) { /* argh; just forget it */
1579 for (i
= g
->mlen
; i
> 0; i
--) {
1580 while (OP(s
= *scan
++) != OCHAR
)
1582 assert(cp
< g
->must
+ g
->mlen
);
1583 *cp
++ = (char)OPND(s
);
1585 assert(cp
== g
->must
+ g
->mlen
);
1586 *cp
++ = '\0'; /* just on general principles */
1590 - pluscount - count + nesting
1591 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1593 static sopno
/* nesting depth */
1594 pluscount(struct parse
*p
, struct re_guts
*g
)
1598 register sopno plusnest
= 0;
1599 register sopno maxnest
= 0;
1602 return(0); /* there may not be an OEND */
1604 scan
= g
->strip
+ 1;
1612 if (plusnest
> maxnest
)
1617 } while (OP(s
) != OEND
);