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