No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / librewrite / rewrite.c
blob3aff4c067e27a523df056c457690221eaa55284a
1 /* $OpenLDAP: pkg/ldap/libraries/librewrite/rewrite.c,v 1.16.2.3 2008/02/11 23:26:42 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2008 The OpenLDAP Foundation.
5 * All rights reserved.
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.
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>.
15 /* ACKNOWLEDGEMENT:
16 * This work was initially developed by Pierangelo Masarati for
17 * inclusion in OpenLDAP Software.
20 #include <portable.h>
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>
32 #include <rewrite.h>
33 #include <lutil.h>
34 #include <ldap.h>
36 int ldap_debug;
37 int ldap_syslog;
38 int ldap_syslog_level;
40 static void
41 apply(
42 FILE *fin,
43 const char *rewriteContext,
44 const char *arg
47 struct rewrite_info *info;
48 char *string, *sep, *result = NULL;
49 int rc;
50 void *cookie = &info;
52 info = rewrite_info_init( REWRITE_MODE_ERR );
54 if ( rewrite_read( fin, info ) != 0 ) {
55 exit( EXIT_FAILURE );
58 rewrite_param_set( info, "prog", "rewrite" );
60 rewrite_session_init( info, cookie );
62 string = (char *)arg;
63 for ( sep = strchr( rewriteContext, ',' );
64 rewriteContext != NULL;
65 rewriteContext = sep,
66 sep ? sep = strchr( rewriteContext, ',' ) : NULL )
68 char *errmsg = "";
70 if ( sep != NULL ) {
71 sep[ 0 ] = '\0';
72 sep++;
74 /* rc = rewrite( info, rewriteContext, string, &result ); */
75 rc = rewrite_session( info, rewriteContext, string,
76 cookie, &result );
78 switch ( rc ) {
79 case REWRITE_REGEXEC_OK:
80 errmsg = "ok";
81 break;
83 case REWRITE_REGEXEC_ERR:
84 errmsg = "error";
85 break;
87 case REWRITE_REGEXEC_STOP:
88 errmsg = "stop";
89 break;
91 case REWRITE_REGEXEC_UNWILLING:
92 errmsg = "unwilling to perform";
93 break;
95 default:
96 if (rc >= REWRITE_REGEXEC_USER) {
97 errmsg = "user-defined";
98 } else {
99 errmsg = "unknown";
101 break;
104 fprintf( stdout, "%s -> %s [%d:%s]\n", string,
105 ( result ? result : "(null)" ),
106 rc, errmsg );
107 if ( result == NULL ) {
108 break;
110 if ( string != arg && string != result ) {
111 free( string );
113 string = result;
116 if ( result && result != arg ) {
117 free( result );
120 rewrite_session_delete( info, cookie );
122 rewrite_info_delete( &info );
126 main( int argc, char *argv[] )
128 FILE *fin = NULL;
129 char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
130 int debug = 0;
132 while ( 1 ) {
133 int opt = getopt( argc, argv, "d:f:hr:" );
135 if ( opt == EOF ) {
136 break;
139 switch ( opt ) {
140 case 'd':
141 if ( lutil_atoi( &debug, optarg ) != 0 ) {
142 fprintf( stderr, "illegal log level '%s'\n",
143 optarg );
144 exit( EXIT_FAILURE );
146 break;
148 case 'f':
149 fin = fopen( optarg, "r" );
150 if ( fin == NULL ) {
151 fprintf( stderr, "unable to open file '%s'\n",
152 optarg );
153 exit( EXIT_FAILURE );
155 break;
157 case 'h':
158 fprintf( stderr,
159 "usage: rewrite [options] string\n"
160 "\n"
161 "\t\t-f file\t\tconfiguration file\n"
162 "\t\t-r rule[s]\tlist of comma-separated rules\n"
163 "\n"
164 "\tsyntax:\n"
165 "\t\trewriteEngine\t{on|off}\n"
166 "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
167 "\t\trewriteRule\tpattern subst [flags]\n"
168 "\n"
170 exit( EXIT_SUCCESS );
172 case 'r':
173 rewriteContext = optarg;
174 break;
178 if ( debug != 0 ) {
179 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
180 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
183 if ( optind >= argc ) {
184 return -1;
187 apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
189 if ( fin ) {
190 fclose( fin );
193 return 0;