forked from axoflow/axosyslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterx-expr.h
211 lines (177 loc) · 5.84 KB
/
filterx-expr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2023 Balazs Scheidler <balazs.scheidler@axoflow.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#ifndef FILTERX_EXPR_H_INCLUDED
#define FILTERX_EXPR_H_INCLUDED
#include "filterx-object.h"
#include "cfg-lexer.h"
#include "stats/stats-counter.h"
struct _FilterXExpr
{
StatsCounterItem *eval_count;
/* evaluate expression */
FilterXObject *(*eval)(FilterXExpr *self);
/* not thread-safe */
guint32 ref_cnt;
guint32 ignore_falsy_result:1, suppress_from_trace:1, inited:1;
/* not to be used except for FilterXMessageRef, replace any cached values
* with the unmarshaled version */
void (*_update_repr)(FilterXExpr *self, FilterXObject *new_repr);
/* assign a new value to this expr */
gboolean (*assign)(FilterXExpr *self, FilterXObject *new_value);
/* is the expression set? */
gboolean (*is_set)(FilterXExpr *self);
/* unset the expression */
gboolean (*unset)(FilterXExpr *self);
gboolean (*init)(FilterXExpr *self, GlobalConfig *cfg);
void (*deinit)(FilterXExpr *self, GlobalConfig *cfg);
void (*free_fn)(FilterXExpr *self);
const gchar *type;
CFG_LTYPE *lloc;
gchar *expr_text;
};
/*
* Evaluate the expression and return the result as a FilterXObject. The
* result can either be a
*
* 1) raw representation (e.g. a marshalled series of bytes + syslog-ng
* type hint encapsulated into a FilterXMessageValue)
*
* 2) typed representation (e.g. a demarshalled object, something other
* than FilterXMessageValue, like FilterXJSON)
*
* If the caller is not ok with handling the raw representation, just use
* filterx_expr_eval_typed() which will unmarshall any values before
* returning them.
*/
static inline FilterXObject *
filterx_expr_eval(FilterXExpr *self)
{
stats_counter_inc(self->eval_count);
return self->eval(self);
}
/*
* Evaluate the expression and return the result as a typed FilterXObject.
* This function will call filterx_expr_eval() and then unmarshal the result
* so the result is always a typed object.
*/
static inline FilterXObject *
filterx_expr_eval_typed(FilterXExpr *self)
{
FilterXObject *result = filterx_expr_eval(self);
if (!result)
return NULL;
FilterXObject *unmarshalled = filterx_object_unmarshal(result);
if (!unmarshalled)
{
filterx_object_unref(result);
return NULL;
}
if (result == unmarshalled)
{
filterx_object_unref(unmarshalled);
return result;
}
filterx_object_unref(result);
if (self->_update_repr)
self->_update_repr(self, unmarshalled);
return unmarshalled;
}
static inline gboolean
filterx_expr_assign(FilterXExpr *self, FilterXObject *new_value)
{
if (self->assign)
return self->assign(self, new_value);
return FALSE;
}
static inline gboolean
filterx_expr_is_set(FilterXExpr *self)
{
if (self->is_set)
return self->is_set(self);
return FALSE;
}
static inline gboolean
filterx_expr_unset(FilterXExpr *self)
{
if (self->unset)
return self->unset(self);
return FALSE;
}
static inline gboolean
filterx_expr_unset_available(FilterXExpr *self)
{
return self->unset != NULL;
}
void filterx_expr_set_location(FilterXExpr *self, CfgLexer *lexer, CFG_LTYPE *lloc);
void filterx_expr_set_location_with_text(FilterXExpr *self, CfgLexer *lexer, CFG_LTYPE *lloc, const gchar *text);
EVTTAG *filterx_expr_format_location_tag(FilterXExpr *self);
void filterx_expr_init_instance(FilterXExpr *self);
FilterXExpr *filterx_expr_new(void);
FilterXExpr *filterx_expr_ref(FilterXExpr *self);
void filterx_expr_unref(FilterXExpr *self);
gboolean filterx_expr_init_method(FilterXExpr *self, GlobalConfig *cfg);
void filterx_expr_deinit_method(FilterXExpr *self, GlobalConfig *cfg);
void filterx_expr_free_method(FilterXExpr *self);
static inline gboolean
filterx_expr_init(FilterXExpr *self, GlobalConfig *cfg)
{
if (!self || self->inited)
return TRUE;
if (!self->init || self->init(self, cfg))
{
self->inited = TRUE;
return TRUE;
}
return FALSE;
}
static inline void
filterx_expr_deinit(FilterXExpr *self, GlobalConfig *cfg)
{
if (!self || !self->inited)
return;
if (self->deinit)
self->deinit(self, cfg);
self->inited = FALSE;
}
typedef struct _FilterXUnaryOp
{
FilterXExpr super;
FilterXExpr *operand;
const gchar *name;
} FilterXUnaryOp;
gboolean filterx_unary_op_init_method(FilterXExpr *s, GlobalConfig *cfg);
void filterx_unary_op_deinit_method(FilterXExpr *s, GlobalConfig *cfg);
void filterx_unary_op_free_method(FilterXExpr *s);
void filterx_unary_op_init_instance(FilterXUnaryOp *self, const gchar *name, FilterXExpr *operand);
typedef struct _FilterXBinaryOp
{
FilterXExpr super;
FilterXExpr *lhs, *rhs;
const gchar *name;
} FilterXBinaryOp;
gboolean filterx_binary_op_init_method(FilterXExpr *s, GlobalConfig *cfg);
void filterx_binary_op_deinit_method(FilterXExpr *s, GlobalConfig *cfg);
void filterx_binary_op_free_method(FilterXExpr *s);
void filterx_binary_op_init_instance(FilterXBinaryOp *self, const gchar *name, FilterXExpr *lhs, FilterXExpr *rhs);
#endif