From 82f59c760b79589fe8c26b9455c1c6cf694cee20 Mon Sep 17 00:00:00 2001 From: Davidson Francis Date: Wed, 31 Jul 2024 21:13:19 -0300 Subject: [PATCH] Add POSIX Reg Extended tool --- tools/Makefile | 22 +++++ tools/index.html | 232 +++++++++++++++++++++++++++++++++++++++++++++++ tools/regext.c | 100 ++++++++++++++++++++ 3 files changed, 354 insertions(+) create mode 100644 tools/Makefile create mode 100644 tools/index.html create mode 100644 tools/regext.c diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..c5469db --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,22 @@ +# +# Alertik: a tiny 'syslog' server & notification tool for Mikrotik routers. +# This is free and unencumbered software released into the public domain. +# + +CC ?= cc +CC_JS = emcc +CFLAGS += -Wall -Wextra -O2 +CFLAGS_JS += $(CFLAGS) +CFLAGS_JS += -s EXPORTED_FUNCTIONS='["_do_regex", "_malloc", "_free"]' +CFLAGS_JS += -s 'EXPORTED_RUNTIME_METHODS=["stringToUTF8", "UTF8ToString", "setValue"]' + +all: regext.js regext Makefile + +regext.js: regext.c + $(CC_JS) $(CFLAGS_JS) regext.c -o regext.js + +regext: regext.c + $(CC) $(CFLAGS) -DUSE_C regext.c -o regext + +clean: + rm -f regext.js regext.wasm regext *.o diff --git a/tools/index.html b/tools/index.html new file mode 100644 index 0000000..7036242 --- /dev/null +++ b/tools/index.html @@ -0,0 +1,232 @@ + + + + + + POSIX Regex Extended Validator + + + +
+

POSIX Regex Extended Validator

+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+ +
    +
    +
    +
    +
    + +
    + + + + + diff --git a/tools/regext.c b/tools/regext.c new file mode 100644 index 0000000..8a00e2f --- /dev/null +++ b/tools/regext.c @@ -0,0 +1,100 @@ +/* + * Alertik: a tiny 'syslog' server & notification tool for Mikrotik routers. + * This is free and unencumbered software released into the public domain. + */ + +#include +#include +#include +#include + +#ifdef DBG +#define LOG(...) printf(__VA_ARGS__) +#else +#define LOG(...) +#endif + +int do_regex( + const char *re, const char *str, + int32_t *sub_expr, int32_t *rm_so, int32_t *rm_eo, + char *error_msg) +{ + int ret; + regex_t regex; + regmatch_t pmatch[32]; + + if ((ret = regcomp(®ex, re, REG_EXTENDED))) { + regerror(ret, ®ex, error_msg, 128); + regfree(®ex); + LOG("Error: %s\n", error_msg); + return (-1); + } + + if (regexec(®ex, str, 32, pmatch, 0) == REG_NOMATCH) { + LOG("No match!\n"); + return (0); + } + + *sub_expr = regex.re_nsub; + if (!regex.re_nsub) { + regfree(®ex); + LOG("Match without subexpressions!\n"); + return (1); + } + + LOG("N subexpr: %d\n", *sub_expr); + + /* If exists sub-expressions, save them. */ + for (size_t i = 0; i < regex.re_nsub + 1; i++) { + rm_so[i - 0] = pmatch[i].rm_so; + rm_eo[i - 0] = pmatch[i].rm_eo; + LOG("rm_so[i-1] = %d\nrm_eo[i-1] = %d\n", + rm_so[i], + rm_eo[i]); + } + + regfree(®ex); + return (2); +} + + +#ifdef USE_C +int main(int argc, char **argv) +{ + int32_t se, so[32], eo[32]; + char msg[128] = {0}; + + char *re = argv[1]; + char *in = argv[2]; + + if (argc < 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return (1); + } + + printf("Regex : %s\n", re); + printf("input-text: %s\n", in); + + int r = do_regex(re, in, &se, so, eo, msg); + switch (r) { + case -1: + printf("Error, reason: %s\n", msg); + break; + case 0: + printf("No match!\n"); + break; + case 1: + printf("Match without sub-expressions!\n"); + break; + case 2: + printf("Match!!: %.*s\n", eo[0]-so[0], in+so[0]); + printf("Found %d sub-expressions:\n", se); + for (int i = 1; i < se+1; i++) { + printf("$%d: %.*s\n", i, eo[i]-so[i], in+so[i]); + } + break; + } + + return (0); +} +#endif