]> git.sur5r.net Git - openldap/blobdiff - libraries/librewrite/rewrite.c
Happy new year!
[openldap] / libraries / librewrite / rewrite.c
index ac301b7a51c0b106f920fb4b410145577d55c95a..51e8fd5a3f3c20ae1c394abebdd2de538078b3e5 100644 (file)
@@ -1,26 +1,21 @@
-/******************************************************************************
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
+ * 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
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* ACKNOWLEDGEMENT:
+ * This work was initially developed by Pierangelo Masarati for
+ * inclusion in OpenLDAP Software.
+ */
 
 #include <portable.h>
 
@@ -35,6 +30,7 @@
 #include <stdio.h>
 
 #include <rewrite.h>
+#include <ldap.h>
 
 int ldap_debug;
 int ldap_syslog;
@@ -52,7 +48,7 @@ apply(
        int rc;
        void *cookie = &info;
 
-       info = rewrite_info_init(REWRITE_MODE_ERR);
+       info = rewrite_info_init( REWRITE_MODE_ERR );
 
        if ( rewrite_read( fin, info ) != 0 ) {
                exit( EXIT_FAILURE );
@@ -67,6 +63,8 @@ apply(
                        rewriteContext != NULL;
                        rewriteContext = sep,
                        sep ? sep = strchr( rewriteContext, ',' ) : NULL ) {
+               char    *errmsg = "";
+
                if ( sep != NULL ) {
                        sep[ 0 ] = '\0';
                        sep++;
@@ -75,8 +73,35 @@ apply(
                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;
                }
@@ -84,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 ) {
@@ -128,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;
 }