From c7578b7c34e34919f183df917dfff15473427892 Mon Sep 17 00:00:00 2001 From: Sean Hunt Date: Thu, 9 Apr 2015 10:38:15 -0400 Subject: [PATCH] Add POSIX implementation of regex. This also includes documentation of the regex engine in posixregex.c, because I couldn't think of anywhere better to put it. --- sys/share/cppregex.cpp | 3 +- sys/share/posixregex.c | 92 ++++++++++++++++++++++++++++++++++++++++++ sys/unix/Makefile.src | 8 ++-- 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 sys/share/posixregex.c diff --git a/sys/share/cppregex.cpp b/sys/share/cppregex.cpp index e775d7c6f..a0a1bbdc8 100644 --- a/sys/share/cppregex.cpp +++ b/sys/share/cppregex.cpp @@ -1,11 +1,12 @@ /* NetHack 3.5 cppregex.cpp $NHDT-Date$ $NHDT-Branch$:$NHDT-Revision$ */ -/* NetHack 3.5 cppregex.cpp $Date: 2009/05/06 10:44:33 $ $Revision: 1.4 $ */ /* Copyright (c) Sean Hunt 2015. */ /* NetHack may be freely redistributed. See license for details. */ #include #include +/* nhregex interface documented in sys/share/posixregex.c */ + extern "C" { #include diff --git a/sys/share/posixregex.c b/sys/share/posixregex.c new file mode 100644 index 000000000..17cb2841e --- /dev/null +++ b/sys/share/posixregex.c @@ -0,0 +1,92 @@ +/* NetHack 3.5 posixregex.c $NHDT-Date: 1428590280 2015/04/09 14:38:00 $ $NHDT-Branch: scshunt-regex $:$NHDT-Revision: 1.0 $ */ +/* Copyright (c) Sean Hunt 2015. */ +/* NetHack may be freely redistributed. See license for details. */ + +#include + +#include + +/* The nhregex interface is implemented by several source files. The + * file to be used can be linked in by the build. + * + * The regex standard implemented should be POSIX extended regular + * expressions, see: + * http://pubs.opengroup.org/onlinepubs/009696899/basedefs/xbd_chap09.html + * If an implementation uses an alternate expression format, this should + * be clearly noted; this will result in incompatibility of config files + * when NetHack is compiled with that implementation. + * + * struct nhregex + * The nhregex structure is an opaque structure type containing the + * information about a compiled regular expression. + * + * struct nhregex *regex_init(void) + * Used to create a new instance of the nhregex structure. It is + * uninitialized and can only safely be passed to regex_compile. + * + * boolean regex_compile(const char *s, struct nhregex *re) + * Used to compile s into a regex and store it in re. Returns TRUE if + * successful and FALSE otherwise. re is invalidated regardless of + * success. + * + * const char *regex_error_desc(struct nhregex *re) + * Used to retrieve an error description from an error created involving + * re. Returns NULL if no description can be retrieved. The returned + * string may be a static buffer and so is only valid until the next + * call to regex_error_desc. + * + * boolean regex_match(const char *s, struct nhregex *re) + * Used to determine if s (or any substring) matches the regex compiled + * into re. Only valid if the most recent call to regex_compile on re + * succeeded. + * + * void regex_free(struct nhregex *re) + * Deallocate a regex object. + */ + +struct nhregex { + regex_t re; + int err; +}; + +struct nhregex *regex_init() { + return malloc (sizeof (struct nhregex)); +} + +boolean regex_compile(const char *s, struct nhregex *re) { + if (!re) + return FALSE; + if ((re->err = regcomp(&re->re, s, REG_EXTENDED | REG_NOSUB))) + return FALSE; + return TRUE; +} + +const char *regex_error_desc(struct nhregex *re) { + static char buf[BUFSZ]; + + if (!re || !re->err) + return NULL; + + /* FIXME: Using a static buffer here is not ideal, but avoids memory + * leaks. Consider the allocation more carefully. */ + regerror(re->err, &re->re, buf, BUFSZ); + + return buf; +} + +boolean regex_match(const char *s, struct nhregex *re) { + if (!re) + return FALSE; + + int result; + if ((result = regexec(&re->re, s, 0, NULL, 0))) { + if (result != REG_NOMATCH) + re->err = result; + return FALSE; + } + return TRUE; +} + +void regex_free(struct nhregex *re) { + free(re); +} diff --git a/sys/unix/Makefile.src b/sys/unix/Makefile.src index b64a838c9..79d34fc64 100644 --- a/sys/unix/Makefile.src +++ b/sys/unix/Makefile.src @@ -1,5 +1,5 @@ # NetHack Makefile. -# NetHack 3.5 Makefile.src $NHDT-Date$ $NHDT-Branch$:$NHDT-Revision$ +# NetHack 3.5 Makefile.src $NHDT-Date: 1428590253 2015/04/09 14:37:33 $ $NHDT-Branch: scshunt-regex $:$NHDT-Revision: 1.38 $ # NetHack 3.5 Makefile.src $Date: 2012/01/20 03:41:33 $ $Revision: 1.37 $ # Root of source tree: @@ -338,7 +338,7 @@ HACKCSRC = allmain.c alloc.c apply.c artifact.c attrib.c ball.c bones.c \ # all operating-system-dependent .c (for dependencies and such) SYSCSRC = ../sys/atari/tos.c ../sys/share/pcmain.c ../sys/share/pcsys.c \ - ../sys/share/pctty.c ../sys/share/pcunix.c ../sys/share/random.c \ + ../sys/share/pctty.c ../sys/share/pcunix.c ../sys/share/posixregex.c ../sys/share/random.c \ ../sys/share/ioctl.c ../sys/share/unixtty.c ../sys/unix/unixmain.c \ ../sys/unix/unixunix.c ../sys/unix/unixres.c ../sys/be/bemain.c @@ -391,7 +391,7 @@ HOBJ = $(FIRSTOBJ) allmain.o alloc.o apply.o artifact.o attrib.o ball.o \ minion.o mklev.o mkmap.o \ mkmaze.o mkobj.o mkroom.o mon.o mondata.o monmove.o monstr.o \ mplayer.o mthrowu.o muse.o music.o o_init.o objnam.o options.o \ - pager.o pickup.o pline.o polyself.o potion.o pray.o priest.o \ + pager.o pickup.o pline.o polyself.o posixregex.o potion.o pray.o priest.o \ quest.o questpgr.o read.o rect.o region.o restore.o rip.o rnd.o \ role.o rumors.o save.o shk.o shknam.o sit.o sounds.o sp_lev.o spell.o \ sys.o \ @@ -603,6 +603,8 @@ random.o: ../sys/share/random.c $(HACK_H) $(CC) $(CFLAGS) -c ../sys/share/random.c ioctl.o: ../sys/share/ioctl.c $(HACK_H) ../include/tcap.h $(CC) $(CFLAGS) -c ../sys/share/ioctl.c +posixregex.o: ../sys/share/posixregex.c $(HACK_H) + $(CC) $(CFLAGS) -c ../sys/share/posixregex.c unixtty.o: ../sys/share/unixtty.c $(HACK_H) $(CC) $(CFLAGS) -c ../sys/share/unixtty.c unixmain.o: ../sys/unix/unixmain.c $(HACK_H) ../include/dlb.h -- 2.40.0