1 /* $Id: recctrl.c,v 1.7 2004-09-27 10:44:50 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
35 struct recTypeClass *next;
39 struct recTypeInstance {
41 struct recTypeInstance *next;
48 struct recTypeInstance *entries;
51 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
52 NMEM nmem, void *module_handle);
54 RecTypeClass recTypeClass_create (Res res, NMEM nmem)
56 struct recTypeClass *rts = 0;
57 const char *module_path = res_get_def(res, "modulePath",
60 extern RecType idzebra_filter_grs_sgml[];
61 recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
62 #ifdef IDZEBRA_STATIC_TEXT
63 extern RecType idzebra_filter_text[];
64 recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
66 #ifdef IDZEBRA_STATIC_GRS_XML
67 extern RecType idzebra_filter_grs_xml[];
68 recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
70 #ifdef IDZEBRA_STATIC_GRS_REGX
71 extern RecType idzebra_filter_grs_regx[];
72 recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
74 #ifdef IDZEBRA_STATIC_GRS_MARC
75 extern RecType idzebra_filter_grs_marc[];
76 recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
78 #ifdef IDZEBRA_STATIC_GRS_PERL
79 extern RecType idzebra_filter_grs_perl[];
80 recTypeClass_add (&rts, idzebra_filter_grs_perl, nmem, 0);
82 #ifdef IDZEBRA_STATIC_GRS_DANBIB
83 extern RecType idzebra_filter_grs_danbib[];
84 recTypeClass_add (&rts, idzebra_filter_grs_danbib, nmem, 0);
89 DIR *dir = opendir(module_path);
90 yaz_log(LOG_LOG, "searching filters in %s", module_path);
95 while ((de = readdir(dir)))
97 size_t dlen = strlen(de->d_name);
98 if ((de->d_type == DT_REG || de->d_type == DT_LNK)
100 !memcmp(de->d_name, "mod-", 4) &&
101 !strcmp(de->d_name + dlen - 3, ".so"))
104 char fname[FILENAME_MAX*2+1];
105 sprintf(fname, "%.*s/%.*s",
106 FILENAME_MAX, module_path,
107 FILENAME_MAX, de->d_name);
108 mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
109 if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
111 yaz_log(LOG_LOG, "Loaded filter module %s", fname);
112 recTypeClass_add(&rts, fl, nmem, mod_p);
116 const char *err = dlerror();
117 yaz_log(LOG_WARN, "dlsym failed %s %s",
118 fname, err ? err : "none");
123 const char *err = dlerror();
124 yaz_log(LOG_WARN, "dlopen failed %s %s",
125 fname, err ? err : "none");
136 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
137 NMEM nmem, void *module_handle)
141 struct recTypeClass *r = (struct recTypeClass *)
142 nmem_malloc (nmem, sizeof(*r));
147 yaz_log(LOG_LOG, "Adding filter %s", (*rt)->name);
148 r->module_handle = module_handle;
149 module_handle = 0; /* so that we only store module_handle once */
156 void recTypeClass_info(RecTypeClass rtc, void *cd,
157 void (*cb)(void *cd, const char *s))
159 for (; rtc; rtc = rtc->next)
160 (*cb)(cd, rtc->recType->name);
163 void recTypeClass_destroy(RecTypeClass rtc)
165 for (; rtc; rtc = rtc->next)
167 if (rtc->module_handle)
168 dlclose(rtc->module_handle);
172 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
174 RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
176 struct recTypeInstance **rti = &rts->entries;
180 for (; rtc; rtc = rtc->next)
182 *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
183 (*rti)->recType = rtc->recType;
184 (*rti)->init_flag = 0;
191 void recTypes_destroy (RecTypes rts)
193 struct recTypeInstance *rti;
195 for (rti = rts->entries; rti; rti = rti->next)
198 (*(rti->recType)->destroy)(rti->clientData);
202 RecType recType_byName (RecTypes rts, Res res, const char *name,
205 struct recTypeInstance *rti;
207 for (rti = rts->entries; rti; rti = rti->next)
209 size_t slen = strlen(rti->recType->name);
210 if (!strncmp (rti->recType->name, name, slen)
211 && (name[slen] == '\0' || name[slen] == '.'))
217 (*(rti->recType)->init)(res, rti->recType);
219 *clientDataP = rti->clientData;
223 if (rti->recType->config)
224 (*(rti->recType)->config)(rti->clientData, res, name+slen);