]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_util.c
Import Ben Collins <bcollins@debian.org> Back-TCL for SLAPD.
[openldap] / servers / slapd / back-tcl / tcl_util.c
1 /*
2  * result.c - tcl backend utility functions
3  *
4  * Copyright 1999, Ben Collins <bcollins@debian.org>, All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15
16 #include <ac/string.h>
17 #include <ac/socket.h>
18 #include <ac/unistd.h>
19
20 #include "slap.h"
21 #include "tcl_back.h"
22
23 int
24 interp_send_results(
25     Backend     *be,
26     Connection  *conn,
27     Operation   *op,
28     char        *result,
29     char        **attrs,
30     int         attrsonly
31 )
32 {
33         int     bsize, len, argcPtr, i, err, code;
34         char    *buf, *bp, **argvPtr, *line, *matched, *info;
35         Entry   *e;
36         struct tclinfo *ti = (struct tclinfo *) be->be_private;
37         /* read in the result and send it along */
38         buf = (char *) ch_malloc( BUFSIZ );
39         buf[0] = '\0';
40         bsize = BUFSIZ;
41         bp = buf;
42         code = Tcl_SplitList(ti->ti_ii->interp, result, &argcPtr, &argvPtr);
43         if (code != TCL_OK) {
44                 argcPtr = 0;
45                 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
46                         "internal backend error");
47                 return -1;
48         }
49         for ( i = 0 ; i < argcPtr ; i++ ) {
50                 line = argvPtr[i];
51                 Debug( LDAP_DEBUG_ANY, "tcl search reading line (%s)\n",
52                     line, 0, 0 );
53                 /* ignore lines beginning with DEBUG: */
54                 if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
55                         continue;
56                 }
57                 len = strlen( line ) + 1;
58                 while ( bp + len - buf > bsize ) {
59                         bsize += BUFSIZ;
60                         buf = (char *) ch_realloc( buf, bsize );
61                 }
62                 sprintf( bp, "%s\n", line );
63                 bp += len;
64
65                 /* line marked the end of an entry or result */
66                 if ( line[0] == '\0' ) {
67                         if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
68                                 break;
69                         }
70                         if ( (e = str2entry( buf )) == NULL ) {
71                                 Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
72                                     buf, 0, 0 );
73                         } else {
74                                 send_search_entry( be, conn, op, e, attrs,
75                                     attrsonly );
76                                 entry_free( e );
77                         }
78
79                         bp = buf;
80                 }
81         }
82
83         (void) str2result( buf, &err, &matched, &info );
84
85         /* otherwise, front end will send this result */
86         if ( err != 0 || op->o_tag != LDAP_REQ_BIND ) {
87                 send_ldap_result( conn, op, err, matched, info );
88         }
89
90         free( buf );
91         Tcl_Free( result );
92         Tcl_Free( (char *) argvPtr );
93         return( err );
94 }
95
96 char *tcl_clean_entry (Entry *e)
97 {
98     char *entrystr, *mark1, *mark2, *buf, *bp, *dup;
99     int len, bsize;
100
101     pthread_mutex_lock( &entry2str_mutex );
102     entrystr = entry2str( e, &len, 0 );
103     pthread_mutex_unlock( &entry2str_mutex );
104
105     buf = (char *) ch_malloc( BUFSIZ );
106     buf[0] = '\0';
107     bsize = BUFSIZ;
108     bp = buf;
109     bp++[0] = ' ';
110
111     mark1 = entrystr;
112     do {
113                 if (mark1[0] == '\n') {
114                 mark1++;
115                 }
116                 dup = (char *) strdup(mark1);
117                 if (dup[0] != '\0') {
118                 if ((mark2 = (char *) strchr (dup, '\n')) != NULL) {
119                                 mark2[0] = '\0';
120                 }
121                 len = strlen( dup ) + 3;
122                 while ( bp + len - buf > bsize ) {
123                                 bsize += BUFSIZ;
124                                 buf = (char *) ch_realloc( buf, bsize );
125                 }
126                 if (mark1[0] == '\0') {
127                                 sprintf(bp, "{} ");
128                 } else {
129                                 sprintf(bp, "{%s} ", dup);
130                 }
131                 bp += len;
132                 if (mark2 != NULL) {
133                                 mark2[0] = '\n';
134                 }
135                 }
136                 free(dup);
137     } while ((mark1 = (char *) strchr (mark1, '\n')) != NULL);
138     return buf;
139 }
140
141 int tcl_ldap_debug (
142     ClientData clientData,
143     Tcl_Interp *interp,
144     int argc,
145     char *argv[]
146 )
147 {
148     if (argv[1] != NULL) {
149                 Debug(LDAP_DEBUG_ANY, "tcl_debug: %s\n", argv[1], 0, 0);
150     }
151     return TCL_OK;
152 }