aboutsummaryrefslogtreecommitdiffstats
path: root/utils/expression_parser.c
blob: 38b52934ff9159e9cf1e06998f62593529658168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "../src/config.h"
#include "../src/main.h"
#include "../src/cfg_file.h"
#include "../src/expressions.h"

rspamd_hash_t *counters = NULL;

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);

	return 0;
}