2 /* result.c - tcl backend utility functions
4 * Copyright 1999, Ben Collins <bcollins@debian.org>, All rights reserved.
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.
16 #include <ac/string.h>
17 #include <ac/socket.h>
18 #include <ac/unistd.h>
30 int bsize, len, argcPtr, i, code;
31 char *buf, *bp, **argvPtr, *line;
32 struct tclinfo *ti = (struct tclinfo *) op->o_bd->be_private;
35 * read in the result and send it along
37 buf = (char *) ch_malloc (BUFSIZ);
41 code = Tcl_SplitList (ti->ti_ii->interp, result, &argcPtr, &argvPtr);
44 send_ldap_error (op, rs, LDAP_UNWILLING_TO_PERFORM,
45 "internal backend error" );
48 for (i = 0; i < argcPtr; i++) {
52 * ignore lines beginning with DEBUG:
54 if (strncasecmp (line, "DEBUG:", 6) == 0) {
57 len = strlen (line) + 1;
58 while (bp + len - buf > bsize) {
60 buf = (char *) ch_realloc (buf, bsize);
62 sprintf (bp, "%s\n", line);
66 * line marked the end of an entry or result
68 if (line[0] == '\0') {
69 if (strncasecmp (buf, "RESULT", 6) == 0) {
72 if ((rs->sr_entry = str2entry (buf)) == NULL) {
73 Debug (LDAP_DEBUG_SHELL,
74 "str2entry(%s) failed\n",
77 rs->sr_attrs = op->oq_search.rs_attrs;
78 send_search_entry (op, rs);
79 entry_free (rs->sr_entry);
86 (void) str2result (buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text);
89 * otherwise, front end will send this result
91 if (rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND) {
92 send_ldap_result (op, rs);
96 Tcl_Free ((char *) argvPtr);
105 char *entrystr, *mark1, *mark2, *buf, *bp, *dup;
108 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
109 entrystr = entry2str (e, &len);
111 buf = (char *) ch_malloc (BUFSIZ);
119 if (mark1[0] == '\n') {
122 dup = (char *) ch_strdup (mark1);
123 if (dup[0] != '\0') {
124 if ((mark2 = (char *) strchr (dup, '\n')) != NULL) {
127 len = strlen (dup) + 3;
128 while (bp + len - buf > bsize) {
130 buf = (char *) ch_realloc (buf, bsize);
132 if (mark1[0] == '\0') {
135 sprintf (bp, "{%s} ", dup);
143 } while ((mark1 = (char *) strchr (mark1, '\n')) != NULL);
145 ldap_pvt_thread_mutex_unlock (&entry2str_mutex);
151 ClientData clientData,
157 if (argv[1] != NULL) {
158 Debug (LDAP_DEBUG_SHELL, "tcl_debug: %s\n", argv[1], 0, 0);
171 f = fopen (script, "r");
173 Debug (LDAP_DEBUG_SHELL, "Could not open scriptpath %s\n", script,
178 code = Tcl_EvalFile (my_tcl, script);
179 if (code != TCL_OK) {
180 Debug (LDAP_DEBUG_SHELL, "%s: %s\n", script,
181 Tcl_GetVar (my_tcl, "errorInfo", TCL_GLOBAL_ONLY), 0);
182 Debug (LDAP_DEBUG_SHELL, "%s: error at line\n", script,
183 my_tcl->errorLine, 0);
191 BerVarray bvlist, struct berval *out)
193 struct berval *ret = NULL;
200 ret = (struct berval *)ch_malloc(sizeof(struct berval));
211 for (i = 0; bvlist[i].bv_val != NULL; i++);
214 char **strlist = ch_malloc ((i + 1) * sizeof(char *));
215 if (strlist == NULL) {
220 for (i = 0; bvlist[i].bv_val != NULL; i++) {
221 strlist[i] = bvlist[i].bv_val;
224 ret->bv_val = Tcl_Merge(i, strlist);
225 ret->bv_len = ret->bv_val ? strlen(ret->bv_val) : 0;