1 /* $Id: recctrl.c,v 1.12 2004-09-30 08:30:05 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
31 #include <idzebra/recctrl.h>
35 struct recTypeClass *next;
39 struct recTypeInstance {
41 struct recTypeInstance *next;
48 struct recTypeInstance *entries;
51 #ifdef IDZEBRA_STATIC_TEXT
52 extern RecType idzebra_filter_text[];
54 #ifdef IDZEBRA_STATIC_GRS_XML
56 extern RecType idzebra_filter_grs_xml[];
59 #ifdef IDZEBRA_STATIC_GRS_REGX
60 extern RecType idzebra_filter_grs_regx[];
62 #ifdef IDZEBRA_STATIC_GRS_MARC
63 extern RecType idzebra_filter_grs_marc[];
65 #ifdef IDZEBRA_STATIC_GRS_DANBIB
66 extern RecType idzebra_filter_grs_danbib[];
69 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
70 NMEM nmem, void *module_handle);
72 RecTypeClass recTypeClass_create (Res res, NMEM nmem)
74 struct recTypeClass *rts = 0;
75 const char *module_path = res_get_def(res, "modulePath",
78 extern RecType idzebra_filter_grs_sgml[];
79 recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
80 #ifdef IDZEBRA_STATIC_TEXT
81 recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
83 #ifdef IDZEBRA_STATIC_GRS_XML
85 recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
88 #ifdef IDZEBRA_STATIC_GRS_REGX
89 recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
91 #ifdef IDZEBRA_STATIC_GRS_MARC
92 recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
94 #ifdef IDZEBRA_STATIC_GRS_DANBIB
95 recTypeClass_add (&rts, idzebra_filter_grs_danbib, nmem, 0);
100 DIR *dir = opendir(module_path);
101 yaz_log(LOG_LOG, "searching filters in %s", module_path);
106 while ((de = readdir(dir)))
108 size_t dlen = strlen(de->d_name);
110 !memcmp(de->d_name, "mod-", 4) &&
111 !strcmp(de->d_name + dlen - 3, ".so"))
114 char fname[FILENAME_MAX*2+1];
115 sprintf(fname, "%.*s/%.*s",
116 FILENAME_MAX, module_path,
117 FILENAME_MAX, de->d_name);
118 mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
119 if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
121 yaz_log(LOG_LOG, "Loaded filter module %s", fname);
122 recTypeClass_add(&rts, fl, nmem, mod_p);
126 const char *err = dlerror();
127 yaz_log(LOG_WARN, "dlsym failed %s %s",
128 fname, err ? err : "none");
133 const char *err = dlerror();
134 yaz_log(LOG_WARN, "dlopen failed %s %s",
135 fname, err ? err : "none");
146 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
147 NMEM nmem, void *module_handle)
151 struct recTypeClass *r = (struct recTypeClass *)
152 nmem_malloc (nmem, sizeof(*r));
157 yaz_log(LOG_LOG, "Adding filter %s", (*rt)->name);
158 r->module_handle = module_handle;
159 module_handle = 0; /* so that we only store module_handle once */
166 void recTypeClass_info(RecTypeClass rtc, void *cd,
167 void (*cb)(void *cd, const char *s))
169 for (; rtc; rtc = rtc->next)
170 (*cb)(cd, rtc->recType->name);
173 void recTypeClass_destroy(RecTypeClass rtc)
175 for (; rtc; rtc = rtc->next)
177 if (rtc->module_handle)
178 dlclose(rtc->module_handle);
182 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
184 RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
186 struct recTypeInstance **rti = &rts->entries;
190 for (; rtc; rtc = rtc->next)
192 *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
193 (*rti)->recType = rtc->recType;
194 (*rti)->init_flag = 0;
201 void recTypes_destroy (RecTypes rts)
203 struct recTypeInstance *rti;
205 for (rti = rts->entries; rti; rti = rti->next)
208 (*(rti->recType)->destroy)(rti->clientData);
212 RecType recType_byName (RecTypes rts, Res res, const char *name,
215 struct recTypeInstance *rti;
217 for (rti = rts->entries; rti; rti = rti->next)
219 size_t slen = strlen(rti->recType->name);
220 if (!strncmp (rti->recType->name, name, slen)
221 && (name[slen] == '\0' || name[slen] == '.'))
227 (*(rti->recType)->init)(res, rti->recType);
229 *clientDataP = rti->clientData;
233 if (rti->recType->config)
234 (*(rti->recType)->config)(rti->clientData, res, name+slen);