]> git.sur5r.net Git - openldap/blob - servers/slapd/back-shell/result.c
4d9f39c2f557b093fe63952e874ee4790c680604
[openldap] / servers / slapd / back-shell / result.c
1 /* result.c - shell backend result reading function */
2 /* $OpenLDAP$ */
3
4 #include "portable.h"
5
6 #include <stdio.h>
7
8 #include <ac/string.h>
9 #include <ac/socket.h>
10 #include <ac/unistd.h>
11
12 #include "slap.h"
13 #include "shell.h"
14
15 int
16 read_and_send_results(
17     Backend     *be,
18     Connection  *conn,
19     Operation   *op,
20     FILE        *fp,
21     char        **attrs,
22     int         attrsonly
23 )
24 {
25         int     bsize, len;
26         char    *buf, *bp;
27         char    line[BUFSIZ];
28         Entry   *e;
29         int     err;
30         char    *matched, *info;
31
32         /* read in the result and send it along */
33         buf = (char *) ch_malloc( BUFSIZ );
34         buf[0] = '\0';
35         bsize = BUFSIZ;
36         bp = buf;
37         while ( fgets( line, sizeof(line), fp ) != NULL ) {
38                 Debug( LDAP_DEBUG_SHELL, "shell search reading line (%s)\n",
39                     line, 0, 0 );
40                 /* ignore lines beginning with DEBUG: */
41                 if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
42                         continue;
43                 }
44                 len = strlen( line );
45                 while ( bp + len - buf > bsize ) {
46                         bsize += BUFSIZ;
47                         buf = (char *) ch_realloc( buf, bsize );
48                 }
49                 strcpy( bp, line );
50                 bp += len;
51
52                 /* line marked the end of an entry or result */
53                 if ( *line == '\n' ) {
54                         if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
55                                 break;
56                         }
57
58                         if ( (e = str2entry( buf )) == NULL ) {
59                                 Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
60                                     buf, 0, 0 );
61                         } else {
62                                 send_search_entry( be, conn, op, e, attrs,
63                                     attrsonly, NULL );
64                                 entry_free( e );
65                         }
66
67                         bp = buf;
68                 }
69         }
70         (void) str2result( buf, &err, &matched, &info );
71
72         /* otherwise, front end will send this result */
73         if ( err != 0 || op->o_tag != LDAP_REQ_BIND ) {
74                 send_ldap_result( conn, op, err, matched, info, NULL, NULL );
75         }
76
77         free( buf );
78
79         return( err );
80 }
81
82 void
83 print_suffixes(
84     FILE        *fp,
85     Backend     *be
86 )
87 {
88         int     i;
89
90         for ( i = 0; be->be_suffix[i] != NULL; i++ ) {
91                 fprintf( fp, "suffix: %s\n", be->be_suffix[i] );
92         }
93 }