]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rewrite.c
Happy New Year!
[openldap] / libraries / librewrite / rewrite.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2000-2005 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 #include <ldap.h>
34
35 int ldap_debug;
36 int ldap_syslog;
37 int ldap_syslog_level;
38
39 static void
40 apply( 
41                 FILE *fin, 
42                 const char *rewriteContext,
43                 const char *arg
44 )
45 {
46         struct rewrite_info *info;
47         char *string, *sep, *result = NULL;
48         int rc;
49         void *cookie = &info;
50
51         info = rewrite_info_init( REWRITE_MODE_ERR );
52
53         if ( rewrite_read( fin, info ) != 0 ) {
54                 exit( EXIT_FAILURE );
55         }
56
57         rewrite_param_set( info, "prog", "rewrite" );
58
59         rewrite_session_init( info, cookie );
60
61         string = (char *)arg;
62         for ( sep = strchr( rewriteContext, ',' );
63                         rewriteContext != NULL;
64                         rewriteContext = sep,
65                         sep ? sep = strchr( rewriteContext, ',' ) : NULL )
66         {
67                 char    *errmsg = "";
68
69                 if ( sep != NULL ) {
70                         sep[ 0 ] = '\0';
71                         sep++;
72                 }
73                 /* rc = rewrite( info, rewriteContext, string, &result ); */
74                 rc = rewrite_session( info, rewriteContext, string,
75                                 cookie, &result );
76                 
77                 switch ( rc ) {
78                 case REWRITE_REGEXEC_OK:
79                         errmsg = "ok";
80                         break;
81
82                 case REWRITE_REGEXEC_ERR:
83                         errmsg = "error";
84                         break;
85
86                 case REWRITE_REGEXEC_STOP:
87                         errmsg = "stop";
88                         break;
89
90                 case REWRITE_REGEXEC_UNWILLING:
91                         errmsg = "unwilling to perform";
92                         break;
93
94                 default:
95                         if (rc >= REWRITE_REGEXEC_USER) {
96                                 errmsg = "user-defined";
97                         } else {
98                                 errmsg = "unknown";
99                         }
100                         break;
101                 }
102                 
103                 fprintf( stdout, "%s -> %s [%d:%s]\n", string, 
104                                 ( result ? result : "(null)" ),
105                                 rc, errmsg );
106                 if ( result == NULL ) {
107                         break;
108                 }
109                 if ( string != arg && string != result ) {
110                         free( string );
111                 }
112                 string = result;
113         }
114
115         if ( result && result != arg ) {
116                 free( result );
117         }
118
119         rewrite_session_delete( info, cookie );
120
121         rewrite_info_delete( &info );
122 }
123
124 int
125 main( int argc, char *argv[] )
126 {
127         FILE    *fin = NULL;
128         char    *rewriteContext = REWRITE_DEFAULT_CONTEXT;
129         int     debug = 0;
130         char    *next;
131
132         while ( 1 ) {
133                 int opt = getopt( argc, argv, "d:f:hr:" );
134
135                 if ( opt == EOF ) {
136                         break;
137                 }
138
139                 switch ( opt ) {
140                 case 'd':
141                         debug = strtol( optarg, &next, 10 );
142                         if ( next == NULL || next[0] != '\0' ) {
143                                 fprintf( stderr, "illegal log level '%s'\n",
144                                                 optarg );
145                                 exit( EXIT_FAILURE );
146                         }
147                         break;
148
149                 case 'f':
150                         fin = fopen( optarg, "r" );
151                         if ( fin == NULL ) {
152                                 fprintf( stderr, "unable to open file '%s'\n",
153                                                 optarg );
154                                 exit( EXIT_FAILURE );
155                         }
156                         break;
157                         
158                 case 'h':
159                         fprintf( stderr, 
160         "usage: rewrite [options] string\n"
161         "\n"
162         "\t\t-f file\t\tconfiguration file\n"
163         "\t\t-r rule[s]\tlist of comma-separated rules\n"
164         "\n"
165         "\tsyntax:\n"
166         "\t\trewriteEngine\t{on|off}\n"
167         "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
168         "\t\trewriteRule\tpattern subst [flags]\n"
169         "\n" 
170                                 );
171                         exit( EXIT_SUCCESS );
172                         
173                 case 'r':
174                         rewriteContext = optarg;
175                         break;
176                 }
177         }
178         
179         if ( debug != 0 ) {
180                 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
181                 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
182         }
183         
184         if ( optind >= argc ) {
185                 return -1;
186         }
187
188         apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
189
190         if ( fin ) {
191                 fclose( fin );
192         }
193
194         return 0;
195 }
196