Registering the bend_sort handler and implemented the missing bits and pieces.
[simpleserver-moved-to-github.git] / SimpleServer.xs
1 /*
2  * $Id: SimpleServer.xs,v 1.42 2006-04-19 13:17:52 sondberg 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 WRBUF zquery2pquery(Z_Query *q)
276 {
277     WRBUF buf = wrbuf_alloc();
278
279     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
280         return 0;
281     yaz_rpnquery_to_wrbuf(buf, q->u.type_1);
282     return buf;
283 }
284
285
286 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
287 #include <stdarg.h>
288 void fatal(char *fmt, ...)
289 {
290     va_list ap;
291
292     fprintf(stderr, "FATAL (SimpleServer): ");
293     va_start(ap, fmt);
294     vfprintf(stderr, fmt, ap);
295     va_end(ap);
296     fprintf(stderr, "\n");
297     abort();
298 }
299
300
301 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
302 /*
303  * Creates a new Perl object of type `class'; the newly-created scalar
304  * that is a reference to the blessed thingy `referent' is returned.
305  */
306 static SV *newObject(char *class, SV *referent)
307 {
308     HV *stash;
309     SV *sv;
310
311     sv = newRV_noinc((SV*) referent);
312     stash = gv_stashpv(class, 0);
313     if (stash == 0)
314         fatal("attempt to create object of undefined class '%s'", class);
315     /*assert(stash != 0);*/
316     sv_bless(sv, stash);
317     return sv;
318 }
319
320
321 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
322 static void setMember(HV *hv, char *name, SV *val)
323 {
324     /* We don't increment `val's reference count -- I think this is
325      * right because it's created with a refcount of 1, and in fact
326      * the reference via this hash is the only reference to it in
327      * general.
328      */
329     if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
330         fatal("couldn't store member in hash");
331 }
332
333
334 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
335 static SV *translateOID(Odr_oid *x)
336 {
337     /* Yaz represents an OID by an int array terminated by a negative
338      * value, typically -1; we represent it as a reference to a
339      * blessed scalar string of "."-separated elements.
340      */
341     char buf[1000];
342     int i;
343
344     *buf = '\0';
345     for (i = 0; x[i] >= 0; i++) {
346         sprintf(buf + strlen(buf), "%d", (int) x[i]);
347         if (x[i+1] >- 0)
348             strcat(buf, ".");
349     }
350
351     /*
352      * ### We'd like to return a blessed scalar (string) here, but of
353      *  course you can't do that in Perl: only references can be
354      *  blessed, so we'd have to return a _reference_ to a string, and
355      *  bless _that_.  Better to do without the blessing, I think.
356      */
357     if (1) {
358         return newSVpv(buf, 0);
359     } else {
360         return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
361     }
362 }
363
364
365 static SV *rpn2perl(Z_RPNStructure *s)
366 {
367     SV *sv;
368     HV *hv;
369     AV *av;
370
371     switch (s->which) {
372     case Z_RPNStructure_simple: {
373         Z_Operand *o = s->u.simple;
374         Z_AttributesPlusTerm *at;
375         if (o->which == Z_Operand_resultSetId) {
376             SV *sv2;
377             /* This code causes a SIGBUS on my machine, and I have no
378                idea why.  It seems as clear as day to me */
379             char *rsid = (char*) o->u.resultSetId;
380             printf("Encoding resultSetId '%s'\n", rsid);
381             sv = newObject("Net::Z3950::RPN::RSID", (SV*) (hv = newHV()));
382             printf("Made sv=0x%lx, hv=0x%lx\n",
383                    (unsigned long) sv ,(unsigned long) hv);
384             sv2 = newSVpv(rsid, strlen(rsid));
385             setMember(hv, "id", sv2);
386             printf("Set hv{id} to 0x%lx\n", (unsigned long) sv2);
387             return sv;
388         }
389         if (o->which != Z_Operand_APT)
390             fatal("can't handle RPN simples other than APT and RSID");
391         at = o->u.attributesPlusTerm;
392         if (at->term->which != Z_Term_general)
393             fatal("can't handle RPN terms other than general");
394
395         sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
396         if (at->attributes) {
397             int i;
398             SV *attrs = newObject("Net::Z3950::RPN::Attributes",
399                                   (SV*) (av = newAV()));
400             for (i = 0; i < at->attributes->num_attributes; i++) {
401                 Z_AttributeElement *elem = at->attributes->attributes[i];
402                 HV *hv2;
403                 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
404                                     (SV*) (hv2 = newHV()));
405                 if (elem->attributeSet)
406                     setMember(hv2, "attributeSet",
407                               translateOID(elem->attributeSet));
408                 setMember(hv2, "attributeType",
409                           newSViv(*elem->attributeType));
410                 assert(elem->which == Z_AttributeValue_numeric);
411                 setMember(hv2, "attributeValue",
412                           newSViv(*elem->value.numeric));
413                 av_push(av, tmp);
414             }
415             setMember(hv, "attributes", attrs);
416         }
417         setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
418                                       at->term->u.general->len));
419         return sv;
420     }
421     case Z_RPNStructure_complex: {
422         SV *tmp;
423         Z_Complex *c = s->u.complex;
424         char *type = 0;         /* vacuous assignment satisfies gcc -Wall */
425         switch (c->roperator->which) {
426         case Z_Operator_and:     type = "Net::Z3950::RPN::And"; break;
427         case Z_Operator_or:      type = "Net::Z3950::RPN::Or"; break;
428         case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
429         case Z_Operator_prox:    fatal("proximity not yet supported");
430         default: fatal("unknown RPN operator %d", (int) c->roperator->which);
431         }
432         sv = newObject(type, (SV*) (av = newAV()));
433         if ((tmp = rpn2perl(c->s1)) == 0)
434             return 0;
435         av_push(av, tmp);
436         if ((tmp = rpn2perl(c->s2)) == 0)
437             return 0;
438         av_push(av, tmp);
439         return sv;
440     }
441     default: fatal("unknown RPN node type %d", (int) s->which);
442     }
443
444     return 0;
445 }
446
447
448 /* Decode the Z_SortKeySpec struct and store the whole thing in a perl hash */
449 int simpleserver_SortKeySpecToHash (HV *sort_spec, Z_SortKeySpec *spec)
450 {
451     Z_SortElement *element = spec->sortElement;
452
453     hv_store(sort_spec, "RELATION", 8, newSViv(*spec->sortRelation), 0);
454     hv_store(sort_spec, "CASE", 4, newSViv(*spec->caseSensitivity), 0);
455     hv_store(sort_spec, "MISSING", 7, newSViv(spec->which), 0);
456
457     if (element->which == Z_SortElement_generic)
458     {
459         Z_SortKey *key = element->u.generic;
460
461         if (key->which == Z_SortKey_sortField)
462         {
463             hv_store(sort_spec, "SORTFIELD", 9,
464                      newSVpv((char *) key->u.sortField, 0), 0);
465         }
466         else if (key->which == Z_SortKey_elementSpec)
467         {
468             Z_Specification *zspec = key->u.elementSpec;
469             
470             hv_store(sort_spec, "ELEMENTSPEC_TYPE", 16,
471                      newSViv(zspec->which), 0);
472
473             if (zspec->which == Z_Schema_oid)
474             {
475                 WRBUF elementSpec = wrbuf_alloc();
476
477                 oid2str(zspec->schema.oid, elementSpec);
478                 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
479                          newSVpv(elementSpec->buf, elementSpec->pos), 0);
480                 wrbuf_free(elementSpec, 1);
481             }
482             else if (zspec->which == Z_Schema_uri)
483             {
484                 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
485                          newSVpv((char *) zspec->schema.uri, 0), 0);
486             }
487         }
488         else if (key->which == Z_SortKey_sortAttributes)
489         {
490             Z_SortAttributes *attr = key->u.sortAttributes;
491             
492         }
493         else
494         {
495             return 0;
496         }
497     }
498     else
499     {
500         return 0;
501     }
502
503     return 1;
504 }
505
506
507 static SV *zquery2perl(Z_Query *q)
508 {
509     SV *sv;
510     HV *hv;
511
512     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
513         return 0;
514     sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
515     if (q->u.type_1->attributeSetId)
516         setMember(hv, "attributeSet",
517                   translateOID(q->u.type_1->attributeSetId));
518     setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
519     return sv;
520 }
521
522
523 int bend_sort(void *handle, bend_sort_rr *rr)
524 {
525         HV *href;
526         AV *aref;
527         AV *sort_seq;
528         SV **temp;
529         SV *err_code;
530         SV *err_str;
531         SV *status;
532         SV *point;
533         STRLEN len;
534         char *ptr;
535         char *ODR_err_str;
536         char **input_setnames;
537         Zfront_handle *zhandle = (Zfront_handle *)handle;
538         Z_SortKeySpecList *sort_spec = rr->sort_sequence;
539         int i;
540         
541         dSP;
542         ENTER;
543         SAVETMPS;
544         
545         aref = newAV();
546         input_setnames = rr->input_setnames;
547         for (i = 0; i < rr->num_input_setnames; i++)
548         {
549             av_push(aref, newSVpv(*input_setnames++, 0));
550         }
551
552         sort_seq = newAV();
553         for (i = 0; i < sort_spec->num_specs; i++)
554         {
555             Z_SortKeySpec *spec = *sort_spec->specs++;
556             HV *sort_spec = newHV();
557
558             if ( simpleserver_SortKeySpecToHash(sort_spec, spec) )
559                 av_push(sort_seq, newRV( sv_2mortal( (SV*) sort_spec ) ));
560             else
561             {
562                 rr->errcode = 207;
563                 return 0;
564             }
565         }
566         
567         href = newHV();
568         hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
569         hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
570         hv_store(href, "SEQUENCE", 8, newRV( (SV*) sort_seq), 0);
571         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
572         hv_store(href, "STATUS", 6, newSViv(0), 0);
573         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
574         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
575
576         PUSHMARK(sp);
577
578         XPUSHs(sv_2mortal(newRV( (SV*) href)));
579
580         PUTBACK;
581
582         perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
583
584         SPAGAIN;
585
586         temp = hv_fetch(href, "ERR_CODE", 8, 1);
587         err_code = newSVsv(*temp);
588
589         temp = hv_fetch(href, "ERR_STR", 7, 1);
590         err_str = newSVsv(*temp);
591
592         temp = hv_fetch(href, "STATUS", 6, 1);
593         status = newSVsv(*temp);
594
595         temp = hv_fetch(href, "HANDLE", 6, 1);
596         point = newSVsv(*temp);
597
598         hv_undef(href);
599         av_undef(aref);
600         av_undef(sort_seq);
601        
602         sv_free( (SV*) aref);
603         sv_free( (SV*) href);
604         sv_free( (SV*) sort_seq);
605
606         rr->errcode = SvIV(err_code);
607         rr->sort_status = SvIV(status);
608         
609         ptr = SvPV(err_str, len);
610         ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
611         strcpy(ODR_err_str, ptr);
612         rr->errstring = ODR_err_str;
613         zhandle->handle = point;
614
615         sv_free(err_code);
616         sv_free(err_str);
617         sv_free(status);
618         
619         PUTBACK;
620         FREETMPS;
621         LEAVE;
622
623         return 0;
624 }
625
626
627 int bend_search(void *handle, bend_search_rr *rr)
628 {
629         HV *href;
630         AV *aref;
631         SV **temp;
632         char *ODR_errstr;
633         STRLEN len;
634         int i;
635         char **basenames;
636         WRBUF query;
637         char *ptr;
638         SV *point;
639         Zfront_handle *zhandle = (Zfront_handle *)handle;
640         CV* handler_cv = 0;
641         SV *rpnSV;
642
643         dSP;
644         ENTER;
645         SAVETMPS;
646
647         aref = newAV();
648         basenames = rr->basenames;
649         for (i = 0; i < rr->num_bases; i++)
650         {
651                 av_push(aref, newSVpv(*basenames++, 0));
652         }
653 #if ENABLE_STOP_SERVER
654         if (rr->num_bases == 1 && !strcmp(rr->basenames[0], "XXstop"))
655         {
656                 zhandle->stop_flag = 1;
657         }
658 #endif
659         href = newHV();         
660         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
661         hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
662         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
663         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
664         hv_store(href, "HITS", 4, newSViv(0), 0);
665         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
666         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
667         hv_store(href, "PID", 3, newSViv(getpid()), 0);
668         if ((rpnSV = zquery2perl(rr->query)) != 0) {
669             hv_store(href, "RPN", 3, rpnSV, 0);
670         }
671         query = zquery2pquery(rr->query);
672         if (query)
673         {
674                 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
675         }
676         else if (rr->query->which == Z_Query_type_104 &&
677                  rr->query->u.type_104->which == Z_External_CQL) {
678             hv_store(href, "CQL", 3,
679                      newSVpv(rr->query->u.type_104->u.cql, 0), 0);
680         }
681         else
682         {       
683                 rr->errcode = 108;
684                 return 0;
685         }
686         PUSHMARK(sp);
687         
688         XPUSHs(sv_2mortal(newRV( (SV*) href)));
689         
690         PUTBACK;
691
692         handler_cv = simpleserver_sv2cv( search_ref );
693         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
694
695         SPAGAIN;
696
697         temp = hv_fetch(href, "HITS", 4, 1);
698         rr->hits = SvIV(*temp);
699
700         temp = hv_fetch(href, "ERR_CODE", 8, 1);
701         rr->errcode = SvIV(*temp);
702
703         temp = hv_fetch(href, "ERR_STR", 7, 1);
704         ptr = SvPV(*temp, len);
705         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
706         strcpy(ODR_errstr, ptr);
707         rr->errstring = ODR_errstr;
708
709         temp = hv_fetch(href, "HANDLE", 6, 1);
710         point = newSVsv(*temp);
711
712         hv_undef(href);
713         av_undef(aref);
714
715         zhandle->handle = point;
716         sv_free( (SV*) aref);
717         sv_free( (SV*) href);
718         if (query)
719             wrbuf_free(query, 1);
720         PUTBACK;
721         FREETMPS;
722         LEAVE;
723         return 0;
724 }
725
726
727 /* ### this is worryingly similar to oid2str() */
728 WRBUF oid2dotted(int *oid)
729 {
730
731         WRBUF buf = wrbuf_alloc();
732         int dot = 0;
733
734         for (; *oid != -1 ; oid++)
735         {
736                 char ibuf[16];
737                 if (dot)
738                 {
739                         wrbuf_putc(buf, '.');
740                 }
741                 else
742                 {
743                         dot = 1;
744                 }
745                 sprintf(ibuf, "%d", *oid);
746                 wrbuf_puts(buf, ibuf);
747         }
748         return buf;
749 }
750                 
751
752 int dotted2oid(char *dotted, int *buffer)
753 {
754         int *oid;
755         char ibuf[16];
756         char *ptr;
757         int n = 0;
758
759         ptr = ibuf;
760         oid = buffer;
761         while (*dotted)
762         {
763                 if (*dotted == '.')
764                 {
765                         n++;
766                         if (n == MAX_OID)  /* Terminate if more than MAX_OID entries */
767                         {
768                                 *oid = -1;
769                                 return -1;
770                         }
771                         *ptr = 0;
772                         sscanf(ibuf, "%d", oid++);
773                         ptr = ibuf;
774                         dotted++;
775
776                 }
777                 else
778                 {
779                         *ptr++ = *dotted++;
780                 }
781         }
782         if (n < MAX_OID)
783         {
784                 *ptr = 0;
785                 sscanf(ibuf, "%d", oid++);
786         }
787         *oid = -1;
788         return 0;
789 }
790
791
792 int bend_fetch(void *handle, bend_fetch_rr *rr)
793 {
794         HV *href;
795         SV **temp;
796         SV *basename;
797         SV *record;
798         SV *last;
799         SV *err_code;
800         SV *err_string;
801         SV *sur_flag;
802         SV *point;
803         SV *rep_form;
804         char *ptr;
805         char *ODR_record;
806         char *ODR_basename;
807         char *ODR_errstr;
808         int *ODR_oid_buf;
809         oident *oid;
810         WRBUF oid_dotted;
811         Zfront_handle *zhandle = (Zfront_handle *)handle;
812         CV* handler_cv = 0;
813
814         Z_RecordComposition *composition;
815         Z_ElementSetNames *simple;
816         STRLEN length;
817
818         dSP;
819         ENTER;
820         SAVETMPS;
821
822         rr->errcode = 0;
823         href = newHV();
824         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
825         temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
826         if (rr->request_format_raw != 0) {
827             oid_dotted = oid2dotted(rr->request_format_raw);
828         } else {
829             /* Probably an SRU request: assume XML is required */
830             oid_dotted = wrbuf_alloc();
831             wrbuf_puts(oid_dotted, "1.2.840.10003.5.109.10");
832         }
833         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
834         hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
835         hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
836         hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
837         hv_store(href, "LAST", 4, newSViv(0), 0);
838         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
839         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
840         hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
841         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
842         hv_store(href, "PID", 3, newSViv(getpid()), 0);
843         if (rr->comp)
844         {
845                 composition = rr->comp;
846                 if (composition->which == Z_RecordComp_simple)
847                 {
848                         simple = composition->u.simple;
849                         if (simple->which == Z_ElementSetNames_generic)
850                         {
851                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
852                         } 
853                         else
854                         {
855                                 rr->errcode = 26;
856                         }
857                 }
858                 else
859                 {
860                         /* This is where we end up in the case of
861                          * SRU.  Since record composition ("element
862                          * sets") are meaningless in SRU anyway, we
863                          * just skip this.
864                          */
865                 }
866         }
867
868         PUSHMARK(sp);
869
870         XPUSHs(sv_2mortal(newRV( (SV*) href)));
871
872         PUTBACK;
873         
874         handler_cv = simpleserver_sv2cv( fetch_ref );
875         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
876
877         SPAGAIN;
878
879         temp = hv_fetch(href, "BASENAME", 8, 1);
880         basename = newSVsv(*temp);
881
882         temp = hv_fetch(href, "RECORD", 6, 1);
883         record = newSVsv(*temp);
884
885         temp = hv_fetch(href, "LAST", 4, 1);
886         last = newSVsv(*temp);
887
888         temp = hv_fetch(href, "ERR_CODE", 8, 1);
889         err_code = newSVsv(*temp);
890
891         temp = hv_fetch(href, "ERR_STR", 7, 1),
892         err_string = newSVsv(*temp);
893
894         temp = hv_fetch(href, "SUR_FLAG", 8, 1);
895         sur_flag = newSVsv(*temp);
896
897         temp = hv_fetch(href, "REP_FORM", 8, 1);
898         rep_form = newSVsv(*temp);
899
900         temp = hv_fetch(href, "HANDLE", 6, 1);
901         point = newSVsv(*temp);
902
903
904         hv_undef(href);
905         
906         ptr = SvPV(basename, length);
907         ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
908         strcpy(ODR_basename, ptr);
909         rr->basename = ODR_basename;
910
911         ptr = SvPV(rep_form, length);
912         ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
913         if (dotted2oid(ptr, ODR_oid_buf) == -1)         /* Maximum number of OID elements exceeded */
914         {
915                 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
916         }
917         rr->output_format_raw = ODR_oid_buf;    
918         
919         ptr = SvPV(record, length);
920         oid = oid_getentbyoid(ODR_oid_buf);
921         if (oid->value == VAL_GRS1)             /* Treat GRS-1 records separately */
922         {
923                 rr->record = (char *) read_grs1(ptr, rr->stream);
924                 rr->len = -1;
925         }
926         else
927         {
928                 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
929                 strcpy(ODR_record, ptr);
930                 rr->record = ODR_record;
931                 rr->len = length;
932         }
933         zhandle->handle = point;
934         handle = zhandle;
935         rr->last_in_set = SvIV(last);
936         
937         if (!(rr->errcode))
938         {
939                 rr->errcode = SvIV(err_code);
940                 ptr = SvPV(err_string, length);
941                 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
942                 strcpy(ODR_errstr, ptr);
943                 rr->errstring = ODR_errstr;
944         }
945         rr->surrogate_flag = SvIV(sur_flag);
946
947         wrbuf_free(oid_dotted, 1);
948         sv_free((SV*) href);
949         sv_free(basename);
950         sv_free(record);
951         sv_free(last);
952         sv_free(err_string);
953         sv_free(err_code),
954         sv_free(sur_flag);
955         sv_free(rep_form);
956
957         PUTBACK;
958         FREETMPS;
959         LEAVE;
960         
961         return 0;
962 }
963
964
965 int bend_present(void *handle, bend_present_rr *rr)
966 {
967         HV *href;
968         SV **temp;
969         SV *err_code;
970         SV *err_string;
971         SV *hits;
972         SV *point;
973         STRLEN len;
974         Z_RecordComposition *composition;
975         Z_ElementSetNames *simple;
976         char *ODR_errstr;
977         char *ptr;
978         Zfront_handle *zhandle = (Zfront_handle *)handle;
979         CV* handler_cv = 0;
980
981 /*      WRBUF oid_dotted; */
982
983         dSP;
984         ENTER;
985         SAVETMPS;
986
987         href = newHV();
988         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
989         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
990         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
991         hv_store(href, "START", 5, newSViv(rr->start), 0);
992         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
993         hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
994         /*oid_dotted = oid2dotted(rr->request_format_raw);
995         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
996         hv_store(href, "HITS", 4, newSViv(0), 0);
997         hv_store(href, "PID", 3, newSViv(getpid()), 0);
998         if (rr->comp)
999         {
1000                 composition = rr->comp;
1001                 if (composition->which == Z_RecordComp_simple)
1002                 {
1003                         simple = composition->u.simple;
1004                         if (simple->which == Z_ElementSetNames_generic)
1005                         {
1006                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
1007                         } 
1008                         else
1009                         {
1010                                 rr->errcode = 26;
1011                                 return 0;
1012                         }
1013                 }
1014                 else
1015                 {
1016                         rr->errcode = 26;
1017                         return 0;
1018                 }
1019         }
1020
1021         PUSHMARK(sp);
1022         
1023         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1024         
1025         PUTBACK;
1026         
1027         handler_cv = simpleserver_sv2cv( present_ref );
1028         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1029         
1030         SPAGAIN;
1031
1032         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1033         err_code = newSVsv(*temp);
1034
1035         temp = hv_fetch(href, "ERR_STR", 7, 1);
1036         err_string = newSVsv(*temp);
1037
1038         temp = hv_fetch(href, "HITS", 4, 1);
1039         hits = newSVsv(*temp);
1040
1041         temp = hv_fetch(href, "HANDLE", 6, 1);
1042         point = newSVsv(*temp);
1043
1044         PUTBACK;
1045         FREETMPS;
1046         LEAVE;
1047         
1048         hv_undef(href);
1049         rr->errcode = SvIV(err_code);
1050         rr->hits = SvIV(hits);
1051
1052         ptr = SvPV(err_string, len);
1053         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1054         strcpy(ODR_errstr, ptr);
1055         rr->errstring = ODR_errstr;
1056 /*      wrbuf_free(oid_dotted, 1);*/
1057         zhandle->handle = point;
1058         handle = zhandle;
1059         sv_free(err_code);
1060         sv_free(err_string);
1061         sv_free(hits);
1062         sv_free( (SV*) href);
1063
1064         return 0;
1065 }
1066
1067
1068 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1069 {
1070         perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1071         return 0;
1072 }
1073
1074
1075 int bend_delete(void *handle, bend_delete_rr *rr)
1076 {
1077         perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
1078         return 0;
1079 }
1080
1081
1082 int bend_scan(void *handle, bend_scan_rr *rr)
1083 {
1084         HV *href;
1085         AV *aref;
1086         AV *list;
1087         AV *entries;
1088         HV *scan_item;
1089         struct scan_entry *scan_list;
1090         struct scan_entry *buffer;
1091         int *step_size = rr->step_size;
1092         int i;
1093         char **basenames;
1094         SV **temp;
1095         SV *err_code = sv_newmortal();
1096         SV *err_str = sv_newmortal();
1097         SV *point = sv_newmortal();
1098         SV *status = sv_newmortal();
1099         SV *number = sv_newmortal();
1100         char *ptr;
1101         char *ODR_errstr;
1102         STRLEN len;
1103         int term_len;
1104         SV *entries_ref;
1105         Zfront_handle *zhandle = (Zfront_handle *)handle;
1106         CV* handler_cv = 0;
1107
1108         dSP;
1109         ENTER;
1110         SAVETMPS;
1111         href = newHV();
1112         list = newAV();
1113         if (rr->term->term->which == Z_Term_general)
1114         {
1115                 term_len = rr->term->term->u.general->len;
1116                 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1117         } else {
1118                 rr->errcode = 229;      /* Unsupported term type */
1119                 return 0;
1120         }
1121         hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1122         hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1123         hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1124         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1125         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1126         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1127         hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1128         hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1129         aref = newAV();
1130         basenames = rr->basenames;
1131         for (i = 0; i < rr->num_bases; i++)
1132         {
1133                 av_push(aref, newSVpv(*basenames++, 0));
1134         }
1135         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1136
1137         PUSHMARK(sp);
1138
1139         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1140
1141         PUTBACK;
1142
1143         handler_cv = simpleserver_sv2cv( scan_ref );
1144         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1145
1146         SPAGAIN;
1147
1148         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1149         err_code = newSVsv(*temp);
1150
1151         temp = hv_fetch(href, "ERR_STR", 7, 1);
1152         err_str = newSVsv(*temp);
1153
1154         temp = hv_fetch(href, "HANDLE", 6, 1);
1155         point = newSVsv(*temp);
1156
1157         temp = hv_fetch(href, "STATUS", 6, 1);
1158         status = newSVsv(*temp);
1159         
1160         temp = hv_fetch(href, "NUMBER", 6, 1);
1161         number = newSVsv(*temp);
1162
1163         temp = hv_fetch(href, "ENTRIES", 7, 1);
1164         entries_ref = newSVsv(*temp);
1165
1166         PUTBACK;
1167         FREETMPS;
1168         LEAVE;
1169
1170         ptr = SvPV(err_str, len);
1171         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1172         strcpy(ODR_errstr, ptr);
1173         rr->errstring = ODR_errstr;
1174         rr->errcode = SvIV(err_code);
1175         rr->num_entries = SvIV(number);
1176         rr->status = SvIV(status);
1177         scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1178         buffer = scan_list;
1179         entries = (AV *)SvRV(entries_ref);
1180         for (i = 0; i < rr->num_entries; i++)
1181         {
1182                 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1183                 temp = hv_fetch(scan_item, "TERM", 4, 1);
1184                 ptr = SvPV(*temp, len);
1185                 buffer->term = (char *) odr_malloc (rr->stream, len + 1); 
1186                 strcpy(buffer->term, ptr);
1187                 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1); 
1188                 buffer->occurrences = SvIV(*temp);
1189                 buffer++;
1190                 hv_undef(scan_item);
1191         }
1192         rr->entries = scan_list;
1193         zhandle->handle = point;
1194         handle = zhandle;
1195         sv_free(err_code);
1196         sv_free(err_str);
1197         sv_free(status);
1198         sv_free(number);
1199         hv_undef(href);
1200         sv_free((SV *)href);
1201         av_undef(aref);
1202         sv_free((SV *)aref);
1203         av_undef(list);
1204         sv_free((SV *)list);
1205         av_undef(entries);
1206         /*sv_free((SV *)entries);*/
1207         sv_free(entries_ref);
1208
1209         return 0;
1210 }
1211
1212 bend_initresult *bend_init(bend_initrequest *q)
1213 {
1214         int dummy = simpleserver_clone();
1215         bend_initresult *r = (bend_initresult *)
1216                 odr_malloc (q->stream, sizeof(*r));
1217         char *ptr;
1218         CV* handler_cv = 0;
1219         dSP;
1220         STRLEN len;
1221         NMEM nmem = nmem_create();
1222         Zfront_handle *zhandle =  (Zfront_handle *) nmem_malloc (nmem,
1223                         sizeof(*zhandle));
1224         SV *handle;
1225         HV *href;
1226         SV **temp;
1227
1228         ENTER;
1229         SAVETMPS;
1230
1231         zhandle->nmem = nmem;
1232         zhandle->stop_flag = 0;
1233
1234         if (sort_ref)
1235         {
1236             q->bend_sort = bend_sort;
1237         }
1238         if (search_ref)
1239         {
1240                 q->bend_search = bend_search;
1241         }
1242         if (present_ref)
1243         {
1244                 q->bend_present = bend_present;
1245         }
1246         /*q->bend_esrequest = bend_esrequest;*/
1247         /*q->bend_delete = bend_delete;*/
1248         if (fetch_ref)
1249         {
1250                 q->bend_fetch = bend_fetch;
1251         }
1252         if (scan_ref)
1253         {
1254                 q->bend_scan = bend_scan;
1255         }
1256
1257         href = newHV(); 
1258         hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1259         hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1260         hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1261         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1262         hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1263         hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1264         hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1265         hv_store(href, "PID", 3, newSViv(getpid()), 0);
1266         if (q->auth) {
1267             char *user = NULL;
1268             char *passwd = NULL;
1269             if (q->auth->which == Z_IdAuthentication_open) {
1270                 char *cp;
1271                 user = nmem_strdup (odr_getmem (q->stream), q->auth->u.open);
1272                 cp = strchr (user, '/');
1273                 if (cp) {
1274                     /* password after / given */
1275                     *cp = '\0';
1276                     passwd = cp+1;
1277                 }
1278             } else if (q->auth->which == Z_IdAuthentication_idPass) {
1279                 user = q->auth->u.idPass->userId;
1280                 passwd = q->auth->u.idPass->password;
1281             }
1282             /* ### some code paths have user/password unassigned here */
1283             if (user)
1284                 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1285             if (passwd)
1286                 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1287         }
1288
1289         PUSHMARK(sp);   
1290
1291         XPUSHs(sv_2mortal(newRV((SV*) href)));
1292
1293         PUTBACK;
1294
1295         if (init_ref != NULL)
1296         {
1297              handler_cv = simpleserver_sv2cv( init_ref );
1298              perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1299         }
1300
1301         SPAGAIN;
1302
1303         temp = hv_fetch(href, "IMP_ID", 6, 1);
1304         ptr = SvPV(*temp, len);
1305         q->implementation_id = nmem_strdup(nmem, ptr);
1306
1307         temp = hv_fetch(href, "IMP_NAME", 8, 1);
1308         ptr = SvPV(*temp, len);
1309         q->implementation_name = nmem_strdup(nmem, ptr);
1310
1311         temp = hv_fetch(href, "IMP_VER", 7, 1);
1312         ptr = SvPV(*temp, len);
1313         q->implementation_version = nmem_strdup(nmem, ptr);
1314
1315         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1316         r->errcode = SvIV(*temp);
1317
1318         temp = hv_fetch(href, "ERR_STR", 7, 1);
1319         ptr = SvPV(*temp, len);
1320         r->errstring = (char *)odr_malloc(q->stream, len + 1);
1321         strcpy(r->errstring, ptr);
1322
1323         temp = hv_fetch(href, "HANDLE", 6, 1);
1324         handle= newSVsv(*temp);
1325         zhandle->handle = handle;
1326
1327         r->handle = zhandle;
1328
1329         hv_undef(href);
1330         sv_free((SV*) href);
1331
1332         PUTBACK;
1333         FREETMPS;
1334         LEAVE;
1335         
1336         return r;       
1337 }
1338
1339 void bend_close(void *handle)
1340 {
1341         HV *href;
1342         Zfront_handle *zhandle = (Zfront_handle *)handle;
1343         CV* handler_cv = 0;
1344         int stop_flag = 0;
1345         dSP;
1346         ENTER;
1347         SAVETMPS;
1348
1349         if (close_ref)
1350         {
1351                 href = newHV();
1352                 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1353
1354                 PUSHMARK(sp);
1355
1356                 XPUSHs(sv_2mortal(newRV((SV *)href)));
1357
1358                 PUTBACK;
1359         
1360                 handler_cv = simpleserver_sv2cv( close_ref );
1361                 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1362         
1363                 SPAGAIN;
1364
1365                 sv_free((SV*) href);
1366         }
1367         else
1368                 sv_free(zhandle->handle);
1369         PUTBACK;
1370         FREETMPS;
1371         LEAVE;
1372         stop_flag = zhandle->stop_flag;
1373         nmem_destroy(zhandle->nmem);
1374         simpleserver_free();
1375
1376         if (stop_flag)
1377                 exit(0);
1378         return;
1379 }
1380
1381
1382 MODULE = Net::Z3950::SimpleServer       PACKAGE = Net::Z3950::SimpleServer
1383
1384 PROTOTYPES: DISABLE
1385
1386
1387 void
1388 set_init_handler(arg)
1389                 SV *arg
1390         CODE:
1391                 init_ref = newSVsv(arg);
1392                 
1393
1394 void
1395 set_close_handler(arg)
1396                 SV *arg
1397         CODE:
1398                 close_ref = newSVsv(arg);
1399
1400
1401 void
1402 set_sort_handler(arg)
1403                 SV *arg
1404         CODE:
1405                 sort_ref = newSVsv(arg);
1406
1407 void
1408 set_search_handler(arg)
1409                 SV *arg
1410         CODE:
1411                 search_ref = newSVsv(arg);
1412
1413
1414 void
1415 set_fetch_handler(arg)
1416                 SV *arg
1417         CODE:
1418                 fetch_ref = newSVsv(arg);
1419
1420
1421 void
1422 set_present_handler(arg)
1423                 SV *arg
1424         CODE:
1425                 present_ref = newSVsv(arg);
1426
1427
1428 void
1429 set_esrequest_handler(arg)
1430                 SV *arg
1431         CODE:
1432                 esrequest_ref = newSVsv(arg);
1433
1434
1435 void
1436 set_delete_handler(arg)
1437                 SV *arg
1438         CODE:
1439                 delete_ref = newSVsv(arg);
1440
1441
1442 void
1443 set_scan_handler(arg)
1444                 SV *arg
1445         CODE:
1446                 scan_ref = newSVsv(arg);
1447
1448
1449 int
1450 start_server(...)
1451         PREINIT:
1452                 char **argv;
1453                 char **argv_buf;
1454                 char *ptr;
1455                 int i;
1456                 STRLEN len;
1457         CODE:
1458                 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1459                 argv = argv_buf;
1460                 for (i = 0; i < items; i++)
1461                 {
1462                         ptr = SvPV(ST(i), len);
1463                         *argv_buf = (char *)xmalloc(len + 1);
1464                         strcpy(*argv_buf++, ptr); 
1465                 }
1466                 *argv_buf = NULL;
1467                 root_perl_context = PERL_GET_CONTEXT;
1468                 nmem_mutex_create(&simpleserver_mutex);
1469 #if 0
1470                 /* only for debugging perl_clone .. */
1471                 tst_clones();
1472 #endif
1473                 
1474                 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1475         OUTPUT:
1476                 RETVAL
1477
1478
1479 int
1480 ScanSuccess()
1481         CODE:
1482                 RETVAL = BEND_SCAN_SUCCESS;
1483         OUTPUT:
1484                 RETVAL
1485
1486 int
1487 ScanPartial()
1488         CODE:
1489                 RETVAL = BEND_SCAN_PARTIAL;
1490         OUTPUT:
1491                 RETVAL
1492
1493  
1494 void
1495 yazlog(arg)
1496                 SV *arg
1497         CODE:
1498                 STRLEN len;
1499                 char *ptr;
1500                 ptr = SvPV(arg, len);
1501                 yaz_log(YLOG_LOG, "%.*s", len, ptr);