diff options
Diffstat (limited to 'generic/regcomp.c')
| -rw-r--r-- | generic/regcomp.c | 107 |
1 files changed, 64 insertions, 43 deletions
diff --git a/generic/regcomp.c b/generic/regcomp.c index 983cd7a..2bc0744 100644 --- a/generic/regcomp.c +++ b/generic/regcomp.c @@ -2,7 +2,7 @@ * re_*comp and friends - compile REs * This file #includes several others (see the bottom). * - * Copyright © 1998, 1999 Henry Spencer. All rights reserved. + * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved. * * Development of this software was funded, in part, by Cray Research Inc., * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics @@ -56,9 +56,10 @@ static const chr *scanplain(struct vars *); static void onechr(struct vars *, pchr, struct state *, struct state *); static void dovec(struct vars *, struct cvec *, struct state *, struct state *); static void wordchrs(struct vars *); -static struct subre *sub_re(struct vars *, int, int, struct state *, struct state *); +static struct subre *subre(struct vars *, int, int, struct state *, struct state *); static void freesubre(struct vars *, struct subre *); static void freesrnode(struct vars *, struct subre *); +static void optst(struct vars *, struct subre *); static int numst(struct subre *, int); static void markst(struct subre *); static void cleanst(struct vars *); @@ -78,10 +79,13 @@ static void lexnest(struct vars *, const chr *, const chr *); static void lexword(struct vars *); static int next(struct vars *); static int lexescape(struct vars *); -static int lexdigits(struct vars *, int, int, int); +static chr lexdigits(struct vars *, int, int, int); static int brenext(struct vars *, pchr); static void skip(struct vars *); -static chr newline(void); +static chr newline(NOPARMS); +#ifdef REG_DEBUG +static const chr *ch(NOPARMS); +#endif static chr chrnamed(struct vars *, const chr *, const chr *, pchr); /* === regc_color.c === */ static void initcm(struct vars *, struct colormap *); @@ -205,11 +209,11 @@ struct vars { int cflags; /* copy of compile flags */ int lasttype; /* type of previous token */ int nexttype; /* type of next token */ - int nextvalue; /* value (if any) of next token */ + chr nextvalue; /* value (if any) of next token */ int lexcon; /* lexical context type (see lex.c) */ int nsubexp; /* subexpression count */ struct subre **subs; /* subRE pointer vector */ - int nsubs; /* length of vector */ + size_t nsubs; /* length of vector */ struct subre *sub10[10]; /* initial vector, enough for most */ struct nfa *nfa; /* the NFA */ struct colormap *cm; /* character color map */ @@ -243,7 +247,6 @@ struct vars { #define EMPTYARC(x, y) newarc(v->nfa, EMPTY, 0, x, y) /* token type codes, some also used as NFA arc types */ -#undef DIGIT /* prevent conflict with libtommath */ #define EMPTY 'n' /* no token present */ #define EOS 'e' /* end of string */ #define PLAIN 'p' /* ordinary character */ @@ -287,7 +290,8 @@ compile( { AllocVars(v); struct guts *g; - int i, j; + int i; + size_t j; FILE *debug = (flags®_PROGRESS) ? stdout : NULL; #define CNOERR() { if (ISERR()) return freev(v, v->err); } @@ -340,13 +344,13 @@ compile( re->re_info = 0; /* bits get set during parse */ re->re_csize = sizeof(chr); re->re_guts = NULL; - re->re_fns = (void*)(&functions); + re->re_fns = VS(&functions); /* * More complex setup, malloced things. */ - re->re_guts = (void*)(MALLOC(sizeof(struct guts))); + re->re_guts = VS(MALLOC(sizeof(struct guts))); if (re->re_guts == NULL) { return freev(v, REG_ESPACE); } @@ -394,6 +398,7 @@ compile( dumpnfa(v->nfa, debug); dumpst(v->tree, debug, 1); } + optst(v, v->tree); v->ntree = numst(v->tree, 1); markst(v->tree); cleanst(v); @@ -432,7 +437,7 @@ compile( * Can sacrifice main NFA now, so use it as work area. */ - (void) optimize(v->nfa, debug); + (DISCARD) optimize(v->nfa, debug); CNOERR(); makesearch(v, v->nfa); CNOERR(); @@ -475,10 +480,10 @@ moresubs( int wanted) /* want enough room for this one */ { struct subre **p; - int n; + size_t n; - assert(wanted > 0 && wanted >= v->nsubs); - n = wanted * 3 / 2 + 1; + assert(wanted > 0 && (size_t)wanted >= v->nsubs); + n = (size_t)wanted * 3 / 2 + 1; if (v->subs == v->sub10) { p = (struct subre **) MALLOC(n * sizeof(struct subre *)); if (p != NULL) { @@ -497,7 +502,7 @@ moresubs( *p = NULL; } assert(v->nsubs == n); - assert(wanted < v->nsubs); + assert((size_t)wanted < v->nsubs); } /* @@ -511,7 +516,7 @@ freev( struct vars *v, int err) { - int ret; + register int ret; if (v->re != NULL) { rfree(v->re); @@ -663,7 +668,7 @@ parse( assert(stopper == ')' || stopper == EOS); - branches = sub_re(v, '|', LONGER, init, final); + branches = subre(v, '|', LONGER, init, final); NOERRN(); branch = branches; firstbranch = 1; @@ -673,7 +678,7 @@ parse( * Need a place to hang the branch. */ - branch->right = sub_re(v, '|', LONGER, init, final); + branch->right = subre(v, '|', LONGER, init, final); NOERRN(); branch = branch->right; } @@ -744,7 +749,7 @@ parsebranch( lp = left; seencontent = 0; - t = sub_re(v, '=', 0, left, right); /* op '=' is tentative */ + t = subre(v, '=', 0, left, right); /* op '=' is tentative */ NOERRN(); while (!SEE('|') && !SEE(stopper) && !SEE(EOS)) { if (seencontent) { /* implicit concat operator */ @@ -808,7 +813,7 @@ parseqatom( atom = NULL; assert(lp->nouts == 0); /* must string new code */ assert(rp->nins == 0); /* between lp and rp */ - subno = 0; + subno = 0; /* just to shut lint up */ /* * An atom or constraint... @@ -921,7 +926,7 @@ parseqatom( */ NOTE(REG_UPBOTCH); - /* FALLTHRU */ + /* fallthrough into case PLAIN */ case PLAIN: onechr(v, v->nextvalue, lp, rp); okcolors(v->nfa, v->cm); @@ -952,10 +957,10 @@ parseqatom( if (cap) { v->nsubexp++; subno = v->nsubexp; - if (subno >= v->nsubs) { + if ((size_t)subno >= v->nsubs) { moresubs(v, subno); } - assert(subno < v->nsubs); + assert((size_t)subno < v->nsubs); } else { atomtype = PLAIN; /* something that's not '(' */ } @@ -977,7 +982,7 @@ parseqatom( NOERR(); if (cap) { v->subs[subno] = atom; - t = sub_re(v, '(', atom->flags|CAP, lp, rp); + t = subre(v, '(', atom->flags|CAP, lp, rp); NOERR(); t->subno = subno; t->left = atom; @@ -995,7 +1000,7 @@ parseqatom( INSIST(v->subs[v->nextvalue] != NULL, REG_ESUBREG); NOERR(); assert(v->nextvalue > 0); - atom = sub_re(v, 'b', BACKR, lp, rp); + atom = subre(v, 'b', BACKR, lp, rp); NOERR(); subno = v->nextvalue; atom->subno = subno; @@ -1110,7 +1115,7 @@ parseqatom( */ if (atom == NULL) { - atom = sub_re(v, '=', 0, lp, rp); + atom = subre(v, '=', 0, lp, rp); NOERR(); } @@ -1147,7 +1152,7 @@ parseqatom( * Break remaining subRE into x{...} and what follows. */ - t = sub_re(v, '.', COMBINE(qprefer, atom->flags), lp, rp); + t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp); NOERR(); t->left = atom; atomp = &t->left; @@ -1161,7 +1166,7 @@ parseqatom( */ assert(top->op == '=' && top->left == NULL && top->right == NULL); - top->left = sub_re(v, '=', top->flags, top->begin, lp); + top->left = subre(v, '=', top->flags, top->begin, lp); NOERR(); top->op = '.'; top->right = t; @@ -1230,9 +1235,9 @@ parseqatom( assert(m >= 1 && m != DUPINF && n >= 1); repeat(v, s, atom->begin, m-1, (n == DUPINF) ? n : n-1); f = COMBINE(qprefer, atom->flags); - t = sub_re(v, '.', f, s, atom->end); /* prefix and atom */ + t = subre(v, '.', f, s, atom->end); /* prefix and atom */ NOERR(); - t->left = sub_re(v, '=', PREF(f), s, atom->begin); + t->left = subre(v, '=', PREF(f), s, atom->begin); NOERR(); t->right = atom; *atomp = t; @@ -1247,7 +1252,7 @@ parseqatom( dupnfa(v->nfa, atom->begin, atom->end, s, s2); repeat(v, s, s2, m, n); f = COMBINE(qprefer, atom->flags); - t = sub_re(v, '*', f, s, s2); + t = subre(v, '*', f, s, s2); NOERR(); t->min = (short) m; t->max = (short) n; @@ -1265,7 +1270,7 @@ parseqatom( t->right = parsebranch(v, stopper, type, s2, rp, 1); } else { EMPTYARC(s2, rp); - t->right = sub_re(v, '=', 0, s2, rp); + t->right = subre(v, '=', 0, s2, rp); } NOERR(); assert(SEE('|') || SEE(stopper) || SEE(EOS)); @@ -1717,12 +1722,12 @@ wordchrs( } /* - - sub_re - allocate a subre - ^ static struct subre *sub_re(struct vars *, int, int, struct state *, + - subre - allocate a subre + ^ static struct subre *subre(struct vars *, int, int, struct state *, ^ struct state *); */ static struct subre * -sub_re( +subre( struct vars *v, int op, int flags, @@ -1810,6 +1815,25 @@ freesrnode( } /* + - optst - optimize a subRE subtree + ^ static void optst(struct vars *, struct subre *); + */ +static void +optst( + struct vars *v, + struct subre *t) +{ + /* + * DGP (2007-11-13): I assume it was the programmer's intent to eventually + * come back and add code to optimize subRE trees, but the routine coded + * just spends effort traversing the tree and doing nothing. We can do + * nothing with less effort. + */ + + return; +} + +/* - numst - number tree nodes (assigning "id" indexes) ^ static int numst(struct subre *, int); */ @@ -1899,10 +1923,10 @@ nfatree( assert(t != NULL && t->begin != NULL); if (t->left != NULL) { - (void) nfatree(v, t->left, f); + (DISCARD) nfatree(v, t->left, f); } if (t->right != NULL) { - (void) nfatree(v, t->right, f); + (DISCARD) nfatree(v, t->right, f); } return nfanode(v, t, f); @@ -2080,9 +2104,6 @@ dump( } fprintf(f, "\n"); dumpst(g->tree, f, 0); -#else - (void)re; - (void)f; #endif } @@ -2146,7 +2167,7 @@ stdump( fprintf(f, "}"); } if (nfapresent) { - fprintf(f, " %d-%d", t->begin->no, t->end->no); + fprintf(f, " %ld-%ld", (long)t->begin->no, (long)t->end->no); } if (t->left != NULL) { fprintf(f, " L:%s", stid(t->left, idbuf, sizeof(idbuf))); @@ -2185,9 +2206,9 @@ stid( return "unable"; } if (t->id != 0) { - snprintf(buf, bufsize, "%d", t->id); + sprintf(buf, "%d", t->id); } else { - snprintf(buf, bufsize, "%p", t); + sprintf(buf, "%p", t); } return buf; } |
