2 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid
[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
42 #endif /* LIBC_SCCS and not lint */
44 #include <sys/types.h>
48 #include <bsd/asciictype.h>
63 * parse structure, passed up and down to avoid global variables and
67 char *next
; /* next character in RE */
68 char *end
; /* end of string (-> NUL normally) */
69 int error
; /* has an error been seen? */
70 sop
*strip
; /* malloced strip */
71 sopno ssize
; /* malloced strip size (allocated) */
72 sopno slen
; /* malloced strip length (used) */
73 int ncsalloc
; /* number of csets allocated */
75 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
76 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
77 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
80 /* ========= begin header generated by ./mkh ========= */
85 /* === regcomp.c === */
86 static void p_ere(struct parse
*p
, int stop
);
87 static void p_ere_exp(struct parse
*p
);
88 static void p_str(struct parse
*p
);
89 static void p_bre(struct parse
*p
, int end1
, int end2
);
90 static int p_simp_re(struct parse
*p
, int starordinary
);
91 static int p_count(struct parse
*p
);
92 static void p_bracket(struct parse
*p
);
93 static void p_b_term(struct parse
*p
, cset
*cs
);
94 static void p_b_cclass(struct parse
*p
, cset
*cs
);
95 static void p_b_eclass(struct parse
*p
, cset
*cs
);
96 static char p_b_symbol(struct parse
*p
);
97 static char p_b_coll_elem(struct parse
*p
, int endc
);
98 static char othercase(int ch
);
99 static void bothcases(struct parse
*p
, int ch
);
100 static void ordinary(struct parse
*p
, int ch
);
101 static void nonnewline(struct parse
*p
);
102 static void repeat(struct parse
*p
, sopno start
, int from
, int to
);
103 static int seterr(struct parse
*p
, int e
);
104 static cset
*allocset(struct parse
*p
);
105 static void freeset(struct parse
*p
, cset
*cs
);
106 static int freezeset(struct parse
*p
, cset
*cs
);
107 static int firstch(struct parse
*p
, cset
*cs
);
108 static int nch(struct parse
*p
, cset
*cs
);
109 static void mcadd(struct parse
*p
, cset
*cs
, char *cp
);
110 static void mcsub(cset
*cs
, char *cp
);
111 static int mcin(cset
*cs
, char *cp
);
112 static char *mcfind(cset
*cs
, char *cp
);
113 static void mcinvert(struct parse
*p
, cset
*cs
);
114 static void mccase(struct parse
*p
, cset
*cs
);
115 static int isinsets(struct re_guts
*g
, int c
);
116 static int samesets(struct re_guts
*g
, int c1
, int c2
);
117 static void categorize(struct parse
*p
, struct re_guts
*g
);
118 static sopno
dupl(struct parse
*p
, sopno start
, sopno finish
);
119 static void doemit(struct parse
*p
, sop op
, size_t opnd
);
120 static void doinsert(struct parse
*p
, sop op
, size_t opnd
, sopno pos
);
121 static void dofwd(struct parse
*p
, sopno pos
, sop value
);
122 static void enlarge(struct parse
*p
, sopno size
);
123 static void stripsnug(struct parse
*p
, struct re_guts
*g
);
124 static void findmust(struct parse
*p
, struct re_guts
*g
);
125 static sopno
pluscount(struct parse
*p
, struct re_guts
*g
);
130 /* ========= end header generated by ./mkh ========= */
132 static char nuls
[10]; /* place to point scanner in event of error */
135 * macros for use with parse structure
136 * BEWARE: these know that the parse structure is named `p' !!!
138 #define PEEK() (*p->next)
139 #define PEEK2() (*(p->next+1))
140 #define MORE() (p->next < p->end)
141 #define MORE2() (p->next+1 < p->end)
142 #define SEE(c) (MORE() && PEEK() == (c))
143 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
144 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
145 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
146 #define NEXT() (p->next++)
147 #define NEXT2() (p->next += 2)
148 #define NEXTn(n) (p->next += (n))
149 #define GETNEXT() (*p->next++)
150 #define SETERROR(e) seterr(p, (e))
151 #define REQUIRE(co, e) ((co) || SETERROR(e))
152 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
153 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
154 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
155 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
156 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
157 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
158 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
159 #define HERE() (p->slen)
160 #define THERE() (p->slen - 1)
161 #define THERETHERE() (p->slen - 2)
162 #define DROP(n) (p->slen -= (n))
165 static int never
= 0; /* for use in asserts; shuts lint up */
167 #define never 0 /* some <assert.h>s have bugs too */
171 - regcomp - interface for parser and compilation
172 = extern int regcomp(regex_t *, const char *, int);
173 = #define REG_BASIC 0000
174 = #define REG_EXTENDED 0001
175 = #define REG_ICASE 0002
176 = #define REG_NOSUB 0004
177 = #define REG_NEWLINE 0010
178 = #define REG_NOSPEC 0020
179 = #define REG_PEND 0040
180 = #define REG_DUMP 0200
182 int /* 0 success, otherwise REG_something */
183 regcomp(preg
, pattern
, cflags
)
189 register struct re_guts
*g
;
190 register struct parse
*p
= &pa
;
194 # define GOODFLAGS(f) (f)
196 # define GOODFLAGS(f) ((f)&~REG_DUMP)
199 cflags
= GOODFLAGS(cflags
);
200 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
203 if (cflags
®_PEND
) {
204 if (preg
->re_endp
< pattern
)
206 len
= preg
->re_endp
- pattern
;
208 len
= strlen((char *)pattern
);
210 /* do the mallocs early so failure handling is easy */
211 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
212 (NC
-1)*sizeof(cat_t
));
215 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
216 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
218 if (p
->strip
== NULL
) {
225 p
->next
= (char *)pattern
; /* convenience; we do not modify it */
226 p
->end
= p
->next
+ len
;
229 for (i
= 0; i
< NPAREN
; i
++) {
244 g
->ncategories
= 1; /* category 0 is "everything else" */
245 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
246 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
251 g
->firststate
= THERE();
252 if (cflags
®_EXTENDED
)
254 else if (cflags
®_NOSPEC
)
259 g
->laststate
= THERE();
261 /* tidy up loose ends and fill things in */
265 g
->nplus
= pluscount(p
, g
);
267 preg
->re_nsub
= g
->nsub
;
269 preg
->re_magic
= MAGIC1
;
271 /* not debugging, so can't rely on the assert() in regexec() */
273 SETERROR(REG_ASSERT
);
276 /* win or lose, we're done */
277 if (p
->error
!= 0) /* lose */
283 - p_ere - ERE parser top level, concatenation and alternation
284 == static void p_ere(register struct parse *p, int stop);
288 register struct parse
*p
;
289 int stop
; /* character this ERE should end at */
292 register sopno prevback
;
293 register sopno prevfwd
;
295 register int first
= 1; /* is this the first alternative? */
298 /* do a bunch of concatenated expressions */
300 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
302 REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
305 break; /* NOTE BREAK OUT */
308 INSERT(OCH_
, conc
); /* offset is wrong */
313 ASTERN(OOR1
, prevback
);
315 AHEAD(prevfwd
); /* fix previous offset */
317 EMIT(OOR2
, 0); /* offset is very wrong */
320 if (!first
) { /* tail-end fixups */
322 ASTERN(O_CH
, prevback
);
325 assert(!MORE() || SEE(stop
));
329 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
330 == static void p_ere_exp(register struct parse *p);
334 register struct parse
*p
;
340 register sopno subno
;
343 assert(MORE()); /* caller should have ensured this */
349 REQUIRE(MORE(), REG_EPAREN
);
353 p
->pbegin
[subno
] = HERE();
354 EMIT(OLPAREN
, subno
);
357 if (subno
< NPAREN
) {
358 p
->pend
[subno
] = HERE();
359 assert(p
->pend
[subno
] != 0);
361 EMIT(ORPAREN
, subno
);
362 MUSTEAT(')', REG_EPAREN
);
364 #ifndef POSIX_MISTAKE
365 case ')': /* happens only if no current unmatched ( */
367 * You may ask, why the ifndef? Because I didn't notice
368 * this until slightly too late for 1003.2, and none of the
369 * other 1003.2 regular-expression reviewers noticed it at
370 * all. So an unmatched ) is legal POSIX, at least until
371 * we can get it fixed.
373 SETERROR(REG_EPAREN
);
378 p
->g
->iflags
|= USEBOL
;
384 p
->g
->iflags
|= USEEOL
;
393 SETERROR(REG_BADRPT
);
396 if (p
->g
->cflags
®_NEWLINE
)
405 REQUIRE(MORE(), REG_EESCAPE
);
409 case '{': /* okay as ordinary except if digit follows */
410 REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT
);
420 /* we call { a repetition if followed by a digit */
421 if (!( c
== '*' || c
== '+' || c
== '?' ||
422 (c
== '{' && MORE2() && isdigit(PEEK2())) ))
423 return; /* no repetition, we're done */
426 REQUIRE(!wascaret
, REG_BADRPT
);
428 case '*': /* implemented as +? */
429 /* this case does not require the (y|) trick, noKLUDGE */
432 INSERT(OQUEST_
, pos
);
433 ASTERN(O_QUEST
, pos
);
440 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
441 INSERT(OCH_
, pos
); /* offset slightly wrong */
442 ASTERN(OOR1
, pos
); /* this one's right */
443 AHEAD(pos
); /* fix the OCH_ */
444 EMIT(OOR2
, 0); /* offset very wrong... */
445 AHEAD(THERE()); /* ...so fix it */
446 ASTERN(O_CH
, THERETHERE());
451 if (isdigit(PEEK())) {
453 REQUIRE(count
<= count2
, REG_BADBR
);
454 } else /* single number with comma */
456 } else /* just a single number */
458 repeat(p
, pos
, count
, count2
);
459 if (!EAT('}')) { /* error heuristics */
460 while (MORE() && PEEK() != '}')
462 REQUIRE(MORE(), REG_EBRACE
);
471 if (!( c
== '*' || c
== '+' || c
== '?' ||
472 (c
== '{' && MORE2() && isdigit(PEEK2())) ) )
474 SETERROR(REG_BADRPT
);
478 - p_str - string (no metacharacters) "parser"
479 == static void p_str(register struct parse *p);
483 register struct parse
*p
;
485 REQUIRE(MORE(), REG_EMPTY
);
487 ordinary(p
, GETNEXT());
491 - p_bre - BRE parser top level, anchoring and concatenation
492 == static void p_bre(register struct parse *p, register int end1, \
493 == register int end2);
494 * Giving end1 as OUT essentially eliminates the end1/end2 check.
496 * This implementation is a bit of a kludge, in that a trailing $ is first
497 * taken as an ordinary character and then revised to be an anchor. The
498 * only undesirable side effect is that '$' gets included as a character
499 * category in such cases. This is fairly harmless; not worth fixing.
500 * The amount of lookahead needed to avoid this kludge is excessive.
504 register struct parse
*p
;
505 register int end1
; /* first terminating character */
506 register int end2
; /* second terminating character */
508 register sopno start
= HERE();
509 register int first
= 1; /* first subexpression? */
510 register int wasdollar
= 0;
514 p
->g
->iflags
|= USEBOL
;
517 while (MORE() && !SEETWO(end1
, end2
)) {
518 wasdollar
= p_simp_re(p
, first
);
521 if (wasdollar
) { /* oops, that was a trailing anchor */
524 p
->g
->iflags
|= USEEOL
;
528 REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
532 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
533 == static int p_simp_re(register struct parse *p, int starordinary);
535 static int /* was the simple RE an unbackslashed $? */
536 p_simp_re(p
, starordinary
)
537 register struct parse
*p
;
538 int starordinary
; /* is a leading * an ordinary character? */
545 register sopno subno
;
546 # define BACKSL (1<<CHAR_BIT)
548 pos
= HERE(); /* repetion op, if any, covers from here */
550 assert(MORE()); /* caller should have ensured this */
553 REQUIRE(MORE(), REG_EESCAPE
);
554 c
= BACKSL
| (unsigned char)GETNEXT();
558 if (p
->g
->cflags
®_NEWLINE
)
567 SETERROR(REG_BADRPT
);
573 p
->pbegin
[subno
] = HERE();
574 EMIT(OLPAREN
, subno
);
575 /* the MORE here is an error heuristic */
576 if (MORE() && !SEETWO('\\', ')'))
578 if (subno
< NPAREN
) {
579 p
->pend
[subno
] = HERE();
580 assert(p
->pend
[subno
] != 0);
582 EMIT(ORPAREN
, subno
);
583 REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
585 case BACKSL
|')': /* should not get here -- must be user */
587 SETERROR(REG_EPAREN
);
598 i
= (c
&~BACKSL
) - '0';
600 if (p
->pend
[i
] != 0) {
601 assert(i
<= p
->g
->nsub
);
603 assert(p
->pbegin
[i
] != 0);
604 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
605 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
606 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
609 SETERROR(REG_ESUBREG
);
613 REQUIRE(starordinary
, REG_BADRPT
);
616 ordinary(p
, c
&~ BACKSL
);
620 if (EAT('*')) { /* implemented as +? */
621 /* this case does not require the (y|) trick, noKLUDGE */
624 INSERT(OQUEST_
, pos
);
625 ASTERN(O_QUEST
, pos
);
626 } else if (EATTWO('\\', '{')) {
629 if (MORE() && isdigit(PEEK())) {
631 REQUIRE(count
<= count2
, REG_BADBR
);
632 } else /* single number with comma */
634 } else /* just a single number */
636 repeat(p
, pos
, count
, count2
);
637 if (!EATTWO('\\', '}')) { /* error heuristics */
638 while (MORE() && !SEETWO('\\', '}'))
640 REQUIRE(MORE(), REG_EBRACE
);
643 } else if (c
== (unsigned char)'$') /* $ (but not \$) ends it */
650 - p_count - parse a repetition count
651 == static int p_count(register struct parse *p);
653 static int /* the value */
655 register struct parse
*p
;
657 register int count
= 0;
658 register int ndigits
= 0;
660 while (MORE() && isdigit(PEEK()) && count
<= DUPMAX
) {
661 count
= count
*10 + (GETNEXT() - '0');
665 REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
670 - p_bracket - parse a bracketed character list
671 == static void p_bracket(register struct parse *p);
673 * Note a significant property of this code: if the allocset() did SETERROR,
674 * no set operations are done.
678 register struct parse
*p
;
681 register cset
*cs
= allocset(p
);
682 register int invert
= 0;
684 /* Dept of Truly Sickening Special-Case Kludges */
685 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:<:]]", 6) == 0) {
690 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:>:]]", 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 char *sp
= p
->next
;
823 register struct cclass
*cp
;
828 while (MORE() && isalpha(PEEK()))
831 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
832 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
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 char *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 (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
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 char *oldnext
= p
->next
;
943 register char *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
;
967 register cat_t
*cap
= p
->g
->categories
;
969 if ((p
->g
->cflags
®_ICASE
) && isalpha(ch
) && othercase(ch
) != ch
)
972 EMIT(OCHAR
, (unsigned char)ch
);
974 cap
[ch
] = p
->g
->ncategories
++;
979 - nonnewline - emit REG_NEWLINE version of OANY
980 == static void nonnewline(register struct parse *p);
982 * Boy, is this implementation ever a kludge...
986 register struct parse
*p
;
988 register char *oldnext
= p
->next
;
989 register char *oldend
= p
->end
;
999 assert(p
->next
== bracket
+3);
1005 - repeat - generate code for a bounded repetition, recursively if needed
1006 == static void repeat(register struct parse *p, sopno start, int from, int to);
1009 repeat(p
, start
, from
, to
)
1010 register struct parse
*p
;
1011 sopno start
; /* operand from here to end of strip */
1012 int from
; /* repeated from this number */
1013 int to
; /* to this number of times (maybe INFINITY) */
1015 register sopno finish
= HERE();
1018 # define REP(f, t) ((f)*8 + (t))
1019 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1020 register sopno copy
;
1022 if (p
->error
!= 0) /* head off possible runaway recursion */
1027 switch (REP(MAP(from
), MAP(to
))) {
1028 case REP(0, 0): /* must be user doing this */
1029 DROP(finish
-start
); /* drop the operand */
1031 case REP(0, 1): /* as x{1,1}? */
1032 case REP(0, N
): /* as x{1,n}? */
1033 case REP(0, INF
): /* as x{1,}? */
1034 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1035 INSERT(OCH_
, start
); /* offset is wrong... */
1036 repeat(p
, start
+1, 1, to
);
1037 ASTERN(OOR1
, start
);
1038 AHEAD(start
); /* ... fix it */
1041 ASTERN(O_CH
, THERETHERE());
1043 case REP(1, 1): /* trivial case */
1046 case REP(1, N
): /* as x?x{1,n-1} */
1047 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1048 INSERT(OCH_
, start
);
1049 ASTERN(OOR1
, start
);
1051 EMIT(OOR2
, 0); /* offset very wrong... */
1052 AHEAD(THERE()); /* ...so fix it */
1053 ASTERN(O_CH
, THERETHERE());
1054 copy
= dupl(p
, start
+1, finish
+1);
1055 assert(copy
== finish
+4);
1056 repeat(p
, copy
, 1, to
-1);
1058 case REP(1, INF
): /* as x+ */
1059 INSERT(OPLUS_
, start
);
1060 ASTERN(O_PLUS
, start
);
1062 case REP(N
, N
): /* as xx{m-1,n-1} */
1063 copy
= dupl(p
, start
, finish
);
1064 repeat(p
, copy
, from
-1, to
-1);
1066 case REP(N
, INF
): /* as xx{n-1,INF} */
1067 copy
= dupl(p
, start
, finish
);
1068 repeat(p
, copy
, from
-1, to
);
1070 default: /* "can't happen" */
1071 SETERROR(REG_ASSERT
); /* just in case */
1077 - seterr - set an error condition
1078 == static int seterr(register struct parse *p, int e);
1080 static int /* useless but makes type checking happy */
1082 register struct parse
*p
;
1085 if (p
->error
== 0) /* keep earliest error condition */
1087 p
->next
= nuls
; /* try to bring things to a halt */
1089 return(0); /* make the return value well-defined */
1093 - allocset - allocate a set of characters for []
1094 == static cset *allocset(register struct parse *p);
1098 register struct parse
*p
;
1100 register int no
= p
->g
->ncsets
++;
1102 register size_t nbytes
;
1104 register size_t css
= (size_t)p
->g
->csetsize
;
1107 if (no
>= p
->ncsalloc
) { /* need another column of space */
1108 p
->ncsalloc
+= CHAR_BIT
;
1110 assert(nc
% CHAR_BIT
== 0);
1111 nbytes
= nc
/ CHAR_BIT
* css
;
1112 if (p
->g
->sets
== NULL
)
1113 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1115 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1117 if (p
->g
->setbits
== NULL
)
1118 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1120 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1122 /* xxx this isn't right if setbits is now NULL */
1123 for (i
= 0; i
< no
; i
++)
1124 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1126 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1127 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1131 SETERROR(REG_ESPACE
);
1132 /* caller's responsibility not to do set ops */
1136 assert(p
->g
->sets
!= NULL
); /* xxx */
1137 cs
= &p
->g
->sets
[no
];
1138 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1139 cs
->mask
= 1 << ((no
) % CHAR_BIT
);
1148 - freeset - free a now-unused set
1149 == static void freeset(register struct parse *p, register cset *cs);
1153 register struct parse
*p
;
1157 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1158 register size_t css
= (size_t)p
->g
->csetsize
;
1160 for (i
= 0; i
< css
; i
++)
1162 if (cs
== top
-1) /* recover only the easy case */
1167 - freezeset - final processing on a set of characters
1168 == static int freezeset(register struct parse *p, register cset *cs);
1170 * The main task here is merging identical sets. This is usually a waste
1171 * of time (although the hash code minimizes the overhead), but can win
1172 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1173 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1176 static int /* set number */
1178 register struct parse
*p
;
1181 register uch h
= cs
->hash
;
1183 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1185 register size_t css
= (size_t)p
->g
->csetsize
;
1187 /* look for an earlier one which is the same */
1188 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1189 if (cs2
->hash
== h
&& cs2
!= cs
) {
1191 for (i
= 0; i
< css
; i
++)
1192 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1198 if (cs2
< top
) { /* found one */
1203 return((int)(cs
- p
->g
->sets
));
1207 - firstch - return first character in a set (which must have at least one)
1208 == static int firstch(register struct parse *p, register cset *cs);
1210 static int /* character; there is no "none" value */
1212 register struct parse
*p
;
1216 register size_t css
= (size_t)p
->g
->csetsize
;
1218 for (i
= 0; i
< css
; i
++)
1222 return(0); /* arbitrary */
1226 - nch - number of characters in a set
1227 == static int nch(register struct parse *p, register cset *cs);
1231 register struct parse
*p
;
1235 register size_t css
= (size_t)p
->g
->csetsize
;
1238 for (i
= 0; i
< css
; i
++)
1245 - mcadd - add a collating element to a cset
1246 == static void mcadd(register struct parse *p, register cset *cs, \
1247 == register char *cp);
1251 register struct parse
*p
;
1255 register size_t oldend
= cs
->smultis
;
1257 cs
->smultis
+= strlen(cp
) + 1;
1258 if (cs
->multis
== NULL
)
1259 cs
->multis
= malloc(cs
->smultis
);
1261 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1262 if (cs
->multis
== NULL
) {
1263 SETERROR(REG_ESPACE
);
1267 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1268 cs
->multis
[cs
->smultis
- 1] = '\0';
1272 - mcsub - subtract a collating element from a cset
1273 == static void mcsub(register cset *cs, register char *cp);
1280 register char *fp
= mcfind(cs
, cp
);
1281 register size_t len
= strlen(fp
);
1284 (void) memmove(fp
, fp
+ len
+ 1,
1285 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1288 if (cs
->smultis
== 0) {
1294 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1295 assert(cs
->multis
!= NULL
);
1299 - mcin - is a collating element in a cset?
1300 == static int mcin(register cset *cs, register char *cp);
1307 return(mcfind(cs
, cp
) != NULL
);
1311 - mcfind - find a collating element in a cset
1312 == static char *mcfind(register cset *cs, register char *cp);
1321 if (cs
->multis
== NULL
)
1323 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1324 if (strcmp(cp
, p
) == 0)
1330 - mcinvert - invert the list of collating elements in a cset
1331 == static void mcinvert(register struct parse *p, register cset *cs);
1333 * This would have to know the set of possibilities. Implementation
1338 register struct parse
*p
;
1341 assert(cs
->multis
== NULL
); /* xxx */
1345 - mccase - add case counterparts of the list of collating elements in a cset
1346 == static void mccase(register struct parse *p, register cset *cs);
1348 * This would have to know the set of possibilities. Implementation
1353 register struct parse
*p
;
1356 assert(cs
->multis
== NULL
); /* xxx */
1360 - isinsets - is this character in any sets?
1361 == static int isinsets(register struct re_guts *g, int c);
1363 static int /* predicate */
1365 register struct re_guts
*g
;
1370 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1371 register unsigned uc
= (unsigned char)c
;
1373 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1380 - samesets - are these two characters in exactly the same sets?
1381 == static int samesets(register struct re_guts *g, int c1, int c2);
1383 static int /* predicate */
1385 register struct re_guts
*g
;
1391 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1392 register unsigned uc1
= (unsigned char)c1
;
1393 register unsigned uc2
= (unsigned char)c2
;
1395 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1396 if (col
[uc1
] != col
[uc2
])
1402 - categorize - sort out character categories
1403 == static void categorize(struct parse *p, register struct re_guts *g);
1408 register struct re_guts
*g
;
1410 register cat_t
*cats
= g
->categories
;
1415 /* avoid making error situations worse */
1419 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1420 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1421 cat
= g
->ncategories
++;
1423 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1424 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1430 - dupl - emit a duplicate of a bunch of sops
1431 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1433 static sopno
/* start of duplicate */
1434 dupl(p
, start
, finish
)
1435 register struct parse
*p
;
1436 sopno start
; /* from here */
1437 sopno finish
; /* to this less one */
1439 register sopno ret
= HERE();
1440 register sopno len
= finish
- start
;
1442 assert(finish
>= start
);
1445 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1446 assert(p
->ssize
>= p
->slen
+ len
);
1447 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1448 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1454 - doemit - emit a strip operator
1455 == static void doemit(register struct parse *p, sop op, size_t opnd);
1457 * It might seem better to implement this as a macro with a function as
1458 * hard-case backup, but it's just too big and messy unless there are
1459 * some changes to the data structures. Maybe later.
1463 register struct parse
*p
;
1467 /* avoid making error situations worse */
1471 /* deal with oversize operands ("can't happen", more or less) */
1472 assert(opnd
< 1<<OPSHIFT
);
1474 /* deal with undersized strip */
1475 if (p
->slen
>= p
->ssize
)
1476 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1477 assert(p
->slen
< p
->ssize
);
1479 /* finally, it's all reduced to the easy case */
1480 p
->strip
[p
->slen
++] = SOP(op
, opnd
);
1484 - doinsert - insert a sop into the strip
1485 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1488 doinsert(p
, op
, opnd
, pos
)
1489 register struct parse
*p
;
1498 /* avoid making error situations worse */
1503 EMIT(op
, opnd
); /* do checks, ensure space */
1504 assert(HERE() == sn
+1);
1507 /* adjust paren pointers */
1509 for (i
= 1; i
< NPAREN
; i
++) {
1510 if (p
->pbegin
[i
] >= pos
) {
1513 if (p
->pend
[i
] >= pos
) {
1518 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1519 (HERE()-pos
-1)*sizeof(sop
));
1524 - dofwd - complete a forward reference
1525 == static void dofwd(register struct parse *p, sopno pos, sop value);
1528 dofwd(p
, pos
, value
)
1529 register struct parse
*p
;
1533 /* avoid making error situations worse */
1537 assert(value
< 1<<OPSHIFT
);
1538 p
->strip
[pos
] = OP(p
->strip
[pos
]) | value
;
1542 - enlarge - enlarge the strip
1543 == static void enlarge(register struct parse *p, sopno size);
1547 register struct parse
*p
;
1548 register sopno size
;
1552 if (p
->ssize
>= size
)
1555 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1557 SETERROR(REG_ESPACE
);
1565 - stripsnug - compact the strip
1566 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1570 register struct parse
*p
;
1571 register struct re_guts
*g
;
1573 g
->nstates
= p
->slen
;
1574 g
->strip
= (sop
*)realloc((char *)p
->strip
, p
->slen
* sizeof(sop
));
1575 if (g
->strip
== NULL
) {
1576 SETERROR(REG_ESPACE
);
1577 g
->strip
= p
->strip
;
1582 - findmust - fill in must and mlen with longest mandatory literal string
1583 == static void findmust(register struct parse *p, register struct re_guts *g);
1585 * This algorithm could do fancy things like analyzing the operands of |
1586 * for common subsequences. Someday. This code is simple and finds most
1587 * of the interesting cases.
1589 * Note that must and mlen got initialized during setup.
1594 register struct re_guts
*g
;
1598 register sop
*newstart
;
1599 register sopno newlen
;
1604 /* avoid making error situations worse */
1608 /* find the longest OCHAR sequence in strip */
1610 scan
= g
->strip
+ 1;
1614 case OCHAR
: /* sequence member */
1615 if (newlen
== 0) /* new sequence */
1616 newstart
= scan
- 1;
1619 case OPLUS_
: /* things that don't break one */
1623 case OQUEST_
: /* things that must be skipped */
1629 /* assert() interferes w debug printouts */
1630 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1635 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1637 default: /* things that break a sequence */
1638 if (newlen
> g
->mlen
) { /* ends one */
1645 } while (OP(s
) != OEND
);
1647 if (g
->mlen
== 0) /* there isn't one */
1650 /* turn it into a character string */
1651 g
->must
= malloc((size_t)g
->mlen
+ 1);
1652 if (g
->must
== NULL
) { /* argh; just forget it */
1658 for (i
= g
->mlen
; i
> 0; i
--) {
1659 while (OP(s
= *scan
++) != OCHAR
)
1661 assert(cp
< g
->must
+ g
->mlen
);
1662 *cp
++ = (char)OPND(s
);
1664 assert(cp
== g
->must
+ g
->mlen
);
1665 *cp
++ = '\0'; /* just on general principles */
1669 - pluscount - count + nesting
1670 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1672 static sopno
/* nesting depth */
1675 register struct re_guts
*g
;
1679 register sopno plusnest
= 0;
1680 register sopno maxnest
= 0;
1683 return(0); /* there may not be an OEND */
1685 scan
= g
->strip
+ 1;
1693 if (plusnest
> maxnest
)
1698 } while (OP(s
) != OEND
);
1705 * $PchId: regcomp.c,v 1.2 1996/03/12 19:10:15 philip Exp $