]>
git.saurik.com Git - apple/libc.git/blob - regex.subproj/regcomp.c
e2e0bf371efe9abd298595ada764eeadcad9a1f7
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1992, 1993, 1994
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/types.h>
73 * parse structure, passed up and down to avoid global variables and
77 char *next
; /* next character in RE */
78 char *end
; /* end of string (-> NUL normally) */
79 int error
; /* has an error been seen? */
80 sop
*strip
; /* malloced strip */
81 sopno ssize
; /* malloced strip size (allocated) */
82 sopno slen
; /* malloced strip length (used) */
83 int ncsalloc
; /* number of csets allocated */
85 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
86 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
87 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
90 /* ========= begin header generated by ./mkh ========= */
95 /* === regcomp.c === */
96 static void p_ere
__P((struct parse
*p
, int stop
));
97 static void p_ere_exp
__P((struct parse
*p
));
98 static void p_str
__P((struct parse
*p
));
99 static void p_bre
__P((struct parse
*p
, int end1
, int end2
));
100 static int p_simp_re
__P((struct parse
*p
, int starordinary
));
101 static int p_count
__P((struct parse
*p
));
102 static void p_bracket
__P((struct parse
*p
));
103 static void p_b_term
__P((struct parse
*p
, cset
*cs
));
104 static void p_b_cclass
__P((struct parse
*p
, cset
*cs
));
105 static void p_b_eclass
__P((struct parse
*p
, cset
*cs
));
106 static char p_b_symbol
__P((struct parse
*p
));
107 static char p_b_coll_elem
__P((struct parse
*p
, int endc
));
108 static char othercase
__P((int ch
));
109 static void bothcases
__P((struct parse
*p
, int ch
));
110 static void ordinary
__P((struct parse
*p
, int ch
));
111 static void nonnewline
__P((struct parse
*p
));
112 static void repeat
__P((struct parse
*p
, sopno start
, int from
, int to
));
113 static int seterr
__P((struct parse
*p
, int e
));
114 static cset
*allocset
__P((struct parse
*p
));
115 static void freeset
__P((struct parse
*p
, cset
*cs
));
116 static int freezeset
__P((struct parse
*p
, cset
*cs
));
117 static int firstch
__P((struct parse
*p
, cset
*cs
));
118 static int nch
__P((struct parse
*p
, cset
*cs
));
119 static void mcadd
__P((struct parse
*p
, cset
*cs
, char *cp
));
120 static void mcsub
__P((cset
*cs
, char *cp
));
121 static int mcin
__P((cset
*cs
, char *cp
));
122 static char *mcfind
__P((cset
*cs
, char *cp
));
123 static void mcinvert
__P((struct parse
*p
, cset
*cs
));
124 static void mccase
__P((struct parse
*p
, cset
*cs
));
125 static int isinsets
__P((struct re_guts
*g
, int c
));
126 static int samesets
__P((struct re_guts
*g
, int c1
, int c2
));
127 static void categorize
__P((struct parse
*p
, struct re_guts
*g
));
128 static sopno dupl
__P((struct parse
*p
, sopno start
, sopno finish
));
129 static void doemit
__P((struct parse
*p
, sop op
, size_t opnd
));
130 static void doinsert
__P((struct parse
*p
, sop op
, size_t opnd
, sopno pos
));
131 static void dofwd
__P((struct parse
*p
, sopno pos
, sop value
));
132 static void enlarge
__P((struct parse
*p
, sopno size
));
133 static void stripsnug
__P((struct parse
*p
, struct re_guts
*g
));
134 static void findmust
__P((struct parse
*p
, struct re_guts
*g
));
135 static sopno pluscount
__P((struct parse
*p
, struct re_guts
*g
));
140 /* ========= end header generated by ./mkh ========= */
142 static char nuls
[10]; /* place to point scanner in event of error */
145 * macros for use with parse structure
146 * BEWARE: these know that the parse structure is named `p' !!!
148 #define PEEK() (*p->next)
149 #define PEEK2() (*(p->next+1))
150 #define MORE() (p->next < p->end)
151 #define MORE2() (p->next+1 < p->end)
152 #define SEE(c) (MORE() && PEEK() == (c))
153 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
154 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
155 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
156 #define NEXT() (p->next++)
157 #define NEXT2() (p->next += 2)
158 #define NEXTn(n) (p->next += (n))
159 #define GETNEXT() (*p->next++)
160 #define SETERROR(e) seterr(p, (e))
161 #define REQUIRE(co, e) ((co) || SETERROR(e))
162 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
163 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
164 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
165 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
166 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
167 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
168 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
169 #define HERE() (p->slen)
170 #define THERE() (p->slen - 1)
171 #define THERETHERE() (p->slen - 2)
172 #define DROP(n) (p->slen -= (n))
175 static int never
= 0; /* for use in asserts; shuts lint up */
177 #define never 0 /* some <assert.h>s have bugs too */
181 - regcomp - interface for parser and compilation
182 = extern int regcomp(regex_t *, const char *, int);
183 = #define REG_BASIC 0000
184 = #define REG_EXTENDED 0001
185 = #define REG_ICASE 0002
186 = #define REG_NOSUB 0004
187 = #define REG_NEWLINE 0010
188 = #define REG_NOSPEC 0020
189 = #define REG_PEND 0040
190 = #define REG_DUMP 0200
192 int /* 0 success, otherwise REG_something */
193 regcomp(preg
, pattern
, cflags
)
199 register struct re_guts
*g
;
200 register struct parse
*p
= &pa
;
204 # define GOODFLAGS(f) (f)
206 # define GOODFLAGS(f) ((f)&~REG_DUMP)
209 cflags
= GOODFLAGS(cflags
);
210 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
213 if (cflags
®_PEND
) {
214 if (preg
->re_endp
< pattern
)
216 len
= preg
->re_endp
- pattern
;
218 len
= strlen((char *)pattern
);
220 /* do the mallocs early so failure handling is easy */
221 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
222 (NC
-1)*sizeof(cat_t
));
225 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
226 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
228 if (p
->strip
== NULL
) {
235 p
->next
= (char *)pattern
; /* convenience; we do not modify it */
236 p
->end
= p
->next
+ len
;
239 for (i
= 0; i
< NPAREN
; i
++) {
254 g
->ncategories
= 1; /* category 0 is "everything else" */
255 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
256 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
261 g
->firststate
= THERE();
262 if (cflags
®_EXTENDED
)
264 else if (cflags
®_NOSPEC
)
269 g
->laststate
= THERE();
271 /* tidy up loose ends and fill things in */
275 g
->nplus
= pluscount(p
, g
);
277 preg
->re_nsub
= g
->nsub
;
279 preg
->re_magic
= MAGIC1
;
281 /* not debugging, so can't rely on the assert() in regexec() */
283 SETERROR(REG_ASSERT
);
286 /* win or lose, we're done */
287 if (p
->error
!= 0) /* lose */
293 - p_ere - ERE parser top level, concatenation and alternation
294 == static void p_ere(register struct parse *p, int stop);
298 register struct parse
*p
;
299 int stop
; /* character this ERE should end at */
302 register sopno prevback
;
303 register sopno prevfwd
;
305 register int first
= 1; /* is this the first alternative? */
308 /* do a bunch of concatenated expressions */
310 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
312 REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
315 break; /* NOTE BREAK OUT */
318 INSERT(OCH_
, conc
); /* offset is wrong */
323 ASTERN(OOR1
, prevback
);
325 AHEAD(prevfwd
); /* fix previous offset */
327 EMIT(OOR2
, 0); /* offset is very wrong */
330 if (!first
) { /* tail-end fixups */
332 ASTERN(O_CH
, prevback
);
335 assert(!MORE() || SEE(stop
));
339 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
340 == static void p_ere_exp(register struct parse *p);
344 register struct parse
*p
;
350 register sopno subno
;
353 assert(MORE()); /* caller should have ensured this */
359 REQUIRE(MORE(), REG_EPAREN
);
363 p
->pbegin
[subno
] = HERE();
364 EMIT(OLPAREN
, subno
);
367 if (subno
< NPAREN
) {
368 p
->pend
[subno
] = HERE();
369 assert(p
->pend
[subno
] != 0);
371 EMIT(ORPAREN
, subno
);
372 MUSTEAT(')', REG_EPAREN
);
374 #ifndef POSIX_MISTAKE
375 case ')': /* happens only if no current unmatched ( */
377 * You may ask, why the ifndef? Because I didn't notice
378 * this until slightly too late for 1003.2, and none of the
379 * other 1003.2 regular-expression reviewers noticed it at
380 * all. So an unmatched ) is legal POSIX, at least until
381 * we can get it fixed.
383 SETERROR(REG_EPAREN
);
388 p
->g
->iflags
|= USEBOL
;
394 p
->g
->iflags
|= USEEOL
;
403 SETERROR(REG_BADRPT
);
406 if (p
->g
->cflags
®_NEWLINE
)
415 REQUIRE(MORE(), REG_EESCAPE
);
419 case '{': /* okay as ordinary except if digit follows */
420 REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT
);
430 /* we call { a repetition if followed by a digit */
431 if (!( c
== '*' || c
== '+' || c
== '?' ||
432 (c
== '{' && MORE2() && isdigit(PEEK2())) ))
433 return; /* no repetition, we're done */
436 REQUIRE(!wascaret
, REG_BADRPT
);
438 case '*': /* implemented as +? */
439 /* this case does not require the (y|) trick, noKLUDGE */
442 INSERT(OQUEST_
, pos
);
443 ASTERN(O_QUEST
, pos
);
450 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
451 INSERT(OCH_
, pos
); /* offset slightly wrong */
452 ASTERN(OOR1
, pos
); /* this one's right */
453 AHEAD(pos
); /* fix the OCH_ */
454 EMIT(OOR2
, 0); /* offset very wrong... */
455 AHEAD(THERE()); /* ...so fix it */
456 ASTERN(O_CH
, THERETHERE());
461 if (isdigit(PEEK())) {
463 REQUIRE(count
<= count2
, REG_BADBR
);
464 } else /* single number with comma */
466 } else /* just a single number */
468 repeat(p
, pos
, count
, count2
);
469 if (!EAT('}')) { /* error heuristics */
470 while (MORE() && PEEK() != '}')
472 REQUIRE(MORE(), REG_EBRACE
);
481 if (!( c
== '*' || c
== '+' || c
== '?' ||
482 (c
== '{' && MORE2() && isdigit(PEEK2())) ) )
484 SETERROR(REG_BADRPT
);
488 - p_str - string (no metacharacters) "parser"
489 == static void p_str(register struct parse *p);
493 register struct parse
*p
;
495 REQUIRE(MORE(), REG_EMPTY
);
497 ordinary(p
, GETNEXT());
501 - p_bre - BRE parser top level, anchoring and concatenation
502 == static void p_bre(register struct parse *p, register int end1, \
503 == register int end2);
504 * Giving end1 as OUT essentially eliminates the end1/end2 check.
506 * This implementation is a bit of a kludge, in that a trailing $ is first
507 * taken as an ordinary character and then revised to be an anchor. The
508 * only undesirable side effect is that '$' gets included as a character
509 * category in such cases. This is fairly harmless; not worth fixing.
510 * The amount of lookahead needed to avoid this kludge is excessive.
514 register struct parse
*p
;
515 register int end1
; /* first terminating character */
516 register int end2
; /* second terminating character */
518 register sopno start
= HERE();
519 register int first
= 1; /* first subexpression? */
520 register int wasdollar
= 0;
524 p
->g
->iflags
|= USEBOL
;
527 while (MORE() && !SEETWO(end1
, end2
)) {
528 wasdollar
= p_simp_re(p
, first
);
531 if (wasdollar
) { /* oops, that was a trailing anchor */
534 p
->g
->iflags
|= USEEOL
;
538 REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
542 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
543 == static int p_simp_re(register struct parse *p, int starordinary);
545 static int /* was the simple RE an unbackslashed $? */
546 p_simp_re(p
, starordinary
)
547 register struct parse
*p
;
548 int starordinary
; /* is a leading * an ordinary character? */
555 register sopno subno
;
556 # define BACKSL (1<<CHAR_BIT)
558 pos
= HERE(); /* repetion op, if any, covers from here */
560 assert(MORE()); /* caller should have ensured this */
563 REQUIRE(MORE(), REG_EESCAPE
);
564 c
= BACKSL
| (unsigned char)GETNEXT();
568 if (p
->g
->cflags
®_NEWLINE
)
577 SETERROR(REG_BADRPT
);
583 p
->pbegin
[subno
] = HERE();
584 EMIT(OLPAREN
, subno
);
585 /* the MORE here is an error heuristic */
586 if (MORE() && !SEETWO('\\', ')'))
588 if (subno
< NPAREN
) {
589 p
->pend
[subno
] = HERE();
590 assert(p
->pend
[subno
] != 0);
592 EMIT(ORPAREN
, subno
);
593 REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
595 case BACKSL
|')': /* should not get here -- must be user */
597 SETERROR(REG_EPAREN
);
608 i
= (c
&~BACKSL
) - '0';
610 if (p
->pend
[i
] != 0) {
611 assert(i
<= p
->g
->nsub
);
613 assert(p
->pbegin
[i
] != 0);
614 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
615 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
616 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
619 SETERROR(REG_ESUBREG
);
623 REQUIRE(starordinary
, REG_BADRPT
);
626 ordinary(p
, c
&~ BACKSL
);
630 if (EAT('*')) { /* implemented as +? */
631 /* this case does not require the (y|) trick, noKLUDGE */
634 INSERT(OQUEST_
, pos
);
635 ASTERN(O_QUEST
, pos
);
636 } else if (EATTWO('\\', '{')) {
639 if (MORE() && isdigit(PEEK())) {
641 REQUIRE(count
<= count2
, REG_BADBR
);
642 } else /* single number with comma */
644 } else /* just a single number */
646 repeat(p
, pos
, count
, count2
);
647 if (!EATTWO('\\', '}')) { /* error heuristics */
648 while (MORE() && !SEETWO('\\', '}'))
650 REQUIRE(MORE(), REG_EBRACE
);
653 } else if (c
== (unsigned char)'$') /* $ (but not \$) ends it */
660 - p_count - parse a repetition count
661 == static int p_count(register struct parse *p);
663 static int /* the value */
665 register struct parse
*p
;
667 register int count
= 0;
668 register int ndigits
= 0;
670 while (MORE() && isdigit(PEEK()) && count
<= DUPMAX
) {
671 count
= count
*10 + (GETNEXT() - '0');
675 REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
680 - p_bracket - parse a bracketed character list
681 == static void p_bracket(register struct parse *p);
683 * Note a significant property of this code: if the allocset() did SETERROR,
684 * no set operations are done.
688 register struct parse
*p
;
691 register cset
*cs
= allocset(p
);
692 register int invert
= 0;
694 /* Dept of Truly Sickening Special-Case Kludges */
695 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:<:]]", 6) == 0) {
700 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:>:]]", 6) == 0) {
707 invert
++; /* make note to invert set at end */
712 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
716 MUSTEAT(']', REG_EBRACK
);
718 if (p
->error
!= 0) /* don't mess things up further */
721 if (p
->g
->cflags
®_ICASE
) {
725 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
726 if (CHIN(cs
, i
) && isalpha(i
)) {
731 if (cs
->multis
!= NULL
)
737 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
742 if (p
->g
->cflags
®_NEWLINE
)
744 if (cs
->multis
!= NULL
)
748 assert(cs
->multis
== NULL
); /* xxx */
750 if (nch(p
, cs
) == 1) { /* optimize singleton sets */
751 ordinary(p
, firstch(p
, cs
));
754 EMIT(OANYOF
, freezeset(p
, cs
));
758 - p_b_term - parse one term of a bracketed character list
759 == static void p_b_term(register struct parse *p, register cset *cs);
763 register struct parse
*p
;
767 register char start
, finish
;
770 /* classify what we've got */
771 switch ((MORE()) ? PEEK() : '\0') {
773 c
= (MORE2()) ? PEEK2() : '\0';
776 SETERROR(REG_ERANGE
);
777 return; /* NOTE RETURN */
785 case ':': /* character class */
787 REQUIRE(MORE(), REG_EBRACK
);
789 REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
791 REQUIRE(MORE(), REG_EBRACK
);
792 REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
794 case '=': /* equivalence class */
796 REQUIRE(MORE(), REG_EBRACK
);
798 REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
800 REQUIRE(MORE(), REG_EBRACK
);
801 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
803 default: /* symbol, ordinary character, or range */
804 /* xxx revision needed for multichar stuff */
805 start
= p_b_symbol(p
);
806 if (SEE('-') && MORE2() && PEEK2() != ']') {
812 finish
= p_b_symbol(p
);
815 /* xxx what about signed chars here... */
816 REQUIRE(start
<= finish
, REG_ERANGE
);
817 for (i
= start
; i
<= finish
; i
++)
824 - p_b_cclass - parse a character-class name and deal with it
825 == static void p_b_cclass(register struct parse *p, register cset *cs);
829 register struct parse
*p
;
832 register char *sp
= p
->next
;
833 register struct cclass
*cp
;
838 while (MORE() && isalpha(PEEK()))
841 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
842 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
844 if (cp
->name
== NULL
) {
845 /* oops, didn't find it */
846 SETERROR(REG_ECTYPE
);
851 while ((c
= *u
++) != '\0')
853 for (u
= cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
858 - p_b_eclass - parse an equivalence-class name and deal with it
859 == static void p_b_eclass(register struct parse *p, register cset *cs);
861 * This implementation is incomplete. xxx
865 register struct parse
*p
;
870 c
= p_b_coll_elem(p
, '=');
875 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
876 == static char p_b_symbol(register struct parse *p);
878 static char /* value of symbol */
880 register struct parse
*p
;
884 REQUIRE(MORE(), REG_EBRACK
);
885 if (!EATTWO('[', '.'))
888 /* collating symbol */
889 value
= p_b_coll_elem(p
, '.');
890 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
895 - p_b_coll_elem - parse a collating-element name and look it up
896 == static char p_b_coll_elem(register struct parse *p, int endc);
898 static char /* value of collating element */
899 p_b_coll_elem(p
, endc
)
900 register struct parse
*p
;
901 int endc
; /* name ended by endc,']' */
903 register char *sp
= p
->next
;
904 register struct cname
*cp
;
908 while (MORE() && !SEETWO(endc
, ']'))
911 SETERROR(REG_EBRACK
);
915 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
916 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
917 return(cp
->code
); /* known name */
919 return(*sp
); /* single character */
920 SETERROR(REG_ECOLLATE
); /* neither */
925 - othercase - return the case counterpart of an alphabetic
926 == static char othercase(int ch);
928 static char /* if no counterpart, return ch */
935 else if (islower(ch
))
937 else /* peculiar, but could happen */
942 - bothcases - emit a dualcase version of a two-case character
943 == static void bothcases(register struct parse *p, int ch);
945 * Boy, is this implementation ever a kludge...
949 register struct parse
*p
;
952 register char *oldnext
= p
->next
;
953 register char *oldend
= p
->end
;
956 assert(othercase(ch
) != ch
); /* p_bracket() would recurse */
963 assert(p
->next
== bracket
+2);
969 - ordinary - emit an ordinary character
970 == static void ordinary(register struct parse *p, register int ch);
974 register struct parse
*p
;
977 register cat_t
*cap
= p
->g
->categories
;
979 if ((p
->g
->cflags
®_ICASE
) && isalpha(ch
) && othercase(ch
) != ch
)
982 EMIT(OCHAR
, (unsigned char)ch
);
984 cap
[ch
] = p
->g
->ncategories
++;
989 - nonnewline - emit REG_NEWLINE version of OANY
990 == static void nonnewline(register struct parse *p);
992 * Boy, is this implementation ever a kludge...
996 register struct parse
*p
;
998 register char *oldnext
= p
->next
;
999 register char *oldend
= p
->end
;
1009 assert(p
->next
== bracket
+3);
1015 - repeat - generate code for a bounded repetition, recursively if needed
1016 == static void repeat(register struct parse *p, sopno start, int from, int to);
1019 repeat(p
, start
, from
, to
)
1020 register struct parse
*p
;
1021 sopno start
; /* operand from here to end of strip */
1022 int from
; /* repeated from this number */
1023 int to
; /* to this number of times (maybe INFINITY) */
1025 register sopno finish
= HERE();
1028 # define REP(f, t) ((f)*8 + (t))
1029 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1030 register sopno copy
;
1032 if (p
->error
!= 0) /* head off possible runaway recursion */
1037 switch (REP(MAP(from
), MAP(to
))) {
1038 case REP(0, 0): /* must be user doing this */
1039 DROP(finish
-start
); /* drop the operand */
1041 case REP(0, 1): /* as x{1,1}? */
1042 case REP(0, N
): /* as x{1,n}? */
1043 case REP(0, INF
): /* as x{1,}? */
1044 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1045 INSERT(OCH_
, start
); /* offset is wrong... */
1046 repeat(p
, start
+1, 1, to
);
1047 ASTERN(OOR1
, start
);
1048 AHEAD(start
); /* ... fix it */
1051 ASTERN(O_CH
, THERETHERE());
1053 case REP(1, 1): /* trivial case */
1056 case REP(1, N
): /* as x?x{1,n-1} */
1057 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1058 INSERT(OCH_
, start
);
1059 ASTERN(OOR1
, start
);
1061 EMIT(OOR2
, 0); /* offset very wrong... */
1062 AHEAD(THERE()); /* ...so fix it */
1063 ASTERN(O_CH
, THERETHERE());
1064 copy
= dupl(p
, start
+1, finish
+1);
1065 assert(copy
== finish
+4);
1066 repeat(p
, copy
, 1, to
-1);
1068 case REP(1, INF
): /* as x+ */
1069 INSERT(OPLUS_
, start
);
1070 ASTERN(O_PLUS
, start
);
1072 case REP(N
, N
): /* as xx{m-1,n-1} */
1073 copy
= dupl(p
, start
, finish
);
1074 repeat(p
, copy
, from
-1, to
-1);
1076 case REP(N
, INF
): /* as xx{n-1,INF} */
1077 copy
= dupl(p
, start
, finish
);
1078 repeat(p
, copy
, from
-1, to
);
1080 default: /* "can't happen" */
1081 SETERROR(REG_ASSERT
); /* just in case */
1087 - seterr - set an error condition
1088 == static int seterr(register struct parse *p, int e);
1090 static int /* useless but makes type checking happy */
1092 register struct parse
*p
;
1095 if (p
->error
== 0) /* keep earliest error condition */
1097 p
->next
= nuls
; /* try to bring things to a halt */
1099 return(0); /* make the return value well-defined */
1103 - allocset - allocate a set of characters for []
1104 == static cset *allocset(register struct parse *p);
1108 register struct parse
*p
;
1110 register int no
= p
->g
->ncsets
++;
1112 register size_t nbytes
;
1114 register size_t css
= (size_t)p
->g
->csetsize
;
1117 if (no
>= p
->ncsalloc
) { /* need another column of space */
1118 p
->ncsalloc
+= CHAR_BIT
;
1120 assert(nc
% CHAR_BIT
== 0);
1121 nbytes
= nc
/ CHAR_BIT
* css
;
1122 if (p
->g
->sets
== NULL
)
1123 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1125 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1127 if (p
->g
->setbits
== NULL
)
1128 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1130 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1132 /* xxx this isn't right if setbits is now NULL */
1133 for (i
= 0; i
< no
; i
++)
1134 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1136 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1137 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1141 SETERROR(REG_ESPACE
);
1142 /* caller's responsibility not to do set ops */
1146 assert(p
->g
->sets
!= NULL
); /* xxx */
1147 cs
= &p
->g
->sets
[no
];
1148 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1149 cs
->mask
= 1 << ((no
) % CHAR_BIT
);
1158 - freeset - free a now-unused set
1159 == static void freeset(register struct parse *p, register cset *cs);
1163 register struct parse
*p
;
1167 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1168 register size_t css
= (size_t)p
->g
->csetsize
;
1170 for (i
= 0; i
< css
; i
++)
1172 if (cs
== top
-1) /* recover only the easy case */
1177 - freezeset - final processing on a set of characters
1178 == static int freezeset(register struct parse *p, register cset *cs);
1180 * The main task here is merging identical sets. This is usually a waste
1181 * of time (although the hash code minimizes the overhead), but can win
1182 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1183 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1186 static int /* set number */
1188 register struct parse
*p
;
1191 register uch h
= cs
->hash
;
1193 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1195 register size_t css
= (size_t)p
->g
->csetsize
;
1197 /* look for an earlier one which is the same */
1198 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1199 if (cs2
->hash
== h
&& cs2
!= cs
) {
1201 for (i
= 0; i
< css
; i
++)
1202 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1208 if (cs2
< top
) { /* found one */
1213 return((int)(cs
- p
->g
->sets
));
1217 - firstch - return first character in a set (which must have at least one)
1218 == static int firstch(register struct parse *p, register cset *cs);
1220 static int /* character; there is no "none" value */
1222 register struct parse
*p
;
1226 register size_t css
= (size_t)p
->g
->csetsize
;
1228 for (i
= 0; i
< css
; i
++)
1232 return(0); /* arbitrary */
1236 - nch - number of characters in a set
1237 == static int nch(register struct parse *p, register cset *cs);
1241 register struct parse
*p
;
1245 register size_t css
= (size_t)p
->g
->csetsize
;
1248 for (i
= 0; i
< css
; i
++)
1255 - mcadd - add a collating element to a cset
1256 == static void mcadd(register struct parse *p, register cset *cs, \
1257 == register char *cp);
1261 register struct parse
*p
;
1265 register size_t oldend
= cs
->smultis
;
1267 cs
->smultis
+= strlen(cp
) + 1;
1268 if (cs
->multis
== NULL
)
1269 cs
->multis
= malloc(cs
->smultis
);
1271 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1272 if (cs
->multis
== NULL
) {
1273 SETERROR(REG_ESPACE
);
1277 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1278 cs
->multis
[cs
->smultis
- 1] = '\0';
1282 - mcsub - subtract a collating element from a cset
1283 == static void mcsub(register cset *cs, register char *cp);
1290 register char *fp
= mcfind(cs
, cp
);
1291 register size_t len
= strlen(fp
);
1294 (void) memmove(fp
, fp
+ len
+ 1,
1295 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1298 if (cs
->smultis
== 0) {
1304 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1305 assert(cs
->multis
!= NULL
);
1309 - mcin - is a collating element in a cset?
1310 == static int mcin(register cset *cs, register char *cp);
1317 return(mcfind(cs
, cp
) != NULL
);
1321 - mcfind - find a collating element in a cset
1322 == static char *mcfind(register cset *cs, register char *cp);
1331 if (cs
->multis
== NULL
)
1333 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1334 if (strcmp(cp
, p
) == 0)
1340 - mcinvert - invert the list of collating elements in a cset
1341 == static void mcinvert(register struct parse *p, register cset *cs);
1343 * This would have to know the set of possibilities. Implementation
1348 register struct parse
*p
;
1351 assert(cs
->multis
== NULL
); /* xxx */
1355 - mccase - add case counterparts of the list of collating elements in a cset
1356 == static void mccase(register struct parse *p, register cset *cs);
1358 * This would have to know the set of possibilities. Implementation
1363 register struct parse
*p
;
1366 assert(cs
->multis
== NULL
); /* xxx */
1370 - isinsets - is this character in any sets?
1371 == static int isinsets(register struct re_guts *g, int c);
1373 static int /* predicate */
1375 register struct re_guts
*g
;
1380 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1381 register unsigned uc
= (unsigned char)c
;
1383 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1390 - samesets - are these two characters in exactly the same sets?
1391 == static int samesets(register struct re_guts *g, int c1, int c2);
1393 static int /* predicate */
1395 register struct re_guts
*g
;
1401 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1402 register unsigned uc1
= (unsigned char)c1
;
1403 register unsigned uc2
= (unsigned char)c2
;
1405 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1406 if (col
[uc1
] != col
[uc2
])
1412 - categorize - sort out character categories
1413 == static void categorize(struct parse *p, register struct re_guts *g);
1418 register struct re_guts
*g
;
1420 register cat_t
*cats
= g
->categories
;
1425 /* avoid making error situations worse */
1429 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1430 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1431 cat
= g
->ncategories
++;
1433 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1434 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1440 - dupl - emit a duplicate of a bunch of sops
1441 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1443 static sopno
/* start of duplicate */
1444 dupl(p
, start
, finish
)
1445 register struct parse
*p
;
1446 sopno start
; /* from here */
1447 sopno finish
; /* to this less one */
1449 register sopno ret
= HERE();
1450 register sopno len
= finish
- start
;
1452 assert(finish
>= start
);
1455 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1456 assert(p
->ssize
>= p
->slen
+ len
);
1457 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1458 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1464 - doemit - emit a strip operator
1465 == static void doemit(register struct parse *p, sop op, size_t opnd);
1467 * It might seem better to implement this as a macro with a function as
1468 * hard-case backup, but it's just too big and messy unless there are
1469 * some changes to the data structures. Maybe later.
1473 register struct parse
*p
;
1477 /* avoid making error situations worse */
1481 /* deal with oversize operands ("can't happen", more or less) */
1482 assert(opnd
< 1<<OPSHIFT
);
1484 /* deal with undersized strip */
1485 if (p
->slen
>= p
->ssize
)
1486 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1487 assert(p
->slen
< p
->ssize
);
1489 /* finally, it's all reduced to the easy case */
1490 p
->strip
[p
->slen
++] = SOP(op
, opnd
);
1494 - doinsert - insert a sop into the strip
1495 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1498 doinsert(p
, op
, opnd
, pos
)
1499 register struct parse
*p
;
1508 /* avoid making error situations worse */
1513 EMIT(op
, opnd
); /* do checks, ensure space */
1514 assert(HERE() == sn
+1);
1517 /* adjust paren pointers */
1519 for (i
= 1; i
< NPAREN
; i
++) {
1520 if (p
->pbegin
[i
] >= pos
) {
1523 if (p
->pend
[i
] >= pos
) {
1528 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1529 (HERE()-pos
-1)*sizeof(sop
));
1534 - dofwd - complete a forward reference
1535 == static void dofwd(register struct parse *p, sopno pos, sop value);
1538 dofwd(p
, pos
, value
)
1539 register struct parse
*p
;
1543 /* avoid making error situations worse */
1547 assert(value
< 1<<OPSHIFT
);
1548 p
->strip
[pos
] = OP(p
->strip
[pos
]) | value
;
1552 - enlarge - enlarge the strip
1553 == static void enlarge(register struct parse *p, sopno size);
1557 register struct parse
*p
;
1558 register sopno size
;
1562 if (p
->ssize
>= size
)
1565 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1567 SETERROR(REG_ESPACE
);
1575 - stripsnug - compact the strip
1576 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1580 register struct parse
*p
;
1581 register struct re_guts
*g
;
1583 g
->nstates
= p
->slen
;
1584 g
->strip
= (sop
*)realloc((char *)p
->strip
, p
->slen
* sizeof(sop
));
1585 if (g
->strip
== NULL
) {
1586 SETERROR(REG_ESPACE
);
1587 g
->strip
= p
->strip
;
1592 - findmust - fill in must and mlen with longest mandatory literal string
1593 == static void findmust(register struct parse *p, register struct re_guts *g);
1595 * This algorithm could do fancy things like analyzing the operands of |
1596 * for common subsequences. Someday. This code is simple and finds most
1597 * of the interesting cases.
1599 * Note that must and mlen got initialized during setup.
1604 register struct re_guts
*g
;
1608 register sop
*newstart
;
1609 register sopno newlen
;
1614 /* avoid making error situations worse */
1618 /* find the longest OCHAR sequence in strip */
1620 scan
= g
->strip
+ 1;
1624 case OCHAR
: /* sequence member */
1625 if (newlen
== 0) /* new sequence */
1626 newstart
= scan
- 1;
1629 case OPLUS_
: /* things that don't break one */
1633 case OQUEST_
: /* things that must be skipped */
1639 /* assert() interferes w debug printouts */
1640 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1645 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1647 default: /* things that break a sequence */
1648 if (newlen
> g
->mlen
) { /* ends one */
1655 } while (OP(s
) != OEND
);
1657 if (g
->mlen
== 0) /* there isn't one */
1660 /* turn it into a character string */
1661 g
->must
= malloc((size_t)g
->mlen
+ 1);
1662 if (g
->must
== NULL
) { /* argh; just forget it */
1668 for (i
= g
->mlen
; i
> 0; i
--) {
1669 while (OP(s
= *scan
++) != OCHAR
)
1671 assert(cp
< g
->must
+ g
->mlen
);
1672 *cp
++ = (char)OPND(s
);
1674 assert(cp
== g
->must
+ g
->mlen
);
1675 *cp
++ = '\0'; /* just on general principles */
1679 - pluscount - count + nesting
1680 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1682 static sopno
/* nesting depth */
1685 register struct re_guts
*g
;
1689 register sopno plusnest
= 0;
1690 register sopno maxnest
= 0;
1693 return(0); /* there may not be an OEND */
1695 scan
= g
->strip
+ 1;
1703 if (plusnest
> maxnest
)
1708 } while (OP(s
) != OEND
);