]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_util.c
* Removed uneeded cvs keywords
[openldap] / servers / slapd / back-tcl / tcl_util.c
1 /* result.c - tcl backend utility functions
2  *
3  * $Id: tcl_util.c,v 1.2 1999/02/17 01:05:28 bcollins 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");
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_ANY,
79                                         "str2entry(%s) failed\n",
80                                         buf, 0, 0);
81                         } else {
82                                 send_search_entry (be, conn, op, e, attrs,
83                                         attrsonly);
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);
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         pthread_mutex_lock (&entry2str_mutex);
114         entrystr = entry2str (e, &len, 0);
115         pthread_mutex_unlock (&entry2str_mutex);
116
117         buf = (char *) ch_malloc (BUFSIZ);
118         buf[0] = '\0';
119         bsize = BUFSIZ;
120         bp = buf;
121         bp++[0] = ' ';
122
123         mark1 = entrystr;
124         do {
125                 if (mark1[0] == '\n') {
126                         mark1++;
127                 }
128                 dup = (char *) strdup (mark1);
129                 if (dup[0] != '\0') {
130                         if ((mark2 = (char *) strchr (dup, '\n')) != NULL) {
131                                 mark2[0] = '\0';
132                         }
133                         len = strlen (dup) + 3;
134                         while (bp + len - buf > bsize) {
135                                 bsize += BUFSIZ;
136                                 buf = (char *) ch_realloc (buf, bsize);
137                         }
138                         if (mark1[0] == '\0') {
139                                 sprintf (bp, "{} ");
140                         } else {
141                                 sprintf (bp, "{%s} ", dup);
142                         }
143                         bp += len;
144                         if (mark2 != NULL) {
145                                 mark2[0] = '\n';
146                         }
147                 }
148                 free (dup);
149         } while ((mark1 = (char *) strchr (mark1, '\n')) != NULL);
150         return buf;
151 }
152
153 int
154 tcl_ldap_debug (
155         ClientData clientData,
156         Tcl_Interp * interp,
157         int argc,
158         char *argv[]
159 )
160 {
161         if (argv[1] != NULL) {
162                 Debug (LDAP_DEBUG_ANY, "tcl_debug: %s\n", argv[1], 0, 0);
163         }
164         return TCL_OK;
165 }
166
167 void
168 readtclscript (
169         char *script,
170         Tcl_Interp * my_tcl)
171 {
172         int code;
173         FILE *f;
174
175         f = fopen (script, "r");
176         if (f == NULL) {
177                 Debug (LDAP_DEBUG_ANY, "Could not open scriptpath %s\n", script,
178                         0, 0);
179                 return;
180         }
181         fclose (f);
182         code = Tcl_EvalFile (my_tcl, script);
183         if (code != TCL_OK) {
184                 Debug (LDAP_DEBUG_ANY, "%s: %s\n", script,
185                         Tcl_GetVar (my_tcl, "errorInfo", TCL_GLOBAL_ONLY), 0);
186                 Debug (LDAP_DEBUG_ANY, "%s: error at line\n", script,
187                         my_tcl->errorLine, 0);
188                 return;
189         }
190 }