]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_config.c
Moved tcl_back_db_open() and interpreter init to tcl_init.c
[openldap] / servers / slapd / back-tcl / tcl_config.c
1 /*
2  * config.c - tcl backend configuration file routine
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  * $Id: tcl_config.c,v 1.2 1999/02/16 23:30:36 bcollins Exp $
12  *
13  * $Log: tcl_config.c,v $
14  * Revision 1.2  1999/02/16 23:30:36  bcollins
15  * fixed exit()'s to be return()'s
16  *
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include "slap.h"
24 #include "tcl_back.h"
25
26 struct i_info *global_i;
27
28 int tcl_back_db_config (
29         BackendDB * bd,
30         char *fname,
31         int lineno,
32         int argc,
33         char **argv
34 )
35 {
36         struct tclinfo *ti = (struct tclinfo *) bd->be_private;
37         int script_loaded = 0;
38
39         if (ti == NULL) {
40                 fprintf (stderr, "%s: line %d: tcl backend info is null!\n", fname,
41                         lineno);
42                 return( 1 );
43         }
44         if (ti->ti_ii == NULL) {
45                 ti->ti_ii = global_i;
46         }
47
48         /* Script to load */
49         if (strcasecmp (argv[0], "scriptpath") == 0) {
50                 if (argc < 2) {
51                         Debug (LDAP_DEBUG_ANY,
52                                 "%s: line %d: missing script in \"scriptpath <script>\" line\n",
53                                 fname, lineno, 0);
54                         return( 1 );
55                 }
56                 ti->script_path = (char *) strdup (argv[1]);
57
58         /* use local interpreter */
59         } else if (strcasecmp (argv[0], "tclrealm") == 0) {
60                 struct i_info *ii;
61
62                 if (argc < 2) {
63                         Debug (LDAP_DEBUG_ANY,
64                                 "%s: line %d: missing script in \"tclrealm <name>\" line\n",
65                                 fname, lineno, 0);
66                         return( 1 );
67                 }
68                 ti->ti_ii = NULL;
69
70                 ii = global_i;
71                 /* Try to see if it already exists */
72                 do {
73                         if (ii != NULL && !strcasecmp (ii->name, argv[1]))
74                         ti->ti_ii = ii;
75                         if (ii->next != NULL)
76                         ii = ii->next;
77                 } while (ii->next != NULL);
78
79                 if (ti->ti_ii == NULL) { /* we need to make a new one */
80                         ii->next = (struct i_info *) ch_malloc
81                                 (sizeof (struct i_info));
82
83                         ii->next->count = 0;
84                         ii->next->name = (char *) strdup (argv[1]);
85                         ii->next->interp = NULL;
86                         ii->next->next = NULL;
87                         ti->ti_ii = ii->next;
88                 }
89
90         /* proc for binds */
91         } else if (strcasecmp (argv[0], "bind") == 0) {
92                 if (argc < 2) {
93                         Debug (LDAP_DEBUG_ANY,
94                                 "%s: line %d: missing proc in \"bind <proc>\" line\n",
95                                 fname, lineno, 0);
96                         return( 1 );
97                 }
98                 ti->ti_bind = (char *) strdup (argv[1]);
99
100         /* proc for unbinds */
101         } else if (strcasecmp (argv[0], "unbind") == 0) {
102                 if (argc < 2) {
103                         Debug (LDAP_DEBUG_ANY,
104                         "%s: line %d: missing proc in \"unbind <proc>\" line\n",
105                         fname, lineno, 0);
106                         return( 1 );
107                 }
108                 ti->ti_unbind = (char *) strdup (argv[1]);
109
110         /* proc for search */
111         } else if (strcasecmp (argv[0], "search") == 0) {
112                 if (argc < 2) {
113                         Debug (LDAP_DEBUG_ANY,
114                                 "%s: line %d: missing proc in \"search <proc>\" line\n",
115                                 fname, lineno, 0);
116                         return( 1 );
117                 }
118                 ti->ti_search = (char *) strdup (argv[1]);
119
120         /* proc for compares */
121         } else if (strcasecmp (argv[0], "compare") == 0) {
122                 if (argc < 2) {
123                         Debug (LDAP_DEBUG_ANY,
124                                 "%s: line %d: missing proc in \"compare <proc>\" line\n",
125                                 fname, lineno, 0);
126                         return( 1 );
127                 }
128                 ti->ti_compare = (char *) strdup (argv[1]);
129
130         /* proc for modify */
131         } else if (strcasecmp (argv[0], "modify") == 0) {
132                 if (argc < 2) {
133                         Debug (LDAP_DEBUG_ANY,
134                                 "%s: line %d: missing proc in \"modify <proc>\" line\n",
135                                 fname, lineno, 0);
136                         return( 1 );
137                 }
138                 ti->ti_modify = (char *) strdup (argv[1]);
139
140         /* proc for modrdn */
141         } else if (strcasecmp (argv[0], "modrdn") == 0) {
142                 if (argc < 2) {
143                         Debug (LDAP_DEBUG_ANY,
144                                 "%s: line %d: missing proc in \"modrdn <proc>\" line\n",
145                                 fname, lineno, 0);
146                         return( 1 );
147                 }
148                 ti->ti_modrdn = (char *) strdup (argv[1]);
149
150         /* proc for add */
151         } else if (strcasecmp (argv[0], "add") == 0) {
152                 if (argc < 2) {
153                         Debug (LDAP_DEBUG_ANY,
154                                 "%s: line %d: missing proc in \"add <proc>\" line\n",
155                                 fname, lineno, 0);
156                         return( 1 );
157                 }
158                 ti->ti_add = (char *) strdup (argv[1]);
159
160         /* proc for delete */
161         } else if (strcasecmp (argv[0], "delete") == 0) {
162                 if (argc < 2) {
163                         Debug (LDAP_DEBUG_ANY,
164                                 "%s: line %d: missing proc in \"delete <proc>\" line\n",
165                                 fname, lineno, 0);
166                         return( 1 );
167                 }
168                 ti->ti_delete = (char *) strdup (argv[1]);
169
170         /* proc for abandon */
171         } else if (strcasecmp (argv[0], "abandon") == 0) {
172                 if (argc < 2) {
173                         Debug (LDAP_DEBUG_ANY,
174                                 "%s: line %d: missing proc in \"abandon <proc>\" line\n",
175                                 fname, lineno, 0);
176                         return( 1 );
177                 }
178                 ti->ti_search = (char *) strdup (argv[1]);
179
180         } else {
181                 Debug (LDAP_DEBUG_ANY,
182                         "Unknown tcl backend config: %s\n", argv[0], 0, 0);
183                 return( 1 );
184         }
185
186         return 0;
187 }
188
189 void readtclscript (char *script, Tcl_Interp * my_tcl)
190 {
191         int code;
192         FILE *f;
193         f = fopen (script, "r");
194         if (f == NULL) {
195                 Debug (LDAP_DEBUG_ANY, "Could not open scriptpath %s\n", script,
196                         0, 0);
197                 return( 1 );
198         }
199         fclose (f);
200         code = Tcl_EvalFile (my_tcl, script);
201         if (code != TCL_OK) {
202                 Debug (LDAP_DEBUG_ANY, "%s: %s\n", script,
203                         Tcl_GetVar (my_tcl, "errorInfo", TCL_GLOBAL_ONLY), 0);
204                 Debug (LDAP_DEBUG_ANY, "%s: error at line\n", script,
205                         my_tcl->errorLine, 0);
206                 return( 1 );
207         }
208 }