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>
33 int bsize, len, argcPtr, i, err, code;
34 char *buf, *bp, **argvPtr, *line, *matched, *info;
36 struct tclinfo *ti = (struct tclinfo *) be->be_private;
39 * read in the result and send it along
41 buf = (char *) ch_malloc (BUFSIZ);
45 code = Tcl_SplitList (ti->ti_ii->interp, result, &argcPtr, &argvPtr);
48 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
49 "internal backend error", NULL, NULL );
52 for (i = 0; i < argcPtr; i++) {
56 * ignore lines beginning with DEBUG:
58 if (strncasecmp (line, "DEBUG:", 6) == 0) {
61 len = strlen (line) + 1;
62 while (bp + len - buf > bsize) {
64 buf = (char *) ch_realloc (buf, bsize);
66 sprintf (bp, "%s\n", line);
70 * line marked the end of an entry or result
72 if (line[0] == '\0') {
73 if (strncasecmp (buf, "RESULT", 6) == 0) {
76 if ((e = str2entry (buf)) == NULL) {
77 Debug (LDAP_DEBUG_SHELL,
78 "str2entry(%s) failed\n",
81 send_search_entry (be, conn, op, e, attrs,
90 (void) str2result (buf, &err, &matched, &info);
93 * otherwise, front end will send this result
95 if (err != 0 || op->o_tag != LDAP_REQ_BIND) {
96 send_ldap_result (conn, op, err, matched, info, NULL, NULL );
100 Tcl_Free ((char *) argvPtr);
109 char *entrystr, *mark1, *mark2, *buf, *bp, *dup;
112 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
113 entrystr = entry2str (e, &len);
115 buf = (char *) ch_malloc (BUFSIZ);
123 if (mark1[0] == '\n') {
126 dup = (char *) ch_strdup (mark1);
127 if (dup[0] != '\0') {
128 if ((mark2 = (char *) strchr (dup, '\n')) != NULL) {
131 len = strlen (dup) + 3;
132 while (bp + len - buf > bsize) {
134 buf = (char *) ch_realloc (buf, bsize);
136 if (mark1[0] == '\0') {
139 sprintf (bp, "{%s} ", dup);
147 } while ((mark1 = (char *) strchr (mark1, '\n')) != NULL);
149 ldap_pvt_thread_mutex_unlock (&entry2str_mutex);
155 ClientData clientData,
161 if (argv[1] != NULL) {
162 Debug (LDAP_DEBUG_SHELL, "tcl_debug: %s\n", argv[1], 0, 0);
175 f = fopen (script, "r");
177 Debug (LDAP_DEBUG_SHELL, "Could not open scriptpath %s\n", script,
182 code = Tcl_EvalFile (my_tcl, script);
183 if (code != TCL_OK) {
184 Debug (LDAP_DEBUG_SHELL, "%s: %s\n", script,
185 Tcl_GetVar (my_tcl, "errorInfo", TCL_GLOBAL_ONLY), 0);
186 Debug (LDAP_DEBUG_SHELL, "%s: error at line\n", script,
187 my_tcl->errorLine, 0);
195 BerVarray bvlist, struct berval *out)
197 struct berval *ret = NULL;
204 ret = (struct berval *)ch_malloc(sizeof(struct berval));
215 for (i = 0; bvlist[i].bv_val != NULL; i++);
218 char **strlist = ch_malloc ((i + 1) * sizeof(char *));
219 if (strlist == NULL) {
224 for (i = 0; bvlist[i].bv_val != NULL; i++) {
225 strlist[i] = bvlist[i].bv_val;
228 ret->bv_val = Tcl_Merge(i, strlist);
229 ret->bv_len = ret->bv_val ? strlen(ret->bv_val) : 0;