IDZEBRA  2.1.2
recctrl.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8 
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 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 <idzebra/util.h>
33 #include <idzebra/recctrl.h>
34 
35 struct recTypeClass {
37  struct recTypeClass *next;
39 };
40 
44  int init_flag;
45  void *clientData;
46 };
47 
48 struct recTypes {
51 };
52 
53 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
54  NMEM nmem, void *module_handle);
55 
56 
58 {
59  struct recTypeClass *rts = 0;
60 
61 #if IDZEBRA_STATIC_GRS_SGML
62  if (1)
63  {
64  extern RecType idzebra_filter_grs_sgml[];
65  recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
66  }
67 #endif
68 
69 #if IDZEBRA_STATIC_TEXT
70  if (1)
71  {
72  extern RecType idzebra_filter_text[];
73  recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
74  }
75 #endif
76 
77 #if IDZEBRA_STATIC_GRS_XML
78 #if HAVE_EXPAT_H
79  if (1)
80  {
81  extern RecType idzebra_filter_grs_xml[];
82  recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
83  }
84 #endif
85 #endif
86 
87 #if IDZEBRA_STATIC_GRS_REGX
88  if (1)
89  {
90  extern RecType idzebra_filter_grs_regx[];
91  recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
92  }
93 #endif
94 
95 #if IDZEBRA_STATIC_GRS_MARC
96  if (1)
97  {
98  extern RecType idzebra_filter_grs_marc[];
99  recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
100  }
101 #endif
102 
103 #if IDZEBRA_STATIC_SAFARI
104  if (1)
105  {
106  extern RecType idzebra_filter_safari[];
107  recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
108  }
109 #endif
110 
111 #if IDZEBRA_STATIC_ALVIS
112  if (1)
113  {
114  extern RecType idzebra_filter_alvis[];
115  recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
116  }
117 #endif
118 
119 #if IDZEBRA_STATIC_DOM
120  if (1)
121  {
122  extern RecType idzebra_filter_dom[];
123  recTypeClass_add (&rts, idzebra_filter_dom, nmem, 0);
124  }
125 #endif
126 
127  return rts;
128 }
129 
130 static void load_from_dir(RecTypeClass *rts, NMEM nmem, const char *dirname)
131 {
132 #if HAVE_DLFCN_H
133  DIR *dir = opendir(dirname);
134  if (dir)
135  {
136  struct dirent *de;
137 
138  while ((de = readdir(dir)))
139  {
140  size_t dlen = strlen(de->d_name);
141  if (dlen >= 5 &&
142  !memcmp(de->d_name, "mod-", 4) &&
143  !strcmp(de->d_name + dlen - 3, ".so"))
144  {
145  void *mod_p, *fl;
146  char fname[FILENAME_MAX*2+1];
147  sprintf(fname, "%.*s/%.*s",
148  FILENAME_MAX, dirname,
149  FILENAME_MAX, 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 
197 static 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 
216 void recTypeClass_info(RecTypeClass rtc, void *cd,
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 
264 RecType 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 
static void destroy(struct zebra_register *reg, void *class_handle)
Definition: rank1.c:93
static void recTypeClass_add(struct recTypeClass **rts, RecType *rt, NMEM nmem, void *module_handle)
Definition: recctrl.c:197
char * name
Definition: recctrl.h:153
#define ZEBRA_OK
Definition: util.h:82
struct recTypes * RecTypes
Definition: recctrl.h:170
void * module_handle
Definition: recctrl.c:38
void * clientData
Definition: recctrl.c:45
RecType recType_byName(RecTypes rts, Res res, const char *name, void **clientDataP)
Definition: recctrl.c:264
RecTypeClass recTypeClass_create(Res res, NMEM nmem)
Definition: recctrl.c:57
void recTypeClass_destroy(RecTypeClass rtc)
Definition: recctrl.c:223
Definition: res.c:46
#define FILENAME_MAX
Definition: mfile.h:42
data1_handle dh
Definition: recctrl.c:49
void recTypes_destroy(RecTypes rts)
Definition: recctrl.c:253
struct recTypeInstance * entries
Definition: recctrl.c:50
RecType recType
Definition: recctrl.c:42
struct recTypeInstance * next
Definition: recctrl.c:43
void recTypeClass_info(RecTypeClass rtc, void *cd, void(*cb)(void *cd, const char *s))
Definition: recctrl.c:216
void recTypeClass_load_modules(RecTypeClass *rts, NMEM nmem, const char *module_path)
Definition: recctrl.c:177
static void load_from_dir(RecTypeClass *rts, NMEM nmem, const char *dirname)
Definition: recctrl.c:130
NMEM data1_nmem_get(data1_handle dh)
Definition: d1_handle.c:66
RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
Definition: recctrl.c:234
struct recTypeClass * next
Definition: recctrl.c:37
ZEBRA_RES(* config)(void *clientData, Res res, const char *args)
Definition: recctrl.h:155
RecType recType
Definition: recctrl.c:36