[syslog-ng] [PATCH] basicfuncs: Implement a $(substr START LEN STR) template function.
Gergely Nagy
algernon at balabit.hu
Sat Apr 9 13:05:38 CEST 2011
This implements the $(substr) template function - a fairly primitive
one: it does error checking, but there's no error reporting present,
therefore invalid function calls will just get ignored.
Usage should be straightforward.
Reported-By: Jakub Jankowski <shasta at toxcorp.com>
Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
modules/basicfuncs/basic-funcs.c | 46 ++++++++++++++++++++++++++++++++++++++
1 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/modules/basicfuncs/basic-funcs.c b/modules/basicfuncs/basic-funcs.c
index 44b8af3..542c2ec 100644
--- a/modules/basicfuncs/basic-funcs.c
+++ b/modules/basicfuncs/basic-funcs.c
@@ -4,6 +4,9 @@
#include "filter-expr-parser.h"
#include "cfg.h"
+#include <stdlib.h>
+#include <errno.h>
+
static void
tf_echo(LogMessage *msg, gint argc, GString *argv[], GString *result)
{
@@ -19,6 +22,48 @@ tf_echo(LogMessage *msg, gint argc, GString *argv[], GString *result)
TEMPLATE_FUNCTION_SIMPLE(tf_echo);
+static gboolean
+tf_substr_parse_int(const gchar *s, long *d)
+{
+ gchar *endptr;
+ glong val;
+
+ errno = 0;
+ val = strtoll(s, &endptr, 10);
+
+ if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
+ || (errno != 0 && val == 0))
+ return FALSE;
+
+ if (endptr == s || *endptr != '\0')
+ return FALSE;
+
+ *d = val;
+ return TRUE;
+}
+
+static void
+tf_substr(LogMessage *msg, gint argc, GString *argv[], GString *result)
+{
+ glong start, len;
+
+ if (argc != 3)
+ return;
+
+ if (!tf_substr_parse_int (argv[0]->str, &start))
+ return;
+ if (!tf_substr_parse_int (argv[1]->str, &len))
+ return;
+
+ if (start > argv[2]->len ||
+ start + len >= argv[2]->len)
+ return;
+
+ g_string_append_len (result, argv[2]->str + start - 1, len);
+}
+
+TEMPLATE_FUNCTION_SIMPLE(tf_substr);
+
typedef struct _TFCondState
{
FilterExprNode *filter;
@@ -158,6 +203,7 @@ static Plugin basicfuncs_plugins[] =
TEMPLATE_FUNCTION_PLUGIN(tf_echo, "echo"),
TEMPLATE_FUNCTION_PLUGIN(tf_grep, "grep"),
TEMPLATE_FUNCTION_PLUGIN(tf_if, "if"),
+ TEMPLATE_FUNCTION_PLUGIN(tf_substr, "substr"),
};
gboolean
--
1.7.2.5
More information about the syslog-ng
mailing list