File: expr-function.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (398 lines) | stat: -rw-r--r-- 10,426 bytes parent folder | download | duplicates (2)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (c) 2023 Balázs Scheidler <balazs.scheidler@axoflow.com>
 * Copyright (c) 2023 shifter
 *
 * 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.
 *
 */

#include "filterx/expr-function.h"
#include "filterx/filterx-grammar.h"
#include "filterx/filterx-globals.h"
#include "filterx/filterx-eval.h"
#include "filterx/expr-literal.h"
#include "filterx/object-string.h"
#include "filterx/object-null.h"
#include "plugin.h"
#include "cfg.h"

GQuark
filterx_function_error_quark(void)
{
  return g_quark_from_static_string("filterx-function-error-quark");
}

typedef struct _FilterXSimpleFunction
{
  FilterXFunction super;
  FilterXFunctionArgs *args;
  FilterXSimpleFunctionProto function_proto;
} FilterXSimpleFunction;

static GPtrArray *
_simple_function_eval_args(FilterXFunctionArgs *args)
{
  guint64 len = filterx_function_args_len(args);
  if (len == 0)
    return NULL;

  GPtrArray *res = g_ptr_array_new_full(len, (GDestroyNotify) filterx_object_unref);

  for (guint64 i = 0; i < len; i++)
    {
      FilterXObject *obj = filterx_function_args_get_object(args, i);
      if (obj == NULL)
        goto error;

      g_ptr_array_add(res, obj);
    }

  return res;

error:
  g_ptr_array_free(res, TRUE);
  return NULL;
}

static FilterXObject *
_simple_eval(FilterXExpr *s)
{
  FilterXSimpleFunction *self = (FilterXSimpleFunction *) s;

  GPtrArray *args = _simple_function_eval_args(self->args);

  FilterXSimpleFunctionProto f = self->function_proto;

  g_assert(f != NULL);
  FilterXObject *res = f(args);

  if (args != NULL)
    g_ptr_array_free(args, TRUE);
  if (!res)
    filterx_eval_push_error("Function call failed", s, NULL);

  return res;
}

static void
_simple_free(FilterXExpr *s)
{
  FilterXSimpleFunction *self = (FilterXSimpleFunction *) s;
  filterx_function_args_free(self->args);
  filterx_function_free_method(&self->super);
}

FilterXExpr *
filterx_simple_function_new(const gchar *function_name, FilterXFunctionArgs *args,
                            FilterXSimpleFunctionProto function_proto)
{
  FilterXSimpleFunction *self = g_new0(FilterXSimpleFunction, 1);

  filterx_function_init_instance(&self->super, function_name);
  self->super.super.eval = _simple_eval;
  self->super.super.free_fn = _simple_free;
  self->args = args;
  self->function_proto = function_proto;

  return &self->super.super;
}

void
filterx_function_free_method(FilterXFunction *s)
{
  g_free(s->function_name);
  filterx_expr_free_method(&s->super);
}

static void
_function_free(FilterXExpr *s)
{
  FilterXFunction *self = (FilterXFunction *) s;
  filterx_function_free_method(self);
}

void
filterx_function_init_instance(FilterXFunction *s, const gchar *function_name)
{
  filterx_expr_init_instance(&s->super);
  s->function_name = g_strdup(function_name);
  s->super.free_fn = _function_free;
}

struct _FilterXFunctionArgs
{
  GList *positional_args;
  GHashTable *named_args;
};

/* Takes reference of value */
FilterXFunctionArg *
filterx_function_arg_new(const gchar *name, FilterXExpr *value)
{
  FilterXFunctionArg *self = g_new0(FilterXFunctionArg, 1);

  self->name = g_strdup(name);
  self->value = value;

  return self;
}

static void
filterx_function_arg_free(FilterXFunctionArg *self)
{
  g_free(self->name);
  filterx_expr_unref(self->value);
  g_free(self);
}

/* Takes reference of positional_args. */
FilterXFunctionArgs *
filterx_function_args_new(GList *args, GError **error)
{
  FilterXFunctionArgs *self = g_new0(FilterXFunctionArgs, 1);

  self->named_args = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify) filterx_expr_unref);

  gboolean has_named = FALSE;
  for (GList *elem = args; elem; elem = elem->next)
    {
      FilterXFunctionArg *arg = (FilterXFunctionArg *) elem->data;
      if (!arg->name)
        {
          if (has_named)
            {
              g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
                          "cannot set positional argument after a named argument");
              filterx_function_args_free(self);
              self = NULL;
              goto exit;
            }
          self->positional_args = g_list_append(self->positional_args, filterx_expr_ref(arg->value));
        }
      else
        {
          g_hash_table_insert(self->named_args, g_strdup(arg->name), filterx_expr_ref(arg->value));
          has_named = TRUE;
        }
    }

exit:
  g_list_free_full(args, (GDestroyNotify) filterx_function_arg_free);
  return self;
}

guint64
filterx_function_args_len(FilterXFunctionArgs *self)
{
  return g_list_length(self->positional_args);
}

FilterXExpr *
filterx_function_args_get_expr(FilterXFunctionArgs *self, guint64 index)
{
  if (g_list_length(self->positional_args) <= index)
    return NULL;

  return filterx_expr_ref((FilterXExpr *) g_list_nth_data(self->positional_args, index));
}

