File: matching.c

package info (click to toggle)
xymon 4.3.30-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,384 kB
  • sloc: ansic: 69,137; sh: 3,601; makefile: 863; javascript: 452; perl: 48
file content (215 lines) | stat: -rw-r--r-- 5,173 bytes parent folder | download | duplicates (2)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*----------------------------------------------------------------------------*/
/* Xymon monitor library.                                                     */
/*                                                                            */
/* This is a library module, part of libxymon.                                */
/* It contains routines for matching names and expressions                    */
/*                                                                            */
/* Copyright (C) 2002-2011 Henrik Storner <henrik@storner.dk>                 */
/*                                                                            */
/* This program is released under the GNU General Public License (GPL),       */
/* version 2. See the file "COPYING" for details.                             */
/*                                                                            */
/*----------------------------------------------------------------------------*/

static char rcsid[] = "$Id: matching.c 7733 2015-11-12 09:24:01Z jccleaver $";

#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>

#include "libxymon.h"

pcre2_code *compileregex_opts(const char *pattern, uint32_t flags)
{
	pcre2_code *result;
	char errmsg[120];
	int err;
	PCRE2_SIZE errofs;

	dbgprintf("Compiling regex %s\n", pattern);
	result = pcre2_compile(pattern, strlen(pattern), flags, &err, &errofs, NULL);
	if (result == NULL) {
		pcre2_get_error_message(err, errmsg, sizeof(errmsg));
		errprintf("pcre compile '%s' failed (offset %zu): %s\n", pattern, errofs, errmsg);
		return NULL;
	}

	return result;
}

pcre2_code *compileregex(const char *pattern)
{
	return compileregex_opts(pattern, PCRE2_CASELESS);
}

pcre2_code *multilineregex(const char *pattern)
{
	return compileregex_opts(pattern, PCRE2_CASELESS|PCRE2_MULTILINE);
}

int matchregex(const char *needle, pcre2_code *pcrecode)
{
	pcre2_match_data *ovector;
	int result;

	if (!needle || !pcrecode) return 0;

	ovector = pcre2_match_data_create(30, NULL);
	result = pcre2_match(pcrecode, needle, strlen(needle), 0, 0, ovector, NULL);
	pcre2_match_data_free(ovector);
	return (result >= 0);
}

void freeregex(pcre2_code *pcrecode)
{
	if (!pcrecode) return;

	pcre2_code_free(pcrecode);
}

int namematch(const char *needle, char *haystack, pcre2_code *pcrecode)
{
	char *xhay;
	char *tokbuf = NULL, *tok;
	int found = 0;
	int result = 0;
	int allneg = 1;

	if ((needle == NULL) || (*needle == '\0')) return 0;

	if (pcrecode) {
		/* Do regex matching. The regex has already been compiled for us. */
		return matchregex(needle, pcrecode);
	}

	if (strcmp(haystack, "*") == 0) {
		/* Match anything */
		return 1;
	}

	/* Implement a simple, no-wildcard match */
	xhay = strdup(haystack);

	tok = strtok_r(xhay, ",", &tokbuf);
	while (tok) {
		allneg = (allneg && (*tok == '!'));

		if (!found) {
			if (*tok == '!') {
				found = (strcmp(tok+1, needle) == 0);
				if (found) result = 0;
			}
			else {
				found = (strcmp(tok, needle) == 0);
				if (found) result = 1;
			}
		}

		/* We must check all of the items in the haystack to see if they are all negative matches */
		tok = strtok_r(NULL, ",", &tokbuf);
	}
	xfree(xhay);

	/* 
	 * If we didn't find it, and the list is exclusively negative matches,
	 * we must return a positive result for "no match".
	 */
	if (!found && allneg) result = 1;

	return result;
}

int patternmatch(char *datatosearch, char *pattern, pcre2_code *pcrecode)
{
	if (pcrecode) {
		/* Do regex matching. The regex has already been compiled for us. */
		return matchregex(datatosearch, pcrecode);
	}

	if (strcmp(pattern, "*") == 0) {
		/* Match anything */
		return 1;
	}

	return (strstr(datatosearch, pattern) != NULL);
}

pcre2_code **compile_exprs(char *id, const char **patterns, int count)
{
	pcre2_code **result = NULL;
	int i;

	result = (pcre2_code **)calloc(count, sizeof(pcre2_code *));
	for (i=0; (i < count); i++) {
		result[i] = compileregex(patterns[i]);
		if (!result[i]) {
			errprintf("Internal error: %s pickdata PCRE-compile failed\n", id);
			for (i=0; (i < count); i++) if (result[i]) pcre2_code_free(result[i]);
			xfree(result);
			return NULL;
		}
	}

	return result;
}

int pickdata(char *buf, pcre2_code *expr, int dupok, ...)
{
	int res, i;
	pcre2_match_data *ovector;
	va_list ap;
	char **ptr;
	char w[100];
	PCRE2_SIZE l;

	if (!expr) return 0;

	ovector = pcre2_match_data_create(30, NULL);
	res = pcre2_match(expr, buf, strlen(buf), 0, 0, ovector, NULL);
	if (res < 0) {
		pcre2_match_data_free(ovector);
		return 0;
	}

	va_start(ap, dupok);

	for (i=1; (i < res); i++) {
		*w = '\0';
		l = sizeof(w);
		pcre2_substring_copy_bynumber(ovector, i, w, &l);
		ptr = va_arg(ap, char **);
		if (dupok) {
			if (*ptr) xfree(*ptr);
			*ptr = strdup(w);
		}
		else {
			if (*ptr == NULL) {
				*ptr = strdup(w);
			}
			else {
				dbgprintf("Internal error: Duplicate match ignored\n");
			}
		}
	}

	va_end(ap);
	pcre2_match_data_free(ovector);

	return 1;
}


int timematch(char *holidaykey, char *tspec)
{
	int result;

	result = within_sla(holidaykey, tspec, 0);

	return result;
}