a09403f2858fe9671b18c835052c6c3a96840def
[simpleserver-moved-to-github.git] / SimpleServer.xs
1 /*
2  * $Id: SimpleServer.xs,v 1.43 2006-04-21 07:30:15 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             return 0;
493         }
494         else
495         {
496             return 0;
497         }
498     }
499     else
500     {
501         return 0;
502     }
503
504     return 1;
505 }
506
507
508 static SV *zquery2perl(Z_Query *q)
509 {
510     SV *sv;
511     HV *hv;
512
513     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
514         return 0;
515     sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
516     if (q->u.type_1->attributeSetId)
517         setMember(hv, "attributeSet",
518                   translateOID(q->u.type_1->attributeSetId));
519     setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
520     return sv;
521 }
522
523
524 int bend_sort(void *handle, bend_sort_rr *rr)
525 {
526         HV *href;
527         AV *aref;
528         AV *sort_seq;
529         SV **temp;
530         SV *err_code;
531         SV *err_str;
532         SV *status;
533         SV *point;
534         STRLEN len;
535         char *ptr;
536         char *ODR_err_str;
537         char **input_setnames;
538         Zfront_handle *zhandle = (Zfront_handle *)handle;
539         Z_SortKeySpecList *sort_spec = rr->sort_sequence;
540         int i;
541         
542         dSP;
543         ENTER;
544         SAVETMPS;
545         
546         aref = newAV();
547         input_setnames = rr->input_setnames;
548         for (i = 0; i < rr->num_input_setnames; i++)
549         {
550             av_push(aref, newSVpv(*input_setnames++, 0));
551         }
552
553         sort_seq = newAV();
554         for (i = 0; i < sort_spec->num_specs; i++)
555         {
556             Z_SortKeySpec *spec = *sort_spec->specs++;
557             HV *sort_spec = newHV();
558
559             if ( simpleserver_SortKeySpecToHash(sort_spec, spec) )
560                 av_push(sort_seq, newRV( sv_2mortal( (SV*) sort_spec ) ));
561             else
562             {
563                 rr->errcode = 207;
564                 return 0;
565             }
566         }
567         
568         href = newHV();
569         hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
570         hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
571         hv_store(href, "SEQUENCE", 8, newRV( (SV*) sort_seq), 0);
572         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
573         hv_store(href, "STATUS", 6, newSViv(0), 0);
574         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
575         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
576
577         PUSHMARK(sp);
578
579         XPUSHs(sv_2mortal(newRV( (SV*) href)));
580
581         PUTBACK;
582
583         perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
584
585         SPAGAIN;
586
587         temp = hv_fetch(href, "ERR_CODE", 8, 1);
588         err_code = newSVsv(*temp);
589
590         temp = hv_fetch(href, "ERR_STR", 7, 1);
591         err_str = newSVsv(*temp);
592
593         temp = hv_fetch(href, "STATUS", 6, 1);
594         status = newSVsv(*temp);
595
596         temp = hv_fetch(href, "HANDLE", 6, 1);
597         point = newSVsv(*temp);
598
599         hv_undef(href);
600         av_undef(aref);
601         av_undef(sort_seq);
602        
603         sv_free( (SV*) aref);
604         sv_free( (SV*) href);
605         sv_free( (SV*) sort_seq);
606
607         rr->errcode = SvIV(err_code);
608         rr->sort_status = SvIV(status);
609         
610         ptr = SvPV(err_str, len);
611         ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
612         strcpy(ODR_err_str, ptr);
613         rr->errstring = ODR_err_str;
614         zhandle->handle = point;
615
616         sv_free(err_code);
617         sv_free(err_str);
618         sv_free(status);
619         
620         PUTBACK;
621         FREETMPS;
622         LEAVE;
623
624         return 0;
625 }
626
627
628 int bend_search(void *handle, bend_search_rr *rr)
629 {
630         HV *href;
631         AV *aref;
632         SV **temp;
633         char *ODR_errstr;
634         STRLEN len;
635         int i;
636         char **basenames;
637         WRBUF query;
638         char *ptr;
639         SV *point;
640         Zfront_handle *zhandle = (Zfront_handle *)handle;
641         CV* handler_cv = 0;
642         SV *rpnSV;
643
644         dSP;
645         ENTER;
646         SAVETMPS;
647
648         aref = newAV();
649         basenames = rr->basenames;
650         for (i = 0; i < rr->num_bases; i++)
651         {
652                 av_push(aref, newSVpv(*basenames++, 0));
653         }
654 #if ENABLE_STOP_SERVER
655         if (rr->num_bases == 1 && !strcmp(rr->basenames[0], "XXstop"))
656         {
657                 zhandle->stop_flag = 1;
658         }
659 #endif
660         href = newHV();         
661         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
662         hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
663         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
664         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
665         hv_store(href, "HITS", 4, newSViv(0), 0);
666         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
667         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
668         hv_store(href, "PID", 3, newSViv(getpid()), 0);
669         if ((rpnSV = zquery2perl(rr->query)) != 0) {
670             hv_store(href, "RPN", 3, rpnSV, 0);
671         }
672         query = zquery2pquery(rr->query);
673         if (query)
674         {
675                 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
676         }
677         else if (rr->query->which == Z_Query_type_104 &&
678                  rr->query->u.type_104->which == Z_External_CQL) {
679             hv_store(href, "CQL", 3,
680                      newSVpv(rr->query->u.type_104->u.cql, 0), 0);
681         }
682         else
683         {       
684                 rr->errcode = 108;
685                 return 0;
686         }
687         PUSHMARK(sp);
688         
689         XPUSHs(sv_2mortal(newRV( (SV*) href)));
690         
691         PUTBACK;
692
693         handler_cv = simpleserver_sv2cv( search_ref );
694         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
695
696         SPAGAIN;
697
698         temp = hv_fetch(href, "HITS", 4, 1);
699         rr->hits = SvIV(*temp);
700
701         temp = hv_fetch(href, "ERR_CODE", 8, 1);
702         rr->errcode = SvIV(*temp);
703
704         temp = hv_fetch(href, "ERR_STR", 7, 1);
705         ptr = SvPV(*temp, len);
706         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
707         strcpy(ODR_errstr, ptr);
708         rr->errstring = ODR_errstr;
709
710         temp = hv_fetch(href, "HANDLE", 6, 1);
711         point = newSVsv(*temp);
712
713         hv_undef(href);
714         av_undef(aref);
715
716         zhandle->handle = point;
717         sv_free( (SV*) aref);
718         sv_free( (SV*) href);
719         if (query)
720             wrbuf_free(query, 1);
721         PUTBACK;
722         FREETMPS;
723         LEAVE;
724         return 0;
725 }
726
727
728 /* ### this is worryingly similar to oid2str() */
729 WRBUF oid2dotted(int *oid)
730 {
731
732         WRBUF buf = wrbuf_alloc();
733         int dot = 0;
734
735         for (; *oid != -1 ; oid++)
736         {
737                 char ibuf[16];
738                 if (dot)
739                 {
740                         wrbuf_putc(buf, '.');
741                 }
742                 else
743                 {
744                         dot = 1;
745                 }
746                 sprintf(ibuf, "%d", *oid);
747                 wrbuf_puts(buf, ibuf);
748         }
749         return buf;
750 }
751                 
752
753 int dotted2oid(char *dotted, int *buffer)
754 {
755         int *oid;
756         char ibuf[16];
757         char *ptr;
758         int n = 0;
759
760         ptr = ibuf;
761         oid = buffer;
762         while (*dotted)
763         {
764                 if (*dotted == '.')
765                 {
766                         n++;
767                         if (n == MAX_OID)  /* Terminate if more than MAX_OID entries */
768                         {
769                                 *oid = -1;
770                                 return -1;
771                         }
772                         *ptr = 0;
773                         sscanf(ibuf, "%d", oid++);
774                         ptr = ibuf;
775                         dotted++;
776
777                 }
778                 else
779                 {
780                         *ptr++ = *dotted++;
781                 }
782         }
783         if (n < MAX_OID)
784         {
785                 *ptr = 0;
786                 sscanf(ibuf, "%d", oid++);
787         }
788         *oid = -1;
789         return 0;
790 }
791
792
793 int bend_fetch(void *handle, bend_fetch_rr *rr)
794 {
795         HV *href;
796         SV **temp;
797         SV *basename;
798         SV *record;
799         SV *last;
800         SV *err_code;
801         SV *err_string;
802         SV *sur_flag;
803         SV *point;
804         SV *rep_form;
805         char *ptr;
806         char *ODR_record;
807         char *ODR_basename;
808         char *ODR_errstr;
809         int *ODR_oid_buf;
810         oident *oid;
811         WRBUF oid_dotted;
812         Zfront_handle *zhandle = (Zfront_handle *)handle;
813         CV* handler_cv = 0;
814
815         Z_RecordComposition *composition;
816         Z_ElementSetNames *simple;
817         STRLEN length;
818
819         dSP;
820         ENTER;
821         SAVETMPS;
822
823         rr->errcode = 0;
824         href = newHV();
825         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
826         temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
827         if (rr->request_format_raw != 0) {
828             oid_dotted = oid2dotted(rr->request_format_raw);
829         } else {
830             /* Probably an SRU request: assume XML is required */
831             oid_dotted = wrbuf_alloc();
832             wrbuf_puts(oid_dotted, "1.2.840.10003.5.109.10");
833         }
834         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
835         hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
836         hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
837         hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
838         hv_store(href, "LAST", 4, newSViv(0), 0);
839         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
840         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
841         hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
842         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
843         hv_store(href, "PID", 3, newSViv(getpid()), 0);
844         if (rr->comp)
845         {
846                 composition = rr->comp;
847                 if (composition->which == Z_RecordComp_simple)
848                 {
849                         simple = composition->u.simple;
850                         if (simple->which == Z_ElementSetNames_generic)
851                         {
852                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
853                         } 
854                         else
855                         {
856                                 rr->errcode = 26;
857                         }
858                 }
859                 else
860                 {
861                         /* This is where we end up in the case of
862                          * SRU.  Since record composition ("element
863                          * sets") are meaningless in SRU anyway, we
864                          * just skip this.
865                          */
866                 }
867         }
868
869         PUSHMARK(sp);
870
871         XPUSHs(sv_2mortal(newRV( (SV*) href)));
872
873         PUTBACK;
874         
875         handler_cv = simpleserver_sv2cv( fetch_ref );
876         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
877
878         SPAGAIN;
879
880         temp = hv_fetch(href, "BASENAME", 8, 1);
881         basename = newSVsv(*temp);
882
883         temp = hv_fetch(href, "RECORD", 6, 1);
884         record = newSVsv(*temp);
885
886         temp = hv_fetch(href, "LAST", 4, 1);
887         last = newSVsv(*temp);
888
889         temp = hv_fetch(href, "ERR_CODE", 8, 1);
890         err_code = newSVsv(*temp);
891
892         temp = hv_fetch(href, "ERR_STR", 7, 1),
893         err_string = newSVsv(*temp);
894
895         temp = hv_fetch(href, "SUR_FLAG", 8, 1);
896         sur_flag = newSVsv(*temp);
897
898         temp = hv_fetch(href, "REP_FORM", 8, 1);
899         rep_form = newSVsv(*temp);
900
901         temp = hv_fetch(href, "HANDLE", 6, 1);
902         point = newSVsv(*temp);
903
904
905         hv_undef(href);
906         
907         ptr = SvPV(basename, length);
908         ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
909         strcpy(ODR_basename, ptr);
910         rr->basename = ODR_basename;
911
912         ptr = SvPV(rep_form, length);
913         ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
914         if (dotted2oid(ptr, ODR_oid_buf) == -1)         /* Maximum number of OID elements exceeded */
915         {
916                 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
917         }
918         rr->output_format_raw = ODR_oid_buf;    
919         
920         ptr = SvPV(record, length);
921         oid = oid_getentbyoid(ODR_oid_buf);
922         if (oid->value == VAL_GRS1)             /* Treat GRS-1 records separately */
923         {
924                 rr->record = (char *) read_grs1(ptr, rr->stream);
925                 rr->len = -1;
926         }
927         else
928         {
929                 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
930                 strcpy(ODR_record, ptr);
931                 rr->record = ODR_record;
932                 rr->len = length;
933         }
934         zhandle->handle = point;
935         handle = zhandle;
936         rr->last_in_set = SvIV(last);
937         
938         if (!(rr->errcode))
939         {
940                 rr->errcode = SvIV(err_code);
941                 ptr = SvPV(err_string, length);
942                 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
943                 strcpy(ODR_errstr, ptr);
944                 rr->errstring = ODR_errstr;
945         }
946         rr->surrogate_flag = SvIV(sur_flag);
947
948         wrbuf_free(oid_dotted, 1);
949         sv_free((SV*) href);
950         sv_free(basename);
951         sv_free(record);
952         sv_free(last);
953         sv_free(err_string);
954         sv_free(err_code),
955         sv_free(sur_flag);
956         sv_free(rep_form);
957
958         PUTBACK;
959         FREETMPS;
960         LEAVE;
961         
962         return 0;
963 }
964
965
966 int bend_present(void *handle, bend_present_rr *rr)
967 {
968         HV *href;
969         SV **temp;
970         SV *err_code;
971         SV *err_string;
972         SV *hits;
973         SV *point;
974         STRLEN len;
975         Z_RecordComposition *composition;
976         Z_ElementSetNames *simple;
977         char *ODR_errstr;
978         char *ptr;
979         Zfront_handle *zhandle = (Zfront_handle *)handle;
980         CV* handler_cv = 0;
981
982 /*      WRBUF oid_dotted; */
983
984         dSP;
985         ENTER;
986         SAVETMPS;
987
988         href = newHV();
989         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
990         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
991         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
992         hv_store(href, "START", 5, newSViv(rr->start), 0);
993         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
994         hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
995         /*oid_dotted = oid2dotted(rr->request_format_raw);
996         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
997         hv_store(href, "HITS", 4, newSViv(0), 0);
998         hv_store(href, "PID", 3, newSViv(getpid()), 0);
999         if (rr->comp)
1000         {
1001                 composition = rr->comp;
1002                 if (composition->which == Z_RecordComp_simple)
1003                 {
1004                         simple = composition->u.simple;
1005                         if (simple->which == Z_ElementSetNames_generic)
1006                         {
1007                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
1008                         } 
1009                         else
1010                         {
1011                                 rr->errcode = 26;
1012                                 return 0;
1013                         }
1014                 }
1015                 else
1016                 {
1017                         rr->errcode = 26;
1018                         return 0;
1019                 }
1020         }
1021
1022         PUSHMARK(sp);
1023         
1024         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1025         
1026         PUTBACK;
1027         
1028         handler_cv = simpleserver_sv2cv( present_ref );
1029         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1030         
1031         SPAGAIN;
1032
1033         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1034         err_code = newSVsv(*temp);
1035
1036         temp = hv_fetch(href, "ERR_STR", 7, 1);
1037         err_string = newSVsv(*temp);
1038
1039         temp = hv_fetch(href, "HITS", 4, 1);
1040         hits = newSVsv(*temp);
1041
1042         temp = hv_fetch(href, "HANDLE", 6, 1);
1043         point = newSVsv(*temp);
1044
1045         PUTBACK;
1046         FREETMPS;
1047         LEAVE;
1048         
1049         hv_undef(href);
1050         rr->errcode = SvIV(err_code);
1051         rr->hits = SvIV(hits);
1052
1053         ptr = SvPV(err_string, len);
1054         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1055         strcpy(ODR_errstr, ptr);
1056         rr->errstring = ODR_errstr;
1057 /*      wrbuf_free(oid_dotted, 1);*/
1058         zhandle->handle = point;
1059         handle = zhandle;
1060         sv_free(err_code);
1061         sv_free(err_string);
1062         sv_free(hits);
1063         sv_free( (SV*) href);
1064
1065         return 0;
1066 }
1067
1068
1069 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1070 {
1071         perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1072         return 0;
1073 }
1074
1075
1076 int bend_delete(void *handle, bend_delete_rr *rr)
1077 {
1078         perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
1079         return 0;
1080 }
1081
1082
1083 int bend_scan(void *handle, bend_scan_rr *rr)
1084 {
1085         HV *href;
1086         AV *aref;
1087         AV *list;
1088         AV *entries;
1089         HV *scan_item;
1090         struct scan_entry *scan_list;
1091         struct scan_entry *buffer;
1092         int *step_size = rr->step_size;
1093         int i;
1094         char **basenames;
1095         SV **temp;
1096         SV *err_code = sv_newmortal();
1097         SV *err_str = sv_newmortal();
1098         SV *point = sv_newmortal();
1099         SV *status = sv_newmortal();
1100         SV *number = sv_newmortal();
1101         char *ptr;
1102         char *ODR_errstr;
1103         STRLEN len;
1104         int term_len;
1105         SV *entries_ref;
1106         Zfront_handle *zhandle = (Zfront_handle *)handle;
1107         CV* handler_cv = 0;
1108
1109         dSP;
1110         ENTER;
1111         SAVETMPS;
1112         href = newHV();
1113         list = newAV();
1114         if (rr->term->term->which == Z_Term_general)
1115         {
1116                 term_len = rr->term->term->u.general->len;
1117                 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1118         } else {
1119                 rr->errcode = 229;      /* Unsupported term type */
1120                 return 0;
1121         }
1122         hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1123         hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1124         hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1125         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1126         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1127         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1128         hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1129         hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1130         aref = newAV();
1131         basenames = rr->basenames;
1132         for (i = 0; i < rr->num_bases; i++)
1133         {
1134                 av_push(aref, newSVpv(*basenames++, 0));
1135         }
1136         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1137
1138         PUSHMARK(sp);
1139
1140         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1141
1142         PUTBACK;
1143
1144         handler_cv = simpleserver_sv2cv( scan_ref );
1145         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1146
1147         SPAGAIN;
1148
1149         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1150         err_code = newSVsv(*temp);
1151
1152         temp = hv_fetch(href, "ERR_STR", 7, 1);
1153         err_str = newSVsv(*temp);
1154
1155         temp = hv_fetch(href, "HANDLE", 6, 1);
1156         point = newSVsv(*temp);
1157
1158         temp = hv_fetch(href, "STATUS", 6, 1);
1159         status = newSVsv(*temp);
1160         
1161         temp = hv_fetch(href, "NUMBER", 6, 1);
1162         number = newSVsv(*temp);
1163
1164         temp = hv_fetch(href, "ENTRIES", 7, 1);
1165         entries_ref = newSVsv(*temp);
1166
1167         PUTBACK;
1168         FREETMPS;
1169         LEAVE;
1170
1171         ptr = SvPV(err_str, len);
1172         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1173         strcpy(ODR_errstr, ptr);
1174         rr->errstring = ODR_errstr;
1175         rr->errcode = SvIV(err_code);
1176         rr->num_entries = SvIV(number);
1177         rr->status = SvIV(status);
1178         scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1179         buffer = scan_list;
1180         entries = (AV *)SvRV(entries_ref);
1181         for (i = 0; i < rr->num_entries; i++)
1182         {
1183                 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1184                 temp = hv_fetch(scan_item, "TERM", 4, 1);
1185                 ptr = SvPV(*temp, len);
1186                 buffer->term = (char *) odr_malloc (rr->stream, len + 1); 
1187                 strcpy(buffer->term, ptr);
1188                 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1); 
1189                 buffer->occurrences = SvIV(*temp);
1190                 buffer++;
1191                 hv_undef(scan_item);
1192         }
1193         rr->entries = scan_list;
1194         zhandle->handle = point;
1195         handle = zhandle;
1196         sv_free(err_code);
1197         sv_free(err_str);
1198         sv_free(status);
1199         sv_free(number);
1200         hv_undef(href);
1201         sv_free((SV *)href);
1202         av_undef(aref);
1203         sv_free((SV *)aref);
1204         av_undef(list);
1205         sv_free((SV *)list);
1206         av_undef(entries);
1207         /*sv_free((SV *)entries);*/
1208         sv_free(entries_ref);
1209
1210         return 0;
1211 }
1212
1213 bend_initresult *bend_init(bend_initrequest *q)
1214 {
1215         int dummy = simpleserver_clone();
1216         bend_initresult *r = (bend_initresult *)
1217                 odr_malloc (q->stream, sizeof(*r));
1218         char *ptr;
1219         CV* handler_cv = 0;
1220         dSP;
1221         STRLEN len;
1222         NMEM nmem = nmem_create();
1223         Zfront_handle *zhandle =  (Zfront_handle *) nmem_malloc (nmem,
1224                         sizeof(*zhandle));
1225         SV *handle;
1226         HV *href;
1227         SV **temp;
1228
1229         ENTER;
1230         SAVETMPS;
1231
1232         zhandle->nmem = nmem;
1233         zhandle->stop_flag = 0;
1234
1235         if (sort_ref)
1236         {
1237             q->bend_sort = bend_sort;
1238         }
1239         if (search_ref)
1240         {
1241                 q->bend_search = bend_search;
1242         }
1243         if (present_ref)
1244         {
1245                 q->bend_present = bend_present;
1246         }
1247         /*q->bend_esrequest = bend_esrequest;*/
1248         /*q->bend_delete = bend_delete;*/
1249         if (fetch_ref)
1250         {
1251                 q->bend_fetch = bend_fetch;
1252         }
1253         if (scan_ref)
1254         {
1255                 q->bend_scan = bend_scan;
1256         }
1257
1258         href = newHV(); 
1259         hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1260         hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1261         hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1262         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1263         hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1264         hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1265         hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1266         hv_store(href, "PID", 3, newSViv(getpid()), 0);
1267         if (q->auth) {
1268             char *user = NULL;
1269             char *passwd = NULL;
1270             if (q->auth->which == Z_IdAuthentication_open) {
1271                 char *cp;
1272                 user = nmem_strdup (odr_getmem (q->stream), q->auth->u.open);
1273                 cp = strchr (user, '/');
1274                 if (cp) {
1275                     /* password after / given */
1276                     *cp = '\0';
1277                     passwd = cp+1;
1278                 }
1279             } else if (q->auth->which == Z_IdAuthentication_idPass) {
1280                 user = q->auth->u.idPass->userId;
1281                 passwd = q->auth->u.idPass->password;
1282             }
1283             /* ### some code paths have user/password unassigned here */
1284             if (user)
1285                 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1286             if (passwd)
1287                 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1288         }
1289
1290         PUSHMARK(sp);   
1291
1292         XPUSHs(sv_2mortal(newRV((SV*) href)));
1293
1294         PUTBACK;
1295
1296         if (init_ref != NULL)
1297         {
1298              handler_cv = simpleserver_sv2cv( init_ref );
1299              perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1300         }
1301
1302         SPAGAIN;
1303
1304         temp = hv_fetch(href, "IMP_ID", 6, 1);
1305         ptr = SvPV(*temp, len);
1306         q->implementation_id = nmem_strdup(nmem, ptr);
1307
1308         temp = hv_fetch(href, "IMP_NAME", 8, 1);
1309         ptr = SvPV(*temp, len);
1310         q->implementation_name = nmem_strdup(nmem, ptr);
1311
1312         temp = hv_fetch(href, "IMP_VER", 7, 1);
1313         ptr = SvPV(*temp, len);
1314         q->implementation_version = nmem_strdup(nmem, ptr);
1315
1316         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1317         r->errcode = SvIV(*temp);
1318
1319         temp = hv_fetch(href, "ERR_STR", 7, 1);
1320         ptr = SvPV(*temp, len);
1321         r->errstring = (char *)odr_malloc(q->stream, len + 1);
1322         strcpy(r->errstring, ptr);
1323
1324         temp = hv_fetch(href, "HANDLE", 6, 1);
1325         handle= newSVsv(*temp);
1326         zhandle->handle = handle;
1327
1328         r->handle = zhandle;
1329
1330         hv_undef(href);
1331         sv_free((SV*) href);
1332
1333         PUTBACK;
1334         FREETMPS;
1335         LEAVE;
1336         
1337         return r;       
1338 }
1339
1340 void bend_close(void *handle)
1341 {
1342         HV *href;
1343         Zfront_handle *zhandle = (Zfront_handle *)handle;
1344         CV* handler_cv = 0;
1345         int stop_flag = 0;
1346         dSP;
1347         ENTER;
1348         SAVETMPS;
1349
1350         if (close_ref)
1351         {
1352                 href = newHV();
1353                 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1354
1355                 PUSHMARK(sp);
1356
1357                 XPUSHs(sv_2mortal(newRV((SV *)href)));
1358
1359                 PUTBACK;
1360         
1361                 handler_cv = simpleserver_sv2cv( close_ref );
1362                 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1363         
1364                 SPAGAIN;
1365
1366                 sv_free((SV*) href);
1367         }
1368         else
1369                 sv_free(zhandle->handle);
1370         PUTBACK;
1371         FREETMPS;
1372         LEAVE;
1373         stop_flag = zhandle->stop_flag;
1374         nmem_destroy(zhandle->nmem);
1375         simpleserver_free();
1376
1377         if (stop_flag)
1378                 exit(0);
1379         return;
1380 }
1381
1382
1383 MODULE = Net::Z3950::SimpleServer       PACKAGE = Net::Z3950::SimpleServer
1384
1385 PROTOTYPES: DISABLE
1386
1387
1388 void
1389 set_init_handler(arg)
1390                 SV *arg
1391         CODE:
1392                 init_ref = newSVsv(arg);
1393                 
1394
1395 void
1396 set_close_handler(arg)
1397                 SV *arg
1398         CODE:
1399                 close_ref = newSVsv(arg);
1400
1401
1402 void
1403 set_sort_handler(arg)
1404                 SV *arg
1405         CODE:
1406                 sort_ref = newSVsv(arg);
1407
1408 void
1409 set_search_handler(arg)
1410                 SV *arg
1411         CODE:
1412                 search_ref = newSVsv(arg);
1413
1414
1415 void
1416 set_fetch_handler(arg)
1417                 SV *arg
1418         CODE:
1419                 fetch_ref = newSVsv(arg);
1420
1421
1422 void
1423 set_present_handler(arg)
1424                 SV *arg
1425         CODE:
1426                 present_ref = newSVsv(arg);
1427
1428
1429 void
1430 set_esrequest_handler(arg)
1431                 SV *arg
1432         CODE:
1433                 esrequest_ref = newSVsv(arg);
1434
1435
1436 void
1437 set_delete_handler(arg)
1438                 SV *arg
1439         CODE:
1440                 delete_ref = newSVsv(arg);
1441
1442
1443 void
1444 set_scan_handler(arg)
1445                 SV *arg
1446         CODE:
1447                 scan_ref = newSVsv(arg);
1448
1449
1450 int
1451 start_server(...)
1452         PREINIT:
1453                 char **argv;
1454                 char **argv_buf;
1455                 char *ptr;
1456                 int i;
1457                 STRLEN len;
1458         CODE:
1459                 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1460                 argv = argv_buf;
1461                 for (i = 0; i < items; i++)
1462                 {
1463                         ptr = SvPV(ST(i), len);
1464                         *argv_buf = (char *)xmalloc(len + 1);
1465                         strcpy(*argv_buf++, ptr); 
1466                 }
1467                 *argv_buf = NULL;
1468                 root_perl_context = PERL_GET_CONTEXT;
1469                 nmem_mutex_create(&simpleserver_mutex);
1470 #if 0
1471                 /* only for debugging perl_clone .. */
1472                 tst_clones();
1473 #endif
1474                 
1475                 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1476         OUTPUT:
1477                 RETVAL
1478
1479
1480 int
1481 ScanSuccess()
1482         CODE:
1483                 RETVAL = BEND_SCAN_SUCCESS;
1484         OUTPUT:
1485                 RETVAL
1486
1487 int
1488 ScanPartial()
1489         CODE:
1490                 RETVAL = BEND_SCAN_PARTIAL;
1491         OUTPUT:
1492                 RETVAL
1493
1494  
1495 void
1496 yazlog(arg)
1497                 SV *arg
1498         CODE:
1499                 STRLEN len;
1500                 char *ptr;
1501                 ptr = SvPV(arg, len);
1502                 yaz_log(YLOG_LOG, "%.*s", len, ptr);