Dont declare module_path if dlfcn.h is not set
[idzebra-moved-to-github.git] / recctrl / recctrl.c
1 /* $Id: recctrl.c,v 1.21 2005-08-30 12:27:18 adam Exp $
2    Copyright (C) 1995-2005
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 RecTypeClass recTypeClass_create (Res res, NMEM nmem)
57 {
58     struct recTypeClass *rts = 0;
59 #if HAVE_DLFCN_H
60     const char *module_path = res_get_def(res, "modulePath",
61                                           DEFAULT_MODULE_PATH);
62 #endif
63
64 #ifdef IDZEBRA_STATIC_GRS_SGML
65     if (1)
66     {
67         extern RecType idzebra_filter_grs_sgml[];
68         recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
69     }
70 #endif
71 #ifdef IDZEBRA_STATIC_TEXT
72     if (1)
73     {
74         extern RecType idzebra_filter_text[];
75         recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
76     }
77 #endif
78 #ifdef IDZEBRA_STATIC_GRS_XML
79 #if HAVE_EXPAT_H
80     if (1)
81     {
82         extern RecType idzebra_filter_grs_xml[];
83         recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
84     }
85 #endif
86 #endif
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 #ifdef IDZEBRA_STATIC_GRS_MARC
95     if (1)
96     {
97         extern RecType idzebra_filter_grs_marc[];
98         recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
99     }
100 #endif
101 #ifdef IDZEBRA_STATIC_GRS_DANBIB
102     if (1)
103     {
104         extern RecType idzebra_filter_grs_danbib[];
105         recTypeClass_add (&rts, idzebra_filter_grs_danbib, nmem, 0);
106     }
107 #endif
108 #ifdef IDZEBRA_STATIC_SAFARI
109     if (1)
110     {
111         extern RecType idzebra_filter_safari[];
112         recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
113     }
114 #endif
115 #ifdef IDZEBRA_STATIC_ALVIS
116 #if HAVE_XSLT
117     if (1)
118     {
119         extern RecType idzebra_filter_alvis[];
120         recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
121     }
122 #endif
123 #endif
124 #ifdef IDZEBRA_STATIC_XSLT
125 #if HAVE_XSLT
126     if (1)
127     {
128         extern RecType idzebra_filter_xslt[];
129         recTypeClass_add (&rts, idzebra_filter_xslt, nmem, 0);
130     }
131 #endif
132 #endif
133
134 #if HAVE_DLFCN_H
135     if (module_path)
136     {
137         DIR *dir = opendir(module_path);
138         yaz_log(YLOG_LOG, "searching filters in %s", module_path);
139         if (dir)
140         {
141             struct dirent *de;
142
143             while ((de = readdir(dir)))
144             {
145                 size_t dlen = strlen(de->d_name);
146                 if (dlen >= 5 &&
147                     !memcmp(de->d_name, "mod-", 4) &&
148                     !strcmp(de->d_name + dlen - 3, ".so"))
149                 {
150                     void *mod_p, *fl;
151                     char fname[FILENAME_MAX*2+1];
152                     sprintf(fname, "%.*s/%.*s",
153                             FILENAME_MAX, module_path,
154                             FILENAME_MAX, de->d_name);
155                     mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
156                     if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
157                     {
158                         yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
159                         recTypeClass_add(&rts, fl, nmem, mod_p);
160                     }
161                     else if (mod_p)
162                     {
163                         const char *err = dlerror();
164                         yaz_log(YLOG_WARN, "dlsym failed %s %s",
165                                 fname, err ? err : "none");
166                         dlclose(mod_p);
167                     }
168                     else
169                     {
170                         const char *err = dlerror();
171                         yaz_log(YLOG_WARN, "dlopen failed %s %s",
172                                 fname, err ? err : "none");
173                         
174                     }
175                 }
176             }
177             closedir(dir);
178         }
179     }
180 #endif
181     return rts;
182 }
183
184 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
185                               NMEM nmem, void *module_handle)
186 {
187     while (*rt)
188     {
189         struct recTypeClass *r = (struct recTypeClass *)
190             nmem_malloc (nmem, sizeof(*r));
191         
192         r->next = *rts;
193         *rts = r;
194
195         yaz_log(YLOG_LOG, "Adding filter %s", (*rt)->name);
196         r->module_handle = module_handle;
197         module_handle = 0; /* so that we only store module_handle once */
198         r->recType = *rt;
199
200         rt++;
201     }
202 }
203
204 void recTypeClass_info(RecTypeClass rtc, void *cd,
205                        void (*cb)(void *cd, const char *s))
206 {
207     for (; rtc; rtc = rtc->next)
208         (*cb)(cd, rtc->recType->name);
209 }
210
211 void recTypeClass_destroy(RecTypeClass rtc)
212 {
213     for (; rtc; rtc = rtc->next)
214     {
215 #if HAVE_DLFCN_H
216         if (rtc->module_handle)
217             dlclose(rtc->module_handle);
218 #endif
219     }
220 }
221
222 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
223 {
224     RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
225
226     struct recTypeInstance **rti = &rts->entries;
227     
228     rts->dh = dh;
229
230     for (; rtc; rtc = rtc->next)
231     {
232         *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
233         (*rti)->recType = rtc->recType;
234         (*rti)->init_flag = 0;
235         rti = &(*rti)->next;
236     }
237     *rti = 0;
238     return rts;
239 }
240
241 void recTypes_destroy (RecTypes rts)
242 {
243     struct recTypeInstance *rti;
244
245     for (rti = rts->entries; rti; rti = rti->next)
246     {
247         if (rti->init_flag)
248             (*(rti->recType)->destroy)(rti->clientData);
249     }
250 }
251
252 RecType recType_byName (RecTypes rts, Res res, const char *name,
253                         void **clientDataP)
254 {
255     struct recTypeInstance *rti;
256
257     for (rti = rts->entries; rti; rti = rti->next)
258     {
259         size_t slen = strlen(rti->recType->name);
260         if (!strncmp (rti->recType->name, name, slen)
261             && (name[slen] == '\0' || name[slen] == '.'))
262         {
263             if (!rti->init_flag)
264             {
265                 rti->init_flag = 1;
266                 rti->clientData =
267                     (*(rti->recType)->init)(res, rti->recType);
268             }
269             *clientDataP = rti->clientData;
270             if (name[slen])
271                 slen++;  /* skip . */
272
273             if (rti->recType->config)
274                 (*(rti->recType)->config)(rti->clientData, res, name+slen);
275             return rti->recType;
276         }
277     }
278     return 0;
279 }
280