From: Michael D. Lowis Date: Thu, 20 Dec 2018 15:44:01 +0000 (-0500) Subject: renamed remaining globals in regcomp.c to confirm understanding of the code X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=88bf7778747de8486ccf9d89fcccb911a9836f58;p=proto%2Flibregexp.git renamed remaining globals in regcomp.c to confirm understanding of the code --- diff --git a/.gitignore b/.gitignore index 93dc097..d6fe385 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ tags *.o *.a test +tests diff --git a/regcomp.c b/regcomp.c index 426a146..b7340e4 100644 --- a/regcomp.c +++ b/regcomp.c @@ -30,9 +30,9 @@ static int* OperatorPtr; /* Pointer to the top of the operator stack static int SubExprStack[NSTACK]; /* parallel to OperatorStack */ static int* SubExprPtr; /* Pointer to the top of the sub-expression stack */ -static Reclass* classp; -static Reinst* freep; -static Reclass* yyclassp; /* last lex'd class */ +static Reinst* FreeInstrPtr; /* Pointer to first free instruction object */ +static Reclass* ClassListPtr; /* Pointer to list of classes */ +static Reclass* CurrClassPtr; /* last lex'd class */ /* predeclared crap */ static Reinst* OpCode(int t); @@ -52,13 +52,15 @@ static Reprog* Compile(char *s, int literal, int dot_type); /******************************************************************************/ static Reinst* OpCode(int t) { - freep->type = t; - freep->l.left = 0; - freep->r.right = 0; - return freep++; + memset(FreeInstrPtr, 0, sizeof(*FreeInstrPtr)); + FreeInstrPtr->type = t; + return FreeInstrPtr++; } -/******************************************************************************/ +static void CompileError(char *s) { + regerror(s); + longjmp(LandingPad, 1); +} static int NextRune(int* p_rune) { if (!*SrcExpr) { @@ -78,10 +80,7 @@ static int GetToken(int literal, int dot_type) { if (literal || quoted) { if (CurrRune == 0) return END; - return RUNE; - } - - switch (CurrRune) { + } else switch (CurrRune) { case 0: return END; case '*': return STAR; case '?': return QUEST; @@ -97,13 +96,6 @@ static int GetToken(int literal, int dot_type) { return RUNE; } -/******************************************************************************/ - -static void CompileError(char *s) { - regerror(s); - longjmp(LandingPad, 1); -} - static void PushOperand(Reinst *f, Reinst *l) { if (OperandPtr >= &OperandStack[NSTACK]) CompileError("operand stack overflow"); @@ -144,7 +136,7 @@ static int BuildClass(void) { if (NClass >= NCLASSES) CompileError("too many character classes; increase Reprog.class size"); int type = CCLASS; - yyclassp = &(classp[NClass++]); + CurrClassPtr = &(ClassListPtr[NClass++]); /* look ahead for negation */ /* SPECIAL CASE!!! negated classes don't match \n */ @@ -183,7 +175,7 @@ static int BuildClass(void) { quoted = NextRune(&rune); } if (ep >= &r[NCLASSSPANS-1]) { - CompileError("char class too large; increase Reclass.spans size"); + CompileError("char class too large; increase NCLASSSPANS size"); return 0; } @@ -201,14 +193,14 @@ static int BuildClass(void) { } /* merge spans */ - np = yyclassp->spans; + np = CurrClassPtr->spans; p = r; - if(r == ep) - yyclassp->end = np; - else { + if(r == ep) { + CurrClassPtr->end = np; + } else { np[0] = *p++; np[1] = *p++; - for(; p < ep; p += 2) + for(; p < ep; p += 2) { /* overlapping or adjacent ranges? */ if(p[0] <= np[1] + 1){ if(p[1] >= np[1]) @@ -218,7 +210,8 @@ static int BuildClass(void) { np[0] = p[0]; np[1] = p[1]; } - yyclassp->end = np+2; + } + CurrClassPtr->end = np+2; } return type; @@ -228,15 +221,13 @@ static int BuildClass(void) { /******************************************************************************/ static void Operand(int t) { - Reinst *i; - if (LastWasOperand) - Operator(CAT); /* catenate is implicit */ - i = OpCode(t); + Operator(CAT); /* catenate is implicit */ + Reinst* i = OpCode(t); if (t == CCLASS || t == NCCLASS) - i->r.cp = yyclassp; - if (t == RUNE) + i->r.cp = CurrClassPtr; + else if (t == RUNE) i->r.r = CurrRune; PushOperand(i, i); @@ -244,82 +235,83 @@ static void Operand(int t) { } static void Operator(int t) { - if(t==RPAREN && --NParens<0) + if (t == RPAREN && --NParens < 0) CompileError("unmatched right paren"); - if(t==LPAREN){ - if(++CurrSubExpr >= NSUBEXP) + if (t==LPAREN) { + if (++CurrSubExpr >= NSUBEXP) CompileError("too many subexpressions"); NParens++; - if(LastWasOperand) + if (LastWasOperand) Operator(CAT); - }else + } else { EvalUntil(t); - if(t != RPAREN) + } + if (t != RPAREN) PushOperator(t); LastWasOperand = false; - if(t==STAR || t==QUEST || t==PLUS || t==RPAREN) - LastWasOperand = true; /* these look like operands */ + if (t == STAR || t == QUEST || t == PLUS || t == RPAREN) + LastWasOperand = true; /* these look like operands */ } static void EvalUntil(int pri) { Node *op1, *op2; Reinst *inst1, *inst2; - while(pri==RPAREN || OperatorPtr[-1]>=pri){ - switch(PopOperator()){ - default: - CompileError("unknown operator in evaluntil"); - break; - case LPAREN: /* must have been RPAREN */ - op1 = PopOperand(); - inst2 = OpCode(RPAREN); - inst2->r.subid = *SubExprPtr; - op1->last->l.next = inst2; - inst1 = OpCode(LPAREN); - inst1->r.subid = *SubExprPtr; - inst1->l.next = op1->first; - PushOperand(inst1, inst2); - return; - case OR: - op2 = PopOperand(); - op1 = PopOperand(); - inst2 = OpCode(NOP); - op2->last->l.next = inst2; - op1->last->l.next = inst2; - inst1 = OpCode(OR); - inst1->r.right = op1->first; - inst1->l.left = op2->first; - PushOperand(inst1, inst2); - break; - case CAT: - op2 = PopOperand(); - op1 = PopOperand(); - op1->last->l.next = op2->first; - PushOperand(op1->first, op2->last); - break; - case STAR: - op2 = PopOperand(); - inst1 = OpCode(OR); - op2->last->l.next = inst1; - inst1->r.right = op2->first; - PushOperand(inst1, inst1); - break; - case PLUS: - op2 = PopOperand(); - inst1 = OpCode(OR); - op2->last->l.next = inst1; - inst1->r.right = op2->first; - PushOperand(op2->first, inst1); - break; - case QUEST: - op2 = PopOperand(); - inst1 = OpCode(OR); - inst2 = OpCode(NOP); - inst1->l.left = inst2; - inst1->r.right = op2->first; - op2->last->l.next = inst2; - PushOperand(inst1, inst2); - break; + while (pri == RPAREN || OperatorPtr[-1] >= pri) { + switch (PopOperator()) { + default: + CompileError("unknown operator in evaluntil"); + break; + case LPAREN: /* must have been RPAREN */ + op1 = PopOperand(); + inst2 = OpCode(RPAREN); + inst2->r.subid = *SubExprPtr; + op1->last->l.next = inst2; + inst1 = OpCode(LPAREN); + inst1->r.subid = *SubExprPtr; + inst1->l.next = op1->first; + PushOperand(inst1, inst2); + return; + case OR: + op2 = PopOperand(); + op1 = PopOperand(); + inst2 = OpCode(NOP); + op2->last->l.next = inst2; + op1->last->l.next = inst2; + inst1 = OpCode(OR); + inst1->r.right = op1->first; + inst1->l.left = op2->first; + PushOperand(inst1, inst2); + break; + case CAT: + op2 = PopOperand(); + op1 = PopOperand(); + op1->last->l.next = op2->first; + PushOperand(op1->first, op2->last); + break; + case STAR: + op2 = PopOperand(); + inst1 = OpCode(OR); + op2->last->l.next = inst1; + inst1->r.right = op2->first; + PushOperand(inst1, inst1); + break; + case PLUS: + op2 = PopOperand(); + inst1 = OpCode(OR); + op2->last->l.next = inst1; + inst1->r.right = op2->first; + PushOperand(op2->first, inst1); + break; + case QUEST: + op2 = PopOperand(); + inst1 = OpCode(OR); + inst2 = OpCode(NOP); + inst1->l.left = inst2; + inst1->r.right = op2->first; + op2->last->l.next = inst2; + PushOperand(inst1, inst2); + break; } } } @@ -332,7 +324,7 @@ static Reprog* Optimize(Reprog *pp) { int diff; /* get rid of NOOP chains */ - for(inst = pp->firstinst; inst->type != END; inst++){ + for (inst = pp->firstinst; inst->type != END; inst++) { target = inst->l.next; while(target->type == NOP) target = target->l.next; @@ -344,26 +336,26 @@ static Reprog* Optimize(Reprog *pp) { * necessary. Reallocate to the actual space used * and then relocate the code. */ - size = sizeof(Reprog) + (freep - pp->firstinst)*sizeof(Reinst); + size = sizeof(Reprog) + (FreeInstrPtr - pp->firstinst)*sizeof(Reinst); npp = realloc(pp, size); if(npp==0 || npp==pp) return pp; diff = (char *)npp - (char *)pp; - freep = (Reinst *)((char *)freep + diff); - for(inst=npp->firstinst; insttype){ - case OR: - case STAR: - case PLUS: - case QUEST: - inst->r.right = (void*)((char*)inst->r.right + diff); - break; - case CCLASS: - case NCCLASS: - inst->r.right = (void*)((char*)inst->r.right + diff); - cl = inst->r.cp; - cl->end = (void*)((char*)cl->end + diff); - break; + FreeInstrPtr = (Reinst *)((char *)FreeInstrPtr + diff); + for (inst = npp->firstinst; inst < FreeInstrPtr; inst++) { + switch (inst->type) { + case OR: + case STAR: + case PLUS: + case QUEST: + inst->r.right = (void*)((char*)inst->r.right + diff); + break; + case CCLASS: + case NCCLASS: + inst->r.right = (void*)((char*)inst->r.right + diff); + cl = inst->r.cp; + cl->end = (void*)((char*)cl->end + diff); + break; } inst->l.left = (void*)((char*)inst->l.left + diff); } @@ -377,13 +369,13 @@ static Reprog* Compile(char *s, int literal, int dot_type) { int token; /* get memory for the program */ - Reprog* volatile pp = malloc(sizeof(Reprog) + 6*sizeof(Reinst)*strlen(s)); - if (pp == 0) { + Reprog* volatile pp = malloc(sizeof(Reprog) + (6 * sizeof(Reinst) * strlen(s))); + if (pp == NULL) { regerror("out of memory"); - return 0; + return NULL; } - freep = pp->firstinst; - classp = pp->class; + FreeInstrPtr = pp->firstinst; + ClassListPtr = pp->class; /* setup landing pad for fatal errors */ if (setjmp(LandingPad)) @@ -400,7 +392,7 @@ static Reprog* Compile(char *s, int literal, int dot_type) { CurrSubExpr = 0; /* Start with a low priority operator to prime parser */ - PushOperator(START-1); + PushOperator(RUNE); while ((token = GetToken(literal, dot_type)) != END){ if ((token & 0300) == OPERATOR) Operator(token);