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
|
/*
* 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"
typedef struct _FilterXExpr FilterXExpr;
struct _FilterXExpr
{
guint32 ref_cnt;
const gchar *type;
guint32 ignore_falsy_result:1;
/* evaluate expression */
FilterXObject *(*eval)(FilterXExpr *self);
/* 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);
void (*free_fn)(FilterXExpr *self);
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)
{
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;
}
void filterx_expr_set_location(FilterXExpr *self, CfgLexer *lexer, CFG_LTYPE *lloc);
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);
void filterx_expr_free_method(FilterXExpr *self);
typedef struct _FilterXUnaryOp
{
FilterXExpr super;
FilterXExpr *operand;
} FilterXUnaryOp;
void filterx_unary_op_free_method(FilterXExpr *s);
void filterx_unary_op_init_instance(FilterXUnaryOp *self, FilterXExpr *operand);
typedef struct _FilterXBinaryOp
{
FilterXExpr super;
FilterXExpr *lhs, *rhs;
} FilterXBinaryOp;
void filterx_binary_op_free_method(FilterXExpr *s);
void filterx_binary_op_init_instance(FilterXBinaryOp *self, FilterXExpr *lhs, FilterXExpr *rhs);
gboolean filterx_expr_list_eval(GList *expressions, FilterXObject **result);
#endif
|