1 /* $Id: perlread.c,v 1.9 2004-09-06 09:31:34 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
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
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
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
33 #include <yaz/tpath.h>
39 #define GRS_PERL_MODULE_NAME_MAXLEN 255
41 /* Context information for the filter */
43 PerlInterpreter *perli;
44 PerlInterpreter *origi;
46 char filterClass[GRS_PERL_MODULE_NAME_MAXLEN];
49 int (*readf)(void *, char *, size_t);
50 off_t (*seekf)(void *, off_t);
51 off_t (*tellf)(void *);
52 void (*endf)(void *, off_t);
60 /* Constructor call for the filter object */
61 void Filter_create (struct perl_context *context)
67 XPUSHs(sv_2mortal(newSVpv(context->filterClass,
68 strlen(context->filterClass)))) ;
70 sv_setref_pv(msv, "_p_perl_context", (void*)context);
73 call_method("new", G_EVAL);
76 context->filterRef = POPs;
81 Execute the process call on the filter. This is a bit dirty.
82 The perl code is going to get dh and nmem from the context trough callbacks,
83 then call readf, to get the stream, and then set the res (d1 node)
84 in the context. However, it's safer, to let swig do as much of wrapping
87 int Filter_process (struct perl_context *context)
99 XPUSHs(context->filterRef);
101 call_method("_process", 0);
113 This one is called to transfer the results of a readf. It's going to create
114 a temporary variable there...
116 So the call stack is something like:
119 ->Filter_process(context) [C]
120 -> _process($context) [Perl]
121 -> grs_perl_get_dh($context) [Perl]
122 -> grs_perl_get_dh(context) [C]
123 -> grs_perl_get_mem($context) [Perl]
124 -> grs_perl_get_mem(context) [C]
127 -> grs_perl_readf($context,$len) [Perl]
128 -> grs_perl_readf(context, len) [C]
129 ->(*context->readf)(context->fh, buf, len) [C]
130 -> Filter_store_buff(context, buf, r) [C]
131 -> _store_buff($buff) [Perl]
132 [... returns buff and length ...]
134 [... returns d1 node ...]
135 -> grs_perl_set_res($context, $node) [Perl]
136 -> grs_perl_set_res(context, node) [C]
138 [... The result is in context->res ...]
140 Dirty, isn't it? It may become nicer, if I'll have some more time to work on
141 it. However, these changes are not going to hurt the filter api, as
142 Filter.pm, which is inherited into all specific filter implementations
143 can hide this whole compexity behind.
146 void Filter_store_buff (struct perl_context *context, char *buff, size_t len)
154 XPUSHs(context->filterRef);
155 XPUSHs(sv_2mortal(newSVpv(buff, len)));
157 call_method("_store_buff", 0);
165 /* The "file" manipulation function wrappers */
166 int grs_perl_readf(struct perl_context *context, size_t len)
169 char *buf = (char *) xmalloc (len+1);
170 r = (*context->readf)(context->fh, buf, len);
171 if (r > 0) Filter_store_buff (context, buf, r);
176 int grs_perl_readline(struct perl_context *context)
179 char *buf = (char *) xmalloc (4096);
182 while ((r = (*context->readf)(context->fh,p,1)) && (p-buf < 4095)) {
184 if (*(p-1) == 10) break;
189 if (p != buf) Filter_store_buff (context, buf, p - buf);
194 char grs_perl_getc(struct perl_context *context)
198 if ((r = (*context->readf)(context->fh,p,1))) {
205 off_t grs_perl_seekf(struct perl_context *context, off_t offset)
207 return ((*context->seekf)(context->fh, offset));
210 off_t grs_perl_tellf(struct perl_context *context)
212 return ((*context->tellf)(context->fh));
215 void grs_perl_endf(struct perl_context *context, off_t offset)
217 (*context->endf)(context->fh, offset);
220 /* Get pointers from the context. Easyer to wrap this by SWIG */
221 data1_handle *grs_perl_get_dh(struct perl_context *context)
223 return(&context->dh);
226 NMEM *grs_perl_get_mem(struct perl_context *context)
228 return(&context->mem);
231 /* Set the result in the context */
232 void grs_perl_set_res(struct perl_context *context, data1_node *n)
237 /* The filter handlers (init, destroy, read) */
238 static void *grs_init_perl(void)
240 struct perl_context *context =
241 (struct perl_context *) xmalloc (sizeof(*context));
243 /* If there is an interpreter (context) running, - we are calling
244 indexing and retrieval from the perl API - we don't create a new one. */
245 context->origi = PL_curinterp;
247 if (!context->origi) {
248 context->perli = perl_alloc();
249 PERL_SET_CONTEXT(context->perli);
250 logf (LOG_LOG, "Initializing new perl interpreter context (%p)",context->perli);
252 logf (LOG_LOG, "Using existing perl interpreter context (%p)",context->origi);
254 context->perli_ready = 0;
255 strcpy(context->filterClass, "");
259 void grs_destroy_perl(void *clientData)
261 struct perl_context *context = (struct perl_context *) clientData;
263 logf (LOG_LOG, "Destroying perl interpreter context");
264 if (context->perli_ready) {
269 if (context->origi == NULL) perl_destruct(context->perli);
271 if (context->origi == NULL) perl_free(context->perli);
275 static data1_node *grs_read_perl (struct grs_read_info *p)
277 struct perl_context *context = (struct perl_context *) p->clientData;
278 char *filterClass = p->type;
280 /* The "file" manipulation function wrappers */
281 context->readf = p->readf;
282 context->seekf = p->seekf;
283 context->tellf = p->tellf;
284 context->endf = p->endf;
286 /* The "file", data1 and NMEM handles */
289 context->mem = p->mem;
291 /* If the class was not interpreted before... */
292 /* This is not too efficient, when indexing with many different filters... */
293 if (strcmp(context->filterClass,filterClass)) {
295 char modarg[GRS_PERL_MODULE_NAME_MAXLEN + 2];
296 char initarg[GRS_PERL_MODULE_NAME_MAXLEN + 2];
297 char *arglist[6] = { "", "-I", "", "-M", "-e", "" };
299 if (context->perli_ready) {
304 if (context->origi == NULL) {
305 perl_destruct(context->perli);
308 if (context->origi == NULL) {
309 perl_construct(context->perli);
317 context->perli_ready = 1;
319 /* parse, and run the init call */
320 if (context->origi == NULL) {
321 logf (LOG_LOG, "Interpreting filter class:%s", filterClass);
323 arglist[2] = (char *) data1_get_tabpath(p->dh);
324 sprintf(modarg,"-M%s",filterClass);
325 arglist[3] = (char *) &modarg;
326 sprintf(initarg,"%s->init;",filterClass);
327 arglist[5] = (char *) &initarg;
329 perl_parse(context->perli, PERL_XS_INIT, 6, arglist, NULL);
330 perl_run(context->perli);
333 strcpy(context->filterClass, filterClass);
335 /* create the filter object as a filterClass blessed reference */
336 Filter_create(context);
339 /* Wow... if calling with individual update_record calls from perl,
340 the filter object reference may go out of scope... */
341 if (!sv_isa(context->filterRef, context->filterClass)) {
342 Filter_create(context);
343 logf (LOG_DEBUG,"Filter recreated");
346 if (!SvTRUE(context->filterRef))
348 logf (LOG_WARN,"Failed to initialize perl filter %s",context->filterClass);
352 /* call the process method */
353 Filter_process(context);
355 /* return the created data1 node */
356 return (context->res);
359 static struct recTypeGrs perl_type = {
366 RecTypeGrs recTypeGrs_perl = &perl_type;