]> git.sur5r.net Git - openldap/blob - libraries/libldap/ftest.c
Fix stupid bug in last commit
[openldap] / libraries / libldap / ftest.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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-int.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                 "\tftest [-d n] filter\n" );
31         return EXIT_FAILURE;
32 }
33
34 int
35 main( int argc, char *argv[] )
36 {
37         int i;
38         int debug=0;
39         char *filter=NULL;
40
41     while( (i = getopt( argc, argv, "Aa:Ss:" "d:" )) != EOF ) {
42                 switch ( i ) {
43                 case 'd':
44                         debug = atoi( optarg );
45                         break;
46                 default:
47                         fprintf( stderr, "ftest: unrecognized option -%c\n",
48                                 optopt );
49                         return usage();
50                 }
51         }
52
53         if ( debug ) {
54                 if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
55                         != LBER_OPT_SUCCESS )
56                 {
57                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n",
58                                 debug );
59                 }
60                 if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
61                         != LDAP_OPT_SUCCESS )
62                 {
63                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n",
64                                 debug );
65                 }
66         }
67
68         if( argc - optind > 1 ) {
69                 return usage();
70
71         } else if ( argc - optind != 1 ) {
72                 return usage();
73         }
74
75         return filter2ber( strdup( argv[optind] ) );
76 }
77
78 static int filter2ber( char *filter )
79 {
80         int rc;
81         struct berval *bv = NULL;
82         BerElement *ber;
83
84         printf( "Filter: %s\n", filter );
85
86         ber = ber_alloc_t( LBER_USE_DER );
87         if( ber == NULL ) {
88                 perror( "ber_alloc_t" );
89                 return EXIT_FAILURE;
90         }
91
92         rc = ldap_int_put_filter( ber, filter );
93         if( rc < 0 ) {
94                 fprintf( stderr, "Filter error!\n");
95                 return EXIT_FAILURE;
96         }
97
98         rc = ber_flatten( ber, &bv );
99         if( rc < 0 ) {
100                 perror( "ber_flatten" );
101                 return EXIT_FAILURE;
102         }
103
104         printf( "BER encoding (len=%ld):\n", (long) bv->bv_len );
105         ber_bprint( bv->bv_val, bv->bv_len );
106
107         ber_free( ber, 0 );
108         ber_bvfree( bv );
109
110         return EXIT_SUCCESS;
111 }
112