summaryrefslogtreecommitdiffstats
path: root/grammar/rainerscript.c
diff options
context:
space:
mode:
Diffstat (limited to 'grammar/rainerscript.c')
-rw-r--r--grammar/rainerscript.c79
1 files changed, 71 insertions, 8 deletions
diff --git a/grammar/rainerscript.c b/grammar/rainerscript.c
index 199aaa97..90bbb335 100644
--- a/grammar/rainerscript.c
+++ b/grammar/rainerscript.c
@@ -45,11 +45,13 @@
#include "srUtils.h"
#include "regexp.h"
#include "obj.h"
+#include "modules.h"
DEFobjCurrIf(obj)
DEFobjCurrIf(regexp)
void cnfexprOptimize(struct cnfexpr *expr);
+static void cnfstmtOptimizePRIFilt(struct cnfstmt *stmt);
char*
getFIOPName(unsigned iFIOP)
@@ -1628,7 +1630,7 @@ cnfstmtPrint(struct cnfstmt *root, int indent)
doIndent(indent); dbgprintf("STOP\n");
break;
case S_ACT:
- doIndent(indent); dbgprintf("ACTION %p (%s)\n", stmt->d.act, stmt->printable);
+ doIndent(indent); dbgprintf("ACTION %p [%s]\n", stmt->d.act, stmt->printable);
break;
case S_IF:
doIndent(indent); dbgprintf("IF\n");
@@ -1845,6 +1847,7 @@ struct cnfstmt *
cnfstmtNewAct(struct nvlst *lst)
{
struct cnfstmt* cnfstmt;
+ char namebuf[256];
if((cnfstmt = cnfstmtNew(S_ACT)) == NULL)
goto done;
if(actionNewInst(lst, &cnfstmt->d.act) != RS_RET_OK) {
@@ -1854,7 +1857,10 @@ cnfstmtNewAct(struct nvlst *lst)
cnfstmt->nodetype = S_NOP; /* disable action! */
goto done;
}
- cnfstmt->printable = (uchar*)strdup("action()");
+ snprintf(namebuf, sizeof(namebuf)-1, "action(type=\"%s\" ...)",
+ modGetName(cnfstmt->d.act->pMod));
+ namebuf[255] = '\0'; /* be on safe side */
+ cnfstmt->printable = (uchar*)strdup(namebuf);
done: return cnfstmt;
}
@@ -2071,7 +2077,7 @@ cnfstmtOptimizeIf(struct cnfstmt *stmt)
if(stmt->d.s_if.expr->nodetype == 'F') {
func = (struct cnffunc*)expr;
if(func->fID == CNFFUNC_PRIFILT) {
- DBGPRINTF("optimize IF to PRIFILT\n");
+ DBGPRINTF("optimizer: change IF to PRIFILT\n");
t_then = stmt->d.s_if.t_then;
t_else = stmt->d.s_if.t_else;
stmt->nodetype = S_PRIFILT;
@@ -2083,10 +2089,66 @@ cnfstmtOptimizeIf(struct cnfstmt *stmt)
stmt->printable = (uchar*)
es_str2cstr(((struct cnfstringval*)func->expr[0])->estr, NULL);
cnfexprDestruct(expr);
+ cnfstmtOptimizePRIFilt(stmt);
}
}
}
+static inline void
+cnfstmtOptimizeAct(struct cnfstmt *stmt)
+{
+ action_t *pAct;
+
+ pAct = stmt->d.act;
+ if(!strcmp((char*)modGetName(stmt->d.act->pMod), "builtin:omdiscard")) {
+ DBGPRINTF("optimizer: replacing omdiscard by STOP\n");
+ actionDestruct(stmt->d.act);
+ stmt->nodetype = S_STOP;
+ }
+}
+
+static void
+cnfstmtOptimizePRIFilt(struct cnfstmt *stmt)
+{
+ int i;
+ int isAlways = 1;
+ struct cnfstmt *subroot, *last;
+
+ stmt->d.s_prifilt.t_then = removeNOPs(stmt->d.s_prifilt.t_then);
+ cnfstmtOptimize(stmt->d.s_prifilt.t_then);
+
+ for(i = 0; i <= LOG_NFACILITIES; i++)
+ if(stmt->d.s_prifilt.pmask[i] != 0xff) {
+ isAlways = 0;
+ break;
+ }
+ if(!isAlways)
+ goto done;
+
+ DBGPRINTF("optimizer: removing always-true PRIFILT %p\n", stmt);
+ if(stmt->d.s_prifilt.t_else != NULL) {
+ parser_errmsg("error: always-true PRI filter has else part!\n");
+ cnfstmtDestruct(stmt->d.s_prifilt.t_else);
+ }
+ free(stmt->printable);
+ stmt->printable = NULL;
+ subroot = stmt->d.s_prifilt.t_then;
+ if(subroot == NULL) {
+ /* very strange, we set it to NOP, best we can do
+ * This case is NOT expected in practice
+ */
+ stmt->nodetype = S_NOP;
+ goto done;
+ }
+ for(last = subroot ; last->next != NULL ; last = last->next)
+ /* find last node in subtree */;
+ last->next = stmt->next;
+ memcpy(stmt, subroot, sizeof(struct cnfstmt));
+ free(subroot);
+
+done: return;
+}
+
/* (recursively) optimize a statement */
void
cnfstmtOptimize(struct cnfstmt *root)
@@ -2100,8 +2162,7 @@ dbgprintf("RRRR: stmtOptimize: stmt %p, nodetype %u\n", stmt, stmt->nodetype);
cnfstmtOptimizeIf(stmt);
break;
case S_PRIFILT:
- stmt->d.s_prifilt.t_then = removeNOPs(stmt->d.s_prifilt.t_then);
- cnfstmtOptimize(stmt->d.s_prifilt.t_then);
+ cnfstmtOptimizePRIFilt(stmt);
break;
case S_PROPFILT:
stmt->d.s_propfilt.t_then = removeNOPs(stmt->d.s_propfilt.t_then);
@@ -2110,9 +2171,11 @@ dbgprintf("RRRR: stmtOptimize: stmt %p, nodetype %u\n", stmt, stmt->nodetype);
case S_SET:
cnfexprOptimize(stmt->d.s_set.expr);
break;
+ case S_ACT:
+ cnfstmtOptimizeAct(stmt);
+ break;
case S_STOP:
- case S_UNSET:
- case S_ACT: /* nothing to do */
+ case S_UNSET: /* nothing to do */
break;
case S_NOP:
DBGPRINTF("optimizer error: we see a NOP, how come?\n");
@@ -2123,7 +2186,7 @@ dbgprintf("RRRR: stmtOptimize: stmt %p, nodetype %u\n", stmt, stmt->nodetype);
break;
}
}
-done: /*EMPTY*/;
+done: return;
}