]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_util.c
New dn2id format with base/one/subtree indices (ldbm/bdb2)
[openldap] / servers / slapd / back-tcl / tcl_util.c
1 /* result.c - tcl backend utility functions
2  *
3  * $Id: tcl_util.c,v 1.8 1999/08/02 23:38:43 hallvard Exp $
4  *
5  * Copyright 1999, Ben Collins <bcollins@debian.org>, All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted only
8  * as authorized by the OpenLDAP Public License.  A copy of this
9  * license is available at http://www.OpenLDAP.org/license.html or
10  * in file LICENSE in the top-level directory of the distribution.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/string.h>
18 #include <ac/socket.h>
19 #include <ac/unistd.h>
20
21 #include "slap.h"
22 #include "tcl_back.h"
23
24 int
25 interp_send_results (
26         Backend * be,
27         Connection * conn,
28         Operation * op,
29         char *result,
30         char **attrs,
31         int attrsonly
32 )
33 {
34         int bsize, len, argcPtr, i, err, code;
35         char *buf, *bp, **argvPtr, *line, *matched, *info;
36         Entry *e;
37         struct tclinfo *ti = (struct tclinfo *) be->be_private;
38
39         /*
40          * read in the result and send it along 
41          */
42         buf = (char *) ch_malloc (BUFSIZ);
43         buf[0] = '\0';
44         bsize = BUFSIZ;
45         bp = buf;
46         code = Tcl_SplitList (ti->ti_ii->interp, result, &argcPtr, &argvPtr);
47         if (code != TCL_OK) {
48                 argcPtr = 0;
49                 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
50                         "internal backend error", NULL, NULL );
51                 return -1;
52         }
53         for (i = 0; i < argcPtr; i++) {
54                 line = argvPtr[i];
55
56                 /*
57                  * ignore lines beginning with DEBUG: 
58                  */
59                 if (strncasecmp (line, "DEBUG:", 6) == 0) {
60                         continue;
61                 }
62                 len = strlen (line) + 1;
63                 while (bp + len - buf > bsize) {
64                         bsize += BUFSIZ;
65                         buf = (char *) ch_realloc (buf, bsize);
66                 }
67                 sprintf (bp, "%s\n", line);
68                 bp += len;
69
70                 /*
71                  * line marked the end of an entry or result 
72                  */
73                 if (line[0] == '\0') {
74                         if (strncasecmp (buf, "RESULT", 6) == 0) {
75                                 break;
76                         }
77                         if ((e = str2entry (buf)) == NULL) {
78                                 Debug (LDAP_DEBUG_SHELL,
79                                         "str2entry(%s) failed\n",
80                                         buf, 0, 0);
81                         } else {
82                                 send_search_entry (be, conn, op, e, attrs,
83                                         attrsonly, NULL );
84                                 entry_free (e);
85                         }
86
87                         bp = buf;
88                 }
89         }
90
91         (void) str2result (buf, &err, &matched, &info);
92
93         /*
94          * otherwise, front end will send this result 
95          */
96         if (err != 0 || op->o_tag != LDAP_REQ_BIND) {
97                 send_ldap_result (conn, op, err, matched, info, NULL, NULL );
98         }
99
100         free (buf);
101         Tcl_Free ((char *) argvPtr);
102         return (err);
103 }
104
105 char *
106 tcl_clean_entry (
107         Entry * e
108 )
109 {
110         char *entrystr, *mark1, *mark2, *buf, *bp, *dup;
111         int len, bsize;
112
113         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
114         entrystr = entry2str (e, &len);
115
116         buf = (char *) ch_malloc (BUFSIZ);
117         buf[0] = '\0';
118         bsize = BUFSIZ;
119         bp = buf;
120         bp++[0] = ' ';
121
122         mark1 = entrystr;
123         do {
124                 if (mark1[0] == '\n') {
125                         mark1++;
126                 }
127                 dup = (char *) ch_strdup (mark1);
128                 if (dup[0] != '\0') {
129                         if ((mark2 = (char *) strchr (dup, '\n')) != NULL) {
130                                 mark2[0] = '\0';
131                         }
132                         len = strlen (dup) + 3;
133                         while (bp + len - buf > bsize) {
134                                 bsize += BUFSIZ;
135                                 buf = (char *) ch_realloc (buf, bsize);
136                         }
137                         if (mark1[0] == '\0') {
138                                 sprintf (bp, "{} ");
139                         } else {
140                                 sprintf (bp, "{%s} ", dup);
141                         }
142                         bp += len;
143                         if (mark2 != NULL) {
144                                 mark2[0] = '\n';
145                         }
146                 }
147                 free (dup);
148         } while ((mark1 = (char *) strchr (mark1, '\n')) != NULL);
149
150         ldap_pvt_thread_mutex_unlock (&entry2str_mutex);
151         return buf;
152 }
153
154 int
155 tcl_ldap_debug (
156         ClientData clientData,
157         Tcl_Interp * interp,
158         int argc,
159         char *argv[]
160 )
161 {
162         if (argv[1] != NULL) {
163                 Debug (LDAP_DEBUG_SHELL, "tcl_debug: %s\n", argv[1], 0, 0);
164         }
165         return TCL_OK;
166 }
167
168 void
169 readtclscript (
170         char *script,
171         Tcl_Interp * my_tcl)
172 {
173         int code;
174         FILE *f;
175
176         f = fopen (script, "r");
177         if (f == NULL) {
178                 Debug (LDAP_DEBUG_SHELL, "Could not open scriptpath %s\n", script,
179                         0, 0);
180                 return;
181         }
182         fclose (f);
183         code = Tcl_EvalFile (my_tcl, script);
184         if (code != TCL_OK) {
185                 Debug (LDAP_DEBUG_SHELL, "%s: %s\n", script,
186                         Tcl_GetVar (my_tcl, "errorInfo", TCL_GLOBAL_ONLY), 0);
187                 Debug (LDAP_DEBUG_SHELL, "%s: error at line\n", script,
188                         my_tcl->errorLine, 0);
189                 return;
190         }
191 }