summaryrefslogtreecommitdiff
path: root/lib/predicate.h
blob: 5860b6f3001106b77c4f838e193489af40c32662 (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
/*  dctrl-tools - Debian control file inspection tools
    Copyright (C) 2003, 2004, 2005 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
 */

#ifndef PREDICATE_H
#define PREDICATE_H

#include <assert.h>
#include <regex.h>
#include <stdint.h>
#include "fieldtrie.h"
#include "paragraph.h"

#define MAX_OPS 4096
#define MAX_ATOMS 4096

#define I_NOP  0
#define I_NEG  1 /* --not; 1-1 */
#define I_AND  2 /* --and; 2-1 */
#define I_OR   3 /* --or;  2-1 */
#define I_PUSH(n) (4+(n)) /* push result of nth atomic proposition */

/* An atomic predicate. */
struct atom {
	/* The name of field to which matching is limited.  Empty
	 * field_name specifies the whole paragraph (in which case
	 * field_inx is -1. */
	char const * field_name; size_t field_inx;
        /* The index to the field whose value is to be used when this
         * field is empty. */
        size_t repl_inx;
	/* Matching mode */
	enum matching_mode {
		M_SUBSTR, /* substring matching */
		M_REGEX, /* POSIX regular expression match */
		M_EREGEX, /* POSIX extended regular expression matching */
		M_EXACT, /* exact string match */
#define M_FIRST_VERSION M_VER_EQ
		M_VER_EQ, /* numeric equality comparison */
		M_VER_LT, /* numeric < */
		M_VER_LE, /* numeric <= */
		M_VER_GT, /* numeric > */
		M_VER_GE, /* numeric >= */
#define M_LAST_VERSION M_VER_GE
	} mode;
	/* Flag: should matching ignore case */
	unsigned ignore_case;
	/* The pattern as given on the command line; interpretation
	 * depends on matching mode. Must be null-terminated and
	 * patlen must equal strlen(pat).  */
	char const * pat; size_t patlen;
	/* A compiled version of pat; valid only when mode is M_REGEX
	 * or M_EREGEX.  */
	regex_t regex;
};

/* A predicate is represented as a set of atomic predicates and a
 * program - a sequence of stack-based "bytecode" instructions - that
 * specifies the structure of the combined predicate.  */
struct predicate {
	/* Number of atomic predicates.  */
	size_t num_atoms;
	/* Length of the program */
	size_t proglen;
	/* The program */
	int program[MAX_OPS];
	/* The atomic predicates */
	struct atom atoms[MAX_ATOMS];
};

void init_predicate(struct predicate * p);

static inline
struct atom * get_current_atom(struct predicate * p)
{
	assert(p->num_atoms > 0);
	return &p->atoms[p->num_atoms-1];
}

void predicate_finish_atom(struct predicate *);

void addinsn(struct predicate * p, int insn);

bool does_para_satisfy(struct predicate * p, para_t *);

bool check_predicate(struct predicate * p);

#endif /* PREDICATE_H */