Fixed bug #670: modulePath is not a path. Uses new utility
[idzebra-moved-to-github.git] / index / recctrl.c
1 /* $Id: recctrl.c,v 1.3 2006-10-11 08:55:52 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #include <string.h>
27 #if HAVE_DLFCN_H
28 #include <dlfcn.h>
29 #endif
30
31 #include <direntz.h>
32 #include <idzebra/util.h>
33 #include <idzebra/recctrl.h>
34
35 struct recTypeClass {
36     RecType recType;
37     struct recTypeClass *next;
38     void *module_handle;
39 };
40
41 struct recTypeInstance {
42     RecType recType;
43     struct recTypeInstance *next;
44     int init_flag;
45     void *clientData;
46 };
47
48 struct recTypes {
49     data1_handle dh;
50     struct recTypeInstance *entries;
51 };
52
53 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
54                               NMEM nmem, void *module_handle);
55
56
57 RecTypeClass recTypeClass_create (Res res, NMEM nmem)
58 {
59     struct recTypeClass *rts = 0;
60
61 #ifdef IDZEBRA_STATIC_GRS_SGML
62     if (1)
63     {
64         extern RecType idzebra_filter_grs_sgml[];
65         recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
66     }
67 #endif
68
69 #ifdef IDZEBRA_STATIC_TEXT
70     if (1)
71     {
72         extern RecType idzebra_filter_text[];
73         recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
74     }
75 #endif
76
77 #ifdef IDZEBRA_STATIC_GRS_XML
78 #if HAVE_EXPAT_H
79     if (1)
80     {
81         extern RecType idzebra_filter_grs_xml[];
82         recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
83     }
84 #endif
85 #endif
86
87 #ifdef IDZEBRA_STATIC_GRS_REGX
88     if (1)
89     {
90         extern RecType idzebra_filter_grs_regx[];
91         recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
92     }
93 #endif
94
95 #ifdef IDZEBRA_STATIC_GRS_MARC
96     if (1)
97     {
98         extern RecType idzebra_filter_grs_marc[];
99         recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
100     }
101 #endif
102
103 #ifdef IDZEBRA_STATIC_SAFARI
104     if (1)
105     {
106         extern RecType idzebra_filter_safari[];
107         recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
108     }
109 #endif
110
111 #ifdef IDZEBRA_STATIC_ALVIS
112 #if HAVE_XSLT
113     if (1)
114     {
115         extern RecType idzebra_filter_alvis[];
116         recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
117     }
118 #endif
119 #endif
120
121     return rts;
122 }
123
124 static void load_from_dir(RecTypeClass *rts, NMEM nmem, const char *dirname)
125 {
126 #if HAVE_DLFCN_H
127     DIR *dir = opendir(dirname);
128     if (dir)
129     {
130         struct dirent *de;
131         
132         while ((de = readdir(dir)))
133         {
134             size_t dlen = strlen(de->d_name);
135             if (dlen >= 5 &&
136                 !memcmp(de->d_name, "mod-", 4) &&
137                 !strcmp(de->d_name + dlen - 3, ".so"))
138             {
139                 void *mod_p, *fl;
140                 char fname[FILENAME_MAX*2+1];
141                 sprintf(fname, "%.*s/%.*s",
142                         FILENAME_MAX, dirname,
143                         FILENAME_MAX, de->d_name);
144                 mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
145                 if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
146                 {
147                     yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
148                     recTypeClass_add(rts, fl, nmem, mod_p);
149                 }
150                 else if (mod_p)
151                 {
152                     const char *err = dlerror();
153                     yaz_log(YLOG_WARN, "dlsym failed %s %s",
154                             fname, err ? err : "none");
155                     dlclose(mod_p);
156                 }
157                 else
158                 {
159                     const char *err = dlerror();
160                     yaz_log(YLOG_WARN, "dlopen failed %s %s",
161                             fname, err ? err : "none");
162                     
163                 }
164             }
165         }
166         closedir(dir);
167     }
168 #endif
169 }
170
171 void recTypeClass_load_modules(RecTypeClass *rts, NMEM nmem,
172                                const char *module_path)
173 {
174     while (module_path)
175     {
176         const char *comp_ptr;
177         char comp[FILENAME_MAX+1];
178         size_t len;
179         
180         len = yaz_filepath_comp(&module_path, &comp_ptr);
181         if (!len || len >= FILENAME_MAX)
182             break;
183         
184         memcpy(comp, comp_ptr, len);
185         comp[len] = '\0';
186
187         load_from_dir(rts, nmem, comp);
188     }
189 }
190
191 static void recTypeClass_add(struct recTypeClass **rts, RecType *rt,
192                              NMEM nmem, void *module_handle)
193 {
194     while (*rt)
195     {
196         struct recTypeClass *r = (struct recTypeClass *)
197             nmem_malloc (nmem, sizeof(*r));
198         
199         r->next = *rts;
200         *rts = r;
201
202         r->module_handle = module_handle;
203         module_handle = 0; /* so that we only store module_handle once */
204         r->recType = *rt;
205
206         rt++;
207     }
208 }
209
210 void recTypeClass_info(RecTypeClass rtc, void *cd,
211                        void (*cb)(void *cd, const char *s))
212 {
213     for (; rtc; rtc = rtc->next)
214         (*cb)(cd, rtc->recType->name);
215 }
216
217 void recTypeClass_destroy(RecTypeClass rtc)
218 {
219     for (; rtc; rtc = rtc->next)
220     {
221 #if HAVE_DLFCN_H
222         if (rtc->module_handle)
223             dlclose(rtc->module_handle);
224 #endif
225     }
226 }
227
228 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
229 {
230     RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
231
232     struct recTypeInstance **rti = &rts->entries;
233     
234     rts->dh = dh;
235
236     for (; rtc; rtc = rtc->next)
237     {
238         *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
239         (*rti)->recType = rtc->recType;
240         (*rti)->init_flag = 0;
241         rti = &(*rti)->next;
242     }
243     *rti = 0;
244     return rts;
245 }
246
247 void recTypes_destroy (RecTypes rts)
248 {
249     struct recTypeInstance *rti;
250
251     for (rti = rts->entries; rti; rti = rti->next)
252     {
253         if (rti->init_flag)
254             (*(rti->recType)->destroy)(rti->clientData);
255     }
256 }
257
258 RecType recType_byName (RecTypes rts, Res res, const char *name,
259                         void **clientDataP)
260 {
261     struct recTypeInstance *rti;
262
263     for (rti = rts->entries; rti; rti = rti->next)
264     {
265         size_t slen = strlen(rti->recType->name);
266         if (!strncmp (rti->recType->name, name, slen)
267             && (name[slen] == '\0' || name[slen] == '.'))
268         {
269             if (!rti->init_flag)
270             {
271                 rti->init_flag = 1;
272                 rti->clientData =
273                     (*(rti->recType)->init)(res, rti->recType);
274             }
275             *clientDataP = rti->clientData;
276             if (name[slen])
277                 slen++;  /* skip . */
278
279             if (rti->recType->config)
280             {
281                 if ((*(rti->recType)->config)
282                     (rti->clientData, res, name+slen) != ZEBRA_OK)
283                     return 0;
284             }
285             return rti->recType;
286         }
287     }
288     return 0;
289 }
290
291 /*
292  * Local variables:
293  * c-basic-offset: 4
294  * indent-tabs-mode: nil
295  * End:
296  * vim: shiftwidth=4 tabstop=8 expandtab
297  */
298