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 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/* dctrl-tools - Debian control file inspection tools
Copyright © 2003, 2004, 2008, 2010, 2011 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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <ctype.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>
#include "atom.h"
#include "fsaf.h"
#include "msg.h"
#include "util.h"
#include "predicate.h"
#include "strutil.h"
#include "version.h"
typedef bool (*eval_t)(struct predicate *, para_t * para);
struct predicate_vtbl {
bool (*eval)(struct predicate *, para_t *);
void (*print)(struct predicate *, size_t indent);
};
struct predicate {
const struct predicate_vtbl *vtbl;
};
static void print(struct predicate *p, size_t indent)
{
p->vtbl->print(p, indent);
}
struct unary_predicate {
struct predicate super;
struct predicate *rand;
};
struct binary_predicate {
struct predicate super;
struct predicate *lrand;
struct predicate *rrand;
};
struct atomary_predicate {
struct predicate super;
struct atom *atom;
};
static bool eval_AND(struct predicate *base_p, para_t * para)
{
struct binary_predicate *p = (struct binary_predicate *)base_p;
if (!does_para_satisfy(p->lrand, para)) return false;
return does_para_satisfy(p->rrand, para);
}
static void print_AND(struct predicate *base_p, size_t indent)
{
struct binary_predicate *p = (struct binary_predicate *)base_p;
for (int i = 0; i < indent; i++) putchar(' ');
puts("AND");
print(p->lrand, indent+1);
print(p->rrand, indent+1);
}
static bool eval_OR(struct predicate *base_p, para_t * para)
{
struct binary_predicate *p = (struct binary_predicate *)base_p;
if (does_para_satisfy(p->lrand, para)) return true;
return does_para_satisfy(p->rrand, para);
}
static void print_OR(struct predicate *base_p, size_t indent)
{
struct binary_predicate *p = (struct binary_predicate *)base_p;
for (int i = 0; i < indent; i++) putchar(' ');
puts("OR");
print(p->lrand, indent+1);
print(p->rrand, indent+1);
}
static bool eval_NOT(struct predicate *base_p, para_t * para)
{
struct unary_predicate *p = (struct unary_predicate *)base_p;
return !does_para_satisfy(p->rand, para);
}
static void print_NOT(struct predicate *base_p, size_t indent)
{
struct unary_predicate *p = (struct unary_predicate *)base_p;
for (int i = 0; i < indent; i++) putchar(' ');
puts("NOT");
print(p->rand, indent+1);
}
static bool eval_ATOM(struct predicate *base_p, para_t * para)
{
struct atomary_predicate *p = (struct atomary_predicate *)base_p;
return atom_verify(p->atom, para);
}
static void print_ATOM(struct predicate *base_p, size_t indent)
{
struct atomary_predicate *p = (struct atomary_predicate *)base_p;
char ind[indent+1];
for (int i = 0; i < indent; i++) ind[i] = ' ';
ind[indent] = '\0';
printf("%sATOM", ind);
printf("%s field_name = %s\n", ind, p->atom->field_name);
printf("%s mode = %i\n", ind, p->atom->mode);
printf("%s ignore_case = %i\n", ind, p->atom->ignore_case);
printf("%s whole_pkg = %i\n", ind, p->atom->whole_pkg);
printf("%s pat = %s\n", ind, p->atom->pat);
}
struct predicate *binary_predicate(const struct predicate_vtbl *vtbl,
struct predicate *pl, struct predicate *pr){
struct binary_predicate *rv = malloc(sizeof *rv);
if (rv == 0) enomem(0);
rv->super.vtbl = vtbl;
rv->lrand= pl;
rv->rrand = pr;
return &rv->super;
}
struct predicate *predicate_AND(struct predicate *pl, struct predicate *pr)
{
static const struct predicate_vtbl vtbl =
{ .eval = eval_AND, .print = print_AND };
return binary_predicate(&vtbl, pl, pr);
}
struct predicate *predicate_OR(struct predicate *pl, struct predicate *pr)
{
static const struct predicate_vtbl vtbl =
{ .eval = eval_OR, .print = print_OR };
return binary_predicate(&vtbl, pl, pr);
}
struct predicate *predicate_NOT(struct predicate *p)
{
static const struct predicate_vtbl vtbl =
{ .eval = eval_NOT, .print = print_NOT };
struct unary_predicate *rv = malloc(sizeof *rv);
if (rv == 0) enomem(0);
rv->super.vtbl = &vtbl;
rv->rand = p;
return &rv->super;
}
struct predicate *predicate_ATOM(struct atom *at)
{
static const struct predicate_vtbl vtbl =
{ .eval = eval_ATOM, .print = print_ATOM };
struct atomary_predicate *rv = malloc(sizeof *rv);
if (rv == 0) enomem(0);
rv->super.vtbl = &vtbl;
rv->atom = at;
return &rv->super;
}
bool check_predicate(struct predicate * p)
{
// static checking of predicate
// currently no operation
return true;
}
bool does_para_satisfy(struct predicate * p, para_t * para)
{
return p->vtbl->eval(p, para);
}
void predicate_print(struct predicate *p)
{
print(p, 0);
}
|