]> git.sur5r.net Git - openldap/blob - servers/slapd/back-shell/result.c
27cddfd6225f1e3928bfc1dab95aa4c995639467
[openldap] / servers / slapd / back-shell / result.c
1 /* result.c - shell backend result reading function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/errno.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <ac/unistd.h>
16
17 #include "slap.h"
18 #include "shell.h"
19
20 int
21 read_and_send_results(
22     Operation   *op,
23     SlapReply   *rs,
24     FILE        *fp )
25 {
26         int     bsize, len;
27         char    *buf, *bp;
28         char    line[BUFSIZ];
29
30         /* read in the result and send it along */
31         buf = (char *) ch_malloc( BUFSIZ );
32         buf[0] = '\0';
33         bsize = BUFSIZ;
34         bp = buf;
35         while ( !feof(fp) ) {
36                 errno = 0;
37                 if ( fgets( line, sizeof(line), fp ) == NULL ) {
38                         if ( errno == EINTR ) continue;
39
40                         Debug( LDAP_DEBUG_ANY, "shell: fgets failed: %s (%d)\n",
41                                 strerror(errno), errno, 0 ); 
42                         break;
43                 }
44
45                 Debug( LDAP_DEBUG_SHELL, "shell search reading line (%s)\n",
46                     line, 0, 0 );
47
48                 /* ignore lines beginning with # (LDIFv1 comments) */
49                 if ( *line == '#' ) {
50                         continue;
51                 }
52
53                 /* ignore lines beginning with DEBUG: */
54                 if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
55                         continue;
56                 }
57
58                 len = strlen( line );
59                 while ( bp + len - buf > bsize ) {
60                         size_t offset = bp - buf;
61                         bsize += BUFSIZ;
62                         buf = (char *) ch_realloc( buf, bsize );
63                         bp = &buf[offset];
64                 }
65                 strcpy( bp, line );
66                 bp += len;
67
68                 /* line marked the end of an entry or result */
69                 if ( *line == '\n' ) {
70                         if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
71                                 break;
72                         }
73
74                         if ( (rs->sr_entry = str2entry( buf )) == NULL ) {
75                                 Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
76                                     buf, 0, 0 );
77                         } else {
78                                 rs->sr_attrs = op->oq_search.rs_attrs;
79                                 send_search_entry( op, rs );
80                                 entry_free( rs->sr_entry );
81                         }
82
83                         bp = buf;
84                 }
85         }
86         (void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text );
87
88         /* otherwise, front end will send this result */
89         if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) {
90                 send_ldap_result( op, rs );
91         }
92
93         free( buf );
94
95         return( rs->sr_err );
96 }
97
98 void
99 print_suffixes(
100     FILE        *fp,
101     Backend     *be
102 )
103 {
104         int     i;
105
106         for ( i = 0; be->be_suffix[i].bv_val != NULL; i++ ) {
107                 fprintf( fp, "suffix: %s\n", be->be_suffix[i].bv_val );
108         }
109 }