Work on facets complete, except documentation
[simpleserver-moved-to-github.git] / SimpleServer.xs
1 /* This file is part of simpleserver.
2  * Copyright (C) 2000-2011 Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "EXTERN.h"
29 #include "perl.h"
30 #include "proto.h"
31 #include "embed.h"
32 #include "XSUB.h"
33 #include <assert.h>
34 #include <yaz/backend.h>
35 #include <yaz/facet.h>
36 #include <yaz/log.h>
37 #include <yaz/wrbuf.h>
38 #include <yaz/pquery.h>
39 #include <yaz/querytowrbuf.h>
40 #include <stdio.h>
41 #include <yaz/mutex.h>
42 #include <yaz/oid_db.h>
43 #ifdef WIN32
44 #else
45 #include <unistd.h>
46 #endif
47 #include <stdlib.h>
48 #include <ctype.h>
49 #define GRS_MAX_FIELDS 500 
50 #ifdef ASN_COMPILED
51 #include <yaz/ill.h>
52 #endif
53 #ifndef sv_undef                /* To fix the problem with Perl 5.6.0 */
54 #define sv_undef PL_sv_undef
55 #endif
56
57 YAZ_MUTEX simpleserver_mutex;
58
59 typedef struct {
60         SV *ghandle;    /* Global handle specified at creation */
61         SV *handle;     /* Per-connection handle set at Init */
62 #if 0
63 /* ### These callback-reference elements are never used! */
64         SV *init_ref;
65         SV *close_ref;
66         SV *sort_ref;
67         SV *search_ref;
68         SV *fetch_ref;
69         SV *present_ref;
70         SV *esrequest_ref;
71         SV *delete_ref;
72         SV *scan_ref;
73         SV *explain_ref;
74 #endif /*0*/
75         NMEM nmem;
76         int stop_flag;  /* is used to stop server prematurely .. */
77 } Zfront_handle;
78
79 #define ENABLE_STOP_SERVER 0
80
81 SV *_global_ghandle = NULL; /* To be copied into zhandle then ignored */
82 SV *init_ref = NULL;
83 SV *close_ref = NULL;
84 SV *sort_ref = NULL;
85 SV *search_ref = NULL;
86 SV *fetch_ref = NULL;
87 SV *present_ref = NULL;
88 SV *esrequest_ref = NULL;
89 SV *delete_ref = NULL;
90 SV *scan_ref = NULL;
91 SV *explain_ref = NULL;
92 PerlInterpreter *root_perl_context;
93
94 #define GRS_BUF_SIZE 8192
95
96
97 /*
98  * Inspects the SV indicated by svp, and returns a null pointer if
99  * it's an undefined value, or a string allocation from `stream'
100  * otherwise.  Using this when filling in addinfo avoids those
101  * irritating "Use of uninitialized value in subroutine entry"
102  * warnings from Perl.
103  */
104 char *string_or_undef(SV **svp, ODR stream) {
105         STRLEN len;
106         char *ptr, *buf;
107
108         if (!SvOK(*svp))
109                 return 0;
110
111         ptr = SvPV(*svp, len);
112         buf = (char*) odr_malloc(stream, len+1);
113         strcpy(buf, ptr);
114         return buf;
115 }
116
117
118 CV * simpleserver_sv2cv(SV *handler) {
119     STRLEN len;
120     char *buf;
121    
122     if (SvPOK(handler)) {
123         CV *ret;
124         buf = SvPV( handler, len);
125         if ( !( ret = perl_get_cv(buf, FALSE ) ) ) {
126             fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf );
127             exit(1);
128         }
129         
130         return ret;
131     } else {
132         return (CV *) handler;
133     }
134 }
135
136 /* debugging routine to check for destruction of Perl interpreters */
137 #ifdef USE_ITHREADS
138 void tst_clones(void)
139 {
140     int i; 
141     PerlInterpreter *parent = PERL_GET_CONTEXT;
142     for (i = 0; i<5000; i++)
143     {
144         PerlInterpreter *perl_interp;
145
146         PERL_SET_CONTEXT(parent);
147         PL_perl_destruct_level = 2;
148         perl_interp = perl_clone(parent, CLONEf_CLONE_HOST);
149         PL_perl_destruct_level = 2;
150         PERL_SET_CONTEXT(perl_interp);
151         perl_destruct(perl_interp);
152         perl_free(perl_interp);
153     }
154     exit (0);
155 }
156 #endif
157
158 int simpleserver_clone(void) {
159 #ifdef USE_ITHREADS
160      yaz_mutex_enter(simpleserver_mutex);
161      if (1)
162      {
163          PerlInterpreter *current = PERL_GET_CONTEXT;
164
165          /* if current is unset, then we're in a new thread with
166           * no Perl interpreter for it. So we must create one .
167           * This will only happen when threaded is used..
168           */
169          if (!current) {
170              PerlInterpreter *perl_interp;
171              PERL_SET_CONTEXT( root_perl_context );
172              perl_interp = perl_clone(root_perl_context, CLONEf_CLONE_HOST);
173              PERL_SET_CONTEXT( perl_interp );
174          }
175      }
176      yaz_mutex_leave(simpleserver_mutex);
177 #endif
178      return 0;
179 }
180
181
182 void simpleserver_free(void) {
183     yaz_mutex_enter(simpleserver_mutex);
184     if (1)
185     {
186         PerlInterpreter *current_interp = PERL_GET_CONTEXT;
187
188         /* If current Perl Interp is different from root interp, then
189          * we're in threaded mode and we must destroy.. 
190          */
191         if (current_interp != root_perl_context) {
192             PL_perl_destruct_level = 2;
193             PERL_SET_CONTEXT(current_interp);
194             perl_destruct(current_interp);
195             perl_free(current_interp);
196         }
197     }
198     yaz_mutex_leave(simpleserver_mutex);
199 }
200
201
202 Z_GenericRecord *read_grs1(char *str, ODR o)
203 {
204         int type, ivalue;
205         char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
206         char value[GRS_BUF_SIZE+1];
207         Z_GenericRecord *r = 0;
208
209         original = str;
210         r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
211         r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
212         r->num_elements = 0;
213         
214         for (;;)
215         {
216                 Z_TaggedElement *t;
217                 Z_ElementData *c;
218                 int len;
219         
220                 ptr = strchr(str, '\n');
221                 if (!ptr) {
222                         return r;
223                 }
224                 len = ptr - str;
225                 if (len > GRS_BUF_SIZE) {
226                     yaz_log(YLOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
227                     len = GRS_BUF_SIZE;
228                 }
229                 strncpy(line, str, len);
230                 line[len] = 0;
231                 buf = line;
232                 str = ptr + 1;
233                 while (*buf && isspace(*buf))
234                         buf++;
235                 if (*buf == '}') {
236                         memmove(original, str, strlen(str));
237                         return r;
238                 }
239                 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
240                 {
241                         yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
242                         return r;
243                 }
244                 if (!type && *value == '0')
245                         return r;
246                 if (!(buf = strchr(buf, ')')))
247                         return r;
248                 buf++;
249                 while (*buf && isspace(*buf))
250                         buf++;
251                 if (r->num_elements >= GRS_MAX_FIELDS)
252                 {
253                         yaz_log(YLOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
254                         exit(0);
255                 }
256                 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
257                 t->tagType = odr_intdup(o, type);
258                 t->tagValue = (Z_StringOrNumeric *)
259                         odr_malloc(o, sizeof(Z_StringOrNumeric));
260                 if ((ivalue = atoi(value)))
261                 {
262                         t->tagValue->which = Z_StringOrNumeric_numeric;
263                         t->tagValue->u.numeric = odr_intdup(o, ivalue);
264                 }
265                 else
266                 {
267                         t->tagValue->which = Z_StringOrNumeric_string;
268                         t->tagValue->u.string = odr_strdup(o, value);
269                 }
270                 t->tagOccurrence = 0;
271                 t->metaData = 0;
272                 t->appliedVariant = 0;
273                 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
274                 if (*buf == '{')
275                 {
276                         c->which = Z_ElementData_subtree;
277                         c->u.subtree = read_grs1(str, o);
278                 }
279                 else
280                 {
281                         c->which = Z_ElementData_string;
282                         c->u.string = odr_strdup(o, buf);
283                 }
284                 r->num_elements++;
285         }
286 }
287
288
289
290 static void oid2str(Odr_oid *o, WRBUF buf)
291 {
292     for (; *o >= 0; o++) {
293         char ibuf[16];
294         sprintf(ibuf, "%d", *o);
295         wrbuf_puts(buf, ibuf);
296         if (o[1] > 0)
297             wrbuf_putc(buf, '.');
298     }
299 }
300
301 WRBUF oid2dotted(Odr_oid *oid)
302 {
303     WRBUF buf = wrbuf_alloc();
304     oid2str(oid, buf);
305     return buf;
306 }
307                 
308
309 WRBUF zquery2pquery(Z_Query *q)
310 {
311     WRBUF buf = wrbuf_alloc();
312
313     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
314         return 0;
315     yaz_rpnquery_to_wrbuf(buf, q->u.type_1);
316     return buf;
317 }
318
319
320 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
321 #include <stdarg.h>
322 void fatal(char *fmt, ...)
323 {
324     va_list ap;
325
326     fprintf(stderr, "FATAL (SimpleServer): ");
327     va_start(ap, fmt);
328     vfprintf(stderr, fmt, ap);
329     va_end(ap);
330     fprintf(stderr, "\n");
331     abort();
332 }
333
334
335 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
336 /*
337  * Creates a new Perl object of type `class'; the newly-created scalar
338  * that is a reference to the blessed thingy `referent' is returned.
339  */
340 static SV *newObject(char *class, SV *referent)
341 {
342     HV *stash;
343     SV *sv;
344
345     sv = newRV_noinc((SV*) referent);
346     stash = gv_stashpv(class, 0);
347     if (stash == 0)
348         fatal("attempt to create object of undefined class '%s'", class);
349     /*assert(stash != 0);*/
350     sv_bless(sv, stash);
351     return sv;
352 }
353
354
355 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
356 static void setMember(HV *hv, char *name, SV *val)
357 {
358     /* We don't increment `val's reference count -- I think this is
359      * right because it's created with a refcount of 1, and in fact
360      * the reference via this hash is the only reference to it in
361      * general.
362      */
363     if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
364         fatal("couldn't store member in hash");
365 }
366
367
368 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
369 static SV *translateOID(Odr_oid *x)
370 {
371     /* Yaz represents an OID by an int array terminated by a negative
372      * value, typically -1; we represent it as a reference to a
373      * blessed scalar string of "."-separated elements.
374      */
375     char buf[1000];
376     int i;
377
378     *buf = '\0';
379     for (i = 0; x[i] >= 0; i++) {
380         sprintf(buf + strlen(buf), "%d", (int) x[i]);
381         if (x[i+1] >- 0)
382             strcat(buf, ".");
383     }
384
385     /*
386      * ### We'd like to return a blessed scalar (string) here, but of
387      *  course you can't do that in Perl: only references can be
388      *  blessed, so we'd have to return a _reference_ to a string, and
389      *  bless _that_.  Better to do without the blessing, I think.
390      */
391     if (1) {
392         return newSVpv(buf, 0);
393     } else {
394         return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
395     }
396 }
397
398 static SV *attributes2perl(Z_AttributeList *list)
399 {
400     AV *av;
401         int i;
402         SV *attrs = newObject("Net::Z3950::RPN::Attributes",
403                               (SV*) (av = newAV()));
404         for (i = 0; i < list->num_attributes; i++) {
405             Z_AttributeElement *elem = list->attributes[i];
406             HV *hv2;
407             SV *tmp = newObject("Net::Z3950::RPN::Attribute",
408                                 (SV*) (hv2 = newHV()));
409             if (elem->attributeSet)
410                 setMember(hv2, "attributeSet",
411                           translateOID(elem->attributeSet));
412             setMember(hv2, "attributeType",
413                       newSViv(*elem->attributeType));
414             if (elem->which == Z_AttributeValue_numeric) {
415                 setMember(hv2, "attributeValue",
416                           newSViv(*elem->value.numeric));
417             } else {
418                 Z_ComplexAttribute *c;
419                 Z_StringOrNumeric *son;
420                 assert(elem->which == Z_AttributeValue_complex);
421                 c = elem->value.complex;
422                 /* We ignore semantic actions and multiple values */
423                 assert(c->num_list > 0);
424                 son = c->list[0];
425                 if (son->which == Z_StringOrNumeric_numeric) {
426                     setMember(hv2, "attributeValue",
427                               newSViv(*son->u.numeric));
428                 } else { /*Z_StringOrNumeric_string*/
429                     setMember(hv2, "attributeValue",
430                               newSVpv(son->u.string, 0));
431                 }
432             }
433             av_push(av, tmp);
434         }
435         return attrs;
436 }
437
438 static SV *f_Term_to_SV(Z_Term *term, Z_AttributeList *attributes)
439 {
440         HV *hv;
441         SV *sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
442
443         if (term->which != Z_Term_general)
444                 fatal("can't handle RPN terms other than general");
445
446         setMember(hv, "term", newSVpv((char*) term->u.general->buf,
447                                   term->u.general->len));
448
449         if (attributes) {
450                 setMember(hv, "attributes", attributes2perl(attributes));
451         }
452         return sv;
453 }
454
455 static SV *rpn2perl(Z_RPNStructure *s)
456 {
457     SV *sv;
458     HV *hv;
459     AV *av;
460     Z_Operand *o;
461
462     switch (s->which) {
463     case Z_RPNStructure_simple:
464         o = s->u.simple;
465         switch (o->which) {
466         case Z_Operand_resultSetId: {
467             /* This code causes a SIGBUS on my machine, and I have no
468                idea why.  It seems as clear as day to me */
469             SV *sv2;
470             char *rsid = (char*) o->u.resultSetId;
471             /*printf("Encoding resultSetId '%s'\n", rsid);*/
472             sv = newObject("Net::Z3950::RPN::RSID", (SV*) (hv = newHV()));
473             /*printf("Made sv=0x%lx, hv=0x%lx\n", (unsigned long) sv ,(unsigned long) hv);*/
474             sv2 = newSVpv(rsid, strlen(rsid));
475             setMember(hv, "id", sv2);
476             /*printf("Set hv{id} to 0x%lx\n", (unsigned long) sv2);*/
477             return sv;
478         }
479
480         case  Z_Operand_APT:
481             return f_Term_to_SV(o->u.attributesPlusTerm->term,
482                         o->u.attributesPlusTerm->attributes);   
483         default:
484             fatal("unknown RPN simple type %d", (int) o->which);
485         }
486
487     case Z_RPNStructure_complex: {
488         SV *tmp;
489         Z_Complex *c = s->u.complex;
490         char *type = 0;         /* vacuous assignment satisfies gcc -Wall */
491         switch (c->roperator->which) {
492         case Z_Operator_and:     type = "Net::Z3950::RPN::And";    break;
493         case Z_Operator_or:      type = "Net::Z3950::RPN::Or";     break;
494         case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
495         case Z_Operator_prox:    fatal("proximity not yet supported");
496         default: fatal("unknown RPN operator %d", (int) c->roperator->which);
497         }
498         sv = newObject(type, (SV*) (av = newAV()));
499         if ((tmp = rpn2perl(c->s1)) == 0)
500             return 0;
501         av_push(av, tmp);
502         if ((tmp = rpn2perl(c->s2)) == 0)
503             return 0;
504         av_push(av, tmp);
505         return sv;
506     }
507
508     default:
509         fatal("unknown RPN node type %d", (int) s->which);
510     }
511     
512     return 0;
513 }
514
515
516 /* Decode the Z_SortAttributes struct and store the whole thing into the
517  * hash by reference
518  */
519 int simpleserver_ExpandSortAttributes (HV *sort_spec, Z_SortAttributes *sattr)
520 {
521     WRBUF attrset_wr = wrbuf_alloc();
522     AV *list = newAV();
523     Z_AttributeList *attr_list = sattr->list;
524     int i;
525
526     oid2str(sattr->id, attrset_wr);
527     hv_store(sort_spec, "ATTRSET", 7,
528              newSVpv(attrset_wr->buf, attrset_wr->pos), 0);
529     wrbuf_destroy(attrset_wr);
530
531     hv_store(sort_spec, "SORT_ATTR", 9, newRV( sv_2mortal( (SV*) list ) ), 0);
532
533     for (i = 0; i < attr_list->num_attributes; i++) 
534     {
535         Z_AttributeElement *attr = *attr_list->attributes++; 
536         HV *attr_spec = newHV();
537                 
538         av_push(list, newRV( sv_2mortal( (SV*) attr_spec ) ));
539         hv_store(attr_spec, "ATTR_TYPE", 9, newSViv(*attr->attributeType), 0);
540
541         if (attr->which == Z_AttributeValue_numeric)
542         {
543             hv_store(attr_spec, "ATTR_VALUE", 10,
544                      newSViv(*attr->value.numeric), 0);
545         } else {
546             return 0;
547         }
548     }
549
550     return 1;
551 }
552
553
554 /* Decode the Z_SortKeySpec struct and store the whole thing in a perl hash */
555 int simpleserver_SortKeySpecToHash (HV *sort_spec, Z_SortKeySpec *spec)
556 {
557     Z_SortElement *element = spec->sortElement;
558
559     hv_store(sort_spec, "RELATION", 8, newSViv(*spec->sortRelation), 0);
560     hv_store(sort_spec, "CASE", 4, newSViv(*spec->caseSensitivity), 0);
561     hv_store(sort_spec, "MISSING", 7, newSViv(spec->which), 0);
562
563     if (element->which == Z_SortElement_generic)
564     {
565         Z_SortKey *key = element->u.generic;
566
567         if (key->which == Z_SortKey_sortField)
568         {
569             hv_store(sort_spec, "SORTFIELD", 9,
570                      newSVpv((char *) key->u.sortField, 0), 0);
571         }
572         else if (key->which == Z_SortKey_elementSpec)
573         {
574             Z_Specification *zspec = key->u.elementSpec;
575             
576             hv_store(sort_spec, "ELEMENTSPEC_TYPE", 16,
577                      newSViv(zspec->which), 0);
578
579             if (zspec->which == Z_Schema_oid)
580             {
581                 WRBUF elementSpec = wrbuf_alloc();
582
583                 oid2str(zspec->schema.oid, elementSpec);
584                 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
585                          newSVpv(elementSpec->buf, elementSpec->pos), 0);
586                 wrbuf_destroy(elementSpec);
587             }
588             else if (zspec->which == Z_Schema_uri)
589             {
590                 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
591                          newSVpv((char *) zspec->schema.uri, 0), 0);
592             }
593         }
594         else if (key->which == Z_SortKey_sortAttributes)
595         {
596             return simpleserver_ExpandSortAttributes(sort_spec,
597                                                      key->u.sortAttributes);
598         }
599         else
600         {
601             return 0;
602         }
603     }
604     else
605     {
606         return 0;
607     }
608
609     return 1;
610 }
611
612
613 static SV *zquery2perl(Z_Query *q)
614 {
615     SV *sv;
616     HV *hv;
617
618     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
619         return 0;
620     sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
621     if (q->u.type_1->attributeSetId)
622         setMember(hv, "attributeSet",
623                   translateOID(q->u.type_1->attributeSetId));
624     setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
625     return sv;
626 }
627
628
629 int bend_sort(void *handle, bend_sort_rr *rr)
630 {
631         HV *href;
632         AV *aref;
633         AV *sort_seq;
634         SV **temp;
635         SV *err_code;
636         SV *err_str;
637         SV *status;
638         SV *point;
639         STRLEN len;
640         char *ptr;
641         char *ODR_err_str;
642         char **input_setnames;
643         Zfront_handle *zhandle = (Zfront_handle *)handle;
644         Z_SortKeySpecList *sort_spec = rr->sort_sequence;
645         int i;
646         
647         dSP;
648         ENTER;
649         SAVETMPS;
650         
651         aref = newAV();
652         input_setnames = rr->input_setnames;
653         for (i = 0; i < rr->num_input_setnames; i++)
654         {
655             av_push(aref, newSVpv(*input_setnames++, 0));
656         }
657
658         sort_seq = newAV();
659         for (i = 0; i < sort_spec->num_specs; i++)
660         {
661             Z_SortKeySpec *spec = *sort_spec->specs++;
662             HV *sort_spec = newHV();
663
664             if ( simpleserver_SortKeySpecToHash(sort_spec, spec) )
665                 av_push(sort_seq, newRV( sv_2mortal( (SV*) sort_spec ) ));
666             else
667             {
668                 rr->errcode = 207;
669                 return 0;
670             }
671         }
672         
673         href = newHV();
674         hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
675         hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
676         hv_store(href, "SEQUENCE", 8, newRV( (SV*) sort_seq), 0);
677         hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
678         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
679         hv_store(href, "STATUS", 6, newSViv(0), 0);
680         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
681         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
682
683         PUSHMARK(sp);
684
685         XPUSHs(sv_2mortal(newRV( (SV*) href)));
686
687         PUTBACK;
688
689         perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
690
691         SPAGAIN;
692
693         temp = hv_fetch(href, "ERR_CODE", 8, 1);
694         err_code = newSVsv(*temp);
695
696         temp = hv_fetch(href, "ERR_STR", 7, 1);
697         err_str = newSVsv(*temp);
698
699         temp = hv_fetch(href, "STATUS", 6, 1);
700         status = newSVsv(*temp);
701
702         temp = hv_fetch(href, "HANDLE", 6, 1);
703         point = newSVsv(*temp);
704
705         hv_undef(href);
706         av_undef(aref);
707         av_undef(sort_seq);
708        
709         sv_free( (SV*) aref);
710         sv_free( (SV*) href);
711         sv_free( (SV*) sort_seq);
712
713         rr->errcode = SvIV(err_code);
714         rr->sort_status = SvIV(status);
715         
716         ptr = SvPV(err_str, len);
717         ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
718         strcpy(ODR_err_str, ptr);
719         rr->errstring = ODR_err_str;
720         zhandle->handle = point;
721
722         sv_free(err_code);
723         sv_free(err_str);
724         sv_free(status);
725         
726         PUTBACK;
727         FREETMPS;
728         LEAVE;
729
730         return 0;
731 }
732
733 static SV *f_FacetField_to_SV(Z_FacetField *facet_field)
734 {
735         HV *hv;
736         AV *av;
737         SV *terms;
738         int i;
739         SV *sv = newObject("Net::Z3950::FacetField", (SV *) (hv = newHV()));
740         if (facet_field->attributes) {
741                 setMember(hv, "attributes",
742                      attributes2perl(facet_field->attributes));
743         }            
744         terms = newObject("Net::Z3950::FacetTerms", (SV *) (av = newAV()));
745
746         for (i = 0; i < facet_field->num_terms; i++) {
747             Z_Term *z_term = facet_field->terms[i]->term;
748             HV *hv;
749             SV *sv_count = newSViv(*facet_field->terms[i]->count);
750             SV *sv_term;
751             SV *tmp;
752             if (z_term->which == Z_Term_general) {
753                 sv_term = newSVpv((char*) z_term->u.general->buf,
754                                    z_term->u.general->len);
755             } else if (z_term->which == Z_Term_characterString) {
756                 sv_term = newSVpv(z_term->u.characterString,
757                                   strlen(z_term->u.characterString));
758             }
759             tmp = newObject("Net::Z3950::FacetTerm", (SV *) (hv = newHV()));
760             
761             setMember(hv, "count", sv_count);
762             setMember(hv, "term", sv_term);
763             
764             av_push(av, tmp);
765         }
766         setMember(hv, "terms", terms);
767         return sv;
768 }
769
770 static SV *f_FacetList_to_SV(Z_FacetList *facet_list)
771 {
772         SV *sv = 0;
773         if (facet_list) {
774                 AV *av;
775                 int i;
776                 sv = newObject("Net::Z3950::FacetList", (SV *) (av = newAV()));
777         
778                 for (i = 0; i < facet_list->num; i++) {
779                        SV *sv = f_FacetField_to_SV(facet_list->elements[i]);
780                        av_push(av, sv);
781                 }
782         }
783         return sv;
784 }
785
786
787 static void f_SV_to_FacetField(HV *facet_field_hv, Z_FacetField **fl, ODR odr)
788 {
789         int i;
790         int num_terms, num_attributes;
791         SV **temp;
792         Z_AttributeList *attributes = odr_malloc(odr, sizeof(*attributes));
793
794         AV *sv_terms, *sv_attributes;
795
796         temp = hv_fetch(facet_field_hv, "attributes", 10, 1);
797         sv_attributes = (AV *) SvRV(*temp);
798         num_attributes = av_len(sv_attributes) + 1;
799         attributes->num_attributes = num_attributes;
800         attributes->attributes = (Z_AttributeElement **)
801              odr_malloc(odr, sizeof(*attributes->attributes) * num_attributes);
802
803         for (i = 0; i < num_attributes; i++) {
804             HV *hv_elem = (HV*) SvRV(sv_2mortal(av_shift(sv_attributes)));
805             Z_AttributeElement *elem;
806             elem = (Z_AttributeElement *) odr_malloc(odr, sizeof(*elem));
807             attributes->attributes[i] = elem;
808
809             elem->attributeSet = 0;
810
811             temp = hv_fetch(hv_elem, "attributeType", 13, 1);
812             elem->attributeType = odr_intdup(odr, SvIV(*temp));
813
814             temp = hv_fetch(hv_elem, "attributeValue", 14, 1);
815
816             if (SvIOK(*temp)) {
817                     elem->which = Z_AttributeValue_numeric;
818                     elem->value.numeric = odr_intdup(odr, SvIV(*temp));
819             } else {
820                     STRLEN s_len;
821                     char *s_buf = SvPV(*temp, s_len);
822                     Z_ComplexAttribute *c = odr_malloc(odr, sizeof *c);
823                     elem->which = Z_AttributeValue_complex;
824                     elem->value.complex = c;
825
826                     c->num_list = 1;
827                     c->list = (Z_StringOrNumeric **) odr_malloc(odr,
828                           sizeof(*c->list));
829                     c->list[0] = (Z_StringOrNumeric *) odr_malloc(odr,
830                           sizeof(**c->list));
831                     c->list[0]->which = Z_StringOrNumeric_string;
832                     c->list[0]->u.string = odr_malloc(odr, s_len + 1);
833                     memcpy(c->list[0]->u.string, s_buf, s_len);
834                     c->list[0]->u.string[s_len] = '\0';
835                     c->num_semanticAction = 0;
836                     c->semanticAction = 0;
837             }
838             hv_undef(hv_elem);
839         }
840
841         temp = hv_fetch(facet_field_hv, "terms", 5, 1);
842
843         sv_terms = (AV *) SvRV(*temp);
844         if (SvTYPE(sv_terms) == SVt_PVAV) {
845             num_terms = av_len(sv_terms) + 1;
846         } else {
847             num_terms = 0;
848         }
849         *fl = facet_field_create(odr, attributes, num_terms);
850         for (i = 0; i < num_terms; i++) {
851             STRLEN s_len;
852             char *s_buf;
853             HV *hv_elem = (HV*) SvRV(sv_2mortal(av_shift(sv_terms)));
854             
855             Z_FacetTerm *facet_term =
856              (Z_FacetTerm *) odr_malloc(odr, sizeof(*facet_term));
857             (*fl)->terms[i] = facet_term;
858
859             temp = hv_fetch(hv_elem, "count", 5, 1);
860             facet_term->count = odr_intdup(odr, SvIV(*temp));
861
862             temp = hv_fetch(hv_elem, "term", 4, 1);
863
864             s_buf = SvPV(*temp, s_len);
865             facet_term->term = z_Term_create(odr, Z_Term_general, s_buf, s_len);
866             hv_undef(hv_elem);
867         }
868 }
869
870 static void f_SV_to_FacetList(SV *sv, Z_OtherInformation **oip, ODR odr)
871 {
872         AV *entries = (AV *) SvRV(sv);
873         int num_facets;
874         if (entries && SvTYPE(entries) == SVt_PVAV && 
875                 (num_facets = av_len(entries) + 1) > 0)
876         {
877             Z_OtherInformation *oi;
878             Z_OtherInformationUnit *oiu;
879             Z_FacetList *facet_list = facet_list_create(odr, num_facets);
880             int i;
881             for (i = 0; i < num_facets; i++) {
882                 HV *facet_field = (HV*) SvRV(sv_2mortal(av_shift(entries)));
883                 f_SV_to_FacetField(facet_field, &facet_list->elements[i], odr);
884                 hv_undef(facet_field);
885             }
886             oi = odr_malloc(odr, sizeof(*oi));
887             oiu = odr_malloc(odr, sizeof(*oiu));
888             oi->num_elements = 1;
889             oi->list = odr_malloc(odr, oi->num_elements * sizeof(*oi->list));
890             oiu->category = 0;
891             oiu->which = Z_OtherInfo_externallyDefinedInfo;
892             oiu->information.externallyDefinedInfo = odr_malloc(odr, sizeof(*oiu->information.externallyDefinedInfo));
893             oiu->information.externallyDefinedInfo->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
894             oiu->information.externallyDefinedInfo->descriptor = 0;
895             oiu->information.externallyDefinedInfo->indirect_reference = 0;
896             oiu->information.externallyDefinedInfo->which = Z_External_userFacets;
897             oiu->information.externallyDefinedInfo->u.facetList = facet_list;
898             oi->list[0] = oiu;
899             *oip = oi;
900         }
901 }
902
903 int bend_search(void *handle, bend_search_rr *rr)
904 {
905         HV *href;
906         AV *aref;
907         SV **temp;
908         int i;
909         char **basenames;
910         WRBUF query;
911         SV *point;
912         Zfront_handle *zhandle = (Zfront_handle *)handle;
913         CV* handler_cv = 0;
914         SV *rpnSV;
915         SV *facetSV;
916
917         dSP;
918         ENTER;
919         SAVETMPS;
920
921         aref = newAV();
922         basenames = rr->basenames;
923         for (i = 0; i < rr->num_bases; i++)
924         {
925                 av_push(aref, newSVpv(*basenames++, 0));
926         }
927 #if ENABLE_STOP_SERVER
928         if (rr->num_bases == 1 && !strcmp(rr->basenames[0], "XXstop"))
929         {
930                 zhandle->stop_flag = 1;
931         }
932 #endif
933         href = newHV();         
934         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
935         if (rr->srw_sortKeys && *rr->srw_sortKeys) 
936             hv_store(href, "SRW_SORTKEYS", 12, newSVpv(rr->srw_sortKeys, 0), 0);
937         hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
938         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
939         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
940         hv_store(href, "HITS", 4, newSViv(0), 0);
941         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
942         hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
943         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
944         hv_store(href, "PID", 3, newSViv(getpid()), 0);
945         if ((rpnSV = zquery2perl(rr->query)) != 0) {
946             hv_store(href, "RPN", 3, rpnSV, 0);
947         }
948         facetSV = f_FacetList_to_SV(yaz_oi_get_facetlist(&rr->search_input));
949         if (facetSV) {
950             hv_store(href, "INPUTFACETS", 11, facetSV, 0);
951         }
952
953         query = zquery2pquery(rr->query);
954         if (query)
955         {
956                 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
957         }
958         else if (rr->query->which == Z_Query_type_104 &&
959                  rr->query->u.type_104->which == Z_External_CQL) {
960             hv_store(href, "CQL", 3,
961                      newSVpv(rr->query->u.type_104->u.cql, 0), 0);
962         }
963         else
964         {       
965                 rr->errcode = 108;
966                 return 0;
967         }
968         PUSHMARK(sp);
969         
970         XPUSHs(sv_2mortal(newRV( (SV*) href)));
971         
972         PUTBACK;
973
974         handler_cv = simpleserver_sv2cv( search_ref );
975         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
976
977         SPAGAIN;
978
979         temp = hv_fetch(href, "HITS", 4, 1);
980         rr->hits = SvIV(*temp);
981
982         temp = hv_fetch(href, "ERR_CODE", 8, 1);
983         rr->errcode = SvIV(*temp);
984
985         temp = hv_fetch(href, "ERR_STR", 7, 1);
986         rr->errstring = string_or_undef(temp, rr->stream);
987
988         temp = hv_fetch(href, "HANDLE", 6, 1);
989         point = newSVsv(*temp);
990
991         temp = hv_fetch(href, "OUTPUTFACETS", 12, 1);
992         f_SV_to_FacetList(*temp, &rr->search_info, rr->stream);
993
994         hv_undef(href);
995         av_undef(aref);
996
997         zhandle->handle = point;
998         sv_free( (SV*) aref);
999         sv_free( (SV*) href);
1000         if (query)
1001             wrbuf_destroy(query);
1002         PUTBACK;
1003         FREETMPS;
1004         LEAVE;
1005         return 0;
1006 }
1007
1008
1009 /* ### I am not 100% about the memory management in this handler */
1010 int bend_delete(void *handle, bend_delete_rr *rr)
1011 {
1012         Zfront_handle *zhandle = (Zfront_handle *)handle;
1013         HV *href;
1014         CV* handler_cv;
1015         int i;
1016         SV **temp;
1017         SV *point;
1018
1019         dSP;
1020         ENTER;
1021         SAVETMPS;
1022
1023         href = newHV();
1024         hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1025         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1026         hv_store(href, "STATUS", 6, newSViv(0), 0);
1027
1028         PUSHMARK(sp);
1029         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1030         PUTBACK;
1031
1032         handler_cv = simpleserver_sv2cv(delete_ref);
1033
1034         if (rr->function == 1) {
1035             /* Delete all result sets in the session */
1036             perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1037             temp = hv_fetch(href, "STATUS", 6, 1);
1038             rr->delete_status = SvIV(*temp);
1039         } else {
1040             rr->delete_status = 0;
1041             /*
1042              * For some reason, deleting two or more result-sets in
1043              * one operation goes horribly wrong, and ### I don't have
1044              * time to debug it right now.
1045              */
1046             if (rr->num_setnames > 1) {
1047                 rr->delete_status = 3; /* "System problem at target" */
1048                 /* There's no way to sent delete-msg using the GFS */
1049                 return 0;
1050             }
1051
1052             for (i = 0; i < rr->num_setnames; i++) {
1053                 hv_store(href, "SETNAME", 7, newSVpv(rr->setnames[i], 0), 0);
1054                 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1055                 temp = hv_fetch(href, "STATUS", 6, 1);
1056                 rr->statuses[i] = SvIV(*temp);
1057                 if (rr->statuses[i] != 0)
1058                     rr->delete_status = rr->statuses[i];
1059             }
1060         }
1061
1062         SPAGAIN;
1063
1064         temp = hv_fetch(href, "HANDLE", 6, 1);
1065         point = newSVsv(*temp);
1066
1067         hv_undef(href);
1068
1069         zhandle->handle = point;
1070
1071         sv_free( (SV*) href);   
1072
1073         PUTBACK;
1074         FREETMPS;
1075         LEAVE;
1076
1077         return 0;
1078 }
1079
1080
1081 int bend_fetch(void *handle, bend_fetch_rr *rr)
1082 {
1083         HV *href;
1084         SV **temp;
1085         SV *basename;
1086         SV *record;
1087         SV *last;
1088         SV *err_code;
1089         SV *err_string;
1090         SV *sur_flag;
1091         SV *point;
1092         SV *rep_form;
1093         SV *schema = 0;
1094         char *ptr;
1095         char *ODR_record;
1096         char *ODR_basename;
1097         char *ODR_errstr;
1098         WRBUF oid_dotted;
1099         Zfront_handle *zhandle = (Zfront_handle *)handle;
1100         CV* handler_cv = 0;
1101
1102         Z_RecordComposition *composition;
1103         Z_ElementSetNames *simple;
1104         Z_CompSpec *complex;
1105         STRLEN length;
1106
1107         dSP;
1108         ENTER;
1109         SAVETMPS;
1110
1111         rr->errcode = 0;
1112         href = newHV();
1113         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
1114         if (rr->schema)
1115                 hv_store(href, "SCHEMA", 6, newSVpv(rr->schema, 0), 0);
1116         else
1117                 hv_store(href, "SCHEMA", 6, newSVpv("", 0), 0);
1118
1119         temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
1120         if (rr->request_format != 0) {
1121             oid_dotted = oid2dotted(rr->request_format);
1122         } else {
1123             /* Probably an SRU request: assume XML is required */
1124             oid_dotted = wrbuf_alloc();
1125             wrbuf_puts(oid_dotted, "1.2.840.10003.5.109.10");
1126         }
1127         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
1128         hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
1129         hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
1130         hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
1131         hv_store(href, "LAST", 4, newSViv(0), 0);
1132         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1133         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1134         hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
1135         hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1136         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1137         hv_store(href, "PID", 3, newSViv(getpid()), 0);
1138         if (rr->comp)
1139         {
1140                 composition = rr->comp;
1141                 if (composition->which == Z_RecordComp_simple)
1142                 {
1143                         simple = composition->u.simple;
1144                         if (simple->which == Z_ElementSetNames_generic)
1145                         {
1146                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
1147                         } 
1148                         else
1149                         {
1150                                 rr->errcode = 26;
1151                                 rr->errstring = odr_strdup(rr->stream, "non-generic 'simple' composition");
1152                                 return 0;
1153                         }
1154                 }
1155                 else if (composition->which == Z_RecordComp_complex)
1156                 {
1157                         if (composition->u.complex->generic &&
1158
1159                                         composition->u.complex->generic &&
1160                                         composition->u.complex->generic->elementSpec &&
1161                                         composition->u.complex->generic->elementSpec->which ==
1162                                         Z_ElementSpec_elementSetName)
1163                         {
1164                                 complex = composition->u.complex;
1165                                 hv_store(href, "COMP", 4,
1166                                         newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
1167                         }
1168                         else
1169                         {
1170 #if 0   /* For now ignore this error, which is ubiquitous in SRU */
1171                                 rr->errcode = 26;
1172                                 rr->errstring = odr_strdup(rr->stream, "'complex' composition is not generic ESN");
1173                                 return 0;
1174 #endif /*0*/
1175                         }
1176                 }
1177                 else
1178                 {
1179                         rr->errcode = 26;
1180                         rr->errstring = odr_strdup(rr->stream, "composition neither simple nor complex");
1181                         return 0;
1182                 }
1183         }
1184
1185         PUSHMARK(sp);
1186
1187         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1188
1189         PUTBACK;
1190         
1191         handler_cv = simpleserver_sv2cv( fetch_ref );
1192         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1193
1194         SPAGAIN;
1195
1196         temp = hv_fetch(href, "BASENAME", 8, 1);
1197         basename = newSVsv(*temp);
1198
1199         temp = hv_fetch(href, "RECORD", 6, 1);
1200         record = newSVsv(*temp);
1201
1202         temp = hv_fetch(href, "LAST", 4, 1);
1203         last = newSVsv(*temp);
1204
1205         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1206         err_code = newSVsv(*temp);
1207
1208         temp = hv_fetch(href, "ERR_STR", 7, 1),
1209         err_string = newSVsv(*temp);
1210
1211         temp = hv_fetch(href, "SUR_FLAG", 8, 1);
1212         sur_flag = newSVsv(*temp);
1213
1214         temp = hv_fetch(href, "REP_FORM", 8, 1);
1215         rep_form = newSVsv(*temp);
1216
1217         temp = hv_fetch(href, "SCHEMA", 6, 1);
1218         if (temp != 0) {
1219                 schema = newSVsv(*temp);
1220                 ptr = SvPV(schema, length);
1221                 if (length > 0) {
1222                         rr->schema = (char *)odr_malloc(rr->stream, length + 1);
1223                         strcpy(rr->schema, ptr);
1224                 }
1225         }
1226
1227         temp = hv_fetch(href, "HANDLE", 6, 1);
1228         point = newSVsv(*temp);
1229
1230
1231         hv_undef(href);
1232         
1233         ptr = SvPV(basename, length);
1234         ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
1235         strcpy(ODR_basename, ptr);
1236         rr->basename = ODR_basename;
1237
1238         ptr = SvPV(rep_form, length);
1239
1240         rr->output_format = yaz_string_to_oid_odr(yaz_oid_std(),
1241                                         CLASS_RECSYN, ptr, rr->stream);
1242         if (!rr->output_format)
1243         {
1244                 printf("Net::Z3950::SimpleServer: WARNING: Bad OID %s\n", ptr);
1245                 rr->output_format =
1246                         odr_oiddup(rr->stream, yaz_oid_recsyn_sutrs);
1247         }
1248         ptr = SvPV(record, length);
1249         /* Treat GRS-1 records separately */
1250         if (!oid_oidcmp(rr->output_format, yaz_oid_recsyn_grs_1))
1251         {
1252                 rr->record = (char *) read_grs1(ptr, rr->stream);
1253                 rr->len = -1;
1254         }
1255         else
1256         {
1257                 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
1258                 strcpy(ODR_record, ptr);
1259                 rr->record = ODR_record;
1260                 rr->len = length;
1261         }
1262         zhandle->handle = point;
1263         handle = zhandle;
1264         rr->last_in_set = SvIV(last);
1265         
1266         if (!(rr->errcode))
1267         {
1268                 rr->errcode = SvIV(err_code);
1269                 ptr = SvPV(err_string, length);
1270                 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
1271                 strcpy(ODR_errstr, ptr);
1272                 rr->errstring = ODR_errstr;
1273         }
1274         rr->surrogate_flag = SvIV(sur_flag);
1275
1276         wrbuf_destroy(oid_dotted);
1277         sv_free((SV*) href);
1278         sv_free(basename);
1279         sv_free(record);
1280         sv_free(last);
1281         sv_free(err_string);
1282         sv_free(err_code),
1283         sv_free(sur_flag);
1284         sv_free(rep_form);
1285
1286         if (schema)
1287                 sv_free(schema);
1288
1289         PUTBACK;
1290         FREETMPS;
1291         LEAVE;
1292         
1293         return 0;
1294 }
1295
1296
1297 int bend_present(void *handle, bend_present_rr *rr)
1298 {
1299         HV *href;
1300         SV **temp;
1301         SV *err_code;
1302         SV *err_string;
1303         SV *point;
1304         STRLEN len;
1305         Z_RecordComposition *composition;
1306         Z_ElementSetNames *simple;
1307         Z_CompSpec *complex;
1308         char *ODR_errstr;
1309         char *ptr;
1310         Zfront_handle *zhandle = (Zfront_handle *)handle;
1311         CV* handler_cv = 0;
1312
1313 /*      WRBUF oid_dotted; */
1314
1315         dSP;
1316         ENTER;
1317         SAVETMPS;
1318
1319         href = newHV();
1320         hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1321         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1322         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1323         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1324         hv_store(href, "START", 5, newSViv(rr->start), 0);
1325         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
1326         hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
1327         /*oid_dotted = oid2dotted(rr->request_format_raw);
1328         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
1329         hv_store(href, "PID", 3, newSViv(getpid()), 0);
1330         if (rr->comp)
1331         {
1332                 composition = rr->comp;
1333                 if (composition->which == Z_RecordComp_simple)
1334                 {
1335                         simple = composition->u.simple;
1336                         if (simple->which == Z_ElementSetNames_generic)
1337                         {
1338                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
1339                         } 
1340                         else
1341                         {
1342                                 rr->errcode = 26;
1343                                 rr->errstring = odr_strdup(rr->stream, "non-generic 'simple' composition");
1344                                 return 0;
1345                         }
1346                 }
1347                 else if (composition->which == Z_RecordComp_complex)
1348                 {
1349                         if (composition->u.complex->generic &&
1350
1351                                         composition->u.complex->generic &&
1352                                         composition->u.complex->generic->elementSpec &&
1353                                         composition->u.complex->generic->elementSpec->which ==
1354                                         Z_ElementSpec_elementSetName)
1355                         {
1356                                 complex = composition->u.complex;
1357                                 hv_store(href, "COMP", 4,
1358                                         newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
1359                         }
1360                         else
1361                         {
1362                                 rr->errcode = 26;
1363                                 rr->errstring = odr_strdup(rr->stream, "'complex' composition is not generic ESN");
1364                                 return 0;
1365                         }
1366                 }
1367                 else
1368                 {
1369                         rr->errcode = 26;
1370                         rr->errstring = odr_strdup(rr->stream, "composition neither simple nor complex");
1371                         return 0;
1372                 }
1373         }
1374
1375         PUSHMARK(sp);
1376         
1377         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1378         
1379         PUTBACK;
1380         
1381         handler_cv = simpleserver_sv2cv( present_ref );
1382         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1383         
1384         SPAGAIN;
1385
1386         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1387         err_code = newSVsv(*temp);
1388
1389         temp = hv_fetch(href, "ERR_STR", 7, 1);
1390         err_string = newSVsv(*temp);
1391
1392         temp = hv_fetch(href, "HANDLE", 6, 1);
1393         point = newSVsv(*temp);
1394
1395         PUTBACK;
1396         FREETMPS;
1397         LEAVE;
1398         
1399         hv_undef(href);
1400         rr->errcode = SvIV(err_code);
1401
1402         ptr = SvPV(err_string, len);
1403         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1404         strcpy(ODR_errstr, ptr);
1405         rr->errstring = ODR_errstr;
1406 /*      wrbuf_free(oid_dotted, 1);*/
1407         zhandle->handle = point;
1408         handle = zhandle;
1409         sv_free(err_code);
1410         sv_free(err_string);
1411         sv_free( (SV*) href);
1412
1413         return 0;
1414 }
1415
1416
1417 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1418 {
1419         perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1420         return 0;
1421 }
1422
1423
1424 int bend_scan(void *handle, bend_scan_rr *rr)
1425 {
1426         HV *href;
1427         AV *aref;
1428         AV *list;
1429         AV *entries;
1430         HV *scan_item;
1431         struct scan_entry *scan_list;
1432         struct scan_entry *buffer;
1433         int *step_size = rr->step_size;
1434         int i;
1435         char **basenames;
1436         SV **temp;
1437         SV *err_code = sv_newmortal();
1438         SV *err_str = sv_newmortal();
1439         SV *point = sv_newmortal();
1440         SV *status = sv_newmortal();
1441         SV *number = sv_newmortal();
1442         char *ptr;
1443         char *ODR_errstr;
1444         STRLEN len;
1445         int term_len;
1446         SV *entries_ref;
1447         Zfront_handle *zhandle = (Zfront_handle *)handle;
1448         CV* handler_cv = 0;
1449         SV *rpnSV;
1450
1451         dSP;
1452         ENTER;
1453         SAVETMPS;
1454         href = newHV();
1455         list = newAV();
1456
1457         /* RPN is better than TERM since it includes attributes */
1458         if ((rpnSV = f_Term_to_SV(rr->term->term, rr->term->attributes)) != 0) {
1459             setMember(href, "RPN", rpnSV);
1460         }
1461
1462         if (rr->term->term->which == Z_Term_general)
1463         {
1464                 term_len = rr->term->term->u.general->len;
1465                 hv_store(href, "TERM", 4, newSVpv((char*) rr->term->term->u.general->buf, term_len), 0);
1466         } else {
1467                 rr->errcode = 229;      /* Unsupported term type */
1468                 return 0;
1469         }
1470         hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1471         hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1472         hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1473         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1474         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1475         hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1476         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1477         hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1478         hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1479         aref = newAV();
1480         basenames = rr->basenames;
1481         for (i = 0; i < rr->num_bases; i++)
1482         {
1483                 av_push(aref, newSVpv(*basenames++, 0));
1484         }
1485         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1486
1487         PUSHMARK(sp);
1488
1489         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1490
1491         PUTBACK;
1492
1493         handler_cv = simpleserver_sv2cv( scan_ref );
1494         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1495
1496         SPAGAIN;
1497
1498         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1499         err_code = newSVsv(*temp);
1500
1501         temp = hv_fetch(href, "ERR_STR", 7, 1);
1502         err_str = newSVsv(*temp);
1503
1504         temp = hv_fetch(href, "HANDLE", 6, 1);
1505         point = newSVsv(*temp);
1506
1507         temp = hv_fetch(href, "STATUS", 6, 1);
1508         status = newSVsv(*temp);
1509         
1510         temp = hv_fetch(href, "NUMBER", 6, 1);
1511         number = newSVsv(*temp);
1512
1513         temp = hv_fetch(href, "ENTRIES", 7, 1);
1514         entries_ref = newSVsv(*temp);
1515
1516         PUTBACK;
1517         FREETMPS;
1518         LEAVE;
1519
1520         ptr = SvPV(err_str, len);
1521         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1522         strcpy(ODR_errstr, ptr);
1523         rr->errstring = ODR_errstr;
1524         rr->errcode = SvIV(err_code);
1525         rr->num_entries = SvIV(number);
1526         rr->status = SvIV(status);
1527         scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1528         buffer = scan_list;
1529         entries = (AV *)SvRV(entries_ref);
1530         if (rr->errcode == 0) for (i = 0; i < rr->num_entries; i++)
1531         {
1532                 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1533                 temp = hv_fetch(scan_item, "TERM", 4, 1);
1534                 ptr = SvPV(*temp, len);
1535                 buffer->term = (char *) odr_malloc (rr->stream, len + 1); 
1536                 strcpy(buffer->term, ptr);
1537                 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1); 
1538                 buffer->occurrences = SvIV(*temp);
1539                 buffer++;
1540                 hv_undef(scan_item);
1541         }
1542         rr->entries = scan_list;
1543         zhandle->handle = point;
1544         handle = zhandle;
1545         sv_free(err_code);
1546         sv_free(err_str);
1547         sv_free(status);
1548         sv_free(number);
1549         hv_undef(href);
1550         sv_free((SV *)href);
1551         av_undef(aref);
1552         sv_free((SV *)aref);
1553         av_undef(list);
1554         sv_free((SV *)list);
1555         av_undef(entries);
1556         /*sv_free((SV *)entries);*/
1557         sv_free(entries_ref);
1558
1559         return 0;
1560 }
1561
1562 int bend_explain(void *handle, bend_explain_rr *q)
1563 {
1564         HV *href;
1565         CV *handler_cv = 0;
1566         SV **temp;
1567         char *explain;
1568         SV *explainsv;
1569         STRLEN len;
1570         Zfront_handle *zhandle = (Zfront_handle *)handle;
1571
1572         dSP;
1573         ENTER;
1574         SAVETMPS;
1575
1576         href = newHV();
1577         hv_store(href, "EXPLAIN", 7, newSVpv("", 0), 0);
1578         hv_store(href, "DATABASE", 8, newSVpv(q->database, 0), 0);
1579         hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1580         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1581
1582         PUSHMARK(sp);
1583         XPUSHs(sv_2mortal(newRV((SV*) href)));
1584         PUTBACK;
1585
1586         handler_cv = simpleserver_sv2cv(explain_ref);
1587         perl_call_sv((SV*) handler_cv, G_SCALAR | G_DISCARD);
1588
1589         SPAGAIN;
1590
1591         temp = hv_fetch(href, "EXPLAIN", 7, 1);
1592         explainsv = newSVsv(*temp);
1593
1594         PUTBACK;
1595         FREETMPS;
1596         LEAVE;
1597
1598         explain = SvPV(explainsv, len);
1599         q->explain_buf = (char*) odr_malloc(q->stream, len + 1);
1600         strcpy(q->explain_buf, explain);
1601
1602         return 0;
1603 }
1604
1605
1606 /*
1607  * You'll laugh when I tell you this ...  Astonishingly, it turns out
1608  * that ActivePerl (which is widely used on Windows) has, in the
1609  * header file Perl\lib\CORE\XSUB.h, the following heinous crime:
1610  *          #    define open            PerlLIO_open
1611  * This of course screws up the use of the "open" member of the
1612  * Z_IdAuthentication structure below, so we have to undo this
1613  * brain-damage.
1614  */
1615 #ifdef open
1616 #undef open
1617 #endif
1618
1619
1620 bend_initresult *bend_init(bend_initrequest *q)
1621 {
1622         int dummy = simpleserver_clone();
1623         bend_initresult *r = (bend_initresult *)
1624                 odr_malloc (q->stream, sizeof(*r));
1625         char *ptr;
1626         CV* handler_cv = 0;
1627         dSP;
1628         STRLEN len;
1629         NMEM nmem = nmem_create();
1630         Zfront_handle *zhandle =  (Zfront_handle *) nmem_malloc (nmem,
1631                         sizeof(*zhandle));
1632         SV *handle;
1633         HV *href;
1634         SV **temp;
1635
1636         ENTER;
1637         SAVETMPS;
1638
1639         zhandle->ghandle = _global_ghandle;
1640         zhandle->nmem = nmem;
1641         zhandle->stop_flag = 0;
1642
1643         if (sort_ref)
1644         {
1645             q->bend_sort = bend_sort;
1646         }
1647         if (search_ref)
1648         {
1649                 q->bend_search = bend_search;
1650         }
1651         if (present_ref)
1652         {
1653                 q->bend_present = bend_present;
1654         }
1655         /*q->bend_esrequest = bend_esrequest;*/
1656         if (delete_ref) {
1657                 q->bend_delete = bend_delete;
1658         }
1659         if (fetch_ref)
1660         {
1661                 q->bend_fetch = bend_fetch;
1662         }
1663         if (scan_ref)
1664         {
1665                 q->bend_scan = bend_scan;
1666         }
1667         if (explain_ref)
1668         {
1669                 q->bend_explain = bend_explain;
1670         }
1671
1672         href = newHV(); 
1673
1674         /* ### These should be given initial values from the client */
1675         hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1676         hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1677         hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1678
1679         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1680         hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1681         hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1682         hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1683         hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1684         hv_store(href, "PID", 3, newSViv(getpid()), 0);
1685         if (q->auth) {
1686             char *user = NULL;
1687             char *passwd = NULL;
1688             if (q->auth->which == Z_IdAuthentication_open) {
1689                 char *cp;
1690                 user = nmem_strdup (odr_getmem (q->stream), q->auth->u.open);
1691                 cp = strchr (user, '/');
1692                 if (cp) {
1693                     /* password after / given */
1694                     *cp = '\0';
1695                     passwd = cp+1;
1696                 }
1697             } else if (q->auth->which == Z_IdAuthentication_idPass) {
1698                 user = q->auth->u.idPass->userId;
1699                 passwd = q->auth->u.idPass->password;
1700             }
1701             /* ### some code paths have user/password unassigned here */
1702             if (user)
1703                 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1704             if (passwd)
1705                 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1706         }
1707
1708         PUSHMARK(sp);   
1709
1710         XPUSHs(sv_2mortal(newRV((SV*) href)));
1711
1712         PUTBACK;
1713
1714         if (init_ref != NULL)
1715         {
1716              handler_cv = simpleserver_sv2cv( init_ref );
1717              perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1718         }
1719
1720         SPAGAIN;
1721
1722         temp = hv_fetch(href, "IMP_ID", 6, 1);
1723         ptr = SvPV(*temp, len);
1724         q->implementation_id = nmem_strdup(nmem, ptr);
1725
1726         temp = hv_fetch(href, "IMP_NAME", 8, 1);
1727         ptr = SvPV(*temp, len);
1728         q->implementation_name = nmem_strdup(nmem, ptr);
1729
1730         temp = hv_fetch(href, "IMP_VER", 7, 1);
1731         ptr = SvPV(*temp, len);
1732         q->implementation_version = nmem_strdup(nmem, ptr);
1733
1734         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1735         r->errcode = SvIV(*temp);
1736
1737         temp = hv_fetch(href, "ERR_STR", 7, 1);
1738         ptr = SvPV(*temp, len);
1739         r->errstring = (char *)odr_malloc(q->stream, len + 1);
1740         strcpy(r->errstring, ptr);
1741
1742         temp = hv_fetch(href, "HANDLE", 6, 1);
1743         handle= newSVsv(*temp);
1744         zhandle->handle = handle;
1745
1746         r->handle = zhandle;
1747
1748         hv_undef(href);
1749         sv_free((SV*) href);
1750
1751         PUTBACK;
1752         FREETMPS;
1753         LEAVE;
1754         
1755         return r;       
1756 }
1757
1758 void bend_close(void *handle)
1759 {
1760         HV *href;
1761         Zfront_handle *zhandle = (Zfront_handle *)handle;
1762         CV* handler_cv = 0;
1763         int stop_flag = 0;
1764         dSP;
1765         ENTER;
1766         SAVETMPS;
1767
1768         if (close_ref)
1769         {
1770                 href = newHV();
1771                 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1772                 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1773
1774                 PUSHMARK(sp);
1775
1776                 XPUSHs(sv_2mortal(newRV((SV *)href)));
1777
1778                 PUTBACK;
1779         
1780                 handler_cv = simpleserver_sv2cv( close_ref );
1781                 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1782         
1783                 SPAGAIN;
1784
1785                 sv_free((SV*) href);
1786         }
1787         else
1788                 sv_free(zhandle->handle);
1789         PUTBACK;
1790         FREETMPS;
1791         LEAVE;
1792         stop_flag = zhandle->stop_flag;
1793         nmem_destroy(zhandle->nmem);
1794         simpleserver_free();
1795
1796         if (stop_flag)
1797                 exit(0);
1798         return;
1799 }
1800
1801
1802 MODULE = Net::Z3950::SimpleServer       PACKAGE = Net::Z3950::SimpleServer
1803
1804 PROTOTYPES: DISABLE
1805
1806
1807 void
1808 set_ghandle(arg)
1809                 SV *arg
1810         CODE:
1811                 _global_ghandle = newSVsv(arg);
1812                 
1813
1814 void
1815 set_init_handler(arg)
1816                 SV *arg
1817         CODE:
1818                 init_ref = newSVsv(arg);
1819                 
1820
1821 void
1822 set_close_handler(arg)
1823                 SV *arg
1824         CODE:
1825                 close_ref = newSVsv(arg);
1826
1827
1828 void
1829 set_sort_handler(arg)
1830                 SV *arg
1831         CODE:
1832                 sort_ref = newSVsv(arg);
1833
1834 void
1835 set_search_handler(arg)
1836                 SV *arg
1837         CODE:
1838                 search_ref = newSVsv(arg);
1839
1840
1841 void
1842 set_fetch_handler(arg)
1843                 SV *arg
1844         CODE:
1845                 fetch_ref = newSVsv(arg);
1846
1847
1848 void
1849 set_present_handler(arg)
1850                 SV *arg
1851         CODE:
1852                 present_ref = newSVsv(arg);
1853
1854
1855 void
1856 set_esrequest_handler(arg)
1857                 SV *arg
1858         CODE:
1859                 esrequest_ref = newSVsv(arg);
1860
1861
1862 void
1863 set_delete_handler(arg)
1864                 SV *arg
1865         CODE:
1866                 delete_ref = newSVsv(arg);
1867
1868
1869 void
1870 set_scan_handler(arg)
1871                 SV *arg
1872         CODE:
1873                 scan_ref = newSVsv(arg);
1874
1875 void
1876 set_explain_handler(arg)
1877                 SV *arg
1878         CODE:
1879                 explain_ref = newSVsv(arg);
1880
1881 int
1882 start_server(...)
1883         PREINIT:
1884                 char **argv;
1885                 char **argv_buf;
1886                 char *ptr;
1887                 int i;
1888                 STRLEN len;
1889         CODE:
1890                 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1891                 argv = argv_buf;
1892                 for (i = 0; i < items; i++)
1893                 {
1894                         ptr = SvPV(ST(i), len);
1895                         *argv_buf = (char *)xmalloc(len + 1);
1896                         strcpy(*argv_buf++, ptr); 
1897                 }
1898                 *argv_buf = NULL;
1899                 root_perl_context = PERL_GET_CONTEXT;
1900                 yaz_mutex_create(&simpleserver_mutex);
1901 #if 0
1902                 /* only for debugging perl_clone .. */
1903                 tst_clones();
1904 #endif
1905                 
1906                 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1907         OUTPUT:
1908                 RETVAL
1909
1910
1911 int
1912 ScanSuccess()
1913         CODE:
1914                 RETVAL = BEND_SCAN_SUCCESS;
1915         OUTPUT:
1916                 RETVAL
1917
1918 int
1919 ScanPartial()
1920         CODE:
1921                 RETVAL = BEND_SCAN_PARTIAL;
1922         OUTPUT:
1923                 RETVAL
1924
1925  
1926 void
1927 yazlog(arg)
1928                 SV *arg
1929         CODE:
1930                 STRLEN len;
1931                 char *ptr;
1932                 ptr = SvPV(arg, len);
1933                 yaz_log(YLOG_LOG, "%.*s", (int) len, ptr);
1934
1935 int
1936 yaz_diag_srw_to_bib1(srw_code)
1937         int srw_code
1938
1939 int
1940 yaz_diag_bib1_to_srw(bib1_code)
1941         int bib1_code
1942