586a187de78f789bb652d3a656e0d43e5ed728c7
[idzebra-moved-to-github.git] / recctrl / recctrl.c
1 /* $Id: recctrl.c,v 1.24 2006-05-05 07:34:26 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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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_GRS_CSV
104     if (1)
105     {
106         extern RecType idzebra_filter_grs_csv[];
107         recTypeClass_add (&rts, idzebra_filter_grs_csv, nmem, 0);
108     }
109 #endif
110
111 #ifdef IDZEBRA_STATIC_GRS_DANBIB
112     if (1)
113     {
114         extern RecType idzebra_filter_grs_danbib[];
115         recTypeClass_add (&rts, idzebra_filter_grs_danbib, nmem, 0);
116     }
117 #endif
118
119 #ifdef IDZEBRA_STATIC_SAFARI
120     if (1)
121     {
122         extern RecType idzebra_filter_safari[];
123         recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
124     }
125 #endif
126
127 #ifdef IDZEBRA_STATIC_ALVIS
128 #if HAVE_XSLT
129     if (1)
130     {
131         extern RecType idzebra_filter_alvis[];
132         recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
133     }
134 #endif
135 #endif
136
137 #ifdef IDZEBRA_STATIC_XSLT
138 #if HAVE_XSLT
139     if (1)
140     {
141         extern RecType idzebra_filter_xslt[];
142         recTypeClass_add (&rts, idzebra_filter_xslt, nmem, 0);
143     }
144 #endif
145 #endif
146     return rts;
147 }
148
149 void recTypeClass_load_modules(RecTypeClass *rts, NMEM nmem,
150                                const char *module_path)
151 {
152 #if HAVE_DLFCN_H
153     if (module_path)
154     {
155         DIR *dir = opendir(module_path);
156         yaz_log(YLOG_LOG, "searching filters in %s", module_path);
157         if (dir)
158         {
159             struct dirent *de;
160
161             while ((de = readdir(dir)))
162             {
163                 size_t dlen = strlen(de->d_name);
164                 if (dlen >= 5 &&
165                     !memcmp(de->d_name, "mod-", 4) &&
166                     !strcmp(de->d_name + dlen - 3, ".so"))
167                 {
168                     void *mod_p, *fl;
169                     char fname[FILENAME_MAX*2+1];
170                     sprintf(fname, "%.*s/%.*s",
171                             FILENAME_MAX, module_path,
172                             FILENAME_MAX, de->d_name);
173                     mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
174                     if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
175                     {
176                         yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
177                         recTypeClass_add(rts, fl, nmem, mod_p);
178                     }
179                     else if (mod_p)
180                     {
181                         const char *err = dlerror();
182                         yaz_log(YLOG_WARN, "dlsym failed %s %s",
183                                 fname, err ? err : "none");
184                         dlclose(mod_p);
185                     }
186                     else
187                     {
188                         const char *err = dlerror();
189                         yaz_log(YLOG_WARN, "dlopen failed %s %s",
190                                 fname, err ? err : "none");
191                         
192                     }
193                 }
194             }
195             closedir(dir);
196         }
197     }
198 #endif
199 }
200
201 static void recTypeClass_add(struct recTypeClass **rts, RecType *rt,
202                              NMEM nmem, void *module_handle)
203 {
204     while (*rt)
205     {
206         struct recTypeClass *r = (struct recTypeClass *)
207             nmem_malloc (nmem, sizeof(*r));
208         
209         r->next = *rts;
210         *rts = r;
211
212         yaz_log(YLOG_LOG, "Adding filter %s", (*rt)->name);
213         r->module_handle = module_handle;
214         module_handle = 0; /* so that we only store module_handle once */
215         r->recType = *rt;
216
217         rt++;
218     }
219 }
220
221 void recTypeClass_info(RecTypeClass rtc, void *cd,
222                        void (*cb)(void *cd, const char *s))
223 {
224     for (; rtc; rtc = rtc->next)
225         (*cb)(cd, rtc->recType->name);
226 }
227
228 void recTypeClass_destroy(RecTypeClass rtc)
229 {
230     for (; rtc; rtc = rtc->next)
231     {
232 #if HAVE_DLFCN_H
233         if (rtc->module_handle)
234             dlclose(rtc->module_handle);
235 #endif
236     }
237 }
238
239 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
240 {
241     RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
242
243     struct recTypeInstance **rti = &rts->entries;
244     
245     rts->dh = dh;
246
247     for (; rtc; rtc = rtc->next)
248     {
249         *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
250         (*rti)->recType = rtc->recType;
251         (*rti)->init_flag = 0;
252         rti = &(*rti)->next;
253     }
254     *rti = 0;
255     return rts;
256 }
257
258 void recTypes_destroy (RecTypes rts)
259 {
260     struct recTypeInstance *rti;
261
262     for (rti = rts->entries; rti; rti = rti->next)
263     {
264         if (rti->init_flag)
265             (*(rti->recType)->destroy)(rti->clientData);
266     }
267 }
268
269 RecType recType_byName (RecTypes rts, Res res, const char *name,
270                         void **clientDataP)
271 {
272     struct recTypeInstance *rti;
273
274     for (rti = rts->entries; rti; rti = rti->next)
275     {
276         size_t slen = strlen(rti->recType->name);
277         if (!strncmp (rti->recType->name, name, slen)
278             && (name[slen] == '\0' || name[slen] == '.'))
279         {
280             if (!rti->init_flag)
281             {
282                 rti->init_flag = 1;
283                 rti->clientData =
284                     (*(rti->recType)->init)(res, rti->recType);
285             }
286             *clientDataP = rti->clientData;
287             if (name[slen])
288                 slen++;  /* skip . */
289
290             if (rti->recType->config)
291             {
292                 if ((*(rti->recType)->config)
293                     (rti->clientData, res, name+slen) != ZEBRA_OK)
294                     return 0;
295             }
296             return rti->recType;
297         }
298     }
299     return 0;
300 }
301