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