1 /*
2 * Copyright (c) 2002-2014 Balabit
3 * Copyright (c) 1998-2014 Balázs Scheidler
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * As an additional exemption you are allowed to compile & link against the
20 * OpenSSL libraries as published by the OpenSSL project. See the file
21 * COPYING for details.
22 *
23 */
24
25 #include "template/simple-function.h"
26 #include "template/templates.h"
27 #include "scratch-buffers.h"
28
29 void
log_template_append_format_recursive(LogTemplate * self,const LogTemplateInvokeArgs * args,GString * result)30 log_template_append_format_recursive(LogTemplate *self, const LogTemplateInvokeArgs *args, GString *result)
31 {
32 log_template_append_format_with_context(self,
33 args->messages, args->num_messages,
34 args->options, result);
35 }
36
37
38 /* simple template functions which take templates as arguments */
39
40 gboolean
tf_simple_func_prepare(LogTemplateFunction * self,gpointer s,LogTemplate * parent,gint argc,gchar * argv[],GError ** error)41 tf_simple_func_prepare(LogTemplateFunction *self, gpointer s, LogTemplate *parent, gint argc, gchar *argv[],
42 GError **error)
43 {
44 TFSimpleFuncState *state = (TFSimpleFuncState *) s;
45 gint i;
46
47 g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
48 state->argv_templates = g_malloc(sizeof(LogTemplate *) * (argc - 1));
49
50 /* NOTE: the argv argument contains the function name as argv[0],
51 * but the LogTemplate array doesn't. Thus the index is shifted by
52 * one. */
53 for (i = 0; i < argc - 1; i++)
54 {
55 state->argv_templates[i] = log_template_new(parent->cfg, NULL);
56 log_template_set_escape(state->argv_templates[i], parent->escape);
57 if (!log_template_compile(state->argv_templates[i], argv[i + 1], error))
58 {
59 state->argc = i + 1;
60 goto error;
61 }
62 }
63 state->argc = argc - 1;
64 return TRUE;
65 error:
66 return FALSE;
67 }
68
69 void
tf_simple_func_eval(LogTemplateFunction * self,gpointer s,LogTemplateInvokeArgs * args)70 tf_simple_func_eval(LogTemplateFunction *self, gpointer s, LogTemplateInvokeArgs *args)
71 {
72 TFSimpleFuncState *state = (TFSimpleFuncState *) s;
73 gint i;
74
75 g_assert(state->argc <= TEMPLATE_INVOKE_MAX_ARGS);
76 for (i = 0; i < state->argc; i++)
77 {
78 args->argv[i] = scratch_buffers_alloc();
79 log_template_append_format_recursive(state->argv_templates[i], args, args->argv[i]);
80 }
81 }
82
83 void
tf_simple_func_call(LogTemplateFunction * self,gpointer s,const LogTemplateInvokeArgs * args,GString * result)84 tf_simple_func_call(LogTemplateFunction *self, gpointer s, const LogTemplateInvokeArgs *args, GString *result)
85 {
86 TFSimpleFunc simple_func = (TFSimpleFunc) self->arg;
87 TFSimpleFuncState *state = (TFSimpleFuncState *) s;
88
89 simple_func(args->messages[args->num_messages-1], state->argc, (GString **) args->argv, result);
90 }
91
92 void
tf_simple_func_free_state(gpointer s)93 tf_simple_func_free_state(gpointer s)
94 {
95 TFSimpleFuncState *state = (TFSimpleFuncState *) s;
96 gint i;
97
98 for (i = 0; i < state->argc; i++)
99 {
100 if (state->argv_templates[i])
101 log_template_unref(state->argv_templates[i]);
102 }
103 g_free(state->argv_templates);
104 }
105