#include <portable.h>
#include "rewrite-int.h"
static int
append_rule(
struct rewrite_context *context,
struct rewrite_rule *rule
)
{
struct rewrite_rule *r;
assert( context != NULL );
assert( context->lc_rule != NULL );
assert( rule != NULL );
for ( r = context->lc_rule; r->lr_next != NULL; r = r->lr_next );
r->lr_next = rule;
rule->lr_prev = r;
return REWRITE_SUCCESS;
}
static int
append_action(
struct rewrite_action *base,
struct rewrite_action *action
)
{
struct rewrite_action *a;
assert( base != NULL );
assert( action != NULL );
for ( a = base; a->la_next != NULL; a = a->la_next );
a->la_next = action;
return REWRITE_SUCCESS;
}
int
rewrite_rule_compile(
struct rewrite_info *info,
struct rewrite_context *context,
const char *pattern,
const char *result,
const char *flagstring
)
{
int flags = REWRITE_REGEX_EXTENDED | REWRITE_REGEX_ICASE;
int mode = REWRITE_RECURSE;
struct rewrite_rule *rule = NULL;
struct rewrite_subst *subst = NULL;
struct rewrite_action *action = NULL, *first_action = NULL;
const char *p;
assert( info != NULL );
assert( context != NULL );
assert( pattern != NULL );
assert( result != NULL );
subst = rewrite_subst_compile( info, result );
if ( subst == NULL ) {
return REWRITE_ERR;
}
for ( p = flagstring; p[ 0 ] != '\0'; p++ ) {
switch( p[ 0 ] ) {
case REWRITE_FLAG_HONORCASE:
flags &= ~REWRITE_REGEX_ICASE;
break;
case REWRITE_FLAG_BASICREGEX:
flags &= ~REWRITE_REGEX_EXTENDED;
break;
case REWRITE_FLAG_EXECONCE:
mode &= ~REWRITE_RECURSE;
mode |= REWRITE_EXEC_ONCE;
break;
case REWRITE_FLAG_STOP:
action = calloc( sizeof( struct rewrite_action ), 1 );
if ( action == NULL ) {
return REWRITE_ERR;
}
mode &= ~REWRITE_RECURSE;
mode |= REWRITE_EXEC_ONCE;
action->la_type = REWRITE_ACTION_STOP;
break;
case REWRITE_FLAG_UNWILLING:
action = calloc( sizeof( struct rewrite_action ), 1 );
if ( action == NULL ) {
return REWRITE_ERR;
}
mode &= ~REWRITE_RECURSE;
mode |= REWRITE_EXEC_ONCE;
action->la_type = REWRITE_ACTION_UNWILLING;
break;
case REWRITE_FLAG_GOTO: {
char buf[16], *q;
size_t l;
int *d;
if ( p[ 1 ] != '{' ) {
return REWRITE_ERR;
}
q = strchr( p + 2, '}' );
if ( q == NULL ) {
return REWRITE_ERR;
}
l = q - p + 2;
if ( l >= sizeof( buf ) ) {
return REWRITE_ERR;
}
AC_MEMCPY( buf, p + 2, l );
buf[ l ] = '\0';
d = malloc( sizeof( int ) );
if ( d == NULL ) {
return REWRITE_ERR;
}
d[ 0 ] = atoi( buf );
action = calloc( sizeof( struct rewrite_action ), 1 );
if ( action == NULL ) {
return REWRITE_ERR;
}
action->la_type = REWRITE_ACTION_GOTO;
action->la_args = (void *)d;
p = q;
break;
}
case REWRITE_FLAG_IGNORE_ERR:
action = calloc( sizeof( struct rewrite_action ), 1 );
if ( action == NULL ) {
return REWRITE_ERR;
}
action->la_type = REWRITE_ACTION_IGNORE_ERR;
break;
default:
break;
}
if ( action != NULL ) {
if ( first_action == NULL ) {
first_action = action;
} else {
append_action( first_action, action );
}
action = NULL;
}
}
rule = calloc( sizeof( struct rewrite_rule ), 1 );
if ( rule == NULL ) {
return REWRITE_ERR;
}
if ( regcomp( &rule->lr_regex, ( char * )pattern, flags ) != 0 ) {
free( rule );
return REWRITE_ERR;
}
rule->lr_pattern = strdup( pattern );
rule->lr_subststring = strdup( result );
rule->lr_flagstring = strdup( flagstring );
rule->lr_subst = subst;
rule->lr_flags = flags;
rule->lr_mode = mode;
rule->lr_action = first_action;
append_rule( context, rule );
return REWRITE_SUCCESS;
}
int
rewrite_rule_apply(
struct rewrite_info *info,
struct rewrite_op *op,
struct rewrite_rule *rule,
const char *arg,
char **result
)
{
size_t nmatch = REWRITE_MAX_MATCH;
regmatch_t match[ REWRITE_MAX_MATCH ];
int rc = REWRITE_SUCCESS;
char *string;
struct berval val;
assert( info != NULL );
assert( op != NULL );
assert( rule != NULL );
assert( arg != NULL );
assert( result != NULL );
*result = NULL;
string = strdup( arg );
if ( string == NULL ) {
return REWRITE_REGEXEC_ERR;
}
recurse:;
Debug( LDAP_DEBUG_TRACE, "==> rewrite_rule_apply"
" rule='%s' string='%s'\n%s",
rule->lr_pattern, string, "" );
op->lo_num_passes++;
if ( regexec( &rule->lr_regex, string, nmatch, match, 0 ) != 0 ) {
if ( *result == NULL ) {
free( string );
}
return REWRITE_REGEXEC_OK;
}
rc = rewrite_subst_apply( info, op, rule->lr_subst, string,
match, &val );
*result = val.bv_val;
free( string );
if ( rc != REWRITE_REGEXEC_OK ) {
return rc;
}
if ( ( rule->lr_mode & REWRITE_RECURSE ) == REWRITE_RECURSE
&& op->lo_num_passes <= info->li_max_passes ) {
string = *result;
goto recurse;
}
return REWRITE_REGEXEC_OK;
}