]>
git.saurik.com Git - apple/libc.git/blob - regex/regcomp.c
c26826d24ee3b7b57d1528581d4a71dbcb0fa0fa
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1992, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/types.h>
76 * parse structure, passed up and down to avoid global variables and
80 char *next
; /* next character in RE */
81 char *end
; /* end of string (-> NUL normally) */
82 int error
; /* has an error been seen? */
83 sop
*strip
; /* malloced strip */
84 sopno ssize
; /* malloced strip size (allocated) */
85 sopno slen
; /* malloced strip length (used) */
86 int ncsalloc
; /* number of csets allocated */
88 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
89 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
90 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
93 /* ========= begin header generated by ./mkh ========= */
98 /* === regcomp.c === */
99 static void p_ere
__P((struct parse
*p
, int stop
));
100 static void p_ere_exp
__P((struct parse
*p
));
101 static void p_str
__P((struct parse
*p
));
102 static void p_bre
__P((struct parse
*p
, int end1
, int end2
));
103 static int p_simp_re
__P((struct parse
*p
, int starordinary
));
104 static int p_count
__P((struct parse
*p
));
105 static void p_bracket
__P((struct parse
*p
));
106 static void p_b_term
__P((struct parse
*p
, cset
*cs
));
107 static void p_b_cclass
__P((struct parse
*p
, cset
*cs
));
108 static void p_b_eclass
__P((struct parse
*p
, cset
*cs
));
109 static char p_b_symbol
__P((struct parse
*p
));
110 static char p_b_coll_elem
__P((struct parse
*p
, int endc
));
111 static char othercase
__P((int ch
));
112 static void bothcases
__P((struct parse
*p
, int ch
));
113 static void ordinary
__P((struct parse
*p
, int ch
));
114 static void nonnewline
__P((struct parse
*p
));
115 static void repeat
__P((struct parse
*p
, sopno start
, int from
, int to
));
116 static int seterr
__P((struct parse
*p
, int e
));
117 static cset
*allocset
__P((struct parse
*p
));
118 static void freeset
__P((struct parse
*p
, cset
*cs
));
119 static int freezeset
__P((struct parse
*p
, cset
*cs
));
120 static int firstch
__P((struct parse
*p
, cset
*cs
));
121 static int nch
__P((struct parse
*p
, cset
*cs
));
122 static void mcadd
__P((struct parse
*p
, cset
*cs
, char *cp
));
123 static void mcsub
__P((cset
*cs
, char *cp
));
124 static int mcin
__P((cset
*cs
, char *cp
));
125 static char *mcfind
__P((cset
*cs
, char *cp
));
126 static void mcinvert
__P((struct parse
*p
, cset
*cs
));
127 static void mccase
__P((struct parse
*p
, cset
*cs
));
128 static int isinsets
__P((struct re_guts
*g
, int c
));
129 static int samesets
__P((struct re_guts
*g
, int c1
, int c2
));
130 static void categorize
__P((struct parse
*p
, struct re_guts
*g
));
131 static sopno dupl
__P((struct parse
*p
, sopno start
, sopno finish
));
132 static void doemit
__P((struct parse
*p
, sop op
, size_t opnd
));
133 static void doinsert
__P((struct parse
*p
, sop op
, size_t opnd
, sopno pos
));
134 static void dofwd
__P((struct parse
*p
, sopno pos
, sop value
));
135 static void enlarge
__P((struct parse
*p
, sopno size
));
136 static void stripsnug
__P((struct parse
*p
, struct re_guts
*g
));
137 static void findmust
__P((struct parse
*p
, struct re_guts
*g
));
138 static sopno pluscount
__P((struct parse
*p
, struct re_guts
*g
));
143 /* ========= end header generated by ./mkh ========= */
145 static char nuls
[10]; /* place to point scanner in event of error */
148 * macros for use with parse structure
149 * BEWARE: these know that the parse structure is named `p' !!!
151 #define PEEK() (*p->next)
152 #define PEEK2() (*(p->next+1))
153 #define MORE() (p->next < p->end)
154 #define MORE2() (p->next+1 < p->end)
155 #define SEE(c) (MORE() && PEEK() == (c))
156 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
157 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
158 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
159 #define NEXT() (p->next++)
160 #define NEXT2() (p->next += 2)
161 #define NEXTn(n) (p->next += (n))
162 #define GETNEXT() (*p->next++)
163 #define SETERROR(e) seterr(p, (e))
164 #define REQUIRE(co, e) ((co) || SETERROR(e))
165 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
166 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
167 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
168 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
169 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
170 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
171 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
172 #define HERE() (p->slen)
173 #define THERE() (p->slen - 1)
174 #define THERETHERE() (p->slen - 2)
175 #define DROP(n) (p->slen -= (n))
178 static int never
= 0; /* for use in asserts; shuts lint up */
180 #define never 0 /* some <assert.h>s have bugs too */
184 - regcomp - interface for parser and compilation
185 = extern int regcomp(regex_t *, const char *, int);
186 = #define REG_BASIC 0000
187 = #define REG_EXTENDED 0001
188 = #define REG_ICASE 0002
189 = #define REG_NOSUB 0004
190 = #define REG_NEWLINE 0010
191 = #define REG_NOSPEC 0020
192 = #define REG_PEND 0040
193 = #define REG_DUMP 0200
195 int /* 0 success, otherwise REG_something */
196 regcomp(preg
, pattern
, cflags
)
202 register struct re_guts
*g
;
203 register struct parse
*p
= &pa
;
207 # define GOODFLAGS(f) (f)
209 # define GOODFLAGS(f) ((f)&~REG_DUMP)
212 cflags
= GOODFLAGS(cflags
);
213 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
216 if (cflags
®_PEND
) {
217 if (preg
->re_endp
< pattern
)
219 len
= preg
->re_endp
- pattern
;
221 len
= strlen((char *)pattern
);
223 /* do the mallocs early so failure handling is easy */
224 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
225 (NC
-1)*sizeof(cat_t
));
228 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
229 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
231 if (p
->strip
== NULL
) {
238 p
->next
= (char *)pattern
; /* convenience; we do not modify it */
239 p
->end
= p
->next
+ len
;
242 for (i
= 0; i
< NPAREN
; i
++) {
257 g
->ncategories
= 1; /* category 0 is "everything else" */
258 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
259 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
264 g
->firststate
= THERE();
265 if (cflags
®_EXTENDED
)
267 else if (cflags
®_NOSPEC
)
272 g
->laststate
= THERE();
274 /* tidy up loose ends and fill things in */
278 g
->nplus
= pluscount(p
, g
);
280 preg
->re_nsub
= g
->nsub
;
282 preg
->re_magic
= MAGIC1
;
284 /* not debugging, so can't rely on the assert() in regexec() */
286 SETERROR(REG_ASSERT
);
289 /* win or lose, we're done */
290 if (p
->error
!= 0) /* lose */
296 - p_ere - ERE parser top level, concatenation and alternation
297 == static void p_ere(register struct parse *p, int stop);
301 register struct parse
*p
;
302 int stop
; /* character this ERE should end at */
305 register sopno prevback
;
306 register sopno prevfwd
;
308 register int first
= 1; /* is this the first alternative? */
311 /* do a bunch of concatenated expressions */
313 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
315 REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
318 break; /* NOTE BREAK OUT */
321 INSERT(OCH_
, conc
); /* offset is wrong */
326 ASTERN(OOR1
, prevback
);
328 AHEAD(prevfwd
); /* fix previous offset */
330 EMIT(OOR2
, 0); /* offset is very wrong */
333 if (!first
) { /* tail-end fixups */
335 ASTERN(O_CH
, prevback
);
338 assert(!MORE() || SEE(stop
));
342 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
343 == static void p_ere_exp(register struct parse *p);
347 register struct parse
*p
;
353 register sopno subno
;
356 assert(MORE()); /* caller should have ensured this */
362 REQUIRE(MORE(), REG_EPAREN
);
366 p
->pbegin
[subno
] = HERE();
367 EMIT(OLPAREN
, subno
);
370 if (subno
< NPAREN
) {
371 p
->pend
[subno
] = HERE();
372 assert(p
->pend
[subno
] != 0);
374 EMIT(ORPAREN
, subno
);
375 MUSTEAT(')', REG_EPAREN
);
377 #ifndef POSIX_MISTAKE
378 case ')': /* happens only if no current unmatched ( */
380 * You may ask, why the ifndef? Because I didn't notice
381 * this until slightly too late for 1003.2, and none of the
382 * other 1003.2 regular-expression reviewers noticed it at
383 * all. So an unmatched ) is legal POSIX, at least until
384 * we can get it fixed.
386 SETERROR(REG_EPAREN
);
391 p
->g
->iflags
|= USEBOL
;
397 p
->g
->iflags
|= USEEOL
;
406 SETERROR(REG_BADRPT
);
409 if (p
->g
->cflags
®_NEWLINE
)
418 REQUIRE(MORE(), REG_EESCAPE
);
422 case '{': /* okay as ordinary except if digit follows */
423 REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT
);
433 /* we call { a repetition if followed by a digit */
434 if (!( c
== '*' || c
== '+' || c
== '?' ||
435 (c
== '{' && MORE2() && isdigit(PEEK2())) ))
436 return; /* no repetition, we're done */
439 REQUIRE(!wascaret
, REG_BADRPT
);
441 case '*': /* implemented as +? */
442 /* this case does not require the (y|) trick, noKLUDGE */
445 INSERT(OQUEST_
, pos
);
446 ASTERN(O_QUEST
, pos
);
453 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
454 INSERT(OCH_
, pos
); /* offset slightly wrong */
455 ASTERN(OOR1
, pos
); /* this one's right */
456 AHEAD(pos
); /* fix the OCH_ */
457 EMIT(OOR2
, 0); /* offset very wrong... */
458 AHEAD(THERE()); /* ...so fix it */
459 ASTERN(O_CH
, THERETHERE());
464 if (isdigit(PEEK())) {
466 REQUIRE(count
<= count2
, REG_BADBR
);
467 } else /* single number with comma */
469 } else /* just a single number */
471 repeat(p
, pos
, count
, count2
);
472 if (!EAT('}')) { /* error heuristics */
473 while (MORE() && PEEK() != '}')
475 REQUIRE(MORE(), REG_EBRACE
);
484 if (!( c
== '*' || c
== '+' || c
== '?' ||
485 (c
== '{' && MORE2() && isdigit(PEEK2())) ) )
487 SETERROR(REG_BADRPT
);
491 - p_str - string (no metacharacters) "parser"
492 == static void p_str(register struct parse *p);
496 register struct parse
*p
;
498 REQUIRE(MORE(), REG_EMPTY
);
500 ordinary(p
, GETNEXT());
504 - p_bre - BRE parser top level, anchoring and concatenation
505 == static void p_bre(register struct parse *p, register int end1, \
506 == register int end2);
507 * Giving end1 as OUT essentially eliminates the end1/end2 check.
509 * This implementation is a bit of a kludge, in that a trailing $ is first
510 * taken as an ordinary character and then revised to be an anchor. The
511 * only undesirable side effect is that '$' gets included as a character
512 * category in such cases. This is fairly harmless; not worth fixing.
513 * The amount of lookahead needed to avoid this kludge is excessive.
517 register struct parse
*p
;
518 register int end1
; /* first terminating character */
519 register int end2
; /* second terminating character */
521 register sopno start
= HERE();
522 register int first
= 1; /* first subexpression? */
523 register int wasdollar
= 0;
527 p
->g
->iflags
|= USEBOL
;
530 while (MORE() && !SEETWO(end1
, end2
)) {
531 wasdollar
= p_simp_re(p
, first
);
534 if (wasdollar
) { /* oops, that was a trailing anchor */
537 p
->g
->iflags
|= USEEOL
;
541 REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
545 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
546 == static int p_simp_re(register struct parse *p, int starordinary);
548 static int /* was the simple RE an unbackslashed $? */
549 p_simp_re(p
, starordinary
)
550 register struct parse
*p
;
551 int starordinary
; /* is a leading * an ordinary character? */
558 register sopno subno
;
559 # define BACKSL (1<<CHAR_BIT)
561 pos
= HERE(); /* repetion op, if any, covers from here */
563 assert(MORE()); /* caller should have ensured this */
566 REQUIRE(MORE(), REG_EESCAPE
);
567 c
= BACKSL
| (unsigned char)GETNEXT();
571 if (p
->g
->cflags
®_NEWLINE
)
580 SETERROR(REG_BADRPT
);
586 p
->pbegin
[subno
] = HERE();
587 EMIT(OLPAREN
, subno
);
588 /* the MORE here is an error heuristic */
589 if (MORE() && !SEETWO('\\', ')'))
591 if (subno
< NPAREN
) {
592 p
->pend
[subno
] = HERE();
593 assert(p
->pend
[subno
] != 0);
595 EMIT(ORPAREN
, subno
);
596 REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
598 case BACKSL
|')': /* should not get here -- must be user */
600 SETERROR(REG_EPAREN
);
611 i
= (c
&~BACKSL
) - '0';
613 if (p
->pend
[i
] != 0) {
614 assert(i
<= p
->g
->nsub
);
616 assert(p
->pbegin
[i
] != 0);
617 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
618 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
619 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
622 SETERROR(REG_ESUBREG
);
626 REQUIRE(starordinary
, REG_BADRPT
);
629 ordinary(p
, c
&~ BACKSL
);
633 if (EAT('*')) { /* implemented as +? */
634 /* this case does not require the (y|) trick, noKLUDGE */
637 INSERT(OQUEST_
, pos
);
638 ASTERN(O_QUEST
, pos
);
639 } else if (EATTWO('\\', '{')) {
642 if (MORE() && isdigit(PEEK())) {
644 REQUIRE(count
<= count2
, REG_BADBR
);
645 } else /* single number with comma */
647 } else /* just a single number */
649 repeat(p
, pos
, count
, count2
);
650 if (!EATTWO('\\', '}')) { /* error heuristics */
651 while (MORE() && !SEETWO('\\', '}'))
653 REQUIRE(MORE(), REG_EBRACE
);
656 } else if (c
== (unsigned char)'$') /* $ (but not \$) ends it */
663 - p_count - parse a repetition count
664 == static int p_count(register struct parse *p);
666 static int /* the value */
668 register struct parse
*p
;
670 register int count
= 0;
671 register int ndigits
= 0;
673 while (MORE() && isdigit(PEEK()) && count
<= DUPMAX
) {
674 count
= count
*10 + (GETNEXT() - '0');
678 REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
683 - p_bracket - parse a bracketed character list
684 == static void p_bracket(register struct parse *p);
686 * Note a significant property of this code: if the allocset() did SETERROR,
687 * no set operations are done.
691 register struct parse
*p
;
694 register cset
*cs
= allocset(p
);
695 register int invert
= 0;
697 /* Dept of Truly Sickening Special-Case Kludges */
698 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:<:]]", 6) == 0) {
703 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:>:]]", 6) == 0) {
710 invert
++; /* make note to invert set at end */
715 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
719 MUSTEAT(']', REG_EBRACK
);
721 if (p
->error
!= 0) /* don't mess things up further */
724 if (p
->g
->cflags
®_ICASE
) {
728 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
729 if (CHIN(cs
, i
) && isalpha(i
)) {
734 if (cs
->multis
!= NULL
)
740 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
745 if (p
->g
->cflags
®_NEWLINE
)
747 if (cs
->multis
!= NULL
)
751 assert(cs
->multis
== NULL
); /* xxx */
753 if (nch(p
, cs
) == 1) { /* optimize singleton sets */
754 ordinary(p
, firstch(p
, cs
));
757 EMIT(OANYOF
, freezeset(p
, cs
));
761 - p_b_term - parse one term of a bracketed character list
762 == static void p_b_term(register struct parse *p, register cset *cs);
766 register struct parse
*p
;
770 register char start
, finish
;
773 /* classify what we've got */
774 switch ((MORE()) ? PEEK() : '\0') {
776 c
= (MORE2()) ? PEEK2() : '\0';
779 SETERROR(REG_ERANGE
);
780 return; /* NOTE RETURN */
788 case ':': /* character class */
790 REQUIRE(MORE(), REG_EBRACK
);
792 REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
794 REQUIRE(MORE(), REG_EBRACK
);
795 REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
797 case '=': /* equivalence class */
799 REQUIRE(MORE(), REG_EBRACK
);
801 REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
803 REQUIRE(MORE(), REG_EBRACK
);
804 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
806 default: /* symbol, ordinary character, or range */
807 /* xxx revision needed for multichar stuff */
808 start
= p_b_symbol(p
);
809 if (SEE('-') && MORE2() && PEEK2() != ']') {
815 finish
= p_b_symbol(p
);
818 /* xxx what about signed chars here... */
819 REQUIRE(start
<= finish
, REG_ERANGE
);
820 for (i
= start
; i
<= finish
; i
++)
827 - p_b_cclass - parse a character-class name and deal with it
828 == static void p_b_cclass(register struct parse *p, register cset *cs);
832 register struct parse
*p
;
835 register char *sp
= p
->next
;
836 register struct cclass
*cp
;
841 while (MORE() && isalpha(PEEK()))
844 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
845 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
847 if (cp
->name
== NULL
) {
848 /* oops, didn't find it */
849 SETERROR(REG_ECTYPE
);
854 while ((c
= *u
++) != '\0')
856 for (u
= cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
861 - p_b_eclass - parse an equivalence-class name and deal with it
862 == static void p_b_eclass(register struct parse *p, register cset *cs);
864 * This implementation is incomplete. xxx
868 register struct parse
*p
;
873 c
= p_b_coll_elem(p
, '=');
878 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
879 == static char p_b_symbol(register struct parse *p);
881 static char /* value of symbol */
883 register struct parse
*p
;
887 REQUIRE(MORE(), REG_EBRACK
);
888 if (!EATTWO('[', '.'))
891 /* collating symbol */
892 value
= p_b_coll_elem(p
, '.');
893 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
898 - p_b_coll_elem - parse a collating-element name and look it up
899 == static char p_b_coll_elem(register struct parse *p, int endc);
901 static char /* value of collating element */
902 p_b_coll_elem(p
, endc
)
903 register struct parse
*p
;
904 int endc
; /* name ended by endc,']' */
906 register char *sp
= p
->next
;
907 register struct cname
*cp
;
911 while (MORE() && !SEETWO(endc
, ']'))
914 SETERROR(REG_EBRACK
);
918 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
919 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
920 return(cp
->code
); /* known name */
922 return(*sp
); /* single character */
923 SETERROR(REG_ECOLLATE
); /* neither */
928 - othercase - return the case counterpart of an alphabetic
929 == static char othercase(int ch);
931 static char /* if no counterpart, return ch */
938 else if (islower(ch
))
940 else /* peculiar, but could happen */
945 - bothcases - emit a dualcase version of a two-case character
946 == static void bothcases(register struct parse *p, int ch);
948 * Boy, is this implementation ever a kludge...
952 register struct parse
*p
;
955 register char *oldnext
= p
->next
;
956 register char *oldend
= p
->end
;
959 assert(othercase(ch
) != ch
); /* p_bracket() would recurse */
966 assert(p
->next
== bracket
+2);
972 - ordinary - emit an ordinary character
973 == static void ordinary(register struct parse *p, register int ch);
977 register struct parse
*p
;
980 register cat_t
*cap
= p
->g
->categories
;
982 if ((p
->g
->cflags
®_ICASE
) && isalpha(ch
) && othercase(ch
) != ch
)
985 EMIT(OCHAR
, (unsigned char)ch
);
987 cap
[ch
] = p
->g
->ncategories
++;
992 - nonnewline - emit REG_NEWLINE version of OANY
993 == static void nonnewline(register struct parse *p);
995 * Boy, is this implementation ever a kludge...
999 register struct parse
*p
;
1001 register char *oldnext
= p
->next
;
1002 register char *oldend
= p
->end
;
1012 assert(p
->next
== bracket
+3);
1018 - repeat - generate code for a bounded repetition, recursively if needed
1019 == static void repeat(register struct parse *p, sopno start, int from, int to);
1022 repeat(p
, start
, from
, to
)
1023 register struct parse
*p
;
1024 sopno start
; /* operand from here to end of strip */
1025 int from
; /* repeated from this number */
1026 int to
; /* to this number of times (maybe INFINITY) */
1028 register sopno finish
= HERE();
1031 # define REP(f, t) ((f)*8 + (t))
1032 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1033 register sopno copy
;
1035 if (p
->error
!= 0) /* head off possible runaway recursion */
1040 switch (REP(MAP(from
), MAP(to
))) {
1041 case REP(0, 0): /* must be user doing this */
1042 DROP(finish
-start
); /* drop the operand */
1044 case REP(0, 1): /* as x{1,1}? */
1045 case REP(0, N
): /* as x{1,n}? */
1046 case REP(0, INF
): /* as x{1,}? */
1047 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1048 INSERT(OCH_
, start
); /* offset is wrong... */
1049 repeat(p
, start
+1, 1, to
);
1050 ASTERN(OOR1
, start
);
1051 AHEAD(start
); /* ... fix it */
1054 ASTERN(O_CH
, THERETHERE());
1056 case REP(1, 1): /* trivial case */
1059 case REP(1, N
): /* as x?x{1,n-1} */
1060 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1061 INSERT(OCH_
, start
);
1062 ASTERN(OOR1
, start
);
1064 EMIT(OOR2
, 0); /* offset very wrong... */
1065 AHEAD(THERE()); /* ...so fix it */
1066 ASTERN(O_CH
, THERETHERE());
1067 copy
= dupl(p
, start
+1, finish
+1);
1068 assert(copy
== finish
+4);
1069 repeat(p
, copy
, 1, to
-1);
1071 case REP(1, INF
): /* as x+ */
1072 INSERT(OPLUS_
, start
);
1073 ASTERN(O_PLUS
, start
);
1075 case REP(N
, N
): /* as xx{m-1,n-1} */
1076 copy
= dupl(p
, start
, finish
);
1077 repeat(p
, copy
, from
-1, to
-1);
1079 case REP(N
, INF
): /* as xx{n-1,INF} */
1080 copy
= dupl(p
, start
, finish
);
1081 repeat(p
, copy
, from
-1, to
);
1083 default: /* "can't happen" */
1084 SETERROR(REG_ASSERT
); /* just in case */
1090 - seterr - set an error condition
1091 == static int seterr(register struct parse *p, int e);
1093 static int /* useless but makes type checking happy */
1095 register struct parse
*p
;
1098 if (p
->error
== 0) /* keep earliest error condition */
1100 p
->next
= nuls
; /* try to bring things to a halt */
1102 return(0); /* make the return value well-defined */
1106 - allocset - allocate a set of characters for []
1107 == static cset *allocset(register struct parse *p);
1111 register struct parse
*p
;
1113 register int no
= p
->g
->ncsets
++;
1115 register size_t nbytes
;
1117 register size_t css
= (size_t)p
->g
->csetsize
;
1120 if (no
>= p
->ncsalloc
) { /* need another column of space */
1121 p
->ncsalloc
+= CHAR_BIT
;
1123 assert(nc
% CHAR_BIT
== 0);
1124 nbytes
= nc
/ CHAR_BIT
* css
;
1125 if (p
->g
->sets
== NULL
)
1126 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1128 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1130 if (p
->g
->setbits
== NULL
)
1131 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1133 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1135 /* xxx this isn't right if setbits is now NULL */
1136 for (i
= 0; i
< no
; i
++)
1137 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1139 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1140 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1144 SETERROR(REG_ESPACE
);
1145 /* caller's responsibility not to do set ops */
1149 assert(p
->g
->sets
!= NULL
); /* xxx */
1150 cs
= &p
->g
->sets
[no
];
1151 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1152 cs
->mask
= 1 << ((no
) % CHAR_BIT
);
1161 - freeset - free a now-unused set
1162 == static void freeset(register struct parse *p, register cset *cs);
1166 register struct parse
*p
;
1170 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1171 register size_t css
= (size_t)p
->g
->csetsize
;
1173 for (i
= 0; i
< css
; i
++)
1175 if (cs
== top
-1) /* recover only the easy case */
1180 - freezeset - final processing on a set of characters
1181 == static int freezeset(register struct parse *p, register cset *cs);
1183 * The main task here is merging identical sets. This is usually a waste
1184 * of time (although the hash code minimizes the overhead), but can win
1185 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1186 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1189 static int /* set number */
1191 register struct parse
*p
;
1194 register uch h
= cs
->hash
;
1196 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1198 register size_t css
= (size_t)p
->g
->csetsize
;
1200 /* look for an earlier one which is the same */
1201 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1202 if (cs2
->hash
== h
&& cs2
!= cs
) {
1204 for (i
= 0; i
< css
; i
++)
1205 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1211 if (cs2
< top
) { /* found one */
1216 return((int)(cs
- p
->g
->sets
));
1220 - firstch - return first character in a set (which must have at least one)
1221 == static int firstch(register struct parse *p, register cset *cs);
1223 static int /* character; there is no "none" value */
1225 register struct parse
*p
;
1229 register size_t css
= (size_t)p
->g
->csetsize
;
1231 for (i
= 0; i
< css
; i
++)
1235 return(0); /* arbitrary */
1239 - nch - number of characters in a set
1240 == static int nch(register struct parse *p, register cset *cs);
1244 register struct parse
*p
;
1248 register size_t css
= (size_t)p
->g
->csetsize
;
1251 for (i
= 0; i
< css
; i
++)
1258 - mcadd - add a collating element to a cset
1259 == static void mcadd(register struct parse *p, register cset *cs, \
1260 == register char *cp);
1264 register struct parse
*p
;
1268 register size_t oldend
= cs
->smultis
;
1270 cs
->smultis
+= strlen(cp
) + 1;
1271 if (cs
->multis
== NULL
)
1272 cs
->multis
= malloc(cs
->smultis
);
1274 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1275 if (cs
->multis
== NULL
) {
1276 SETERROR(REG_ESPACE
);
1280 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1281 cs
->multis
[cs
->smultis
- 1] = '\0';
1285 - mcsub - subtract a collating element from a cset
1286 == static void mcsub(register cset *cs, register char *cp);
1293 register char *fp
= mcfind(cs
, cp
);
1294 register size_t len
= strlen(fp
);
1297 (void) memmove(fp
, fp
+ len
+ 1,
1298 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1301 if (cs
->smultis
== 0) {
1307 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1308 assert(cs
->multis
!= NULL
);
1312 - mcin - is a collating element in a cset?
1313 == static int mcin(register cset *cs, register char *cp);
1320 return(mcfind(cs
, cp
) != NULL
);
1324 - mcfind - find a collating element in a cset
1325 == static char *mcfind(register cset *cs, register char *cp);
1334 if (cs
->multis
== NULL
)
1336 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1337 if (strcmp(cp
, p
) == 0)
1343 - mcinvert - invert the list of collating elements in a cset
1344 == static void mcinvert(register struct parse *p, register cset *cs);
1346 * This would have to know the set of possibilities. Implementation
1351 register struct parse
*p
;
1354 assert(cs
->multis
== NULL
); /* xxx */
1358 - mccase - add case counterparts of the list of collating elements in a cset
1359 == static void mccase(register struct parse *p, register cset *cs);
1361 * This would have to know the set of possibilities. Implementation
1366 register struct parse
*p
;
1369 assert(cs
->multis
== NULL
); /* xxx */
1373 - isinsets - is this character in any sets?
1374 == static int isinsets(register struct re_guts *g, int c);
1376 static int /* predicate */
1378 register struct re_guts
*g
;
1383 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1384 register unsigned uc
= (unsigned char)c
;
1386 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1393 - samesets - are these two characters in exactly the same sets?
1394 == static int samesets(register struct re_guts *g, int c1, int c2);
1396 static int /* predicate */
1398 register struct re_guts
*g
;
1404 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1405 register unsigned uc1
= (unsigned char)c1
;
1406 register unsigned uc2
= (unsigned char)c2
;
1408 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1409 if (col
[uc1
] != col
[uc2
])
1415 - categorize - sort out character categories
1416 == static void categorize(struct parse *p, register struct re_guts *g);
1421 register struct re_guts
*g
;
1423 register cat_t
*cats
= g
->categories
;
1428 /* avoid making error situations worse */
1432 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1433 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1434 cat
= g
->ncategories
++;
1436 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1437 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1443 - dupl - emit a duplicate of a bunch of sops
1444 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1446 static sopno
/* start of duplicate */
1447 dupl(p
, start
, finish
)
1448 register struct parse
*p
;
1449 sopno start
; /* from here */
1450 sopno finish
; /* to this less one */
1452 register sopno ret
= HERE();
1453 register sopno len
= finish
- start
;
1455 assert(finish
>= start
);
1458 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1459 assert(p
->ssize
>= p
->slen
+ len
);
1460 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1461 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1467 - doemit - emit a strip operator
1468 == static void doemit(register struct parse *p, sop op, size_t opnd);
1470 * It might seem better to implement this as a macro with a function as
1471 * hard-case backup, but it's just too big and messy unless there are
1472 * some changes to the data structures. Maybe later.
1476 register struct parse
*p
;
1480 /* avoid making error situations worse */
1484 /* deal with oversize operands ("can't happen", more or less) */
1485 assert(opnd
< 1<<OPSHIFT
);
1487 /* deal with undersized strip */
1488 if (p
->slen
>= p
->ssize
)
1489 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1490 assert(p
->slen
< p
->ssize
);
1492 /* finally, it's all reduced to the easy case */
1493 p
->strip
[p
->slen
++] = SOP(op
, opnd
);
1497 - doinsert - insert a sop into the strip
1498 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1501 doinsert(p
, op
, opnd
, pos
)
1502 register struct parse
*p
;
1511 /* avoid making error situations worse */
1516 EMIT(op
, opnd
); /* do checks, ensure space */
1517 assert(HERE() == sn
+1);
1520 /* adjust paren pointers */
1522 for (i
= 1; i
< NPAREN
; i
++) {
1523 if (p
->pbegin
[i
] >= pos
) {
1526 if (p
->pend
[i
] >= pos
) {
1531 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1532 (HERE()-pos
-1)*sizeof(sop
));
1537 - dofwd - complete a forward reference
1538 == static void dofwd(register struct parse *p, sopno pos, sop value);
1541 dofwd(p
, pos
, value
)
1542 register struct parse
*p
;
1546 /* avoid making error situations worse */
1550 assert(value
< 1<<OPSHIFT
);
1551 p
->strip
[pos
] = OP(p
->strip
[pos
]) | value
;
1555 - enlarge - enlarge the strip
1556 == static void enlarge(register struct parse *p, sopno size);
1560 register struct parse
*p
;
1561 register sopno size
;
1565 if (p
->ssize
>= size
)
1568 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1570 SETERROR(REG_ESPACE
);
1578 - stripsnug - compact the strip
1579 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1583 register struct parse
*p
;
1584 register struct re_guts
*g
;
1586 g
->nstates
= p
->slen
;
1587 g
->strip
= (sop
*)realloc((char *)p
->strip
, p
->slen
* sizeof(sop
));
1588 if (g
->strip
== NULL
) {
1589 SETERROR(REG_ESPACE
);
1590 g
->strip
= p
->strip
;
1595 - findmust - fill in must and mlen with longest mandatory literal string
1596 == static void findmust(register struct parse *p, register struct re_guts *g);
1598 * This algorithm could do fancy things like analyzing the operands of |
1599 * for common subsequences. Someday. This code is simple and finds most
1600 * of the interesting cases.
1602 * Note that must and mlen got initialized during setup.
1607 register struct re_guts
*g
;
1611 register sop
*newstart
;
1612 register sopno newlen
;
1617 /* avoid making error situations worse */
1621 /* find the longest OCHAR sequence in strip */
1623 scan
= g
->strip
+ 1;
1627 case OCHAR
: /* sequence member */
1628 if (newlen
== 0) /* new sequence */
1629 newstart
= scan
- 1;
1632 case OPLUS_
: /* things that don't break one */
1636 case OQUEST_
: /* things that must be skipped */
1642 /* assert() interferes w debug printouts */
1643 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1648 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1650 default: /* things that break a sequence */
1651 if (newlen
> g
->mlen
) { /* ends one */
1658 } while (OP(s
) != OEND
);
1660 if (g
->mlen
== 0) /* there isn't one */
1663 /* turn it into a character string */
1664 g
->must
= malloc((size_t)g
->mlen
+ 1);
1665 if (g
->must
== NULL
) { /* argh; just forget it */
1671 for (i
= g
->mlen
; i
> 0; i
--) {
1672 while (OP(s
= *scan
++) != OCHAR
)
1674 assert(cp
< g
->must
+ g
->mlen
);
1675 *cp
++ = (char)OPND(s
);
1677 assert(cp
== g
->must
+ g
->mlen
);
1678 *cp
++ = '\0'; /* just on general principles */
1682 - pluscount - count + nesting
1683 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1685 static sopno
/* nesting depth */
1688 register struct re_guts
*g
;
1692 register sopno plusnest
= 0;
1693 register sopno maxnest
= 0;
1696 return(0); /* there may not be an OEND */
1698 scan
= g
->strip
+ 1;
1706 if (plusnest
> maxnest
)
1711 } while (OP(s
) != OEND
);