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 prevback;
217
register sopno prevfwd;
219
register int first = 1; /* is this the first alternative? */
221
/* do a bunch of concatenated expressions */
223
while (MORE() && (c = PEEK()) != '|' && c != stop)
225
if(REQUIRE(HERE() != conc, REG_EMPTY)) {}/* require nonempty */
228
break; /* NOTE BREAK OUT */
231
INSERT(OCH_, conc); /* offset is wrong */
236
ASTERN(OOR1, prevback);
238
AHEAD(prevfwd); /* fix previous offset */
240
EMIT(OOR2, 0); /* offset is very wrong */
243
if (!first) { /* tail-end fixups */
245
ASTERN(O_CH, prevback);
248
assert(!MORE() || SEE(stop));
252
- p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
253
== static void p_ere_exp(register struct parse *p);
257
register struct parse *p;
263
register sopno subno;
266
assert(MORE()); /* caller should have ensured this */
272
if(REQUIRE(MORE(), REG_EPAREN)) {}
274
subno = (sopno) p->g->nsub;
276
p->pbegin[subno] = HERE();
277
EMIT(OLPAREN, subno);
280
if (subno < NPAREN) {
281
p->pend[subno] = HERE();
282
assert(p->pend[subno] != 0);
284
EMIT(ORPAREN, subno);
285
if(MUSTEAT(')', REG_EPAREN)) {}
287
#ifndef POSIX_MISTAKE
288
case ')': /* happens only if no current unmatched ( */
290
* You may ask, why the ifndef? Because I didn't notice
291
* this until slightly too late for 1003.2, and none of the
292
* other 1003.2 regular-expression reviewers noticed it at
293
* all. So an unmatched ) is legal POSIX, at least until
294
* we can get it fixed.
296
SETERROR(REG_EPAREN);
301
p->g->iflags |= USEBOL;
307
p->g->iflags |= USEEOL;
316
SETERROR(REG_BADRPT);
319
if (p->g->cflags®_NEWLINE)
328
if(REQUIRE(MORE(), REG_EESCAPE)) {}
332
case '{': /* okay as ordinary except if digit follows */
333
if(REQUIRE(!MORE() || !my_isdigit(p->charset,PEEK()), REG_BADRPT)) {}
343
/* we call { a repetition if followed by a digit */
344
if (!( c == '*' || c == '+' || c == '?' ||
345
(c == '{' && MORE2() &&
346
my_isdigit(p->charset,PEEK2())) ))
347
return; /* no repetition, we're done */
350
if(REQUIRE(!wascaret, REG_BADRPT)) {}
352
case '*': /* implemented as +? */
353
/* this case does not require the (y|) trick, noKLUDGE */
356
INSERT(OQUEST_, pos);
357
ASTERN(O_QUEST, pos);
364
/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
365
INSERT(OCH_, pos); /* offset slightly wrong */
366
ASTERN(OOR1, pos); /* this one's right */
367
AHEAD(pos); /* fix the OCH_ */
368
EMIT(OOR2, 0); /* offset very wrong... */
369
AHEAD(THERE()); /* ...so fix it */
370
ASTERN(O_CH, THERETHERE());
375
if (my_isdigit(p->charset,PEEK())) {
377
if(REQUIRE(count <= count2, REG_BADBR)) {}
378
} else /* single number with comma */
379
count2 = RE_INFINITY;
380
} else /* just a single number */
382
repeat(p, pos, count, count2);
383
if (!EAT('}')) { /* error heuristics */
384
while (MORE() && PEEK() != '}')
386
if(REQUIRE(MORE(), REG_EBRACE)) {}
395
if (!( c == '*' || c == '+' || c == '?' ||
396
(c == '{' && MORE2() &&
397
my_isdigit(p->charset,PEEK2())) ) )
399
SETERROR(REG_BADRPT);
403
- p_str - string (no metacharacters) "parser"
404
== static void p_str(register struct parse *p);
408
register struct parse *p;
410
if(REQUIRE(MORE(), REG_EMPTY)) {}
412
ordinary(p, GETNEXT());
416
- p_bre - BRE parser top level, anchoring and concatenation
417
== static void p_bre(register struct parse *p, register int end1, \
418
== register int end2);
419
* Giving end1 as OUT essentially eliminates the end1/end2 check.
421
* This implementation is a bit of a kludge, in that a trailing $ is first
422
* taken as an ordinary character and then revised to be an anchor. The
423
* only undesirable side effect is that '$' gets included as a character
424
* category in such cases. This is fairly harmless; not worth fixing.
425
* The amount of lookahead needed to avoid this kludge is excessive.
429
register struct parse *p;
430
register int end1; /* first terminating character */
431
register int end2; /* second terminating character */
433
register sopno start = HERE();
434
register int first = 1; /* first subexpression? */
435
register int wasdollar = 0;
439
p->g->iflags |= USEBOL;
442
while (MORE() && !SEETWO(end1, end2)) {
443
wasdollar = p_simp_re(p, first);
446
if (wasdollar) { /* oops, that was a trailing anchor */
449
p->g->iflags |= USEEOL;
453
if(REQUIRE(HERE() != start, REG_EMPTY)) {} /* require nonempty */
457
- p_simp_re - parse a simple RE, an atom possibly followed by a repetition
458
== static int p_simp_re(register struct parse *p, int starordinary);
460
static int /* was the simple RE an unbackslashed $? */
461
p_simp_re(p, starordinary)
462
register struct parse *p;
463
int starordinary; /* is a leading * an ordinary character? */
470
register sopno subno;
471
# define BACKSL (1<<CHAR_BIT)
473
pos = HERE(); /* repetion op, if any, covers from here */
475
assert(MORE()); /* caller should have ensured this */
478
if(REQUIRE(MORE(), REG_EESCAPE)) {}
479
c = BACKSL | (unsigned char)GETNEXT();
483
if (p->g->cflags®_NEWLINE)
492
SETERROR(REG_BADRPT);
496
subno = (sopno) p->g->nsub;
498
p->pbegin[subno] = HERE();
499
EMIT(OLPAREN, subno);
500
/* the MORE here is an error heuristic */
501
if (MORE() && !SEETWO('\\', ')'))
503
if (subno < NPAREN) {
504
p->pend[subno] = HERE();
505
assert(p->pend[subno] != 0);
507
EMIT(ORPAREN, subno);
508
if(REQUIRE(EATTWO('\\', ')'), REG_EPAREN)) {}
510
case BACKSL|')': /* should not get here -- must be user */
512
SETERROR(REG_EPAREN);
523
i = (c&~BACKSL) - '0';
525
if (p->pend[i] != 0) {
526
assert((uint) i <= p->g->nsub);
528
assert(p->pbegin[i] != 0);
529
assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
530
assert(OP(p->strip[p->pend[i]]) == ORPAREN);
531
(void) dupl(p, p->pbegin[i]+1, p->pend[i]);
534
SETERROR(REG_ESUBREG);
538
if(REQUIRE(starordinary, REG_BADRPT)) {}
541
ordinary(p, c &~ BACKSL);
545
if (EAT('*')) { /* implemented as +? */
546
/* this case does not require the (y|) trick, noKLUDGE */
549
INSERT(OQUEST_, pos);
550
ASTERN(O_QUEST, pos);
551
} else if (EATTWO('\\', '{')) {
554
if (MORE() && my_isdigit(p->charset,PEEK())) {
556
if(REQUIRE(count <= count2, REG_BADBR)) {}
557
} else /* single number with comma */
558
count2 = RE_INFINITY;
559
} else /* just a single number */
561
repeat(p, pos, count, count2);
562
if (!EATTWO('\\', '}')) { /* error heuristics */
563
while (MORE() && !SEETWO('\\', '}'))
565
if(REQUIRE(MORE(), REG_EBRACE)) {}
568
} else if (c == (unsigned char)'$') /* $ (but not \$) ends it */
575
- p_count - parse a repetition count
576
== static int p_count(register struct parse *p);
578
static int /* the value */
580
register struct parse *p;
582
register int count = 0;
583
register int ndigits = 0;
585
while (MORE() && my_isdigit(p->charset,PEEK()) && count <= DUPMAX) {
586
count = count*10 + (GETNEXT() - '0');
590
if(REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR)) {}
595
- p_bracket - parse a bracketed character list
596
== static void p_bracket(register struct parse *p);
598
* Note a significant property of this code: if the allocset() did SETERROR,
599
* no set operations are done.
603
register struct parse *p;
605
register cset *cs = allocset(p);
606
register int invert = 0;
608
/* Dept of Truly Sickening Special-Case Kludges */
609
if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
614
if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
621
invert++; /* make note to invert set at end */
626
while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
630
if(MUSTEAT(']', REG_EBRACK)) {}
632
if (p->error != 0) /* don't mess things up further */
635
if (p->g->cflags®_ICASE) {
639
for (i = p->g->csetsize - 1; i >= 0; i--)
640
if (CHIN(cs, i) && my_isalpha(p->charset,i)) {
641
ci = othercase(p->charset,i);
645
if (cs->multis != NULL)
651
for (i = p->g->csetsize - 1; i >= 0; i--)
656
if (p->g->cflags®_NEWLINE)
658
if (cs->multis != NULL)
662
assert(cs->multis == NULL); /* xxx */
664
if (nch(p, cs) == 1) { /* optimize singleton sets */
665
ordinary(p, firstch(p, cs));
668
EMIT(OANYOF, freezeset(p, cs));
672
- p_b_term - parse one term of a bracketed character list
673
== static void p_b_term(register struct parse *p, register cset *cs);
677
register struct parse *p;
681
register char start, finish;
684
/* classify what we've got */
685
switch ((MORE()) ? PEEK() : '\0') {
687
c = (MORE2()) ? PEEK2() : '\0';
690
SETERROR(REG_ERANGE);
691
return; /* NOTE RETURN */
699
case ':': /* character class */
701
if(REQUIRE(MORE(), REG_EBRACK)) {}
703
if(REQUIRE(c != '-' && c != ']', REG_ECTYPE)) {}
705
if(REQUIRE(MORE(), REG_EBRACK)) {}
706
if(REQUIRE(EATTWO(':', ']'), REG_ECTYPE)) {}
708
case '=': /* equivalence class */
710
if(REQUIRE(MORE(), REG_EBRACK)) {}
712
if(REQUIRE(c != '-' && c != ']', REG_ECOLLATE)) {}
714
if(REQUIRE(MORE(), REG_EBRACK)) {}
715
if(REQUIRE(EATTWO('=', ']'), REG_ECOLLATE)) {}
717
default: /* symbol, ordinary character, or range */
718
/* xxx revision needed for multichar stuff */
719
start = p_b_symbol(p);
720
if (SEE('-') && MORE2() && PEEK2() != ']') {
726
finish = p_b_symbol(p);
729
/* xxx what about signed chars here... */
730
if(REQUIRE(start <= finish, REG_ERANGE)) {}
731
for (i = start; i <= finish; i++)
738
- p_b_cclass - parse a character-class name and deal with it
739
== static void p_b_cclass(register struct parse *p, register cset *cs);
743
register struct parse *p;
746
register char *sp = p->next;
747
register struct cclass *cp;
750
while (MORE() && my_isalpha(p->charset,PEEK()))
753
for (cp = cclasses; cp->name != NULL; cp++)
754
if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
756
if (cp->name == NULL) {
757
/* oops, didn't find it */
758
SETERROR(REG_ECTYPE);
762
#ifndef USE_ORIG_REGEX_CODE
765
for (i=1 ; i<256 ; i++)
766
if (p->charset->ctype[i+1] & cp->mask)
771
register char *u = (char*) cp->chars;
774
while ((c = *u++) != '\0')
777
for (u = (char*) cp->multis; *u != '\0'; u += strlen(u) + 1)
785
- p_b_eclass - parse an equivalence-class name and deal with it
786
== static void p_b_eclass(register struct parse *p, register cset *cs);
788
* This implementation is incomplete. xxx
792
register struct parse *p;
797
c = p_b_coll_elem(p, '=');
802
- p_b_symbol - parse a character or [..]ed multicharacter collating symbol
803
== static char p_b_symbol(register struct parse *p);
805
static char /* value of symbol */
807
register struct parse *p;
811
if(REQUIRE(MORE(), REG_EBRACK)) {}
812
if (!EATTWO('[', '.'))
815
/* collating symbol */
816
value = p_b_coll_elem(p, '.');
817
if(REQUIRE(EATTWO('.', ']'), REG_ECOLLATE)) {}
822
- p_b_coll_elem - parse a collating-element name and look it up
823
== static char p_b_coll_elem(register struct parse *p, int endc);
825
static char /* value of collating element */
826
p_b_coll_elem(p, endc)
827
register struct parse *p;
828
int endc; /* name ended by endc,']' */
830
register char *sp = p->next;
831
register struct cname *cp;
833
register __int64 len;
837
while (MORE() && !SEETWO(endc, ']'))
840
SETERROR(REG_EBRACK);
844
for (cp = cnames; cp->name != NULL; cp++)
845
if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
846
return(cp->code); /* known name */
848
return(*sp); /* single character */
849
SETERROR(REG_ECOLLATE); /* neither */
854
- othercase - return the case counterpart of an alphabetic
855
== static char othercase(int ch);
857
static char /* if no counterpart, return ch */
858
othercase(charset,ch)
859
CHARSET_INFO *charset;
863
In MySQL some multi-byte character sets
864
have 'ctype' array but don't have 'to_lower'
865
and 'to_upper' arrays. In this case we handle
866
only basic latin letters a..z and A..Z.
868
If 'to_lower' and 'to_upper' arrays are empty in a character set,
869
then my_isalpha(cs, ch) should never return TRUE for characters
870
other than basic latin letters. Otherwise it should be
871
considered as a mistake in character set definition.
873
assert(my_isalpha(charset,ch));
874
if (my_isupper(charset,ch))
876
return(charset->to_lower ? my_tolower(charset,ch) :
879
else if (my_islower(charset,ch))
881
return(charset->to_upper ? my_toupper(charset,ch) :
884
else /* peculiar, but could happen */
889
- bothcases - emit a dualcase version of a two-case character
890
== static void bothcases(register struct parse *p, int ch);
892
* Boy, is this implementation ever a kludge...
896
register struct parse *p;
899
register char *oldnext = p->next;
900
register char *oldend = p->end;
903
assert(othercase(p->charset, ch) != ch); /* p_bracket() would recurse */
910
assert(p->next == bracket+2);
916
- ordinary - emit an ordinary character
917
== static void ordinary(register struct parse *p, register int ch);
921
register struct parse *p;
924
register cat_t *cap = p->g->categories;
926
if ((p->g->cflags®_ICASE) && my_isalpha(p->charset,ch) &&
927
othercase(p->charset,ch) != ch)
930
EMIT(OCHAR, (unsigned char)ch);
932
cap[ch] = p->g->ncategories++;
937
- nonnewline - emit REG_NEWLINE version of OANY
938
== static void nonnewline(register struct parse *p);
940
* Boy, is this implementation ever a kludge...
944
register struct parse *p;
946
register char *oldnext = p->next;
947
register char *oldend = p->end;
957
assert(p->next == bracket+3);
963
- repeat - generate code for a bounded repetition, recursively if needed
964
== static void repeat(register struct parse *p, sopno start, int from, int to);
967
repeat(p, start, from, to)
968
register struct parse *p;
969
sopno start; /* operand from here to end of strip */
970
int from; /* repeated from this number */
971
int to; /* to this number of times (maybe RE_INFINITY) */
973
register sopno finish = HERE();
976
# define REP(f, t) ((f)*8 + (t))
977
# define MAP(n) (((n) <= 1) ? (n) : ((n) == RE_INFINITY) ? INF : N)
980
if (p->error != 0) /* head off possible runaway recursion */
985
switch (REP(MAP(from), MAP(to))) {
986
case REP(0, 0): /* must be user doing this */
987
DROP(finish-start); /* drop the operand */
989
case REP(0, 1): /* as x{1,1}? */
990
case REP(0, N): /* as x{1,n}? */
991
case REP(0, INF): /* as x{1,}? */
992
/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
993
INSERT(OCH_, start); /* offset is wrong... */
994
repeat(p, start+1, 1, to);
996
AHEAD(start); /* ... fix it */
999
ASTERN(O_CH, THERETHERE());
1001
case REP(1, 1): /* trivial case */
1004
case REP(1, N): /* as x?x{1,n-1} */
1005
/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1006
INSERT(OCH_, start);
1007
ASTERN(OOR1, start);
1009
EMIT(OOR2, 0); /* offset very wrong... */
1010
AHEAD(THERE()); /* ...so fix it */
1011
ASTERN(O_CH, THERETHERE());
1012
copy = dupl(p, start+1, finish+1);
1013
assert(copy == finish+4);
1014
repeat(p, copy, 1, to-1);
1016
case REP(1, INF): /* as x+ */
1017
INSERT(OPLUS_, start);
1018
ASTERN(O_PLUS, start);
1020
case REP(N, N): /* as xx{m-1,n-1} */
1021
copy = dupl(p, start, finish);
1022
repeat(p, copy, from-1, to-1);
1024
case REP(N, INF): /* as xx{n-1,INF} */
1025
copy = dupl(p, start, finish);
1026
repeat(p, copy, from-1, to);
1028
default: /* "can't happen" */
1029
SETERROR(REG_ASSERT); /* just in case */
1035
- seterr - set an error condition
1036
== static int seterr(register struct parse *p, int e);
1038
static int /* useless but makes type checking happy */
1040
register struct parse *p;
1043
if (p->error == 0) /* keep earliest error condition */
1045
p->next = nuls; /* try to bring things to a halt */
1047
return(0); /* make the return value well-defined */
1051
- allocset - allocate a set of characters for []
1052
== static cset *allocset(register struct parse *p);
1056
register struct parse *p;
1058
register int no = p->g->ncsets++;
1060
register size_t nbytes;
1062
register size_t css = (size_t)p->g->csetsize;
1065
if (no >= p->ncsalloc) { /* need another column of space */
1066
p->ncsalloc += CHAR_BIT;
1068
assert(nc % CHAR_BIT == 0);
1069
nbytes = nc / CHAR_BIT * css;
1070
if (p->g->sets == NULL)
1071
p->g->sets = (cset *)malloc(nc * sizeof(cset));
1073
p->g->sets = (cset *)realloc((char *)p->g->sets,
1075
if (p->g->setbits == NULL)
1076
p->g->setbits = (uch *)malloc(nbytes);
1078
p->g->setbits = (uch *)realloc((char *)p->g->setbits,
1080
/* xxx this isn't right if setbits is now NULL */
1081
for (i = 0; i < no; i++)
1082
p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1084
if (p->g->sets != NULL && p->g->setbits != NULL)
1085
(void) memset((char *)p->g->setbits + (nbytes - css),
1089
SETERROR(REG_ESPACE);
1090
/* caller's responsibility not to do set ops */
1094
assert(p->g->sets != NULL); /* xxx */
1095
cs = &p->g->sets[no];
1096
cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1097
cs->mask = 1 << ((no) % CHAR_BIT);
1106
- freeset - free a now-unused set
1107
== static void freeset(register struct parse *p, register cset *cs);
1111
register struct parse *p;
1115
register cset *top = &p->g->sets[p->g->ncsets];
1116
register size_t css = (size_t)p->g->csetsize;
1118
for (i = 0; i < css; i++)
1120
if (cs == top-1) /* recover only the easy case */
1125
- freezeset - final processing on a set of characters
1126
== static int freezeset(register struct parse *p, register cset *cs);
1128
* The main task here is merging identical sets. This is usually a waste
1129
* of time (although the hash code minimizes the overhead), but can win
1130
* big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1131
* is done using addition rather than xor -- all ASCII [aA] sets xor to
1134
static int /* set number */
1136
register struct parse *p;
1139
register uch h = cs->hash;
1141
register cset *top = &p->g->sets[p->g->ncsets];
1143
register size_t css = (size_t)p->g->csetsize;
1145
/* look for an earlier one which is the same */
1146
for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1147
if (cs2->hash == h && cs2 != cs) {
1149
for (i = 0; i < css; i++)
1150
if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1156
if (cs2 < top) { /* found one */
1161
return((int)(cs - p->g->sets));
1165
- firstch - return first character in a set (which must have at least one)
1166
== static int firstch(register struct parse *p, register cset *cs);
1168
static int /* character; there is no "none" value */
1170
register struct parse *p;
1174
register size_t css = (size_t)p->g->csetsize;
1176
for (i = 0; i < css; i++)
1180
return(0); /* arbitrary */
1184
- nch - number of characters in a set
1185
== static int nch(register struct parse *p, register cset *cs);
1189
register struct parse *p;
1193
register size_t css = (size_t)p->g->csetsize;
1196
for (i = 0; i < css; i++)
1202
#ifdef USE_ORIG_REGEX_CODE
1204
- mcadd - add a collating element to a cset
1205
== static void mcadd(register struct parse *p, register cset *cs, \
1206
== register char *cp);
1210
register struct parse *p;
1214
register size_t oldend = cs->smultis;
1216
cs->smultis += strlen(cp) + 1;
1217
if (cs->multis == NULL)
1218
cs->multis = malloc(cs->smultis);
1220
cs->multis = realloc(cs->multis, cs->smultis);
1221
if (cs->multis == NULL) {
1222
SETERROR(REG_ESPACE);
1226
(void) strcpy(cs->multis + oldend - 1, cp);
1227
cs->multis[cs->smultis - 1] = '\0';
1233
- mcsub - subtract a collating element from a cset
1234
== static void mcsub(register cset *cs, register char *cp);
1241
register char *fp = mcfind(cs, cp);
1242
register size_t len = strlen(fp);
1245
(void) memmove(fp, fp + len + 1,
1246
cs->smultis - (fp + len + 1 - cs->multis));
1249
if (cs->smultis == 0) {
1255
cs->multis = realloc(cs->multis, cs->smultis);
1256
assert(cs->multis != NULL);
1260
- mcin - is a collating element in a cset?
1261
== static int mcin(register cset *cs, register char *cp);
1268
return(mcfind(cs, cp) != NULL);
1272
- mcfind - find a collating element in a cset
1273
== static char *mcfind(register cset *cs, register char *cp);
1282
if (cs->multis == NULL)
1284
for (p = cs->multis; *p != '\0'; p += strlen(p) + 1)
1285
if (strcmp(cp, p) == 0)
1292
- mcinvert - invert the list of collating elements in a cset
1293
== static void mcinvert(register struct parse *p, register cset *cs);
1295
* This would have to know the set of possibilities. Implementation
1300
register struct parse *p __attribute__((unused));
1301
register cset *cs __attribute__((unused));
1303
assert(cs->multis == NULL); /* xxx */
1307
- mccase - add case counterparts of the list of collating elements in a cset
1308
== static void mccase(register struct parse *p, register cset *cs);
1310
* This would have to know the set of possibilities. Implementation
1315
register struct parse *p __attribute__((unused));
1316
register cset *cs __attribute__((unused));
1318
assert(cs->multis == NULL); /* xxx */
1322
- isinsets - is this character in any sets?
1323
== static int isinsets(register struct re_guts *g, int c);
1325
static int /* predicate */
1327
register struct re_guts *g;
1332
register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1333
register unsigned uc = (unsigned char)c;
1335
for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1342
- samesets - are these two characters in exactly the same sets?
1343
== static int samesets(register struct re_guts *g, int c1, int c2);
1345
static int /* predicate */
1347
register struct re_guts *g;
1353
register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1354
register unsigned uc1 = (unsigned char)c1;
1355
register unsigned uc2 = (unsigned char)c2;
1357
for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1358
if (col[uc1] != col[uc2])
1364
- categorize - sort out character categories
1365
== static void categorize(struct parse *p, register struct re_guts *g);
1370
register struct re_guts *g;
1372
register cat_t *cats = g->categories;
1377
/* avoid making error situations worse */
1381
for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1382
if (cats[c] == 0 && isinsets(g, c)) {
1383
cat = g->ncategories++;
1385
for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1386
if (cats[c2] == 0 && samesets(g, c, c2))
1392
- dupl - emit a duplicate of a bunch of sops
1393
== static sopno dupl(register struct parse *p, sopno start, sopno finish);
1395
static sopno /* start of duplicate */
1396
dupl(p, start, finish)
1397
register struct parse *p;
1398
sopno start; /* from here */
1399
sopno finish; /* to this less one */
1401
register sopno ret = HERE();
1402
register sopno len = finish - start;
1404
assert(finish >= start);
1407
enlarge(p, p->ssize + len); /* this many unexpected additions */
1408
assert(p->ssize >= p->slen + len);
1409
(void) memcpy((char *)(p->strip + p->slen),
1410
(char *)(p->strip + start), (size_t)len*sizeof(sop));
1416
- doemit - emit a strip operator
1417
== static void doemit(register struct parse *p, sop op, size_t opnd);
1419
* It might seem better to implement this as a macro with a function as
1420
* hard-case backup, but it's just too big and messy unless there are
1421
* some changes to the data structures. Maybe later.
1425
register struct parse *p;
1429
/* avoid making error situations worse */
1433
/* deal with oversize operands ("can't happen", more or less) */
1434
assert(opnd < 1<<OPSHIFT);
1436
/* deal with undersized strip */
1437
if (p->slen >= p->ssize)
1438
enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1439
assert(p->slen < p->ssize);
1441
/* finally, it's all reduced to the easy case */
1442
p->strip[p->slen++] = SOP(op, opnd);
1446
- doinsert - insert a sop into the strip
1447
== static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1450
doinsert(p, op, opnd, pos)
1451
register struct parse *p;
1460
/* avoid making error situations worse */
1465
EMIT(op, opnd); /* do checks, ensure space */
1466
assert(HERE() == sn+1);
1469
/* adjust paren pointers */
1471
for (i = 1; i < NPAREN; i++) {
1472
if (p->pbegin[i] >= pos) {
1475
if (p->pend[i] >= pos) {
1480
int length=(HERE()-pos-1)*sizeof(sop);
1481
bmove_upp((uchar *) &p->strip[pos+1]+length,
1482
(uchar *) &p->strip[pos]+length,
1486
memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1487
(HERE()-pos-1)*sizeof(sop));
1493
- dofwd - complete a forward reference
1494
== static void dofwd(register struct parse *p, sopno pos, sop value);
1497
dofwd(p, pos, value)
1498
register struct parse *p;
1502
/* avoid making error situations worse */
1506
assert(value < 1<<OPSHIFT);
1507
p->strip[pos] = OP(p->strip[pos]) | value;
1511
- enlarge - enlarge the strip
1512
== static void enlarge(register struct parse *p, sopno size);
1516
register struct parse *p;
1517
register sopno size;
1521
if (p->ssize >= size)
1524
sp = (sop *)realloc(p->strip, size*sizeof(sop));
1526
SETERROR(REG_ESPACE);
1534
- stripsnug - compact the strip
1535
== static void stripsnug(register struct parse *p, register struct re_guts *g);
1539
register struct parse *p;
1540
register struct re_guts *g;
1542
g->nstates = p->slen;
1543
g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
1544
if (g->strip == NULL) {
1545
SETERROR(REG_ESPACE);
1546
g->strip = p->strip;
1551
- findmust - fill in must and mlen with longest mandatory literal string
1552
== static void findmust(register struct parse *p, register struct re_guts *g);
1554
* This algorithm could do fancy things like analyzing the operands of |
1555
* for common subsequences. Someday. This code is simple and finds most
1556
* of the interesting cases.
1558
* Note that must and mlen got initialized during setup.
1563
register struct re_guts *g;
1567
register sop *newstart;
1568
register sopno newlen;
1572
/* avoid making error situations worse */
1576
/* find the longest OCHAR sequence in strip */
1578
scan = g->strip + 1;
1582
case OCHAR: /* sequence member */
1583
if (newlen == 0) /* new sequence */
1584
newstart = scan - 1;
1587
case OPLUS_: /* things that don't break one */
1591
case OQUEST_: /* things that must be skipped */
1597
/* assert() interferes w debug printouts */
1598
if (OP(s) != O_QUEST && OP(s) != O_CH &&
1603
} while (OP(s) != O_QUEST && OP(s) != O_CH);
1605
default: /* things that break a sequence */
1606
if (newlen > g->mlen) { /* ends one */
1613
} while (OP(s) != OEND);
1615
if (g->mlen == 0) /* there isn't one */
1618
/* turn it into a character string */
1619
g->must = malloc((size_t)g->mlen + 1);
1620
if (g->must == NULL) { /* argh; just forget it */
1626
for (i = g->mlen; i > 0; i--) {
1627
while (OP(s = *scan++) != OCHAR)
1629
assert(cp < g->must + g->mlen);
1630
*cp++ = (char)OPND(s);
1632
assert(cp == g->must + g->mlen);
1633
*cp++ = '\0'; /* just on general principles */
1637
- pluscount - count + nesting
1638
== static sopno pluscount(register struct parse *p, register struct re_guts *g);
1640
static sopno /* nesting depth */
1643
register struct re_guts *g;
1647
register sopno plusnest = 0;
1648
register sopno maxnest = 0;
1651
return(0); /* there may not be an OEND */
1653
scan = g->strip + 1;
1661
if (plusnest > maxnest)
1666
} while (OP(s) != OEND);