1 /******************************************************************************
3 * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
6 * Permission is granted to anyone to use this software for any purpose
7 * on any computer system, and to alter it and redistribute it, subject
8 * to the following restrictions:
10 * 1. The author is not responsible for the consequences of use of this
11 * software, no matter how awful, even if they arise from flaws in it.
13 * 2. The origin of this software must not be misrepresented, either by
14 * explicit claim or by omission. Since few users ever read sources,
15 * credits should appear in the documentation.
17 * 3. Altered versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software. Since few users
19 * ever read sources, credits should appear in the documentation.
21 * 4. This notice may not be removed or altered.
23 ******************************************************************************/
27 #include "rewrite-int.h"
30 * Compiles a substitution pattern
32 struct rewrite_subst *
33 rewrite_subst_compile(
34 struct rewrite_info *info,
39 struct berval **subs = NULL;
40 struct rewrite_submatch **submatch = NULL;
42 struct rewrite_subst *s = NULL;
44 const char *begin, *p;
47 assert( info != NULL );
48 assert( result != NULL );
51 * Take care of substitution string
53 for ( p = begin = result, subs_len = 0; p[ 0 ] != '\0'; p++ ) {
56 * Keep only single escapes '\'
58 if ( p[ 0 ] != REWRITE_SUBMATCH_ESCAPE ) {
60 } else if ( p[ 1 ] == REWRITE_SUBMATCH_ESCAPE ) {
66 subs = (struct berval **)realloc( subs,
67 sizeof( struct berval * )*( nsub + 1 ) );
75 * I think an `if l > 0' at runtime is better outside than
76 * inside a function call ...
82 calloc( sizeof( struct berval ), 1 );
83 if ( subs[ nsub - 1 ] == NULL ) {
87 subs[ nsub - 1 ]->bv_len = l;
88 subs[ nsub - 1 ]->bv_val = malloc( l + 1 );
89 if ( subs[ nsub - 1 ]->bv_val == NULL ) {
92 strncpy( subs[ nsub - 1 ]->bv_val, begin, l );
93 subs[ nsub - 1 ]->bv_val[ l ] = '\0';
95 subs[ nsub - 1 ] = NULL;
99 * Substitution pattern
101 if ( isdigit( p[ 1 ] ) ) {
102 int d = p[ 1 ] - '0';
105 * Add a new value substitution scheme
107 submatch = realloc( submatch,
108 sizeof( struct rewrite_submatch * )*( nsub + 1 ) );
109 if ( submatch == NULL ) {
113 submatch[ nsub ] = NULL;
114 submatch[ nsub - 1 ] =
115 calloc( sizeof( struct rewrite_submatch ), 1 );
116 if ( submatch[ nsub - 1 ] == NULL ) {
120 submatch[ nsub - 1 ]->ls_submatch = d;
123 * If there is no argument, use default
124 * (substitute substring as is)
126 if ( p[ 2 ] != '{' ) {
127 submatch[ nsub - 1 ]->ls_type =
128 REWRITE_SUBMATCH_ASIS;
131 struct rewrite_map *map;
133 submatch[ nsub - 1 ]->ls_type =
134 REWRITE_SUBMATCH_XMAP;
136 map = rewrite_xmap_parse( info,
144 submatch[ nsub - 1 ]->ls_map = map;
150 } else if ( p[ 1 ] == '{' ) {
151 struct rewrite_map *map;
153 map = rewrite_map_parse( info, p + 2, &begin );
161 * Add a new value substitution scheme
163 submatch = realloc( submatch,
164 sizeof( struct rewrite_submatch * )*( nsub + 1 ) );
165 if ( submatch == NULL ) {
169 submatch[ nsub ] = NULL;
170 submatch[ nsub - 1 ] =
171 calloc( sizeof( struct rewrite_submatch ), 1 );
172 if ( submatch[ nsub - 1 ] == NULL ) {
177 submatch[ nsub - 1 ]->ls_type =
178 REWRITE_SUBMATCH_MAP_W_ARG;
180 submatch[ nsub - 1 ]->ls_map = map;
185 * Last part of string
187 subs = realloc( subs, sizeof( struct berval *)*( nsub + 2 ) );
188 if ( subs == NULL ) {
190 * XXX need to free the value subst stuff!
196 subs[ nsub + 1 ] = NULL;
199 subs[ nsub ] = calloc( sizeof( struct berval ), 1 );
201 subs[ nsub ]->bv_len = l;
202 subs[ nsub ]->bv_val = malloc( l + 1 );
203 strncpy( subs[ nsub ]->bv_val, begin, l );
204 subs[ nsub ]->bv_val[ l ] = '\0';
209 s = calloc( sizeof( struct rewrite_subst ), 1 );
215 s->lt_subs_len = subs_len;
217 s->lt_num_submatch = nsub;
218 s->lt_submatch = submatch;
224 * Copies the match referred to by submatch and fetched in string by match.
225 * Helper for rewrite_rule_apply.
229 struct rewrite_submatch *submatch,
231 const regmatch_t *match,
238 assert( submatch != NULL );
239 assert( submatch->ls_type == REWRITE_SUBMATCH_ASIS
240 || submatch->ls_type == REWRITE_SUBMATCH_XMAP );
241 assert( string != NULL );
242 assert( match != NULL );
243 assert( val != NULL );
245 c = submatch->ls_submatch;
246 s = string + match[ c ].rm_so;
247 l = match[ c ].rm_eo - match[ c ].rm_so;
251 val->bv_val = calloc( sizeof( char ), l + 1 );
252 if ( val->bv_val == NULL ) {
256 strncpy( val->bv_val, s, l );
257 val->bv_val[ l ] = '\0';
259 return REWRITE_SUCCESS;
263 * Substitutes a portion of rewritten string according to substitution
264 * pattern using submatches
268 struct rewrite_info *info,
269 struct rewrite_op *op,
270 struct rewrite_subst *subst,
272 const regmatch_t *match,
276 struct berval *submatch = NULL;
279 int rc = REWRITE_REGEXEC_OK;
281 assert( info != NULL );
282 assert( op != NULL );
283 assert( subst != NULL );
284 assert( string != NULL );
285 assert( match != NULL );
286 assert( val != NULL );
292 * Prepare room for submatch expansion
294 submatch = calloc( sizeof( struct berval ),
295 subst->lt_num_submatch );
296 if ( submatch == NULL ) {
297 return REWRITE_REGEXEC_ERR;
301 * Resolve submatches (simple subst, map expansion and so).
303 for ( n = 0, l = 0; n < subst->lt_num_submatch; n++ ) {
310 switch( subst->lt_submatch[ n ]->ls_type ) {
311 case REWRITE_SUBMATCH_ASIS:
312 case REWRITE_SUBMATCH_XMAP:
313 rc = submatch_copy( subst->lt_submatch[ n ],
314 string, match, &key );
315 if ( rc != REWRITE_SUCCESS ) {
317 return REWRITE_REGEXEC_ERR;
321 case REWRITE_SUBMATCH_MAP_W_ARG:
322 switch ( subst->lt_submatch[ n ]->ls_map->lm_type ) {
323 case REWRITE_MAP_GET_OP_VAR:
324 case REWRITE_MAP_GET_SESN_VAR:
325 case REWRITE_MAP_GET_PARAM:
326 rc = REWRITE_SUCCESS;
329 rc = rewrite_subst_apply( info, op,
330 subst->lt_submatch[ n ]->ls_map->lm_subst,
331 string, match, &key);
334 if ( rc != REWRITE_SUCCESS ) {
336 return REWRITE_REGEXEC_ERR;
341 Debug( LDAP_DEBUG_ANY, "Not Implemented\n%s%s%s",
347 if ( rc != REWRITE_SUCCESS ) {
349 return REWRITE_REGEXEC_ERR;
355 switch ( subst->lt_submatch[ n ]->ls_type ) {
356 case REWRITE_SUBMATCH_ASIS:
358 rc = REWRITE_SUCCESS;
361 case REWRITE_SUBMATCH_XMAP:
362 rc = rewrite_xmap_apply( info, op,
363 subst->lt_submatch[ n ]->ls_map,
364 &key, &submatch[ n ] );
367 case REWRITE_SUBMATCH_MAP_W_ARG:
368 rc = rewrite_map_apply( info, op,
369 subst->lt_submatch[ n ]->ls_map,
370 &key, &submatch[ n ] );
375 * When implemented, this might return the
376 * exit status of a rewrite context,
377 * which may include a stop, or an
378 * unwilling to perform
384 if ( rc != REWRITE_SUCCESS ) {
386 return REWRITE_REGEXEC_ERR;
390 * Increment the length of the resulting string
392 l += submatch[ n ].bv_len;
396 * Alloc result buffer as big as the constant part
397 * of the subst pattern and initialize it
399 l += subst->lt_subs_len;
400 res = calloc( sizeof( char ), l + 1 );
403 return REWRITE_REGEXEC_ERR;
407 * Apply submatches (possibly resolved thru maps
409 for ( n = 0, cl = 0; n < subst->lt_num_submatch; n++ ) {
410 if ( subst->lt_subs[ n ] != NULL ) {
411 strcpy( res + cl, subst->lt_subs[ n ]->bv_val);
412 cl += subst->lt_subs[ n ]->bv_len;
414 strcpy( res + cl, submatch[ n ].bv_val );
415 cl += submatch[ n ].bv_len;
416 free( submatch[ n ].bv_val );
418 if ( subst->lt_subs[ n ] != NULL ) {
419 strcpy( res + cl, subst->lt_subs[ n ]->bv_val );