Skip to content

Commit

Permalink
Make regex compiling not return a run-time error
Browse files Browse the repository at this point in the history
  • Loading branch information
alfille committed Mar 20, 2015
1 parent d84d00b commit 63520e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
15 changes: 4 additions & 11 deletions module/owlib/src/c/ow_arg.c
Expand Up @@ -28,17 +28,10 @@ static enum arg_address ArgType( const char * arg )
static regex_t rx_col ;

// compile regex expressions
if (
BAD( ow_regcomp( &rx_dev, "/", REG_NOSUB ) )
||
BAD( ow_regcomp( &rx_num, "^[:digit:]\\{1,\\}$", REG_NOSUB ) )
||
BAD( ow_regcomp( &rx_ip, "[:digit:]\\{1,3\\}\\.[:digit:]\\{1,3\\}\\.[:digit:]\\{1,3\\}\\.[:digit:]\\{1,3\\}", REG_NOSUB ) )
||
BAD( ow_regcomp( &rx_col, ":", REG_NOSUB ) )
) {
return arg_addr_error ;
}
ow_regcomp( &rx_dev, "/", REG_NOSUB ) ;
ow_regcomp( &rx_num, "^[:digit:]\\{1,\\}$", REG_NOSUB ) ;
ow_regcomp( &rx_ip, "[:digit:]\\{1,3\\}\\.[:digit:]\\{1,3\\}\\.[:digit:]\\{1,3\\}\\.[:digit:]\\{1,3\\}", REG_NOSUB ) ;
ow_regcomp( &rx_col, ":", REG_NOSUB ) ;

if ( arg == NULL ) {
return arg_addr_null ;
Expand Down
42 changes: 24 additions & 18 deletions module/owlib/src/c/ow_regex.c
Expand Up @@ -19,6 +19,8 @@
// tree to hold pointers to compiled regex expressions to cache compilation and free
void * regex_tree = NULL ;

enum e_regcomp_test { e_regcomp_exists, e_regcomp_new, e_regcomp_error, } ;

struct s_regex {
regex_t * reg ;
} ;
Expand All @@ -39,46 +41,50 @@ static int reg_compare( const void * a, const void *b)
return (int) (pa->reg - pb->reg) ;
}

// Add a reg comp and return 1 or 0 if exists already
static int regcomp_test( regex_t * reg )
// Add a reg comp to tree if doesn't already exist
static enum e_regcomp_test regcomp_test( regex_t * reg )
{
struct s_regex * pnode = owmalloc( sizeof( struct s_regex ) ) ;
struct s_regex * found ;
void * result ;

if ( pnode == NULL ) {
LEVEL_DEBUG("memory exhuasted") ;
return 0 ;
return e_regcomp_error ;
}

pnode->reg = reg ;
result = tsearch( (void *) pnode, &regex_tree, reg_compare ) ;
found = *(struct s_regex **) result ;
if ( found == pnode ) {
// new entry
return 1 ;
return e_regcomp_new ;
}
// existing entry
owfree( pnode ) ;
return 0 ;
return e_regcomp_exists ;
}


GOOD_OR_BAD ow_regcomp( regex_t * reg, const char * regex, int cflags )
// Test if regcomp new or needs to be compiled
void ow_regcomp( regex_t * reg, const char * regex, int cflags )
{
if ( regcomp_test( reg ) ) {
int reg_res = regcomp( reg, regex, cflags ) ;
if ( reg_res == 0 ) {
LEVEL_DEBUG("Reg Ex expression <%s> compiled to %p\n",regex,reg) ;
return gbGOOD ;
} else {
char e[101];
regerror( reg_res, reg, e, 100 ) ;
LEVEL_DEBUG("Problem compiling reg expression <%s>: %s",regex, e ) ;
return gbBAD ;
switch ( regcomp_test( reg ) ) {
case e_regcomp_error:
return ;
case e_regcomp_exists:
return ;
case e_regcomp_new:
{
int reg_res = regcomp( reg, regex, cflags ) ;
if ( reg_res == 0 ) {
LEVEL_DEBUG("Reg Ex expression <%s> compiled to %p\n",regex,reg) ;
} else {
char e[101];
regerror( reg_res, reg, e, 100 ) ;
LEVEL_DEBUG("Problem compiling reg expression <%s>: %s",regex, e ) ;
}
}
}
return gbGOOD ;
}

// Add a reg comp and return 1 or 0 if exists already
Expand Down
2 changes: 1 addition & 1 deletion module/owlib/src/include/ow_regex.h
Expand Up @@ -33,7 +33,7 @@

#include <regex.h>

GOOD_OR_BAD ow_regcomp( regex_t * preg, const char * regex, int cflags ) ;
void ow_regcomp( regex_t * preg, const char * regex, int cflags ) ;
void ow_regdestroy( void ) ;
void ow_regfree( regex_t * reg ) ;

Expand Down

0 comments on commit 63520e1

Please sign in to comment.