]> granicus.if.org Git - nethack/commitdiff
Use the common regex engine in more places.
authorSean Hunt <scshunt@csclub.uwaterloo.ca>
Sun, 24 May 2015 13:23:17 +0000 (22:23 +0900)
committernhmall <mjnh@persona.ca>
Sun, 24 May 2015 14:17:58 +0000 (10:17 -0400)
In particular, in autopickup_exceptions and user sounds.

include/config.h
include/decl.h
src/options.c
src/pickup.c
src/sounds.c
sys/share/posixregex.c
util/makedefs.c

index 3533f0bfb6178c4d8f1615c8e43d7429175df717..2d06c044e0f3514fc6f58c48e188b83ba7d790a8 100644 (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) */
index ddfe356935a0fa14c530a84e78b85596d289f7ed..fa2e34f2139457d62e59fd4bb5e4b3a012352771 100644 (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;
 };
index c98c3d2cdb65b23e31b50f21076b5604cf4ce0b9..f969e07cb83693322fb9faa7f43b44fc39d46118 100644 (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);
index eb8f9dc6d1e6c34a52318b0b18b3f250a1d458df..38b8d61c75a70010e777df72800f190c249cf7df 100644 (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;
     }
index ba8b518c4c3c97aad9f0452e85a83e2f434e4ccd..50c281d5dd675767e22badc8435d2459541dad2a 100644 (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;
index ea7dbd5fea190c1f2b9c38d798ad4dda397ee4f3..c4deab2089e196791653b11c4c765cd9949ad97c 100644 (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))) {
index 9ea22bc1fc5f35e13ceeef52c39ed945e0543e24..92ee31db978f7fb535dffb3252a58eaadf9c21cf 100644 (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",