From f439236f2c93de58d327593176d4efb44209f537 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Mon, 11 Oct 2021 03:59:35 +0900 Subject: [PATCH] lsfd: (filter) implement !~, an operator for regex unmatching Signed-off-by: Masatake YAMATO --- misc-utils/lsfd-filter.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/misc-utils/lsfd-filter.c b/misc-utils/lsfd-filter.c index 7b525a6179..0202b6a939 100644 --- a/misc-utils/lsfd-filter.c +++ b/misc-utils/lsfd-filter.c @@ -57,6 +57,7 @@ enum op2_type { OP2_GT, OP2_GE, OP2_RE_MATCH, + OP2_RE_UNMATCH, }; struct token { @@ -199,6 +200,7 @@ static bool op2_le (struct node *, struct node *, struct parameter*, struct libs static bool op2_gt (struct node *, struct node *, struct parameter*, struct libscols_line *); static bool op2_ge (struct node *, struct node *, struct parameter*, struct libscols_line *); static bool op2_re_match (struct node *, struct node *, struct parameter*, struct libscols_line *); +static bool op2_re_unmatch (struct node *, struct node *, struct parameter*, struct libscols_line *); static bool op2_check_type_eq_or_bool_or_op(struct parser *, struct op2_class *, struct node *, struct node *); static bool op2_check_type_boolean_or_op (struct parser *, struct op2_class *, struct node *, struct node *); @@ -319,6 +321,11 @@ struct op2_class op2_classes [] = { .is_acceptable = op2_re_match, .check_type = op2_check_type_re, }, + [OP2_RE_UNMATCH] = { + .name = "!~", + .is_acceptable = op2_re_unmatch, + .check_type = op2_check_type_re, + }, }; #define NODE_CLASS(NODE) (&node_classes[(NODE)->type]) @@ -509,6 +516,10 @@ static struct token *parser_read(struct parser *parser) t->type = TOKEN_OP2; t->val.op2 = OP2_NE; break; + } else if (c0 == '~') { + t->type = TOKEN_OP2; + t->val.op2 = OP2_RE_UNMATCH; + break; } parser_ungetc(parser, c0); t->type = TOKEN_OP1; @@ -1161,6 +1172,12 @@ static bool op2_re_match(struct node *left, struct node *right, return (regexec(&VAL(right,re), str, 0, NULL, 0) == 0); } +static bool op2_re_unmatch(struct node *left, struct node *right, + struct parameter *params, struct libscols_line *ln) +{ + return !op2_re_match(left, right, params, ln); +} + static bool op2_check_type_boolean_or_op(struct parser* parser, struct op2_class *op2_class, struct node *left, struct node *right) { -- 2.47.3