X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibrewrite%2Frewrite.c;h=51e8fd5a3f3c20ae1c394abebdd2de538078b3e5;hb=4ace645df8644a70f39396a277adcd15242a8dd4;hp=6753f996003d08df551eb18f002edd7c1fbebd2f;hpb=74fa239a201cd2d785fe34bdbaf6804161bdb231;p=openldap diff --git a/libraries/librewrite/rewrite.c b/libraries/librewrite/rewrite.c index 6753f99600..51e8fd5a3f 100644 --- a/libraries/librewrite/rewrite.c +++ b/libraries/librewrite/rewrite.c @@ -1,48 +1,41 @@ -/****************************************************************************** +/* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . * - * Copyright (C) 2000 Pierangelo Masarati, + * Copyright 2000-2005 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 #include +#include #include #include #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 * apply( FILE *fin, @@ -55,9 +48,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 ); } @@ -70,16 +63,45 @@ apply( rewriteContext != NULL; rewriteContext = sep, 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; } @@ -87,25 +109,40 @@ apply( string = result; } + free( string ); + rewrite_session_delete( info, cookie ); + rewrite_info_delete( &info ); + return result; } 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; + char *next; 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': + debug = strtol( optarg, &next, 10 ); + if ( next == NULL || next[0] != '\0' ) { + fprintf( stderr, "illegal log level '%s'\n", + optarg ); + exit( EXIT_FAILURE ); + } + break; + case 'f': fin = fopen( optarg, "r" ); if ( fin == NULL ) { @@ -131,17 +168,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; }