]> source.dussan.org Git - rspamd.git/commitdiff
* Add expression parser utility
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Mon, 27 Apr 2009 15:03:11 +0000 (19:03 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Mon, 27 Apr 2009 15:03:11 +0000 (19:03 +0400)
* Temporaly switch off expression optimizer as it should be reworked

CMakeLists.txt
src/expressions.c
src/plugins/regexp.c
utils/expression_parser.c [new file with mode: 0644]

index e1e0c7ba2395c912d959e8bf4435426af69089dc..3b89b265bbef3e49d840a75850775f3ef0c1d50d 100644 (file)
@@ -330,10 +330,14 @@ SET(TESTDEPENDS   src/mem_pool.c
                                src/statfile.c)
 
 SET(UTILSSRC   utils/url_extracter.c)
+SET(EXPRSRC    utils/expression_parser.c)
 
 SET(UTILSDEPENDS src/mem_pool.c
                                src/hash.c
                                src/url.c
+                               src/fuzzy.c
+                               src/expressions.c
+                               src/message.c
                                src/util.c)
 
 LIST(LENGTH PLUGINSSRC RSPAMD_MODULES_NUM)
@@ -414,6 +418,15 @@ ENDIF(LIBUTIL_LIBRARY)
 TARGET_LINK_LIBRARIES(utils/url-extracter ${GLIB2_LIBRARIES})
 TARGET_LINK_LIBRARIES(utils/url-extracter ${GMIME2_LIBRARIES})
 
+ADD_EXECUTABLE(utils/expression-parser ${UTILSDEPENDS} ${CONTRIBSRC} ${EXPRSRC})
+SET_TARGET_PROPERTIES(utils/expression-parser PROPERTIES LINKER_LANGUAGE C)
+TARGET_LINK_LIBRARIES(utils/expression-parser m)
+IF(LIBUTIL_LIBRARY)
+       TARGET_LINK_LIBRARIES(utils/expression-parser util)
+ENDIF(LIBUTIL_LIBRARY)
+TARGET_LINK_LIBRARIES(utils/expression-parser ${GLIB2_LIBRARIES})
+TARGET_LINK_LIBRARIES(utils/expression-parser ${GMIME2_LIBRARIES})
+
 INSTALL(PROGRAMS rspamd-${RSPAMD_VERSION} DESTINATION bin RENAME rspamd)
 INSTALL(PROGRAMS rspamc.pl DESTINATION bin RENAME rspamc)
 IF(ENABLE_PERL MATCHES "ON")
index e656cc433fed0b021a68542a9cce21ad4d860c0b..200c1abd3b6bd6be5c2623ee19ac8ed68e9a8921 100644 (file)
@@ -382,7 +382,7 @@ parse_expression (memory_pool_t *pool, char *line)
                        case READ_REGEXP_FLAGS:
                                if (!is_regexp_flag (*p) || *(p + 1) == '\0') {
                                        if (c != p) {
-                                               if (is_regexp_flag (*p) && *(p + 1) == '\0') {
+                                               if ((is_regexp_flag (*p) || *p == '/') && *(p + 1) == '\0') {
                                                        p ++;
                                                }
                                                str = memory_pool_alloc (pool, p - c + 2);
index 622c06c835af1961e5c158ea6abbd5e5f68dea10..d21baf4941d8665d1f0b9c1004c8b78cf07d181a 100644 (file)
@@ -369,17 +369,17 @@ optimize_regexp_expression (struct expression **e, GQueue *stack, gboolean res)
                                msg_debug ("optimize_regexp_expression: found '!' operator, inversing result");
                                res = !res;
                                it = it->next;
-                               *e = it;
+                               *e = it->next;
                                continue;
                        }
                        else if (it->content.operation == '&' && res == FALSE) {
                                msg_debug ("optimize_regexp_expression: found '&' and previous expression is false");
-                               *e = it;
+                               *e = it->next;
                                ret = TRUE;
                        }
                        else if (it->content.operation == '|' && res == TRUE) {
                                msg_debug ("optimize_regexp_expression: found '|' and previous expression is true");
-                               *e = it;
+                               *e = it->next;
                                ret = TRUE;
                        }
                        break;
@@ -400,7 +400,6 @@ process_regexp_expression (struct expression *expr, struct worker_task *task)
        gsize cur, op1, op2;
        struct expression *it = expr;
        struct rspamd_regexp *re;
-       gboolean try_optimize = TRUE;
        
        stack = g_queue_new ();
 
@@ -409,21 +408,13 @@ process_regexp_expression (struct expression *expr, struct worker_task *task)
                        /* Find corresponding symbol */
                        cur = process_regexp ((struct rspamd_regexp *)it->content.operand, task);
                        msg_debug ("process_regexp_expression: regexp %s found", cur ? "is" : "is not");
-                       if (try_optimize) {
-                               try_optimize = optimize_regexp_expression (&it, stack, cur);
-                       } else {
-                               g_queue_push_head (stack, GSIZE_TO_POINTER (cur));
-                       }
+                       g_queue_push_head (stack, GSIZE_TO_POINTER (cur));
 
                } else if (it->type == EXPR_FUNCTION) {
                        cur = (gsize)call_expression_function ((struct expression_function *)it->content.operand, task);
                        msg_debug ("process_regexp_expression: function %s returned %s", ((struct expression_function *)it->content.operand)->name,
                                                                                                                        cur ? "true" : "false");
-                       if (try_optimize) {
-                               try_optimize = optimize_regexp_expression (&it, stack, cur);
-                       } else {
-                               g_queue_push_head (stack, GSIZE_TO_POINTER (cur));
-                       }
+                       g_queue_push_head (stack, GSIZE_TO_POINTER (cur));
                } else if (it->type == EXPR_REGEXP) {
                        /* Compile regexp if it is not parsed */
                        if (it->content.operand == NULL) {
@@ -445,7 +436,6 @@ process_regexp_expression (struct expression *expr, struct worker_task *task)
                                g_queue_free (stack);
                                return FALSE;
                        }
-                       try_optimize = TRUE;
                        switch (it->content.operation) {
                                case '!':
                                        op1 = GPOINTER_TO_SIZE (g_queue_pop_head (stack));
diff --git a/utils/expression_parser.c b/utils/expression_parser.c
new file mode 100644 (file)
index 0000000..fa0237f
--- /dev/null
@@ -0,0 +1,50 @@
+#include "../src/config.h"
+#include "../src/main.h"
+#include "../src/cfg_file.h"
+#include "../src/expressions.h"
+
+int 
+main (int argc, char **argv)
+{
+       memory_pool_t *pool;
+       struct expression *cur;
+       char *line, *outstr;
+       int r, s;
+       char buf[BUFSIZ];
+
+       pool = memory_pool_new (memory_pool_get_size ());
+       
+       line = fgets (buf, sizeof (buf), stdin);
+       while (line) {
+               s = strlen (line);
+               if (buf[s - 1] == '\n') {
+                       buf[s - 1] = '\0';
+               }
+               if (buf[s - 2] == '\r') {
+                       buf[s - 2] = '\0';
+               }
+
+               r = 0;
+               cur = parse_expression (pool, line);
+               s = strlen (line) * 4;
+               outstr = memory_pool_alloc (pool, s);
+               while (cur) {
+                       if (cur->type == EXPR_REGEXP) {
+                               r += snprintf (outstr + r, s - r, "OP:%s ", (char *)cur->content.operand);
+                       } else if (cur->type == EXPR_STR) {
+                               r += snprintf (outstr + r, s - r, "S:%s ", (char *)cur->content.operand);
+
+                       } else if (cur->type == EXPR_FUNCTION) {
+                               r += snprintf (outstr + r, s - r, "F:%s ", ((struct expression_function *)cur->content.operand)->name);
+                       }
+                       else {
+                               r += snprintf (outstr + r, s - r, "O:%c ", cur->content.operation);
+                       }
+                       cur = cur->next;
+               }
+               printf ("Parsed expression: '%s' -> '%s'\n", line, outstr);
+               line = fgets (buf, sizeof (buf), stdin);
+       }
+
+       memory_pool_delete (pool);
+}