FilterXObject *
filterx_function_args_get_object(FilterXFunctionArgs *self, guint64 index)
{
  FilterXExpr *expr = filterx_function_args_get_expr(self, index);
  if (!expr)
    return NULL;

  FilterXObject *obj = filterx_expr_eval(expr);
  filterx_expr_unref(expr);
  return obj;
}

static const gchar *
_get_literal_string_from_expr(FilterXExpr *expr, gsize *len)
{
  FilterXObject *obj = NULL;
  const gchar *str = NULL;

  if (!filterx_expr_is_literal(expr))
    goto error;

  obj = filterx_expr_eval(expr);
  if (!obj)
    goto error;

  str = filterx_string_get_value(obj, len);

  /*
   * We can unref both obj and expr, the underlying string will be kept alive as long as the literal expr is alive,
   * which is kept alive in either in the positional or named args container.
   */

error:
  filterx_object_unref(obj);
  return str;
}

const gchar *
filterx_function_args_get_literal_string(FilterXFunctionArgs *self, guint64 index, gsize *len)
{
  FilterXExpr *expr = filterx_function_args_get_expr(self, index);
  if (!expr)
    return NULL;

  const gchar *str = _get_literal_string_from_expr(expr, len);

  filterx_expr_unref(expr);
  return str;
}

gboolean
filterx_function_args_is_literal_null(FilterXFunctionArgs *self, guint64 index)
{
  gboolean is_literal_null = FALSE;

  FilterXExpr *expr = filterx_function_args_get_expr(self, index);
  if (!expr)
    goto error;

  if (!filterx_expr_is_literal(expr))
    goto error;

  FilterXObject *obj = filterx_expr_eval(expr);
  if (!obj)
    goto error;

  is_literal_null = filterx_object_is_type(obj, &FILTERX_TYPE_NAME(null));
  filterx_object_unref(obj);

error:
  filterx_expr_unref(expr);
  return is_literal_null;
}

FilterXExpr *
filterx_function_args_get_named_expr(FilterXFunctionArgs *self, const gchar *name)
{
  return filterx_expr_ref((FilterXExpr *) g_hash_table_lookup(self->named_args, name));
}

FilterXObject *
filterx_function_args_get_named_object(FilterXFunctionArgs *self, const gchar *name, gboolean *exists)
{
  FilterXExpr *expr = filterx_function_args_get_named_expr(self, name);
  *exists = !!expr;
  if (!expr)
    return NULL;

  FilterXObject *obj = filterx_expr_eval(expr);
  filterx_expr_unref(expr);
  return obj;
}

const gchar *
filterx_function_args_get_named_literal_string(FilterXFunctionArgs *self, const gchar *name, gsize *len,
                                               gboolean *exists)
{
  FilterXExpr *expr = filterx_function_args_get_named_expr(self, name);
  *exists = !!expr;
  if (!expr)
    return NULL;

  const gchar *str = _get_literal_string_from_expr(expr, len);

  filterx_expr_unref(expr);
  return str;
}

void
filterx_function_args_free(FilterXFunctionArgs *self)
{
  g_list_free_full(self->positional_args, (GDestroyNotify) filterx_expr_unref);
  g_hash_table_unref(self->named_args);
  g_free(self);
}


static FilterXExpr *
_lookup_simple_function(GlobalConfig *cfg, const gchar *function_name, FilterXFunctionArgs *args)
{
  // Checking filterx builtin functions first
  FilterXSimpleFunctionProto f = filterx_builtin_simple_function_lookup(function_name);
  if (f != NULL)
    {
      return filterx_simple_function_new(function_name, args, f);
    }

  // fallback to plugin lookup
  Plugin *p = cfg_find_plugin(cfg, LL_CONTEXT_FILTERX_SIMPLE_FUNC, function_name);

  if (p == NULL)
    return NULL;

  f = plugin_construct(p);
  if (f == NULL)
    return NULL;

  return filterx_simple_function_new(function_name, args, f);
}

static FilterXExpr *
_lookup_function(GlobalConfig *cfg, const gchar *function_name, FilterXFunctionArgs *args, GError **error)
{
  // Checking filterx builtin functions first
  FilterXFunctionCtor ctor = filterx_builtin_function_ctor_lookup(function_name);

  if (!ctor)
    {
      // fallback to plugin lookup
      Plugin *p = cfg_find_plugin(cfg, LL_CONTEXT_FILTERX_FUNC, function_name);
      if (!p)
        return NULL;
      ctor = plugin_construct(p);
    }

  if (!ctor)
    return NULL;

  FilterXFunction *func_expr = ctor(function_name, args, error);
  if (!func_expr)
    return NULL;
  return &func_expr->super;
}

/* NOTE: takes the reference of "args_list" */
FilterXExpr *
filterx_function_lookup(GlobalConfig *cfg, const gchar *function_name, GList *args_list, GError **error)
{
  FilterXFunctionArgs *args = filterx_function_args_new(args_list, error);
  if (!args)
    return NULL;

  FilterXExpr *expr = _lookup_simple_function(cfg, function_name, args);
  if (expr)
    return expr;

  expr = _lookup_function(cfg, function_name, args, error);
  if (expr)
    return expr;

  if (!(*error))
    g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_FUNCTION_NOT_FOUND, "function not found");
  return NULL;
}