Cast to unsigned char for ctype is.. compare
[idzebra-moved-to-github.git] / recctrl / recctrl.c
1 /* $Id: recctrl.c,v 1.14 2004-11-29 21:55:28 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #include <string.h>
27 #include <dlfcn.h>
28
29 #include <direntz.h>
30 #include <zebrautl.h>
31 #include <idzebra/recctrl.h>
32
33 struct recTypeClass {
34     RecType recType;
35     struct recTypeClass *next;
36     void *module_handle;
37 };
38
39 struct recTypeInstance {
40     RecType recType;
41     struct recTypeInstance *next;
42     int init_flag;
43     void *clientData;
44 };
45
46 struct recTypes {
47     data1_handle dh;
48     struct recTypeInstance *entries;
49 };
50
51 #ifdef IDZEBRA_STATIC_TEXT
52     extern RecType idzebra_filter_text[];
53 #endif
54 #ifdef IDZEBRA_STATIC_GRS_XML
55 #if HAVE_EXPAT_H
56     extern RecType idzebra_filter_grs_xml[];
57 #endif
58 #endif
59 #ifdef IDZEBRA_STATIC_GRS_REGX
60     extern RecType idzebra_filter_grs_regx[];
61 #endif
62 #ifdef IDZEBRA_STATIC_GRS_MARC
63     extern RecType idzebra_filter_grs_marc[];
64 #endif
65 #ifdef IDZEBRA_STATIC_GRS_DANBIB
66     extern RecType idzebra_filter_grs_danbib[];
67 #endif
68 #ifdef IDZEBRA_STATIC_SAFARI
69     extern RecType idzebra_filter_safari[];
70 #endif
71
72 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
73                               NMEM nmem, void *module_handle);
74
75 RecTypeClass recTypeClass_create (Res res, NMEM nmem)
76 {
77     struct recTypeClass *rts = 0;
78     const char *module_path = res_get_def(res, "modulePath",
79                                           DEFAULT_MODULE_PATH);
80
81     extern RecType idzebra_filter_grs_sgml[];
82     recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
83 #ifdef IDZEBRA_STATIC_TEXT
84     recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
85 #endif
86 #ifdef IDZEBRA_STATIC_GRS_XML
87 #if HAVE_EXPAT_H
88     recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
89 #endif
90 #endif
91 #ifdef IDZEBRA_STATIC_GRS_REGX
92     recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
93 #endif
94 #ifdef IDZEBRA_STATIC_GRS_MARC
95     recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
96 #endif
97 #ifdef IDZEBRA_STATIC_GRS_DANBIB
98     recTypeClass_add (&rts, idzebra_filter_grs_danbib, nmem, 0);
99 #endif
100 #ifdef IDZEBRA_STATIC_SAFARI
101     recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
102 #endif
103
104     if (module_path)
105     {
106         DIR *dir = opendir(module_path);
107         yaz_log(YLOG_LOG, "searching filters in %s", module_path);
108         if (dir)
109         {
110             struct dirent *de;
111
112             while ((de = readdir(dir)))
113             {
114                 size_t dlen = strlen(de->d_name);
115                 if (dlen >= 5 &&
116                     !memcmp(de->d_name, "mod-", 4) &&
117                     !strcmp(de->d_name + dlen - 3, ".so"))
118                 {
119                     void *mod_p, *fl;
120                     char fname[FILENAME_MAX*2+1];
121                     sprintf(fname, "%.*s/%.*s",
122                             FILENAME_MAX, module_path,
123                             FILENAME_MAX, de->d_name);
124                     mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
125                     if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
126                     {
127                         yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
128                         recTypeClass_add(&rts, fl, nmem, mod_p);
129                     }
130                     else if (mod_p)
131                     {
132                         const char *err = dlerror();
133                         yaz_log(YLOG_WARN, "dlsym failed %s %s",
134                                 fname, err ? err : "none");
135                         dlclose(mod_p);
136                     }
137                     else
138                     {
139                         const char *err = dlerror();
140                         yaz_log(YLOG_WARN, "dlopen failed %s %s",
141                                 fname, err ? err : "none");
142                         
143                     }
144                 }
145             }
146             closedir(dir);
147         }
148     }
149     return rts;
150 }
151
152 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
153                               NMEM nmem, void *module_handle)
154 {
155     while (*rt)
156     {
157         struct recTypeClass *r = (struct recTypeClass *)
158             nmem_malloc (nmem, sizeof(*r));
159         
160         r->next = *rts;
161         *rts = r;
162
163         yaz_log(YLOG_LOG, "Adding filter %s", (*rt)->name);
164         r->module_handle = module_handle;
165         module_handle = 0; /* so that we only store module_handle once */
166         r->recType = *rt;
167
168         rt++;
169     }
170 }
171
172 void recTypeClass_info(RecTypeClass rtc, void *cd,
173                        void (*cb)(void *cd, const char *s))
174 {
175     for (; rtc; rtc = rtc->next)
176         (*cb)(cd, rtc->recType->name);
177 }
178
179 void recTypeClass_destroy(RecTypeClass rtc)
180 {
181     for (; rtc; rtc = rtc->next)
182     {
183         if (rtc->module_handle)
184             dlclose(rtc->module_handle);
185     }
186 }
187
188 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
189 {
190     RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
191
192     struct recTypeInstance **rti = &rts->entries;
193     
194     rts->dh = dh;
195
196     for (; rtc; rtc = rtc->next)
197     {
198         *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
199         (*rti)->recType = rtc->recType;
200         (*rti)->init_flag = 0;
201         rti = &(*rti)->next;
202     }
203     *rti = 0;
204     return rts;
205 }
206
207 void recTypes_destroy (RecTypes rts)
208 {
209     struct recTypeInstance *rti;
210
211     for (rti = rts->entries; rti; rti = rti->next)
212     {
213         if (rti->init_flag)
214             (*(rti->recType)->destroy)(rti->clientData);
215     }
216 }
217
218 RecType recType_byName (RecTypes rts, Res res, const char *name,
219                         void **clientDataP)
220 {
221     struct recTypeInstance *rti;
222
223     for (rti = rts->entries; rti; rti = rti->next)
224     {
225         size_t slen = strlen(rti->recType->name);
226         if (!strncmp (rti->recType->name, name, slen)
227             && (name[slen] == '\0' || name[slen] == '.'))
228         {
229             if (!rti->init_flag)
230             {
231                 rti->init_flag = 1;
232                 rti->clientData =
233                     (*(rti->recType)->init)(res, rti->recType);
234             }
235             *clientDataP = rti->clientData;
236             if (name[slen])
237                 slen++;  /* skip . */
238
239             if (rti->recType->config)
240                 (*(rti->recType)->config)(rti->clientData, res, name+slen);
241             return rti->recType;
242         }
243     }
244     return 0;
245 }
246