From a885259e1ffaa6de1d71107d19c815fb7875c86a Mon Sep 17 00:00:00 2001 From: fcolin Date: Fri, 21 Apr 2006 12:13:54 +0000 Subject: Separation du code Regexp dans un module --- src/ivybind.c | 194 +++++++++++++++------------------------------------------- 1 file changed, 50 insertions(+), 144 deletions(-) (limited to 'src/ivybind.c') diff --git a/src/ivybind.c b/src/ivybind.c index 237ee78..5e0ffe0 100644 --- a/src/ivybind.c +++ b/src/ivybind.c @@ -37,7 +37,6 @@ #endif #include "list.h" -#include "hash.h" #include "ivybind.h" static int err_offset; @@ -49,10 +48,7 @@ static int err_offset; #endif struct _binding { - IvyBindingType type; - const char *msgname; /* msg tag name first word of message */ - char **msgargs; /* list of msg argument name */ - IvyArgument args; /* result */ + const char *expression; /* regexp*/ #ifdef USE_PCRE_REGEX pcre *regexp; pcre_extra *inspect; @@ -64,45 +60,13 @@ struct _binding { #endif }; - /* 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; - -/* stokage du message parse avant l'execution des regles de binding simple */ -static char *current_msg = NULL; -static char *msgtag; -static HASHTABLE msg_args_values = NULL; - -static IvyBinding IvyBindingCompileSimple( IvyBindingType typ, const char * expression ) -{ - int nb_arg= 0; - char *argname; - char **argv; - char *expr; - IvyBinding bind=0; - - expr = strdup( expression ); //Allocate a new buffer of separated token - /* count nb args */ - argname = expr; - while ( *argname ) - { - if ( *argname++ == ' ' ) nb_arg++; - } - - bind = (IvyBinding)malloc( sizeof( struct _binding )); - memset( bind, 0, sizeof(*bind ) ); - bind->type = IvyBindSimple; - bind->msgname = strtok( expr, " "); - bind->msgargs = malloc ( sizeof( char* ) * ( nb_arg + 1) ); - argv = bind->msgargs; - while ( (argname = strtok( NULL, " ")) ) - *argv++ = argname; - *argv++ = argname; /* end with NULL */ - return bind; -} -static IvyBinding IvyBindingCompileRegexp( IvyBindingType typ, const char * expression ) +IvyBinding IvyBindingCompile( const char * expression ) { IvyBinding bind=0; #ifdef USE_PCRE_REGEX @@ -111,9 +75,13 @@ static IvyBinding IvyBindingCompileRegexp( IvyBindingType typ, const char * expr if ( regexp != NULL ) { bind = (IvyBinding)malloc( sizeof( struct _binding )); + if ( ! bind ) + { + perror( "IvyBindingCompile malloc error: "); + exit(-1); + } memset( bind, 0, sizeof(*bind ) ); bind->regexp = regexp; - bind->type = IvyBindRegexp; bind->inspect = pcre_study(regexp,0,&err_buf); if (err_buf!=NULL) { @@ -144,13 +112,7 @@ static IvyBinding IvyBindingCompileRegexp( IvyBindingType typ, const char * expr #endif return bind; } -IvyBinding IvyBindingCompile( IvyBindingType typ, const char * expression ) -{ - if ( typ == IvyBindRegexp ) - return IvyBindingCompileRegexp( typ, expression); - else - return IvyBindingCompileSimple( typ, expression); -} + void IvyBindingGetCompileError( int *offset, const char **errmessage ) { *offset = err_offset; @@ -159,17 +121,15 @@ void IvyBindingGetCompileError( int *offset, const char **errmessage ) void IvyBindingFree( IvyBinding bind ) { #ifdef USE_PCRE_REGEX - if (bind->inspect!=NULL) pcre_free(bind->inspect); + if (bind->inspect!=NULL) + pcre_free(bind->inspect); pcre_free(bind->regexp); #else + free( bind->regexp ); #endif - if (bind->msgname) - free ( bind->msgname ); - if (bind->msgargs) - free ( bind->msgargs ); free ( bind ); } -int IvyBindingExecRegexp( IvyBinding bind, const char * message ) +int IvyBindingExec( IvyBinding bind, const char * message ) { int nb_match = 0; #ifdef USE_PCRE_REGEX @@ -200,123 +160,69 @@ int IvyBindingExecRegexp( IvyBinding bind, const char * message ) #endif return nb_match; } -int IvyBindingExecSimple( IvyBinding bind, const char * message ) -{ - char **msg_args; - if ( strcmp( bind->msgname, msgtag ) != 0 ) - return 0; - msg_args = bind->msgargs; - bind->args = IvyArgumentNew( 0,NULL ); - while( *msg_args ) - { - char *value; - value = hash_lookup(msg_args_values, (HASHKEYTYPE)*msg_args++); - if ( !value ) value = ""; /* TODO should we report matching ??? */ - IvyAddChildValue( bind->args, strlen( value ), value); - } - return 1; -} -int IvyBindingExec( IvyBinding bind, const char * message ) -{ - if ( bind->type == IvyBindRegexp ) - return IvyBindingExecRegexp( bind, message); - else - return IvyBindingExecSimple( bind, message); -} -static IvyArgument IvyBindingMatchSimple( IvyBinding bind, const char *message) -{ - return bind->args; -} -static IvyArgument IvyBindingMatchRegexp( IvyBinding bind, const char *message) + +void IvyBindingMatch( IvyBinding bind, const char *message, int argnum, int *arglen, const char **arg) { - int index=1;// firts arg wall string ??? - int arglen; - const void* arg; - IvyArgument args; - args = IvyArgumentNew( 0,NULL ); #ifdef USE_PCRE_REGEX - while ( indexnb_match ) { - arglen = bind->ovector[2*index+1]- bind->ovector[2*index]; - arg = message + bind->ovector[2*index]; - index++; + + *arglen = bind->ovector[2*argnum+1]- bind->ovector[2*argnum]; + *arg = message + bind->ovector[2*argnum]; #else /* we don't USE_PCRE_REGEX */ - for ( index = 1; index < MAX_MSG_FIELDS; index++ ) - { + regmatch_t* p; - p = &bind->match[index]; + p = &bind->match[argnum+1]; if ( p->rm_so != -1 ) { - arglen = p->rm_eo - p->rm_so; - arg = message + p->rm_so; + *arglen = p->rm_eo - p->rm_so; + *arg = message + p->rm_so; } else { // ARG VIDE - arglen = 0; - arg = NULL; + *arglen = 0; + *arg = NULL; } #endif // USE_PCRE_REGEX - IvyAddChildValue( args, arglen, arg ); - } - return args; -} -IvyArgument IvyBindingMatch( IvyBinding bind, const char *message) -{ - if ( bind->type == IvyBindRegexp ) - return IvyBindingMatchRegexp( bind, message); - else - return IvyBindingMatchSimple( bind, message); + } //filter Expression Bind void IvyBindingSetFilter( int argc, const char **argv) { + const char *errbuf; + int erroffset; + messages_classes_count = argc; messages_classes = argv; -} -void IvyBindingParseMessage( const char *msg ) -{ - char *arg; - if ( current_msg ) free( current_msg ); - if ( msg_args_values ) hash_destroy( msg_args_values ); - current_msg = strdup( msg ); - msg_args_values = hash_create( 256, TRUE ); - msgtag = strtok( current_msg, " " ); - while( (arg = strtok( NULL, " =" )) ) + /* compile the token extraction regexp */ + + token_extract = IvyBindingCompile("^\\^([a-zA-Z_0-9-]+).*"); + if ( !token_extract ) { - char *val = strtok( NULL, " ="); - if ( arg && val ) - hash_addstring( msg_args_values, arg, val ); + IvyBindingGetCompileError( & erroffset, & errbuf ); + printf("Error compiling Token Extract regexp: %s\n", errbuf); } } -int IvyBindingFilter(IvyBindingType typ, int len, const char *exp) + +int IvyBindingFilter(const char *expression) { - /* TODO check args limits !!!*/ int i; - /* accepte tout par default */ - int regexp_ok = 1; - // TODO simplify test 3 conditions - if ( typ == IvyBindRegexp ) - { - if ( *exp =='^' && messages_classes_count !=0 ) - { - regexp_ok = 0; - for ( i = 0 ; i < messages_classes_count; i++ ) - { - if (strncmp( messages_classes[i], exp+1, strlen( messages_classes[i] )) == 0) - return 1; - } - } - } - else - { - if ( messages_classes_count !=0 ) + 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 = IvyBindingExec( token_extract, expression ); + if ( err < 1 ) return 1; + IvyBindingMatch( token_extract, expression , 0, &tokenlen, &token ); for ( i = 0 ; i < messages_classes_count; i++ ) { - if (strncmp( messages_classes[i], exp, strlen( messages_classes[i] )) == 0) + if (strncmp( messages_classes[i], token, tokenlen ) == 0) return 1; } } - } return regexp_ok; -} \ No newline at end of file +} -- cgit v1.1