No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / libldap / ftest.c
blob1f1a166f608251594ecae375a4d16bcbdf985102
1 /* ftest.c -- OpenLDAP Filter API Test */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap/ftest.c,v 1.15.2.3 2008/02/11 23:26:41 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 #include "portable.h"
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
23 #include <stdio.h>
25 #include <ldap.h>
27 #include "ldap_pvt.h"
28 #include "lber_pvt.h"
30 #include "ldif.h"
31 #include "lutil.h"
32 #include "lutil_ldap.h"
33 #include "ldap_defaults.h"
35 static int filter2ber( char *filter );
37 int usage()
39 fprintf( stderr, "usage:\n"
40 " ftest [-d n] filter\n"
41 " filter - RFC 4515 string representation of an "
42 "LDAP search filter\n" );
43 return EXIT_FAILURE;
46 int
47 main( int argc, char *argv[] )
49 int c;
50 int debug=0;
52 while( (c = getopt( argc, argv, "d:" )) != EOF ) {
53 switch ( c ) {
54 case 'd':
55 debug = atoi( optarg );
56 break;
57 default:
58 fprintf( stderr, "ftest: unrecognized option -%c\n",
59 optopt );
60 return usage();
64 if ( debug ) {
65 if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
66 != LBER_OPT_SUCCESS )
68 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n",
69 debug );
71 if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
72 != LDAP_OPT_SUCCESS )
74 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n",
75 debug );
79 if ( argc - optind != 1 ) {
80 return usage();
83 return filter2ber( strdup( argv[optind] ) );
86 static int filter2ber( char *filter )
88 int rc;
89 struct berval bv = BER_BVNULL;
90 BerElement *ber;
92 printf( "Filter: %s\n", filter );
94 ber = ber_alloc_t( LBER_USE_DER );
95 if( ber == NULL ) {
96 perror( "ber_alloc_t" );
97 return EXIT_FAILURE;
100 rc = ldap_pvt_put_filter( ber, filter );
101 if( rc < 0 ) {
102 fprintf( stderr, "Filter error!\n");
103 return EXIT_FAILURE;
106 rc = ber_flatten2( ber, &bv, 0 );
107 if( rc < 0 ) {
108 perror( "ber_flatten2" );
109 return EXIT_FAILURE;
112 printf( "BER encoding (len=%ld):\n", (long) bv.bv_len );
113 ber_bprint( bv.bv_val, bv.bv_len );
115 ber_free( ber, 1 );
117 return EXIT_SUCCESS;