b.c revision 125601
185587Sobrien/**************************************************************** 285587SobrienCopyright (C) Lucent Technologies 1997 385587SobrienAll Rights Reserved 485587Sobrien 585587SobrienPermission to use, copy, modify, and distribute this software and 685587Sobrienits documentation for any purpose and without fee is hereby 785587Sobriengranted, provided that the above copyright notice appear in all 885587Sobriencopies and that both that the copyright notice and this 985587Sobrienpermission notice and warranty disclaimer appear in supporting 1085587Sobriendocumentation, and that the name Lucent Technologies or any of 1185587Sobrienits entities not be used in advertising or publicity pertaining 1285587Sobriento distribution of the software without specific, written prior 1385587Sobrienpermission. 1485587Sobrien 1585587SobrienLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 1685587SobrienINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 1785587SobrienIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 1885587SobrienSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1985587SobrienWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 2085587SobrienIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 2185587SobrienARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 2285587SobrienTHIS SOFTWARE. 2385587Sobrien****************************************************************/ 2485587Sobrien 2585587Sobrien/* lasciate ogne speranza, voi ch'entrate. */ 2685587Sobrien 2785587Sobrien#define DEBUG 2885587Sobrien 2985587Sobrien#include <ctype.h> 3085587Sobrien#include <stdio.h> 3185587Sobrien#include <string.h> 3285587Sobrien#include <stdlib.h> 3385587Sobrien#include "awk.h" 3485587Sobrien#include "ytab.h" 3585587Sobrien 36118194Sru#define HAT (NCHARS+2) /* matches ^ in regular expr */ 3785587Sobrien /* NCHARS is 2**n */ 3885587Sobrien#define MAXLIN 22 3985587Sobrien 4085587Sobrien#define type(v) (v)->nobj /* badly overloaded here */ 4185587Sobrien#define info(v) (v)->ntype /* badly overloaded here */ 4285587Sobrien#define left(v) (v)->narg[0] 4385587Sobrien#define right(v) (v)->narg[1] 4485587Sobrien#define parent(v) (v)->nnext 4585587Sobrien 4685587Sobrien#define LEAF case CCL: case NCCL: case CHAR: case DOT: case FINAL: case ALL: 4785587Sobrien#define UNARY case STAR: case PLUS: case QUEST: 4885587Sobrien 4985587Sobrien/* encoding in tree Nodes: 5085587Sobrien leaf (CCL, NCCL, CHAR, DOT, FINAL, ALL): 5185587Sobrien left is index, right contains value or pointer to value 5285587Sobrien unary (STAR, PLUS, QUEST): left is child, right is null 5385587Sobrien binary (CAT, OR): left and right are children 5485587Sobrien parent contains pointer to parent 5585587Sobrien*/ 5685587Sobrien 5785587Sobrien 5885587Sobrienint *setvec; 5985587Sobrienint *tmpset; 6085587Sobrienint maxsetvec = 0; 6185587Sobrien 6285587Sobrienint rtok; /* next token in current re */ 6385587Sobrienint rlxval; 6485587Sobrienstatic uschar *rlxstr; 6585587Sobrienstatic uschar *prestr; /* current position in current re */ 6685587Sobrienstatic uschar *lastre; /* origin of last re */ 6785587Sobrien 6885587Sobrienstatic int setcnt; 6985587Sobrienstatic int poscnt; 7085587Sobrien 7185587Sobrienchar *patbeg; 7285587Sobrienint patlen; 7385587Sobrien 7485587Sobrien#define NFA 20 /* cache this many dynamic fa's */ 7585587Sobrienfa *fatab[NFA]; 7685587Sobrienint nfatab = 0; /* entries in fatab */ 7785587Sobrien 78107806Sobrienfa *makedfa(const char *s, int anchor) /* returns dfa for reg expr s */ 7985587Sobrien{ 8085587Sobrien int i, use, nuse; 8185587Sobrien fa *pfa; 8285587Sobrien static int now = 1; 8385587Sobrien 8485587Sobrien if (setvec == 0) { /* first time through any RE */ 8585587Sobrien maxsetvec = MAXLIN; 8685587Sobrien setvec = (int *) malloc(maxsetvec * sizeof(int)); 8785587Sobrien tmpset = (int *) malloc(maxsetvec * sizeof(int)); 8885587Sobrien if (setvec == 0 || tmpset == 0) 8985587Sobrien overflo("out of space initializing makedfa"); 9085587Sobrien } 9185587Sobrien 9285587Sobrien if (compile_time) /* a constant for sure */ 9385587Sobrien return mkdfa(s, anchor); 9485587Sobrien for (i = 0; i < nfatab; i++) /* is it there already? */ 9585587Sobrien if (fatab[i]->anchor == anchor 9690902Sdes && strcmp((const char *) fatab[i]->restr, s) == 0) { 9785587Sobrien fatab[i]->use = now++; 9885587Sobrien return fatab[i]; 9985587Sobrien } 10085587Sobrien pfa = mkdfa(s, anchor); 10185587Sobrien if (nfatab < NFA) { /* room for another */ 10285587Sobrien fatab[nfatab] = pfa; 10385587Sobrien fatab[nfatab]->use = now++; 10485587Sobrien nfatab++; 10585587Sobrien return pfa; 10685587Sobrien } 10785587Sobrien use = fatab[0]->use; /* replace least-recently used */ 10885587Sobrien nuse = 0; 10985587Sobrien for (i = 1; i < nfatab; i++) 11085587Sobrien if (fatab[i]->use < use) { 11185587Sobrien use = fatab[i]->use; 11285587Sobrien nuse = i; 11385587Sobrien } 11485587Sobrien freefa(fatab[nuse]); 11585587Sobrien fatab[nuse] = pfa; 11685587Sobrien pfa->use = now++; 11785587Sobrien return pfa; 11885587Sobrien} 11985587Sobrien 120107806Sobrienfa *mkdfa(const char *s, int anchor) /* does the real work of making a dfa */ 12185587Sobrien /* anchor = 1 for anchored matches, else 0 */ 12285587Sobrien{ 12385587Sobrien Node *p, *p1; 12485587Sobrien fa *f; 12585587Sobrien 12685587Sobrien p = reparse(s); 12785587Sobrien p1 = op2(CAT, op2(STAR, op2(ALL, NIL, NIL), NIL), p); 12885587Sobrien /* put ALL STAR in front of reg. exp. */ 12985587Sobrien p1 = op2(CAT, p1, op2(FINAL, NIL, NIL)); 13085587Sobrien /* put FINAL after reg. exp. */ 13185587Sobrien 13285587Sobrien poscnt = 0; 13385587Sobrien penter(p1); /* enter parent pointers and leaf indices */ 13485587Sobrien if ((f = (fa *) calloc(1, sizeof(fa) + poscnt*sizeof(rrow))) == NULL) 13585587Sobrien overflo("out of space for fa"); 13685587Sobrien f->accept = poscnt-1; /* penter has computed number of positions in re */ 13785587Sobrien cfoll(f, p1); /* set up follow sets */ 13885587Sobrien freetr(p1); 13985587Sobrien if ((f->posns[0] = (int *) calloc(1, *(f->re[0].lfollow)*sizeof(int))) == NULL) 14085587Sobrien overflo("out of space in makedfa"); 14185587Sobrien if ((f->posns[1] = (int *) calloc(1, sizeof(int))) == NULL) 14285587Sobrien overflo("out of space in makedfa"); 14385587Sobrien *f->posns[1] = 0; 14485587Sobrien f->initstat = makeinit(f, anchor); 14585587Sobrien f->anchor = anchor; 14685587Sobrien f->restr = (uschar *) tostring(s); 14785587Sobrien return f; 14885587Sobrien} 14985587Sobrien 15085587Sobrienint makeinit(fa *f, int anchor) 15185587Sobrien{ 15285587Sobrien int i, k; 15385587Sobrien 15485587Sobrien f->curstat = 2; 15585587Sobrien f->out[2] = 0; 15685587Sobrien f->reset = 0; 15785587Sobrien k = *(f->re[0].lfollow); 15885587Sobrien xfree(f->posns[2]); 15985587Sobrien if ((f->posns[2] = (int *) calloc(1, (k+1)*sizeof(int))) == NULL) 16085587Sobrien overflo("out of space in makeinit"); 16185587Sobrien for (i=0; i <= k; i++) { 16285587Sobrien (f->posns[2])[i] = (f->re[0].lfollow)[i]; 16385587Sobrien } 16485587Sobrien if ((f->posns[2])[1] == f->accept) 16585587Sobrien f->out[2] = 1; 16685587Sobrien for (i=0; i < NCHARS; i++) 16785587Sobrien f->gototab[2][i] = 0; 16885587Sobrien f->curstat = cgoto(f, 2, HAT); 16985587Sobrien if (anchor) { 17085587Sobrien *f->posns[2] = k-1; /* leave out position 0 */ 17185587Sobrien for (i=0; i < k; i++) { 17285587Sobrien (f->posns[0])[i] = (f->posns[2])[i]; 17385587Sobrien } 17485587Sobrien 17585587Sobrien f->out[0] = f->out[2]; 17685587Sobrien if (f->curstat != 2) 17785587Sobrien --(*f->posns[f->curstat]); 17885587Sobrien } 17985587Sobrien return f->curstat; 18085587Sobrien} 18185587Sobrien 18285587Sobrienvoid penter(Node *p) /* set up parent pointers and leaf indices */ 18385587Sobrien{ 18485587Sobrien switch (type(p)) { 18585587Sobrien LEAF 18685587Sobrien info(p) = poscnt; 18785587Sobrien poscnt++; 18885587Sobrien break; 18985587Sobrien UNARY 19085587Sobrien penter(left(p)); 19185587Sobrien parent(left(p)) = p; 19285587Sobrien break; 19385587Sobrien case CAT: 19485587Sobrien case OR: 19585587Sobrien penter(left(p)); 19685587Sobrien penter(right(p)); 19785587Sobrien parent(left(p)) = p; 19885587Sobrien parent(right(p)) = p; 19985587Sobrien break; 20085587Sobrien default: /* can't happen */ 20185587Sobrien FATAL("can't happen: unknown type %d in penter", type(p)); 20285587Sobrien break; 20385587Sobrien } 20485587Sobrien} 20585587Sobrien 20685587Sobrienvoid freetr(Node *p) /* free parse tree */ 20785587Sobrien{ 20885587Sobrien switch (type(p)) { 20985587Sobrien LEAF 21085587Sobrien xfree(p); 21185587Sobrien break; 21285587Sobrien UNARY 21385587Sobrien freetr(left(p)); 21485587Sobrien xfree(p); 21585587Sobrien break; 21685587Sobrien case CAT: 21785587Sobrien case OR: 21885587Sobrien freetr(left(p)); 21985587Sobrien freetr(right(p)); 22085587Sobrien xfree(p); 22185587Sobrien break; 22285587Sobrien default: /* can't happen */ 22385587Sobrien FATAL("can't happen: unknown type %d in freetr", type(p)); 22485587Sobrien break; 22585587Sobrien } 22685587Sobrien} 22785587Sobrien 22885587Sobrien/* in the parsing of regular expressions, metacharacters like . have */ 22985587Sobrien/* to be seen literally; \056 is not a metacharacter. */ 23085587Sobrien 23185587Sobrienint hexstr(char **pp) /* find and eval hex string at pp, return new p */ 23285587Sobrien{ /* only pick up one 8-bit byte (2 chars) */ 23385587Sobrien uschar *p; 23485587Sobrien int n = 0; 23585587Sobrien int i; 23685587Sobrien 23785587Sobrien for (i = 0, p = (uschar *) *pp; i < 2 && isxdigit(*p); i++, p++) { 23885587Sobrien if (isdigit(*p)) 23985587Sobrien n = 16 * n + *p - '0'; 24085587Sobrien else if (*p >= 'a' && *p <= 'f') 24185587Sobrien n = 16 * n + *p - 'a' + 10; 24285587Sobrien else if (*p >= 'A' && *p <= 'F') 24385587Sobrien n = 16 * n + *p - 'A' + 10; 24485587Sobrien } 24585587Sobrien *pp = (char *) p; 24685587Sobrien return n; 24785587Sobrien} 24885587Sobrien 24985587Sobrien#define isoctdigit(c) ((c) >= '0' && (c) <= '7') /* multiple use of arg */ 25085587Sobrien 25185587Sobrienint quoted(char **pp) /* pick up next thing after a \\ */ 25285587Sobrien /* and increment *pp */ 25385587Sobrien{ 25485587Sobrien char *p = *pp; 25585587Sobrien int c; 25685587Sobrien 25785587Sobrien if ((c = *p++) == 't') 25885587Sobrien c = '\t'; 25985587Sobrien else if (c == 'n') 26085587Sobrien c = '\n'; 26185587Sobrien else if (c == 'f') 26285587Sobrien c = '\f'; 26385587Sobrien else if (c == 'r') 26485587Sobrien c = '\r'; 26585587Sobrien else if (c == 'b') 26685587Sobrien c = '\b'; 26785587Sobrien else if (c == '\\') 26885587Sobrien c = '\\'; 26985587Sobrien else if (c == 'x') { /* hexadecimal goo follows */ 27085587Sobrien c = hexstr(&p); /* this adds a null if number is invalid */ 27185587Sobrien } else if (isoctdigit(c)) { /* \d \dd \ddd */ 27285587Sobrien int n = c - '0'; 27385587Sobrien if (isoctdigit(*p)) { 27485587Sobrien n = 8 * n + *p++ - '0'; 27585587Sobrien if (isoctdigit(*p)) 27685587Sobrien n = 8 * n + *p++ - '0'; 27785587Sobrien } 27885587Sobrien c = n; 27985587Sobrien } /* else */ 28085587Sobrien /* c = c; */ 28185587Sobrien *pp = p; 28285587Sobrien return c; 28385587Sobrien} 28485587Sobrien 285107806Sobrienchar *cclenter(const char *argp) /* add a character class */ 286107806Sobrien{ 28785587Sobrien int i, c, c2; 28885587Sobrien uschar *p = (uschar *) argp; 28985587Sobrien uschar *op, *bp; 29085587Sobrien static uschar *buf = 0; 29185587Sobrien static int bufsz = 100; 29285587Sobrien 29385587Sobrien op = p; 29485587Sobrien if (buf == 0 && (buf = (uschar *) malloc(bufsz)) == NULL) 29585587Sobrien FATAL("out of space for character class [%.10s...] 1", p); 29685587Sobrien bp = buf; 29785587Sobrien for (i = 0; (c = *p++) != 0; ) { 29885587Sobrien if (c == '\\') { 29985587Sobrien c = quoted((char **) &p); 30085587Sobrien } else if (c == '-' && i > 0 && bp[-1] != 0) { 30185587Sobrien if (*p != 0) { 30285587Sobrien c = bp[-1]; 30385587Sobrien c2 = *p++; 30485587Sobrien if (c2 == '\\') 30585587Sobrien c2 = quoted((char **) &p); 306118194Sru if (c > c2) { /* empty; ignore */ 30785587Sobrien bp--; 30885587Sobrien i--; 30985587Sobrien continue; 31085587Sobrien } 311118194Sru while (c < c2) { 31285587Sobrien if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, 0)) 31385587Sobrien FATAL("out of space for character class [%.10s...] 2", p); 314118194Sru *bp++ = ++c; 31585587Sobrien i++; 31685587Sobrien } 31785587Sobrien continue; 31885587Sobrien } 31985587Sobrien } 32085587Sobrien if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, 0)) 32185587Sobrien FATAL("out of space for character class [%.10s...] 3", p); 32285587Sobrien *bp++ = c; 32385587Sobrien i++; 32485587Sobrien } 32585587Sobrien *bp = 0; 32685587Sobrien dprintf( ("cclenter: in = |%s|, out = |%s|\n", op, buf) ); 32785587Sobrien xfree(op); 32885587Sobrien return (char *) tostring((char *) buf); 32985587Sobrien} 33085587Sobrien 331107806Sobrienvoid overflo(const char *s) 33285587Sobrien{ 33385587Sobrien FATAL("regular expression too big: %.30s...", s); 33485587Sobrien} 33585587Sobrien 33685587Sobrienvoid cfoll(fa *f, Node *v) /* enter follow set of each leaf of vertex v into lfollow[leaf] */ 33785587Sobrien{ 33885587Sobrien int i; 33985587Sobrien int *p; 34085587Sobrien 34185587Sobrien switch (type(v)) { 34285587Sobrien LEAF 34385587Sobrien f->re[info(v)].ltype = type(v); 34485587Sobrien f->re[info(v)].lval.np = right(v); 34585587Sobrien while (f->accept >= maxsetvec) { /* guessing here! */ 34685587Sobrien maxsetvec *= 4; 34785587Sobrien setvec = (int *) realloc(setvec, maxsetvec * sizeof(int)); 34885587Sobrien tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(int)); 34985587Sobrien if (setvec == 0 || tmpset == 0) 35085587Sobrien overflo("out of space in cfoll()"); 35185587Sobrien } 35285587Sobrien for (i = 0; i <= f->accept; i++) 35385587Sobrien setvec[i] = 0; 35485587Sobrien setcnt = 0; 35585587Sobrien follow(v); /* computes setvec and setcnt */ 35685587Sobrien if ((p = (int *) calloc(1, (setcnt+1)*sizeof(int))) == NULL) 35785587Sobrien overflo("out of space building follow set"); 35885587Sobrien f->re[info(v)].lfollow = p; 35985587Sobrien *p = setcnt; 36085587Sobrien for (i = f->accept; i >= 0; i--) 36185587Sobrien if (setvec[i] == 1) 36285587Sobrien *++p = i; 36385587Sobrien break; 36485587Sobrien UNARY 36585587Sobrien cfoll(f,left(v)); 36685587Sobrien break; 36785587Sobrien case CAT: 36885587Sobrien case OR: 36985587Sobrien cfoll(f,left(v)); 37085587Sobrien cfoll(f,right(v)); 37185587Sobrien break; 37285587Sobrien default: /* can't happen */ 37385587Sobrien FATAL("can't happen: unknown type %d in cfoll", type(v)); 37485587Sobrien } 37585587Sobrien} 37685587Sobrien 37785587Sobrienint first(Node *p) /* collects initially active leaves of p into setvec */ 37885587Sobrien /* returns 1 if p matches empty string */ 37985587Sobrien{ 38085587Sobrien int b, lp; 38185587Sobrien 38285587Sobrien switch (type(p)) { 38385587Sobrien LEAF 38485587Sobrien lp = info(p); /* look for high-water mark of subscripts */ 38585587Sobrien while (setcnt >= maxsetvec || lp >= maxsetvec) { /* guessing here! */ 38685587Sobrien maxsetvec *= 4; 38785587Sobrien setvec = (int *) realloc(setvec, maxsetvec * sizeof(int)); 38885587Sobrien tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(int)); 38985587Sobrien if (setvec == 0 || tmpset == 0) 39085587Sobrien overflo("out of space in first()"); 39185587Sobrien } 39285587Sobrien if (setvec[lp] != 1) { 39385587Sobrien setvec[lp] = 1; 39485587Sobrien setcnt++; 39585587Sobrien } 39685587Sobrien if (type(p) == CCL && (*(char *) right(p)) == '\0') 39785587Sobrien return(0); /* empty CCL */ 39885587Sobrien else return(1); 39985587Sobrien case PLUS: 40085587Sobrien if (first(left(p)) == 0) return(0); 40185587Sobrien return(1); 40285587Sobrien case STAR: 40385587Sobrien case QUEST: 40485587Sobrien first(left(p)); 40585587Sobrien return(0); 40685587Sobrien case CAT: 40785587Sobrien if (first(left(p)) == 0 && first(right(p)) == 0) return(0); 40885587Sobrien return(1); 40985587Sobrien case OR: 41085587Sobrien b = first(right(p)); 41185587Sobrien if (first(left(p)) == 0 || b == 0) return(0); 41285587Sobrien return(1); 41385587Sobrien } 41485587Sobrien FATAL("can't happen: unknown type %d in first", type(p)); /* can't happen */ 41585587Sobrien return(-1); 41685587Sobrien} 41785587Sobrien 41885587Sobrienvoid follow(Node *v) /* collects leaves that can follow v into setvec */ 41985587Sobrien{ 42085587Sobrien Node *p; 42185587Sobrien 42285587Sobrien if (type(v) == FINAL) 42385587Sobrien return; 42485587Sobrien p = parent(v); 42585587Sobrien switch (type(p)) { 42685587Sobrien case STAR: 42785587Sobrien case PLUS: 42885587Sobrien first(v); 42985587Sobrien follow(p); 43085587Sobrien return; 43185587Sobrien 43285587Sobrien case OR: 43385587Sobrien case QUEST: 43485587Sobrien follow(p); 43585587Sobrien return; 43685587Sobrien 43785587Sobrien case CAT: 43885587Sobrien if (v == left(p)) { /* v is left child of p */ 43985587Sobrien if (first(right(p)) == 0) { 44085587Sobrien follow(p); 44185587Sobrien return; 44285587Sobrien } 44385587Sobrien } else /* v is right child */ 44485587Sobrien follow(p); 44585587Sobrien return; 44685587Sobrien } 44785587Sobrien} 44885587Sobrien 449107806Sobrienint member(int c, const char *sarg) /* is c in s? */ 45085587Sobrien{ 45185587Sobrien uschar *s = (uschar *) sarg; 45285587Sobrien 45385587Sobrien while (*s) 45485587Sobrien if (c == *s++) 45585587Sobrien return(1); 45685587Sobrien return(0); 45785587Sobrien} 45885587Sobrien 459107806Sobrienint match(fa *f, const char *p0) /* shortest match ? */ 46085587Sobrien{ 46185587Sobrien int s, ns; 46285587Sobrien uschar *p = (uschar *) p0; 46385587Sobrien 46485587Sobrien s = f->reset ? makeinit(f,0) : f->initstat; 46585587Sobrien if (f->out[s]) 46685587Sobrien return(1); 46785587Sobrien do { 46885587Sobrien if ((ns = f->gototab[s][*p]) != 0) 46985587Sobrien s = ns; 47085587Sobrien else 47185587Sobrien s = cgoto(f, s, *p); 47285587Sobrien if (f->out[s]) 47385587Sobrien return(1); 47485587Sobrien } while (*p++ != 0); 47585587Sobrien return(0); 47685587Sobrien} 47785587Sobrien 478107806Sobrienint pmatch(fa *f, const char *p0) /* longest match, for sub */ 47985587Sobrien{ 48085587Sobrien int s, ns; 48185587Sobrien uschar *p = (uschar *) p0; 48285587Sobrien uschar *q; 48385587Sobrien int i, k; 48485587Sobrien 485125601Sru /* s = f->reset ? makeinit(f,1) : f->initstat; */ 486125601Sru if (f->reset) { 487125601Sru f->initstat = s = makeinit(f,1); 488125601Sru } else { 489125601Sru s = f->initstat; 490125601Sru } 49185587Sobrien patbeg = (char *) p; 49285587Sobrien patlen = -1; 49385587Sobrien do { 49485587Sobrien q = p; 49585587Sobrien do { 49685587Sobrien if (f->out[s]) /* final state */ 49785587Sobrien patlen = q-p; 49885587Sobrien if ((ns = f->gototab[s][*q]) != 0) 49985587Sobrien s = ns; 50085587Sobrien else 50185587Sobrien s = cgoto(f, s, *q); 50285587Sobrien if (s == 1) { /* no transition */ 50385587Sobrien if (patlen >= 0) { 50485587Sobrien patbeg = (char *) p; 50585587Sobrien return(1); 50685587Sobrien } 50785587Sobrien else 50885587Sobrien goto nextin; /* no match */ 50985587Sobrien } 51085587Sobrien } while (*q++ != 0); 51185587Sobrien if (f->out[s]) 51285587Sobrien patlen = q-p-1; /* don't count $ */ 51385587Sobrien if (patlen >= 0) { 51485587Sobrien patbeg = (char *) p; 51585587Sobrien return(1); 51685587Sobrien } 51785587Sobrien nextin: 51885587Sobrien s = 2; 51985587Sobrien if (f->reset) { 52085587Sobrien for (i = 2; i <= f->curstat; i++) 52185587Sobrien xfree(f->posns[i]); 52285587Sobrien k = *f->posns[0]; 52385587Sobrien if ((f->posns[2] = (int *) calloc(1, (k+1)*sizeof(int))) == NULL) 52485587Sobrien overflo("out of space in pmatch"); 52585587Sobrien for (i = 0; i <= k; i++) 52685587Sobrien (f->posns[2])[i] = (f->posns[0])[i]; 52785587Sobrien f->initstat = f->curstat = 2; 52885587Sobrien f->out[2] = f->out[0]; 52985587Sobrien for (i = 0; i < NCHARS; i++) 53085587Sobrien f->gototab[2][i] = 0; 53185587Sobrien } 53285587Sobrien } while (*p++ != 0); 53385587Sobrien return (0); 53485587Sobrien} 53585587Sobrien 536107806Sobrienint nematch(fa *f, const char *p0) /* non-empty match, for sub */ 53785587Sobrien{ 53885587Sobrien int s, ns; 53985587Sobrien uschar *p = (uschar *) p0; 54085587Sobrien uschar *q; 54185587Sobrien int i, k; 54285587Sobrien 543125601Sru /* s = f->reset ? makeinit(f,1) : f->initstat; */ 544125601Sru if (f->reset) { 545125601Sru f->initstat = s = makeinit(f,1); 546125601Sru } else { 547125601Sru s = f->initstat; 548125601Sru } 54985587Sobrien patlen = -1; 55085587Sobrien while (*p) { 55185587Sobrien q = p; 55285587Sobrien do { 55385587Sobrien if (f->out[s]) /* final state */ 55485587Sobrien patlen = q-p; 55585587Sobrien if ((ns = f->gototab[s][*q]) != 0) 55685587Sobrien s = ns; 55785587Sobrien else 55885587Sobrien s = cgoto(f, s, *q); 55985587Sobrien if (s == 1) { /* no transition */ 56085587Sobrien if (patlen > 0) { 56185587Sobrien patbeg = (char *) p; 56285587Sobrien return(1); 56385587Sobrien } else 56485587Sobrien goto nnextin; /* no nonempty match */ 56585587Sobrien } 56685587Sobrien } while (*q++ != 0); 56785587Sobrien if (f->out[s]) 56885587Sobrien patlen = q-p-1; /* don't count $ */ 56985587Sobrien if (patlen > 0 ) { 57085587Sobrien patbeg = (char *) p; 57185587Sobrien return(1); 57285587Sobrien } 57385587Sobrien nnextin: 57485587Sobrien s = 2; 57585587Sobrien if (f->reset) { 57685587Sobrien for (i = 2; i <= f->curstat; i++) 57785587Sobrien xfree(f->posns[i]); 57885587Sobrien k = *f->posns[0]; 57985587Sobrien if ((f->posns[2] = (int *) calloc(1, (k+1)*sizeof(int))) == NULL) 58085587Sobrien overflo("out of state space"); 58185587Sobrien for (i = 0; i <= k; i++) 58285587Sobrien (f->posns[2])[i] = (f->posns[0])[i]; 58385587Sobrien f->initstat = f->curstat = 2; 58485587Sobrien f->out[2] = f->out[0]; 58585587Sobrien for (i = 0; i < NCHARS; i++) 58685587Sobrien f->gototab[2][i] = 0; 58785587Sobrien } 58885587Sobrien p++; 58985587Sobrien } 59085587Sobrien return (0); 59185587Sobrien} 59285587Sobrien 593107806SobrienNode *reparse(const char *p) /* parses regular expression pointed to by p */ 59485587Sobrien{ /* uses relex() to scan regular expression */ 59585587Sobrien Node *np; 59685587Sobrien 59785587Sobrien dprintf( ("reparse <%s>\n", p) ); 59885587Sobrien lastre = prestr = (uschar *) p; /* prestr points to string to be parsed */ 59985587Sobrien rtok = relex(); 600107806Sobrien /* GNU compatibility: an empty regexp matches anything */ 60185587Sobrien if (rtok == '\0') 602107806Sobrien /* FATAL("empty regular expression"); previous */ 603107806Sobrien return(op2(ALL, NIL, NIL)); 60485587Sobrien np = regexp(); 60585587Sobrien if (rtok != '\0') 60685587Sobrien FATAL("syntax error in regular expression %s at %s", lastre, prestr); 60785587Sobrien return(np); 60885587Sobrien} 60985587Sobrien 61085587SobrienNode *regexp(void) /* top-level parse of reg expr */ 61185587Sobrien{ 61285587Sobrien return (alt(concat(primary()))); 61385587Sobrien} 61485587Sobrien 61585587SobrienNode *primary(void) 61685587Sobrien{ 61785587Sobrien Node *np; 61885587Sobrien 61985587Sobrien switch (rtok) { 62085587Sobrien case CHAR: 62185587Sobrien np = op2(CHAR, NIL, itonp(rlxval)); 62285587Sobrien rtok = relex(); 62385587Sobrien return (unary(np)); 62485587Sobrien case ALL: 62585587Sobrien rtok = relex(); 62685587Sobrien return (unary(op2(ALL, NIL, NIL))); 62785587Sobrien case DOT: 62885587Sobrien rtok = relex(); 62985587Sobrien return (unary(op2(DOT, NIL, NIL))); 63085587Sobrien case CCL: 63185587Sobrien np = op2(CCL, NIL, (Node*) cclenter((char *) rlxstr)); 63285587Sobrien rtok = relex(); 63385587Sobrien return (unary(np)); 63485587Sobrien case NCCL: 63585587Sobrien np = op2(NCCL, NIL, (Node *) cclenter((char *) rlxstr)); 63685587Sobrien rtok = relex(); 63785587Sobrien return (unary(np)); 63885587Sobrien case '^': 63985587Sobrien rtok = relex(); 64085587Sobrien return (unary(op2(CHAR, NIL, itonp(HAT)))); 64185587Sobrien case '$': 64285587Sobrien rtok = relex(); 64385587Sobrien return (unary(op2(CHAR, NIL, NIL))); 64485587Sobrien case '(': 64585587Sobrien rtok = relex(); 64685587Sobrien if (rtok == ')') { /* special pleading for () */ 64785587Sobrien rtok = relex(); 64885587Sobrien return unary(op2(CCL, NIL, (Node *) tostring(""))); 64985587Sobrien } 65085587Sobrien np = regexp(); 65185587Sobrien if (rtok == ')') { 65285587Sobrien rtok = relex(); 65385587Sobrien return (unary(np)); 65485587Sobrien } 65585587Sobrien else 65685587Sobrien FATAL("syntax error in regular expression %s at %s", lastre, prestr); 65785587Sobrien default: 65885587Sobrien FATAL("illegal primary in regular expression %s at %s", lastre, prestr); 65985587Sobrien } 66085587Sobrien return 0; /*NOTREACHED*/ 66185587Sobrien} 66285587Sobrien 66385587SobrienNode *concat(Node *np) 66485587Sobrien{ 66585587Sobrien switch (rtok) { 66685587Sobrien case CHAR: case DOT: case ALL: case CCL: case NCCL: case '$': case '(': 66785587Sobrien return (concat(op2(CAT, np, primary()))); 66885587Sobrien } 66985587Sobrien return (np); 67085587Sobrien} 67185587Sobrien 67285587SobrienNode *alt(Node *np) 67385587Sobrien{ 67485587Sobrien if (rtok == OR) { 67585587Sobrien rtok = relex(); 67685587Sobrien return (alt(op2(OR, np, concat(primary())))); 67785587Sobrien } 67885587Sobrien return (np); 67985587Sobrien} 68085587Sobrien 68185587SobrienNode *unary(Node *np) 68285587Sobrien{ 68385587Sobrien switch (rtok) { 68485587Sobrien case STAR: 68585587Sobrien rtok = relex(); 68685587Sobrien return (unary(op2(STAR, np, NIL))); 68785587Sobrien case PLUS: 68885587Sobrien rtok = relex(); 68985587Sobrien return (unary(op2(PLUS, np, NIL))); 69085587Sobrien case QUEST: 69185587Sobrien rtok = relex(); 69285587Sobrien return (unary(op2(QUEST, np, NIL))); 69385587Sobrien default: 69485587Sobrien return (np); 69585587Sobrien } 69685587Sobrien} 69785587Sobrien 69890902Sdes/* 69990902Sdes * Character class definitions conformant to the POSIX locale as 70090902Sdes * defined in IEEE P1003.1 draft 7 of June 2001, assuming the source 70190902Sdes * and operating character sets are both ASCII (ISO646) or supersets 70290902Sdes * thereof. 70390902Sdes * 70490902Sdes * Note that to avoid overflowing the temporary buffer used in 70590902Sdes * relex(), the expanded character class (prior to range expansion) 70690902Sdes * must be less than twice the size of their full name. 70790902Sdes */ 708112336Sobrien 709112336Sobrien/* Because isblank doesn't show up in any of the header files on any 710112336Sobrien * system i use, it's defined here. if some other locale has a richer 711112336Sobrien * definition of "blank", define HAS_ISBLANK and provide your own 712112336Sobrien * version. 713118194Sru * the parentheses here are an attempt to find a path through the maze 714118194Sru * of macro definition and/or function and/or version provided. thanks 715118194Sru * to nelson beebe for the suggestion; let's see if it works everywhere. 716112336Sobrien */ 717112336Sobrien 718112336Sobrien#ifndef HAS_ISBLANK 719112336Sobrien 720118194Sruint (isblank)(int c) 721112336Sobrien{ 722112336Sobrien return c==' ' || c=='\t'; 723112336Sobrien} 724112336Sobrien 725112336Sobrien#endif 726112336Sobrien 72790902Sdesstruct charclass { 72890902Sdes const char *cc_name; 72990902Sdes int cc_namelen; 730112336Sobrien int (*cc_func)(int); 73190902Sdes} charclasses[] = { 732112336Sobrien { "alnum", 5, isalnum }, 733112336Sobrien { "alpha", 5, isalpha }, 734112336Sobrien { "blank", 5, isblank }, 735112336Sobrien { "cntrl", 5, iscntrl }, 736112336Sobrien { "digit", 5, isdigit }, 737112336Sobrien { "graph", 5, isgraph }, 738112336Sobrien { "lower", 5, islower }, 739112336Sobrien { "print", 5, isprint }, 740112336Sobrien { "punct", 5, ispunct }, 741112336Sobrien { "space", 5, isspace }, 742112336Sobrien { "upper", 5, isupper }, 743112336Sobrien { "xdigit", 6, isxdigit }, 74490902Sdes { NULL, 0, NULL }, 74590902Sdes}; 74690902Sdes 74790902Sdes 74885587Sobrienint relex(void) /* lexical analyzer for reparse */ 74985587Sobrien{ 75085587Sobrien int c, n; 75185587Sobrien int cflag; 75285587Sobrien static uschar *buf = 0; 75385587Sobrien static int bufsz = 100; 75485587Sobrien uschar *bp; 75590902Sdes struct charclass *cc; 756112336Sobrien int i; 75785587Sobrien 75885587Sobrien switch (c = *prestr++) { 75985587Sobrien case '|': return OR; 76085587Sobrien case '*': return STAR; 76185587Sobrien case '+': return PLUS; 76285587Sobrien case '?': return QUEST; 76385587Sobrien case '.': return DOT; 76485587Sobrien case '\0': prestr--; return '\0'; 76585587Sobrien case '^': 76685587Sobrien case '$': 76785587Sobrien case '(': 76885587Sobrien case ')': 76985587Sobrien return c; 77085587Sobrien case '\\': 77185587Sobrien rlxval = quoted((char **) &prestr); 77285587Sobrien return CHAR; 77385587Sobrien default: 77485587Sobrien rlxval = c; 77585587Sobrien return CHAR; 77685587Sobrien case '[': 77785587Sobrien if (buf == 0 && (buf = (uschar *) malloc(bufsz)) == NULL) 77885587Sobrien FATAL("out of space in reg expr %.10s..", lastre); 77985587Sobrien bp = buf; 78085587Sobrien if (*prestr == '^') { 78185587Sobrien cflag = 1; 78285587Sobrien prestr++; 78385587Sobrien } 78485587Sobrien else 78585587Sobrien cflag = 0; 78690902Sdes n = 2 * strlen((const char *) prestr)+1; 78785587Sobrien if (!adjbuf((char **) &buf, &bufsz, n, n, (char **) &bp, 0)) 78885587Sobrien FATAL("out of space for reg expr %.10s...", lastre); 78985587Sobrien for (; ; ) { 79085587Sobrien if ((c = *prestr++) == '\\') { 79185587Sobrien *bp++ = '\\'; 79285587Sobrien if ((c = *prestr++) == '\0') 79385587Sobrien FATAL("nonterminated character class %.20s...", lastre); 79485587Sobrien *bp++ = c; 79585587Sobrien /* } else if (c == '\n') { */ 79685587Sobrien /* FATAL("newline in character class %.20s...", lastre); */ 79790902Sdes } else if (c == '[' && *prestr == ':') { 79890902Sdes /* POSIX char class names, Dag-Erling Smorgrav, des@ofug.org */ 79990902Sdes for (cc = charclasses; cc->cc_name; cc++) 80090902Sdes if (strncmp((const char *) prestr + 1, (const char *) cc->cc_name, cc->cc_namelen) == 0) 80190902Sdes break; 80290902Sdes if (cc->cc_name != NULL && prestr[1 + cc->cc_namelen] == ':' && 80390902Sdes prestr[2 + cc->cc_namelen] == ']') { 80490902Sdes prestr += cc->cc_namelen + 3; 805112336Sobrien for (i = 0; i < NCHARS; i++) { 806112336Sobrien if (!adjbuf((char **) &buf, &bufsz, bp-buf+1, 100, (char **) &bp, 0)) 807112336Sobrien FATAL("out of space for reg expr %.10s...", lastre); 808112336Sobrien if (cc->cc_func(i)) { 809112336Sobrien *bp++ = i; 810112336Sobrien n++; 811112336Sobrien } 812112336Sobrien } 81390902Sdes } else 81490902Sdes *bp++ = c; 81585587Sobrien } else if (c == '\0') { 81685587Sobrien FATAL("nonterminated character class %.20s", lastre); 81785587Sobrien } else if (bp == buf) { /* 1st char is special */ 81885587Sobrien *bp++ = c; 81985587Sobrien } else if (c == ']') { 82085587Sobrien *bp++ = 0; 82185587Sobrien rlxstr = (uschar *) tostring((char *) buf); 82285587Sobrien if (cflag == 0) 82385587Sobrien return CCL; 82485587Sobrien else 82585587Sobrien return NCCL; 82685587Sobrien } else 82785587Sobrien *bp++ = c; 82885587Sobrien } 82985587Sobrien } 83085587Sobrien} 83185587Sobrien 83285587Sobrienint cgoto(fa *f, int s, int c) 83385587Sobrien{ 83485587Sobrien int i, j, k; 83585587Sobrien int *p, *q; 83685587Sobrien 83785587Sobrien while (f->accept >= maxsetvec) { /* guessing here! */ 83885587Sobrien maxsetvec *= 4; 83985587Sobrien setvec = (int *) realloc(setvec, maxsetvec * sizeof(int)); 84085587Sobrien tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(int)); 84185587Sobrien if (setvec == 0 || tmpset == 0) 84285587Sobrien overflo("out of space in cgoto()"); 84385587Sobrien } 84485587Sobrien for (i = 0; i <= f->accept; i++) 84585587Sobrien setvec[i] = 0; 84685587Sobrien setcnt = 0; 84785587Sobrien /* compute positions of gototab[s,c] into setvec */ 84885587Sobrien p = f->posns[s]; 84985587Sobrien for (i = 1; i <= *p; i++) { 85085587Sobrien if ((k = f->re[p[i]].ltype) != FINAL) { 85185587Sobrien if ((k == CHAR && c == ptoi(f->re[p[i]].lval.np)) 85285587Sobrien || (k == DOT && c != 0 && c != HAT) 85385587Sobrien || (k == ALL && c != 0) 85485587Sobrien || (k == CCL && member(c, (char *) f->re[p[i]].lval.up)) 85585587Sobrien || (k == NCCL && !member(c, (char *) f->re[p[i]].lval.up) && c != 0 && c != HAT)) { 85685587Sobrien q = f->re[p[i]].lfollow; 85785587Sobrien for (j = 1; j <= *q; j++) { 85885587Sobrien if (q[j] >= maxsetvec) { 85985587Sobrien maxsetvec *= 4; 86085587Sobrien setvec = (int *) realloc(setvec, maxsetvec * sizeof(int)); 86185587Sobrien tmpset = (int *) realloc(setvec, maxsetvec * sizeof(int)); 86285587Sobrien if (setvec == 0 || tmpset == 0) 86385587Sobrien overflo("cgoto overflow"); 86485587Sobrien } 86585587Sobrien if (setvec[q[j]] == 0) { 86685587Sobrien setcnt++; 86785587Sobrien setvec[q[j]] = 1; 86885587Sobrien } 86985587Sobrien } 87085587Sobrien } 87185587Sobrien } 87285587Sobrien } 87385587Sobrien /* determine if setvec is a previous state */ 87485587Sobrien tmpset[0] = setcnt; 87585587Sobrien j = 1; 87685587Sobrien for (i = f->accept; i >= 0; i--) 87785587Sobrien if (setvec[i]) { 87885587Sobrien tmpset[j++] = i; 87985587Sobrien } 88085587Sobrien /* tmpset == previous state? */ 88185587Sobrien for (i = 1; i <= f->curstat; i++) { 88285587Sobrien p = f->posns[i]; 88385587Sobrien if ((k = tmpset[0]) != p[0]) 88485587Sobrien goto different; 88585587Sobrien for (j = 1; j <= k; j++) 88685587Sobrien if (tmpset[j] != p[j]) 88785587Sobrien goto different; 88885587Sobrien /* setvec is state i */ 88985587Sobrien f->gototab[s][c] = i; 89085587Sobrien return i; 89185587Sobrien different:; 89285587Sobrien } 89385587Sobrien 89485587Sobrien /* add tmpset to current set of states */ 89585587Sobrien if (f->curstat >= NSTATES-1) { 89685587Sobrien f->curstat = 2; 89785587Sobrien f->reset = 1; 89885587Sobrien for (i = 2; i < NSTATES; i++) 89985587Sobrien xfree(f->posns[i]); 90085587Sobrien } else 90185587Sobrien ++(f->curstat); 90285587Sobrien for (i = 0; i < NCHARS; i++) 90385587Sobrien f->gototab[f->curstat][i] = 0; 90485587Sobrien xfree(f->posns[f->curstat]); 90585587Sobrien if ((p = (int *) calloc(1, (setcnt+1)*sizeof(int))) == NULL) 90685587Sobrien overflo("out of space in cgoto"); 90785587Sobrien 90885587Sobrien f->posns[f->curstat] = p; 90985587Sobrien f->gototab[s][c] = f->curstat; 91085587Sobrien for (i = 0; i <= setcnt; i++) 91185587Sobrien p[i] = tmpset[i]; 91285587Sobrien if (setvec[f->accept]) 91385587Sobrien f->out[f->curstat] = 1; 91485587Sobrien else 91585587Sobrien f->out[f->curstat] = 0; 91685587Sobrien return f->curstat; 91785587Sobrien} 91885587Sobrien 91985587Sobrien 92085587Sobrienvoid freefa(fa *f) /* free a finite automaton */ 92185587Sobrien{ 92285587Sobrien int i; 92385587Sobrien 92485587Sobrien if (f == NULL) 92585587Sobrien return; 92685587Sobrien for (i = 0; i <= f->curstat; i++) 92785587Sobrien xfree(f->posns[i]); 92885587Sobrien for (i = 0; i <= f->accept; i++) { 92985587Sobrien xfree(f->re[i].lfollow); 93085587Sobrien if (f->re[i].ltype == CCL || f->re[i].ltype == NCCL) 93185587Sobrien xfree((f->re[i].lval.np)); 93285587Sobrien } 93385587Sobrien xfree(f->restr); 93485587Sobrien xfree(f); 93585587Sobrien} 936