1 /* $Id: recctrl.c,v 1.22 2005-12-05 12:18:41 marc Exp $
2 Copyright (C) 1995-2005
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);
56 RecTypeClass recTypeClass_create (Res res, NMEM nmem)
58 struct recTypeClass *rts = 0;
60 const char *module_path = res_get_def(res, "modulePath",
64 #ifdef IDZEBRA_STATIC_GRS_SGML
67 extern RecType idzebra_filter_grs_sgml[];
68 recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
71 #ifdef IDZEBRA_STATIC_TEXT
74 extern RecType idzebra_filter_text[];
75 recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
78 #ifdef IDZEBRA_STATIC_GRS_XML
82 extern RecType idzebra_filter_grs_xml[];
83 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);
94 #ifdef IDZEBRA_STATIC_GRS_MARC
97 extern RecType idzebra_filter_grs_marc[];
98 recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
101 #ifdef IDZEBRA_STATIC_GRS_CSV
104 extern RecType idzebra_filter_grs_csv[];
105 recTypeClass_add (&rts, idzebra_filter_grs_csv, nmem, 0);
108 #ifdef IDZEBRA_STATIC_GRS_DANBIB
111 extern RecType idzebra_filter_grs_danbib[];
112 recTypeClass_add (&rts, idzebra_filter_grs_danbib, nmem, 0);
115 #ifdef IDZEBRA_STATIC_SAFARI
118 extern RecType idzebra_filter_safari[];
119 recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
122 #ifdef IDZEBRA_STATIC_ALVIS
126 extern RecType idzebra_filter_alvis[];
127 recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
131 #ifdef IDZEBRA_STATIC_XSLT
135 extern RecType idzebra_filter_xslt[];
136 recTypeClass_add (&rts, idzebra_filter_xslt, nmem, 0);
144 DIR *dir = opendir(module_path);
145 yaz_log(YLOG_LOG, "searching filters in %s", module_path);
150 while ((de = readdir(dir)))
152 size_t dlen = strlen(de->d_name);
154 !memcmp(de->d_name, "mod-", 4) &&
155 !strcmp(de->d_name + dlen - 3, ".so"))
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")))
165 yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
166 recTypeClass_add(&rts, fl, nmem, mod_p);
170 const char *err = dlerror();
171 yaz_log(YLOG_WARN, "dlsym failed %s %s",
172 fname, err ? err : "none");
177 const char *err = dlerror();
178 yaz_log(YLOG_WARN, "dlopen failed %s %s",
179 fname, err ? err : "none");
191 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
192 NMEM nmem, void *module_handle)
196 struct recTypeClass *r = (struct recTypeClass *)
197 nmem_malloc (nmem, sizeof(*r));
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 */
211 void recTypeClass_info(RecTypeClass rtc, void *cd,
212 void (*cb)(void *cd, const char *s))
214 for (; rtc; rtc = rtc->next)
215 (*cb)(cd, rtc->recType->name);
218 void recTypeClass_destroy(RecTypeClass rtc)
220 for (; rtc; rtc = rtc->next)
223 if (rtc->module_handle)
224 dlclose(rtc->module_handle);
229 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
231 RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
233 struct recTypeInstance **rti = &rts->entries;
237 for (; rtc; rtc = rtc->next)
239 *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
240 (*rti)->recType = rtc->recType;
241 (*rti)->init_flag = 0;
248 void recTypes_destroy (RecTypes rts)
250 struct recTypeInstance *rti;
252 for (rti = rts->entries; rti; rti = rti->next)
255 (*(rti->recType)->destroy)(rti->clientData);
259 RecType recType_byName (RecTypes rts, Res res, const char *name,
262 struct recTypeInstance *rti;
264 for (rti = rts->entries; rti; rti = rti->next)
266 size_t slen = strlen(rti->recType->name);
267 if (!strncmp (rti->recType->name, name, slen)
268 && (name[slen] == '\0' || name[slen] == '.'))
274 (*(rti->recType)->init)(res, rti->recType);
276 *clientDataP = rti->clientData;
280 if (rti->recType->config)
281 (*(rti->recType)->config)(rti->clientData, res, name+slen);