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