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