]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rewrite.c
Need ../cr.o
[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 #include <stdio.h>
36
37 #include <rewrite.h>
38
39 int ldap_debug;
40 int ldap_syslog;
41 int ldap_syslog_level;
42
43 char *
44 apply( 
45                 FILE *fin, 
46                 const char *rewriteContext,
47                 const char *arg
48 )
49 {
50         struct rewrite_info *info;
51         char *string, *sep, *result = NULL;
52         int rc;
53         void *cookie = &info;
54
55         info = rewrite_info_init(REWRITE_MODE_ERR);
56
57         if ( rewrite_read( fin, info ) != 0 ) {
58                 exit( EXIT_FAILURE );
59         }
60
61         rewrite_param_set( info, "prog", "rewrite" );
62
63         rewrite_session_init( info, cookie );
64
65         string = strdup( arg );
66         for ( sep = strchr( rewriteContext, ',' );
67                         rewriteContext != NULL;
68                         rewriteContext = sep,
69                         sep ? sep = strchr( rewriteContext, ',' ) : NULL ) {
70                 if ( sep != NULL ) {
71                         sep[ 0 ] = '\0';
72                         sep++;
73                 }
74                 /* rc = rewrite( info, rewriteContext, string, &result ); */
75                 rc = rewrite_session( info, rewriteContext, string,
76                                 cookie, &result );
77                 
78                 fprintf( stdout, "%s -> %s\n", string, 
79                                 ( result ? result : "unwilling to perform" ) );
80                 if ( result == NULL ) {
81                         break;
82                 }
83                 free( string );
84                 string = result;
85         }
86
87         rewrite_session_delete( info, cookie );
88
89         return result;
90 }
91
92 int
93 main( int argc, char *argv[] )
94 {
95         FILE *fin = NULL;
96         char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
97
98         while ( 1 ) {
99                 int opt = getopt( argc, argv, "f:hr:" );
100
101                 if ( opt == EOF ) {
102                         break;
103                 }
104
105                 switch ( opt ) {
106                 case 'f':
107                         fin = fopen( optarg, "r" );
108                         if ( fin == NULL ) {
109                                 fprintf( stderr, "unable to open file '%s'\n",
110                                                 optarg );
111                                 exit( EXIT_FAILURE );
112                         }
113                         break;
114                         
115                 case 'h':
116                         fprintf( stderr, 
117         "usage: rewrite [options] string\n"
118         "\n"
119         "\t\t-f file\t\tconfiguration file\n"
120         "\t\t-r rule[s]\tlist of comma-separated rules\n"
121         "\n"
122         "\tsyntax:\n"
123         "\t\trewriteEngine\t{on|off}\n"
124         "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
125         "\t\trewriteRule\tpattern subst [flags]\n"
126         "\n" 
127                                 );
128                         exit( EXIT_SUCCESS );
129                         
130                 case 'r':
131                         rewriteContext = strdup( optarg );
132                         break;
133                 }
134         }
135         
136         if ( optind >= argc ) {
137                 return -1;
138         }
139
140         apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
141
142         return 0;
143 }
144