1 /* $Id: recctrl.c,v 1.3 2006-10-11 08:55:52 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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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_SAFARI
106 extern RecType idzebra_filter_safari[];
107 recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
111 #ifdef IDZEBRA_STATIC_ALVIS
115 extern RecType idzebra_filter_alvis[];
116 recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
124 static void load_from_dir(RecTypeClass *rts, NMEM nmem, const char *dirname)
127 DIR *dir = opendir(dirname);
132 while ((de = readdir(dir)))
134 size_t dlen = strlen(de->d_name);
136 !memcmp(de->d_name, "mod-", 4) &&
137 !strcmp(de->d_name + dlen - 3, ".so"))
140 char fname[FILENAME_MAX*2+1];
141 sprintf(fname, "%.*s/%.*s",
142 FILENAME_MAX, dirname,
143 FILENAME_MAX, de->d_name);
144 mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
145 if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
147 yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
148 recTypeClass_add(rts, fl, nmem, mod_p);
152 const char *err = dlerror();
153 yaz_log(YLOG_WARN, "dlsym failed %s %s",
154 fname, err ? err : "none");
159 const char *err = dlerror();
160 yaz_log(YLOG_WARN, "dlopen failed %s %s",
161 fname, err ? err : "none");
171 void recTypeClass_load_modules(RecTypeClass *rts, NMEM nmem,
172 const char *module_path)
176 const char *comp_ptr;
177 char comp[FILENAME_MAX+1];
180 len = yaz_filepath_comp(&module_path, &comp_ptr);
181 if (!len || len >= FILENAME_MAX)
184 memcpy(comp, comp_ptr, len);
187 load_from_dir(rts, nmem, comp);
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 r->module_handle = module_handle;
203 module_handle = 0; /* so that we only store module_handle once */
210 void recTypeClass_info(RecTypeClass rtc, void *cd,
211 void (*cb)(void *cd, const char *s))
213 for (; rtc; rtc = rtc->next)
214 (*cb)(cd, rtc->recType->name);
217 void recTypeClass_destroy(RecTypeClass rtc)
219 for (; rtc; rtc = rtc->next)
222 if (rtc->module_handle)
223 dlclose(rtc->module_handle);
228 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
230 RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
232 struct recTypeInstance **rti = &rts->entries;
236 for (; rtc; rtc = rtc->next)
238 *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
239 (*rti)->recType = rtc->recType;
240 (*rti)->init_flag = 0;
247 void recTypes_destroy (RecTypes rts)
249 struct recTypeInstance *rti;
251 for (rti = rts->entries; rti; rti = rti->next)
254 (*(rti->recType)->destroy)(rti->clientData);
258 RecType recType_byName (RecTypes rts, Res res, const char *name,
261 struct recTypeInstance *rti;
263 for (rti = rts->entries; rti; rti = rti->next)
265 size_t slen = strlen(rti->recType->name);
266 if (!strncmp (rti->recType->name, name, slen)
267 && (name[slen] == '\0' || name[slen] == '.'))
273 (*(rti->recType)->init)(res, rti->recType);
275 *clientDataP = rti->clientData;
279 if (rti->recType->config)
281 if ((*(rti->recType)->config)
282 (rti->clientData, res, name+slen) != ZEBRA_OK)
294 * indent-tabs-mode: nil
296 * vim: shiftwidth=4 tabstop=8 expandtab