X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibrewrite%2Frewrite.c;h=673a8640767128a400fc1aa4c77c4efc731dee0b;hb=55339651d6d6e21dc85dca7949cb094e57a1f6b2;hp=6097b61f71cde699ebeed99f17ec87a2c0a83f89;hpb=1aa97d5f87981a4f113f28d0852f2ab130c4e2f7;p=openldap diff --git a/libraries/librewrite/rewrite.c b/libraries/librewrite/rewrite.c index 6097b61f71..673a864076 100644 --- a/libraries/librewrite/rewrite.c +++ b/libraries/librewrite/rewrite.c @@ -1,26 +1,21 @@ -/****************************************************************************** +/* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . * - * Copyright (C) 2000 Pierangelo Masarati, + * Copyright 2000-2012 The OpenLDAP Foundation. * All rights reserved. * - * Permission is granted to anyone to use this software for any purpose - * on any computer system, and to alter it and redistribute it, subject - * to the following restrictions: + * Redistribution and use in source and binary forms, with or without + * modification, are permitted only as authorized by the OpenLDAP + * Public License. * - * 1. The author is not responsible for the consequences of use of this - * software, no matter how awful, even if they arise from flaws in it. - * - * 2. The origin of this software must not be misrepresented, either by - * explicit claim or by omission. Since few users ever read sources, - * credits should appear in the documentation. - * - * 3. Altered versions must be plainly marked as such, and must not be - * misrepresented as being the original software. Since few users - * ever read sources, credits should appear in the documentation. - * - * 4. This notice may not be removed or altered. - * - ******************************************************************************/ + * A copy of this license is available in the file LICENSE in the + * top-level directory of the distribution or, alternatively, at + * . + */ +/* ACKNOWLEDGEMENT: + * This work was initially developed by Pierangelo Masarati for + * inclusion in OpenLDAP Software. + */ #include @@ -32,20 +27,17 @@ #include #include #include +#include #include +#include +#include int ldap_debug; int ldap_syslog; int ldap_syslog_level; -extern int -read_rewrite( - FILE *fin, - struct rewrite_info *info -); - -char * +static void apply( FILE *fin, const char *rewriteContext, @@ -57,9 +49,9 @@ apply( int rc; void *cookie = &info; - info = rewrite_info_init(REWRITE_MODE_ERR); + info = rewrite_info_init( REWRITE_MODE_ERR ); - if ( read_rewrite( fin, info ) != 0 ) { + if ( rewrite_read( fin, info ) != 0 ) { exit( EXIT_FAILURE ); } @@ -67,47 +59,92 @@ apply( rewrite_session_init( info, cookie ); - string = strdup( arg ); + string = (char *)arg; for ( sep = strchr( rewriteContext, ',' ); rewriteContext != NULL; rewriteContext = sep, - sep ? sep = strchr( rewriteContext, ',' ) : NULL ) { + sep ? sep = strchr( rewriteContext, ',' ) : NULL ) + { + char *errmsg = ""; + if ( sep != NULL ) { sep[ 0 ] = '\0'; sep++; } - // rc = rewrite( info, rewriteContext, string, &result ); + /* rc = rewrite( info, rewriteContext, string, &result ); */ rc = rewrite_session( info, rewriteContext, string, cookie, &result ); - fprintf( stdout, "%s -> %s\n", string, - ( result ? result : "unwilling to perform" ) ); + switch ( rc ) { + case REWRITE_REGEXEC_OK: + errmsg = "ok"; + break; + + case REWRITE_REGEXEC_ERR: + errmsg = "error"; + break; + + case REWRITE_REGEXEC_STOP: + errmsg = "stop"; + break; + + case REWRITE_REGEXEC_UNWILLING: + errmsg = "unwilling to perform"; + break; + + default: + if (rc >= REWRITE_REGEXEC_USER) { + errmsg = "user-defined"; + } else { + errmsg = "unknown"; + } + break; + } + + fprintf( stdout, "%s -> %s [%d:%s]\n", string, + ( result ? result : "(null)" ), + rc, errmsg ); if ( result == NULL ) { break; } - free( string ); + if ( string != arg && string != result ) { + free( string ); + } string = result; } + if ( result && result != arg ) { + free( result ); + } + rewrite_session_delete( info, cookie ); - return result; + rewrite_info_delete( &info ); } int main( int argc, char *argv[] ) { - FILE *fin = NULL; - char *rewriteContext = REWRITE_DEFAULT_CONTEXT; + FILE *fin = NULL; + char *rewriteContext = REWRITE_DEFAULT_CONTEXT; + int debug = 0; while ( 1 ) { - int opt = getopt( argc, argv, "f:hr:" ); + int opt = getopt( argc, argv, "d:f:hr:" ); if ( opt == EOF ) { break; } switch ( opt ) { + case 'd': + if ( lutil_atoi( &debug, optarg ) != 0 ) { + fprintf( stderr, "illegal log level '%s'\n", + optarg ); + exit( EXIT_FAILURE ); + } + break; + case 'f': fin = fopen( optarg, "r" ); if ( fin == NULL ) { @@ -133,17 +170,26 @@ main( int argc, char *argv[] ) exit( EXIT_SUCCESS ); case 'r': - rewriteContext = strdup( optarg ); + rewriteContext = optarg; break; } } + if ( debug != 0 ) { + ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug); + ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug); + } + if ( optind >= argc ) { return -1; } apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] ); + if ( fin ) { + fclose( fin ); + } + return 0; }