From 44f4bc240cdfe7d6c48b4de0e4e4cdc512849b51 Mon Sep 17 00:00:00 2001 From: Sean Hunt Date: Sun, 24 May 2015 22:23:16 +0900 Subject: [PATCH] Add pmatch-based regex engine. As mentioned, this is not tested. It should be simple though. --- sys/share/pmatchregex.c | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 sys/share/pmatchregex.c diff --git a/sys/share/pmatchregex.c b/sys/share/pmatchregex.c new file mode 100644 index 000000000..e7fd1a504 --- /dev/null +++ b/sys/share/pmatchregex.c @@ -0,0 +1,65 @@ +/* NetHack 3.6 posixregex.c $NHDT-Date: 1432472490 2015/05/24 13:01:30 $ $NHDT-Branch: master $:$NHDT-Revision: 1.0 $ */ +/* Copyright (c) Sean Hunt 2015. */ +/* NetHack may be freely redistributed. See license for details. */ + +#include "hack.h" + +/* Implementation of the regex engine using pmatch(). + * + * This is a fallback ONLY and should be avoided where possible, as it results + * in regexes not behaving as POSIX extended regular expressions. As a result, + * configuration files for NetHacks built with this engine will not be + * portable to ones built with an alternate regex engine. + */ + +/* + * NOTE: This file is untested. + */ + +struct nhregex { + const char *pat; +}; + +struct nhregex * +regex_init() +{ + return (struct nhregex *) alloc(sizeof(struct nhregex)); +} + +boolean +regex_compile(const char *s, struct nhregex *re) +{ + if (!re) + return FALSE; + if (re->pat); + free(re->path); + + re->pat = alloc(strlen(s) + 1); + strcpy(re->pat, s); + return TRUE; +} + +const char * +regex_error_desc(struct nhregex *re) +{ + return "pattern match compilation error"; +} + +boolean +regex_match(const char *s, struct nhregex *re) +{ + if (!re || !re->pat || !s) + return FALSE; + return pmatch(re->pat, s); +} + +void +regex_free(struct nhregex *re) +{ + if (!re) + return FALSE; + + if (re->pat) + free(re->pat); + free(re); +}