2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 * This work was initially developed by Pierangelo Masarati for
17 * inclusion in OpenLDAP Software.
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/syslog.h>
26 #include <ac/socket.h>
27 #include <ac/unistd.h>
29 #include <ac/string.h>
36 int ldap_syslog_level;
41 const char *rewriteContext,
45 struct rewrite_info *info;
46 char *string, *sep, *result = NULL;
50 info = rewrite_info_init( REWRITE_MODE_ERR );
52 if ( rewrite_read( fin, info ) != 0 ) {
56 rewrite_param_set( info, "prog", "rewrite" );
58 rewrite_session_init( info, cookie );
60 string = strdup( arg );
61 for ( sep = strchr( rewriteContext, ',' );
62 rewriteContext != NULL;
64 sep ? sep = strchr( rewriteContext, ',' ) : NULL ) {
71 /* rc = rewrite( info, rewriteContext, string, &result ); */
72 rc = rewrite_session( info, rewriteContext, string,
76 case REWRITE_REGEXEC_OK:
80 case REWRITE_REGEXEC_ERR:
84 case REWRITE_REGEXEC_STOP:
88 case REWRITE_REGEXEC_UNWILLING:
89 errmsg = "unwilling to perform";
93 if (rc >= REWRITE_REGEXEC_USER) {
94 errmsg = "user-defined";
101 fprintf( stdout, "%s -> %s [%d:%s]\n", string,
102 ( result ? result : "(null)" ),
104 if ( result == NULL ) {
113 rewrite_session_delete( info, cookie );
115 rewrite_info_delete( &info );
121 main( int argc, char *argv[] )
124 char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
127 int opt = getopt( argc, argv, "f:hr:" );
135 fin = fopen( optarg, "r" );
137 fprintf( stderr, "unable to open file '%s'\n",
139 exit( EXIT_FAILURE );
145 "usage: rewrite [options] string\n"
147 "\t\t-f file\t\tconfiguration file\n"
148 "\t\t-r rule[s]\tlist of comma-separated rules\n"
151 "\t\trewriteEngine\t{on|off}\n"
152 "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
153 "\t\trewriteRule\tpattern subst [flags]\n"
156 exit( EXIT_SUCCESS );
159 rewriteContext = optarg;
164 if ( optind >= argc ) {
168 apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );