From d19851fcdf2ba3c80584a3d7ab4d646c5a26d6da Mon Sep 17 00:00:00 2001 From: Roman Venediktov Date: Mon, 1 Jul 2024 14:53:24 +0200 Subject: [PATCH] Fix regex --- Makefile | 2 +- runtime/runtime.c | 31 +++++++++++++------------------ 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 5156c34a3..0835a1b28 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ remake_runtime: copy_to_build: all remake_runtime mkdir -p $(BUILDDIR) - cp runtime/Std.i runtime/runtime.a stdlib/* src/lamac $(BUILDDIR) + cp -r runtime/Std.i runtime/runtime.a stdlib/* src/lamac $(BUILDDIR) install: all diff --git a/runtime/runtime.c b/runtime/runtime.c index af1da54c8..e8930de06 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -480,40 +480,35 @@ extern void *Lsubstring (aint* args /*void *subj, aint p, aint l*/) { } extern regex_t *Lregexp (char *regexp) { - regex_t *b = (regex_t *)malloc(sizeof(regex_t)); + regex_t *regexp_compiled = (regex_t *)malloc(sizeof(regex_t)); - /* printf ("regexp: %s,\t%x\n", regexp, b); */ + memset(regexp_compiled, 0, sizeof(regex_t)); - memset(b, 0, sizeof(regex_t)); + int res = regcomp(regexp_compiled, regexp, REG_EXTENDED); -// aint n = (aint)re_compile_pattern(regexp, strlen(regexp), b); - aint n = (aint)regcomp(b, regexp, REG_EXTENDED); + // printf("Lregexp: got compiled regexp %p, for string %s\n", regexp_compiled, regexp); -#ifdef DEBUG_PRINT - printf("Lregexp: got compiled regexp %p, for string %s\n", b, regexp); -#endif + if (res != 0) { + char buf[100]; + regerror(res, regexp_compiled, buf, 100); + failure("%", buf); + } - if (n != 0) { failure("%", strerror(n)); }; - - return b; + return regexp_compiled; } extern aint LregexpMatch (regex_t *b, char *s, aint pos) { - aint res; regmatch_t match; ASSERT_BOXED("regexpMatch:1", b); ASSERT_STRING("regexpMatch:2", s); ASSERT_UNBOXED("regexpMatch:3", pos); -// res = re_match(b, s, LEN(TO_DATA(s)->data_header), UNBOX(pos), 0); - res = regexec(b, s + UNBOX(pos), (size_t) 1, &match, 0); + int res = regexec(b, s + UNBOX(pos), (size_t) 1, &match, 0); + + // printf("regexpMatch %p: %s, res=%d so=%d eo=%d\n", b, s + UNBOX(pos), res, match.rm_so, match.rm_eo); if (res == 0 && match.rm_so == 0) { -#ifdef DEBUG_PRINT - printf("Matched!\n"); - printf ("regexpMatch %p: %s, res=%d\n", b, s+UNBOX(pos), match.rm_eo); -#endif return BOX(match.rm_eo); } else { return BOX(-1);