| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <unistd.h> |
| 5 |
#include <regex.h> |
| 6 |
|
| 7 |
#include "cctype.h" |
| 8 |
#include "rpmatch.h" |
| 9 |
#include "smartcolsP.h" |
| 10 |
|
| 11 |
struct filter_param { |
| 12 |
struct filter_node node; |
| 13 |
int type; |
| 14 |
enum filter_holder holder; |
| 15 |
|
| 16 |
union { |
| 17 |
char *str; |
| 18 |
unsigned long long num; |
| 19 |
long double fnum; |
| 20 |
bool boolean; |
| 21 |
} val; |
| 22 |
|
| 23 |
struct list_head pr_params; |
| 24 |
struct libscols_column *col; |
| 25 |
char *holder_name; |
| 26 |
regex_t *re; |
| 27 |
|
| 28 |
bool fetched, /* holder requested */ |
| 29 |
empty; |
| 30 |
}; |
| 31 |
|
| 32 |
static int cast_param(int type, struct filter_param *n); |
| 33 |
|
| 34 |
static inline const char *datatype2str(int type) |
| 35 |
{ |
| 36 |
static const char *const types[] = { |
| 37 |
[SCOLS_DATA_NONE] = "none", |
| 38 |
[SCOLS_DATA_STRING] = "string", |
| 39 |
[SCOLS_DATA_U64] = "u64", |
| 40 |
[SCOLS_DATA_FLOAT] = "float", |
| 41 |
[SCOLS_DATA_BOOLEAN] = "boolean" |
| 42 |
}; |
| 43 |
return types[type]; |
| 44 |
} |
| 45 |
|
| 46 |
static char *rem_quotation(const char *p, int c) |
| 47 |
{ |
| 48 |
size_t len = strlen(p); |
| 49 |
|
| 50 |
if (*(p + (len - 1)) == c) |
| 51 |
len -= 2; |
| 52 |
return strndup(p + 1, len); |
| 53 |
} |
| 54 |
|
| 55 |
static int param_set_data(struct filter_param *n, int type, const void *data) |
| 56 |
{ |
| 57 |
const char *p; |
| 58 |
|
| 59 |
/*DBG(FPARAM, ul_debugobj(n, " set %s data", datatype2str(type)));*/ |
| 60 |
|
| 61 |
switch (type) { |
| 62 |
case SCOLS_DATA_STRING: |
| 63 |
p = data; |
| 64 |
if (p && (*p == '"' || *p == '\'')) |
| 65 |
n->val.str = rem_quotation(p, *p); |
| 66 |
else if (data) |
| 67 |
n->val.str = strdup((char *) data); |
| 68 |
if (data && !n->val.str) |
| 69 |
return -ENOMEM; |
| 70 |
if (data) { |
| 71 |
rtrim_whitespace((unsigned char *) n->val.str); |
| 72 |
ltrim_whitespace((unsigned char *) n->val.str); |
| 73 |
} |
| 74 |
break; |
| 75 |
case SCOLS_DATA_U64: |
| 76 |
n->val.num = data ? *((unsigned long long *) data) : 0; |
| 77 |
break; |
| 78 |
case SCOLS_DATA_FLOAT: |
| 79 |
n->val.fnum = data ? *((long double *) data) : 0; |
| 80 |
break; |
| 81 |
case SCOLS_DATA_BOOLEAN: |
| 82 |
n->val.boolean = data ? (*((bool *) data) == 0 ? 0 : 1) : 0; |
| 83 |
break; |
| 84 |
default: |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
n->type = type; |
| 89 |
n->empty = data == NULL; |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
struct filter_node *filter_new_param( |
| 94 |
struct libscols_filter *fltr, |
| 95 |
int type, |
| 96 |
enum filter_holder holder, |
| 97 |
void *data) |
| 98 |
{ |
| 99 |
struct filter_param *n = (struct filter_param *) __filter_new_node( |
| 100 |
F_NODE_PARAM, |
| 101 |
sizeof(struct filter_param)); |
| 102 |
if (!n) |
| 103 |
return NULL; |
| 104 |
|
| 105 |
n->type = type; |
| 106 |
n->holder = holder; |
| 107 |
INIT_LIST_HEAD(&n->pr_params); |
| 108 |
|
| 109 |
if (param_set_data(n, type, data) != 0) { |
| 110 |
filter_free_param(n); |
| 111 |
return NULL; |
| 112 |
} |
| 113 |
|
| 114 |
if (holder == F_HOLDER_COLUMN) { |
| 115 |
n->holder_name = strdup((char *) data); |
| 116 |
DBG(FLTR, ul_debugobj(fltr, "new %s holder", n->holder_name)); |
| 117 |
} |
| 118 |
|
| 119 |
if (fltr) |
| 120 |
list_add_tail(&n->pr_params, &fltr->params); |
| 121 |
|
| 122 |
return (struct filter_node *) n; |
| 123 |
} |
| 124 |
|
| 125 |
int filter_compile_param(struct libscols_filter *fltr, struct filter_param *n) |
| 126 |
{ |
| 127 |
int rc; |
| 128 |
|
| 129 |
if (n->re) |
| 130 |
return 0; |
| 131 |
if (!n->val.str) |
| 132 |
return -EINVAL; |
| 133 |
|
| 134 |
n->re = calloc(1, sizeof(regex_t)); |
| 135 |
if (!n->re) |
| 136 |
return -ENOMEM; |
| 137 |
|
| 138 |
rc = regcomp(n->re, n->val.str, REG_NOSUB | REG_EXTENDED); |
| 139 |
if (rc) { |
| 140 |
size_t size = regerror(rc, n->re, NULL, 0); |
| 141 |
|
| 142 |
fltr->errmsg = malloc(size + 1); |
| 143 |
if (!fltr->errmsg) |
| 144 |
return -ENOMEM; |
| 145 |
regerror(rc, n->re, fltr->errmsg, size); |
| 146 |
return -EINVAL; |
| 147 |
} |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
static struct filter_param *copy_param(struct filter_param *n) |
| 152 |
{ |
| 153 |
void *data = NULL; |
| 154 |
|
| 155 |
switch (n->type) { |
| 156 |
case SCOLS_DATA_STRING: |
| 157 |
data = n->val.str; |
| 158 |
break; |
| 159 |
case SCOLS_DATA_U64: |
| 160 |
data = &n->val.num; |
| 161 |
break; |
| 162 |
case SCOLS_DATA_FLOAT: |
| 163 |
data = &n->val.fnum; |
| 164 |
break; |
| 165 |
case SCOLS_DATA_BOOLEAN: |
| 166 |
data = &n->val.boolean; |
| 167 |
break; |
| 168 |
} |
| 169 |
|
| 170 |
DBG(FPARAM, ul_debugobj(n, "copying")); |
| 171 |
return (struct filter_param *) filter_new_param(NULL, n->type, F_HOLDER_NONE, data); |
| 172 |
} |
| 173 |
|
| 174 |
static void param_reset_data(struct filter_param *n) |
| 175 |
{ |
| 176 |
if (n->type == SCOLS_DATA_STRING) |
| 177 |
free(n->val.str); |
| 178 |
|
| 179 |
memset(&n->val, 0, sizeof(n->val)); |
| 180 |
n->fetched = 0; |
| 181 |
n->empty = 1; |
| 182 |
|
| 183 |
if (n->re) { |
| 184 |
regfree(n->re); |
| 185 |
free(n->re); |
| 186 |
n->re = NULL; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void filter_free_param(struct filter_param *n) |
| 191 |
{ |
| 192 |
param_reset_data(n); |
| 193 |
|
| 194 |
free(n->holder_name); |
| 195 |
list_del_init(&n->pr_params); |
| 196 |
scols_unref_column(n->col); |
| 197 |
free(n); |
| 198 |
} |
| 199 |
|
| 200 |
int filter_param_get_datatype(struct filter_param *n) |
| 201 |
{ |
| 202 |
return n ? n->type : SCOLS_DATA_NONE; |
| 203 |
} |
| 204 |
|
| 205 |
int is_filter_holder_node(struct filter_node *n) |
| 206 |
{ |
| 207 |
return n && filter_node_get_type(n) == F_NODE_PARAM |
| 208 |
&& ((struct filter_param *)(n))->holder; |
| 209 |
} |
| 210 |
|
| 211 |
void filter_dump_param(struct ul_jsonwrt *json, struct filter_param *n) |
| 212 |
{ |
| 213 |
ul_jsonwrt_object_open(json, "param"); |
| 214 |
|
| 215 |
if (n->empty) { |
| 216 |
ul_jsonwrt_value_boolean(json, "empty", true); |
| 217 |
ul_jsonwrt_value_s(json, "type", datatype2str(n->type)); |
| 218 |
} else { |
| 219 |
switch (n->type) { |
| 220 |
case SCOLS_DATA_STRING: |
| 221 |
ul_jsonwrt_value_s(json, "string", n->val.str); |
| 222 |
break; |
| 223 |
case SCOLS_DATA_U64: |
| 224 |
ul_jsonwrt_value_u64(json, "number", n->val.num); |
| 225 |
break; |
| 226 |
case SCOLS_DATA_FLOAT: |
| 227 |
ul_jsonwrt_value_double(json, "float", n->val.fnum); |
| 228 |
break; |
| 229 |
case SCOLS_DATA_BOOLEAN: |
| 230 |
ul_jsonwrt_value_boolean(json, "bool", n->val.boolean); |
| 231 |
break; |
| 232 |
default: |
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if (n->holder == F_HOLDER_COLUMN) |
| 238 |
ul_jsonwrt_value_s(json, "column", n->holder_name); |
| 239 |
|
| 240 |
ul_jsonwrt_object_close(json); |
| 241 |
} |
| 242 |
|
| 243 |
int filter_param_reset_holder(struct filter_param *n) |
| 244 |
{ |
| 245 |
if (!n->holder || !n->col) |
| 246 |
return 0; |
| 247 |
|
| 248 |
param_reset_data(n); |
| 249 |
|
| 250 |
if (n->type != SCOLS_DATA_NONE) |
| 251 |
return 0; /* already set */ |
| 252 |
|
| 253 |
if (scols_column_get_data_type(n->col)) |
| 254 |
/* use by application defined type */ |
| 255 |
n->type = scols_column_get_data_type(n->col); |
| 256 |
else { |
| 257 |
/* use by JSON defined type, default to string if not specified */ |
| 258 |
switch (n->col->json_type) { |
| 259 |
case SCOLS_JSON_NUMBER: |
| 260 |
n->type = SCOLS_DATA_U64; |
| 261 |
break; |
| 262 |
case SCOLS_JSON_BOOLEAN: |
| 263 |
n->type = SCOLS_DATA_BOOLEAN; |
| 264 |
break; |
| 265 |
case SCOLS_JSON_FLOAT: |
| 266 |
n->type = SCOLS_DATA_FLOAT; |
| 267 |
break; |
| 268 |
case SCOLS_JSON_STRING: |
| 269 |
default: |
| 270 |
n->type = SCOLS_DATA_STRING; |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
DBG(FPARAM, ul_debugobj(n, "holder %s type: %s", n->holder_name, datatype2str(n->type))); |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
static int fetch_holder_data(struct libscols_filter *fltr __attribute__((__unused__)), |
| 280 |
struct filter_param *n, struct libscols_line *ln) |
| 281 |
{ |
| 282 |
const char *data = NULL; |
| 283 |
struct libscols_column *cl = n->col; |
| 284 |
int type = n->type; |
| 285 |
int rc = 0; |
| 286 |
|
| 287 |
if (n->fetched || n->holder != F_HOLDER_COLUMN) |
| 288 |
return 0; |
| 289 |
if (!cl) { |
| 290 |
DBG(FPARAM, ul_debugobj(n, "no column for %s holder", n->holder_name)); |
| 291 |
return -EINVAL; |
| 292 |
} |
| 293 |
DBG(FPARAM, ul_debugobj(n, "fetching %s data", n->holder_name)); |
| 294 |
|
| 295 |
if (fltr->filler_cb && !scols_line_is_filled(ln, cl->seqnum)) { |
| 296 |
DBG(FPARAM, ul_debugobj(n, " by callback")); |
| 297 |
rc = fltr->filler_cb(fltr, ln, cl->seqnum, fltr->filler_data); |
| 298 |
if (rc) |
| 299 |
return rc; |
| 300 |
} |
| 301 |
|
| 302 |
n->fetched = 1; |
| 303 |
|
| 304 |
if (scols_column_has_data_func(cl)) { |
| 305 |
struct libscols_cell *ce = scols_line_get_column_cell(ln, cl); |
| 306 |
|
| 307 |
DBG(FPARAM, ul_debugobj(n, " using datafunc()")); |
| 308 |
if (ce) |
| 309 |
data = cl->datafunc(n->col, ce, cl->datafunc_data); |
| 310 |
if (data) |
| 311 |
rc = param_set_data(n, scols_column_get_data_type(cl), data); |
| 312 |
} else { |
| 313 |
DBG(FPARAM, ul_debugobj(n, " using as string")); |
| 314 |
data = scols_line_get_column_data(ln, n->col); |
| 315 |
rc = param_set_data(n, SCOLS_DATA_STRING, data); |
| 316 |
} |
| 317 |
|
| 318 |
/* cast to the wanted type */ |
| 319 |
if (rc == 0 && type != SCOLS_DATA_NONE) |
| 320 |
rc = cast_param(type, n); |
| 321 |
return rc; |
| 322 |
} |
| 323 |
|
| 324 |
int filter_eval_param(struct libscols_filter *fltr, |
| 325 |
struct libscols_line *ln, |
| 326 |
struct filter_param *n, |
| 327 |
int *status) |
| 328 |
{ |
| 329 |
int rc = 0; |
| 330 |
|
| 331 |
DBG(FLTR, ul_debugobj(fltr, "eval param")); |
| 332 |
|
| 333 |
rc = fetch_holder_data(fltr, n, ln); |
| 334 |
if (n->empty || rc) { |
| 335 |
*status = 0; |
| 336 |
goto done; |
| 337 |
} |
| 338 |
|
| 339 |
switch (n->type) { |
| 340 |
case SCOLS_DATA_STRING: |
| 341 |
*status = n->val.str != NULL && *n->val.str != '\0'; |
| 342 |
break; |
| 343 |
case SCOLS_DATA_U64: |
| 344 |
*status = n->val.num != 0; |
| 345 |
break; |
| 346 |
case SCOLS_DATA_FLOAT: |
| 347 |
*status = n->val.fnum != 0.0; |
| 348 |
break; |
| 349 |
case SCOLS_DATA_BOOLEAN: |
| 350 |
*status = n->val.boolean != false; |
| 351 |
break; |
| 352 |
default: |
| 353 |
rc = -EINVAL; |
| 354 |
break; |
| 355 |
} |
| 356 |
done: |
| 357 |
if (rc) |
| 358 |
DBG(FLTR, ul_debugobj(fltr, "failed eval param [rc=%d]", rc)); |
| 359 |
return rc; |
| 360 |
} |
| 361 |
|
| 362 |
int filter_count_param(struct libscols_filter *fltr, |
| 363 |
struct libscols_line *ln, |
| 364 |
struct libscols_counter *ct) |
| 365 |
{ |
| 366 |
unsigned long long num = 0; |
| 367 |
|
| 368 |
if (ct->func == SCOLS_COUNTER_COUNT) { |
| 369 |
ct->result++; |
| 370 |
return 0; |
| 371 |
} |
| 372 |
|
| 373 |
if (ct->param) { |
| 374 |
int rc; |
| 375 |
|
| 376 |
ct->param->type = SCOLS_DATA_U64; |
| 377 |
rc = fetch_holder_data(fltr, ct->param, ln); |
| 378 |
if (rc) |
| 379 |
return rc; |
| 380 |
|
| 381 |
if (ct->param->empty) |
| 382 |
return -EINVAL; |
| 383 |
|
| 384 |
num = ct->param->val.num; |
| 385 |
} |
| 386 |
|
| 387 |
switch (ct->func) { |
| 388 |
case SCOLS_COUNTER_MAX: |
| 389 |
if (!ct->has_result) |
| 390 |
ct->result = num; |
| 391 |
else if (num > ct->result) |
| 392 |
ct->result = num; |
| 393 |
break; |
| 394 |
case SCOLS_COUNTER_MIN: |
| 395 |
if (!ct->has_result) |
| 396 |
ct->result = num; |
| 397 |
else if (num < ct->result) |
| 398 |
ct->result = num; |
| 399 |
break; |
| 400 |
case SCOLS_COUNTER_SUM: |
| 401 |
ct->result += num; |
| 402 |
break; |
| 403 |
default: |
| 404 |
return -EINVAL; |
| 405 |
} |
| 406 |
|
| 407 |
ct->has_result = 1; |
| 408 |
DBG(FLTR, ul_debugobj(fltr, "counted '%s' [result: %llu]", ct->name, ct->result)); |
| 409 |
return 0; |
| 410 |
} |
| 411 |
|
| 412 |
static int xstrcmp(char *a, char *b) |
| 413 |
{ |
| 414 |
if (!a && !b) |
| 415 |
return 0; |
| 416 |
if (!a && b) |
| 417 |
return -1; |
| 418 |
if (a && !b) |
| 419 |
return 1; |
| 420 |
return strcmp(a, b); |
| 421 |
} |
| 422 |
|
| 423 |
static int string_opers(enum filter_etype oper, struct filter_param *l, |
| 424 |
struct filter_param *r, int *status) |
| 425 |
{ |
| 426 |
switch (oper) { |
| 427 |
case F_EXPR_EQ: |
| 428 |
*status = xstrcmp(l->val.str, r->val.str) == 0; |
| 429 |
break; |
| 430 |
case F_EXPR_NE: |
| 431 |
*status = xstrcmp(l->val.str, r->val.str) != 0; |
| 432 |
break; |
| 433 |
case F_EXPR_LE: |
| 434 |
*status = xstrcmp(l->val.str, r->val.str) <= 0; |
| 435 |
break; |
| 436 |
case F_EXPR_LT: |
| 437 |
*status = xstrcmp(l->val.str, r->val.str) < 0; |
| 438 |
break; |
| 439 |
case F_EXPR_GE: |
| 440 |
*status = xstrcmp(l->val.str, r->val.str) >= 0; |
| 441 |
break; |
| 442 |
case F_EXPR_GT: |
| 443 |
*status = xstrcmp(l->val.str, r->val.str) > 0; |
| 444 |
break; |
| 445 |
case F_EXPR_REG: |
| 446 |
if (!r->re) |
| 447 |
return -EINVAL; |
| 448 |
*status = regexec(r->re, l->val.str ? : "", 0, NULL, 0) == 0; |
| 449 |
break; |
| 450 |
case F_EXPR_NREG: |
| 451 |
if (!r->re) |
| 452 |
return -EINVAL; |
| 453 |
*status = regexec(r->re, l->val.str ? : "", 0, NULL, 0) != 0; |
| 454 |
break; |
| 455 |
default: |
| 456 |
return -EINVAL; |
| 457 |
} |
| 458 |
return 0; |
| 459 |
} |
| 460 |
|
| 461 |
static int u64_opers(enum filter_etype oper, struct filter_param *l, |
| 462 |
struct filter_param *r, int *status) |
| 463 |
{ |
| 464 |
if (l->empty || r->empty) { |
| 465 |
*status = 0; |
| 466 |
return 0; |
| 467 |
} |
| 468 |
|
| 469 |
switch (oper) { |
| 470 |
case F_EXPR_EQ: |
| 471 |
*status = l->val.num == r->val.num; |
| 472 |
break; |
| 473 |
case F_EXPR_NE: |
| 474 |
*status = l->val.num != r->val.num; |
| 475 |
break; |
| 476 |
case F_EXPR_LE: |
| 477 |
*status = l->val.num <= r->val.num; |
| 478 |
break; |
| 479 |
case F_EXPR_LT: |
| 480 |
*status = l->val.num < r->val.num; |
| 481 |
break; |
| 482 |
case F_EXPR_GE: |
| 483 |
*status = l->val.num >= r->val.num; |
| 484 |
break; |
| 485 |
case F_EXPR_GT: |
| 486 |
*status = l->val.num > r->val.num; |
| 487 |
break; |
| 488 |
default: |
| 489 |
return -EINVAL; |
| 490 |
} |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
static int float_opers(enum filter_etype oper, struct filter_param *l, |
| 495 |
struct filter_param *r, int *status) |
| 496 |
{ |
| 497 |
if (l->empty || r->empty) { |
| 498 |
*status = 0; |
| 499 |
return 0; |
| 500 |
} |
| 501 |
|
| 502 |
switch (oper) { |
| 503 |
case F_EXPR_EQ: |
| 504 |
*status = l->val.fnum == r->val.fnum; |
| 505 |
break; |
| 506 |
case F_EXPR_NE: |
| 507 |
*status = l->val.fnum != r->val.fnum; |
| 508 |
break; |
| 509 |
case F_EXPR_LE: |
| 510 |
*status = l->val.fnum <= r->val.fnum; |
| 511 |
break; |
| 512 |
case F_EXPR_LT: |
| 513 |
*status = l->val.fnum < r->val.fnum; |
| 514 |
break; |
| 515 |
case F_EXPR_GE: |
| 516 |
*status = l->val.fnum >= r->val.fnum; |
| 517 |
break; |
| 518 |
case F_EXPR_GT: |
| 519 |
*status = l->val.fnum > r->val.fnum; |
| 520 |
break; |
| 521 |
default: |
| 522 |
return -EINVAL; |
| 523 |
} |
| 524 |
return 0; |
| 525 |
} |
| 526 |
|
| 527 |
static int bool_opers(enum filter_etype oper, struct filter_param *l, |
| 528 |
struct filter_param *r, int *status) |
| 529 |
{ |
| 530 |
if (l->empty || r->empty) { |
| 531 |
*status = 0; |
| 532 |
return 0; |
| 533 |
} |
| 534 |
|
| 535 |
switch (oper) { |
| 536 |
case F_EXPR_EQ: |
| 537 |
*status = l->val.boolean == r->val.boolean; |
| 538 |
break; |
| 539 |
case F_EXPR_NE: |
| 540 |
*status = l->val.boolean != r->val.boolean; |
| 541 |
break; |
| 542 |
case F_EXPR_LE: |
| 543 |
*status = l->val.boolean <= r->val.boolean; |
| 544 |
break; |
| 545 |
case F_EXPR_LT: |
| 546 |
*status = l->val.boolean < r->val.boolean; |
| 547 |
break; |
| 548 |
case F_EXPR_GE: |
| 549 |
*status = l->val.boolean >= r->val.boolean; |
| 550 |
break; |
| 551 |
case F_EXPR_GT: |
| 552 |
*status = l->val.boolean > r->val.boolean; |
| 553 |
break; |
| 554 |
default: |
| 555 |
return -EINVAL; |
| 556 |
} |
| 557 |
return 0; |
| 558 |
} |
| 559 |
|
| 560 |
/* call filter_cast_param() to be sure that param data are ready (fetched from |
| 561 |
* holder, etc.) */ |
| 562 |
int filter_compare_params(struct libscols_filter *fltr __attribute__((__unused__)), |
| 563 |
enum filter_etype oper, |
| 564 |
struct filter_param *l, |
| 565 |
struct filter_param *r, |
| 566 |
int *status) |
| 567 |
{ |
| 568 |
int rc; |
| 569 |
|
| 570 |
if (!l || !r || l->type != r->type) |
| 571 |
return -EINVAL; |
| 572 |
|
| 573 |
*status = 0; |
| 574 |
|
| 575 |
switch (l->type) { |
| 576 |
case SCOLS_DATA_STRING: |
| 577 |
rc = string_opers(oper, l, r, status); |
| 578 |
break; |
| 579 |
case SCOLS_DATA_U64: |
| 580 |
rc = u64_opers(oper, l, r, status); |
| 581 |
break; |
| 582 |
case SCOLS_DATA_FLOAT: |
| 583 |
rc = float_opers(oper, l, r, status); |
| 584 |
break; |
| 585 |
case SCOLS_DATA_BOOLEAN: |
| 586 |
rc = bool_opers(oper, l, r, status); |
| 587 |
break; |
| 588 |
default: |
| 589 |
rc = -EINVAL; |
| 590 |
break; |
| 591 |
} |
| 592 |
|
| 593 |
return rc; |
| 594 |
} |
| 595 |
|
| 596 |
static int string_cast(int type, struct filter_param *n) |
| 597 |
{ |
| 598 |
char *str = n->val.str; |
| 599 |
|
| 600 |
if (type == SCOLS_DATA_STRING) |
| 601 |
return 0; |
| 602 |
|
| 603 |
n->val.str = NULL; |
| 604 |
|
| 605 |
switch (type) { |
| 606 |
case SCOLS_DATA_U64: |
| 607 |
{ |
| 608 |
uint64_t num = 0; |
| 609 |
if (str) { |
| 610 |
int rc = ul_strtou64(str, &num, 10); |
| 611 |
if (rc) |
| 612 |
return rc; |
| 613 |
} |
| 614 |
n->val.num = num; |
| 615 |
break; |
| 616 |
} |
| 617 |
case SCOLS_DATA_FLOAT: |
| 618 |
{ |
| 619 |
long double num = 0; |
| 620 |
if (str) { |
| 621 |
int rc = ul_strtold(str, &num); |
| 622 |
if (rc) |
| 623 |
return rc; |
| 624 |
} |
| 625 |
n->val.fnum = num; |
| 626 |
break; |
| 627 |
} |
| 628 |
case SCOLS_DATA_BOOLEAN: |
| 629 |
{ |
| 630 |
bool x = str && *str |
| 631 |
&& (strcmp(str, "1") == 0 |
| 632 |
|| c_strcasecmp(str, "true") == 0 |
| 633 |
|| rpmatch(str) == RPMATCH_YES); |
| 634 |
n->val.boolean = x; |
| 635 |
break; |
| 636 |
} |
| 637 |
default: |
| 638 |
return -EINVAL; |
| 639 |
} |
| 640 |
|
| 641 |
free(str); |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
static int u64_cast(int type, struct filter_param *n) |
| 646 |
{ |
| 647 |
unsigned long long num = n->val.num; |
| 648 |
|
| 649 |
switch (type) { |
| 650 |
case SCOLS_DATA_STRING: |
| 651 |
n->val.str = NULL; |
| 652 |
if (asprintf(&n->val.str, "%llu", num) <= 0) |
| 653 |
return -ENOMEM; |
| 654 |
break; |
| 655 |
case SCOLS_DATA_U64: |
| 656 |
break; |
| 657 |
case SCOLS_DATA_FLOAT: |
| 658 |
n->val.fnum = num; |
| 659 |
break; |
| 660 |
case SCOLS_DATA_BOOLEAN: |
| 661 |
n->val.boolean = num > 0 ? true : false; |
| 662 |
break; |
| 663 |
default: |
| 664 |
return -EINVAL; |
| 665 |
} |
| 666 |
return 0; |
| 667 |
} |
| 668 |
|
| 669 |
static int float_cast(int type, struct filter_param *n) |
| 670 |
{ |
| 671 |
long double fnum = n->val.fnum; |
| 672 |
|
| 673 |
switch (type) { |
| 674 |
case SCOLS_DATA_STRING: |
| 675 |
n->val.str = NULL; |
| 676 |
if (asprintf(&n->val.str, "%Lg", fnum) <= 0) |
| 677 |
return -ENOMEM; |
| 678 |
break; |
| 679 |
case SCOLS_DATA_U64: |
| 680 |
n->val.num = fnum; |
| 681 |
break; |
| 682 |
case SCOLS_DATA_FLOAT: |
| 683 |
break; |
| 684 |
case SCOLS_DATA_BOOLEAN: |
| 685 |
n->val.boolean = fnum > 0.0 ? true : false; |
| 686 |
break; |
| 687 |
default: |
| 688 |
return -EINVAL; |
| 689 |
} |
| 690 |
return 0; |
| 691 |
} |
| 692 |
|
| 693 |
static int bool_cast(int type, struct filter_param *n) |
| 694 |
{ |
| 695 |
bool x = n->val.boolean; |
| 696 |
|
| 697 |
switch (type) { |
| 698 |
case SCOLS_DATA_STRING: |
| 699 |
n->val.str = NULL; |
| 700 |
if (asprintf(&n->val.str, "%s", x ? "true" : "false") <= 0) |
| 701 |
return -ENOMEM; |
| 702 |
break; |
| 703 |
case SCOLS_DATA_U64: |
| 704 |
n->val.num = x ? 1 : 0; |
| 705 |
break; |
| 706 |
case SCOLS_DATA_FLOAT: |
| 707 |
n->val.fnum = x ? 1.0 : 0.0; |
| 708 |
break; |
| 709 |
case SCOLS_DATA_BOOLEAN: |
| 710 |
break; |
| 711 |
default: |
| 712 |
return -EINVAL; |
| 713 |
} |
| 714 |
return 0; |
| 715 |
} |
| 716 |
|
| 717 |
static int cast_param(int type, struct filter_param *n) |
| 718 |
{ |
| 719 |
int rc; |
| 720 |
int orgtype = n->type; |
| 721 |
|
| 722 |
if (type == orgtype) |
| 723 |
return 0; |
| 724 |
|
| 725 |
if (orgtype == SCOLS_DATA_STRING) |
| 726 |
DBG(FPARAM, ul_debugobj(n, " casting \"%s\" to %s", n->val.str, datatype2str(type))); |
| 727 |
else |
| 728 |
DBG(FPARAM, ul_debugobj(n, " casting %s to %s", datatype2str(orgtype), datatype2str(type))); |
| 729 |
|
| 730 |
switch (orgtype) { |
| 731 |
case SCOLS_DATA_STRING: |
| 732 |
rc = string_cast(type, n); |
| 733 |
break; |
| 734 |
case SCOLS_DATA_U64: |
| 735 |
rc = u64_cast(type, n); |
| 736 |
break; |
| 737 |
case SCOLS_DATA_FLOAT: |
| 738 |
rc = float_cast(type, n); |
| 739 |
break; |
| 740 |
case SCOLS_DATA_BOOLEAN: |
| 741 |
rc = bool_cast(type, n); |
| 742 |
break; |
| 743 |
default: |
| 744 |
rc = -EINVAL; |
| 745 |
break; |
| 746 |
} |
| 747 |
|
| 748 |
if (rc == 0) |
| 749 |
n->type = type; |
| 750 |
|
| 751 |
if (rc) |
| 752 |
DBG(FPARAM, ul_debugobj(n, "cast done [rc=%d]", rc)); |
| 753 |
return rc; |
| 754 |
} |
| 755 |
|
| 756 |
int filter_cast_param(struct libscols_filter *fltr, |
| 757 |
struct libscols_line *ln, |
| 758 |
int type, |
| 759 |
struct filter_param *n, |
| 760 |
struct filter_param **result) |
| 761 |
{ |
| 762 |
int rc; |
| 763 |
int orgtype = n->type; |
| 764 |
|
| 765 |
DBG(FPARAM, ul_debugobj(n, "casting param to %s", datatype2str(type))); |
| 766 |
rc = fetch_holder_data(fltr, n, ln); |
| 767 |
if (rc) |
| 768 |
return rc; |
| 769 |
|
| 770 |
if (type == orgtype) { |
| 771 |
filter_ref_node((struct filter_node *) n); /* caller wants to call filter_unref_node() for the result */ |
| 772 |
*result = n; |
| 773 |
return 0; |
| 774 |
} |
| 775 |
|
| 776 |
*result = copy_param(n); |
| 777 |
if (!*result) |
| 778 |
return -ENOMEM; |
| 779 |
rc = cast_param(type, *result); |
| 780 |
|
| 781 |
DBG(FPARAM, ul_debugobj(n, "cast done [rc=%d]", rc)); |
| 782 |
return rc; |
| 783 |
} |
| 784 |
|
| 785 |
int filter_next_param(struct libscols_filter *fltr, |
| 786 |
struct libscols_iter *itr, struct filter_param **prm) |
| 787 |
{ |
| 788 |
int rc = 1; |
| 789 |
|
| 790 |
if (!fltr || !itr || !prm) |
| 791 |
return -EINVAL; |
| 792 |
*prm = NULL; |
| 793 |
|
| 794 |
if (!itr->head) |
| 795 |
SCOLS_ITER_INIT(itr, &fltr->params); |
| 796 |
if (itr->p != itr->head) { |
| 797 |
SCOLS_ITER_ITERATE(itr, *prm, struct filter_param, pr_params); |
| 798 |
rc = 0; |
| 799 |
} |
| 800 |
|
| 801 |
return rc; |
| 802 |
} |
| 803 |
|
| 804 |
/** |
| 805 |
* scols_filter_assign_column: |
| 806 |
* @fltr: pointer to filter |
| 807 |
* @itr: iterator |
| 808 |
* @name: holder name |
| 809 |
* @col: column |
| 810 |
* |
| 811 |
* Assign @col to filter parameter. The parameter is addressed by @itr or by |
| 812 |
* @name. See scols_filter_next_holder(). |
| 813 |
* |
| 814 |
* Returns: 0, a negative value in case of an error. |
| 815 |
* |
| 816 |
* Since: 2.40 |
| 817 |
*/ |
| 818 |
int scols_filter_assign_column(struct libscols_filter *fltr, |
| 819 |
struct libscols_iter *itr, |
| 820 |
const char *name, struct libscols_column *col) |
| 821 |
{ |
| 822 |
struct filter_param *n = NULL; |
| 823 |
|
| 824 |
if (itr && itr->p) { |
| 825 |
struct list_head *p = IS_ITER_FORWARD(itr) ? |
| 826 |
itr->p->prev : itr->p->next; |
| 827 |
n = list_entry(p, struct filter_param, pr_params); |
| 828 |
} else if (name) { |
| 829 |
struct libscols_iter xitr; |
| 830 |
struct filter_param *x = NULL; |
| 831 |
|
| 832 |
scols_reset_iter(&xitr, SCOLS_ITER_FORWARD); |
| 833 |
while (filter_next_param(fltr, &xitr, &x) == 0) { |
| 834 |
if (x->col |
| 835 |
|| x->holder != F_HOLDER_COLUMN |
| 836 |
|| strcmp(name, x->holder_name) != 0) |
| 837 |
continue; |
| 838 |
n = x; |
| 839 |
break; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
if (n) { |
| 844 |
if (n->col) |
| 845 |
scols_unref_column(n->col); |
| 846 |
|
| 847 |
DBG(FPARAM, ul_debugobj(n, "assign %s to column %s", name, |
| 848 |
scols_column_get_name(col))); |
| 849 |
n->col = col; |
| 850 |
scols_ref_column(col); |
| 851 |
} |
| 852 |
|
| 853 |
return n ? 0 : -EINVAL; |
| 854 |
} |
| 855 |
|
| 856 |
/** |
| 857 |
* scols_filter_next_holder: |
| 858 |
* @fltr: filter instance |
| 859 |
* @itr: a pointer to a struct libscols_iter instance |
| 860 |
* @name: returns the next column name |
| 861 |
* @type: 0 (not implemented yet) |
| 862 |
* |
| 863 |
* Finds the next holder used in the expression and and returns a name via |
| 864 |
* @name. The currently supported holder type is only column name. |
| 865 |
* |
| 866 |
* Returns: 0, a negative value in case of an error, and 1 at the end. |
| 867 |
* |
| 868 |
* Since: 2.40 |
| 869 |
*/ |
| 870 |
int scols_filter_next_holder(struct libscols_filter *fltr, |
| 871 |
struct libscols_iter *itr, |
| 872 |
const char **name, |
| 873 |
int type) |
| 874 |
{ |
| 875 |
struct filter_param *prm = NULL; |
| 876 |
int rc = 0; |
| 877 |
|
| 878 |
*name = NULL; |
| 879 |
if (!type) |
| 880 |
type = F_HOLDER_COLUMN; /* default */ |
| 881 |
|
| 882 |
do { |
| 883 |
rc = filter_next_param(fltr, itr, &prm); |
| 884 |
if (rc == 0 && (int) prm->holder == type) { |
| 885 |
*name = prm->holder_name; |
| 886 |
} |
| 887 |
} while (rc == 0 && !*name); |
| 888 |
|
| 889 |
return rc; |
| 890 |
} |
| 891 |
|
| 892 |
/** |
| 893 |
* scols_filter_has_holder: |
| 894 |
* @fltr: filter instance |
| 895 |
* @name: wanted holder |
| 896 |
* |
| 897 |
* Returns: 0 or 1 |
| 898 |
* |
| 899 |
* Since: 2.42 |
| 900 |
*/ |
| 901 |
int scols_filter_has_holder(struct libscols_filter *fltr, const char *name) |
| 902 |
{ |
| 903 |
struct libscols_iter itr; |
| 904 |
const char *n = NULL; |
| 905 |
|
| 906 |
if (!fltr || !name) |
| 907 |
return 0; |
| 908 |
|
| 909 |
scols_reset_iter(&itr, SCOLS_ITER_FORWARD); |
| 910 |
|
| 911 |
while (scols_filter_next_holder(fltr, &itr, &n, 0) == 0) { |
| 912 |
if (strcmp(n, name) == 0) |
| 913 |
return 1; |
| 914 |
} |
| 915 |
|
| 916 |
return 0; |
| 917 |
} |