2 * filter-visitor-ir-validate-globbing.c
4 * LTTng filter IR validate globbing
6 * Copyright 2017 - Philippe Proulx <pproulx@efficios.com>
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <common/macros.h>
32 #include "filter-ast.h"
33 #include "filter-parser.h"
34 #include "filter-ir.h"
37 int validate_globbing(struct ir_op
*node
)
44 fprintf(stderr
, "[error] %s: unknown op type\n", __func__
);
48 return validate_globbing(node
->u
.root
.child
);
52 return validate_globbing(node
->u
.unary
.child
);
55 struct ir_op
*left
= node
->u
.binary
.left
;
56 struct ir_op
*right
= node
->u
.binary
.right
;
58 if (left
->op
== IR_OP_LOAD
&& right
->op
== IR_OP_LOAD
&&
59 left
->data_type
== IR_DATA_STRING
&&
60 right
->data_type
== IR_DATA_STRING
) {
62 if (left
->u
.load
.u
.string
.type
== IR_LOAD_STRING_TYPE_GLOB_STAR
&&
63 right
->u
.load
.u
.string
.type
!= IR_LOAD_STRING_TYPE_PLAIN
) {
64 fprintf(stderr
, "[error] Cannot compare two globbing patterns\n");
68 if (right
->u
.load
.u
.string
.type
== IR_LOAD_STRING_TYPE_GLOB_STAR
&&
69 left
->u
.load
.u
.string
.type
!= IR_LOAD_STRING_TYPE_PLAIN
) {
70 fprintf(stderr
, "[error] Cannot compare two globbing patterns\n");
75 if ((left
->op
== IR_OP_LOAD
&& left
->data_type
== IR_DATA_STRING
) ||
76 (right
->op
== IR_OP_LOAD
&& right
->data_type
== IR_DATA_STRING
)) {
77 if ((left
->op
== IR_OP_LOAD
&& left
->u
.load
.u
.string
.type
== IR_LOAD_STRING_TYPE_GLOB_STAR
) ||
78 (right
->op
== IR_OP_LOAD
&& right
->u
.load
.u
.string
.type
== IR_LOAD_STRING_TYPE_GLOB_STAR
)) {
80 if (node
->u
.binary
.type
!= AST_OP_EQ
&&
81 node
->u
.binary
.type
!= AST_OP_NE
) {
82 fprintf(stderr
, "[error] Only the `==` and `!=` operators are allowed with a globbing pattern\n");
88 ret
= validate_globbing(left
);
93 return validate_globbing(right
);
96 ret
= validate_globbing(node
->u
.logical
.left
);
99 return validate_globbing(node
->u
.logical
.right
);
104 * This function recursively validates that:
106 * 1. When there's a binary operation between two literal strings,
107 * if one of them has the IR_LOAD_STRING_TYPE_GLOB_STAR type,
108 * the other one has the IR_LOAD_STRING_TYPE_PLAIN type.
110 * In other words, you cannot compare two globbing patterns, except
111 * for two globbing patterns with only a star at the end for backward
112 * compatibility reasons.
114 * 2. When there's a binary operation between two literal strings, if
115 * one of them is a (full) star globbing pattern, the binary
116 * operation is either == or !=.
119 int filter_visitor_ir_validate_globbing(struct filter_parser_ctx
*ctx
)
121 return validate_globbing(ctx
->ir_root
);