2003-12-09 01:41:40 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
2001-05-12 08:51:28 +08:00
|
|
|
*
|
2009-01-22 08:40:04 +08:00
|
|
|
* Copyright 2000-2009 The OpenLDAP Foundation.
|
2001-05-12 08:51:28 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2003-12-09 01:41:40 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
2001-05-12 08:51:28 +08:00
|
|
|
*
|
2003-12-09 01:41:40 +08:00
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENT:
|
|
|
|
* This work was initially developed by Pierangelo Masarati for
|
|
|
|
* inclusion in OpenLDAP Software.
|
|
|
|
*/
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
#include <portable.h>
|
|
|
|
|
|
|
|
#include "rewrite-int.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compiles a substitution pattern
|
|
|
|
*/
|
|
|
|
struct rewrite_subst *
|
|
|
|
rewrite_subst_compile(
|
|
|
|
struct rewrite_info *info,
|
2003-12-30 00:14:47 +08:00
|
|
|
const char *str
|
2001-05-12 08:51:28 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
size_t subs_len;
|
2003-11-15 02:39:18 +08:00
|
|
|
struct berval *subs = NULL, *tmps;
|
|
|
|
struct rewrite_submatch *submatch = NULL;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
struct rewrite_subst *s = NULL;
|
|
|
|
|
2003-12-30 00:14:47 +08:00
|
|
|
char *result, *begin, *p;
|
2001-05-12 08:51:28 +08:00
|
|
|
int nsub = 0, l;
|
|
|
|
|
|
|
|
assert( info != NULL );
|
2003-12-30 00:14:47 +08:00
|
|
|
assert( str != NULL );
|
|
|
|
|
|
|
|
result = strdup( str );
|
|
|
|
if ( result == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Take care of substitution string
|
|
|
|
*/
|
|
|
|
for ( p = begin = result, subs_len = 0; p[ 0 ] != '\0'; p++ ) {
|
2003-11-15 18:02:44 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
/*
|
2002-04-16 02:29:39 +08:00
|
|
|
* Keep only single escapes '%'
|
2001-05-12 08:51:28 +08:00
|
|
|
*/
|
2004-03-11 03:21:39 +08:00
|
|
|
if ( !IS_REWRITE_SUBMATCH_ESCAPE( p[ 0 ] ) ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
continue;
|
2001-08-25 23:20:16 +08:00
|
|
|
}
|
2003-11-15 02:39:18 +08:00
|
|
|
|
2004-03-11 03:21:39 +08:00
|
|
|
if ( IS_REWRITE_SUBMATCH_ESCAPE( p[ 1 ] ) ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
/* Pull &p[1] over p, including the trailing '\0' */
|
|
|
|
AC_MEMCPY((char *)p, &p[ 1 ], strlen( p ) );
|
2001-05-12 08:51:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-11-15 18:02:44 +08:00
|
|
|
tmps = ( struct berval * )realloc( subs,
|
2003-11-15 02:39:18 +08:00
|
|
|
sizeof( struct berval )*( nsub + 1 ) );
|
2001-10-22 18:24:32 +08:00
|
|
|
if ( tmps == NULL ) {
|
2003-12-30 00:14:47 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2001-10-22 18:24:32 +08:00
|
|
|
subs = tmps;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* I think an `if l > 0' at runtime is better outside than
|
|
|
|
* inside a function call ...
|
|
|
|
*/
|
|
|
|
l = p - begin;
|
|
|
|
if ( l > 0 ) {
|
|
|
|
subs_len += l;
|
2003-11-15 02:39:18 +08:00
|
|
|
subs[ nsub ].bv_len = l;
|
|
|
|
subs[ nsub ].bv_val = malloc( l + 1 );
|
|
|
|
if ( subs[ nsub ].bv_val == NULL ) {
|
2003-12-30 00:14:47 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2003-11-15 02:39:18 +08:00
|
|
|
AC_MEMCPY( subs[ nsub ].bv_val, begin, l );
|
|
|
|
subs[ nsub ].bv_val[ l ] = '\0';
|
2001-05-12 08:51:28 +08:00
|
|
|
} else {
|
2003-11-15 02:39:18 +08:00
|
|
|
subs[ nsub ].bv_val = NULL;
|
|
|
|
subs[ nsub ].bv_len = 0;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Substitution pattern
|
|
|
|
*/
|
2002-04-08 17:43:22 +08:00
|
|
|
if ( isdigit( (unsigned char) p[ 1 ] ) ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
struct rewrite_submatch *tmpsm;
|
2003-11-15 18:02:44 +08:00
|
|
|
int d = p[ 1 ] - '0';
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new value substitution scheme
|
|
|
|
*/
|
2003-11-15 18:02:44 +08:00
|
|
|
|
|
|
|
tmpsm = ( struct rewrite_submatch * )realloc( submatch,
|
|
|
|
sizeof( struct rewrite_submatch )*( nsub + 1 ) );
|
2001-10-22 18:24:32 +08:00
|
|
|
if ( tmpsm == NULL ) {
|
2003-12-30 00:14:47 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2001-10-22 18:24:32 +08:00
|
|
|
submatch = tmpsm;
|
2003-11-15 02:39:18 +08:00
|
|
|
submatch[ nsub ].ls_submatch = d;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is no argument, use default
|
|
|
|
* (substitute substring as is)
|
|
|
|
*/
|
|
|
|
if ( p[ 2 ] != '{' ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
submatch[ nsub ].ls_type =
|
2001-05-12 08:51:28 +08:00
|
|
|
REWRITE_SUBMATCH_ASIS;
|
2003-11-15 18:02:44 +08:00
|
|
|
submatch[ nsub ].ls_map = NULL;
|
2001-05-12 08:51:28 +08:00
|
|
|
begin = ++p + 1;
|
2003-11-15 18:02:44 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
} else {
|
|
|
|
struct rewrite_map *map;
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
submatch[ nsub ].ls_type =
|
2001-05-12 08:51:28 +08:00
|
|
|
REWRITE_SUBMATCH_XMAP;
|
|
|
|
|
|
|
|
map = rewrite_xmap_parse( info,
|
2003-12-30 00:14:47 +08:00
|
|
|
p + 3, (const char **)&begin );
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( map == NULL ) {
|
2003-12-30 00:14:47 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2003-11-15 02:39:18 +08:00
|
|
|
submatch[ nsub ].ls_map = map;
|
2003-11-15 18:02:44 +08:00
|
|
|
p = begin - 1;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map with args ...
|
|
|
|
*/
|
|
|
|
} else if ( p[ 1 ] == '{' ) {
|
|
|
|
struct rewrite_map *map;
|
2003-11-15 02:39:18 +08:00
|
|
|
struct rewrite_submatch *tmpsm;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2003-12-30 00:14:47 +08:00
|
|
|
map = rewrite_map_parse( info, p + 2,
|
|
|
|
(const char **)&begin );
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( map == NULL ) {
|
2003-12-30 00:14:47 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
p = begin - 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new value substitution scheme
|
|
|
|
*/
|
2003-11-15 18:02:44 +08:00
|
|
|
tmpsm = ( struct rewrite_submatch * )realloc( submatch,
|
2003-11-15 02:39:18 +08:00
|
|
|
sizeof( struct rewrite_submatch )*( nsub + 1 ) );
|
2001-10-22 18:24:32 +08:00
|
|
|
if ( tmpsm == NULL ) {
|
2003-12-30 00:14:47 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2001-10-22 18:24:32 +08:00
|
|
|
submatch = tmpsm;
|
2003-11-15 02:39:18 +08:00
|
|
|
submatch[ nsub ].ls_type =
|
2001-05-12 08:51:28 +08:00
|
|
|
REWRITE_SUBMATCH_MAP_W_ARG;
|
2003-11-15 02:39:18 +08:00
|
|
|
submatch[ nsub ].ls_map = map;
|
2003-12-29 23:10:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Escape '%' ...
|
|
|
|
*/
|
|
|
|
} else if ( p[ 1 ] == '%' ) {
|
|
|
|
AC_MEMCPY( &p[ 1 ], &p[ 2 ], strlen( &p[ 1 ] ) );
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else {
|
2003-12-30 00:14:47 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2003-11-15 02:39:18 +08:00
|
|
|
|
|
|
|
nsub++;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Last part of string
|
|
|
|
*/
|
2003-11-15 18:02:44 +08:00
|
|
|
tmps = (struct berval * )realloc( subs, sizeof( struct berval )*( nsub + 1 ) );
|
2001-10-22 18:24:32 +08:00
|
|
|
if ( tmps == NULL ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
/*
|
|
|
|
* XXX need to free the value subst stuff!
|
|
|
|
*/
|
2003-11-15 18:02:44 +08:00
|
|
|
free( subs );
|
2003-12-30 00:14:47 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2001-10-22 18:24:32 +08:00
|
|
|
subs = tmps;
|
2001-05-12 08:51:28 +08:00
|
|
|
l = p - begin;
|
|
|
|
if ( l > 0 ) {
|
|
|
|
subs_len += l;
|
2003-11-15 02:39:18 +08:00
|
|
|
subs[ nsub ].bv_len = l;
|
|
|
|
subs[ nsub ].bv_val = malloc( l + 1 );
|
2008-09-10 03:58:47 +08:00
|
|
|
if ( subs[ nsub ].bv_val == NULL ) {
|
|
|
|
free( subs );
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2003-11-15 02:39:18 +08:00
|
|
|
AC_MEMCPY( subs[ nsub ].bv_val, begin, l );
|
|
|
|
subs[ nsub ].bv_val[ l ] = '\0';
|
2001-05-12 08:51:28 +08:00
|
|
|
} else {
|
2003-11-15 02:39:18 +08:00
|
|
|
subs[ nsub ].bv_val = NULL;
|
|
|
|
subs[ nsub ].bv_len = 0;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
s = calloc( sizeof( struct rewrite_subst ), 1 );
|
|
|
|
if ( s == NULL ) {
|
2003-12-30 00:14:47 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
s->lt_subs_len = subs_len;
|
|
|
|
s->lt_subs = subs;
|
|
|
|
s->lt_num_submatch = nsub;
|
|
|
|
s->lt_submatch = submatch;
|
|
|
|
|
2003-12-30 00:14:47 +08:00
|
|
|
cleanup:;
|
|
|
|
free( result );
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copies the match referred to by submatch and fetched in string by match.
|
|
|
|
* Helper for rewrite_rule_apply.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
submatch_copy(
|
|
|
|
struct rewrite_submatch *submatch,
|
|
|
|
const char *string,
|
|
|
|
const regmatch_t *match,
|
|
|
|
struct berval *val
|
|
|
|
)
|
|
|
|
{
|
2003-11-15 02:39:18 +08:00
|
|
|
int c, l;
|
|
|
|
const char *s;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
assert( submatch != NULL );
|
|
|
|
assert( submatch->ls_type == REWRITE_SUBMATCH_ASIS
|
|
|
|
|| submatch->ls_type == REWRITE_SUBMATCH_XMAP );
|
|
|
|
assert( string != NULL );
|
|
|
|
assert( match != NULL );
|
|
|
|
assert( val != NULL );
|
2003-11-15 02:39:18 +08:00
|
|
|
assert( val->bv_val == NULL );
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
c = submatch->ls_submatch;
|
|
|
|
s = string + match[ c ].rm_so;
|
|
|
|
l = match[ c ].rm_eo - match[ c ].rm_so;
|
|
|
|
|
|
|
|
val->bv_len = l;
|
2003-11-15 18:02:44 +08:00
|
|
|
val->bv_val = malloc( l + 1 );
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( val->bv_val == NULL ) {
|
|
|
|
return REWRITE_ERR;
|
|
|
|
}
|
|
|
|
|
2001-10-22 17:39:02 +08:00
|
|
|
AC_MEMCPY( val->bv_val, s, l );
|
2001-05-12 08:51:28 +08:00
|
|
|
val->bv_val[ l ] = '\0';
|
|
|
|
|
|
|
|
return REWRITE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Substitutes a portion of rewritten string according to substitution
|
|
|
|
* pattern using submatches
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rewrite_subst_apply(
|
|
|
|
struct rewrite_info *info,
|
|
|
|
struct rewrite_op *op,
|
|
|
|
struct rewrite_subst *subst,
|
|
|
|
const char *string,
|
|
|
|
const regmatch_t *match,
|
|
|
|
struct berval *val
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct berval *submatch = NULL;
|
|
|
|
char *res = NULL;
|
2003-11-15 02:39:18 +08:00
|
|
|
int n = 0, l, cl;
|
2001-05-12 08:51:28 +08:00
|
|
|
int rc = REWRITE_REGEXEC_OK;
|
|
|
|
|
|
|
|
assert( info != NULL );
|
|
|
|
assert( op != NULL );
|
|
|
|
assert( subst != NULL );
|
|
|
|
assert( string != NULL );
|
|
|
|
assert( match != NULL );
|
|
|
|
assert( val != NULL );
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
assert( val->bv_val == NULL );
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
val->bv_val = NULL;
|
|
|
|
val->bv_len = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare room for submatch expansion
|
|
|
|
*/
|
2003-04-05 19:30:43 +08:00
|
|
|
if ( subst->lt_num_submatch > 0 ) {
|
|
|
|
submatch = calloc( sizeof( struct berval ),
|
|
|
|
subst->lt_num_submatch );
|
|
|
|
if ( submatch == NULL ) {
|
|
|
|
return REWRITE_REGEXEC_ERR;
|
|
|
|
}
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resolve submatches (simple subst, map expansion and so).
|
|
|
|
*/
|
|
|
|
for ( n = 0, l = 0; n < subst->lt_num_submatch; n++ ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
struct berval key = { 0, NULL };
|
|
|
|
|
|
|
|
submatch[ n ].bv_val = NULL;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get key
|
|
|
|
*/
|
2003-11-15 02:39:18 +08:00
|
|
|
switch ( subst->lt_submatch[ n ].ls_type ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
case REWRITE_SUBMATCH_ASIS:
|
|
|
|
case REWRITE_SUBMATCH_XMAP:
|
2003-11-15 02:39:18 +08:00
|
|
|
rc = submatch_copy( &subst->lt_submatch[ n ],
|
2001-05-12 08:51:28 +08:00
|
|
|
string, match, &key );
|
|
|
|
if ( rc != REWRITE_SUCCESS ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
rc = REWRITE_REGEXEC_ERR;
|
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_SUBMATCH_MAP_W_ARG:
|
2003-11-15 02:39:18 +08:00
|
|
|
switch ( subst->lt_submatch[ n ].ls_map->lm_type ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
case REWRITE_MAP_GET_OP_VAR:
|
|
|
|
case REWRITE_MAP_GET_SESN_VAR:
|
|
|
|
case REWRITE_MAP_GET_PARAM:
|
|
|
|
rc = REWRITE_SUCCESS;
|
|
|
|
break;
|
2003-11-15 02:39:18 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
default:
|
|
|
|
rc = rewrite_subst_apply( info, op,
|
2003-11-15 02:39:18 +08:00
|
|
|
subst->lt_submatch[ n ].ls_map->lm_subst,
|
2001-05-12 08:51:28 +08:00
|
|
|
string, match, &key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rc != REWRITE_SUCCESS ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-11-15 02:39:18 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "Not Implemented\n", 0, 0, 0 );
|
2001-05-12 08:51:28 +08:00
|
|
|
rc = REWRITE_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rc != REWRITE_SUCCESS ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
rc = REWRITE_REGEXEC_ERR;
|
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resolve key
|
|
|
|
*/
|
2003-11-15 02:39:18 +08:00
|
|
|
switch ( subst->lt_submatch[ n ].ls_type ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
case REWRITE_SUBMATCH_ASIS:
|
|
|
|
submatch[ n ] = key;
|
|
|
|
rc = REWRITE_SUCCESS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_SUBMATCH_XMAP:
|
|
|
|
rc = rewrite_xmap_apply( info, op,
|
2003-11-15 02:39:18 +08:00
|
|
|
subst->lt_submatch[ n ].ls_map,
|
2001-05-12 08:51:28 +08:00
|
|
|
&key, &submatch[ n ] );
|
2003-11-15 18:02:44 +08:00
|
|
|
free( key.bv_val );
|
|
|
|
key.bv_val = NULL;
|
2001-05-12 08:51:28 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_SUBMATCH_MAP_W_ARG:
|
|
|
|
rc = rewrite_map_apply( info, op,
|
2003-11-15 02:39:18 +08:00
|
|
|
subst->lt_submatch[ n ].ls_map,
|
2001-05-12 08:51:28 +08:00
|
|
|
&key, &submatch[ n ] );
|
2003-11-15 18:02:44 +08:00
|
|
|
free( key.bv_val );
|
|
|
|
key.bv_val = NULL;
|
2001-05-12 08:51:28 +08:00
|
|
|
break;
|
2003-11-15 02:39:18 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* When implemented, this might return the
|
|
|
|
* exit status of a rewrite context,
|
|
|
|
* which may include a stop, or an
|
|
|
|
* unwilling to perform
|
|
|
|
*/
|
|
|
|
rc = REWRITE_ERR;
|
|
|
|
break;
|
|
|
|
}
|
2003-12-30 00:14:47 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( rc != REWRITE_SUCCESS ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
rc = REWRITE_REGEXEC_ERR;
|
2004-02-28 20:04:06 +08:00
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Increment the length of the resulting string
|
|
|
|
*/
|
|
|
|
l += submatch[ n ].bv_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-11-29 19:10:16 +08:00
|
|
|
* Alloc result buffer
|
2001-05-12 08:51:28 +08:00
|
|
|
*/
|
|
|
|
l += subst->lt_subs_len;
|
2003-11-15 18:02:44 +08:00
|
|
|
res = malloc( l + 1 );
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( res == NULL ) {
|
2003-04-05 19:30:43 +08:00
|
|
|
rc = REWRITE_REGEXEC_ERR;
|
|
|
|
goto cleanup;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-11-29 19:10:16 +08:00
|
|
|
* Apply submatches (possibly resolved thru maps)
|
2001-05-12 08:51:28 +08:00
|
|
|
*/
|
|
|
|
for ( n = 0, cl = 0; n < subst->lt_num_submatch; n++ ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
if ( subst->lt_subs[ n ].bv_val != NULL ) {
|
|
|
|
AC_MEMCPY( res + cl, subst->lt_subs[ n ].bv_val,
|
|
|
|
subst->lt_subs[ n ].bv_len );
|
|
|
|
cl += subst->lt_subs[ n ].bv_len;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2001-10-22 17:39:02 +08:00
|
|
|
AC_MEMCPY( res + cl, submatch[ n ].bv_val,
|
|
|
|
submatch[ n ].bv_len );
|
2001-05-12 08:51:28 +08:00
|
|
|
cl += submatch[ n ].bv_len;
|
|
|
|
}
|
2003-11-15 02:39:18 +08:00
|
|
|
if ( subst->lt_subs[ n ].bv_val != NULL ) {
|
|
|
|
AC_MEMCPY( res + cl, subst->lt_subs[ n ].bv_val,
|
|
|
|
subst->lt_subs[ n ].bv_len );
|
2003-11-15 18:02:44 +08:00
|
|
|
cl += subst->lt_subs[ n ].bv_len;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2003-11-15 18:02:44 +08:00
|
|
|
res[ cl ] = '\0';
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
val->bv_val = res;
|
|
|
|
val->bv_len = l;
|
2003-04-05 19:30:43 +08:00
|
|
|
|
|
|
|
cleanup:;
|
|
|
|
if ( submatch ) {
|
2003-11-15 02:39:18 +08:00
|
|
|
for ( ; --n >= 0; ) {
|
|
|
|
if ( submatch[ n ].bv_val ) {
|
|
|
|
free( submatch[ n ].bv_val );
|
|
|
|
}
|
|
|
|
}
|
2003-04-05 19:30:43 +08:00
|
|
|
free( submatch );
|
|
|
|
}
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
/*
|
|
|
|
* frees data
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rewrite_subst_destroy(
|
|
|
|
struct rewrite_subst **psubst
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
struct rewrite_subst *subst;
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( psubst != NULL );
|
|
|
|
assert( *psubst != NULL );
|
2003-11-15 02:39:18 +08:00
|
|
|
|
|
|
|
subst = *psubst;
|
|
|
|
|
|
|
|
for ( n = 0; n < subst->lt_num_submatch; n++ ) {
|
|
|
|
if ( subst->lt_subs[ n ].bv_val ) {
|
|
|
|
free( subst->lt_subs[ n ].bv_val );
|
|
|
|
subst->lt_subs[ n ].bv_val = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( subst->lt_submatch[ n ].ls_type ) {
|
|
|
|
case REWRITE_SUBMATCH_ASIS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_SUBMATCH_XMAP:
|
|
|
|
rewrite_xmap_destroy( &subst->lt_submatch[ n ].ls_map );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_SUBMATCH_MAP_W_ARG:
|
|
|
|
rewrite_map_destroy( &subst->lt_submatch[ n ].ls_map );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-15 18:02:44 +08:00
|
|
|
free( subst->lt_submatch );
|
|
|
|
subst->lt_submatch = NULL;
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
/* last one */
|
|
|
|
if ( subst->lt_subs[ n ].bv_val ) {
|
|
|
|
free( subst->lt_subs[ n ].bv_val );
|
|
|
|
subst->lt_subs[ n ].bv_val = NULL;
|
|
|
|
}
|
|
|
|
|
2003-11-15 18:02:44 +08:00
|
|
|
free( subst->lt_subs );
|
|
|
|
subst->lt_subs = NULL;
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
free( subst );
|
|
|
|
*psubst = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|