From 01d04adecba58ccc4c461b5893c9f183378bd219 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 5 Sep 2023 10:57:42 +0200 Subject: [PATCH] libsmartcols: (filter) implement logical operators Signed-off-by: Karel Zak --- libsmartcols/src/filter-expr.c | 30 ++++++++++++++++++++++++++++++ libsmartcols/src/filter-param.c | 1 - libsmartcols/src/filter.c | 2 -- libsmartcols/src/smartcolsP.h | 3 ++- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/libsmartcols/src/filter-expr.c b/libsmartcols/src/filter-expr.c index e34365d165..59e5df7888 100644 --- a/libsmartcols/src/filter-expr.c +++ b/libsmartcols/src/filter-expr.c @@ -85,3 +85,33 @@ void filter_dump_expr(struct ul_jsonwrt *json, struct filter_expr *n) ul_jsonwrt_object_close(json); } + +int filter_eval_expr(struct libscols_filter *fltr, struct filter_expr *n, + struct libscols_line *ln, int *status) +{ + int rc = 0; + enum filter_etype oper = n->type; + + /* logical operators */ + switch (oper) { + case F_EXPR_AND: + rc = filter_eval_node(fltr, n->left, ln, status); + if (rc == 0 && *status) + rc = filter_eval_node(fltr, n->right, ln, status); + return rc; + case F_EXPR_OR: + rc = filter_eval_node(fltr, n->left, ln, status); + if (rc == 0 && !*status) + rc = filter_eval_node(fltr, n->right, ln, status); + return rc; + case F_EXPR_NEG: + rc = filter_eval_node(fltr, n->right, ln, status); + if (rc == 0) + *status = !*status; + return rc; + default: + break; + } + + return rc; +} diff --git a/libsmartcols/src/filter-param.c b/libsmartcols/src/filter-param.c index f67efa509a..a5ef6abc27 100644 --- a/libsmartcols/src/filter-param.c +++ b/libsmartcols/src/filter-param.c @@ -108,7 +108,6 @@ int filter_eval_param(struct libscols_filter *fltr __attribute__((__unused__)), break; } - DBG(FLTR, ul_debugobj(fltr, "eval param [rc=%d, status=%d]", rc, *status)); return rc; } diff --git a/libsmartcols/src/filter.c b/libsmartcols/src/filter.c index 4a1e56b28a..3bb2e3d3e7 100644 --- a/libsmartcols/src/filter.c +++ b/libsmartcols/src/filter.c @@ -195,10 +195,8 @@ int filter_eval_node(struct libscols_filter *fltr, struct filter_node *n, switch (n->type) { case F_NODE_PARAM: return filter_eval_param(fltr, (struct filter_param *) n, ln, status); - /* TODO case F_NODE_EXPR: return filter_eval_expr(fltr, (struct filter_expr *) n, ln, status); - */ default: break; } diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 0407b089a0..93fc6c633f 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -574,7 +574,8 @@ int filter_next_param(struct libscols_filter *fltr, /* expr */ void filter_free_expr(struct filter_expr *n); void filter_dump_expr(struct ul_jsonwrt *json, struct filter_expr *n); - +int filter_eval_expr(struct libscols_filter *fltr, struct filter_expr *n, + struct libscols_line *ln, int *status); /* required by parser */ struct filter_node *filter_new_param(struct libscols_filter *filter, -- 2.47.3