]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rewrite.c
fix wrong copyright date
[openldap] / libraries / librewrite / rewrite.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2000-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENT:
16  * This work was initially developed by Pierangelo Masarati for
17  * inclusion in OpenLDAP Software.
18  */
19
20 #include <portable.h>
21
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/syslog.h>
25 #include <ac/regex.h>
26 #include <ac/socket.h>
27 #include <ac/unistd.h>
28 #include <ac/ctype.h>
29 #include <ac/string.h>
30 #include <stdio.h>
31
32 #include <rewrite.h>
33
34 int ldap_debug;
35 int ldap_syslog;
36 int ldap_syslog_level;
37
38 char *
39 apply( 
40                 FILE *fin, 
41                 const char *rewriteContext,
42                 const char *arg
43 )
44 {
45         struct rewrite_info *info;
46         char *string, *sep, *result = NULL;
47         int rc;
48         void *cookie = &info;
49
50         info = rewrite_info_init( REWRITE_MODE_ERR );
51
52         if ( rewrite_read( fin, info ) != 0 ) {
53                 exit( EXIT_FAILURE );
54         }
55
56         rewrite_param_set( info, "prog", "rewrite" );
57
58         rewrite_session_init( info, cookie );
59
60         string = strdup( arg );
61         for ( sep = strchr( rewriteContext, ',' );
62                         rewriteContext != NULL;
63                         rewriteContext = sep,
64                         sep ? sep = strchr( rewriteContext, ',' ) : NULL ) {
65                 if ( sep != NULL ) {
66                         sep[ 0 ] = '\0';
67                         sep++;
68                 }
69                 /* rc = rewrite( info, rewriteContext, string, &result ); */
70                 rc = rewrite_session( info, rewriteContext, string,
71                                 cookie, &result );
72                 
73                 fprintf( stdout, "%s -> %s\n", string, 
74                                 ( result ? result : "unwilling to perform" ) );
75                 if ( result == NULL ) {
76                         break;
77                 }
78                 free( string );
79                 string = result;
80         }
81
82         free( string );
83
84         rewrite_session_delete( info, cookie );
85
86         rewrite_info_delete( &info );
87
88         return result;
89 }
90
91 int
92 main( int argc, char *argv[] )
93 {
94         FILE *fin = NULL;
95         char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
96
97         while ( 1 ) {
98                 int opt = getopt( argc, argv, "f:hr:" );
99
100                 if ( opt == EOF ) {
101                         break;
102                 }
103
104                 switch ( opt ) {
105                 case 'f':
106                         fin = fopen( optarg, "r" );
107                         if ( fin == NULL ) {
108                                 fprintf( stderr, "unable to open file '%s'\n",
109                                                 optarg );
110                                 exit( EXIT_FAILURE );
111                         }
112                         break;
113                         
114                 case 'h':
115                         fprintf( stderr, 
116         "usage: rewrite [options] string\n"
117         "\n"
118         "\t\t-f file\t\tconfiguration file\n"
119         "\t\t-r rule[s]\tlist of comma-separated rules\n"
120         "\n"
121         "\tsyntax:\n"
122         "\t\trewriteEngine\t{on|off}\n"
123         "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
124         "\t\trewriteRule\tpattern subst [flags]\n"
125         "\n" 
126                                 );
127                         exit( EXIT_SUCCESS );
128                         
129                 case 'r':
130                         rewriteContext = optarg;
131                         break;
132                 }
133         }
134         
135         if ( optind >= argc ) {
136                 return -1;
137         }
138
139         apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
140
141         if ( fin ) {
142                 fclose( fin );
143         }
144
145         return 0;
146 }
147