File: p_regex.c

package info (click to toggle)
super 3.30.0-2+lenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,072 kB
  • ctags: 755
  • sloc: ansic: 10,089; sh: 288; makefile: 204
file content (91 lines) | stat: -rw-r--r-- 1,638 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * This is an interface to the posix regular-expression matching.
 */

#include "localsys.h"

#ifdef HAVE_POSIX_REGEX

static regex_t compiled_pattern;	/* for storing a compiled pattern */
static int free_pattern = 0;		/* set !0 if a later call to p_compile()
					 * should free the compiled pattern.
					 */

static int posix_flags = 0;		/* user-specified regcomp flags */

/*
 * Specify cflags for use in future regcomp() calls.
 */
void
p_regcomp_flags(cflags)
int cflags;
{
    posix_flags |= cflags;
}

/*
 * Compile a pattern and store in compiled_pattern, above.
 * Return NULL on success, else some error string.
 */
char *
p_compile(regex)
char *regex;
{
    int i;
    static char buf[1000];

    if (free_pattern) {
	/* Free old pattern space */
	regfree(&compiled_pattern);
	free_pattern = 0;
    }

    if ((i=regcomp(&compiled_pattern, regex, posix_flags | REG_NOSUB)) != 0) {
	/* compile failed */
	regerror(i, &compiled_pattern, buf, sizeof(buf));
	return buf;
    }

    free_pattern = 1;	/* remind us to free the pattern next time. */

    return NULL;
}

int
p_compare(str)
char *str;
{
    regmatch_t pmatch[1];
    return (regexec(&compiled_pattern, str, 1, pmatch, 0) == 0);
}

#else

    /* --------- POSIX regex routines not available -----------  */

static int posix_flags = 0;

void
p_regcomp_flags(cflags)
int cflags;
{
    return;
}

char *
p_compile(regex)
char *regex;
{
    Error(0, 1, "p_compile(): POSIX regular expressions not available.\n");
    return NULL;
}

int
p_compare(str)
char *str;
{
    Error(0, 1, "p_compare(): POSIX regular expressions not available.\n");
    return NULL;
}

#endif