1 /* $Id: recctrl.c,v 1.25 2006-05-10 08:13:28 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
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
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
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
32 #include <idzebra/util.h>
33 #include <idzebra/recctrl.h>
37 struct recTypeClass *next;
41 struct recTypeInstance {
43 struct recTypeInstance *next;
50 struct recTypeInstance *entries;
53 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
54 NMEM nmem, void *module_handle);
57 RecTypeClass recTypeClass_create (Res res, NMEM nmem)
59 struct recTypeClass *rts = 0;
61 #ifdef IDZEBRA_STATIC_GRS_SGML
64 extern RecType idzebra_filter_grs_sgml[];
65 recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
69 #ifdef IDZEBRA_STATIC_TEXT
72 extern RecType idzebra_filter_text[];
73 recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
77 #ifdef IDZEBRA_STATIC_GRS_XML
81 extern RecType idzebra_filter_grs_xml[];
82 recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
87 #ifdef IDZEBRA_STATIC_GRS_REGX
90 extern RecType idzebra_filter_grs_regx[];
91 recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
95 #ifdef IDZEBRA_STATIC_GRS_MARC
98 extern RecType idzebra_filter_grs_marc[];
99 recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
103 #ifdef IDZEBRA_STATIC_GRS_CSV
106 extern RecType idzebra_filter_grs_csv[];
107 recTypeClass_add (&rts, idzebra_filter_grs_csv, nmem, 0);
111 #ifdef IDZEBRA_STATIC_GRS_DANBIB
114 extern RecType idzebra_filter_grs_danbib[];
115 recTypeClass_add (&rts, idzebra_filter_grs_danbib, nmem, 0);
119 #ifdef IDZEBRA_STATIC_SAFARI
122 extern RecType idzebra_filter_safari[];
123 recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
127 #ifdef IDZEBRA_STATIC_ALVIS
131 extern RecType idzebra_filter_alvis[];
132 recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
137 #ifdef IDZEBRA_STATIC_XSLT
141 extern RecType idzebra_filter_xslt[];
142 recTypeClass_add (&rts, idzebra_filter_xslt, nmem, 0);
149 void recTypeClass_load_modules(RecTypeClass *rts, NMEM nmem,
150 const char *module_path)
155 DIR *dir = opendir(module_path);
156 yaz_log(YLOG_LOG, "searching filters in %s", module_path);
161 while ((de = readdir(dir)))
163 size_t dlen = strlen(de->d_name);
165 !memcmp(de->d_name, "mod-", 4) &&
166 !strcmp(de->d_name + dlen - 3, ".so"))
169 char fname[FILENAME_MAX*2+1];
170 sprintf(fname, "%.*s/%.*s",
171 FILENAME_MAX, module_path,
172 FILENAME_MAX, de->d_name);
173 mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
174 if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
176 yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
177 recTypeClass_add(rts, fl, nmem, mod_p);
181 const char *err = dlerror();
182 yaz_log(YLOG_WARN, "dlsym failed %s %s",
183 fname, err ? err : "none");
188 const char *err = dlerror();
189 yaz_log(YLOG_WARN, "dlopen failed %s %s",
190 fname, err ? err : "none");
201 static void recTypeClass_add(struct recTypeClass **rts, RecType *rt,
202 NMEM nmem, void *module_handle)
206 struct recTypeClass *r = (struct recTypeClass *)
207 nmem_malloc (nmem, sizeof(*r));
212 yaz_log(YLOG_LOG, "Adding filter %s", (*rt)->name);
213 r->module_handle = module_handle;
214 module_handle = 0; /* so that we only store module_handle once */
221 void recTypeClass_info(RecTypeClass rtc, void *cd,
222 void (*cb)(void *cd, const char *s))
224 for (; rtc; rtc = rtc->next)
225 (*cb)(cd, rtc->recType->name);
228 void recTypeClass_destroy(RecTypeClass rtc)
230 for (; rtc; rtc = rtc->next)
233 if (rtc->module_handle)
234 dlclose(rtc->module_handle);
239 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
241 RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
243 struct recTypeInstance **rti = &rts->entries;
247 for (; rtc; rtc = rtc->next)
249 *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
250 (*rti)->recType = rtc->recType;
251 (*rti)->init_flag = 0;
258 void recTypes_destroy (RecTypes rts)
260 struct recTypeInstance *rti;
262 for (rti = rts->entries; rti; rti = rti->next)
265 (*(rti->recType)->destroy)(rti->clientData);
269 RecType recType_byName (RecTypes rts, Res res, const char *name,
272 struct recTypeInstance *rti;
274 for (rti = rts->entries; rti; rti = rti->next)
276 size_t slen = strlen(rti->recType->name);
277 if (!strncmp (rti->recType->name, name, slen)
278 && (name[slen] == '\0' || name[slen] == '.'))
284 (*(rti->recType)->init)(res, rti->recType);
286 *clientDataP = rti->clientData;
290 if (rti->recType->config)
292 if ((*(rti->recType)->config)
293 (rti->clientData, res, name+slen) != ZEBRA_OK)
305 * indent-tabs-mode: nil
307 * vim: shiftwidth=4 tabstop=8 expandtab