summaryrefslogtreecommitdiff
path: root/predicate.c
blob: 421079a31d7cd4354aa62092b4a80c7c2885fc1d (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*  dctrl-tools - Debian control file inspection tools
    Copyright (C) 2003, 2004 Antti-Juhani Kaijanaho

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <ctype.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>
#include "fsaf.h"
#include "msg.h"
#include "util.h"
#include "predicate.h"

void init_predicate(struct predicate * p)
{
	p->num_atoms = 0;
	p->proglen = 0;
	fieldtrie_init(&p->trie);
}

void addinsn(struct predicate * p, int insn)
{
	if (insn == I_NOP) return;
	if (p->proglen >= MAX_OPS) {
		message(L_FATAL, "predicate is too complex", 0);
		fail();
	}
	p->program[p->proglen++] = insn;
}

void predicate_finish_atom(struct predicate * p)
{
	struct atom * atom =  get_current_atom(p);
	if (atom->field_name != 0) {
		atom->field_inx = fieldtrie_insert(&p->trie, atom->field_name);
	}

	if (atom->mode == M_REGEX || atom->mode == M_EREGEX) {
		int rerr = regcomp(&atom->regex, atom->pat,
				   (atom->mode == M_EREGEX ? REG_EXTENDED : 0)
				   | REG_NOSUB
				   | (atom->ignore_case ? REG_ICASE : 0));
		if (rerr != 0) {
			char * s;
			s = get_regerror(rerr, &atom->regex);
			if (s == 0) fatal_enomem(0);
			message(L_FATAL, s, 0);
			free(s);
			fail();
		}
	}
}

static bool verify_atom(struct atom * atom, para_t * para)
{
	size_t start, end;
	if (atom->field_inx == -1) {
		/* Take the full paragraph */
		start = para->start;
		end = para->end;
	} else {
		/* Take the field */
		struct field_data * fd = &para->fields[atom->field_inx];
		start = fd->start;
		end = fd->end;
	}
	size_t len = end - start;
	struct fsaf_read_rv r = fsaf_read(para->fp, start, len);
	assert(r.len == len);
	switch (atom->mode) {
	case M_EXACT:
		if (len != atom->patlen) return false;
		if (atom->ignore_case) {
			return strncasecmp(atom->pat, r.b, len) == 0;
		} else {
			return strncmp(atom->pat, r.b, len) == 0;
		}
	case M_SUBSTR: {
#if 0
		if (atom->ignore_case) {
			return strncasestr(r.b, atom->pat, len);
		} else {
			return strnstr(r.b, atom->pat, len);
		}
#else
		bool rv;
		char * s = strndup(r.b, len);
		if (s == 0) fatal_enomem(0);
		if (atom->ignore_case) {
			rv = strcasestr(s, atom->pat) != 0;
		} else {
			rv = strstr(s, atom->pat) != 0;
		}
		free(s);
		return rv;
#endif
	}
	case M_REGEX: case M_EREGEX: {
		char * s = strndup(r.b, len);
		if (s == 0) fatal_enomem(0);
		int regex_errcode = regexec(&atom->regex, s, 0, 0, 0);
		free(s);
		if (regex_errcode == 0 || regex_errcode == REG_NOMATCH) {
			return (regex_errcode == 0);
		}
		/* Error handling be here. */
		assert(regex_errcode != 0 && regex_errcode != REG_NOMATCH);
		s = get_regerror (regex_errcode, &atom->regex);
		if (s == 0) { enomem (0); return false; }
		message(L_IMPORTANT, s, 0);
		free(s);
		return false;
	}
	}
	assert(0);
}

bool does_para_satisfy(struct predicate * p, para_t * para)
{
	assert(para->trie == & p->trie);

	bool sat_atom[MAX_ATOMS];
	bool stack[MAX_OPS];
	size_t sp = 0;

	/* Verify atoms. */
	for (size_t i = 0; i < p->num_atoms; i++) {
		sat_atom[i] = verify_atom(&p->atoms[i], para);
	}

	/* Run the program. */
	for (size_t i = 0; i < p->proglen; i++) {
		switch (p->program[i]) {
		case I_NOP: break;
		case I_NEG: 
			assert(sp >= 1);
			stack[sp-1] = !stack[sp-1];
			break;
		case I_AND:
			assert(sp >= 2);
			stack[sp-2] = stack[sp-2] && stack[sp-1];
			--sp;
			break;
		case I_OR:
			assert(sp >= 2);
			stack[sp-2] = stack[sp-2] || stack[sp-1];
			--sp;
			break;
		default: 
		{
			int atom = p->program[i] - I_PUSH(0);
			assert(atom <= p->num_atoms);
			stack[sp] = sat_atom[atom];
			++sp;
		}
		}
	}
	assert(sp == 1);
	return stack[0];
}