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