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