Avoid mixed stmt/var declare
[simpleserver-moved-to-github.git] / SimpleServer.xs
1 /*
2  * $Id: SimpleServer.xs,v 1.36 2006-01-30 21:29:41 adam Exp $ 
3  * ----------------------------------------------------------------------
4  * 
5  * Copyright (c) 2000-2004, Index Data.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation, in whole or in part, for any purpose, is hereby granted,
9  * provided that:
10  *
11  * 1. This copyright and permission notice appear in all copies of the
12  * software and its documentation. Notices of copyright or attribution
13  * which appear at the beginning of any file must remain unchanged.
14  *
15  * 2. The name of Index Data or the individual authors may not be used to
16  * endorse or promote products derived from this software without specific
17  * prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
21  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
22  * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
23  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
24  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
25  * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
26  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27  * OF THIS SOFTWARE.
28  */
29
30 #include "EXTERN.h"
31 #include "perl.h"
32 #include "proto.h"
33 #include "embed.h"
34 #include "XSUB.h"
35 #include <yaz/backend.h>
36 #include <yaz/log.h>
37 #include <yaz/wrbuf.h>
38 #include <stdio.h>
39 #ifdef WIN32
40 #else
41 #include <unistd.h>
42 #endif
43 #include <stdlib.h>
44 #include <ctype.h>
45 #define GRS_MAX_FIELDS 500 
46 #ifdef ASN_COMPILED
47 #include <yaz/ill.h>
48 #endif
49 #ifndef sv_undef                /* To fix the problem with Perl 5.6.0 */
50 #define sv_undef PL_sv_undef
51 #endif
52
53 NMEM_MUTEX simpleserver_mutex;
54
55 typedef struct {
56         SV *handle;
57
58         SV *init_ref;
59         SV *close_ref;
60         SV *sort_ref;
61         SV *search_ref;
62         SV *fetch_ref;
63         SV *present_ref;
64         SV *esrequest_ref;
65         SV *delete_ref;
66         SV *scan_ref;
67         NMEM nmem;
68         int stop_flag;  /* is used to stop server prematurely .. */
69 } Zfront_handle;
70
71 #define ENABLE_STOP_SERVER 0
72
73 SV *init_ref = NULL;
74 SV *close_ref = NULL;
75 SV *sort_ref = NULL;
76 SV *search_ref = NULL;
77 SV *fetch_ref = NULL;
78 SV *present_ref = NULL;
79 SV *esrequest_ref = NULL;
80 SV *delete_ref = NULL;
81 SV *scan_ref = NULL;
82 PerlInterpreter *root_perl_context;
83 int MAX_OID = 15;
84
85 #define GRS_BUF_SIZE 512
86
87 CV * simpleserver_sv2cv(SV *handler) {
88     STRLEN len;
89     char *buf;
90    
91     if (SvPOK(handler)) {
92         CV *ret;
93         buf = SvPV( handler, len);
94         if ( !( ret = perl_get_cv(buf, FALSE ) ) ) {
95             fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf );
96             exit(1);
97         }
98         
99         return ret;
100     } else {
101         return (CV *) handler;
102     }
103 }
104
105 /* debugging routine to check for destruction of Perl interpreters */
106 #ifdef USE_ITHREADS
107 void tst_clones(void)
108 {
109     int i; 
110     PerlInterpreter *parent = PERL_GET_CONTEXT;
111     for (i = 0; i<5000; i++)
112     {
113         PerlInterpreter *perl_interp;
114
115         PERL_SET_CONTEXT(parent);
116         PL_perl_destruct_level = 2;
117         perl_interp = perl_clone(parent, CLONEf_CLONE_HOST);
118         PL_perl_destruct_level = 2;
119         PERL_SET_CONTEXT(perl_interp);
120         perl_destruct(perl_interp);
121         perl_free(perl_interp);
122     }
123     exit (0);
124 }
125 #endif
126
127 int simpleserver_clone(void) {
128 #ifdef USE_ITHREADS
129      nmem_mutex_enter(simpleserver_mutex);
130      if (1)
131      {
132          PerlInterpreter *current = PERL_GET_CONTEXT;
133
134          /* if current is unset, then we're in a new thread with
135           * no Perl interpreter for it. So we must create one .
136           * This will only happen when threaded is used..
137           */
138          if (!current) {
139              PerlInterpreter *perl_interp;
140              PERL_SET_CONTEXT( root_perl_context );
141              perl_interp = perl_clone(root_perl_context, CLONEf_CLONE_HOST);
142              PERL_SET_CONTEXT( perl_interp );
143          }
144      }
145      nmem_mutex_leave(simpleserver_mutex);
146 #endif
147      return 0;
148 }
149
150
151 void simpleserver_free(void) {
152     nmem_mutex_enter(simpleserver_mutex);
153     if (1)
154     {
155         PerlInterpreter *current_interp = PERL_GET_CONTEXT;
156
157         /* If current Perl Interp is different from root interp, then
158          * we're in threaded mode and we must destroy.. 
159          */
160         if (current_interp != root_perl_context) {
161             PL_perl_destruct_level = 2;
162             PERL_SET_CONTEXT(current_interp);
163             perl_destruct(current_interp);
164             perl_free(current_interp);
165         }
166     }
167     nmem_mutex_leave(simpleserver_mutex);
168 }
169
170
171 Z_GenericRecord *read_grs1(char *str, ODR o)
172 {
173         int type, ivalue;
174         char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
175         char value[GRS_BUF_SIZE+1];
176         Z_GenericRecord *r = 0;
177
178         original = str;
179         r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
180         r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
181         r->num_elements = 0;
182         
183         for (;;)
184         {
185                 Z_TaggedElement *t;
186                 Z_ElementData *c;
187                 int len;
188         
189                 ptr = strchr(str, '\n');
190                 if (!ptr) {
191                         return r;
192                 }
193                 len = ptr - str;
194                 if (len > GRS_BUF_SIZE) {
195                     yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
196                     len = GRS_BUF_SIZE;
197                 }
198                 strncpy(line, str, len);
199                 line[len] = 0;
200                 buf = line;
201                 str = ptr + 1;
202                 while (*buf && isspace(*buf))
203                         buf++;
204                 if (*buf == '}') {
205                         memmove(original, str, strlen(str));
206                         return r;
207                 }
208                 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
209                 {
210                         yaz_log(LOG_WARN, "Bad data in '%s'", buf);
211                         return r;
212                 }
213                 if (!type && *value == '0')
214                         return r;
215                 if (!(buf = strchr(buf, ')')))
216                         return r;
217                 buf++;
218                 while (*buf && isspace(*buf))
219                         buf++;
220                 if (r->num_elements >= GRS_MAX_FIELDS)
221                 {
222                         yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
223                         exit(0);
224                 }
225                 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
226                 t->tagType = (int *)odr_malloc(o, sizeof(int));
227                 *t->tagType = type;
228                 t->tagValue = (Z_StringOrNumeric *)
229                         odr_malloc(o, sizeof(Z_StringOrNumeric));
230                 if ((ivalue = atoi(value)))
231                 {
232                         t->tagValue->which = Z_StringOrNumeric_numeric;
233                         t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
234                         *t->tagValue->u.numeric = ivalue;
235                 }
236                 else
237                 {
238                         t->tagValue->which = Z_StringOrNumeric_string;
239                         t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
240                         strcpy(t->tagValue->u.string, value);
241                 }
242                 t->tagOccurrence = 0;
243                 t->metaData = 0;
244                 t->appliedVariant = 0;
245                 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
246                 if (*buf == '{')
247                 {
248                         c->which = Z_ElementData_subtree;
249                         c->u.subtree = read_grs1(str, o);
250                 }
251                 else
252                 {
253                         c->which = Z_ElementData_string;
254                         c->u.string = odr_strdup(o, buf);
255                 }
256                 r->num_elements++;
257         }
258 }
259
260
261
262
263 static void oid2str(Odr_oid *o, WRBUF buf)
264 {
265     for (; *o >= 0; o++) {
266         char ibuf[16];
267         sprintf(ibuf, "%d", *o);
268         wrbuf_puts(buf, ibuf);
269         if (o[1] > 0)
270             wrbuf_putc(buf, '.');
271     }
272 }
273
274
275 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
276 {
277     switch (s->which) {
278         case Z_RPNStructure_simple: {
279             Z_Operand *o = s->u.simple;
280
281             switch (o->which) {
282                 case Z_Operand_APT: {
283                     Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
284
285                     if (at->attributes) {
286                         int i;
287                         char ibuf[16];
288
289                         for (i = 0; i < at->attributes->num_attributes; i++) {
290                             wrbuf_puts(buf, "@attr ");
291                             if (at->attributes->attributes[i]->attributeSet) {
292                                 oid2str(at->attributes->attributes[i]->attributeSet, buf);
293                                 wrbuf_putc(buf, ' ');
294                             }
295                             sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
296                             assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
297                             wrbuf_puts(buf, ibuf);
298                             sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
299                             wrbuf_puts(buf, ibuf);
300                         }
301                     }
302                     switch (at->term->which) {
303                         case Z_Term_general: {
304                             wrbuf_putc(buf, '"');
305                             wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
306                             wrbuf_puts(buf, "\" ");
307                             break;
308                         }
309                         default: abort();
310                     }
311                     break;
312                 }
313                 default: abort();
314             }
315             break;
316         }
317         case Z_RPNStructure_complex: {
318             Z_Complex *c = s->u.complex;
319
320             switch (c->roperator->which) {
321                 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
322                 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
323                 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
324                 case Z_Operator_prox: abort();
325                 default: abort();
326             }
327             if (!rpn2pquery(c->s1, buf))
328                 return 0;
329             if (!rpn2pquery(c->s2, buf))
330                 return 0;
331             break;
332         }
333         default: abort();
334     }
335     return 1;
336 }
337
338
339 WRBUF zquery2pquery(Z_Query *q)
340 {
341     WRBUF buf = wrbuf_alloc();
342
343     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
344         return 0;
345     if (q->u.type_1->attributeSetId) {
346         /* Output attribute set ID */
347         wrbuf_puts(buf, "@attrset ");
348         oid2str(q->u.type_1->attributeSetId, buf);
349         wrbuf_putc(buf, ' ');
350     }
351     return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
352 }
353
354
355 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
356 #include <stdarg.h>
357 void fatal(char *fmt, ...)
358 {
359     va_list ap;
360
361     fprintf(stderr, "FATAL (SimpleServer): ");
362     va_start(ap, fmt);
363     vfprintf(stderr, fmt, ap);
364     va_end(ap);
365     fprintf(stderr, "\n");
366     abort();
367 }
368
369
370 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
371 /*
372  * Creates a new Perl object of type `class'; the newly-created scalar
373  * that is a reference to the blessed thingy `referent' is returned.
374  */
375 static SV *newObject(char *class, SV *referent)
376 {
377     HV *stash;
378     SV *sv;
379
380     sv = newRV_noinc((SV*) referent);
381     stash = gv_stashpv(class, 0);
382     if (stash == 0)
383         fatal("attempt to create object of undefined class '%s'", class);
384     /*assert(stash != 0);*/
385     sv_bless(sv, stash);
386     return sv;
387 }
388
389
390 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
391 static void setMember(HV *hv, char *name, SV *val)
392 {
393     /* We don't increment `val's reference count -- I think this is
394      * right because it's created with a refcount of 1, and in fact
395      * the reference via this hash is the only reference to it in
396      * general.
397      */
398     if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
399         fatal("couldn't store member in hash");
400 }
401
402
403 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
404 static SV *translateOID(Odr_oid *x)
405 {
406     /* Yaz represents an OID by an int array terminated by a negative
407      * value, typically -1; we represent it as a reference to a
408      * blessed scalar string of "."-separated elements.
409      */
410     char buf[1000];
411     int i;
412
413     *buf = '\0';
414     for (i = 0; x[i] >= 0; i++) {
415         sprintf(buf + strlen(buf), "%d", (int) x[i]);
416         if (x[i+1] >- 0)
417             strcat(buf, ".");
418     }
419
420     /*
421      * ### We'd like to return a blessed scalar (string) here, but of
422      *  course you can't do that in Perl: only references can be
423      *  blessed, so we'd have to return a _reference_ to a string, and
424      *  bless _that_.  Better to do without the blessing, I think.
425      */
426     if (1) {
427         return newSVpv(buf, 0);
428     } else {
429         return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
430     }
431 }
432
433
434 static SV *rpn2perl(Z_RPNStructure *s)
435 {
436     SV *sv;
437     HV *hv;
438     AV *av;
439
440     switch (s->which) {
441     case Z_RPNStructure_simple: {
442         Z_Operand *o = s->u.simple;
443         Z_AttributesPlusTerm *at;
444         if (o->which == Z_Operand_resultSetId) {
445             SV *sv2;
446             /* This code causes a SIGBUS on my machine, and I have no
447                idea why.  It seems as clear as day to me */
448             char *rsid = (char*) o->u.resultSetId;
449             printf("Encoding resultSetId '%s'\n", rsid);
450             sv = newObject("Net::Z3950::RPN::RSID", (SV*) (hv = newHV()));
451             printf("Made sv=0x%lx, hv=0x%lx\n",
452                    (unsigned long) sv ,(unsigned long) hv);
453             sv2 = newSVpv(rsid, strlen(rsid));
454             setMember(hv, "id", sv2);
455             printf("Set hv{id} to 0x%lx\n", (unsigned long) sv2);
456             return sv;
457         }
458         if (o->which != Z_Operand_APT)
459             fatal("can't handle RPN simples other than APT and RSID");
460         at = o->u.attributesPlusTerm;
461         if (at->term->which != Z_Term_general)
462             fatal("can't handle RPN terms other than general");
463
464         sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
465         if (at->attributes) {
466             int i;
467             SV *attrs = newObject("Net::Z3950::RPN::Attributes",
468                                   (SV*) (av = newAV()));
469             for (i = 0; i < at->attributes->num_attributes; i++) {
470                 Z_AttributeElement *elem = at->attributes->attributes[i];
471                 HV *hv2;
472                 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
473                                     (SV*) (hv2 = newHV()));
474                 if (elem->attributeSet)
475                     setMember(hv2, "attributeSet",
476                               translateOID(elem->attributeSet));
477                 setMember(hv2, "attributeType",
478                           newSViv(*elem->attributeType));
479                 assert(elem->which == Z_AttributeValue_numeric);
480                 setMember(hv2, "attributeValue",
481                           newSViv(*elem->value.numeric));
482                 av_push(av, tmp);
483             }
484             setMember(hv, "attributes", attrs);
485         }
486         setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
487                                       at->term->u.general->len));
488         return sv;
489     }
490     case Z_RPNStructure_complex: {
491         SV *tmp;
492         Z_Complex *c = s->u.complex;
493         char *type = 0;         /* vacuous assignment satisfies gcc -Wall */
494         switch (c->roperator->which) {
495         case Z_Operator_and:     type = "Net::Z3950::RPN::And"; break;
496         case Z_Operator_or:      type = "Net::Z3950::RPN::Or"; break;
497         case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
498         case Z_Operator_prox:    fatal("proximity not yet supported");
499         default: fatal("unknown RPN operator %d", (int) c->roperator->which);
500         }
501         sv = newObject(type, (SV*) (av = newAV()));
502         if ((tmp = rpn2perl(c->s1)) == 0)
503             return 0;
504         av_push(av, tmp);
505         if ((tmp = rpn2perl(c->s2)) == 0)
506             return 0;
507         av_push(av, tmp);
508         return sv;
509     }
510     default: fatal("unknown RPN node type %d", (int) s->which);
511     }
512
513     return 0;
514 }
515
516
517 static SV *zquery2perl(Z_Query *q)
518 {
519     SV *sv;
520     HV *hv;
521
522     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
523         return 0;
524     sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
525     if (q->u.type_1->attributeSetId)
526         setMember(hv, "attributeSet",
527                   translateOID(q->u.type_1->attributeSetId));
528     setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
529     return sv;
530 }
531
532
533 int bend_sort(void *handle, bend_sort_rr *rr)
534 {
535         HV *href;
536         AV *aref;
537         SV **temp;
538         SV *err_code;
539         SV *err_str;
540         SV *status;
541         STRLEN len;
542         char *ptr;
543         char *ODR_err_str;
544         char **input_setnames;
545         Zfront_handle *zhandle = (Zfront_handle *)handle;
546         int i;
547         
548         dSP;
549         ENTER;
550         SAVETMPS;
551         
552         aref = newAV();
553         input_setnames = rr->input_setnames;
554         for (i = 0; i < rr->num_input_setnames; i++)
555         {
556                 av_push(aref, newSVpv(*input_setnames++, 0));
557         }
558         href = newHV();
559         hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
560         hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
561         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
562         hv_store(href, "STATUS", 6, newSViv(0), 0);
563
564         PUSHMARK(sp);
565
566         XPUSHs(sv_2mortal(newRV( (SV*) href)));
567
568         PUTBACK;
569
570         perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
571
572         SPAGAIN;
573
574         temp = hv_fetch(href, "ERR_CODE", 8, 1);
575         err_code = newSVsv(*temp);
576
577         temp = hv_fetch(href, "ERR_STR", 7, 1);
578         err_str = newSVsv(*temp);
579
580         temp = hv_fetch(href, "STATUS", 6, 1);
581         status = newSVsv(*temp);
582
583         PUTBACK;
584         FREETMPS;
585         LEAVE;
586
587         hv_undef(href),
588         av_undef(aref);
589         rr->errcode = SvIV(err_code);
590         rr->sort_status = SvIV(status);
591         ptr = SvPV(err_str, len);
592         ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
593         strcpy(ODR_err_str, ptr);
594         rr->errstring = ODR_err_str;
595
596         sv_free(err_code);
597         sv_free(err_str);
598         sv_free(status);
599         
600         return 0;
601 }
602
603
604 int bend_search(void *handle, bend_search_rr *rr)
605 {
606         HV *href;
607         AV *aref;
608         SV **temp;
609         char *ODR_errstr;
610         STRLEN len;
611         int i;
612         char **basenames;
613         WRBUF query;
614         char *ptr;
615         SV *point;
616         Zfront_handle *zhandle = (Zfront_handle *)handle;
617         CV* handler_cv = 0;
618
619         dSP;
620         ENTER;
621         SAVETMPS;
622
623         aref = newAV();
624         basenames = rr->basenames;
625         for (i = 0; i < rr->num_bases; i++)
626         {
627                 av_push(aref, newSVpv(*basenames++, 0));
628         }
629 #if ENABLE_STOP_SERVER
630         if (rr->num_bases == 1 && !strcmp(rr->basenames[0], "XXstop"))
631         {
632                 zhandle->stop_flag = 1;
633         }
634 #endif
635         href = newHV();         
636         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
637         hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
638         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
639         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
640         hv_store(href, "HITS", 4, newSViv(0), 0);
641         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
642         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
643         hv_store(href, "PID", 3, newSViv(getpid()), 0);
644         hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
645         query = zquery2pquery(rr->query);
646         if (query)
647         {
648                 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
649         }
650         else
651         {       
652                 rr->errcode = 108;
653         }
654         PUSHMARK(sp);
655         
656         XPUSHs(sv_2mortal(newRV( (SV*) href)));
657         
658         PUTBACK;
659
660         handler_cv = simpleserver_sv2cv( search_ref );
661         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
662
663         SPAGAIN;
664
665         temp = hv_fetch(href, "HITS", 4, 1);
666         rr->hits = SvIV(*temp);
667
668         temp = hv_fetch(href, "ERR_CODE", 8, 1);
669         rr->errcode = SvIV(*temp);
670
671         temp = hv_fetch(href, "ERR_STR", 7, 1);
672         ptr = SvPV(*temp, len);
673         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
674         strcpy(ODR_errstr, ptr);
675         rr->errstring = ODR_errstr;
676
677         temp = hv_fetch(href, "HANDLE", 6, 1);
678         point = newSVsv(*temp);
679
680         hv_undef(href);
681         av_undef(aref);
682
683         zhandle->handle = point;
684         sv_free( (SV*) aref);
685         sv_free( (SV*) href);
686         wrbuf_free(query, 1);
687         PUTBACK;
688         FREETMPS;
689         LEAVE;
690         return 0;
691 }
692
693
694 /* ### this is worryingly similar to oid2str() */
695 WRBUF oid2dotted(int *oid)
696 {
697
698         WRBUF buf = wrbuf_alloc();
699         int dot = 0;
700
701         for (; *oid != -1 ; oid++)
702         {
703                 char ibuf[16];
704                 if (dot)
705                 {
706                         wrbuf_putc(buf, '.');
707                 }
708                 else
709                 {
710                         dot = 1;
711                 }
712                 sprintf(ibuf, "%d", *oid);
713                 wrbuf_puts(buf, ibuf);
714         }
715         return buf;
716 }
717                 
718
719 int dotted2oid(char *dotted, int *buffer)
720 {
721         int *oid;
722         char ibuf[16];
723         char *ptr;
724         int n = 0;
725
726         ptr = ibuf;
727         oid = buffer;
728         while (*dotted)
729         {
730                 if (*dotted == '.')
731                 {
732                         n++;
733                         if (n == MAX_OID)  /* Terminate if more than MAX_OID entries */
734                         {
735                                 *oid = -1;
736                                 return -1;
737                         }
738                         *ptr = 0;
739                         sscanf(ibuf, "%d", oid++);
740                         ptr = ibuf;
741                         dotted++;
742
743                 }
744                 else
745                 {
746                         *ptr++ = *dotted++;
747                 }
748         }
749         if (n < MAX_OID)
750         {
751                 *ptr = 0;
752                 sscanf(ibuf, "%d", oid++);
753         }
754         *oid = -1;
755         return 0;
756 }
757
758
759 int bend_fetch(void *handle, bend_fetch_rr *rr)
760 {
761         HV *href;
762         SV **temp;
763         SV *basename;
764         SV *record;
765         SV *last;
766         SV *err_code;
767         SV *err_string;
768         SV *sur_flag;
769         SV *point;
770         SV *rep_form;
771         char *ptr;
772         char *ODR_record;
773         char *ODR_basename;
774         char *ODR_errstr;
775         int *ODR_oid_buf;
776         oident *oid;
777         WRBUF oid_dotted;
778         Zfront_handle *zhandle = (Zfront_handle *)handle;
779         CV* handler_cv = 0;
780
781         Z_RecordComposition *composition;
782         Z_ElementSetNames *simple;
783         STRLEN length;
784
785         dSP;
786         ENTER;
787         SAVETMPS;
788
789         rr->errcode = 0;
790         href = newHV();
791         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
792         temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
793         oid_dotted = oid2dotted(rr->request_format_raw);
794         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
795         hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
796         hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
797         hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
798         hv_store(href, "LAST", 4, newSViv(0), 0);
799         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
800         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
801         hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
802         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
803         hv_store(href, "PID", 3, newSViv(getpid()), 0);
804         if (rr->comp)
805         {
806                 composition = rr->comp;
807                 if (composition->which == Z_RecordComp_simple)
808                 {
809                         simple = composition->u.simple;
810                         if (simple->which == Z_ElementSetNames_generic)
811                         {
812                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
813                         } 
814                         else
815                         {
816                                 rr->errcode = 26;
817                         }
818                 }
819                 else
820                 {
821                         rr->errcode = 26;
822                 }
823         }
824
825         PUSHMARK(sp);
826
827         XPUSHs(sv_2mortal(newRV( (SV*) href)));
828
829         PUTBACK;
830         
831         handler_cv = simpleserver_sv2cv( fetch_ref );
832         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
833
834         SPAGAIN;
835
836         temp = hv_fetch(href, "BASENAME", 8, 1);
837         basename = newSVsv(*temp);
838
839         temp = hv_fetch(href, "RECORD", 6, 1);
840         record = newSVsv(*temp);
841
842         temp = hv_fetch(href, "LAST", 4, 1);
843         last = newSVsv(*temp);
844
845         temp = hv_fetch(href, "ERR_CODE", 8, 1);
846         err_code = newSVsv(*temp);
847
848         temp = hv_fetch(href, "ERR_STR", 7, 1),
849         err_string = newSVsv(*temp);
850
851         temp = hv_fetch(href, "SUR_FLAG", 8, 1);
852         sur_flag = newSVsv(*temp);
853
854         temp = hv_fetch(href, "REP_FORM", 8, 1);
855         rep_form = newSVsv(*temp);
856
857         temp = hv_fetch(href, "HANDLE", 6, 1);
858         point = newSVsv(*temp);
859
860
861         hv_undef(href);
862         
863         ptr = SvPV(basename, length);
864         ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
865         strcpy(ODR_basename, ptr);
866         rr->basename = ODR_basename;
867
868         ptr = SvPV(rep_form, length);
869         ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
870         if (dotted2oid(ptr, ODR_oid_buf) == -1)         /* Maximum number of OID elements exceeded */
871         {
872                 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
873         }
874         rr->output_format_raw = ODR_oid_buf;    
875         
876         ptr = SvPV(record, length);
877         oid = oid_getentbyoid(ODR_oid_buf);
878         if (oid->value == VAL_GRS1)             /* Treat GRS-1 records separately */
879         {
880                 rr->record = (char *) read_grs1(ptr, rr->stream);
881                 rr->len = -1;
882         }
883         else
884         {
885                 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
886                 strcpy(ODR_record, ptr);
887                 rr->record = ODR_record;
888                 rr->len = length;
889         }
890         zhandle->handle = point;
891         handle = zhandle;
892         rr->last_in_set = SvIV(last);
893         
894         if (!(rr->errcode))
895         {
896                 rr->errcode = SvIV(err_code);
897                 ptr = SvPV(err_string, length);
898                 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
899                 strcpy(ODR_errstr, ptr);
900                 rr->errstring = ODR_errstr;
901         }
902         rr->surrogate_flag = SvIV(sur_flag);
903
904         wrbuf_free(oid_dotted, 1);
905         sv_free((SV*) href);
906         sv_free(basename);
907         sv_free(record);
908         sv_free(last);
909         sv_free(err_string);
910         sv_free(err_code),
911         sv_free(sur_flag);
912         sv_free(rep_form);
913
914         PUTBACK;
915         FREETMPS;
916         LEAVE;
917         
918         return 0;
919 }
920
921
922 int bend_present(void *handle, bend_present_rr *rr)
923 {
924         HV *href;
925         SV **temp;
926         SV *err_code;
927         SV *err_string;
928         SV *hits;
929         SV *point;
930         STRLEN len;
931         Z_RecordComposition *composition;
932         Z_ElementSetNames *simple;
933         char *ODR_errstr;
934         char *ptr;
935         Zfront_handle *zhandle = (Zfront_handle *)handle;
936         CV* handler_cv = 0;
937
938 /*      WRBUF oid_dotted; */
939
940         dSP;
941         ENTER;
942         SAVETMPS;
943
944         href = newHV();
945         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
946         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
947         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
948         hv_store(href, "START", 5, newSViv(rr->start), 0);
949         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
950         hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
951         /*oid_dotted = oid2dotted(rr->request_format_raw);
952         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
953         hv_store(href, "HITS", 4, newSViv(0), 0);
954         hv_store(href, "PID", 3, newSViv(getpid()), 0);
955         if (rr->comp)
956         {
957                 composition = rr->comp;
958                 if (composition->which == Z_RecordComp_simple)
959                 {
960                         simple = composition->u.simple;
961                         if (simple->which == Z_ElementSetNames_generic)
962                         {
963                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
964                         } 
965                         else
966                         {
967                                 rr->errcode = 26;
968                                 return 0;
969                         }
970                 }
971                 else
972                 {
973                         rr->errcode = 26;
974                         return 0;
975                 }
976         }
977
978         PUSHMARK(sp);
979         
980         XPUSHs(sv_2mortal(newRV( (SV*) href)));
981         
982         PUTBACK;
983         
984         handler_cv = simpleserver_sv2cv( present_ref );
985         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
986         
987         SPAGAIN;
988
989         temp = hv_fetch(href, "ERR_CODE", 8, 1);
990         err_code = newSVsv(*temp);
991
992         temp = hv_fetch(href, "ERR_STR", 7, 1);
993         err_string = newSVsv(*temp);
994
995         temp = hv_fetch(href, "HITS", 4, 1);
996         hits = newSVsv(*temp);
997
998         temp = hv_fetch(href, "HANDLE", 6, 1);
999         point = newSVsv(*temp);
1000
1001         PUTBACK;
1002         FREETMPS;
1003         LEAVE;
1004         
1005         hv_undef(href);
1006         rr->errcode = SvIV(err_code);
1007         rr->hits = SvIV(hits);
1008
1009         ptr = SvPV(err_string, len);
1010         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1011         strcpy(ODR_errstr, ptr);
1012         rr->errstring = ODR_errstr;
1013 /*      wrbuf_free(oid_dotted, 1);*/
1014         zhandle->handle = point;
1015         handle = zhandle;
1016         sv_free(err_code);
1017         sv_free(err_string);
1018         sv_free(hits);
1019         sv_free( (SV*) href);
1020
1021         return 0;
1022 }
1023
1024
1025 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1026 {
1027         perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1028         return 0;
1029 }
1030
1031
1032 int bend_delete(void *handle, bend_delete_rr *rr)
1033 {
1034         perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
1035         return 0;
1036 }
1037
1038
1039 int bend_scan(void *handle, bend_scan_rr *rr)
1040 {
1041         HV *href;
1042         AV *aref;
1043         AV *list;
1044         AV *entries;
1045         HV *scan_item;
1046         struct scan_entry *scan_list;
1047         struct scan_entry *buffer;
1048         int *step_size = rr->step_size;
1049         int i;
1050         char **basenames;
1051         SV **temp;
1052         SV *err_code = sv_newmortal();
1053         SV *err_str = sv_newmortal();
1054         SV *point = sv_newmortal();
1055         SV *status = sv_newmortal();
1056         SV *number = sv_newmortal();
1057         char *ptr;
1058         char *ODR_errstr;
1059         STRLEN len;
1060         int term_len;
1061         SV *entries_ref;
1062         Zfront_handle *zhandle = (Zfront_handle *)handle;
1063         CV* handler_cv = 0;
1064
1065         dSP;
1066         ENTER;
1067         SAVETMPS;
1068         href = newHV();
1069         list = newAV();
1070         if (rr->term->term->which == Z_Term_general)
1071         {
1072                 term_len = rr->term->term->u.general->len;
1073                 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1074         } else {
1075                 rr->errcode = 229;      /* Unsupported term type */
1076                 return 0;
1077         }
1078         hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1079         hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1080         hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1081         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1082         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1083         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1084         hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1085         hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1086         aref = newAV();
1087         basenames = rr->basenames;
1088         for (i = 0; i < rr->num_bases; i++)
1089         {
1090                 av_push(aref, newSVpv(*basenames++, 0));
1091         }
1092         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1093
1094         PUSHMARK(sp);
1095
1096         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1097
1098         PUTBACK;
1099
1100         handler_cv = simpleserver_sv2cv( scan_ref );
1101         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1102
1103         SPAGAIN;
1104
1105         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1106         err_code = newSVsv(*temp);
1107
1108         temp = hv_fetch(href, "ERR_STR", 7, 1);
1109         err_str = newSVsv(*temp);
1110
1111         temp = hv_fetch(href, "HANDLE", 6, 1);
1112         point = newSVsv(*temp);
1113
1114         temp = hv_fetch(href, "STATUS", 6, 1);
1115         status = newSVsv(*temp);
1116         
1117         temp = hv_fetch(href, "NUMBER", 6, 1);
1118         number = newSVsv(*temp);
1119
1120         temp = hv_fetch(href, "ENTRIES", 7, 1);
1121         entries_ref = newSVsv(*temp);
1122
1123         PUTBACK;
1124         FREETMPS;
1125         LEAVE;
1126
1127         ptr = SvPV(err_str, len);
1128         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1129         strcpy(ODR_errstr, ptr);
1130         rr->errstring = ODR_errstr;
1131         rr->errcode = SvIV(err_code);
1132         rr->num_entries = SvIV(number);
1133         rr->status = SvIV(status);
1134         scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1135         buffer = scan_list;
1136         entries = (AV *)SvRV(entries_ref);
1137         for (i = 0; i < rr->num_entries; i++)
1138         {
1139                 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1140                 temp = hv_fetch(scan_item, "TERM", 4, 1);
1141                 ptr = SvPV(*temp, len);
1142                 buffer->term = (char *) odr_malloc (rr->stream, len + 1); 
1143                 strcpy(buffer->term, ptr);
1144                 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1); 
1145                 buffer->occurrences = SvIV(*temp);
1146                 buffer++;
1147                 hv_undef(scan_item);
1148         }
1149         rr->entries = scan_list;
1150         zhandle->handle = point;
1151         handle = zhandle;
1152         sv_free(err_code);
1153         sv_free(err_str);
1154         sv_free(status);
1155         sv_free(number);
1156         hv_undef(href);
1157         sv_free((SV *)href);
1158         av_undef(aref);
1159         sv_free((SV *)aref);
1160         av_undef(list);
1161         sv_free((SV *)list);
1162         av_undef(entries);
1163         /*sv_free((SV *)entries);*/
1164         sv_free(entries_ref);
1165
1166         return 0;
1167 }
1168
1169 bend_initresult *bend_init(bend_initrequest *q)
1170 {
1171         int dummy = simpleserver_clone();
1172         bend_initresult *r = (bend_initresult *)
1173                 odr_malloc (q->stream, sizeof(*r));
1174         char *ptr;
1175         char *user = NULL;
1176         char *passwd = NULL;
1177         CV* handler_cv = 0;
1178         dSP;
1179         STRLEN len;
1180         NMEM nmem = nmem_create();
1181         Zfront_handle *zhandle =  (Zfront_handle *) nmem_malloc (nmem,
1182                         sizeof(*zhandle));
1183         SV *handle;
1184         HV *href;
1185         SV **temp;
1186
1187         ENTER;
1188         SAVETMPS;
1189
1190         zhandle->nmem = nmem;
1191         zhandle->stop_flag = 0;
1192         /*q->bend_sort = bend_sort;*/
1193         if (search_ref)
1194         {
1195                 q->bend_search = bend_search;
1196         }
1197         if (present_ref)
1198         {
1199                 q->bend_present = bend_present;
1200         }
1201         /*q->bend_esrequest = bend_esrequest;*/
1202         /*q->bend_delete = bend_delete;*/
1203         if (fetch_ref)
1204         {
1205                 q->bend_fetch = bend_fetch;
1206         }
1207         if (scan_ref)
1208         {
1209                 q->bend_scan = bend_scan;
1210         }
1211
1212         href = newHV(); 
1213         hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1214         hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1215         hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1216         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1217         hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1218         hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1219         hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1220         hv_store(href, "PID", 3, newSViv(getpid()), 0);
1221         if (q->auth) {
1222             if (q->auth->which == Z_IdAuthentication_open) {
1223                 char *openpass = xstrdup (q->auth->u.open);
1224                 char *cp = strchr (openpass, '/');
1225                 if (cp) {
1226                     *cp = '\0';
1227                     user = nmem_strdup (odr_getmem (q->stream), openpass);
1228                     passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1229                 }
1230                 xfree(openpass);
1231             } else if (q->auth->which == Z_IdAuthentication_idPass) {
1232                 user = q->auth->u.idPass->userId;
1233                 passwd = q->auth->u.idPass->password;
1234             }
1235             /* ### some code paths have user/password unassigned here */
1236             hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1237             hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1238         }
1239
1240         PUSHMARK(sp);   
1241
1242         XPUSHs(sv_2mortal(newRV((SV*) href)));
1243
1244         PUTBACK;
1245
1246         if (init_ref != NULL)
1247         {
1248              handler_cv = simpleserver_sv2cv( init_ref );
1249              perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1250         }
1251
1252         SPAGAIN;
1253
1254         temp = hv_fetch(href, "IMP_ID", 6, 1);
1255         ptr = SvPV(*temp, len);
1256         q->implementation_id = nmem_strdup(nmem, ptr);
1257
1258         temp = hv_fetch(href, "IMP_NAME", 8, 1);
1259         ptr = SvPV(*temp, len);
1260         q->implementation_name = nmem_strdup(nmem, ptr);
1261
1262         temp = hv_fetch(href, "IMP_VER", 7, 1);
1263         ptr = SvPV(*temp, len);
1264         q->implementation_version = nmem_strdup(nmem, ptr);
1265
1266         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1267         r->errcode = SvIV(*temp);
1268
1269         temp = hv_fetch(href, "ERR_STR", 7, 1);
1270         ptr = SvPV(*temp, len);
1271         r->errstring = (char *)odr_malloc(q->stream, len + 1);
1272         strcpy(r->errstring, ptr);
1273
1274         temp = hv_fetch(href, "HANDLE", 6, 1);
1275         handle= newSVsv(*temp);
1276         zhandle->handle = handle;
1277
1278         r->handle = zhandle;
1279
1280         hv_undef(href);
1281         sv_free((SV*) href);
1282
1283         PUTBACK;
1284         FREETMPS;
1285         LEAVE;
1286         
1287         return r;       
1288 }
1289
1290 void bend_close(void *handle)
1291 {
1292         HV *href;
1293         Zfront_handle *zhandle = (Zfront_handle *)handle;
1294         CV* handler_cv = 0;
1295         int stop_flag = 0;
1296         dSP;
1297         ENTER;
1298         SAVETMPS;
1299
1300         if (close_ref)
1301         {
1302                 href = newHV();
1303                 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1304
1305                 PUSHMARK(sp);
1306
1307                 XPUSHs(sv_2mortal(newRV((SV *)href)));
1308
1309                 PUTBACK;
1310         
1311                 handler_cv = simpleserver_sv2cv( close_ref );
1312                 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1313         
1314                 SPAGAIN;
1315
1316                 sv_free((SV*) href);
1317         }
1318         else
1319                 sv_free(zhandle->handle);
1320         PUTBACK;
1321         FREETMPS;
1322         LEAVE;
1323         stop_flag = zhandle->stop_flag;
1324         nmem_destroy(zhandle->nmem);
1325         simpleserver_free();
1326
1327         if (stop_flag)
1328                 exit(0);
1329         return;
1330 }
1331
1332
1333 MODULE = Net::Z3950::SimpleServer       PACKAGE = Net::Z3950::SimpleServer
1334
1335 PROTOTYPES: DISABLE
1336
1337
1338 void
1339 set_init_handler(arg)
1340                 SV *arg
1341         CODE:
1342                 init_ref = newSVsv(arg);
1343                 
1344
1345 void
1346 set_close_handler(arg)
1347                 SV *arg
1348         CODE:
1349                 close_ref = newSVsv(arg);
1350
1351
1352 void
1353 set_sort_handler(arg)
1354                 SV *arg
1355         CODE:
1356                 sort_ref = newSVsv(arg);
1357
1358 void
1359 set_search_handler(arg)
1360                 SV *arg
1361         CODE:
1362                 search_ref = newSVsv(arg);
1363
1364
1365 void
1366 set_fetch_handler(arg)
1367                 SV *arg
1368         CODE:
1369                 fetch_ref = newSVsv(arg);
1370
1371
1372 void
1373 set_present_handler(arg)
1374                 SV *arg
1375         CODE:
1376                 present_ref = newSVsv(arg);
1377
1378
1379 void
1380 set_esrequest_handler(arg)
1381                 SV *arg
1382         CODE:
1383                 esrequest_ref = newSVsv(arg);
1384
1385
1386 void
1387 set_delete_handler(arg)
1388                 SV *arg
1389         CODE:
1390                 delete_ref = newSVsv(arg);
1391
1392
1393 void
1394 set_scan_handler(arg)
1395                 SV *arg
1396         CODE:
1397                 scan_ref = newSVsv(arg);
1398
1399
1400 int
1401 start_server(...)
1402         PREINIT:
1403                 char **argv;
1404                 char **argv_buf;
1405                 char *ptr;
1406                 int i;
1407                 STRLEN len;
1408         CODE:
1409                 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1410                 argv = argv_buf;
1411                 for (i = 0; i < items; i++)
1412                 {
1413                         ptr = SvPV(ST(i), len);
1414                         *argv_buf = (char *)xmalloc(len + 1);
1415                         strcpy(*argv_buf++, ptr); 
1416                 }
1417                 *argv_buf = NULL;
1418                 root_perl_context = PERL_GET_CONTEXT;
1419                 nmem_mutex_create(&simpleserver_mutex);
1420 #if 0
1421                 /* only for debugging perl_clone .. */
1422                 tst_clones();
1423 #endif
1424                 
1425                 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1426         OUTPUT:
1427                 RETVAL
1428
1429
1430 int
1431 ScanSuccess()
1432         CODE:
1433                 RETVAL = BEND_SCAN_SUCCESS;
1434         OUTPUT:
1435                 RETVAL
1436
1437 int
1438 ScanPartial()
1439         CODE:
1440                 RETVAL = BEND_SCAN_PARTIAL;
1441         OUTPUT:
1442                 RETVAL
1443
1444  
1445 void
1446 yazlog(arg)
1447                 SV *arg
1448         CODE:
1449                 STRLEN len;
1450                 char *ptr;
1451                 ptr = SvPV(arg, len);
1452                 yaz_log(YLOG_LOG, "%.*s", len, ptr);