Skip to content

Commit

Permalink
Merge pull request #113 from bazsi/app-specific-transformations
Browse files Browse the repository at this point in the history
App specific transformations
  • Loading branch information
alltilla authored May 22, 2024
2 parents 56b42e0 + ebccc0d commit 12b98b4
Show file tree
Hide file tree
Showing 17 changed files with 456 additions and 25 deletions.
2 changes: 2 additions & 0 deletions modules/appmodel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ set (APPMODEL_SOURCES
appmodel-context.c
app-object-generator.c
app-parser-generator.c
app-transform-generator.c
application.c
transformation.c
)

add_module(
Expand Down
4 changes: 4 additions & 0 deletions modules/appmodel/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ modules_appmodel_libappmodel_la_SOURCES = \
modules/appmodel/app-object-generator.h \
modules/appmodel/app-parser-generator.c \
modules/appmodel/app-parser-generator.h \
modules/appmodel/app-transform-generator.c \
modules/appmodel/app-transform-generator.h \
modules/appmodel/application.c \
modules/appmodel/application.h \
modules/appmodel/transformation.c \
modules/appmodel/transformation.h \
modules/appmodel/appmodel-grammar.y

modules_appmodel_libappmodel_la_CPPFLAGS = \
Expand Down
59 changes: 39 additions & 20 deletions modules/appmodel/app-parser-generator.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
* Copyright (c) 2017 Balabit
* Copyright (c) 2017 Balazs Scheidler <balazs.scheidler@balabit.com>
* Copyright (c) 2024 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 program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* This program 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.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU 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
* You should have received a copy of the GNU General Public License
* along with this program; 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
Expand Down Expand Up @@ -98,18 +98,36 @@ _generate_parser(AppParserGenerator *self, const gchar *parser_expr)
" };\n", parser_expr);
}

static void
_generate_filterx(AppParserGenerator *self, const gchar *filterx_expr)
{
if (filterx_expr)
g_string_append_printf(self->block,
" filterx {\n"
" %s\n"
" };\n", filterx_expr);
}

static void
_generate_action(AppParserGenerator *self, Application *app)
{
if (!self->allow_overlaps)
{
g_string_append_printf(self->block,
" rewrite {\n"
" set-tag('.app.%s');\n"
" set('%s' value('.app.name'));\n"
" };\n",
app->super.name, app->super.name);
}
if (self->allow_overlaps)
return;

if (app->filterx_expr)
g_string_append_printf(self->block,
" filterx {\n"
" meta.app_name = '%s';\n"
" };\n",
app->super.name);

else
g_string_append_printf(self->block,
" rewrite {\n"
" set-tag('.app.%s');\n"
" set('%s' value('.app.name'));\n"
" };\n",
app->super.name, app->super.name);
}

static void
Expand Down Expand Up @@ -147,6 +165,7 @@ _generate_application(Application *app, gpointer user_data)

_generate_filter(self, app->filter_expr);
_generate_parser(self, app->parser_expr);
_generate_filterx(self, app->filterx_expr);
_generate_action(self, app);
g_string_append_printf(self->block,
" #End Application %s\n", app->super.name);
Expand Down Expand Up @@ -175,7 +194,7 @@ _generate_framing(AppParserGenerator *self, GlobalConfig *cfg)
g_string_append(self->block, " channel {\n");

g_string_append(self->block,
" filter { tags('.app.doesnotexist'); };\n"
" filterx { false; };\n"
" };\n");
}
else
Expand All @@ -190,7 +209,7 @@ _generate_framing(AppParserGenerator *self, GlobalConfig *cfg)
static void
_generate_empty_frame(AppParserGenerator *self)
{
g_string_append(self->block, "channel { filter { tags('.app.doesnotexist'); }; };");
g_string_append(self->block, "channel { filterx { false; }; };");
}

void
Expand Down
4 changes: 2 additions & 2 deletions modules/appmodel/app-parser-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*
*/

#ifndef APPMODEL_APPPARSER_GENERATOR_H_INCLUDED
#define APPMODEL_APPPARSER_GENERATOR_H_INCLUDED
#ifndef APPMODEL_APP_PARSER_GENERATOR_H_INCLUDED
#define APPMODEL_APP_PARSER_GENERATOR_H_INCLUDED

#include "plugin.h"

Expand Down
121 changes: 121 additions & 0 deletions modules/appmodel/app-transform-generator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2024 Balazs Scheidler <balazs.scheidler@axoflow.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; 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 "app-object-generator.h"
#include "appmodel.h"

/* app-transform() */

typedef struct _AppTransformGenerator
{
AppObjectGenerator super;
const gchar *flavour;
GString *block;
} AppTransformGenerator;

static gboolean
_parse_transforms_arg(AppTransformGenerator *self, CfgArgs *args, const gchar *reference)
{
self->flavour = cfg_args_get(args, "flavour");
if (!self->flavour)
self->flavour = "default";
return TRUE;
}

static gboolean
app_transform_generator_parse_arguments(AppObjectGenerator *s, CfgArgs *args, const gchar *reference)
{
AppTransformGenerator *self = (AppTransformGenerator *) s;
g_assert(args != NULL);

if (!_parse_transforms_arg(self, args, reference))
return FALSE;

if (!app_object_generator_parse_arguments_method(&self->super, args, reference))
return FALSE;

return TRUE;
}

static void
_generate_steps(AppTransformGenerator *self, GList *steps)
{
for (GList *l = steps; l; l = l->next)
{
TransformationStep *step = l->data;
g_string_append_printf(self->block, " # step: %s", step->name);
g_string_append_printf(self->block, " %s\n", step->expr);
}
}

