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 */
36
static char nuls[10]; /* place to point scanner in event of error */
38
struct cclass cclasses[CCLASS_LAST+1]= {
39
{ "alnum", "","", _MY_U | _MY_L | _MY_NMR},
40
{ "alpha", "","", _MY_U | _MY_L },
41
{ "blank", "","", _MY_B },
42
{ "cntrl", "","", _MY_CTR },
43
{ "digit", "","", _MY_NMR },
44
{ "graph", "","", _MY_PNT | _MY_U | _MY_L | _MY_NMR},
45
{ "lower", "","", _MY_L },
46
{ "print", "","", _MY_PNT | _MY_U | _MY_L | _MY_NMR | _MY_B },
47
{ "punct", "","", _MY_PNT },
48
{ "space", "","", _MY_SPC },
49
{ "upper", "","", _MY_U },
50
{ "xdigit", "","", _MY_X },
55
* macros for use with parse structure
56
* BEWARE: these know that the parse structure is named `p' !!!
58
#define PEEK() (*p->next)
59
#define PEEK2() (*(p->next+1))
60
#define MORE() (p->next < p->end)
61
#define MORE2() (p->next+1 < p->end)
62
#define SEE(c) (MORE() && PEEK() == (c))
63
#define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
64
#define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
65
#define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
66
#define NEXT() (p->next++)
67
#define NEXT2() (p->next += 2)
68
#define NEXTn(n) (p->next += (n))
69
#define GETNEXT() (*p->next++)
70
#define SETERROR(e) seterr(p, (e))
71
#define REQUIRE(co, e) ((co) || SETERROR(e))
72
#define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
73
#define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
74
#define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
75
#define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
76
#define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
77
#define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
78
#define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
79
#define HERE() (p->slen)
80
#define THERE() (p->slen - 1)
81
#define THERETHERE() (p->slen - 2)
82
#define DROP(n) (p->slen -= (n))
85
static int never = 0; /* for use in asserts; shuts lint up */
87
#define never 0 /* some <assert.h>s have bugs too */
91
- regcomp - interface for parser and compilation
92
= extern int regcomp(regex_t *, const char *, int);
93
= #define REG_BASIC 0000
94
= #define REG_EXTENDED 0001
95
= #define REG_ICASE 0002
96
= #define REG_NOSUB 0004
97
= #define REG_NEWLINE 0010
98
= #define REG_NOSPEC 0020
99
= #define REG_PEND 0040
100
= #define REG_DUMP 0200
102
int /* 0 success, otherwise REG_something */
103
my_regcomp(preg, pattern, cflags, charset)
107
CHARSET_INFO *charset;
110
register struct re_guts *g;
111
register struct parse *p = &pa;
115
# define GOODFLAGS(f) (f)
117
# define GOODFLAGS(f) ((f)&~REG_DUMP)
120
my_regex_init(charset); /* Init cclass if neaded */
121
preg->charset=charset;
122
cflags = GOODFLAGS(cflags);
123
if ((cflags®_EXTENDED) && (cflags®_NOSPEC))
126
if (cflags®_PEND) {
127
if (preg->re_endp < pattern)
129
len = preg->re_endp - pattern;
131
len = strlen((char *)pattern);
133
/* do the mallocs early so failure handling is easy */
134
g = (struct re_guts *)malloc(sizeof(struct re_guts) +
135
(NC-1)*sizeof(cat_t));
138
p->ssize = (long) (len/(size_t)2*(size_t)3 + (size_t)1); /* ugh */
139
p->strip = (sop *)malloc(p->ssize * sizeof(sop));
141
if (p->strip == NULL) {
148
p->next = (char *)pattern; /* convenience; we do not modify it */
149
p->end = p->next + len;
152
p->charset = preg->charset;
153
for (i = 0; i < NPAREN; i++) {
168
g->ncategories = 1; /* category 0 is "everything else" */
169
g->categories = &g->catspace[-(CHAR_MIN)];
170
(void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
175
g->firststate = THERE();
176
if (cflags®_EXTENDED)
178
else if (cflags®_NOSPEC)
183
g->laststate = THERE();
185
/* tidy up loose ends and fill things in */
189
g->nplus = pluscount(p, g);
191
preg->re_nsub = g->nsub;
193
preg->re_magic = MAGIC1;
195
/* not debugging, so can't rely on the assert() in regexec() */
197
SETERROR(REG_ASSERT);
200
/* win or lose, we're done */
201
if (p->error != 0) /* lose */
207
- p_ere - ERE parser top level, concatenation and alternation
208
== static void p_ere(register struct parse *p, int stop);
212
register struct parse *p;
213
int stop; /* character this ERE should end at */
216
register sopno UNINIT_VAR(prevback);
217
register sopno UNINIT_VAR(prevfwd);
219
register int first = 1; /* is this the first alternative? */
222
/* do a bunch of concatenated expressions */
224
while (MORE() && (c = PEEK()) != '|' && c != stop)
226
if(REQUIRE(HERE() != conc, REG_EMPTY)) {}/* require nonempty */
229
break; /* NOTE BREAK OUT */
232
INSERT(OCH_, conc); /* offset is wrong */
237
ASTERN(OOR1, prevback);
239
AHEAD(prevfwd); /* fix previous offset */
241
EMIT(OOR2, 0); /* offset is very wrong */
244
if (!first) { /* tail-end fixups */
246
ASTERN(O_CH, prevback);
249
assert(!MORE() || SEE(stop));
253
- p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
254
== static void p_ere_exp(register struct parse *p);
258
register struct parse *p;
264
register sopno subno;
267
assert(MORE()); /* caller should have ensured this */
273
if(REQUIRE(MORE(), REG_EPAREN)) {}
275
subno = (sopno) p->g->nsub;
277
p->pbegin[subno] = HERE();
278
EMIT(OLPAREN, subno);
281
if (subno < NPAREN) {
282
p->pend[subno] = HERE();
283
assert(p->pend[subno] != 0);
285
EMIT(ORPAREN, subno);
286
if(MUSTEAT(')', REG_EPAREN)) {}
288
#ifndef POSIX_MISTAKE
289
case ')': /* happens only if no current unmatched ( */
291
* You may ask, why the ifndef? Because I didn't notice
292
* this until slightly too late for 1003.2, and none of the
293
* other 1003.2 regular-expression reviewers noticed it at
294
* all. So an unmatched ) is legal POSIX, at least until
295
* we can get it fixed.
297
SETERROR(REG_EPAREN);
302
p->g->iflags |= USEBOL;
308
p->g->iflags |= USEEOL;
317
SETERROR(REG_BADRPT);
320
if (p->g->cflags®_NEWLINE)
329
if(REQUIRE(MORE(), REG_EESCAPE)) {}
333
case '{': /* okay as ordinary except if digit follows */
334
if(REQUIRE(!MORE() || !my_isdigit(p->charset,PEEK()), REG_BADRPT)) {}
344
/* we call { a repetition if followed by a digit */
345
if (!( c == '*' || c == '+' || c == '?' ||
346
(c == '{' && MORE2() &&
347
my_isdigit(p->charset,PEEK2())) ))
348
return; /* no repetition, we're done */
351
if(REQUIRE(!wascaret, REG_BADRPT)) {}
353
case '*': /* implemented as +? */
354
/* this case does not require the (y|) trick, noKLUDGE */
357
INSERT(OQUEST_, pos);
358
ASTERN(O_QUEST, pos);
365
/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
366
INSERT(OCH_, pos); /* offset slightly wrong */
367
ASTERN(OOR1, pos); /* this one's right */
368
AHEAD(pos); /* fix the OCH_ */
369
EMIT(OOR2, 0); /* offset very wrong... */
370
AHEAD(THERE()); /* ...so fix it */
371
ASTERN(O_CH, THERETHERE());
376
if (my_isdigit(p->charset,PEEK())) {
378
if(REQUIRE(count <= count2, REG_BADBR)) {}
379
} else /* single number with comma */
380
count2 = RE_INFINITY;
381
} else /* just a single number */
383
repeat(p, pos, count, count2);
384
if (!EAT('}')) { /* error heuristics */
385
while (MORE() && PEEK() != '}')
387
if(REQUIRE(MORE(), REG_EBRACE)) {}
396
if (!( c == '*' || c == '+' || c == '?' ||
397
(c == '{' && MORE2() &&
398
my_isdigit(p->charset,PEEK2())) ) )
400
SETERROR(REG_BADRPT);
404
- p_str - string (no metacharacters) "parser"
405
== static void p_str(register struct parse *p);
409
register struct parse *p;
411
if(REQUIRE(MORE(), REG_EMPTY)) {}
413
ordinary(p, GETNEXT());
417
- p_bre - BRE parser top level, anchoring and concatenation
418
== static void p_bre(register struct parse *p, register int end1, \
419
== register int end2);
420
* Giving end1 as OUT essentially eliminates the end1/end2 check.
422
* This implementation is a bit of a kludge, in that a trailing $ is first
423
* taken as an ordinary character and then revised to be an anchor. The
424
* only undesirable side effect is that '$' gets included as a character
425
* category in such cases. This is fairly harmless; not worth fixing.
426
* The amount of lookahead needed to avoid this kludge is excessive.
430
register struct parse *p;
431
register int end1; /* first terminating character */
432
register int end2; /* second terminating character */
434
register sopno start = HERE();
435
register int first = 1; /* first subexpression? */
436
register int wasdollar = 0;
440
p->g->iflags |= USEBOL;
443
while (MORE() && !SEETWO(end1, end2)) {
444
wasdollar = p_simp_re(p, first);
447
if (wasdollar) { /* oops, that was a trailing anchor */
450
p->g->iflags |= USEEOL;
454
if(REQUIRE(HERE() != start, REG_EMPTY)) {} /* require nonempty */
458
- p_simp_re - parse a simple RE, an atom possibly followed by a repetition
459
== static int p_simp_re(register struct parse *p, int starordinary);
461
static int /* was the simple RE an unbackslashed $? */
462
p_simp_re(p, starordinary)
463
register struct parse *p;
464
int starordinary; /* is a leading * an ordinary character? */
471
register sopno subno;
472
# define BACKSL (1<<CHAR_BIT)
474
pos = HERE(); /* repetion op, if any, covers from here */
476
assert(MORE()); /* caller should have ensured this */
479
if(REQUIRE(MORE(), REG_EESCAPE)) {}
480
c = BACKSL | (unsigned char)GETNEXT();
484
if (p->g->cflags®_NEWLINE)
493
SETERROR(REG_BADRPT);
497
subno = (sopno) p->g->nsub;
499
p->pbegin[subno] = HERE();
500
EMIT(OLPAREN, subno);
501
/* the MORE here is an error heuristic */
502
if (MORE() && !SEETWO('\\', ')'))
504
if (subno < NPAREN) {
505
p->pend[subno] = HERE();
506
assert(p->pend[subno] != 0);
508
EMIT(ORPAREN, subno);
509
if(REQUIRE(EATTWO('\\', ')'), REG_EPAREN)) {}
511
case BACKSL|')': /* should not get here -- must be user */
513
SETERROR(REG_EPAREN);
524
i = (c&~BACKSL) - '0';
526
if (p->pend[i] != 0) {
527
assert((uint) i <= p->g->nsub);
529
assert(p->pbegin[i] != 0);
530
assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
531
assert(OP(p->strip[p->pend[i]]) == ORPAREN);
532
(void) dupl(p, p->pbegin[i]+1, p->pend[i]);
535
SETERROR(REG_ESUBREG);
539
if(REQUIRE(starordinary, REG_BADRPT)) {}
542
ordinary(p, c &~ BACKSL);
546
if (EAT('*')) { /* implemented as +? */
547
/* this case does not require the (y|) trick, noKLUDGE */
550
INSERT(OQUEST_, pos);
551
ASTERN(O_QUEST, pos);
552
} else if (EATTWO('\\', '{')) {
555
if (MORE() && my_isdigit(p->charset,PEEK())) {
557
if(REQUIRE(count <= count2, REG_BADBR)) {}
558
} else /* single number with comma */
559
count2 = RE_INFINITY;
560
} else /* just a single number */
562
repeat(p, pos, count, count2);
563
if (!EATTWO('\\', '}')) { /* error heuristics */
564
while (MORE() && !SEETWO('\\', '}'))
566
if(REQUIRE(MORE(), REG_EBRACE)) {}
569
} else if (c == (unsigned char)'$') /* $ (but not \$) ends it */
576
- p_count - parse a repetition count
577
== static int p_count(register struct parse *p);
579
static int /* the value */
581
register struct parse *p;
583
register int count = 0;
584
register int ndigits = 0;
586
while (MORE() && my_isdigit(p->charset,PEEK()) && count <= DUPMAX) {
587
count = count*10 + (GETNEXT() - '0');
591
if(REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR)) {}
596
- p_bracket - parse a bracketed character list
597
== static void p_bracket(register struct parse *p);
599
* Note a significant property of this code: if the allocset() did SETERROR,
600
* no set operations are done.
604
register struct parse *p;
606
register cset *cs = allocset(p);
607
register int invert = 0;
609
/* Dept of Truly Sickening Special-Case Kludges */
610
if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
615
if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
622
invert++; /* make note to invert set at end */
627
while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
631
if(MUSTEAT(']', REG_EBRACK)) {}
633
if (p->error != 0) /* don't mess things up further */
636
if (p->g->cflags®_ICASE) {
640
for (i = p->g->csetsize - 1; i >= 0; i--)
641
if (CHIN(cs, i) && my_isalpha(p->charset,i)) {
642
ci = othercase(p->charset,i);
646
if (cs->multis != NULL)
652
for (i = p->g->csetsize - 1; i >= 0; i--)
657
if (p->g->cflags®_NEWLINE)
659
if (cs->multis != NULL)
663
assert(cs->multis == NULL); /* xxx */
665
if (nch(p, cs) == 1) { /* optimize singleton sets */
666
ordinary(p, firstch(p, cs));
669
EMIT(OANYOF, freezeset(p, cs));
673
- p_b_term - parse one term of a bracketed character list
674
== static void p_b_term(register struct parse *p, register cset *cs);
678
register struct parse *p;
682
register char start, finish;
685
/* classify what we've got */
686
switch ((MORE()) ? PEEK() : '\0') {
688
c = (MORE2()) ? PEEK2() : '\0';
691
SETERROR(REG_ERANGE);
692
return; /* NOTE RETURN */
700
case ':': /* character class */
702
if(REQUIRE(MORE(), REG_EBRACK)) {}
704
if(REQUIRE(c != '-' && c != ']', REG_ECTYPE)) {}
706
if(REQUIRE(MORE(), REG_EBRACK)) {}
707
if(REQUIRE(EATTWO(':', ']'), REG_ECTYPE)) {}
709
case '=': /* equivalence class */
711
if(REQUIRE(MORE(), REG_EBRACK)) {}
713
if(REQUIRE(c != '-' && c != ']', REG_ECOLLATE)) {}
715
if(REQUIRE(MORE(), REG_EBRACK)) {}
716
if(REQUIRE(EATTWO('=', ']'), REG_ECOLLATE)) {}
718
default: /* symbol, ordinary character, or range */
719
/* xxx revision needed for multichar stuff */
720
start = p_b_symbol(p);
721
if (SEE('-') && MORE2() && PEEK2() != ']') {
727
finish = p_b_symbol(p);
730
/* xxx what about signed chars here... */
731
if(REQUIRE(start <= finish, REG_ERANGE)) {}
732
for (i = start; i <= finish; i++)
739
- p_b_cclass - parse a character-class name and deal with it
740
== static void p_b_cclass(register struct parse *p, register cset *cs);
744
register struct parse *p;
747
register char *sp = p->next;
748
register struct cclass *cp;
751
while (MORE() && my_isalpha(p->charset,PEEK()))
754
for (cp = cclasses; cp->name != NULL; cp++)
755
if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
757
if (cp->name == NULL) {
758
/* oops, didn't find it */
759
SETERROR(REG_ECTYPE);
763
#ifndef USE_ORIG_REGEX_CODE
766
for (i=1 ; i<256 ; i++)
767
if (p->charset->ctype[i+1] & cp->mask)
772
register char *u = (char*) cp->chars;
775
while ((c = *u++) != '\0')
778
for (u = (char*) cp->multis; *u != '\0'; u += strlen(u) + 1)
786
- p_b_eclass - parse an equivalence-class name and deal with it
787
== static void p_b_eclass(register struct parse *p, register cset *cs);
789
* This implementation is incomplete. xxx
793
register struct parse *p;
798
c = p_b_coll_elem(p, '=');
803
- p_b_symbol - parse a character or [..]ed multicharacter collating symbol
804
== static char p_b_symbol(register struct parse *p);
806
static char /* value of symbol */
808
register struct parse *p;
812
if(REQUIRE(MORE(), REG_EBRACK)) {}
813
if (!EATTWO('[', '.'))
816
/* collating symbol */
817
value = p_b_coll_elem(p, '.');
818
if(REQUIRE(EATTWO('.', ']'), REG_ECOLLATE)) {}
823
- p_b_coll_elem - parse a collating-element name and look it up
824
== static char p_b_coll_elem(register struct parse *p, int endc);
826
static char /* value of collating element */
827
p_b_coll_elem(p, endc)
828
register struct parse *p;
829
int endc; /* name ended by endc,']' */
831
register char *sp = p->next;
832
register struct cname *cp;
834
register __int64 len;
838
while (MORE() && !SEETWO(endc, ']'))
841
SETERROR(REG_EBRACK);
845
for (cp = cnames; cp->name != NULL; cp++)
846
if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
847
return(cp->code); /* known name */
849
return(*sp); /* single character */
850
SETERROR(REG_ECOLLATE); /* neither */
855
- othercase - return the case counterpart of an alphabetic
856
== static char othercase(int ch);
858
static char /* if no counterpart, return ch */
859
othercase(charset,ch)
860
CHARSET_INFO *charset;
864
In MySQL some multi-byte character sets
865
have 'ctype' array but don't have 'to_lower'
866
and 'to_upper' arrays. In this case we handle
867
only basic latin letters a..z and A..Z.
869
If 'to_lower' and 'to_upper' arrays are empty in a character set,
870
then my_isalpha(cs, ch) should never return TRUE for characters
871
other than basic latin letters. Otherwise it should be
872
considered as a mistake in character set definition.
874
assert(my_isalpha(charset,ch));
875
if (my_isupper(charset,ch))
877
return(charset->to_lower ? my_tolower(charset,ch) :
880
else if (my_islower(charset,ch))
882
return(charset->to_upper ? my_toupper(charset,ch) :
885
else /* peculiar, but could happen */
890
- bothcases - emit a dualcase version of a two-case character
891
== static void bothcases(register struct parse *p, int ch);
893
* Boy, is this implementation ever a kludge...
897
register struct parse *p;
900
register char *oldnext = p->next;
901
register char *oldend = p->end;
904
assert(othercase(p->charset, ch) != ch); /* p_bracket() would recurse */
911
assert(p->next == bracket+2);
917
- ordinary - emit an ordinary character
918
== static void ordinary(register struct parse *p, register int ch);
922
register struct parse *p;
925
register cat_t *cap = p->g->categories;
927
if ((p->g->cflags®_ICASE) && my_isalpha(p->charset,ch) &&
928
othercase(p->charset,ch) != ch)
931
EMIT(OCHAR, (unsigned char)ch);
933
cap[ch] = p->g->ncategories++;
938
- nonnewline - emit REG_NEWLINE version of OANY
939
== static void nonnewline(register struct parse *p);
941
* Boy, is this implementation ever a kludge...
945
register struct parse *p;
947
register char *oldnext = p->next;
948
register char *oldend = p->end;
958
assert(p->next == bracket+3);
964
- repeat - generate code for a bounded repetition, recursively if needed
965
== static void repeat(register struct parse *p, sopno start, int from, int to);
968
repeat(p, start, from, to)
969
register struct parse *p;
970
sopno start; /* operand from here to end of strip */
971
int from; /* repeated from this number */
972
int to; /* to this number of times (maybe RE_INFINITY) */
974
register sopno finish = HERE();
977
# define REP(f, t) ((f)*8 + (t))
978
# define MAP(n) (((n) <= 1) ? (n) : ((n) == RE_INFINITY) ? INF : N)
981
if (p->error != 0) /* head off possible runaway recursion */
986
switch (REP(MAP(from), MAP(to))) {
987
case REP(0, 0): /* must be user doing this */
988
DROP(finish-start); /* drop the operand */
990
case REP(0, 1): /* as x{1,1}? */
991
case REP(0, N): /* as x{1,n}? */
992
case REP(0, INF): /* as x{1,}? */
993
/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
994
INSERT(OCH_, start); /* offset is wrong... */
995
repeat(p, start+1, 1, to);
997
AHEAD(start); /* ... fix it */
1000
ASTERN(O_CH, THERETHERE());
1002
case REP(1, 1): /* trivial case */
1005
case REP(1, N): /* as x?x{1,n-1} */
1006
/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1007
INSERT(OCH_, start);
1008
ASTERN(OOR1, start);
1010
EMIT(OOR2, 0); /* offset very wrong... */
1011
AHEAD(THERE()); /* ...so fix it */
1012
ASTERN(O_CH, THERETHERE());
1013
copy = dupl(p, start+1, finish+1);
1014
assert(copy == finish+4);
1015
repeat(p, copy, 1, to-1);
1017
case REP(1, INF): /* as x+ */
1018
INSERT(OPLUS_, start);
1019
ASTERN(O_PLUS, start);
1021
case REP(N, N): /* as xx{m-1,n-1} */
1022
copy = dupl(p, start, finish);
1023
repeat(p, copy, from-1, to-1);
1025
case REP(N, INF): /* as xx{n-1,INF} */
1026
copy = dupl(p, start, finish);
1027
repeat(p, copy, from-1, to);
1029
default: /* "can't happen" */
1030
SETERROR(REG_ASSERT); /* just in case */
1036
- seterr - set an error condition
1037
== static int seterr(register struct parse *p, int e);
1039
static int /* useless but makes type checking happy */
1041
register struct parse *p;
1044
if (p->error == 0) /* keep earliest error condition */
1046
p->next = nuls; /* try to bring things to a halt */
1048
return(0); /* make the return value well-defined */
1052
- allocset - allocate a set of characters for []
1053
== static cset *allocset(register struct parse *p);
1057
register struct parse *p;
1059
register int no = p->g->ncsets++;
1061
register size_t nbytes;
1063
register size_t css = (size_t)p->g->csetsize;
1066
if (no >= p->ncsalloc) { /* need another column of space */
1067
p->ncsalloc += CHAR_BIT;
1069
assert(nc % CHAR_BIT == 0);
1070
nbytes = nc / CHAR_BIT * css;
1071
if (p->g->sets == NULL)
1072
p->g->sets = (cset *)malloc(nc * sizeof(cset));
1074
p->g->sets = (cset *)realloc((char *)p->g->sets,
1076
if (p->g->setbits == NULL)
1077
p->g->setbits = (uch *)malloc(nbytes);
1079
p->g->setbits = (uch *)realloc((char *)p->g->setbits,
1081
/* xxx this isn't right if setbits is now NULL */
1082
for (i = 0; i < no; i++)
1083
p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1085
if (p->g->sets != NULL && p->g->setbits != NULL)
1086
(void) memset((char *)p->g->setbits + (nbytes - css),
1090
SETERROR(REG_ESPACE);
1091
/* caller's responsibility not to do set ops */
1095
assert(p->g->sets != NULL); /* xxx */
1096
cs = &p->g->sets[no];
1097
cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1098
cs->mask = 1 << ((no) % CHAR_BIT);
1107
- freeset - free a now-unused set
1108
== static void freeset(register struct parse *p, register cset *cs);
1112
register struct parse *p;
1116
register cset *top = &p->g->sets[p->g->ncsets];
1117
register size_t css = (size_t)p->g->csetsize;
1119
for (i = 0; i < css; i++)
1121
if (cs == top-1) /* recover only the easy case */
1126
- freezeset - final processing on a set of characters
1127
== static int freezeset(register struct parse *p, register cset *cs);
1129
* The main task here is merging identical sets. This is usually a waste
1130
* of time (although the hash code minimizes the overhead), but can win
1131
* big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1132
* is done using addition rather than xor -- all ASCII [aA] sets xor to
1135
static int /* set number */
1137
register struct parse *p;
1140
register uch h = cs->hash;
1142
register cset *top = &p->g->sets[p->g->ncsets];
1144
register size_t css = (size_t)p->g->csetsize;
1146
/* look for an earlier one which is the same */
1147
for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1148
if (cs2->hash == h && cs2 != cs) {
1150
for (i = 0; i < css; i++)
1151
if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1157
if (cs2 < top) { /* found one */
1162
return((int)(cs - p->g->sets));
1166
- firstch - return first character in a set (which must have at least one)
1167
== static int firstch(register struct parse *p, register cset *cs);
1169
static int /* character; there is no "none" value */
1171
register struct parse *p;
1175
register size_t css = (size_t)p->g->csetsize;
1177
for (i = 0; i < css; i++)
1181
return(0); /* arbitrary */
1185
- nch - number of characters in a set
1186
== static int nch(register struct parse *p, register cset *cs);
1190
register struct parse *p;
1194
register size_t css = (size_t)p->g->csetsize;
1197
for (i = 0; i < css; i++)
1203
#ifdef USE_ORIG_REGEX_CODE
1205
- mcadd - add a collating element to a cset
1206
== static void mcadd(register struct parse *p, register cset *cs, \
1207
== register char *cp);
1211
register struct parse *p;
1215
register size_t oldend = cs->smultis;
1217
cs->smultis += strlen(cp) + 1;
1218
if (cs->multis == NULL)
1219
cs->multis = malloc(cs->smultis);
1221
cs->multis = realloc(cs->multis, cs->smultis);
1222
if (cs->multis == NULL) {
1223
SETERROR(REG_ESPACE);
1227
(void) strcpy(cs->multis + oldend - 1, cp);
1228
cs->multis[cs->smultis - 1] = '\0';
1234
- mcsub - subtract a collating element from a cset
1235
== static void mcsub(register cset *cs, register char *cp);
1242
register char *fp = mcfind(cs, cp);
1243
register size_t len = strlen(fp);
1246
(void) memmove(fp, fp + len + 1,
1247
cs->smultis - (fp + len + 1 - cs->multis));
1250
if (cs->smultis == 0) {
1256
cs->multis = realloc(cs->multis, cs->smultis);
1257
assert(cs->multis != NULL);
1261
- mcin - is a collating element in a cset?
1262
== static int mcin(register cset *cs, register char *cp);
1269
return(mcfind(cs, cp) != NULL);
1273
- mcfind - find a collating element in a cset
1274
== static char *mcfind(register cset *cs, register char *cp);
1283
if (cs->multis == NULL)
1285
for (p = cs->multis; *p != '\0'; p += strlen(p) + 1)
1286
if (strcmp(cp, p) == 0)
1293
- mcinvert - invert the list of collating elements in a cset
1294
== static void mcinvert(register struct parse *p, register cset *cs);
1296
* This would have to know the set of possibilities. Implementation
1301
register struct parse *p __attribute__((unused));
1302
register cset *cs __attribute__((unused));
1304
assert(cs->multis == NULL); /* xxx */
1308
- mccase - add case counterparts of the list of collating elements in a cset
1309
== static void mccase(register struct parse *p, register cset *cs);
1311
* This would have to know the set of possibilities. Implementation
1316
register struct parse *p __attribute__((unused));
1317
register cset *cs __attribute__((unused));
1319
assert(cs->multis == NULL); /* xxx */
1323
- isinsets - is this character in any sets?
1324
== static int isinsets(register struct re_guts *g, int c);
1326
static int /* predicate */
1328
register struct re_guts *g;
1333
register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1334
register unsigned uc = (unsigned char)c;
1336
for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1343
- samesets - are these two characters in exactly the same sets?
1344
== static int samesets(register struct re_guts *g, int c1, int c2);
1346
static int /* predicate */
1348
register struct re_guts *g;
1354
register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1355
register unsigned uc1 = (unsigned char)c1;
1356
register unsigned uc2 = (unsigned char)c2;
1358
for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1359
if (col[uc1] != col[uc2])
1365
- categorize - sort out character categories
1366
== static void categorize(struct parse *p, register struct re_guts *g);
1371
register struct re_guts *g;
1373
register cat_t *cats = g->categories;
1378
/* avoid making error situations worse */
1382
for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1383
if (cats[c] == 0 && isinsets(g, c)) {
1384
cat = g->ncategories++;
1386
for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1387
if (cats[c2] == 0 && samesets(g, c, c2))
1393
- dupl - emit a duplicate of a bunch of sops
1394
== static sopno dupl(register struct parse *p, sopno start, sopno finish);
1396
static sopno /* start of duplicate */
1397
dupl(p, start, finish)
1398
register struct parse *p;
1399
sopno start; /* from here */
1400
sopno finish; /* to this less one */
1402
register sopno ret = HERE();
1403
register sopno len = finish - start;
1405
assert(finish >= start);
1408
enlarge(p, p->ssize + len); /* this many unexpected additions */
1409
assert(p->ssize >= p->slen + len);
1410
(void) memcpy((char *)(p->strip + p->slen),
1411
(char *)(p->strip + start), (size_t)len*sizeof(sop));
1417
- doemit - emit a strip operator
1418
== static void doemit(register struct parse *p, sop op, size_t opnd);
1420
* It might seem better to implement this as a macro with a function as
1421
* hard-case backup, but it's just too big and messy unless there are
1422
* some changes to the data structures. Maybe later.
1426
register struct parse *p;
1430
/* avoid making error situations worse */
1434
/* deal with oversize operands ("can't happen", more or less) */
1435
assert(opnd < 1<<OPSHIFT);
1437
/* deal with undersized strip */
1438
if (p->slen >= p->ssize)
1439
enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1440
assert(p->slen < p->ssize);
1442
/* finally, it's all reduced to the easy case */
1443
p->strip[p->slen++] = SOP(op, opnd);
1447
- doinsert - insert a sop into the strip
1448
== static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1451
doinsert(p, op, opnd, pos)
1452
register struct parse *p;
1461
/* avoid making error situations worse */
1466
EMIT(op, opnd); /* do checks, ensure space */
1467
assert(HERE() == sn+1);
1470
/* adjust paren pointers */
1472
for (i = 1; i < NPAREN; i++) {
1473
if (p->pbegin[i] >= pos) {
1476
if (p->pend[i] >= pos) {
1481
int length=(HERE()-pos-1)*sizeof(sop);
1482
bmove_upp((uchar *) &p->strip[pos+1]+length,
1483
(uchar *) &p->strip[pos]+length,
1487
memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1488
(HERE()-pos-1)*sizeof(sop));
1494
- dofwd - complete a forward reference
1495
== static void dofwd(register struct parse *p, sopno pos, sop value);
1498
dofwd(p, pos, value)
1499
register struct parse *p;
1503
/* avoid making error situations worse */
1507
assert(value < 1<<OPSHIFT);
1508
p->strip[pos] = OP(p->strip[pos]) | value;
1512
- enlarge - enlarge the strip
1513
== static void enlarge(register struct parse *p, sopno size);
1517
register struct parse *p;
1518
register sopno size;
1522
if (p->ssize >= size)
1525
sp = (sop *)realloc(p->strip, size*sizeof(sop));
1527
SETERROR(REG_ESPACE);
1535
- stripsnug - compact the strip
1536
== static void stripsnug(register struct parse *p, register struct re_guts *g);
1540
register struct parse *p;
1541
register struct re_guts *g;
1543
g->nstates = p->slen;
1544
g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
1545
if (g->strip == NULL) {
1546
SETERROR(REG_ESPACE);
1547
g->strip = p->strip;
1552
- findmust - fill in must and mlen with longest mandatory literal string
1553
== static void findmust(register struct parse *p, register struct re_guts *g);
1555
* This algorithm could do fancy things like analyzing the operands of |
1556
* for common subsequences. Someday. This code is simple and finds most
1557
* of the interesting cases.
1559
* Note that must and mlen got initialized during setup.
1564
register struct re_guts *g;
1568
register sop *newstart;
1569
register sopno newlen;
1573
LINT_INIT(start); LINT_INIT(newstart);
1574
/* avoid making error situations worse */
1578
/* find the longest OCHAR sequence in strip */
1580
scan = g->strip + 1;
1584
case OCHAR: /* sequence member */
1585
if (newlen == 0) /* new sequence */
1586
newstart = scan - 1;
1589
case OPLUS_: /* things that don't break one */
1593
case OQUEST_: /* things that must be skipped */
1599
/* assert() interferes w debug printouts */
1600
if (OP(s) != O_QUEST && OP(s) != O_CH &&
1605
} while (OP(s) != O_QUEST && OP(s) != O_CH);
1607
default: /* things that break a sequence */
1608
if (newlen > g->mlen) { /* ends one */
1615
} while (OP(s) != OEND);
1617
if (g->mlen == 0) /* there isn't one */
1620
/* turn it into a character string */
1621
g->must = malloc((size_t)g->mlen + 1);
1622
if (g->must == NULL) { /* argh; just forget it */
1628
for (i = g->mlen; i > 0; i--) {
1629
while (OP(s = *scan++) != OCHAR)
1631
assert(cp < g->must + g->mlen);
1632
*cp++ = (char)OPND(s);
1634
assert(cp == g->must + g->mlen);
1635
*cp++ = '\0'; /* just on general principles */
1639
- pluscount - count + nesting
1640
== static sopno pluscount(register struct parse *p, register struct re_guts *g);
1642
static sopno /* nesting depth */
1645
register struct re_guts *g;
1649
register sopno plusnest = 0;
1650
register sopno maxnest = 0;
1653
return(0); /* there may not be an OEND */
1655
scan = g->strip + 1;
1663
if (plusnest > maxnest)
1668
} while (OP(s) != OEND);