| 1 |
/* dctrl-tools - Debian control file inspection tools |
| 2 |
Copyright © 2003, 2004, 2008, 2010, 2011, 2012 Antti-Juhani Kaijanaho |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 16 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <ctype.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <regex.h> |
| 22 |
#include <string.h> |
| 23 |
#include "atom.h" |
| 24 |
#include "fsaf.h" |
| 25 |
#include "msg.h" |
| 26 |
#include "util.h" |
| 27 |
#include "predicate.h" |
| 28 |
#include "strutil.h" |
| 29 |
#include "version.h" |
| 30 |
|
| 31 |
typedef bool (*eval_t)(struct predicate *, para_t * para); |
| 32 |
|
| 33 |
struct predicate_vtbl { |
| 34 |
bool (*eval)(struct predicate *, para_t *); |
| 35 |
void (*print)(struct predicate *, size_t indent); |
| 36 |
}; |
| 37 |
|
| 38 |
struct predicate { |
| 39 |
const struct predicate_vtbl *vtbl; |
| 40 |
}; |
| 41 |
|
| 42 |
static void print(struct predicate *p, size_t indent) |
| 43 |
{ |
| 44 |
p->vtbl->print(p, indent); |
| 45 |
} |
| 46 |
|
| 47 |
struct unary_predicate { |
| 48 |
struct predicate super; |
| 49 |
struct predicate *rand; |
| 50 |
}; |
| 51 |
|
| 52 |
struct binary_predicate { |
| 53 |
struct predicate super; |
| 54 |
struct predicate *lrand; |
| 55 |
struct predicate *rrand; |
| 56 |
}; |
| 57 |
|
| 58 |
struct atomary_predicate { |
| 59 |
struct predicate super; |
| 60 |
struct atom *atom; |
| 61 |
}; |
| 62 |
|
| 63 |
static bool eval_AND(struct predicate *base_p, para_t * para) |
| 64 |
{ |
| 65 |
struct binary_predicate *p = (struct binary_predicate *)base_p; |
| 66 |
if (!does_para_satisfy(p->lrand, para)) return false; |
| 67 |
return does_para_satisfy(p->rrand, para); |
| 68 |
} |
| 69 |
|
| 70 |
static void print_AND(struct predicate *base_p, size_t indent) |
| 71 |
{ |
| 72 |
struct binary_predicate *p = (struct binary_predicate *)base_p; |
| 73 |
for (size_t i = 0; i < indent; i++) putchar(' '); |
| 74 |
puts("AND"); |
| 75 |
print(p->lrand, indent+1); |
| 76 |
print(p->rrand, indent+1); |
| 77 |
} |
| 78 |
|
| 79 |
static bool eval_OR(struct predicate *base_p, para_t * para) |
| 80 |
{ |
| 81 |
struct binary_predicate *p = (struct binary_predicate *)base_p; |
| 82 |
if (does_para_satisfy(p->lrand, para)) return true; |
| 83 |
return does_para_satisfy(p->rrand, para); |
| 84 |
} |
| 85 |
|
| 86 |
static void print_OR(struct predicate *base_p, size_t indent) |
| 87 |
{ |
| 88 |
struct binary_predicate *p = (struct binary_predicate *)base_p; |
| 89 |
for (size_t i = 0; i < indent; i++) putchar(' '); |
| 90 |
puts("OR"); |
| 91 |
print(p->lrand, indent+1); |
| 92 |
print(p->rrand, indent+1); |
| 93 |
} |
| 94 |
|
| 95 |
static bool eval_NOT(struct predicate *base_p, para_t * para) |
| 96 |
{ |
| 97 |
struct unary_predicate *p = (struct unary_predicate *)base_p; |
| 98 |
return !does_para_satisfy(p->rand, para); |
| 99 |
} |
| 100 |
|
| 101 |
static void print_NOT(struct predicate *base_p, size_t indent) |
| 102 |
{ |
| 103 |
struct unary_predicate *p = (struct unary_predicate *)base_p; |
| 104 |
for (size_t i = 0; i < indent; i++) putchar(' '); |
| 105 |
puts("NOT"); |
| 106 |
print(p->rand, indent+1); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
static bool eval_ATOM(struct predicate *base_p, para_t * para) |
| 111 |
{ |
| 112 |
struct atomary_predicate *p = (struct atomary_predicate *)base_p; |
| 113 |
return atom_verify(p->atom, para); |
| 114 |
} |
| 115 |
|
| 116 |
static void print_ATOM(struct predicate *base_p, size_t indent) |
| 117 |
{ |
| 118 |
struct atomary_predicate *p = (struct atomary_predicate *)base_p; |
| 119 |
char ind[indent+1]; |
| 120 |
for (size_t i = 0; i < indent; i++) ind[i] = ' '; |
| 121 |
ind[indent] = '\0'; |
| 122 |
printf("%sATOM", ind); |
| 123 |
printf("%s field_name = %s\n", ind, p->atom->field_name); |
| 124 |
printf("%s mode = %i\n", ind, p->atom->mode); |
| 125 |
printf("%s ignore_case = %i\n", ind, p->atom->ignore_case); |
| 126 |
printf("%s whole_pkg = %i\n", ind, p->atom->whole_pkg); |
| 127 |
printf("%s pat = %s\n", ind, p->atom->pat); |
| 128 |
} |
| 129 |
|
| 130 |
struct predicate *binary_predicate(const struct predicate_vtbl *vtbl, |
| 131 |
struct predicate *pl, struct predicate *pr){ |
| 132 |
struct binary_predicate *rv = malloc(sizeof *rv); |
| 133 |
if (rv == 0) enomem(0); |
| 134 |
rv->super.vtbl = vtbl; |
| 135 |
rv->lrand= pl; |
| 136 |
rv->rrand = pr; |
| 137 |
return &rv->super; |
| 138 |
} |
| 139 |
struct predicate *predicate_AND(struct predicate *pl, struct predicate *pr) |
| 140 |
{ |
| 141 |
static const struct predicate_vtbl vtbl = |
| 142 |
{ .eval = eval_AND, .print = print_AND }; |
| 143 |
return binary_predicate(&vtbl, pl, pr); |
| 144 |
} |
| 145 |
struct predicate *predicate_OR(struct predicate *pl, struct predicate *pr) |
| 146 |
{ |
| 147 |
static const struct predicate_vtbl vtbl = |
| 148 |
{ .eval = eval_OR, .print = print_OR }; |
| 149 |
return binary_predicate(&vtbl, pl, pr); |
| 150 |
} |
| 151 |
struct predicate *predicate_NOT(struct predicate *p) |
| 152 |
{ |
| 153 |
static const struct predicate_vtbl vtbl = |
| 154 |
{ .eval = eval_NOT, .print = print_NOT }; |
| 155 |
struct unary_predicate *rv = malloc(sizeof *rv); |
| 156 |
if (rv == 0) enomem(0); |
| 157 |
rv->super.vtbl = &vtbl; |
| 158 |
rv->rand = p; |
| 159 |
return &rv->super; |
| 160 |
} |
| 161 |
struct predicate *predicate_ATOM(struct atom *at) |
| 162 |
{ |
| 163 |
static const struct predicate_vtbl vtbl = |
| 164 |
{ .eval = eval_ATOM, .print = print_ATOM }; |
| 165 |
struct atomary_predicate *rv = malloc(sizeof *rv); |
| 166 |
if (rv == 0) enomem(0); |
| 167 |
rv->super.vtbl = &vtbl; |
| 168 |
rv->atom = at; |
| 169 |
return &rv->super; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
bool check_predicate(struct predicate * p __attribute__((unused))) |
| 174 |
{ |
| 175 |
// static checking of predicate |
| 176 |
// currently no operation |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
bool does_para_satisfy(struct predicate * p, para_t * para) |
| 181 |
{ |
| 182 |
return p->vtbl->eval(p, para); |
| 183 |
} |
| 184 |
|
| 185 |
void predicate_print(struct predicate *p) |
| 186 |
{ |
| 187 |
print(p, 0); |
| 188 |
} |