]> git.sur5r.net Git - openldap/blob - libraries/libldap/ftest.c
Happy new year
[openldap] / libraries / libldap / ftest.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* OpenLDAP Filter API Test */
7
8 #include "portable.h"
9
10 #include <ac/stdlib.h>
11 #include <ac/string.h>
12 #include <ac/unistd.h>
13
14 #include <stdio.h>
15
16 #include <ldap.h>
17
18 #include "ldap_pvt.h"
19
20 #include "ldif.h"
21 #include "lutil.h"
22 #include "lutil_ldap.h"
23 #include "ldap_defaults.h"
24
25 static int filter2ber( char *filter );
26
27 int usage()
28 {
29         fprintf( stderr, "usage:\n"
30                 "  ftest [-d n] filter\n"
31                 "    filter - RFC 2254 string representation of an "
32                         "LDAP search filter\n" );
33         return EXIT_FAILURE;
34 }
35
36 int
37 main( int argc, char *argv[] )
38 {
39         int c;
40         int debug=0;
41
42     while( (c = getopt( argc, argv, "d:" )) != EOF ) {
43                 switch ( c ) {
44                 case 'd':
45                         debug = atoi( optarg );
46                         break;
47                 default:
48                         fprintf( stderr, "ftest: unrecognized option -%c\n",
49                                 optopt );
50                         return usage();
51                 }
52         }
53
54         if ( debug ) {
55                 if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
56                         != LBER_OPT_SUCCESS )
57                 {
58                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n",
59                                 debug );
60                 }
61                 if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
62                         != LDAP_OPT_SUCCESS )
63                 {
64                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n",
65                                 debug );
66                 }
67         }
68
69         if ( argc - optind != 1 ) {
70                 return usage();
71         }
72
73         return filter2ber( strdup( argv[optind] ) );
74 }
75
76 static int filter2ber( char *filter )
77 {
78         int rc;
79         struct berval bv = {0};
80         BerElement *ber;
81
82         printf( "Filter: %s\n", filter );
83
84         ber = ber_alloc_t( LBER_USE_DER );
85         if( ber == NULL ) {
86                 perror( "ber_alloc_t" );
87                 return EXIT_FAILURE;
88         }
89
90         rc = ldap_pvt_put_filter( ber, filter );
91         if( rc < 0 ) {
92                 fprintf( stderr, "Filter error!\n");
93                 return EXIT_FAILURE;
94         }
95
96         rc = ber_flatten2( ber, &bv, 0 );
97         if( rc < 0 ) {
98                 perror( "ber_flatten2" );
99                 return EXIT_FAILURE;
100         }
101
102         printf( "BER encoding (len=%ld):\n", (long) bv.bv_len );
103         ber_bprint( bv.bv_val, bv.bv_len );
104
105         ber_free( ber, 1 );
106
107         return EXIT_SUCCESS;
108 }
109