]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rewrite.c
Patch: ucdata 2.4 bugs (ITS#1751)
[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 char *
43 apply( 
44                 FILE *fin, 
45                 const char *rewriteContext,
46                 const char *arg
47 )
48 {
49         struct rewrite_info *info;
50         char *string, *sep, *result = NULL;
51         int rc;
52         void *cookie = &info;
53
54         info = rewrite_info_init(REWRITE_MODE_ERR);
55
56         if ( rewrite_read( fin, info ) != 0 ) {
57                 exit( EXIT_FAILURE );
58         }
59
60         rewrite_param_set( info, "prog", "rewrite" );
61
62         rewrite_session_init( info, cookie );
63
64         string = strdup( arg );
65         for ( sep = strchr( rewriteContext, ',' );
66                         rewriteContext != NULL;
67                         rewriteContext = sep,
68                         sep ? sep = strchr( rewriteContext, ',' ) : NULL ) {
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                 fprintf( stdout, "%s -> %s\n", string, 
78                                 ( result ? result : "unwilling to perform" ) );
79                 if ( result == NULL ) {
80                         break;
81                 }
82                 free( string );
83                 string = result;
84         }
85
86         rewrite_session_delete( info, cookie );
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 = strdup( 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         return 0;
142 }
143