OSDN Git Service

upgrade to 3.6.2
[jnethack/source.git] / sys / share / pmatchregex.c
1 /* NetHack 3.6  pmatchregex.c   $NHDT-Date: 1544482890 2018/12/10 23:01:30 $  $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.2 $ */
2 /* Copyright (c) Sean Hunt  2015.                                 */
3 /* NetHack may be freely redistributed.  See license for details. */
4
5 #include "hack.h"
6
7 /* Implementation of the regex engine using pmatch().
8  * [Switched to pmatchi() so as to ignore case.]
9  *
10  * This is a fallback ONLY and should be avoided where possible, as it results
11  * in regexes not behaving as POSIX extended regular expressions. As a result,
12  * configuration files for NetHacks built with this engine will not be
13  * portable to ones built with an alternate regex engine.
14  */
15
16 const char regex_id[] = "pmatchregex";
17
18 struct nhregex {
19     const char *pat;
20 };
21
22 struct nhregex *
23 regex_init()
24 {
25     struct nhregex *re;
26
27     re = (struct nhregex *) alloc(sizeof (struct nhregex));
28     re->pat = (const char *) 0;
29     return re;
30 }
31
32 boolean
33 regex_compile(s, re)
34 const char *s;
35 struct nhregex *re;
36 {
37     if (!re)
38         return FALSE;
39     if (re->pat)
40         free((genericptr_t) re->pat);
41
42     re->pat = dupstr(s);
43     return TRUE;
44 }
45
46 const char *
47 regex_error_desc(re)
48 struct nhregex *re UNUSED;
49 {
50     return "pattern match compilation error";
51 }
52
53 boolean
54 regex_match(s, re)
55 const char *s;
56 struct nhregex *re;
57 {
58     if (!re || !re->pat || !s)
59         return FALSE;
60
61     return pmatchi(re->pat, s);
62 }
63
64 void
65 regex_free(re)
66 struct nhregex *re;
67 {
68     if (re) {
69         if (re->pat)
70             free((genericptr_t) re->pat);
71         free((genericptr_t) re);
72     }
73 }