>From 19649300a635e20afc826a6e7cc8ae70990cc5c8 Mon Sep 17 00:00:00 2001
From: Yi Zhang <yzhang@redhat.com>
Date: Mon, 9 Nov 2009 13:41:25 -0800
Subject: [PATCH] 516089 - add dereference search option into ldclt

This option will accept format like "-e deref=derefAttr:attr"
	to ldclt when "-e esearch" is requested.
example: -e esearch -e deref=secretary:mail
        search will dereference the "secretary" attribute and then return the "mail" attribute value
---
 ldap/servers/slapd/tools/ldclt/ldapfct.c |   84 ++++++++++++++++--------------
 1 files changed, 44 insertions(+), 40 deletions(-)

diff --git a/ldap/servers/slapd/tools/ldclt/ldapfct.c b/ldap/servers/slapd/tools/ldclt/ldapfct.c
index 1d491cc..ac8250a 100644
--- a/ldap/servers/slapd/tools/ldclt/ldapfct.c
+++ b/ldap/servers/slapd/tools/ldclt/ldapfct.c
@@ -3781,58 +3781,62 @@ doExactSearch (
       }								/*JLS 15-12-00*/
     }
     else
-    { /* if search success */
-      for (e = ldap_first_message (tttctx->ldapCtx, res); e != NULL ; e = ldap_next_message (tttctx->ldapCtx, e) )
+    { /* if search success & we are in verbose mode */
+      if (mctx.mode & VERBOSE)
       {
-        msgtype = ldap_msgtype (e);
-        switch (msgtype)
+        for (e = ldap_first_message (tttctx->ldapCtx, res); e != NULL ; e = ldap_next_message (tttctx->ldapCtx, e) )
         {
-          /* if this is an entry that returned */
-          case LDAP_RES_SEARCH_ENTRY:
-            /* get dereferenced value into resctrls:  deref parsing  */
-            parse_rc = ldap_get_entry_controls ( tttctx->ldapCtx, e, &resctrls );
-            if ( resctrls != NULL ) 
-            {/* parse it only when we have return saved in server control */
-              /* get dn */
-              if (( dn = ldap_get_dn (tttctx->ldapCtx, e)) != NULL )
-              {
-                if (mctx.mode & VERY_VERBOSE)
+          msgtype = ldap_msgtype (e);
+          switch (msgtype)
+          {
+            /* if this is an entry that returned */
+            case LDAP_RES_SEARCH_ENTRY:
+              /* get dereferenced value into resctrls:  deref parsing  */
+              parse_rc = ldap_get_entry_controls ( tttctx->ldapCtx, e, &resctrls );
+              if ( resctrls != NULL ) 
+              {/* parse it only when we have return saved in server control */
+                /* get dn */
+                if (( dn = ldap_get_dn (tttctx->ldapCtx, e)) != NULL )
                 {
                   for ( rcp = resctrls; rcp && *rcp; rcp++ ) 
                   {
+                    /* if very_verbose */
+                    if ( mctx.mode & VERY_VERBOSE )
+                    {
                       printf("dn: [%s] -> deref oid: %s, value: %s\n",
                         dn, 
                         (**rcp).ldctl_oid?(**rcp).ldctl_oid:"none",
                         (**rcp).ldctl_value.bv_val?(**rcp).ldctl_value.bv_val:"none");
+                    }
                   }
+                  ldap_controls_free( resctrls );
+                  ldap_memfree (dn);
                 }
-                ldap_controls_free( resctrls );
-                ldap_memfree (dn);
               }
-            }
-          break; /*end of case LDAP_RES_SEARCH_ENTRY */
-
-          /* if this is an reference that returned */
-          case LDAP_RES_SEARCH_REFERENCE: /* we do nothing here */
-          break;
-
-          /* if we reach the end of search result */
-          case LDAP_RES_SEARCH_RESULT:
-            /* just free the returned msg */
-            parse_rc = ldap_parse_result (tttctx->ldapCtx, e, &rc, &matcheddn, &errmsg, NULL , NULL , 0); 
-            if ( parse_rc != LDAP_SUCCESS )
-            {
-              printf("ldap_parse_result error: [%s]\n", ldap_err2string( parse_rc ));
-            }
-            if ( rc != LDAP_SUCCESS )
-            {
-              printf ("ldap_search_ext_s error: [%s]\n", ldap_err2string( rc ));
-            }
-          break; /* end of case LDAP_RES_SEARCH_RESULT */
-          default:
-          break;
-        }
-      } /*end of message retriving */
+            break; /*end of case LDAP_RES_SEARCH_ENTRY */
+  
+            /* if this is an reference that returned */
+            case LDAP_RES_SEARCH_REFERENCE: /* we do nothing here */
+            break;
+  
+            /* if we reach the end of search result */
+            case LDAP_RES_SEARCH_RESULT:
+              /* just free the returned msg */
+              parse_rc = ldap_parse_result (tttctx->ldapCtx, e, &rc, &matcheddn, &errmsg, NULL , NULL , 0); 
+              if ( parse_rc != LDAP_SUCCESS )
+              {
+                printf("ldap_parse_result error: [%s]\n", ldap_err2string( parse_rc ));
+              }
+              if ( rc != LDAP_SUCCESS )
+              {
+                printf ("ldap_search_ext_s error: [%s]\n", ldap_err2string( rc ));
+              }
+            break; /* end of case LDAP_RES_SEARCH_RESULT */
+            default:
+            break;
+          }
+        } /*end of message retriving */
+      } /* end of verbose mode */
 
       if (incrementNbOpers (tttctx) < 0)/* Memorize operation */
       {
-- 
1.6.2.5

