From: Sean Hunt Date: Sun, 24 May 2015 13:23:17 +0000 (+0900) Subject: Use the common regex engine in more places. X-Git-Tag: NetHack-3.6.0_RC01~377 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=84d63e169b3fb9c354cb0272622364a741784e17;p=nethack Use the common regex engine in more places. In particular, in autopickup_exceptions and user sounds. --- diff --git a/include/config.h b/include/config.h index 3533f0bfb..2d06c044e 100644 --- a/include/config.h +++ b/include/config.h @@ -1,4 +1,4 @@ -/* NetHack 3.6 config.h $NHDT-Date: 1428084467 2015/04/03 18:07:47 $ $NHDT-Branch: scshunt-regex $:$NHDT-Revision: 1.76 $ */ +/* NetHack 3.6 config.h $NHDT-Date: 1432472662 2015/05/24 13:04:22 $ $NHDT-Branch: master $:$NHDT-Revision: 1.85 $ */ /* NetHack 3.6 config.h $Date: 2012/01/27 20:15:26 $ $Revision: 1.37 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -91,7 +91,6 @@ # endif #ifndef NOUSER_SOUNDS # define USER_SOUNDS /* Use sounds */ -/* # define USER_SOUNDS_REGEX */ /* Use regexps for sound message matches */ #endif # define USE_XPM /* Use XPM format for images (required) */ # define GRAPHIC_TOMBSTONE /* Use graphical tombstone (rip.ppm) */ diff --git a/include/decl.h b/include/decl.h index ddfe35693..fa2e34f21 100644 --- a/include/decl.h +++ b/include/decl.h @@ -1,4 +1,4 @@ -/* NetHack 3.6 decl.h $NHDT-Date: 1425081976 2015/02/28 00:06:16 $ $NHDT-Branch: master $:$NHDT-Revision: 1.50 $ */ +/* NetHack 3.6 decl.h $NHDT-Date: 1432472662 2015/05/24 13:04:22 $ $NHDT-Branch: master $:$NHDT-Revision: 1.73 $ */ /* NetHack 3.6 decl.h $Date: 2011/12/29 20:06:27 $ $Revision: 1.44 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -389,7 +389,8 @@ E char *fqn_prefix_names[PREFIX_COUNT]; E NEARDATA struct savefile_info sfcap, sfrestinfo, sfsaveinfo; struct autopickup_exception { - char *pattern; + struct nhregex *regex; + const char *pattern; boolean grab; struct autopickup_exception *next; }; diff --git a/src/options.c b/src/options.c index c98c3d2cd..f969e07cb 100644 --- a/src/options.c +++ b/src/options.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 options.c $NHDT-Date: 1431192763 2015/05/09 17:32:43 $ $NHDT-Branch: master $:$NHDT-Revision: 1.197 $ */ +/* NetHack 3.6 options.c $NHDT-Date: 1432472660 2015/05/24 13:04:20 $ $NHDT-Branch: master $:$NHDT-Revision: 1.198 $ */ /* NetHack 3.6 options.c $Date: 2012/04/09 02:56:30 $ $Revision: 1.153 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -4547,8 +4547,15 @@ const char *mapping; : &iflags.autopickup_exceptions[AP_LEAVE]; ape = (struct autopickup_exception *) alloc( sizeof(struct autopickup_exception)); - ape->pattern = (char *) alloc(textsize + 1); - Strcpy(ape->pattern, text2); + ape->regex = regex_init(); + if (!regex_compile(text2, ape->regex)) { + raw_print("regex error in AUTOPICKUP_EXCEPTION"); + regex_free(ape->regex); + free(ape); + return 0; + } + ape->pattern = alloc(strlen(text2) + 1); + strcpy(ape->pattern, text2); ape->grab = grab; ape->next = *apehead; *apehead = ape; @@ -4574,6 +4581,7 @@ struct autopickup_exception *whichape; prev->next = ape; else iflags.autopickup_exceptions[chain] = ape; + regex_free(freeape->regex); free(freeape->pattern); free(freeape); } else { @@ -4613,6 +4621,7 @@ free_autopickup_exceptions() for (pass = AP_LEAVE; pass <= AP_GRAB; ++pass) { while ((ape = iflags.autopickup_exceptions[pass]) != 0) { + regex_free(ape->regex); free(ape->pattern); iflags.autopickup_exceptions[pass] = ape->next; free(ape); diff --git a/src/pickup.c b/src/pickup.c index eb8f9dc6d..38b8d61c7 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 pickup.c $NHDT-Date: 1431192768 2015/05/09 17:32:48 $ $NHDT-Branch: master $:$NHDT-Revision: 1.153 $ */ +/* NetHack 3.6 pickup.c $NHDT-Date: 1432472661 2015/05/24 13:04:21 $ $NHDT-Branch: master $:$NHDT-Revision: 1.154 $ */ /* NetHack 3.6 pickup.c $Date: 2012/02/16 03:01:38 $ $Revision: 1.123 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -689,7 +689,7 @@ boolean grab; /* forced pickup, rather than forced leave behind? */ (grab) ? iflags.autopickup_exceptions[AP_GRAB] : iflags.autopickup_exceptions[AP_LEAVE]; while (ape) { - if (pmatch(ape->pattern, objdesc)) + if (regex_match(objdesc, ape->regex)) return TRUE; ape = ape->next; } diff --git a/src/sounds.c b/src/sounds.c index ba8b518c4..50c281d5d 100644 --- a/src/sounds.c +++ b/src/sounds.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 sounds.c $NHDT-Date: 1431192768 2015/05/09 17:32:48 $ $NHDT-Branch: master $:$NHDT-Revision: 1.60 $ */ +/* NetHack 3.6 sounds.c $NHDT-Date: 1432472661 2015/05/24 13:04:21 $ $NHDT-Branch: master $:$NHDT-Revision: 1.61 $ */ /* NetHack 3.6 sounds.c $Date: 2012/03/10 02:49:08 $ $Revision: 1.39 $ */ /* Copyright (c) 1989 Janet Walz, Mike Threepoint */ /* NetHack may be freely redistributed. See license for details. */ @@ -6,10 +6,7 @@ #include "hack.h" #ifdef USER_SOUNDS -#ifdef USER_SOUNDS_REGEX -#include -#include -#endif +#include #endif STATIC_DCL boolean FDECL(mon_is_gecko, (struct monst *)); @@ -1075,11 +1072,7 @@ dochat() extern void FDECL(play_usersound, (const char *, int)); typedef struct audio_mapping_rec { -#ifdef USER_SOUNDS_REGEX - struct re_pattern_buffer regex; -#else - char *pattern; -#endif + struct nhregex *regex; char *filename; int volume; struct audio_mapping_rec *next; @@ -1101,7 +1094,6 @@ const char *mapping; if (sscanf(mapping, "MESG \"%255[^\"]\"%*[\t ]\"%255[^\"]\" %d", text, filename, &volume) == 3) { - const char *err; audio_mapping *new_map; if (strlen(sounddir) + strlen(filename) > 254) { @@ -1112,26 +1104,14 @@ const char *mapping; if (can_read_file(filespec)) { new_map = (audio_mapping *) alloc(sizeof(audio_mapping)); -#ifdef USER_SOUNDS_REGEX - new_map->regex.translate = 0; - new_map->regex.fastmap = 0; - new_map->regex.buffer = 0; - new_map->regex.allocated = 0; - new_map->regex.regs_allocated = REGS_FIXED; -#else - new_map->pattern = dupstr(text); -#endif + new_map->regex = regex_init(); new_map->filename = dupstr(filespec); new_map->volume = volume; new_map->next = soundmap; -#ifdef USER_SOUNDS_REGEX - err = re_compile_pattern(text, strlen(text), &new_map->regex); -#else - err = 0; -#endif - if (err) { - raw_print(err); + if (!regex_compile(text, new_map->regex)) { + raw_print(regex_error_desc(new_map->regex)); + regex_free(new_map->regex); free(new_map->filename); free(new_map); return 0; @@ -1158,11 +1138,7 @@ const char *msg; audio_mapping *cursor = soundmap; while (cursor) { -#ifdef USER_SOUNDS_REGEX - if (re_search(&cursor->regex, msg, strlen(msg), 0, 9999, 0) >= 0) { -#else - if (pmatch(cursor->pattern, msg)) { -#endif + if (regex_match(msg, cursor->regex)) { play_usersound(cursor->filename, cursor->volume); } cursor = cursor->next; diff --git a/sys/share/posixregex.c b/sys/share/posixregex.c index ea7dbd5fe..c4deab208 100644 --- a/sys/share/posixregex.c +++ b/sys/share/posixregex.c @@ -85,7 +85,7 @@ regex_match(const char *s, struct nhregex *re) { int result; - if (!re) + if (!re || !s) return FALSE; if ((result = regexec(&re->re, s, 0, (genericptr_t) 0, 0))) { diff --git a/util/makedefs.c b/util/makedefs.c index 9ea22bc1f..92ee31db9 100644 --- a/util/makedefs.c +++ b/util/makedefs.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 makedefs.c $NHDT-Date: 1432448606 2015/05/24 06:23:26 $ $NHDT-Branch: master $:$NHDT-Revision: 1.95 $ */ +/* NetHack 3.6 makedefs.c $NHDT-Date: 1432472661 2015/05/24 13:04:21 $ $NHDT-Branch: master $:$NHDT-Revision: 1.95 $ */ /* NetHack 3.6 makedefs.c $Date: 2012/01/15 09:27:03 $ $Revision: 1.50 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) M. Stephenson, 1990, 1991. */ @@ -1406,11 +1406,7 @@ static const char *build_opts[] = { "timed wait for display effects", #endif #ifdef USER_SOUNDS -#ifdef USER_SOUNDS_REGEX - "user sounds via regular expressions", -#else - "user sounds via pmatch", -#endif + "user sounds", #endif #ifdef PREFIXES_IN_USE "variable playground",