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