]> git.sur5r.net Git - openldap/blobdiff - libraries/librewrite/rewrite.c
Happy new year!
[openldap] / libraries / librewrite / rewrite.c
index 7e5b28c7f7aee8e785780e4f75be6118ec5bd0ec..51e8fd5a3f3c20ae1c394abebdd2de538078b3e5 100644 (file)
@@ -1,49 +1,41 @@
-/******************************************************************************
+/* $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>
 
 #include <ac/stdlib.h>
+#include <ac/string.h>
 #include <ac/syslog.h>
 #include <ac/regex.h>
 #include <ac/socket.h>
 #include <ac/unistd.h>
 #include <ac/ctype.h>
 #include <ac/string.h>
+#include <stdio.h>
 
 #include <rewrite.h>
+#include <ldap.h>
 
 int ldap_debug;
 int ldap_syslog;
 int ldap_syslog_level;
 
-extern int
-read_rewrite(
-               FILE *fin,
-               struct rewrite_info *info
-);
-
 char *
 apply( 
                FILE *fin, 
@@ -56,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 );
        }
 
@@ -71,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;
                }
@@ -88,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 ) {
@@ -132,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;
 }