1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <idzebra/util.h>
30 #include <idzebra/recctrl.h>
34 struct recTypeClass *next;
38 struct recTypeInstance {
40 struct recTypeInstance *next;
47 struct recTypeInstance *entries;
50 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
51 NMEM nmem, void *module_handle);
54 RecTypeClass recTypeClass_create (Res res, NMEM nmem)
56 struct recTypeClass *rts = 0;
58 #ifdef IDZEBRA_STATIC_GRS_SGML
61 extern RecType idzebra_filter_grs_sgml[];
62 recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
66 #ifdef IDZEBRA_STATIC_TEXT
69 extern RecType idzebra_filter_text[];
70 recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
74 #ifdef IDZEBRA_STATIC_GRS_XML
78 extern RecType idzebra_filter_grs_xml[];
79 recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
84 #ifdef IDZEBRA_STATIC_GRS_REGX
87 extern RecType idzebra_filter_grs_regx[];
88 recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
92 #ifdef IDZEBRA_STATIC_GRS_MARC
95 extern RecType idzebra_filter_grs_marc[];
96 recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
100 #ifdef IDZEBRA_STATIC_SAFARI
103 extern RecType idzebra_filter_safari[];
104 recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
108 #ifdef IDZEBRA_STATIC_ALVIS
111 extern RecType idzebra_filter_alvis[];
112 recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
116 #ifdef IDZEBRA_STATIC_DOM
119 extern RecType idzebra_filter_dom[];
120 recTypeClass_add (&rts, idzebra_filter_dom, nmem, 0);
127 static void load_from_dir(RecTypeClass *rts, NMEM nmem, const char *dirname)
130 DIR *dir = opendir(dirname);
135 while ((de = readdir(dir)))
137 size_t dlen = strlen(de->d_name);
139 !memcmp(de->d_name, "mod-", 4) &&
140 !strcmp(de->d_name + dlen - 3, ".so"))
143 char fname[FILENAME_MAX*2+1];
144 sprintf(fname, "%.*s/%.*s",
145 FILENAME_MAX, dirname,
146 FILENAME_MAX, de->d_name);
147 mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
148 if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
150 yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
151 recTypeClass_add(rts, fl, nmem, mod_p);
155 const char *err = dlerror();
156 yaz_log(YLOG_WARN, "dlsym failed %s %s",
157 fname, err ? err : "none");
162 const char *err = dlerror();
163 yaz_log(YLOG_WARN, "dlopen failed %s %s",
164 fname, err ? err : "none");
174 void recTypeClass_load_modules(RecTypeClass *rts, NMEM nmem,
175 const char *module_path)
179 const char *comp_ptr;
180 char comp[FILENAME_MAX+1];
183 len = yaz_filepath_comp(&module_path, &comp_ptr);
184 if (!len || len >= FILENAME_MAX)
187 memcpy(comp, comp_ptr, len);
190 load_from_dir(rts, nmem, comp);
194 static void recTypeClass_add(struct recTypeClass **rts, RecType *rt,
195 NMEM nmem, void *module_handle)
199 struct recTypeClass *r = (struct recTypeClass *)
200 nmem_malloc (nmem, sizeof(*r));
205 r->module_handle = module_handle;
206 module_handle = 0; /* so that we only store module_handle once */
213 void recTypeClass_info(RecTypeClass rtc, void *cd,
214 void (*cb)(void *cd, const char *s))
216 for (; rtc; rtc = rtc->next)
217 (*cb)(cd, rtc->recType->name);
220 void recTypeClass_destroy(RecTypeClass rtc)
222 for (; rtc; rtc = rtc->next)
225 if (rtc->module_handle)
226 dlclose(rtc->module_handle);
231 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
233 RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
235 struct recTypeInstance **rti = &rts->entries;
239 for (; rtc; rtc = rtc->next)
241 *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
242 (*rti)->recType = rtc->recType;
243 (*rti)->init_flag = 0;
250 void recTypes_destroy (RecTypes rts)
252 struct recTypeInstance *rti;
254 for (rti = rts->entries; rti; rti = rti->next)
257 (*(rti->recType)->destroy)(rti->clientData);
261 RecType recType_byName (RecTypes rts, Res res, const char *name,
264 struct recTypeInstance *rti;
266 for (rti = rts->entries; rti; rti = rti->next)
268 size_t slen = strlen(rti->recType->name);
269 if (!strncmp (rti->recType->name, name, slen)
270 && (name[slen] == '\0' || name[slen] == '.'))
276 (*(rti->recType)->init)(res, rti->recType);
278 *clientDataP = rti->clientData;
282 if (rti->recType->config)
284 if ((*(rti->recType)->config)
285 (rti->clientData, res, name+slen) != ZEBRA_OK)
297 * c-file-style: "Stroustrup"
298 * indent-tabs-mode: nil
300 * vim: shiftwidth=4 tabstop=8 expandtab