]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rewrite.c
6747bd60aa76bc126907b95f68377485f39a6447
[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                 char    *errmsg = "";
66
67                 if ( sep != NULL ) {
68                         sep[ 0 ] = '\0';
69                         sep++;
70                 }
71                 /* rc = rewrite( info, rewriteContext, string, &result ); */
72                 rc = rewrite_session( info, rewriteContext, string,
73                                 cookie, &result );
74                 
75                 switch ( rc ) {
76                 case REWRITE_REGEXEC_OK:
77                         errmsg = "ok";
78                         break;
79
80                 case REWRITE_REGEXEC_ERR:
81                         errmsg = "error";
82                         break;
83
84                 case REWRITE_REGEXEC_STOP:
85                         errmsg = "stop";
86                         break;
87
88                 case REWRITE_REGEXEC_UNWILLING:
89                         errmsg = "unwilling to perform";
90                         break;
91
92                 default:
93                         if (rc >= REWRITE_REGEXEC_USER) {
94                                 errmsg = "user-defined";
95                         } else {
96                                 errmsg = "unknown";
97                         }
98                         break;
99                 }
100                 
101                 fprintf( stdout, "%s -> %s [%d:%s]\n", string, 
102                                 ( result ? result : "(null)" ),
103                                 rc, errmsg );
104                 if ( result == NULL ) {
105                         break;
106                 }
107                 free( string );
108                 string = result;
109         }
110
111         free( string );
112
113         rewrite_session_delete( info, cookie );
114
115         rewrite_info_delete( &info );
116
117         return result;
118 }
119
120 int
121 main( int argc, char *argv[] )
122 {
123         FILE *fin = NULL;
124         char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
125
126         while ( 1 ) {
127                 int opt = getopt( argc, argv, "f:hr:" );
128
129                 if ( opt == EOF ) {
130                         break;
131                 }
132
133                 switch ( opt ) {
134                 case 'f':
135                         fin = fopen( optarg, "r" );
136                         if ( fin == NULL ) {
137                                 fprintf( stderr, "unable to open file '%s'\n",
138                                                 optarg );
139                                 exit( EXIT_FAILURE );
140                         }
141                         break;
142                         
143                 case 'h':
144                         fprintf( stderr, 
145         "usage: rewrite [options] string\n"
146         "\n"
147         "\t\t-f file\t\tconfiguration file\n"
148         "\t\t-r rule[s]\tlist of comma-separated rules\n"
149         "\n"
150         "\tsyntax:\n"
151         "\t\trewriteEngine\t{on|off}\n"
152         "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
153         "\t\trewriteRule\tpattern subst [flags]\n"
154         "\n" 
155                                 );
156                         exit( EXIT_SUCCESS );
157                         
158                 case 'r':
159                         rewriteContext = optarg;
160                         break;
161                 }
162         }
163         
164         if ( optind >= argc ) {
165                 return -1;
166         }
167
168         apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
169
170         if ( fin ) {
171                 fclose( fin );
172         }
173
174         return 0;
175 }
176