IDZEBRA 2.2.8
recctrl.c
Go to the documentation of this file.
1/* This file is part of the Zebra server.
2 Copyright (C) Index Data
3
4Zebra is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18*/
19
20
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <stdio.h>
25#include <assert.h>
26#include <string.h>
27#if HAVE_DLFCN_H
28#include <dlfcn.h>
29#endif
30
31#include <direntz.h>
32#include <yaz/snprintf.h>
33#include <idzebra/util.h>
34#include <idzebra/recctrl.h>
35
41
48
53
54static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
55 NMEM nmem, void *module_handle);
56
57
59{
60 struct recTypeClass *rts = 0;
61
62#if IDZEBRA_STATIC_GRS_SGML
63 if (1)
64 {
65 extern RecType idzebra_filter_grs_sgml[];
66 recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
67 }
68#endif
69
70#if IDZEBRA_STATIC_TEXT
71 if (1)
72 {
73 extern RecType idzebra_filter_text[];
74 recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
75 }
76#endif
77
78#if IDZEBRA_STATIC_GRS_XML
79#if HAVE_EXPAT_H
80 if (1)
81 {
82 extern RecType idzebra_filter_grs_xml[];
83 recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
84 }
85#endif
86#endif
87
88#if IDZEBRA_STATIC_GRS_REGX
89 if (1)
90 {
91 extern RecType idzebra_filter_grs_regx[];
92 recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
93 }
94#endif
95
96#if IDZEBRA_STATIC_GRS_MARC
97 if (1)
98 {
99 extern RecType idzebra_filter_grs_marc[];
100 recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
101 }
102#endif
103
104#if IDZEBRA_STATIC_SAFARI
105 if (1)
106 {
107 extern RecType idzebra_filter_safari[];
108 recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
109 }
110#endif
111
112#if IDZEBRA_STATIC_ALVIS
113 if (1)
114 {
115 extern RecType idzebra_filter_alvis[];
116 recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
117 }
118#endif
119
120#if IDZEBRA_STATIC_DOM
121 if (1)
122 {
123 extern RecType idzebra_filter_dom[];
124 recTypeClass_add (&rts, idzebra_filter_dom, nmem, 0);
125 }
126#endif
127
128 return rts;
129}
130
131static void load_from_dir(RecTypeClass *rts, NMEM nmem, const char *dirname)
132{
133#if HAVE_DLFCN_H
134 DIR *dir = opendir(dirname);
135 if (dir)
136 {
137 struct dirent *de;
138
139 while ((de = readdir(dir)))
140 {
141 size_t dlen = strlen(de->d_name);
142 if (dlen >= 5 &&
143 !memcmp(de->d_name, "mod-", 4) &&
144 !strcmp(de->d_name + dlen - 3, ".so"))
145 {
146 void *mod_p, *fl;
147 char fname[FILENAME_MAX*2+1];
148 yaz_snprintf(fname, sizeof(fname), "%s/%s",
149 dirname, de->d_name);
150 mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
151 if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
152 {
153 yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
154 recTypeClass_add(rts, fl, nmem, mod_p);
155 }
156 else if (mod_p)
157 {
158 const char *err = dlerror();
159 yaz_log(YLOG_WARN, "dlsym failed %s %s",
160 fname, err ? err : "none");
161 dlclose(mod_p);
162 }
163 else
164 {
165 const char *err = dlerror();
166 yaz_log(YLOG_WARN, "dlopen failed %s %s",
167 fname, err ? err : "none");
168
169 }
170 }
171 }
172 closedir(dir);
173 }
174#endif
175}
176
178 const char *module_path)
179{
180 while (module_path)
181 {
182 const char *comp_ptr;
183 char comp[FILENAME_MAX+1];
184 size_t len;
185
186 len = yaz_filepath_comp(&module_path, &comp_ptr);
187 if (!len || len >= FILENAME_MAX)
188 break;
189
190 memcpy(comp, comp_ptr, len);
191 comp[len] = '\0';
192
193 load_from_dir(rts, nmem, comp);
194 }
195}
196
197static void recTypeClass_add(struct recTypeClass **rts, RecType *rt,
198 NMEM nmem, void *module_handle)
199{
200 while (*rt)
201 {
202 struct recTypeClass *r = (struct recTypeClass *)
203 nmem_malloc (nmem, sizeof(*r));
204
205 r->next = *rts;
206 *rts = r;
207
209 module_handle = 0; /* so that we only store module_handle once */
210 r->recType = *rt;
211
212 rt++;
213 }
214}
215
217 void (*cb)(void *cd, const char *s))
218{
219 for (; rtc; rtc = rtc->next)
220 (*cb)(cd, rtc->recType->name);
221}
222
224{
225 for (; rtc; rtc = rtc->next)
226 {
227#if HAVE_DLFCN_H
228 if (rtc->module_handle)
229 dlclose(rtc->module_handle);
230#endif
231 }
232}
233
235{
236 RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
237
238 struct recTypeInstance **rti = &rts->entries;
239
240 rts->dh = dh;
241
242 for (; rtc; rtc = rtc->next)
243 {
244 *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
245 (*rti)->recType = rtc->recType;
246 (*rti)->init_flag = 0;
247 rti = &(*rti)->next;
248 }
249 *rti = 0;
250 return rts;
251}
252
254{
255 struct recTypeInstance *rti;
256
257 for (rti = rts->entries; rti; rti = rti->next)
258 {
259 if (rti->init_flag)
260 (*(rti->recType)->destroy)(rti->clientData);
261 }
262}
263
264RecType recType_byName (RecTypes rts, Res res, const char *name,
265 void **clientDataP)
266{
267 struct recTypeInstance *rti;
268
269 for (rti = rts->entries; rti; rti = rti->next)
270 {
271 size_t slen = strlen(rti->recType->name);
272 if (!strncmp (rti->recType->name, name, slen)
273 && (name[slen] == '\0' || name[slen] == '.'))
274 {
275 if (!rti->init_flag)
276 {
277 rti->init_flag = 1;
278 rti->clientData =
279 (*(rti->recType)->init)(res, rti->recType);
280 }
281 *clientDataP = rti->clientData;
282 if (name[slen])
283 slen++; /* skip . */
284
285 if (rti->recType->config)
286 {
287 if ((*(rti->recType)->config)
288 (rti->clientData, res, name+slen) != ZEBRA_OK)
289 return 0;
290 }
291 return rti->recType;
292 }
293 }
294 return 0;
295}
296
297/*
298 * Local variables:
299 * c-basic-offset: 4
300 * c-file-style: "Stroustrup"
301 * indent-tabs-mode: nil
302 * End:
303 * vim: shiftwidth=4 tabstop=8 expandtab
304 */
305
NMEM data1_nmem_get(data1_handle dh)
Definition d1_handle.c:66
#define FILENAME_MAX
Definition mfile.h:42
static void destroy(struct zebra_register *reg, void *class_handle)
Definition rank1.c:93
void recTypeClass_load_modules(RecTypeClass *rts, NMEM nmem, const char *module_path)
Definition recctrl.c:177
void recTypeClass_destroy(RecTypeClass rtc)
Definition recctrl.c:223
static void load_from_dir(RecTypeClass *rts, NMEM nmem, const char *dirname)
Definition recctrl.c:131
void recTypes_destroy(RecTypes rts)
Definition recctrl.c:253
RecTypeClass recTypeClass_create(Res res, NMEM nmem)
Definition recctrl.c:58
void recTypeClass_info(RecTypeClass rtc, void *cd, void(*cb)(void *cd, const char *s))
Definition recctrl.c:216
static void recTypeClass_add(struct recTypeClass **rts, RecType *rt, NMEM nmem, void *module_handle)
Definition recctrl.c:197
RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
Definition recctrl.c:234
RecType recType_byName(RecTypes rts, Res res, const char *name, void **clientDataP)
Definition recctrl.c:264
struct recTypes * RecTypes
Definition recctrl.h:170
RecType recType
Definition recctrl.c:37
void * module_handle
Definition recctrl.c:39
struct recTypeClass * next
Definition recctrl.c:38
void * clientData
Definition recctrl.c:46
RecType recType
Definition recctrl.c:43
struct recTypeInstance * next
Definition recctrl.c:44
char * name
Definition recctrl.h:153
ZEBRA_RES(* config)(void *clientData, Res res, const char *args)
Definition recctrl.h:155
struct recTypeInstance * entries
Definition recctrl.c:51
data1_handle dh
Definition recctrl.c:50
#define ZEBRA_OK
Definition util.h:82