summaryrefslogtreecommitdiff
path: root/Ivy/IvyBinding.cxx
blob: f3000fdf33e3a31387bb8204f222d923218491c9 (plain)
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
/*
 *	Ivy, C++ interface
 *
 *	Copyright (C) 1997-2000
 *	Centre d'Études de la Navigation Aérienne
 *
 *	Bind syntax for extracting message comtent 
 *  using regexp or other 
 *
 *	Authors: François-Régis Colin <fcolin@cena.fr>
 *
 *	$Id: ivybind.c,v 1.9.2.7 2006/06/01 12:07:17 bustico Exp $
 * 
 *	Please refer to file version.h for the
 *	copyright notice regarding this software
 */
/* Module de gestion de la syntaxe des messages Ivy */
#include "IvyStdAfx.h"

#include "IvyBinding.h"

static int err_offset;

#ifdef USE_PCRE
	static const char *err_buf;
#else  /* we don't USE_PCRE */
	static char err_buf[4096];
#endif /* USE_PCRE */

/* classes de messages emis par l'application utilise pour le filtrage */
static int	messages_classes_count = 0;
static const char **messages_classes = 0;
/* regexp d'extraction du mot clef des regexp client pour le filtrage des regexp , ca va c'est clair ??? */
static IvyBinding token_extract;

IvyBinding::IvyBinding()
{
#ifdef USE_PCRE
	regexp = NULL;
	inspect = NULL;
#else  /* we don't USE_PCRE */
	free( regexp );
#endif /* USE_PCRE */
	nb_match = 0;
}
IvyBinding::~IvyBinding()
{
#ifdef USE_PCRE
	if (inspect!=NULL)
		pcre_free(inspect);
	if (regexp!=NULL)
		pcre_free(regexp);
#else  /* we don't USE_PCRE */
	free( regexp );
#endif /* USE_PCRE */
}
bool IvyBinding::Compile( const char * expression,  int *erroffset, const char **errmessage )
{
	bool compile = false;
#ifdef USE_PCRE
	regexp = pcre_compile(expression, PCRE_CASELESS, &err_buf, &err_offset, NULL);
	if ( regexp != NULL )
		{
			this->inspect = pcre_study(regexp,0,&err_buf);
			if (err_buf!=NULL)
				{
					printf("Error studying %s, message: %s\n",expression,err_buf);
				}
			compile = true;
		}
		else
		{
		*erroffset = err_offset;
		*errmessage = err_buf;
		printf("Error compiling '%s', %s\n", expression, err_buf);
		}
#else  /* we don't USE_PCRE */
	regex_t regexp;
	int reg;
	reg = regcomp(&regexp, expression, REGCOMP_OPT|REG_EXTENDED);
	if ( reg == 0 )
		{
			this->next = NULL;
		}
		else
		{
		regerror (reg, &regexp, err_buf, sizeof(err_buf) );
		*erroffset = err_offset;
		*errmessage = err_buf;
		printf("Error compiling '%s', %s\n", expression, err_buf);
		}
#endif /* USE_PCRE */
	return compile;
}


int IvyBinding::Exec( const char * message )
{
#ifdef USE_PCRE
	
	nb_match = pcre_exec(
					regexp,
					inspect,
					message,
					strlen(message),
					0, /* debut */
					0, /* no other regexp option */
					ovector,
					OVECSIZE);
	if (nb_match<1) return 0; /* no match */
#else  /* we don't USE_PCRE */
	memset( match, -1, sizeof(match )); /* work around bug !!!*/
	nb_match = regexec (&regexp, message, MAX_MSG_FIELDS, match, 0) 
	if (nb_match == REG_NOMATCH)
		return 0;
	for ( index = 1; index < MAX_MSG_FIELDS; index++ )
	{
		if ( match[i].rm_so != -1 )
			nb_match++;
	}
#endif /* USE_PCRE */
	return nb_match;
}

void IvyBinding::Match( const char *message, int argnum, int *arglen, const char **arg)
{
#ifdef USE_PCRE
	
		*arglen = ovector[2*argnum+1]- ovector[2*argnum];
		*arg =   message + ovector[2*argnum];
#else  /* we don't USE_PCRE */
	
	regmatch_t* p;

	p = &match[argnum+1];
	if ( p->rm_so != -1 ) {
			*arglen = p->rm_eo - p->rm_so;
			*arg = message + p->rm_so;
	} else { // ARG VIDE
			*arglen = 0;
			*arg = NULL;
	}
#endif /* USE_PCRE */

}

//filter Expression Bind 
void IvyBinding::SetFilter( int argc, const char **argv)
{
	const char *errbuf;
	int erroffset;

	messages_classes_count = argc;
	messages_classes = argv;
	/* compile the token extraction regexp */

	if ( !token_extract.Compile("^\\^([a-zA-Z_0-9-]+).*", & erroffset, & errbuf) )
	{
		printf("Error compiling Token Extract regexp: %s\n", errbuf);
	}
}
	
int IvyBinding::Filter(const char *expression)
{
	int i;
	int err;
	int regexp_ok = 1; /* accepte tout par default */
	int tokenlen;
	const char *token;
	
	if ( *expression =='^' && messages_classes_count !=0 )
	{
		regexp_ok = 0;
		
		/* extract token */
		err = token_extract.Exec( expression );
		if ( err < 1 ) return 1;
		token_extract.Match( expression , 1, &tokenlen, &token );
		for ( i = 0 ; i < messages_classes_count; i++ )
		{
		  if (strncmp( messages_classes[i], token, tokenlen ) == 0) {
		    return 1; }
		  //		  else {
		  //printf ("DBG> %s eliminé [%s]\n", token, expression);
		  //}
		}
 	}
	return regexp_ok;
}