static void
_generate_app_transform(Transformation *transformation, gpointer user_data)
{
AppTransformGenerator *self = (AppTransformGenerator *) user_data;

if (strcmp(transformation->super.instance, self->flavour) != 0)
return;

if (!app_object_generator_is_application_included(&self->super, transformation->super.name))
return;

if (app_object_generator_is_application_excluded(&self->super, transformation->super.name))
return;

g_string_append_printf(self->block, "\n#Start Application %s\n", transformation->super.name);
g_string_append_printf(self->block, " if (meta.app_name == '%s') {\n", transformation->super.name);
for (GList *l = transformation->blocks; l; l = l->next)
{
TransformationBlock *block = l->data;

_generate_steps(self, block->steps);
}
g_string_append(self->block, " };\n");
g_string_append_printf(self->block, "\n#End Application %s\n", transformation->super.name);
}


static void
app_transform_generate_config(AppObjectGenerator *s, GlobalConfig *cfg, GString *result)
{
AppTransformGenerator *self = (AppTransformGenerator *) s;

self->block = result;
g_string_append_printf(result, "## app-transform(flavour(%s))\n"
"channel {\n"
" filterx {\n", self->flavour);
appmodel_iter_transformations(cfg, _generate_app_transform, self);
g_string_append(result, " };\n"
"}");
self->block = NULL;
}

CfgBlockGenerator *
app_transform_generator_new(gint context, const gchar *name)
{
AppTransformGenerator *self = g_new0(AppTransformGenerator, 1);

app_object_generator_init_instance(&self->super, context, name);
self->super.parse_arguments = app_transform_generator_parse_arguments;
self->super.generate_config = app_transform_generate_config;
return &self->super.super;
}
30 changes: 30 additions & 0 deletions modules/appmodel/app-transform-generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Balazs Scheidler <balazs.scheidler@axoflow.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; 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 APPMODEL_APP_TRANSFORM_GENERATOR_H_INCLUDED
#define APPMODEL_APP_TRANSFORM_GENERATOR_H_INCLUDED

#include "plugin.h"

CfgBlockGenerator *app_transform_generator_new(gint context, const gchar *name);

#endif
8 changes: 8 additions & 0 deletions modules/appmodel/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ application_set_parser(Application *self, const gchar *parser_expr)
self->parser_expr = g_strdup(parser_expr);
}

void
application_set_filterx(Application *self, const gchar *filterx_expr)
{
g_free(self->filterx_expr);
self->filterx_expr = g_strdup(filterx_expr);
}

static void
application_free(AppModelObject *s)
{
Application *self = (Application *) s;
g_free(self->filter_expr);
g_free(self->parser_expr);
g_free(self->filterx_expr);
}

Application *
Expand Down
2 changes: 2 additions & 0 deletions modules/appmodel/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ typedef struct _Application
AppModelObject super;
gchar *filter_expr;
gchar *parser_expr;
gchar *filterx_expr;
} Application;

void application_set_filter(Application *self, const gchar *filter_expr);
void application_set_parser(Application *self, const gchar *parser_expr);
void application_set_filterx(Application *self, const gchar *parser_expr);

Application *application_new(const gchar *name, const gchar *topic);

Expand Down
50 changes: 50 additions & 0 deletions modules/appmodel/appmodel-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@
/* INCLUDE_DECLS */

%token KW_APPLICATION
%token KW_TRANSFORMATION
%token KW_TRANSFORM
%token KW_STEP

%type <ptr> application_definition
%type <ptr> transformation_definition

%%

Expand All @@ -60,6 +64,12 @@ start
*instance = $2;
YYACCEPT;
}
| LL_CONTEXT_ROOT transformation_definition
{
appmodel_register_transformation(configuration, $2);
*instance = $2;
YYACCEPT;
}
;


Expand All @@ -84,6 +94,46 @@ application_options
application_option
: KW_FILTER _block_content_context_push LL_BLOCK _block_content_context_pop { application_set_filter($<ptr>0, $3); free($3); }
| KW_PARSER _block_content_context_push LL_BLOCK _block_content_context_pop { application_set_parser($<ptr>0, $3); free($3); }
| KW_FILTERX _block_content_context_push LL_BLOCK _block_content_context_pop { application_set_filterx($<ptr>0, $3); free($3); }
;


transformation_definition
: KW_TRANSFORMATION string '[' string ']'
<ptr>{
$$ = transformation_new($2, $4);
}[transformation]
'{' <ptr>{ $$ = $transformation; } transformation_options '}'
{
$$ = $transformation;
free($2);
free($4);
}
;

transformation_options
: transformation_option semicolons { $<ptr>$ = $<ptr>0; } transformation_options
|
;

/* $0 is Transformation */
transformation_option
: KW_TRANSFORM '[' string ']' '{' { $<ptr>$ = transformation_block_new($3); } transformation_steps '}'
{ free($3); transformation_add_block($<ptr>0, $<ptr>6); }
;

/* $0 is TransformationBlock */
transformation_steps
: transformation_step semicolons { $<ptr>$ = $<ptr>0; } transformation_steps
|
;

/* $0 is TransformationBlock */
transformation_step
: KW_STEP '[' string ']' _block_content_context_push LL_BLOCK _block_content_context_pop
{
transformation_block_add_step($<ptr>0, $3, $6); free($3); free($6);
}
;

/* INCLUDE_RULES */
Expand Down
Loading

0 comments on commit 12b98b4

Please sign in to comment.