d8df0bfd7d64a69e240496826f5266117ab9c266
[simpleserver-moved-to-github.git] / SimpleServer.xs
1 /*
2  * Copyright (c) 2000, Index Data.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation, in whole or in part, for any purpose, is hereby granted,
6  * provided that:
7  *
8  * 1. This copyright and permission notice appear in all copies of the
9  * software and its documentation. Notices of copyright or attribution
10  * which appear at the beginning of any file must remain unchanged.
11  *
12  * 2. The name of Index Data or the individual authors may not be used to
13  * endorse or promote products derived from this software without specific
14  * prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
20  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
22  * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26
27 /*$Log: SimpleServer.xs,v $
28 /*Revision 1.14  2002-03-05 00:34:13  mike
29 /*Support for implementation_id (commented out until it's
30 /*in mainstream Yaz)
31 /*
32 /*Revision 1.13  2002/02/28 11:21:57  mike
33 /*Add RPN structure to search-handler argument hash.
34 /*
35 /*Revision 1.12  2001/08/30 14:02:10  sondberg
36 /*Small changes.
37 /*
38 /*Revision 1.11  2001/08/30 13:15:11  sondberg
39 /*Corrected a memory leak, one more to go.
40 /*
41 /*Revision 1.10  2001/08/29 11:48:36  sondberg
42 /*Added routines
43 /*
44 /*      Net::Z3950::SimpleServer::ScanSuccess
45 /*      Net::Z3950::SimpleServer::ScanPartial
46 /*
47 /*and a bit of documentation.
48 /*
49 /*Revision 1.9  2001/08/24 14:00:20  sondberg
50 /*Added support for scan.
51 /*
52 /*Revision 1.8  2001/05/21 11:07:02  sondberg
53 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
54 /*
55 /*Revision 1.7  2001/03/13 14:17:15  sondberg
56 /*Added support for GRS-1.
57 /**/
58
59
60 #include "EXTERN.h"
61 #include "perl.h"
62 #include "XSUB.h"
63 #include <yaz/backend.h>
64 #include <yaz/log.h>
65 #include <yaz/wrbuf.h>
66 #include <stdio.h>
67 #include <unistd.h>
68 #include <stdlib.h>
69 #include <ctype.h>
70 #define GRS_MAX_FIELDS 500 
71 #ifdef ASN_COMPILED
72 #include <yaz/ill.h>
73 #endif
74 #ifndef sv_undef                /* To fix the problem with Perl 5.6.0 */
75 #define sv_undef PL_sv_undef
76 #endif
77
78 typedef struct {
79         SV *handle;
80
81         SV *init_ref;
82         SV *close_ref;
83         SV *sort_ref;
84         SV *search_ref;
85         SV *fetch_ref;
86         SV *present_ref;
87         SV *esrequest_ref;
88         SV *delete_ref;
89         SV *scan_ref;
90 } Zfront_handle;
91
92 SV *init_ref = NULL;
93 SV *close_ref = NULL;
94 SV *sort_ref = NULL;
95 SV *search_ref = NULL;
96 SV *fetch_ref = NULL;
97 SV *present_ref = NULL;
98 SV *esrequest_ref = NULL;
99 SV *delete_ref = NULL;
100 SV *scan_ref = NULL;
101 int MAX_OID = 15;
102
103
104 Z_GenericRecord *read_grs1(char *str, ODR o)
105 {
106         int type, ivalue;
107         char line[512], *buf, *ptr, *original;
108         char value[512];
109         Z_GenericRecord *r = 0;
110
111         original = str;
112         for (;;)
113         {
114                 Z_TaggedElement *t;
115                 Z_ElementData *c;
116         
117                 ptr = strchr(str, '\n');
118                 if (!ptr) {
119                         return r;
120                 }
121                 strncpy(line, str, ptr - str);
122                 line[ptr - str] = 0;
123                 buf = line;
124                 str = ptr + 1;
125                 while (*buf && isspace(*buf))
126                         buf++;
127                 if (*buf == '}') {
128                         memmove(original, str, strlen(str));
129                         return r;
130                 }
131                 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
132                 {
133                         yaz_log(LOG_WARN, "Bad data in '%s'", buf);
134                         return 0;
135                 }
136                 if (!type && *value == '0')
137                         return r;
138                 if (!(buf = strchr(buf, ')')))
139                         return 0;
140                 buf++;
141                 while (*buf && isspace(*buf))
142                         buf++;
143                 if (!*buf)
144                         return 0;
145                 if (!r)
146                 {
147                         r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
148                         r->elements = (Z_TaggedElement **)
149                         odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
150                         r->num_elements = 0;
151                 }
152                 if (r->num_elements > GRS_MAX_FIELDS)
153                 {
154                         yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
155                         exit(0);
156                 }
157                 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
158                 t->tagType = (int *)odr_malloc(o, sizeof(int));
159                 *t->tagType = type;
160                 t->tagValue = (Z_StringOrNumeric *)
161                         odr_malloc(o, sizeof(Z_StringOrNumeric));
162                 if ((ivalue = atoi(value)))
163                 {
164                         t->tagValue->which = Z_StringOrNumeric_numeric;
165                         t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
166                         *t->tagValue->u.numeric = ivalue;
167                 }
168                 else
169                 {
170                         t->tagValue->which = Z_StringOrNumeric_string;
171                         t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
172                         strcpy(t->tagValue->u.string, value);
173                 }
174                 t->tagOccurrence = 0;
175                 t->metaData = 0;
176                 t->appliedVariant = 0;
177                 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
178                 if (*buf == '{')
179                 {
180                         c->which = Z_ElementData_subtree;
181                         c->u.subtree = read_grs1(str, o);
182                 }
183                 else
184                 {
185                         c->which = Z_ElementData_string;
186 /*                      buf[strlen(buf)-1] = '\0';*/
187                         buf[strlen(buf)] = '\0';
188                         c->u.string = odr_strdup(o, buf);
189                 }
190                 r->num_elements++;
191         }
192 }
193
194
195
196
197 static void oid2str(Odr_oid *o, WRBUF buf)
198 {
199     for (; *o >= 0; o++) {
200         char ibuf[16];
201         sprintf(ibuf, "%d", *o);
202         wrbuf_puts(buf, ibuf);
203         if (o[1] > 0)
204             wrbuf_putc(buf, '.');
205     }
206 }
207
208
209 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
210 {
211     switch (s->which) {
212         case Z_RPNStructure_simple: {
213             Z_Operand *o = s->u.simple;
214
215             switch (o->which) {
216                 case Z_Operand_APT: {
217                     Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
218
219                     if (at->attributes) {
220                         int i;
221                         char ibuf[16];
222
223                         for (i = 0; i < at->attributes->num_attributes; i++) {
224                             wrbuf_puts(buf, "@attr ");
225                             if (at->attributes->attributes[i]->attributeSet) {
226                                 oid2str(at->attributes->attributes[i]->attributeSet, buf);
227                                 wrbuf_putc(buf, ' ');
228                             }
229                             sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
230                             assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
231                             wrbuf_puts(buf, ibuf);
232                             sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
233                             wrbuf_puts(buf, ibuf);
234                         }
235                     }
236                     switch (at->term->which) {
237                         case Z_Term_general: {
238                             wrbuf_putc(buf, '"');
239                             wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
240                             wrbuf_puts(buf, "\" ");
241                             break;
242                         }
243                         default: abort();
244                     }
245                     break;
246                 }
247                 default: abort();
248             }
249             break;
250         }
251         case Z_RPNStructure_complex: {
252             Z_Complex *c = s->u.complex;
253
254             switch (c->roperator->which) {
255                 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
256                 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
257                 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
258                 case Z_Operator_prox: abort();
259                 default: abort();
260             }
261             if (!rpn2pquery(c->s1, buf))
262                 return 0;
263             if (!rpn2pquery(c->s2, buf))
264                 return 0;
265             break;
266         }
267         default: abort();
268     }
269     return 1;
270 }
271
272
273 WRBUF zquery2pquery(Z_Query *q)
274 {
275     WRBUF buf = wrbuf_alloc();
276
277     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
278         return 0;
279     if (q->u.type_1->attributeSetId) {
280         /* Output attribute set ID */
281         wrbuf_puts(buf, "@attrset ");
282         oid2str(q->u.type_1->attributeSetId, buf);
283         wrbuf_putc(buf, ' ');
284     }
285     return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
286 }
287
288
289 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
290 #include <stdarg.h>
291 void fatal(char *fmt, ...)
292 {
293     va_list ap;
294
295     fprintf(stderr, "FATAL (yazwrap): ");
296     va_start(ap, fmt);
297     vfprintf(stderr, fmt, ap);
298     va_end(ap);
299     fprintf(stderr, "\n");
300     abort();
301 }
302
303
304 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
305 /*
306  * Creates a new Perl object of type `class'; the newly-created scalar
307  * that is a reference to the blessed thingy `referent' is returned.
308  */
309 static SV *newObject(char *class, SV *referent)
310 {
311     HV *stash;
312     SV *sv;
313
314     sv = newRV_noinc((SV*) referent);
315     stash = gv_stashpv(class, 0);
316     if (stash == 0)
317         fatal("attempt to create object of undefined class '%s'", class);
318     /*assert(stash != 0);*/
319     sv_bless(sv, stash);
320     return sv;
321 }
322
323
324 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
325 static void setMember(HV *hv, char *name, SV *val)
326 {
327     /* We don't increment `val's reference count -- I think this is
328      * right because it's created with a refcount of 1, and in fact
329      * the reference via this hash is the only reference to it in
330      * general.
331      */
332     if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
333         fatal("couldn't store member in hash");
334 }
335
336
337 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
338 static SV *translateOID(Odr_oid *x)
339 {
340     /* Yaz represents an OID by an int array terminated by a negative
341      * value, typically -1; we represent it as a reference to a
342      * blessed scalar string of "."-separated elements.
343      */
344     char buf[1000];
345     int i;
346
347     *buf = '\0';
348     for (i = 0; x[i] >= 0; i++) {
349         sprintf(buf + strlen(buf), "%d", (int) x[i]);
350         if (x[i+1] >- 0)
351             strcat(buf, ".");
352     }
353
354     /*
355      * ### We'd like to return a blessed scalar (string) here, but of
356      *  course you can't do that in Perl: only references can be
357      *  blessed, so we'd have to return a _reference_ to a string, and
358      *  bless _that_.  Better to do without the blessing, I think.
359      */
360     if (1) {
361         return newSVpv(buf, 0);
362     } else {
363         return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
364     }
365 }
366
367
368 static SV *rpn2perl(Z_RPNStructure *s)
369 {
370     SV *sv;
371     HV *hv;
372     AV *av;
373
374     switch (s->which) {
375     case Z_RPNStructure_simple: {
376         Z_Operand *o = s->u.simple;
377         Z_AttributesPlusTerm *at;
378         if (o->which != Z_Operand_APT)
379             fatal("can't handle RPN simples other than APT");
380         at = o->u.attributesPlusTerm;
381         if (at->term->which != Z_Term_general)
382             fatal("can't handle RPN terms other than general");
383
384         sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
385         if (at->attributes) {
386             int i;
387             SV *attrs = newObject("Net::Z3950::RPN::Attributes",
388                                   (SV*) (av = newAV()));
389             for (i = 0; i < at->attributes->num_attributes; i++) {
390                 Z_AttributeElement *elem = at->attributes->attributes[i];
391                 HV *hv2;
392                 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
393                                     (SV*) (hv2 = newHV()));
394                 if (elem->attributeSet)
395                     setMember(hv2, "attributeSet",
396                               translateOID(elem->attributeSet));
397                 setMember(hv2, "attributeType",
398                           newSViv(*elem->attributeType));
399                 assert(elem->which == Z_AttributeValue_numeric);
400                 setMember(hv2, "attributeValue",
401                           newSViv(*elem->value.numeric));
402                 av_push(av, tmp);
403             }
404             setMember(hv, "attributes", attrs);
405         }
406         setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
407                                       at->term->u.general->len));
408         return sv;
409     }
410     case Z_RPNStructure_complex: {
411         SV *tmp;
412         Z_Complex *c = s->u.complex;
413         char *type = 0;         /* vacuous assignment satisfies gcc -Wall */
414         switch (c->roperator->which) {
415         case Z_Operator_and:     type = "Net::Z3950::RPN::And"; break;
416         case Z_Operator_or:      type = "Net::Z3950::RPN::Or"; break;
417         case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
418         case Z_Operator_prox:    fatal("proximity not yet supported");
419         default: fatal("unknown RPN operator %d", (int) c->roperator->which);
420         }
421         sv = newObject(type, (SV*) (av = newAV()));
422         if ((tmp = rpn2perl(c->s1)) == 0)
423             return 0;
424         av_push(av, tmp);
425         if ((tmp = rpn2perl(c->s2)) == 0)
426             return 0;
427         av_push(av, tmp);
428         return sv;
429     }
430     default: fatal("unknown RPN node type %d", (int) s->which);
431     }
432
433     return 0;
434 }
435
436
437 static SV *zquery2perl(Z_Query *q)
438 {
439     SV *sv;
440     HV *hv;
441
442     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
443         return 0;
444     sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
445     if (q->u.type_1->attributeSetId)
446         setMember(hv, "attributeSet",
447                   translateOID(q->u.type_1->attributeSetId));
448     setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
449     return sv;
450 }
451
452
453 int bend_sort(void *handle, bend_sort_rr *rr)
454 {
455         HV *href;
456         AV *aref;
457         SV **temp;
458         SV *err_code;
459         SV *err_str;
460         SV *status;
461         STRLEN len;
462         char *ptr;
463         char *ODR_err_str;
464         char **input_setnames;
465         Zfront_handle *zhandle = (Zfront_handle *)handle;
466         int i;
467         
468         dSP;
469         ENTER;
470         SAVETMPS;
471         
472         aref = newAV();
473         input_setnames = rr->input_setnames;
474         for (i = 0; i < rr->num_input_setnames; i++)
475         {
476                 av_push(aref, newSVpv(*input_setnames++, 0));
477         }
478         href = newHV();
479         hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
480         hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
481         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
482         hv_store(href, "STATUS", 6, newSViv(0), 0);
483
484         PUSHMARK(sp);
485
486         XPUSHs(sv_2mortal(newRV( (SV*) href)));
487
488         PUTBACK;
489
490         perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
491
492         SPAGAIN;
493
494         temp = hv_fetch(href, "ERR_CODE", 8, 1);
495         err_code = newSVsv(*temp);
496
497         temp = hv_fetch(href, "ERR_STR", 7, 1);
498         err_str = newSVsv(*temp);
499
500         temp = hv_fetch(href, "STATUS", 6, 1);
501         status = newSVsv(*temp);
502
503
504         
505
506         PUTBACK;
507         FREETMPS;
508         LEAVE;
509
510         hv_undef(href),
511         av_undef(aref);
512         rr->errcode = SvIV(err_code);
513         rr->sort_status = SvIV(status);
514         ptr = SvPV(err_str, len);
515         ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
516         strcpy(ODR_err_str, ptr);
517         rr->errstring = ODR_err_str;
518
519         sv_free(err_code);
520         sv_free(err_str);
521         sv_free(status);
522         
523         return 0;
524 }
525
526
527 int bend_search(void *handle, bend_search_rr *rr)
528 {
529         HV *href;
530         AV *aref;
531         SV **temp;
532         SV *hits;
533         SV *err_code;
534         SV *err_str;
535         char *ODR_errstr;
536         STRLEN len;
537         int i;
538         char **basenames;
539         int n;
540         WRBUF query;
541         char *ptr;
542         SV *point;
543         SV *ODR_point;
544         Zfront_handle *zhandle = (Zfront_handle *)handle;
545
546         dSP;
547         ENTER;
548         SAVETMPS;
549
550         aref = newAV();
551         basenames = rr->basenames;
552         for (i = 0; i < rr->num_bases; i++)
553         {
554                 av_push(aref, newSVpv(*basenames++, 0));
555         }
556         href = newHV();         
557         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
558         hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
559         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
560         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
561         hv_store(href, "HITS", 4, newSViv(0), 0);
562         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
563         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
564         hv_store(href, "PID", 3, newSViv(getpid()), 0);
565         hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
566         query = zquery2pquery(rr->query);
567         if (query)
568         {
569                 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
570         }
571         else
572         {       
573                 rr->errcode = 108;
574         }
575         PUSHMARK(sp);
576         
577         XPUSHs(sv_2mortal(newRV( (SV*) href)));
578         
579         PUTBACK;
580
581         n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
582
583         SPAGAIN;
584
585         temp = hv_fetch(href, "HITS", 4, 1);
586         hits = newSVsv(*temp);
587
588         temp = hv_fetch(href, "ERR_CODE", 8, 1);
589         err_code = newSVsv(*temp);
590
591         temp = hv_fetch(href, "ERR_STR", 7, 1);
592         err_str = newSVsv(*temp);
593
594         temp = hv_fetch(href, "HANDLE", 6, 1);
595         point = newSVsv(*temp);
596
597         PUTBACK;
598         FREETMPS;
599         LEAVE;
600         
601         hv_undef(href);
602         av_undef(aref);
603         rr->hits = SvIV(hits);
604         rr->errcode = SvIV(err_code);
605         ptr = SvPV(err_str, len);
606         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
607         strcpy(ODR_errstr, ptr);
608         rr->errstring = ODR_errstr;
609 /*      ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
610         memcpy(ODR_point, point, sizeof(*point));
611         zhandle->handle = ODR_point;*/
612         zhandle->handle = point;
613         handle = zhandle;
614         sv_free(hits);
615         sv_free(err_code);
616         sv_free(err_str);
617         sv_free( (SV*) aref);
618         sv_free( (SV*) href);
619         /*sv_free(point);*/
620         wrbuf_free(query, 1);
621         return 0;
622 }
623
624
625 /* ### this is worryingly similar to oid2str() */
626 WRBUF oid2dotted(int *oid)
627 {
628
629         WRBUF buf = wrbuf_alloc();
630         int dot = 0;
631
632         for (; *oid != -1 ; oid++)
633         {
634                 char ibuf[16];
635                 if (dot)
636                 {
637                         wrbuf_putc(buf, '.');
638                 }
639                 else
640                 {
641                         dot = 1;
642                 }
643                 sprintf(ibuf, "%d", *oid);
644                 wrbuf_puts(buf, ibuf);
645         }
646         return buf;
647 }
648                 
649
650 int dotted2oid(char *dotted, int *buffer)
651 {
652         int *oid;
653         char ibuf[16];
654         char *ptr;
655         int n = 0;
656
657         ptr = ibuf;
658         oid = buffer;
659         while (*dotted)
660         {
661                 if (*dotted == '.')
662                 {
663                         n++;
664                         if (n == MAX_OID)  /* Terminate if more than MAX_OID entries */
665                         {
666                                 *oid = -1;
667                                 return -1;
668                         }
669                         *ptr = 0;
670                         sscanf(ibuf, "%d", oid++);
671                         ptr = ibuf;
672                         dotted++;
673
674                 }
675                 else
676                 {
677                         *ptr++ = *dotted++;
678                 }
679         }
680         if (n < MAX_OID)
681         {
682                 *ptr = 0;
683                 sscanf(ibuf, "%d", oid++);
684         }
685         *oid = -1;
686         return 0;
687 }
688
689
690 int bend_fetch(void *handle, bend_fetch_rr *rr)
691 {
692         HV *href;
693         SV **temp;
694         SV *basename;
695         SV *record;
696         SV *last;
697         SV *err_code;
698         SV *err_string;
699         SV *sur_flag;
700         SV *point;
701         SV *rep_form;
702         char *ptr;
703         char *ODR_record;
704         char *ODR_basename;
705         char *ODR_errstr;
706         int *ODR_oid_buf;
707         oident *oid;
708         WRBUF oid_dotted;
709         Zfront_handle *zhandle = (Zfront_handle *)handle;
710
711         Z_RecordComposition *composition;
712         Z_ElementSetNames *simple;
713         STRLEN length;
714
715         dSP;
716         ENTER;
717         SAVETMPS;
718
719         rr->errcode = 0;
720         href = newHV();
721         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
722         temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
723         oid_dotted = oid2dotted(rr->request_format_raw);
724         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
725         hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
726         hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
727         hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
728         hv_store(href, "LAST", 4, newSViv(0), 0);
729         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
730         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
731         hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
732         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
733         hv_store(href, "PID", 3, newSViv(getpid()), 0);
734         if (rr->comp)
735         {
736                 composition = rr->comp;
737                 if (composition->which == Z_RecordComp_simple)
738                 {
739                         simple = composition->u.simple;
740                         if (simple->which == Z_ElementSetNames_generic)
741                         {
742                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
743                         } 
744                         else
745                         {
746                                 rr->errcode = 26;
747                         }
748                 }
749                 else
750                 {
751                         rr->errcode = 26;
752                 }
753         }
754
755         PUSHMARK(sp);
756
757         XPUSHs(sv_2mortal(newRV( (SV*) href)));
758
759         PUTBACK;
760         
761         perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
762
763         SPAGAIN;
764
765         temp = hv_fetch(href, "BASENAME", 8, 1);
766         basename = newSVsv(*temp);
767
768         temp = hv_fetch(href, "RECORD", 6, 1);
769         record = newSVsv(*temp);
770
771         temp = hv_fetch(href, "LAST", 4, 1);
772         last = newSVsv(*temp);
773
774         temp = hv_fetch(href, "ERR_CODE", 8, 1);
775         err_code = newSVsv(*temp);
776
777         temp = hv_fetch(href, "ERR_STR", 7, 1),
778         err_string = newSVsv(*temp);
779
780         temp = hv_fetch(href, "SUR_FLAG", 8, 1);
781         sur_flag = newSVsv(*temp);
782
783         temp = hv_fetch(href, "REP_FORM", 8, 1);
784         rep_form = newSVsv(*temp);
785
786         temp = hv_fetch(href, "HANDLE", 6, 1);
787         point = newSVsv(*temp);
788
789         PUTBACK;
790         FREETMPS;
791         LEAVE;
792
793         hv_undef(href);
794         
795         ptr = SvPV(basename, length);
796         ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
797         strcpy(ODR_basename, ptr);
798         rr->basename = ODR_basename;
799
800         ptr = SvPV(rep_form, length);
801         ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
802         if (dotted2oid(ptr, ODR_oid_buf) == -1)         /* Maximum number of OID elements exceeded */
803         {
804                 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
805         }
806         rr->output_format_raw = ODR_oid_buf;    
807         
808         ptr = SvPV(record, length);
809         oid = oid_getentbyoid(ODR_oid_buf);
810         if (oid->value == VAL_GRS1)             /* Treat GRS-1 records separately */
811         {
812                 rr->record = (char *) read_grs1(ptr, rr->stream);
813                 rr->len = -1;
814         }
815         else
816         {
817                 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
818                 strcpy(ODR_record, ptr);
819                 rr->record = ODR_record;
820                 rr->len = length;
821         }
822         zhandle->handle = point;
823         handle = zhandle;
824         rr->last_in_set = SvIV(last);
825         
826         if (!(rr->errcode))
827         {
828                 rr->errcode = SvIV(err_code);
829                 ptr = SvPV(err_string, length);
830                 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
831                 strcpy(ODR_errstr, ptr);
832                 rr->errstring = ODR_errstr;
833         }
834         rr->surrogate_flag = SvIV(sur_flag);
835
836         wrbuf_free(oid_dotted, 1);
837         sv_free((SV*) href);
838         sv_free(basename);
839         sv_free(record);
840         sv_free(last);
841         sv_free(err_string);
842         sv_free(err_code),
843         sv_free(sur_flag);
844         sv_free(rep_form);
845         
846         return 0;
847 }
848
849
850 int bend_present(void *handle, bend_present_rr *rr)
851 {
852
853         HV *href;
854         SV **temp;
855         SV *err_code;
856         SV *err_string;
857         SV *hits;
858         SV *point;
859         STRLEN len;
860         Z_RecordComposition *composition;
861         Z_ElementSetNames *simple;
862         char *ODR_errstr;
863         char *ptr;
864         Zfront_handle *zhandle = (Zfront_handle *)handle;
865
866 /*      WRBUF oid_dotted; */
867
868         dSP;
869         ENTER;
870         SAVETMPS;
871
872         href = newHV();
873         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
874         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
875         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
876         hv_store(href, "START", 5, newSViv(rr->start), 0);
877         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
878         hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
879         /*oid_dotted = oid2dotted(rr->request_format_raw);
880         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
881         hv_store(href, "HITS", 4, newSViv(0), 0);
882         hv_store(href, "PID", 3, newSViv(getpid()), 0);
883         if (rr->comp)
884         {
885                 composition = rr->comp;
886                 if (composition->which == Z_RecordComp_simple)
887                 {
888                         simple = composition->u.simple;
889                         if (simple->which == Z_ElementSetNames_generic)
890                         {
891                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
892                         } 
893                         else
894                         {
895                                 rr->errcode = 26;
896                                 return 0;
897                         }
898                 }
899                 else
900                 {
901                         rr->errcode = 26;
902                         return 0;
903                 }
904         }
905
906         PUSHMARK(sp);
907         
908         XPUSHs(sv_2mortal(newRV( (SV*) href)));
909         
910         PUTBACK;
911         
912         perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
913         
914         SPAGAIN;
915
916         temp = hv_fetch(href, "ERR_CODE", 8, 1);
917         err_code = newSVsv(*temp);
918
919         temp = hv_fetch(href, "ERR_STR", 7, 1);
920         err_string = newSVsv(*temp);
921
922         temp = hv_fetch(href, "HITS", 4, 1);
923         hits = newSVsv(*temp);
924
925         temp = hv_fetch(href, "HANDLE", 6, 1);
926         point = newSVsv(*temp);
927
928         PUTBACK;
929         FREETMPS;
930         LEAVE;
931         
932         hv_undef(href);
933         rr->errcode = SvIV(err_code);
934         rr->hits = SvIV(hits);
935
936         ptr = SvPV(err_string, len);
937         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
938         strcpy(ODR_errstr, ptr);
939         rr->errstring = ODR_errstr;
940 /*      wrbuf_free(oid_dotted, 1);*/
941         zhandle->handle = point;
942         handle = zhandle;
943         sv_free(err_code);
944         sv_free(err_string);
945         sv_free(hits);
946         sv_free( (SV*) href);
947
948         return 0;
949 }
950
951
952 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
953 {
954         perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
955         return 0;
956 }
957
958
959 int bend_delete(void *handle, bend_delete_rr *rr)
960 {
961         perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
962         return 0;
963 }
964
965
966 int bend_scan(void *handle, bend_scan_rr *rr)
967 {
968         HV *href;
969         AV *aref;
970         AV *list;
971         AV *entries;
972         HV *scan_item;
973         struct scan_entry *scan_list;
974         struct scan_entry *buffer;
975         int *step_size = rr->step_size;
976         int i;
977         char **basenames;
978         SV **temp;
979         SV *err_code = sv_newmortal();
980         SV *err_str = sv_newmortal();
981         SV *point = sv_newmortal();
982         SV *status = sv_newmortal();
983         SV *number = sv_newmortal();
984         char *ptr;
985         char *ODR_errstr;
986         STRLEN len;
987         int term_len;
988         SV *term_tmp;
989         SV *entries_ref;
990         
991         Zfront_handle *zhandle = (Zfront_handle *)handle;
992
993         dSP;
994         ENTER;
995         SAVETMPS;
996         href = newHV();
997         list = newAV();
998         if (rr->term->term->which == Z_Term_general)
999         {
1000                 term_len = rr->term->term->u.general->len;
1001                 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1002         } else {
1003                 rr->errcode = 229;      /* Unsupported term type */
1004                 return 0;
1005         }
1006         hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1007         hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1008         hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1009         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1010         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1011         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1012         hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1013         hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1014         aref = newAV();
1015         basenames = rr->basenames;
1016         for (i = 0; i < rr->num_bases; i++)
1017         {
1018                 av_push(aref, newSVpv(*basenames++, 0));
1019         }
1020         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1021
1022         PUSHMARK(sp);
1023
1024         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1025
1026         PUTBACK;
1027
1028         perl_call_sv(scan_ref, G_SCALAR | G_DISCARD);
1029
1030         SPAGAIN;
1031
1032         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1033         err_code = newSVsv(*temp);
1034
1035         temp = hv_fetch(href, "ERR_STR", 7, 1);
1036         err_str = newSVsv(*temp);
1037
1038         temp = hv_fetch(href, "HANDLE", 6, 1);
1039         point = newSVsv(*temp);
1040
1041         temp = hv_fetch(href, "STATUS", 6, 1);
1042         status = newSVsv(*temp);
1043         
1044         temp = hv_fetch(href, "NUMBER", 6, 1);
1045         number = newSVsv(*temp);
1046
1047         temp = hv_fetch(href, "ENTRIES", 7, 1);
1048         entries_ref = newSVsv(*temp);
1049
1050         PUTBACK;
1051         FREETMPS;
1052         LEAVE;
1053
1054         ptr = SvPV(err_str, len);
1055         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1056         strcpy(ODR_errstr, ptr);
1057         rr->errstring = ODR_errstr;
1058         rr->errcode = SvIV(err_code);
1059         rr->num_entries = SvIV(number);
1060         rr->status = SvIV(status);
1061         scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1062         buffer = scan_list;
1063         entries = (AV *)SvRV(entries_ref);
1064         for (i = 0; i < rr->num_entries; i++)
1065         {
1066                 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1067                 temp = hv_fetch(scan_item, "TERM", 4, 1);
1068                 ptr = SvPV(*temp, len);
1069                 buffer->term = (char *) odr_malloc (rr->stream, len + 1); 
1070                 strcpy(buffer->term, ptr);
1071                 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1); 
1072                 buffer->occurrences = SvIV(*temp);
1073                 buffer++;
1074                 hv_undef(scan_item);
1075         }
1076         rr->entries = scan_list;
1077         zhandle->handle = point;
1078         handle = zhandle;
1079         sv_free(err_code);
1080         sv_free(err_str);
1081         sv_free(status);
1082         sv_free(number);
1083         hv_undef(href);
1084         sv_free((SV *)href);
1085         av_undef(aref);
1086         sv_free((SV *)aref);
1087         av_undef(list);
1088         sv_free((SV *)list);
1089         av_undef(entries);
1090         /*sv_free((SV *)entries);*/
1091         sv_free(entries_ref);
1092
1093         return 0;
1094 }
1095
1096
1097 bend_initresult *bend_init(bend_initrequest *q)
1098 {
1099         bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1100         HV *href;
1101         SV **temp;
1102         SV *id;
1103         SV *name;
1104         SV *ver;
1105         SV *err_str;
1106         SV *status;
1107         Zfront_handle *zhandle =  (Zfront_handle *) xmalloc (sizeof(*zhandle));
1108         STRLEN len;
1109         int n;
1110         SV *handle;
1111         /*char *name_ptr;
1112         char *ver_ptr;*/
1113         char *ptr;
1114
1115         dSP;
1116         ENTER;
1117         SAVETMPS;
1118
1119         /*q->bend_sort = bend_sort;*/
1120         if (search_ref)
1121         {
1122                 q->bend_search = bend_search;
1123         }
1124         if (present_ref)
1125         {
1126                 q->bend_present = bend_present;
1127         }
1128         /*q->bend_esrequest = bend_esrequest;*/
1129         /*q->bend_delete = bend_delete;*/
1130         if (fetch_ref)
1131         {
1132                 q->bend_fetch = bend_fetch;
1133         }
1134         if (scan_ref)
1135         {
1136                 q->bend_scan = bend_scan;
1137         }
1138         href = newHV(); 
1139         hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1140         hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1141         hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1142         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1143         hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1144         hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1145         hv_store(href, "PID", 3, newSViv(getpid()), 0);
1146
1147         PUSHMARK(sp);   
1148
1149         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1150
1151         PUTBACK;
1152
1153         if (init_ref != NULL)
1154         {
1155                 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
1156         }
1157
1158         SPAGAIN;
1159
1160         temp = hv_fetch(href, "IMP_ID", 6, 1);
1161         id = newSVsv(*temp);
1162
1163         temp = hv_fetch(href, "IMP_NAME", 8, 1);
1164         name = newSVsv(*temp);
1165
1166         temp = hv_fetch(href, "IMP_VER", 7, 1);
1167         ver = newSVsv(*temp);
1168
1169         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1170         status = newSVsv(*temp);
1171
1172         temp = hv_fetch(href, "HANDLE", 6, 1);
1173         handle= newSVsv(*temp);
1174
1175         hv_undef(href);
1176         PUTBACK;
1177         FREETMPS;
1178         LEAVE;
1179         zhandle->handle = handle;
1180         r->errcode = SvIV(status);
1181         r->handle = zhandle;
1182 #if 0 /* implementation_id support is not yet in mainstream Yaz */
1183         ptr = SvPV(id, len);
1184         q->implementation_id = (char *)xmalloc(len + 1);
1185         strcpy(q->implementation_id, ptr);
1186 #endif
1187         ptr = SvPV(name, len);
1188         q->implementation_name = (char *)xmalloc(len + 1);
1189         strcpy(q->implementation_name, ptr);
1190 /*      q->implementation_name = SvPV(name, len);*/
1191         ptr = SvPV(ver, len);
1192         q->implementation_version = (char *)xmalloc(len + 1);
1193         strcpy(q->implementation_version, ptr);
1194         
1195         return r;
1196 }
1197
1198
1199 void bend_close(void *handle)
1200 {
1201         HV *href;
1202         Zfront_handle *zhandle = (Zfront_handle *)handle;
1203         SV **temp;
1204
1205         dSP;
1206         ENTER;
1207         SAVETMPS;
1208
1209         if (close_ref == NULL)
1210         {
1211                 return;
1212         }
1213
1214         href = newHV();
1215         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1216
1217         PUSHMARK(sp);
1218
1219         XPUSHs(sv_2mortal(newRV((SV *)href)));
1220
1221         PUTBACK;
1222         
1223         perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
1224         
1225         SPAGAIN;
1226
1227         PUTBACK;
1228         FREETMPS;
1229         LEAVE;
1230
1231         xfree(handle);
1232         
1233         return;
1234 }
1235
1236
1237 MODULE = Net::Z3950::SimpleServer       PACKAGE = Net::Z3950::SimpleServer
1238
1239 void
1240 set_init_handler(arg)
1241                 SV *arg
1242         CODE:
1243                 init_ref = newSVsv(arg);
1244                 
1245
1246 void
1247 set_close_handler(arg)
1248                 SV *arg
1249         CODE:
1250                 close_ref = newSVsv(arg);
1251
1252
1253 void
1254 set_sort_handler(arg)
1255                 SV *arg
1256         CODE:
1257                 sort_ref = newSVsv(arg);
1258
1259 void
1260 set_search_handler(arg)
1261                 SV *arg
1262         CODE:
1263                 search_ref = newSVsv(arg);
1264
1265
1266 void
1267 set_fetch_handler(arg)
1268                 SV *arg
1269         CODE:
1270                 fetch_ref = newSVsv(arg);
1271
1272
1273 void
1274 set_present_handler(arg)
1275                 SV *arg
1276         CODE:
1277                 present_ref = newSVsv(arg);
1278
1279
1280 void
1281 set_esrequest_handler(arg)
1282                 SV *arg
1283         CODE:
1284                 esrequest_ref = newSVsv(arg);
1285
1286
1287 void
1288 set_delete_handler(arg)
1289                 SV *arg
1290         CODE:
1291                 delete_ref = newSVsv(arg);
1292
1293
1294 void
1295 set_scan_handler(arg)
1296                 SV *arg
1297         CODE:
1298                 scan_ref = newSVsv(arg);
1299
1300
1301 int
1302 start_server(...)
1303         PREINIT:
1304                 char **argv;
1305                 char **argv_buf;
1306                 char *ptr;
1307                 int i;
1308                 STRLEN len;
1309         CODE:
1310                 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1311                 argv = argv_buf;
1312                 for (i = 0; i < items; i++)
1313                 {
1314                         ptr = SvPV(ST(i), len);
1315                         *argv_buf = (char *)xmalloc(len + 1);
1316                         strcpy(*argv_buf++, ptr); 
1317                 }
1318                 *argv_buf = NULL;
1319                 
1320                 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1321         OUTPUT:
1322                 RETVAL
1323
1324
1325 int
1326 ScanSuccess()
1327         CODE:
1328                 RETVAL = BEND_SCAN_SUCCESS;
1329         OUTPUT:
1330                 RETVAL
1331
1332 int
1333 ScanPartial()
1334         CODE:
1335                 RETVAL = BEND_SCAN_PARTIAL;
1336         OUTPUT:
1337                 RETVAL
1338
1339