1 /*
2 * This is an interface to the posix regular-expression matching.
3 */
4
5 #include "localsys.h"
6
7 #ifdef HAVE_POSIX_REGEX
8
9 static regex_t compiled_pattern; /* for storing a compiled pattern */
10 static int free_pattern = 0; /* set !0 if a later call to p_compile()
11 * should free the compiled pattern.
12 */
13
14 static int posix_flags = 0; /* user-specified regcomp flags */
15
16 /*
17 * Specify cflags for use in future regcomp() calls.
18 */
19 void
p_regcomp_flags(cflags)20 p_regcomp_flags(cflags)
21 int cflags;
22 {
23 posix_flags |= cflags;
24 }
25
26 /*
27 * Compile a pattern and store in compiled_pattern, above.
28 * Return NULL on success, else some error string.
29 */
30 char *
p_compile(regex)31 p_compile(regex)
32 char *regex;
33 {
34 int i;
35 static char buf[1000];
36
37 if (free_pattern) {
38 /* Free old pattern space */
39 regfree(&compiled_pattern);
40 free_pattern = 0;
41 }
42
43 if ((i=regcomp(&compiled_pattern, regex, posix_flags | REG_NOSUB)) != 0) {
44 /* compile failed */
45 regerror(i, &compiled_pattern, buf, sizeof(buf));
46 return buf;
47 }
48
49 free_pattern = 1; /* remind us to free the pattern next time. */
50
51 return NULL;
52 }
53
54 int
p_compare(str)55 p_compare(str)
56 char *str;
57 {
58 regmatch_t pmatch[1];
59 return (regexec(&compiled_pattern, str, 1, pmatch, 0) == 0);
60 }
61
62 #else
63
64 /* --------- POSIX regex routines not available ----------- */
65
66 static int posix_flags = 0;
67
68 void
p_regcomp_flags(cflags)69 p_regcomp_flags(cflags)
70 int cflags;
71 {
72 return;
73 }
74
75 char *
p_compile(regex)76 p_compile(regex)
77 char *regex;
78 {
79 Error(0, 1, "p_compile(): POSIX regular expressions not available.\n");
80 return NULL;
81 }
82
83 int
p_compare(str)84 p_compare(str)
85 char *str;
86 {
87 Error(0, 1, "p_compare(): POSIX regular expressions not available.\n");
88 return NULL;
89 }
90
91 #endif
92