File: recctrl.c

package info (click to toggle)
idzebra 2.2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,572 kB
  • sloc: ansic: 54,389; xml: 27,058; sh: 5,892; makefile: 1,102; perl: 210; tcl: 64
file content (305 lines) | stat: -rw-r--r-- 7,466 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* This file is part of the Zebra server.
   Copyright (C) Index Data

Zebra is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <assert.h>
#include <string.h>
#if HAVE_DLFCN_H
#include <dlfcn.h>
#endif

#include <direntz.h>
#include <yaz/snprintf.h>
#include <idzebra/util.h>
#include <idzebra/recctrl.h>

struct recTypeClass {
    RecType recType;
    struct recTypeClass *next;
    void *module_handle;
};

struct recTypeInstance {
    RecType recType;
    struct recTypeInstance *next;
    int init_flag;
    void *clientData;
};

struct recTypes {
    data1_handle dh;
    struct recTypeInstance *entries;
};

static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
                              NMEM nmem, void *module_handle);


RecTypeClass recTypeClass_create (Res res, NMEM nmem)
{
    struct recTypeClass *rts = 0;

#if IDZEBRA_STATIC_GRS_SGML
    if (1)
    {
        extern RecType idzebra_filter_grs_sgml[];
        recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
    }
#endif

#if IDZEBRA_STATIC_TEXT
    if (1)
    {
        extern RecType idzebra_filter_text[];
        recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
    }
#endif

#if IDZEBRA_STATIC_GRS_XML
#if HAVE_EXPAT_H
    if (1)
    {
        extern RecType idzebra_filter_grs_xml[];
        recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
    }
#endif
#endif

#if IDZEBRA_STATIC_GRS_REGX
    if (1)
    {
        extern RecType idzebra_filter_grs_regx[];
        recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
    }
#endif

#if IDZEBRA_STATIC_GRS_MARC
    if (1)
    {
        extern RecType idzebra_filter_grs_marc[];
        recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
    }
#endif

#if IDZEBRA_STATIC_SAFARI
    if (1)
    {
        extern RecType idzebra_filter_safari[];
        recTypeClass_add (&rts, idzebra_filter_safari, nmem, 0);
    }
#endif

#if IDZEBRA_STATIC_ALVIS
    if (1)
    {
        extern RecType idzebra_filter_alvis[];
        recTypeClass_add (&rts, idzebra_filter_alvis, nmem, 0);
    }
#endif

#if IDZEBRA_STATIC_DOM
    if (1)
    {
        extern RecType idzebra_filter_dom[];
        recTypeClass_add (&rts, idzebra_filter_dom, nmem, 0);
    }
#endif

    return rts;
}

static void load_from_dir(RecTypeClass *rts, NMEM nmem, const char *dirname)
{
#if HAVE_DLFCN_H
    DIR *dir = opendir(dirname);
    if (dir)
    {
        struct dirent *de;

        while ((de = readdir(dir)))
        {
            size_t dlen = strlen(de->d_name);
            if (dlen >= 5 &&
                !memcmp(de->d_name, "mod-", 4) &&
                !strcmp(de->d_name + dlen - 3, ".so"))
            {
                void *mod_p, *fl;
                char fname[FILENAME_MAX*2+1];
                yaz_snprintf(fname, sizeof(fname), "%s/%s",
                        dirname, de->d_name);
                mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
                if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
                {
                    yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
                    recTypeClass_add(rts, fl, nmem, mod_p);
                }
                else if (mod_p)
                {
                    const char *err = dlerror();
                    yaz_log(YLOG_WARN, "dlsym failed %s %s",
                            fname, err ? err : "none");
                    dlclose(mod_p);
                }
                else
                {
                    const char *err = dlerror();
                    yaz_log(YLOG_WARN, "dlopen failed %s %s",
                            fname, err ? err : "none");

                }
            }
        }
        closedir(dir);
    }
#endif
}

void recTypeClass_load_modules(RecTypeClass *rts, NMEM nmem,
                               const char *module_path)
{
    while (module_path)
    {
        const char *comp_ptr;
        char comp[FILENAME_MAX+1];
        size_t len;

        len = yaz_filepath_comp(&module_path, &comp_ptr);
        if (!len || len >= FILENAME_MAX)
            break;

        memcpy(comp, comp_ptr, len);
        comp[len] = '\0';

        load_from_dir(rts, nmem, comp);
    }
}

static void recTypeClass_add(struct recTypeClass **rts, RecType *rt,
                             NMEM nmem, void *module_handle)
{
    while (*rt)
    {
        struct recTypeClass *r = (struct recTypeClass *)
            nmem_malloc (nmem, sizeof(*r));

        r->next = *rts;
        *rts = r;

        r->module_handle = module_handle;
        module_handle = 0; /* so that we only store module_handle once */
        r->recType = *rt;

        rt++;
    }
}

void recTypeClass_info(RecTypeClass rtc, void *cd,
                       void (*cb)(void *cd, const char *s))
{
    for (; rtc; rtc = rtc->next)
        (*cb)(cd, rtc->recType->name);
}

void recTypeClass_destroy(RecTypeClass rtc)
{
    for (; rtc; rtc = rtc->next)
    {
#if HAVE_DLFCN_H
        if (rtc->module_handle)
            dlclose(rtc->module_handle);
#endif
    }
}

RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
{
    RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));

    struct recTypeInstance **rti = &rts->entries;

    rts->dh = dh;

    for (; rtc; rtc = rtc->next)
    {
        *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
        (*rti)->recType = rtc->recType;
        (*rti)->init_flag = 0;
        rti = &(*rti)->next;
    }
    *rti = 0;
    return rts;
}

void recTypes_destroy (RecTypes rts)
{
    struct recTypeInstance *rti;

    for (rti = rts->entries; rti; rti = rti->next)
    {
        if (rti->init_flag)
            (*(rti->recType)->destroy)(rti->clientData);
    }
}

RecType recType_byName (RecTypes rts, Res res, const char *name,
                        void **clientDataP)
{
    struct recTypeInstance *rti;

    for (rti = rts->entries; rti; rti = rti->next)
    {
        size_t slen = strlen(rti->recType->name);
        if (!strncmp (rti->recType->name, name, slen)
            && (name[slen] == '\0' || name[slen] == '.'))
        {
            if (!rti->init_flag)
            {
                rti->init_flag = 1;
                rti->clientData =
                    (*(rti->recType)->init)(res, rti->recType);
            }
            *clientDataP = rti->clientData;
            if (name[slen])
                slen++;  /* skip . */

            if (rti->recType->config)
            {
                if ((*(rti->recType)->config)
                    (rti->clientData, res, name+slen) != ZEBRA_OK)
                    return 0;
            }
            return rti->recType;
        }
    }
    return 0;
}

/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */