16 * parse structure, passed up and down to avoid global variables and
20 char *next
; /* next character in RE */
21 char *end
; /* end of string (-> NUL normally) */
22 int error
; /* has an error been seen? */
23 sop
*strip
; /* malloced strip */
24 sopno ssize
; /* malloced strip size (allocated) */
25 sopno slen
; /* malloced strip length (used) */
26 int ncsalloc
; /* number of csets allocated */
28 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
29 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
30 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
31 CHARSET_INFO
*charset
; /* for ctype things */
34 /* Check if there is enough stack space for recursion. */
35 my_regex_stack_check_t my_regex_enough_mem_in_stack
= NULL
;
39 static char nuls
[10]; /* place to point scanner in event of error */
41 struct cclass cclasses
[CCLASS_LAST
+1]= {
42 { "alnum", "","", _MY_U
| _MY_L
| _MY_NMR
},
43 { "alpha", "","", _MY_U
| _MY_L
},
44 { "blank", "","", _MY_B
},
45 { "cntrl", "","", _MY_CTR
},
46 { "digit", "","", _MY_NMR
},
47 { "graph", "","", _MY_PNT
| _MY_U
| _MY_L
| _MY_NMR
},
48 { "lower", "","", _MY_L
},
49 { "print", "","", _MY_PNT
| _MY_U
| _MY_L
| _MY_NMR
| _MY_B
},
50 { "punct", "","", _MY_PNT
},
51 { "space", "","", _MY_SPC
},
52 { "upper", "","", _MY_U
},
53 { "xdigit", "","", _MY_X
},
58 * macros for use with parse structure
59 * BEWARE: these know that the parse structure is named `p' !!!
61 #define PEEK() (*p->next)
62 #define PEEK2() (*(p->next+1))
63 #define MORE() (p->next < p->end)
64 #define MORE2() (p->next+1 < p->end)
65 #define SEE(c) (MORE() && PEEK() == (c))
66 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
67 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
68 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
69 #define NEXT() (p->next++)
70 #define NEXT2() (p->next += 2)
71 #define NEXTn(n) (p->next += (n))
72 #define GETNEXT() (*p->next++)
73 #define SETERROR(e) seterr(p, (e))
74 #define REQUIRE(co, e) ((co) || SETERROR(e))
75 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
76 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
77 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
78 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
79 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
80 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
81 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
82 #define HERE() (p->slen)
83 #define THERE() (p->slen - 1)
84 #define THERETHERE() (p->slen - 2)
85 #define DROP(n) (p->slen -= (n))
88 static int never
= 0; /* for use in asserts; shuts lint up */
90 #define never 0 /* some <assert.h>s have bugs too */
94 - regcomp - interface for parser and compilation
95 = extern int regcomp(regex_t *, const char *, int);
96 = #define REG_BASIC 0000
97 = #define REG_EXTENDED 0001
98 = #define REG_ICASE 0002
99 = #define REG_NOSUB 0004
100 = #define REG_NEWLINE 0010
101 = #define REG_NOSPEC 0020
102 = #define REG_PEND 0040
103 = #define REG_DUMP 0200
105 int /* 0 success, otherwise REG_something */
106 my_regcomp(preg
, pattern
, cflags
, charset
)
110 CHARSET_INFO
*charset
;
113 register struct re_guts
*g
;
114 register struct parse
*p
= &pa
;
118 # define GOODFLAGS(f) (f)
120 # define GOODFLAGS(f) ((f)&~REG_DUMP)
123 my_regex_init(charset
, NULL
); /* Init cclass if neaded */
124 preg
->charset
=charset
;
125 cflags
= GOODFLAGS(cflags
);
126 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
129 if (cflags
®_PEND
) {
130 if (preg
->re_endp
< pattern
)
132 len
= preg
->re_endp
- pattern
;
134 len
= strlen((char *)pattern
);
136 /* do the mallocs early so failure handling is easy */
137 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
) +
138 (NC
-1)*sizeof(cat_t
));
141 p
->ssize
= (long) (len
/(size_t)2*(size_t)3 + (size_t)1); /* ugh */
142 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
144 if (p
->strip
== NULL
) {
151 p
->next
= (char *)pattern
; /* convenience; we do not modify it */
152 p
->end
= p
->next
+ len
;
155 p
->charset
= preg
->charset
;
156 for (i
= 0; i
< NPAREN
; i
++) {
171 g
->ncategories
= 1; /* category 0 is "everything else" */
172 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
173 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
178 g
->firststate
= THERE();
179 if (cflags
®_EXTENDED
)
181 else if (cflags
®_NOSPEC
)
186 g
->laststate
= THERE();
188 /* tidy up loose ends and fill things in */
192 g
->nplus
= pluscount(p
, g
);
194 preg
->re_nsub
= g
->nsub
;
196 preg
->re_magic
= MAGIC1
;
198 /* not debugging, so can't rely on the assert() in regexec() */
200 SETERROR(REG_ASSERT
);
203 /* win or lose, we're done */
204 if (p
->error
!= 0) /* lose */
210 - p_ere - ERE parser top level, concatenation and alternation
211 == static void p_ere(register struct parse *p, int stop);
215 register struct parse
*p
;
216 int stop
; /* character this ERE should end at */
219 register sopno
UNINIT_VAR(prevback
);
220 register sopno
UNINIT_VAR(prevfwd
);
222 register int first
= 1; /* is this the first alternative? */
225 /* do a bunch of concatenated expressions */
227 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
229 if (my_regex_enough_mem_in_stack
&&
230 my_regex_enough_mem_in_stack(0))
232 SETERROR(REG_ESPACE
);
237 if(REQUIRE(HERE() != conc
, REG_EMPTY
)) {}/* require nonempty */
240 break; /* NOTE BREAK OUT */
243 INSERT(OCH_
, conc
); /* offset is wrong */
248 ASTERN(OOR1
, prevback
);
250 AHEAD(prevfwd
); /* fix previous offset */
252 EMIT(OOR2
, 0); /* offset is very wrong */
255 if (!first
) { /* tail-end fixups */
257 ASTERN(O_CH
, prevback
);
260 assert(!MORE() || SEE(stop
));
264 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
265 == static void p_ere_exp(register struct parse *p);
269 register struct parse
*p
;
275 register sopno subno
;
278 assert(MORE()); /* caller should have ensured this */
284 if(REQUIRE(MORE(), REG_EPAREN
)) {}
286 subno
= (sopno
) p
->g
->nsub
;
288 p
->pbegin
[subno
] = HERE();
289 EMIT(OLPAREN
, subno
);
292 if (subno
< NPAREN
) {
293 p
->pend
[subno
] = HERE();
294 assert(p
->pend
[subno
] != 0);
296 EMIT(ORPAREN
, subno
);
297 if(MUSTEAT(')', REG_EPAREN
)) {}
299 #ifndef POSIX_MISTAKE
300 case ')': /* happens only if no current unmatched ( */
302 * You may ask, why the ifndef? Because I didn't notice
303 * this until slightly too late for 1003.2, and none of the
304 * other 1003.2 regular-expression reviewers noticed it at
305 * all. So an unmatched ) is legal POSIX, at least until
306 * we can get it fixed.
308 SETERROR(REG_EPAREN
);
313 p
->g
->iflags
|= USEBOL
;
319 p
->g
->iflags
|= USEEOL
;
328 SETERROR(REG_BADRPT
);
331 if (p
->g
->cflags
®_NEWLINE
)
340 if(REQUIRE(MORE(), REG_EESCAPE
)) {}
344 case '{': /* okay as ordinary except if digit follows */
345 if(REQUIRE(!MORE() || !my_isdigit(p
->charset
,PEEK()), REG_BADRPT
)) {}
355 /* we call { a repetition if followed by a digit */
356 if (!( c
== '*' || c
== '+' || c
== '?' ||
357 (c
== '{' && MORE2() &&
358 my_isdigit(p
->charset
,PEEK2())) ))
359 return; /* no repetition, we're done */
362 if(REQUIRE(!wascaret
, REG_BADRPT
)) {}
364 case '*': /* implemented as +? */
365 /* this case does not require the (y|) trick, noKLUDGE */
368 INSERT(OQUEST_
, pos
);
369 ASTERN(O_QUEST
, pos
);
376 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
377 INSERT(OCH_
, pos
); /* offset slightly wrong */
378 ASTERN(OOR1
, pos
); /* this one's right */
379 AHEAD(pos
); /* fix the OCH_ */
380 EMIT(OOR2
, 0); /* offset very wrong... */
381 AHEAD(THERE()); /* ...so fix it */
382 ASTERN(O_CH
, THERETHERE());
387 if (my_isdigit(p
->charset
,PEEK())) {
389 if(REQUIRE(count
<= count2
, REG_BADBR
)) {}
390 } else /* single number with comma */
391 count2
= RE_INFINITY
;
392 } else /* just a single number */
394 repeat(p
, pos
, count
, count2
);
395 if (!EAT('}')) { /* error heuristics */
396 while (MORE() && PEEK() != '}')
398 if(REQUIRE(MORE(), REG_EBRACE
)) {}
407 if (!( c
== '*' || c
== '+' || c
== '?' ||
408 (c
== '{' && MORE2() &&
409 my_isdigit(p
->charset
,PEEK2())) ) )
411 SETERROR(REG_BADRPT
);
415 - p_str - string (no metacharacters) "parser"
416 == static void p_str(register struct parse *p);
420 register struct parse
*p
;
422 if(REQUIRE(MORE(), REG_EMPTY
)) {}
424 ordinary(p
, GETNEXT());
428 - p_bre - BRE parser top level, anchoring and concatenation
429 == static void p_bre(register struct parse *p, register int end1, \
430 == register int end2);
431 * Giving end1 as OUT essentially eliminates the end1/end2 check.
433 * This implementation is a bit of a kludge, in that a trailing $ is first
434 * taken as an ordinary character and then revised to be an anchor. The
435 * only undesirable side effect is that '$' gets included as a character
436 * category in such cases. This is fairly harmless; not worth fixing.
437 * The amount of lookahead needed to avoid this kludge is excessive.
441 register struct parse
*p
;
442 register int end1
; /* first terminating character */
443 register int end2
; /* second terminating character */
445 register sopno start
= HERE();
446 register int first
= 1; /* first subexpression? */
447 register int wasdollar
= 0;
451 p
->g
->iflags
|= USEBOL
;
454 while (MORE() && !SEETWO(end1
, end2
)) {
455 wasdollar
= p_simp_re(p
, first
);
458 if (wasdollar
) { /* oops, that was a trailing anchor */
461 p
->g
->iflags
|= USEEOL
;
465 if(REQUIRE(HERE() != start
, REG_EMPTY
)) {} /* require nonempty */
469 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
470 == static int p_simp_re(register struct parse *p, int starordinary);
472 static int /* was the simple RE an unbackslashed $? */
473 p_simp_re(p
, starordinary
)
474 register struct parse
*p
;
475 int starordinary
; /* is a leading * an ordinary character? */
482 register sopno subno
;
483 # define BACKSL (1<<CHAR_BIT)
485 pos
= HERE(); /* repetion op, if any, covers from here */
487 assert(MORE()); /* caller should have ensured this */
490 if(REQUIRE(MORE(), REG_EESCAPE
)) {}
491 c
= BACKSL
| (unsigned char)GETNEXT();
495 if (p
->g
->cflags
®_NEWLINE
)
504 SETERROR(REG_BADRPT
);
508 subno
= (sopno
) p
->g
->nsub
;
510 p
->pbegin
[subno
] = HERE();
511 EMIT(OLPAREN
, subno
);
512 /* the MORE here is an error heuristic */
513 if (MORE() && !SEETWO('\\', ')'))
515 if (subno
< NPAREN
) {
516 p
->pend
[subno
] = HERE();
517 assert(p
->pend
[subno
] != 0);
519 EMIT(ORPAREN
, subno
);
520 if(REQUIRE(EATTWO('\\', ')'), REG_EPAREN
)) {}
522 case BACKSL
|')': /* should not get here -- must be user */
524 SETERROR(REG_EPAREN
);
535 i
= (c
&~BACKSL
) - '0';
537 if (p
->pend
[i
] != 0) {
538 assert((uint
) i
<= p
->g
->nsub
);
540 assert(p
->pbegin
[i
] != 0);
541 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
542 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
543 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
546 SETERROR(REG_ESUBREG
);
550 if(REQUIRE(starordinary
, REG_BADRPT
)) {}
553 ordinary(p
, c
&~ BACKSL
);
557 if (EAT('*')) { /* implemented as +? */
558 /* this case does not require the (y|) trick, noKLUDGE */
561 INSERT(OQUEST_
, pos
);
562 ASTERN(O_QUEST
, pos
);
563 } else if (EATTWO('\\', '{')) {
566 if (MORE() && my_isdigit(p
->charset
,PEEK())) {
568 if(REQUIRE(count
<= count2
, REG_BADBR
)) {}
569 } else /* single number with comma */
570 count2
= RE_INFINITY
;
571 } else /* just a single number */
573 repeat(p
, pos
, count
, count2
);
574 if (!EATTWO('\\', '}')) { /* error heuristics */
575 while (MORE() && !SEETWO('\\', '}'))
577 if(REQUIRE(MORE(), REG_EBRACE
)) {}
580 } else if (c
== (unsigned char)'$') /* $ (but not \$) ends it */
587 - p_count - parse a repetition count
588 == static int p_count(register struct parse *p);
590 static int /* the value */
592 register struct parse
*p
;
594 register int count
= 0;
595 register int ndigits
= 0;
597 while (MORE() && my_isdigit(p
->charset
,PEEK()) && count
<= DUPMAX
) {
598 count
= count
*10 + (GETNEXT() - '0');
602 if(REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
)) {}
607 - p_bracket - parse a bracketed character list
608 == static void p_bracket(register struct parse *p);
610 * Note a significant property of this code: if the allocset() did SETERROR,
611 * no set operations are done.
615 register struct parse
*p
;
617 register cset
*cs
= allocset(p
);
618 register int invert
= 0;
620 /* Dept of Truly Sickening Special-Case Kludges */
621 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:<:]]", 6) == 0) {
626 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:>:]]", 6) == 0) {
633 invert
++; /* make note to invert set at end */
638 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
642 if(MUSTEAT(']', REG_EBRACK
)) {}
644 if (p
->error
!= 0) /* don't mess things up further */
647 if (p
->g
->cflags
®_ICASE
) {
651 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
652 if (CHIN(cs
, i
) && my_isalpha(p
->charset
,i
)) {
653 ci
= othercase(p
->charset
,i
);
657 if (cs
->multis
!= NULL
)
663 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
668 if (p
->g
->cflags
®_NEWLINE
)
670 if (cs
->multis
!= NULL
)
674 assert(cs
->multis
== NULL
); /* xxx */
676 if (nch(p
, cs
) == 1) { /* optimize singleton sets */
677 ordinary(p
, firstch(p
, cs
));
680 EMIT(OANYOF
, freezeset(p
, cs
));
684 - p_b_term - parse one term of a bracketed character list
685 == static void p_b_term(register struct parse *p, register cset *cs);
689 register struct parse
*p
;
693 register char start
, finish
;
696 /* classify what we've got */
697 switch ((MORE()) ? PEEK() : '\0') {
699 c
= (MORE2()) ? PEEK2() : '\0';
702 SETERROR(REG_ERANGE
);
703 return; /* NOTE RETURN */
710 case ':': /* character class */
712 if(REQUIRE(MORE(), REG_EBRACK
)) {}
714 if(REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
)) {}
716 if(REQUIRE(MORE(), REG_EBRACK
)) {}
717 if(REQUIRE(EATTWO(':', ']'), REG_ECTYPE
)) {}
719 case '=': /* equivalence class */
721 if(REQUIRE(MORE(), REG_EBRACK
)) {}
723 if(REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
)) {}
725 if(REQUIRE(MORE(), REG_EBRACK
)) {}
726 if(REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
)) {}
728 default: /* symbol, ordinary character, or range */
729 /* xxx revision needed for multichar stuff */
730 start
= p_b_symbol(p
);
731 if (SEE('-') && MORE2() && PEEK2() != ']') {
737 finish
= p_b_symbol(p
);
740 /* xxx what about signed chars here... */
741 if(REQUIRE(start
<= finish
, REG_ERANGE
)) {}
742 for (i
= start
; i
<= finish
; i
++)
749 - p_b_cclass - parse a character-class name and deal with it
750 == static void p_b_cclass(register struct parse *p, register cset *cs);
754 register struct parse
*p
;
757 register char *sp
= p
->next
;
758 register struct cclass
*cp
;
761 while (MORE() && my_isalpha(p
->charset
,PEEK()))
764 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
765 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
767 if (cp
->name
== NULL
) {
768 /* oops, didn't find it */
769 SETERROR(REG_ECTYPE
);
773 #ifndef USE_ORIG_REGEX_CODE
776 for (i
=1 ; i
<256 ; i
++)
777 if (p
->charset
->ctype
[i
+1] & cp
->mask
)
782 register char *u
= (char*) cp
->chars
;
785 while ((c
= *u
++) != '\0')
788 for (u
= (char*) cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
796 - p_b_eclass - parse an equivalence-class name and deal with it
797 == static void p_b_eclass(register struct parse *p, register cset *cs);
799 * This implementation is incomplete. xxx
803 register struct parse
*p
;
808 c
= p_b_coll_elem(p
, '=');
813 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
814 == static char p_b_symbol(register struct parse *p);
816 static char /* value of symbol */
818 register struct parse
*p
;
822 if(REQUIRE(MORE(), REG_EBRACK
)) {}
823 if (!EATTWO('[', '.'))
826 /* collating symbol */
827 value
= p_b_coll_elem(p
, '.');
828 if(REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
)) {}
833 - p_b_coll_elem - parse a collating-element name and look it up
834 == static char p_b_coll_elem(register struct parse *p, int endc);
836 static char /* value of collating element */
837 p_b_coll_elem(p
, endc
)
838 register struct parse
*p
;
839 int endc
; /* name ended by endc,']' */
841 register char *sp
= p
->next
;
842 register struct cname
*cp
;
844 register __int64 len
;
848 while (MORE() && !SEETWO(endc
, ']'))
851 SETERROR(REG_EBRACK
);
855 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
856 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
857 return(cp
->code
); /* known name */
859 return(*sp
); /* single character */
860 SETERROR(REG_ECOLLATE
); /* neither */
865 - othercase - return the case counterpart of an alphabetic
866 == static char othercase(int ch);
868 static char /* if no counterpart, return ch */
869 othercase(charset
,ch
)
870 CHARSET_INFO
*charset
;
874 In MySQL some multi-byte character sets
875 have 'ctype' array but don't have 'to_lower'
876 and 'to_upper' arrays. In this case we handle
877 only basic latin letters a..z and A..Z.
879 If 'to_lower' and 'to_upper' arrays are empty in a character set,
880 then my_isalpha(cs, ch) should never return TRUE for characters
881 other than basic latin letters. Otherwise it should be
882 considered as a mistake in character set definition.
884 assert(my_isalpha(charset
,ch
));
885 if (my_isupper(charset
,ch
))
887 return(charset
->to_lower
? my_tolower(charset
,ch
) :
890 else if (my_islower(charset
,ch
))
892 return(charset
->to_upper
? my_toupper(charset
,ch
) :
895 else /* peculiar, but could happen */
900 - bothcases - emit a dualcase version of a two-case character
901 == static void bothcases(register struct parse *p, int ch);
903 * Boy, is this implementation ever a kludge...
907 register struct parse
*p
;
910 register char *oldnext
= p
->next
;
911 register char *oldend
= p
->end
;
914 assert(othercase(p
->charset
, ch
) != ch
); /* p_bracket() would recurse */
921 assert(p
->next
== bracket
+2);
927 - ordinary - emit an ordinary character
928 == static void ordinary(register struct parse *p, register int ch);
932 register struct parse
*p
;
935 register cat_t
*cap
= p
->g
->categories
;
937 if ((p
->g
->cflags
®_ICASE
) && my_isalpha(p
->charset
,ch
) &&
938 othercase(p
->charset
,ch
) != ch
)
941 EMIT(OCHAR
, (unsigned char)ch
);
943 cap
[ch
] = p
->g
->ncategories
++;
948 - nonnewline - emit REG_NEWLINE version of OANY
949 == static void nonnewline(register struct parse *p);
951 * Boy, is this implementation ever a kludge...
955 register struct parse
*p
;
957 register char *oldnext
= p
->next
;
958 register char *oldend
= p
->end
;
968 assert(p
->next
== bracket
+3);
974 - repeat - generate code for a bounded repetition, recursively if needed
975 == static void repeat(register struct parse *p, sopno start, int from, int to);
978 repeat(p
, start
, from
, to
)
979 register struct parse
*p
;
980 sopno start
; /* operand from here to end of strip */
981 int from
; /* repeated from this number */
982 int to
; /* to this number of times (maybe RE_INFINITY) */
984 register sopno finish
= HERE();
987 # define REP(f, t) ((f)*8 + (t))
988 # define MAP(n) (((n) <= 1) ? (n) : ((n) == RE_INFINITY) ? INF : N)
991 if (p
->error
!= 0) /* head off possible runaway recursion */
996 switch (REP(MAP(from
), MAP(to
))) {
997 case REP(0, 0): /* must be user doing this */
998 DROP(finish
-start
); /* drop the operand */
1000 case REP(0, 1): /* as x{1,1}? */
1001 case REP(0, N
): /* as x{1,n}? */
1002 case REP(0, INF
): /* as x{1,}? */
1003 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1004 INSERT(OCH_
, start
); /* offset is wrong... */
1005 repeat(p
, start
+1, 1, to
);
1006 ASTERN(OOR1
, start
);
1007 AHEAD(start
); /* ... fix it */
1010 ASTERN(O_CH
, THERETHERE());
1012 case REP(1, 1): /* trivial case */
1015 case REP(1, N
): /* as x?x{1,n-1} */
1016 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1017 INSERT(OCH_
, start
);
1018 ASTERN(OOR1
, start
);
1020 EMIT(OOR2
, 0); /* offset very wrong... */
1021 AHEAD(THERE()); /* ...so fix it */
1022 ASTERN(O_CH
, THERETHERE());
1023 copy
= dupl(p
, start
+1, finish
+1);
1024 assert(copy
== finish
+4);
1025 repeat(p
, copy
, 1, to
-1);
1027 case REP(1, INF
): /* as x+ */
1028 INSERT(OPLUS_
, start
);
1029 ASTERN(O_PLUS
, start
);
1031 case REP(N
, N
): /* as xx{m-1,n-1} */
1032 copy
= dupl(p
, start
, finish
);
1033 repeat(p
, copy
, from
-1, to
-1);
1035 case REP(N
, INF
): /* as xx{n-1,INF} */
1036 copy
= dupl(p
, start
, finish
);
1037 repeat(p
, copy
, from
-1, to
);
1039 default: /* "can't happen" */
1040 SETERROR(REG_ASSERT
); /* just in case */
1046 - seterr - set an error condition
1047 == static int seterr(register struct parse *p, int e);
1049 static int /* useless but makes type checking happy */
1051 register struct parse
*p
;
1054 if (p
->error
== 0) /* keep earliest error condition */
1056 p
->next
= nuls
; /* try to bring things to a halt */
1058 return(0); /* make the return value well-defined */
1062 - allocset - allocate a set of characters for []
1063 == static cset *allocset(register struct parse *p);
1067 register struct parse
*p
;
1069 register int no
= p
->g
->ncsets
++;
1071 register size_t nbytes
;
1073 register size_t css
= (size_t)p
->g
->csetsize
;
1076 if (no
>= p
->ncsalloc
) { /* need another column of space */
1077 p
->ncsalloc
+= CHAR_BIT
;
1079 assert(nc
% CHAR_BIT
== 0);
1080 nbytes
= nc
/ CHAR_BIT
* css
;
1081 if (p
->g
->sets
== NULL
)
1082 p
->g
->sets
= (cset
*)malloc(nc
* sizeof(cset
));
1084 p
->g
->sets
= (cset
*)realloc((char *)p
->g
->sets
,
1086 if (p
->g
->setbits
== NULL
)
1087 p
->g
->setbits
= (uch
*)malloc(nbytes
);
1089 p
->g
->setbits
= (uch
*)realloc((char *)p
->g
->setbits
,
1091 /* xxx this isn't right if setbits is now NULL */
1092 for (i
= 0; i
< no
; i
++)
1093 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1095 if (p
->g
->sets
!= NULL
&& p
->g
->setbits
!= NULL
)
1096 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
),
1100 SETERROR(REG_ESPACE
);
1101 /* caller's responsibility not to do set ops */
1105 assert(p
->g
->sets
!= NULL
); /* xxx */
1106 cs
= &p
->g
->sets
[no
];
1107 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1108 cs
->mask
= 1 << ((no
) % CHAR_BIT
);
1117 - freeset - free a now-unused set
1118 == static void freeset(register struct parse *p, register cset *cs);
1122 register struct parse
*p
;
1126 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1127 register size_t css
= (size_t)p
->g
->csetsize
;
1129 for (i
= 0; i
< css
; i
++)
1131 if (cs
== top
-1) /* recover only the easy case */
1136 - freezeset - final processing on a set of characters
1137 == static int freezeset(register struct parse *p, register cset *cs);
1139 * The main task here is merging identical sets. This is usually a waste
1140 * of time (although the hash code minimizes the overhead), but can win
1141 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1142 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1145 static int /* set number */
1147 register struct parse
*p
;
1150 register uch h
= cs
->hash
;
1152 register cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1154 register size_t css
= (size_t)p
->g
->csetsize
;
1156 /* look for an earlier one which is the same */
1157 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1158 if (cs2
->hash
== h
&& cs2
!= cs
) {
1160 for (i
= 0; i
< css
; i
++)
1161 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1167 if (cs2
< top
) { /* found one */
1172 return((int)(cs
- p
->g
->sets
));
1176 - firstch - return first character in a set (which must have at least one)
1177 == static int firstch(register struct parse *p, register cset *cs);
1179 static int /* character; there is no "none" value */
1181 register struct parse
*p
;
1185 register size_t css
= (size_t)p
->g
->csetsize
;
1187 for (i
= 0; i
< css
; i
++)
1191 return(0); /* arbitrary */
1195 - nch - number of characters in a set
1196 == static int nch(register struct parse *p, register cset *cs);
1200 register struct parse
*p
;
1204 register size_t css
= (size_t)p
->g
->csetsize
;
1207 for (i
= 0; i
< css
; i
++)
1213 #ifdef USE_ORIG_REGEX_CODE
1215 - mcadd - add a collating element to a cset
1216 == static void mcadd(register struct parse *p, register cset *cs, \
1217 == register char *cp);
1221 register struct parse
*p
;
1225 register size_t oldend
= cs
->smultis
;
1227 cs
->smultis
+= strlen(cp
) + 1;
1228 if (cs
->multis
== NULL
)
1229 cs
->multis
= malloc(cs
->smultis
);
1231 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1232 if (cs
->multis
== NULL
) {
1233 SETERROR(REG_ESPACE
);
1237 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1238 cs
->multis
[cs
->smultis
- 1] = '\0';
1244 - mcsub - subtract a collating element from a cset
1245 == static void mcsub(register cset *cs, register char *cp);
1252 register char *fp
= mcfind(cs
, cp
);
1253 register size_t len
= strlen(fp
);
1256 (void) memmove(fp
, fp
+ len
+ 1,
1257 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1260 if (cs
->smultis
== 0) {
1266 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1267 assert(cs
->multis
!= NULL
);
1271 - mcin - is a collating element in a cset?
1272 == static int mcin(register cset *cs, register char *cp);
1279 return(mcfind(cs
, cp
) != NULL
);
1283 - mcfind - find a collating element in a cset
1284 == static char *mcfind(register cset *cs, register char *cp);
1293 if (cs
->multis
== NULL
)
1295 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1296 if (strcmp(cp
, p
) == 0)
1303 - mcinvert - invert the list of collating elements in a cset
1304 == static void mcinvert(register struct parse *p, register cset *cs);
1306 * This would have to know the set of possibilities. Implementation
1311 register struct parse
*p
__attribute__((unused
));
1312 register cset
*cs
__attribute__((unused
));
1314 assert(cs
->multis
== NULL
); /* xxx */
1318 - mccase - add case counterparts of the list of collating elements in a cset
1319 == static void mccase(register struct parse *p, register cset *cs);
1321 * This would have to know the set of possibilities. Implementation
1326 register struct parse
*p
__attribute__((unused
));
1327 register cset
*cs
__attribute__((unused
));
1329 assert(cs
->multis
== NULL
); /* xxx */
1333 - isinsets - is this character in any sets?
1334 == static int isinsets(register struct re_guts *g, int c);
1336 static int /* predicate */
1338 register struct re_guts
*g
;
1343 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1344 register unsigned uc
= (unsigned char)c
;
1346 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1353 - samesets - are these two characters in exactly the same sets?
1354 == static int samesets(register struct re_guts *g, int c1, int c2);
1356 static int /* predicate */
1358 register struct re_guts
*g
;
1364 register int ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1365 register unsigned uc1
= (unsigned char)c1
;
1366 register unsigned uc2
= (unsigned char)c2
;
1368 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1369 if (col
[uc1
] != col
[uc2
])
1375 - categorize - sort out character categories
1376 == static void categorize(struct parse *p, register struct re_guts *g);
1381 register struct re_guts
*g
;
1383 register cat_t
*cats
= g
->categories
;
1388 /* avoid making error situations worse */
1392 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1393 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1394 cat
= g
->ncategories
++;
1396 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1397 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1403 - dupl - emit a duplicate of a bunch of sops
1404 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1406 static sopno
/* start of duplicate */
1407 dupl(p
, start
, finish
)
1408 register struct parse
*p
;
1409 sopno start
; /* from here */
1410 sopno finish
; /* to this less one */
1412 register sopno ret
= HERE();
1413 register sopno len
= finish
- start
;
1415 assert(finish
>= start
);
1418 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1419 assert(p
->ssize
>= p
->slen
+ len
);
1420 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1421 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1427 - doemit - emit a strip operator
1428 == static void doemit(register struct parse *p, sop op, size_t opnd);
1430 * It might seem better to implement this as a macro with a function as
1431 * hard-case backup, but it's just too big and messy unless there are
1432 * some changes to the data structures. Maybe later.
1436 register struct parse
*p
;
1440 /* avoid making error situations worse */
1444 /* deal with oversize operands ("can't happen", more or less) */
1445 assert(opnd
< 1<<OPSHIFT
);
1447 /* deal with undersized strip */
1448 if (p
->slen
>= p
->ssize
)
1449 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1450 assert(p
->slen
< p
->ssize
);
1452 /* finally, it's all reduced to the easy case */
1453 p
->strip
[p
->slen
++] = SOP(op
, opnd
);
1457 - doinsert - insert a sop into the strip
1458 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1461 doinsert(p
, op
, opnd
, pos
)
1462 register struct parse
*p
;
1471 /* avoid making error situations worse */
1476 EMIT(op
, opnd
); /* do checks, ensure space */
1477 assert(HERE() == sn
+1);
1480 /* adjust paren pointers */
1482 for (i
= 1; i
< NPAREN
; i
++) {
1483 if (p
->pbegin
[i
] >= pos
) {
1486 if (p
->pend
[i
] >= pos
) {
1491 int length
=(HERE()-pos
-1)*sizeof(sop
);
1492 bmove_upp((uchar
*) &p
->strip
[pos
+1]+length
,
1493 (uchar
*) &p
->strip
[pos
]+length
,
1497 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1498 (HERE()-pos
-1)*sizeof(sop
));
1504 - dofwd - complete a forward reference
1505 == static void dofwd(register struct parse *p, sopno pos, sop value);
1508 dofwd(p
, pos
, value
)
1509 register struct parse
*p
;
1513 /* avoid making error situations worse */
1517 assert(value
< 1<<OPSHIFT
);
1518 p
->strip
[pos
] = OP(p
->strip
[pos
]) | value
;
1522 - enlarge - enlarge the strip
1523 == static void enlarge(register struct parse *p, sopno size);
1527 register struct parse
*p
;
1528 register sopno size
;
1532 if (p
->ssize
>= size
)
1535 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1537 SETERROR(REG_ESPACE
);
1545 - stripsnug - compact the strip
1546 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1550 register struct parse
*p
;
1551 register struct re_guts
*g
;
1553 g
->nstates
= p
->slen
;
1554 g
->strip
= (sop
*)realloc((char *)p
->strip
, p
->slen
* sizeof(sop
));
1555 if (g
->strip
== NULL
) {
1556 SETERROR(REG_ESPACE
);
1557 g
->strip
= p
->strip
;
1562 - findmust - fill in must and mlen with longest mandatory literal string
1563 == static void findmust(register struct parse *p, register struct re_guts *g);
1565 * This algorithm could do fancy things like analyzing the operands of |
1566 * for common subsequences. Someday. This code is simple and finds most
1567 * of the interesting cases.
1569 * Note that must and mlen got initialized during setup.
1574 register struct re_guts
*g
;
1577 sop
*UNINIT_VAR(start
);
1578 register sop
*UNINIT_VAR(newstart
);
1579 register sopno newlen
;
1584 /* avoid making error situations worse */
1588 /* find the longest OCHAR sequence in strip */
1590 scan
= g
->strip
+ 1;
1594 case OCHAR
: /* sequence member */
1595 if (newlen
== 0) /* new sequence */
1596 newstart
= scan
- 1;
1599 case OPLUS_
: /* things that don't break one */
1603 case OQUEST_
: /* things that must be skipped */
1609 /* assert() interferes w debug printouts */
1610 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1615 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1617 default: /* things that break a sequence */
1618 if (newlen
> g
->mlen
) { /* ends one */
1625 } while (OP(s
) != OEND
);
1627 if (g
->mlen
== 0) /* there isn't one */
1630 /* turn it into a character string */
1631 g
->must
= malloc((size_t)g
->mlen
+ 1);
1632 if (g
->must
== NULL
) { /* argh; just forget it */
1638 for (i
= g
->mlen
; i
> 0; i
--) {
1639 while (OP(s
= *scan
++) != OCHAR
)
1641 assert(cp
< g
->must
+ g
->mlen
);
1642 *cp
++ = (char)OPND(s
);
1644 assert(cp
== g
->must
+ g
->mlen
);
1645 *cp
++ = '\0'; /* just on general principles */
1649 - pluscount - count + nesting
1650 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1652 static sopno
/* nesting depth */
1655 register struct re_guts
*g
;
1659 register sopno plusnest
= 0;
1660 register sopno maxnest
= 0;
1663 return(0); /* there may not be an OEND */
1665 scan
= g
->strip
+ 1;
1673 if (plusnest
> maxnest
)
1678 } while (OP(s
) != OEND
);