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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/* This file is part of "reprepro"
* Copyright (C) 2004,2005,2007,2009,2016 Bernhard R. Link
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 St, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <config.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "error.h"
#include "mprintf.h"
#include "strlist.h"
#include "names.h"
#include "chunks.h"
#include "globmatch.h"
#include "dpkgversions.h"
#include "terms.h"
#include "termdecide.h"
static inline bool check_field(enum term_comparison c, const char *value, const char *with) {
if (c == tc_none) {
return true;
} else if (c == tc_globmatch) {
return globmatch(value, with);
} else if (c == tc_notglobmatch) {
return !globmatch(value, with);
} else {
int i;
i = strcmp(value, with);
if (i < 0)
return c == tc_strictless
|| c == tc_lessorequal
|| c == tc_notequal;
else if (i > 0)
return c == tc_strictmore
|| c == tc_moreorequal
|| c == tc_notequal;
else
return c == tc_lessorequal
|| c == tc_moreorequal
|| c == tc_equal;
}
}
/* this has a target argument instead of using package->target
* as the package might come from one distribution/architecture/...
* and the decision being about adding it somewhere else */
retvalue term_decidepackage(const term *condition, struct package *package, struct target *target) {
const struct term_atom *atom = condition;
while (atom != NULL) {
bool correct; char *value;
enum term_comparison c = atom->comparison;
retvalue r;
if (atom->isspecial) {
correct = atom->special.type->compare(c,
&atom->special.comparewith,
package, target);
} else {
r = chunk_getvalue(package->control,
atom->generic.key, &value);
if (RET_WAS_ERROR(r))
return r;
if (r == RET_NOTHING) {
correct = (c == tc_notequal
|| c == tc_notglobmatch);
} else {
correct = check_field(c, value,
atom->generic.comparewith);
free(value);
}
}
if (atom->negated)
correct = !correct;
if (correct) {
atom = atom->nextiftrue;
} else {
atom = atom->nextiffalse;
if (atom == NULL) {
/* do not include */
return RET_NOTHING;
}
}
}
/* do include */
return RET_OK;
}
static retvalue parsestring(enum term_comparison c, const char *value, size_t len, struct compare_with *v) {
if (c == tc_none) {
fprintf(stderr,
"Error: Special formula predicates (those starting with '$') are always\n"
"defined, thus specifying them without parameter to compare against\n"
"makes not sense!\n");
return RET_ERROR;
}
v->pointer = strndup(value, len);
if (FAILEDTOALLOC(v->pointer))
return RET_ERROR_OOM;
return RET_OK;
}
// TODO: check for well-formed versions
#define parseversion parsestring
static bool comparesource(enum term_comparison c, const struct compare_with *v, void *d1, UNUSED(void *d2)) {
struct package *package = d1;
retvalue r;
r = package_getsource(package);
if (!RET_IS_OK(r))
return false;
return check_field(c, package->source, v->pointer);
}
static inline bool compare_dpkgversions(enum term_comparison c, const char *version, const char *param) {
if (c != tc_globmatch && c != tc_notglobmatch) {
int cmp;
retvalue r;
r = dpkgversions_cmp(version, param, &cmp);
if (RET_IS_OK(r)) {
if (cmp < 0)
return c == tc_strictless
|| c == tc_lessorequal
|| c == tc_notequal;
else if (cmp > 0)
return c == tc_strictmore
|| c == tc_moreorequal
|| c == tc_notequal;
else
return c == tc_lessorequal
|| c == tc_moreorequal
|| c == tc_equal;
} else
return false;
} else
return check_field(c, version, param);
}
static bool compareversion(enum term_comparison c, const struct compare_with *v, void *d1, UNUSED(void *d2)) {
struct package *package = d1;
retvalue r;
r = package_getversion(package);
if (!RET_IS_OK(r))
return false;
return compare_dpkgversions(c, package->version, v->pointer);
}
static bool comparesourceversion(enum term_comparison c, const struct compare_with *v, void *d1, UNUSED(void *d2)) {
struct package *package = d1;
retvalue r;
r = package_getsource(package);
if (!RET_IS_OK(r))
return false;
return compare_dpkgversions(c, package->sourceversion, v->pointer);
}
static void freestring(UNUSED(enum term_comparison c), struct compare_with *d) {
free(d->pointer);
}
static void freeatom(enum term_comparison c, struct compare_with *d) {
if (c != tc_equal && c != tc_notequal)
free(d->pointer);
}
static retvalue parsetype(enum term_comparison c, const char *value, size_t len, struct compare_with *v) {
if (c == tc_none) {
fprintf(stderr,
"Error: $Type is always defined, it does not make sense without parameter\n"
"to compare against!\n");
return RET_ERROR;
}
if (c != tc_equal && c != tc_notequal) {
v->pointer = strndup(value, len);
if (FAILEDTOALLOC(v->pointer))
return RET_ERROR_OOM;
return RET_OK;
}
v->number = packagetype_find_l(value, len);
if (atom_defined(v->number))
return RET_OK;
fprintf(stderr, "Unknown package type '%.*s' in formula!\n",
(int)len, value);
return RET_ERROR;
}
static retvalue parsearchitecture(enum term_comparison c, const char *value, size_t len, struct compare_with *v) {
if (c == tc_none) {
fprintf(stderr,
"Error: $Architecture is always defined, it does not make sense without parameter\n"
"to compare against!\n");
return RET_ERROR;
}
if (c != tc_equal && c != tc_notequal) {
v->pointer = strndup(value, len);
if (FAILEDTOALLOC(v->pointer))
return RET_ERROR_OOM;
return RET_OK;
}
v->number = architecture_find_l(value, len);
if (atom_defined(v->number))
return RET_OK;
fprintf(stderr,
"Unknown architecture '%.*s' in formula (must be listed in conf/distributions to be known)!\n",
(int)len, value);
return RET_ERROR;
}
static retvalue parsecomponent(enum term_comparison c, const char *value, size_t len, struct compare_with *v) {
if (c == tc_none) {
fprintf(stderr,
"Error: $Component is always defined, it does not make sense without parameter\n"
"to compare against!\n");
return RET_ERROR;
}
if (c != tc_equal && c != tc_notequal) {
v->pointer = strndup(value, len);
if (FAILEDTOALLOC(v->pointer))
return RET_ERROR_OOM;
return RET_OK;
}
v->number = component_find_l(value, len);
if (atom_defined(v->number))
return RET_OK;
fprintf(stderr,
"Unknown component '%.*s' in formula (must be listed in conf/distributions to be known)!\n",
(int)len, value);
return RET_ERROR;
}
static bool comparetype(enum term_comparison c, const struct compare_with *v, UNUSED( void *d1), void *d2) {
const struct target *target = d2;
if (c == tc_equal)
return v->number == target->packagetype;
else if (c == tc_notequal)
return v->number != target->packagetype;
else
return check_field(c,
atoms_packagetypes[target->packagetype],
v->pointer);
}
static bool comparearchitecture(enum term_comparison c, const struct compare_with *v, UNUSED(void *d1), void *d2) {
const struct target *target = d2;
if (c == tc_equal)
return v->number == target->architecture;
else if (c == tc_notequal)
return v->number != target->architecture;
else
return check_field(c,
atoms_architectures[target->architecture],
v->pointer);
}
static bool comparecomponent(enum term_comparison c, const struct compare_with *v, UNUSED(void *d1), void *d2) {
const struct target *target = d2;
if (c == tc_equal)
return v->number == target->component;
else if (c == tc_notequal)
return v->number != target->component;
else
return check_field(c,
atoms_components[target->component],
v->pointer);
}
static struct term_special targetdecisionspecial[] = {
{"$Source", parsestring, comparesource, freestring},
{"$SourceVersion", parseversion, comparesourceversion, freestring},
{"$Version", parseversion, compareversion, freestring},
{"$Architecture", parsearchitecture, comparearchitecture, freeatom},
{"$Component", parsecomponent, comparecomponent, freeatom},
{"$Type", parsetype, comparetype, freeatom},
{"$PackageType", parsetype, comparetype, freeatom},
{NULL, NULL, NULL, NULL}
};
retvalue term_compilefortargetdecision(term **term_p, const char *formula) {
return term_compile(term_p, formula,
T_GLOBMATCH|T_OR|T_BRACKETS|T_NEGATION|T_VERSION|T_NOTEQUAL,
targetdecisionspecial);
}
|