0a9fc80a98293fb52edda1a32914f7cc580a2aa7
[idzebra-moved-to-github.git] / recctrl / recctrl.c
1 /* $Id: recctrl.c,v 1.9 2004-09-28 10:15:03 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 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
52                               NMEM nmem, void *module_handle);
53
54 RecTypeClass recTypeClass_create (Res res, NMEM nmem)
55 {
56     struct recTypeClass *rts = 0;
57     const char *module_path = res_get_def(res, "modulePath",
58                                           DEFAULT_MODULE_PATH);
59
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);
65 #endif
66 #ifdef IDZEBRA_STATIC_GRS_XML
67 #if HAVE_EXPAT_H
68     extern RecType idzebra_filter_grs_xml[];
69     recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
70 #endif
71 #endif
72 #ifdef IDZEBRA_STATIC_GRS_REGX
73     extern RecType idzebra_filter_grs_regx[];
74     recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
75 #endif
76 #ifdef IDZEBRA_STATIC_GRS_MARC
77     extern RecType idzebra_filter_grs_marc[];
78     recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
79 #endif
80 #ifdef IDZEBRA_STATIC_GRS_PERL
81     extern RecType idzebra_filter_grs_perl[];
82     recTypeClass_add (&rts, idzebra_filter_grs_perl, nmem, 0);
83 #endif
84 #ifdef IDZEBRA_STATIC_GRS_DANBIB
85     extern RecType idzebra_filter_grs_danbib[];
86     recTypeClass_add (&rts, idzebra_filter_grs_danbib, nmem, 0);
87 #endif
88
89     if (module_path)
90     {
91         DIR *dir = opendir(module_path);
92         yaz_log(LOG_LOG, "searching filters in %s", module_path);
93         if (dir)
94         {
95             struct dirent *de;
96
97             while ((de = readdir(dir)))
98             {
99                 size_t dlen = strlen(de->d_name);
100                 if ((de->d_type == DT_REG || de->d_type == DT_LNK) 
101                     && dlen >= 5 &&
102                     !memcmp(de->d_name, "mod-", 4) &&
103                     !strcmp(de->d_name + dlen - 3, ".so"))
104                 {
105                     void *mod_p, *fl;
106                     char fname[FILENAME_MAX*2+1];
107                     sprintf(fname, "%.*s/%.*s",
108                             FILENAME_MAX, module_path,
109                             FILENAME_MAX, de->d_name);
110                     mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
111                     if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
112                     {
113                         yaz_log(LOG_LOG, "Loaded filter module %s", fname);
114                         recTypeClass_add(&rts, fl, nmem, mod_p);
115                     }
116                     else if (mod_p)
117                     {
118                         const char *err = dlerror();
119                         yaz_log(LOG_WARN, "dlsym failed %s %s",
120                                 fname, err ? err : "none");
121                         dlclose(mod_p);
122                     }
123                     else
124                     {
125                         const char *err = dlerror();
126                         yaz_log(LOG_WARN, "dlopen failed %s %s",
127                                 fname, err ? err : "none");
128                         
129                     }
130                 }
131             }
132             closedir(dir);
133         }
134     }
135     return rts;
136 }
137
138 static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
139                               NMEM nmem, void *module_handle)
140 {
141     while (*rt)
142     {
143         struct recTypeClass *r = (struct recTypeClass *)
144             nmem_malloc (nmem, sizeof(*r));
145         
146         r->next = *rts;
147         *rts = r;
148
149         yaz_log(LOG_LOG, "Adding filter %s", (*rt)->name);
150         r->module_handle = module_handle;
151         module_handle = 0; /* so that we only store module_handle once */
152         r->recType = *rt;
153
154         rt++;
155     }
156 }
157
158 void recTypeClass_info(RecTypeClass rtc, void *cd,
159                        void (*cb)(void *cd, const char *s))
160 {
161     for (; rtc; rtc = rtc->next)
162         (*cb)(cd, rtc->recType->name);
163 }
164
165 void recTypeClass_destroy(RecTypeClass rtc)
166 {
167     for (; rtc; rtc = rtc->next)
168     {
169         if (rtc->module_handle)
170             dlclose(rtc->module_handle);
171     }
172 }
173
174 RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
175 {
176     RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
177
178     struct recTypeInstance **rti = &rts->entries;
179     
180     rts->dh = dh;
181
182     for (; rtc; rtc = rtc->next)
183     {
184         *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
185         (*rti)->recType = rtc->recType;
186         (*rti)->init_flag = 0;
187         rti = &(*rti)->next;
188     }
189     *rti = 0;
190     return rts;
191 }
192
193 void recTypes_destroy (RecTypes rts)
194 {
195     struct recTypeInstance *rti;
196
197     for (rti = rts->entries; rti; rti = rti->next)
198     {
199         if (rti->init_flag)
200             (*(rti->recType)->destroy)(rti->clientData);
201     }
202 }
203
204 RecType recType_byName (RecTypes rts, Res res, const char *name,
205                         void **clientDataP)
206 {
207     struct recTypeInstance *rti;
208
209     for (rti = rts->entries; rti; rti = rti->next)
210     {
211         size_t slen = strlen(rti->recType->name);
212         if (!strncmp (rti->recType->name, name, slen)
213             && (name[slen] == '\0' || name[slen] == '.'))
214         {
215             if (!rti->init_flag)
216             {
217                 rti->init_flag = 1;
218                 rti->clientData =
219                     (*(rti->recType)->init)(res, rti->recType);
220             }
221             *clientDataP = rti->clientData;
222             if (name[slen])
223                 slen++;  /* skip . */
224
225             if (rti->recType->config)
226                 (*(rti->recType)->config)(rti->clientData, res, name+slen);
227             return rti->recType;
228         }
229     }
230     return 0;
231 }
232