Clean-up: consumer.hpp: coding style indentation fix
[lttng-tools.git] / src / common / filter / filter-visitor-ir-validate-string.cpp
1 /*
2 * filter-visitor-ir-validate-string.c
3 *
4 * LTTng filter IR validate string
5 *
6 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * SPDX-License-Identifier: LGPL-2.1-only
9 *
10 */
11
12 #include "filter-ast.hpp"
13 #include "filter-ir.hpp"
14 #include "filter-parser.hpp"
15
16 #include <common/compat/errno.hpp>
17 #include <common/macros.hpp>
18
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 enum parse_char_result {
26 PARSE_CHAR_UNKNOWN = -2,
27 PARSE_CHAR_WILDCARD = -1,
28 PARSE_CHAR_NORMAL = 0,
29 };
30
31 static enum parse_char_result parse_char(const char **p)
32 {
33 switch (**p) {
34 case '\\':
35 (*p)++;
36 switch (**p) {
37 case '\\':
38 case '*':
39 return PARSE_CHAR_NORMAL;
40 default:
41 return PARSE_CHAR_UNKNOWN;
42 }
43 case '*':
44 return PARSE_CHAR_WILDCARD;
45 default:
46 return PARSE_CHAR_NORMAL;
47 }
48 }
49
50 static int validate_string(struct ir_op *node)
51 {
52 switch (node->op) {
53 case IR_OP_UNKNOWN:
54 default:
55 fprintf(stderr, "[error] %s: unknown op type\n", __func__);
56 return -EINVAL;
57
58 case IR_OP_ROOT:
59 return validate_string(node->u.root.child);
60 case IR_OP_LOAD:
61 {
62 int ret = 0;
63
64 if (node->data_type == IR_DATA_STRING) {
65 const char *str;
66
67 LTTNG_ASSERT(node->u.load.u.string.value);
68 str = node->u.load.u.string.value;
69
70 for (;;) {
71 enum parse_char_result res;
72
73 if (!(*str)) {
74 break;
75 }
76
77 res = parse_char(&str);
78 str++;
79
80 switch (res) {
81 case PARSE_CHAR_UNKNOWN:
82 ret = -EINVAL;
83 fprintf(stderr, "Unsupported escape character detected.\n");
84 goto end_load;
85 case PARSE_CHAR_NORMAL:
86 default:
87 break;
88 }
89 }
90 }
91 end_load:
92 return ret;
93 }
94 case IR_OP_UNARY:
95 return validate_string(node->u.unary.child);
96 case IR_OP_BINARY:
97 {
98 int ret = validate_string(node->u.binary.left);
99
100 if (ret)
101 return ret;
102 return validate_string(node->u.binary.right);
103 }
104 case IR_OP_LOGICAL:
105 {
106 int ret;
107
108 ret = validate_string(node->u.logical.left);
109 if (ret)
110 return ret;
111 return validate_string(node->u.logical.right);
112 }
113 }
114 }
115
116 int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx)
117 {
118 return validate_string(ctx->ir_root);
119 }
This page took 0.031019 seconds and 4 git commands to generate.