1 /* NetHack 3.6 posixregex.c $NHDT-Date: 1434446947 2015/06/16 09:29:07 $ $NHDT-Branch: master $:$NHDT-Revision: 1.5 $ */
2 /* Copyright (c) Sean Hunt 2015. */
3 /* NetHack may be freely redistributed. See license for details. */
9 /* The nhregex interface is implemented by several source files. The
10 * file to be used can be linked in by the build.
12 * The regex standard implemented should be POSIX extended regular
14 * http://pubs.opengroup.org/onlinepubs/009696899/basedefs/xbd_chap09.html
15 * If an implementation uses an alternate expression format, this should
16 * be clearly noted; this will result in incompatibility of config files
17 * when NetHack is compiled with that implementation.
20 * The nhregex structure is an opaque structure type containing the
21 * information about a compiled regular expression.
23 * struct nhregex *regex_init(void)
24 * Used to create a new instance of the nhregex structure. It is
25 * uninitialized and can only safely be passed to regex_compile.
27 * boolean regex_compile(const char *s, struct nhregex *re)
28 * Used to compile s into a regex and store it in re. Returns TRUE if
29 * successful and FALSE otherwise. re is invalidated regardless of
32 * const char *regex_error_desc(struct nhregex *re)
33 * Used to retrieve an error description from an error created involving
34 * re. Returns NULL if no description can be retrieved. The returned
35 * string may be a static buffer and so is only valid until the next
36 * call to regex_error_desc.
38 * boolean regex_match(const char *s, struct nhregex *re)
39 * Used to determine if s (or any substring) matches the regex compiled
40 * into re. Only valid if the most recent call to regex_compile on re
43 * void regex_free(struct nhregex *re)
44 * Deallocate a regex object.
47 const char regex_id
[] = "posixregex";
57 return (struct nhregex
*) alloc(sizeof(struct nhregex
));
61 regex_compile(const char *s
, struct nhregex
*re
)
65 if ((re
->err
= regcomp(&re
->re
, s
, REG_EXTENDED
| REG_NOSUB
)))
71 regex_error_desc(struct nhregex
*re
)
73 static char buf
[BUFSZ
];
76 return (const char *) 0;
78 /* FIXME: Using a static buffer here is not ideal, but avoids memory
79 * leaks. Consider the allocation more carefully. */
80 regerror(re
->err
, &re
->re
, buf
, BUFSZ
);
86 regex_match(const char *s
, struct nhregex
*re
)
93 if ((result
= regexec(&re
->re
, s
, 0, (genericptr_t
) 0, 0))) {
94 if (result
!= REG_NOMATCH
)
102 regex_free(struct nhregex
*re
)