]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rewrite.c
56eb66bfd648ca08369561c8af4dcefabc80e5e2
[openldap] / libraries / librewrite / rewrite.c
1 /******************************************************************************
2  *
3  * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
4  * All rights reserved.
5  *
6  * Permission is granted to anyone to use this software for any purpose
7  * on any computer system, and to alter it and redistribute it, subject
8  * to the following restrictions:
9  *
10  * 1. The author is not responsible for the consequences of use of this
11  * software, no matter how awful, even if they arise from flaws in it.
12  *
13  * 2. The origin of this software must not be misrepresented, either by
14  * explicit claim or by omission.  Since few users ever read sources,
15  * credits should appear in the documentation.
16  *
17  * 3. Altered versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.  Since few users
19  * ever read sources, credits should appear in the documentation.
20  * 
21  * 4. This notice may not be removed or altered.
22  *
23  ******************************************************************************/
24
25 #include <portable.h>
26
27 #include <ac/stdlib.h>
28 #include <ac/string.h>
29 #include <ac/syslog.h>
30 #include <ac/regex.h>
31 #include <ac/socket.h>
32 #include <ac/unistd.h>
33 #include <ac/ctype.h>
34 #include <ac/string.h>
35
36 #include <rewrite.h>
37
38 int ldap_debug;
39 int ldap_syslog;
40 int ldap_syslog_level;
41
42 extern int
43 read_rewrite(
44                 FILE *fin,
45                 struct rewrite_info *info
46 );
47
48 char *
49 apply( 
50                 FILE *fin, 
51                 const char *rewriteContext,
52                 const char *arg
53 )
54 {
55         struct rewrite_info *info;
56         char *string, *sep, *result = NULL;
57         int rc;
58         void *cookie = &info;
59
60         info = rewrite_info_init(REWRITE_MODE_ERR);
61
62         if ( read_rewrite( fin, info ) != 0 ) {
63                 exit( EXIT_FAILURE );
64         }
65
66         rewrite_param_set( info, "prog", "rewrite" );
67
68         rewrite_session_init( info, cookie );
69
70         string = strdup( arg );
71         for ( sep = strchr( rewriteContext, ',' );
72                         rewriteContext != NULL;
73                         rewriteContext = sep,
74                         sep ? sep = strchr( rewriteContext, ',' ) : NULL ) {
75                 if ( sep != NULL ) {
76                         sep[ 0 ] = '\0';
77                         sep++;
78                 }
79                 /* rc = rewrite( info, rewriteContext, string, &result ); */
80                 rc = rewrite_session( info, rewriteContext, string,
81                                 cookie, &result );
82                 
83                 fprintf( stdout, "%s -> %s\n", string, 
84                                 ( result ? result : "unwilling to perform" ) );
85                 if ( result == NULL ) {
86                         break;
87                 }
88                 free( string );
89                 string = result;
90         }
91
92         rewrite_session_delete( info, cookie );
93
94         return result;
95 }
96
97 int
98 main( int argc, char *argv[] )
99 {
100         FILE *fin = NULL;
101         char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
102
103         while ( 1 ) {
104                 int opt = getopt( argc, argv, "f:hr:" );
105
106                 if ( opt == EOF ) {
107                         break;
108                 }
109
110                 switch ( opt ) {
111                 case 'f':
112                         fin = fopen( optarg, "r" );
113                         if ( fin == NULL ) {
114                                 fprintf( stderr, "unable to open file '%s'\n",
115                                                 optarg );
116                                 exit( EXIT_FAILURE );
117                         }
118                         break;
119                         
120                 case 'h':
121                         fprintf( stderr, 
122         "usage: rewrite [options] string\n"
123         "\n"
124         "\t\t-f file\t\tconfiguration file\n"
125         "\t\t-r rule[s]\tlist of comma-separated rules\n"
126         "\n"
127         "\tsyntax:\n"
128         "\t\trewriteEngine\t{on|off}\n"
129         "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
130         "\t\trewriteRule\tpattern subst [flags]\n"
131         "\n" 
132                                 );
133                         exit( EXIT_SUCCESS );
134                         
135                 case 'r':
136                         rewriteContext = strdup( optarg );
137                         break;
138                 }
139         }
140         
141         if ( optind >= argc ) {
142                 return -1;
143         }
144
145         apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
146
147         return 0;
148 }
149