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
7 * Henry Spencer of the University of Toronto.
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.4 (Berkeley) 3/19/94
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid
[] = "@(#)regcomp.c 8.4 (Berkeley) 3/19/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 RCHAR_T
*next
; /* next character in RE */
64 RCHAR_T
*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 RCHAR_T 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) ((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 RCHAR_T *, 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 /* 0 success, otherwise REG_something */
179 regcomp(preg
, pattern
, cflags
)
181 const RCHAR_T
*pattern
;
185 register struct re_guts
*g
;
186 register struct parse
*p
= &pa
;
190 # define GOODFLAGS(f) (f)
192 # define GOODFLAGS(f) ((f)&~REG_DUMP)
195 cflags
= GOODFLAGS(cflags
);
196 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
199 if (cflags
®_PEND
) {
200 if (preg
->re_endp
< pattern
)
202 len
= preg
->re_endp
- pattern
;
204 len
= STRLEN(pattern
);
206 /* do the mallocs early so failure handling is easy */
207 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
208 (NC
-1)*sizeof(cat_t
));
211 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
212 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
214 if (p
->strip
== NULL
) {
221 p
->next
= (RCHAR_T
*)pattern
; /* convenience; we do not modify it */
222 p
->end
= p
->next
+ len
;
225 for (i
= 0; i
< NPAREN
; i
++) {
241 g
->ncategories
= 1; /* category 0 is "everything else" */
242 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
243 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
249 g
->firststate
= THERE();
250 if (cflags
®_EXTENDED
)
252 else if (cflags
®_NOSPEC
)
257 g
->laststate
= THERE();
259 /* tidy up loose ends and fill things in */
263 g
->nplus
= pluscount(p
, g
);
265 preg
->re_nsub
= g
->nsub
;
267 preg
->re_magic
= MAGIC1
;
269 /* not debugging, so can't rely on the assert() in regexec() */
271 SETERROR(REG_ASSERT
);
274 /* win or lose, we're done */
275 if (p
->error
!= 0) /* lose */
281 - p_ere - ERE parser top level, concatenation and alternation
282 == static void p_ere(register struct parse *p, int stop);
286 register struct parse
*p
;
287 int stop
; /* character this ERE should end at */
290 register sopno prevback
;
291 register sopno prevfwd
;
293 register int first
= 1; /* is this the first alternative? */
296 /* do a bunch of concatenated expressions */
298 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
300 REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
303 break; /* NOTE BREAK OUT */
306 INSERT(OCH_
, conc
); /* offset is wrong */
311 ASTERN(OOR1
, prevback
);
313 AHEAD(prevfwd
); /* fix previous offset */
315 EMIT(OOR2
, 0); /* offset is very wrong */
318 if (!first
) { /* tail-end fixups */
320 ASTERN(O_CH
, prevback
);
323 assert(!MORE() || SEE(stop
));
327 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
328 == static void p_ere_exp(register struct parse *p);
332 register struct parse
*p
;
338 register sopno subno
;
341 assert(MORE()); /* caller should have ensured this */
347 REQUIRE(MORE(), REG_EPAREN
);
351 p
->pbegin
[subno
] = HERE();
352 EMIT(OLPAREN
, subno
);
355 if (subno
< NPAREN
) {
356 p
->pend
[subno
] = HERE();
357 assert(p
->pend
[subno
] != 0);
359 EMIT(ORPAREN
, subno
);
360 MUSTEAT(')', REG_EPAREN
);
362 #ifndef POSIX_MISTAKE
363 case ')': /* happens only if no current unmatched ( */
365 * You may ask, why the ifndef? Because I didn't notice
366 * this until slightly too late for 1003.2, and none of the
367 * other 1003.2 regular-expression reviewers noticed it at
368 * all. So an unmatched ) is legal POSIX, at least until
369 * we can get it fixed.
371 SETERROR(REG_EPAREN
);
376 p
->g
->iflags
|= USEBOL
;
382 p
->g
->iflags
|= USEEOL
;
391 SETERROR(REG_BADRPT
);
394 if (p
->g
->cflags
®_NEWLINE
)
403 REQUIRE(MORE(), REG_EESCAPE
);
407 case '{': /* okay as ordinary except if digit follows */
408 REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT
);
418 /* we call { a repetition if followed by a digit */
419 if (!( c
== '*' || c
== '+' || c
== '?' ||
420 (c
== '{' && MORE2() && isdigit(PEEK2())) ))
421 return; /* no repetition, we're done */
424 REQUIRE(!wascaret
, REG_BADRPT
);
426 case '*': /* implemented as +? */
427 /* this case does not require the (y|) trick, noKLUDGE */
430 INSERT(OQUEST_
, pos
);
431 ASTERN(O_QUEST
, pos
);
438 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
439 INSERT(OCH_
, pos
); /* offset slightly wrong */
440 ASTERN(OOR1
, pos
); /* this one's right */
441 AHEAD(pos
); /* fix the OCH_ */
442 EMIT(OOR2
, 0); /* offset very wrong... */
443 AHEAD(THERE()); /* ...so fix it */
444 ASTERN(O_CH
, THERETHERE());
449 if (isdigit(PEEK())) {
451 REQUIRE(count
<= count2
, REG_BADBR
);
452 } else /* single number with comma */
454 } else /* just a single number */
456 repeat(p
, pos
, count
, count2
);
457 if (!EAT('}')) { /* error heuristics */
458 while (MORE() && PEEK() != '}')
460 REQUIRE(MORE(), REG_EBRACE
);
469 if (!( c
== '*' || c
== '+' || c
== '?' ||
470 (c
== '{' && MORE2() && isdigit(PEEK2())) ) )
472 SETERROR(REG_BADRPT
);
476 - p_str - string (no metacharacters) "parser"
477 == static void p_str(register struct parse *p);
481 register struct parse
*p
;
483 REQUIRE(MORE(), REG_EMPTY
);
485 ordinary(p
, GETNEXT());
489 - p_bre - BRE parser top level, anchoring and concatenation
490 == static void p_bre(register struct parse *p, register int end1, \
491 == register int end2);
492 * Giving end1 as OUT essentially eliminates the end1/end2 check.
494 * This implementation is a bit of a kludge, in that a trailing $ is first
495 * taken as an ordinary character and then revised to be an anchor. The
496 * only undesirable side effect is that '$' gets included as a character
497 * category in such cases. This is fairly harmless; not worth fixing.
498 * The amount of lookahead needed to avoid this kludge is excessive.
502 register struct parse
*p
;
503 register int end1
; /* first terminating character */
504 register int end2
; /* second terminating character */
506 register sopno start
= HERE();
507 register int first
= 1; /* first subexpression? */
508 register int wasdollar
= 0;
512 p
->g
->iflags
|= USEBOL
;
515 while (MORE() && !SEETWO(end1
, end2
)) {
516 wasdollar
= p_simp_re(p
, first
);
519 if (wasdollar
) { /* oops, that was a trailing anchor */
522 p
->g
->iflags
|= USEEOL
;
526 REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
530 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
531 == static int p_simp_re(register struct parse *p, int starordinary);
533 static int /* was the simple RE an unbackslashed $? */
534 p_simp_re(p
, starordinary
)
535 register struct parse
*p
;
536 int starordinary
; /* is a leading * an ordinary character? */
543 register sopno subno
;
544 # define BACKSL (1<<RCHAR_BIT)
546 pos
= HERE(); /* repetion op, if any, covers from here */
548 assert(MORE()); /* caller should have ensured this */
551 REQUIRE(MORE(), REG_EESCAPE
);
552 c
= BACKSL
| (unsigned char)GETNEXT();
556 if (p
->g
->cflags
®_NEWLINE
)
565 SETERROR(REG_BADRPT
);
571 p
->pbegin
[subno
] = HERE();
572 EMIT(OLPAREN
, subno
);
573 /* the MORE here is an error heuristic */
574 if (MORE() && !SEETWO('\\', ')'))
576 if (subno
< NPAREN
) {
577 p
->pend
[subno
] = HERE();
578 assert(p
->pend
[subno
] != 0);
580 EMIT(ORPAREN
, subno
);
581 REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
583 case BACKSL
|')': /* should not get here -- must be user */
585 SETERROR(REG_EPAREN
);
596 i
= (c
&~BACKSL
) - '0';
598 if (p
->pend
[i
] != 0) {
599 assert(i
<= p
->g
->nsub
);
601 assert(p
->pbegin
[i
] != 0);
602 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
603 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
604 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
607 SETERROR(REG_ESUBREG
);
611 REQUIRE(starordinary
, REG_BADRPT
);
614 ordinary(p
, c
&~ BACKSL
);
618 if (EAT('*')) { /* implemented as +? */
619 /* this case does not require the (y|) trick, noKLUDGE */
622 INSERT(OQUEST_
, pos
);
623 ASTERN(O_QUEST
, pos
);
624 } else if (EATTWO('\\', '{')) {
627 if (MORE() && isdigit(PEEK())) {
629 REQUIRE(count
<= count2
, REG_BADBR
);
630 } else /* single number with comma */
632 } else /* just a single number */
634 repeat(p
, pos
, count
, count2
);
635 if (!EATTWO('\\', '}')) { /* error heuristics */
636 while (MORE() && !SEETWO('\\', '}'))
638 REQUIRE(MORE(), REG_EBRACE
);
641 } else if (c
== (unsigned char)'$') /* $ (but not \$) ends it */
648 - p_count - parse a repetition count
649 == static int p_count(register struct parse *p);
651 static int /* the value */
653 register struct parse
*p
;
655 register int count
= 0;
656 register int ndigits
= 0;
658 while (MORE() && isdigit(PEEK()) && count
<= DUPMAX
) {
659 count
= count
*10 + (GETNEXT() - '0');
663 REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
668 - p_bracket - parse a bracketed character list
669 == static void p_bracket(register struct parse *p);
671 * Note a significant property of this code: if the allocset() did SETERROR,
672 * no set operations are done.
676 register struct parse
*p
;
679 register cset
*cs
= allocset(p
);
680 register int invert
= 0;
681 static RCHAR_T bow
[] = { '[', ':', '<', ':', ']', ']' };
682 static RCHAR_T eow
[] = { '[', ':', '>', ':', ']', ']' };
684 /* Dept of Truly Sickening Special-Case Kludges */
685 if (p
->next
+ 5 < p
->end
&& MEMCMP(p
->next
, bow
, 6) == 0) {
690 if (p
->next
+ 5 < p
->end
&& MEMCMP(p
->next
, eow
, 6) == 0) {
697 invert
++; /* make note to invert set at end */
702 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
706 MUSTEAT(']', REG_EBRACK
);
708 if (p
->error
!= 0) /* don't mess things up further */
711 if (p
->g
->cflags
®_ICASE
) {
715 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
716 if (CHIN(cs
, i
) && isalpha(i
)) {
721 if (cs
->multis
!= NULL
)
727 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
732 if (p
->g
->cflags
®_NEWLINE
)
734 if (cs
->multis
!= NULL
)
738 assert(cs
->multis
== NULL
); /* xxx */
740 if (nch(p
, cs
) == 1) { /* optimize singleton sets */
741 ordinary(p
, firstch(p
, cs
));
744 EMIT(OANYOF
, freezeset(p
, cs
));
748 - p_b_term - parse one term of a bracketed character list
749 == static void p_b_term(register struct parse *p, register cset *cs);
753 register struct parse
*p
;
757 register char start
, finish
;
760 /* classify what we've got */
761 switch ((MORE()) ? PEEK() : '\0') {
763 c
= (MORE2()) ? PEEK2() : '\0';
766 SETERROR(REG_ERANGE
);
767 return; /* NOTE RETURN */
775 case ':': /* character class */
777 REQUIRE(MORE(), REG_EBRACK
);
779 REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
781 REQUIRE(MORE(), REG_EBRACK
);
782 REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
784 case '=': /* equivalence class */
786 REQUIRE(MORE(), REG_EBRACK
);
788 REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
790 REQUIRE(MORE(), REG_EBRACK
);
791 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
793 default: /* symbol, ordinary character, or range */
794 /* xxx revision needed for multichar stuff */
795 start
= p_b_symbol(p
);
796 if (SEE('-') && MORE2() && PEEK2() != ']') {
802 finish
= p_b_symbol(p
);
805 /* xxx what about signed chars here... */
806 REQUIRE(start
<= finish
, REG_ERANGE
);
807 for (i
= start
; i
<= finish
; i
++)
814 - p_b_cclass - parse a character-class name and deal with it
815 == static void p_b_cclass(register struct parse *p, register cset *cs);
819 register struct parse
*p
;
822 register RCHAR_T
*sp
= p
->next
;
823 register struct cclass
*cp
;
828 while (MORE() && isalpha(PEEK()))
831 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
832 if (STRLEN(cp
->name
) == len
&& MEMCMP(cp
->name
, sp
, len
))
834 if (cp
->name
== NULL
) {
835 /* oops, didn't find it */
836 SETERROR(REG_ECTYPE
);
841 while ((c
= *u
++) != '\0')
843 for (u
= cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
848 - p_b_eclass - parse an equivalence-class name and deal with it
849 == static void p_b_eclass(register struct parse *p, register cset *cs);
851 * This implementation is incomplete. xxx
855 register struct parse
*p
;
860 c
= p_b_coll_elem(p
, '=');
865 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
866 == static char p_b_symbol(register struct parse *p);
868 static char /* value of symbol */
870 register struct parse
*p
;
874 REQUIRE(MORE(), REG_EBRACK
);
875 if (!EATTWO('[', '.'))
878 /* collating symbol */
879 value
= p_b_coll_elem(p
, '.');
880 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
885 - p_b_coll_elem - parse a collating-element name and look it up
886 == static char p_b_coll_elem(register struct parse *p, int endc);
888 static char /* value of collating element */
889 p_b_coll_elem(p
, endc
)
890 register struct parse
*p
;
891 int endc
; /* name ended by endc,']' */
893 register RCHAR_T
*sp
= p
->next
;
894 register struct cname
*cp
;
898 while (MORE() && !SEETWO(endc
, ']'))
901 SETERROR(REG_EBRACK
);
905 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
906 if (STRLEN(cp
->name
) == len
&& MEMCMP(cp
->name
, sp
, len
))
907 return(cp
->code
); /* known name */
909 return(*sp
); /* single character */
910 SETERROR(REG_ECOLLATE
); /* neither */
915 - othercase - return the case counterpart of an alphabetic
916 == static char othercase(int ch);
918 static char /* if no counterpart, return ch */
925 else if (islower(ch
))
927 else /* peculiar, but could happen */
932 - bothcases - emit a dualcase version of a two-case character
933 == static void bothcases(register struct parse *p, int ch);
935 * Boy, is this implementation ever a kludge...
939 register struct parse
*p
;
942 register RCHAR_T
*oldnext
= p
->next
;
943 register RCHAR_T
*oldend
= p
->end
;
946 assert(othercase(ch
) != ch
); /* p_bracket() would recurse */
953 assert(p
->next
== bracket
+2);
959 - ordinary - emit an ordinary character
960 == static void ordinary(register struct parse *p, register int ch);
964 register struct parse
*p
;
968 register cat_t *cap = p->g->categories;
971 if ((p
->g
->cflags
®_ICASE
) && isalpha(ch
) && othercase(ch
) != ch
)
974 EMIT(OCHAR
, (UCHAR_T
)ch
);
977 cap[ch] = p->g->ncategories++;
983 - nonnewline - emit REG_NEWLINE version of OANY
984 == static void nonnewline(register struct parse *p);
986 * Boy, is this implementation ever a kludge...
990 register struct parse
*p
;
992 register RCHAR_T
*oldnext
= p
->next
;
993 register RCHAR_T
*oldend
= p
->end
;
1003 assert(p
->next
== bracket
+3);
1009 - repeat - generate code for a bounded repetition, recursively if needed
1010 == static void repeat(register struct parse *p, sopno start, int from, int to);
1013 repeat(p
, start
, from
, to
)
1014 register struct parse
*p
;
1015 sopno start
; /* operand from here to end of strip */
1016 int from
; /* repeated from this number */
1017 int to
; /* to this number of times (maybe INFINITY) */
1019 register sopno finish
= HERE();
1022 # define REP(f, t) ((f)*8 + (t))
1023 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1024 register sopno copy
;
1026 if (p
->error
!= 0) /* head off possible runaway recursion */
1031 switch (REP(MAP(from
), MAP(to
))) {
1032 case REP(0, 0): /* must be user doing this */
1033 DROP(finish
-start
); /* drop the operand */
1035 case REP(0, 1): /* as x{1,1}? */
1036 case REP(0, N
): /* as x{1,n}? */
1037 case REP(0, INF
): /* as x{1,}? */
1038 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1039 INSERT(OCH_
, start
); /* offset is wrong... */
1040 repeat(p
, start
+1, 1, to
);
1041 ASTERN(OOR1
, start
);
1042 AHEAD(start
); /* ... fix it */
1045 ASTERN(O_CH
, THERETHERE());
1047 case REP(1, 1): /* trivial case */
1050 case REP(1, N
): /* as x?x{1,n-1} */
1051 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1052 INSERT(OCH_
, start
);
1053 ASTERN(OOR1
, start
);
1055 EMIT(OOR2
, 0); /* offset very wrong... */
1056 AHEAD(THERE()); /* ...so fix it */
1057 ASTERN(O_CH
, THERETHERE());
1058 copy
= dupl(p
, start
+1, finish
+1);
1059 assert(copy
== finish
+4);
1060 repeat(p
, copy
, 1, to
-1);
1062 case REP(1, INF
): /* as x+ */
1063 INSERT(OPLUS_
, start
);
1064 ASTERN(O_PLUS
, start
);
1066 case REP(N
, N
): /* as xx{m-1,n-1} */
1067 copy
= dupl(p
, start
, finish
);
1068 repeat(p
, copy
, from
-1, to
-1);
1070 case REP(N
, INF
): /* as xx{n-1,INF} */
1071 copy
= dupl(p
, start
, finish
);
1072 repeat(p
, copy
, from
-1, to
);
1074 default: /* "can't happen" */
1075 SETERROR(REG_ASSERT
); /* just in case */
1081 - seterr - set an error condition
1082 == static int seterr(register struct parse *p, int e);
1084 static int /* useless but makes type checking happy */
1086 register struct parse
*p
;
1089 if (p
->error
== 0) /* keep earliest error condition */
1091 p
->next
= nuls
; /* try to bring things to a halt */
1093 return(0); /* make the return value well-defined */
1097 - allocset - allocate a set of characters for []
1098 == static cset *allocset(register struct parse *p);
1102 register struct parse
*p
;
1104 register int no
= p
->g
->ncsets
++;
1106 register size_t nbytes
;
1108 register size_t css
= (size_t)p
->g
->csetsize
;
1111 if (no
>= p
->ncsalloc
) { /* need another column of space */
1112 p
->ncsalloc
+= RCHAR_BIT
;
1114 assert(nc
% RCHAR_BIT
== 0);
1115 nbytes
= nc
/ RCHAR_BIT
* css
;
1116 if (p
->g
->sets
== NULL
)
1117 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1119 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1121 if (p
->g
->setbits
== NULL
)
1122 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1124 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1126 /* xxx this isn't right if setbits is now NULL */
1127 for (i
= 0; i
< no
; i
++)
1128 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/RCHAR_BIT
);
1130 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1131 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1135 SETERROR(REG_ESPACE
);
1136 /* caller's responsibility not to do set ops */
1140 assert(p
->g
->sets
!= NULL
); /* xxx */
1141 cs
= &p
->g
->sets
[no
];
1142 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/RCHAR_BIT
);
1143 cs
->mask
= 1 << ((no
) % RCHAR_BIT
);
1152 - freeset - free a now-unused set
1153 == static void freeset(register struct parse *p, register cset *cs);
1157 register struct parse
*p
;
1161 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1162 register size_t css
= (size_t)p
->g
->csetsize
;
1164 for (i
= 0; i
< css
; i
++)
1166 if (cs
== top
-1) /* recover only the easy case */
1171 - freezeset - final processing on a set of characters
1172 == static int freezeset(register struct parse *p, register cset *cs);
1174 * The main task here is merging identical sets. This is usually a waste
1175 * of time (although the hash code minimizes the overhead), but can win
1176 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1177 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1180 static int /* set number */
1182 register struct parse
*p
;
1185 register uch h
= cs
->hash
;
1187 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1189 register size_t css
= (size_t)p
->g
->csetsize
;
1191 /* look for an earlier one which is the same */
1192 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1193 if (cs2
->hash
== h
&& cs2
!= cs
) {
1195 for (i
= 0; i
< css
; i
++)
1196 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1202 if (cs2
< top
) { /* found one */
1207 return((int)(cs
- p
->g
->sets
));
1211 - firstch - return first character in a set (which must have at least one)
1212 == static int firstch(register struct parse *p, register cset *cs);
1214 static int /* character; there is no "none" value */
1216 register struct parse
*p
;
1220 register size_t css
= (size_t)p
->g
->csetsize
;
1222 for (i
= 0; i
< css
; i
++)
1226 return(0); /* arbitrary */
1230 - nch - number of characters in a set
1231 == static int nch(register struct parse *p, register cset *cs);
1235 register struct parse
*p
;
1239 register size_t css
= (size_t)p
->g
->csetsize
;
1242 for (i
= 0; i
< css
; i
++)
1249 - mcadd - add a collating element to a cset
1250 == static void mcadd(register struct parse *p, register cset *cs, \
1251 == register char *cp);
1255 register struct parse
*p
;
1259 register size_t oldend
= cs
->smultis
;
1261 cs
->smultis
+= strlen(cp
) + 1;
1262 if (cs
->multis
== NULL
)
1263 cs
->multis
= malloc(cs
->smultis
);
1265 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1266 if (cs
->multis
== NULL
) {
1267 SETERROR(REG_ESPACE
);
1271 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1272 cs
->multis
[cs
->smultis
- 1] = '\0';
1276 - mcsub - subtract a collating element from a cset
1277 == static void mcsub(register cset *cs, register char *cp);
1284 register char *fp
= mcfind(cs
, cp
);
1285 register size_t len
= strlen(fp
);
1288 (void) memmove(fp
, fp
+ len
+ 1,
1289 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1292 if (cs
->smultis
== 0) {
1298 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1299 assert(cs
->multis
!= NULL
);
1303 - mcin - is a collating element in a cset?
1304 == static int mcin(register cset *cs, register char *cp);
1311 return(mcfind(cs
, cp
) != NULL
);
1315 - mcfind - find a collating element in a cset
1316 == static char *mcfind(register cset *cs, register char *cp);
1325 if (cs
->multis
== NULL
)
1327 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1328 if (strcmp(cp
, p
) == 0)
1334 - mcinvert - invert the list of collating elements in a cset
1335 == static void mcinvert(register struct parse *p, register cset *cs);
1337 * This would have to know the set of possibilities. Implementation
1342 register struct parse
*p
;
1345 assert(cs
->multis
== NULL
); /* xxx */
1349 - mccase - add case counterparts of the list of collating elements in a cset
1350 == static void mccase(register struct parse *p, register cset *cs);
1352 * This would have to know the set of possibilities. Implementation
1357 register struct parse
*p
;
1360 assert(cs
->multis
== NULL
); /* xxx */
1364 - isinsets - is this character in any sets?
1365 == static int isinsets(register struct re_guts *g, int c);
1367 static int /* predicate */
1369 register struct re_guts
*g
;
1374 register int ncols
= (g
->ncsets
+(RCHAR_BIT
-1)) / RCHAR_BIT
;
1375 register unsigned uc
= (unsigned char)c
;
1377 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1384 - samesets - are these two characters in exactly the same sets?
1385 == static int samesets(register struct re_guts *g, int c1, int c2);
1387 static int /* predicate */
1389 register struct re_guts
*g
;
1395 register int ncols
= (g
->ncsets
+(RCHAR_BIT
-1)) / RCHAR_BIT
;
1396 register unsigned uc1
= (unsigned char)c1
;
1397 register unsigned uc2
= (unsigned char)c2
;
1399 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1400 if (col
[uc1
] != col
[uc2
])
1406 - categorize - sort out character categories
1407 == static void categorize(struct parse *p, register struct re_guts *g);
1412 register struct re_guts
*g
;
1415 register cat_t *cats = g->categories;
1421 /* avoid making error situations worse */
1426 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1427 if (cats[c] == 0 && isinsets(g, c)) {
1428 cat = g->ncategories++;
1430 for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1431 if (cats[c2] == 0 && samesets(g, c, c2))
1438 - dupl - emit a duplicate of a bunch of sops
1439 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1441 static sopno
/* start of duplicate */
1442 dupl(p
, start
, finish
)
1443 register struct parse
*p
;
1444 sopno start
; /* from here */
1445 sopno finish
; /* to this less one */
1447 register sopno ret
= HERE();
1448 register sopno len
= finish
- start
;
1450 assert(finish
>= start
);
1453 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1454 assert(p
->ssize
>= p
->slen
+ len
);
1455 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1456 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1462 - doemit - emit a strip operator
1463 == static void doemit(register struct parse *p, sop op, size_t opnd);
1465 * It might seem better to implement this as a macro with a function as
1466 * hard-case backup, but it's just too big and messy unless there are
1467 * some changes to the data structures. Maybe later.
1471 register struct parse
*p
;
1475 /* avoid making error situations worse */
1479 /* deal with oversize operands ("can't happen", more or less) */
1480 assert(opnd
< 1<<OPSHIFT
);
1482 /* deal with undersized strip */
1483 if (p
->slen
>= p
->ssize
)
1484 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1485 assert(p
->slen
< p
->ssize
);
1487 /* finally, it's all reduced to the easy case */
1488 p
->strip
[p
->slen
++] = SOP(op
, opnd
);
1492 - doinsert - insert a sop into the strip
1493 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1496 doinsert(p
, op
, opnd
, pos
)
1497 register struct parse
*p
;
1506 /* avoid making error situations worse */
1511 EMIT(op
, opnd
); /* do checks, ensure space */
1512 assert(HERE() == sn
+1);
1515 /* adjust paren pointers */
1517 for (i
= 1; i
< NPAREN
; i
++) {
1518 if (p
->pbegin
[i
] >= pos
) {
1521 if (p
->pend
[i
] >= pos
) {
1526 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1527 (HERE()-pos
-1)*sizeof(sop
));
1532 - dofwd - complete a forward reference
1533 == static void dofwd(register struct parse *p, sopno pos, sop value);
1536 dofwd(p
, pos
, value
)
1537 register struct parse
*p
;
1541 /* avoid making error situations worse */
1545 assert(value
< 1<<OPSHIFT
);
1546 p
->strip
[pos
] = OP(p
->strip
[pos
]) | value
;
1550 - enlarge - enlarge the strip
1551 == static void enlarge(register struct parse *p, sopno size);
1555 register struct parse
*p
;
1556 register sopno size
;
1560 if (p
->ssize
>= size
)
1563 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1565 SETERROR(REG_ESPACE
);
1573 - stripsnug - compact the strip
1574 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1578 register struct parse
*p
;
1579 register struct re_guts
*g
;
1581 g
->nstates
= p
->slen
;
1582 g
->strip
= (sop
*)realloc((char *)p
->strip
, p
->slen
* sizeof(sop
));
1583 if (g
->strip
== NULL
) {
1584 SETERROR(REG_ESPACE
);
1585 g
->strip
= p
->strip
;
1590 - findmust - fill in must and mlen with longest mandatory literal string
1591 == static void findmust(register struct parse *p, register struct re_guts *g);
1593 * This algorithm could do fancy things like analyzing the operands of |
1594 * for common subsequences. Someday. This code is simple and finds most
1595 * of the interesting cases.
1597 * Note that must and mlen got initialized during setup.
1602 register struct re_guts
*g
;
1606 register sop
*newstart
;
1607 register sopno newlen
;
1609 register RCHAR_T
*cp
;
1612 /* avoid making error situations worse */
1616 /* find the longest OCHAR sequence in strip */
1618 scan
= g
->strip
+ 1;
1622 case OCHAR
: /* sequence member */
1623 if (newlen
== 0) /* new sequence */
1624 newstart
= scan
- 1;
1627 case OPLUS_
: /* things that don't break one */
1631 case OQUEST_
: /* things that must be skipped */
1637 /* assert() interferes w debug printouts */
1638 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1643 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1645 default: /* things that break a sequence */
1646 if (newlen
> g
->mlen
) { /* ends one */
1653 } while (OP(s
) != OEND
);
1655 if (g
->mlen
== 0) /* there isn't one */
1658 /* turn it into a character string */
1659 g
->must
= malloc(((size_t)g
->mlen
+ 1) * sizeof(RCHAR_T
));
1660 if (g
->must
== NULL
) { /* argh; just forget it */
1666 for (i
= g
->mlen
; i
> 0; i
--) {
1667 while (OP(s
= *scan
++) != OCHAR
)
1669 assert(cp
< g
->must
+ g
->mlen
);
1670 *cp
++ = (RCHAR_T
)OPND(s
);
1672 assert(cp
== g
->must
+ g
->mlen
);
1673 *cp
++ = '\0'; /* just on general principles */
1677 - pluscount - count + nesting
1678 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1680 static sopno
/* nesting depth */
1683 register struct re_guts
*g
;
1687 register sopno plusnest
= 0;
1688 register sopno maxnest
= 0;
1691 return(0); /* there may not be an OEND */
1693 scan
= g
->strip
+ 1;
1701 if (plusnest
> maxnest
)
1706 } while (OP(s
) != OEND
);