]> git.sur5r.net Git - openldap/blob - servers/slapd/back-shell/result.c
Fix prev commit
[openldap] / servers / slapd / back-shell / result.c
1 /* result.c - shell backend result reading function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
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.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26 /* ACKNOWLEDGEMENTS:
27  * This work was originally developed by the University of Michigan
28  * (as part of U-MICH LDAP).
29  */
30
31 #include "portable.h"
32
33 #include <stdio.h>
34
35 #include <ac/errno.h>
36 #include <ac/string.h>
37 #include <ac/socket.h>
38 #include <ac/unistd.h>
39
40 #include "slap.h"
41 #include "shell.h"
42
43 int
44 read_and_send_results(
45     Operation   *op,
46     SlapReply   *rs,
47     FILE        *fp )
48 {
49         int     bsize, len;
50         char    *buf, *bp;
51         char    line[BUFSIZ];
52
53         /* read in the result and send it along */
54         buf = (char *) ch_malloc( BUFSIZ );
55         buf[0] = '\0';
56         bsize = BUFSIZ;
57         bp = buf;
58         while ( !feof(fp) ) {
59                 errno = 0;
60                 if ( fgets( line, sizeof(line), fp ) == NULL ) {
61                         if ( errno == EINTR ) continue;
62
63                         Debug( LDAP_DEBUG_ANY, "shell: fgets failed: %s (%d)\n",
64                                 strerror(errno), errno, 0 ); 
65                         break;
66                 }
67
68                 Debug( LDAP_DEBUG_SHELL, "shell search reading line (%s)\n",
69                     line, 0, 0 );
70
71                 /* ignore lines beginning with # (LDIFv1 comments) */
72                 if ( *line == '#' ) {
73                         continue;
74                 }
75
76                 /* ignore lines beginning with DEBUG: */
77                 if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
78                         continue;
79                 }
80
81                 len = strlen( line );
82                 while ( bp + len - buf > bsize ) {
83                         size_t offset = bp - buf;
84                         bsize += BUFSIZ;
85                         buf = (char *) ch_realloc( buf, bsize );
86                         bp = &buf[offset];
87                 }
88                 strcpy( bp, line );
89                 bp += len;
90
91                 /* line marked the end of an entry or result */
92                 if ( *line == '\n' ) {
93                         if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
94                                 break;
95                         }
96
97                         if ( (rs->sr_entry = str2entry( buf )) == NULL ) {
98                                 Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
99                                     buf, 0, 0 );
100                         } else {
101                                 rs->sr_attrs = op->oq_search.rs_attrs;
102                                 send_search_entry( op, rs );
103                                 entry_free( rs->sr_entry );
104                         }
105
106                         bp = buf;
107                 }
108         }
109         (void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text );
110
111         /* otherwise, front end will send this result */
112         if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) {
113                 send_ldap_result( op, rs );
114         }
115
116         free( buf );
117
118         return( rs->sr_err );
119 }
120
121 void
122 print_suffixes(
123     FILE        *fp,
124     Backend     *be
125 )
126 {
127         int     i;
128
129         for ( i = 0; be->be_suffix[i].bv_val != NULL; i++ ) {
130                 fprintf( fp, "suffix: %s\n", be->be_suffix[i].bv_val );
131         }
132 }