Use the common regex engine in more places.

In particular, in autopickup_exceptions and user sounds.
This commit is contained in:
Sean Hunt
2015-05-24 22:23:17 +09:00
committed by nhmall
parent 49b9f6c926
commit 84d63e169b
7 changed files with 29 additions and 48 deletions

View File

@@ -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) */

View File

@@ -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;
};

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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 <sys/types.h>
#include <regex.h>
#endif
#include <nhregex.h>
#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;

View File

@@ -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))) {

View File

@@ -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",