74d22814e99aa6368e13c800e481b6b7bdd1cda8
[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.8  2001-05-21 11:07:02  sondberg
29 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
30 /*
31 /*Revision 1.7  2001/03/13 14:17:15  sondberg
32 /*Added support for GRS-1.
33 /**/
34
35
36 #include "EXTERN.h"
37 #include "perl.h"
38 #include "XSUB.h"
39 #include <yaz/backend.h>
40 #include <yaz/log.h>
41 #include <yaz/wrbuf.h>
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <ctype.h>
46 #define GRS_MAX_FIELDS 500 
47 #ifdef ASN_COMPILED
48 #include <yaz/ill.h>
49 #endif
50 #ifndef sv_undef                /* To fix the problem with Perl 5.6.0 */
51 #define sv_undef PL_sv_undef
52 #endif
53
54 typedef struct {
55         SV *handle;
56
57         SV *init_ref;
58         SV *close_ref;
59         SV *sort_ref;
60         SV *search_ref;
61         SV *fetch_ref;
62         SV *present_ref;
63         SV *esrequest_ref;
64         SV *delete_ref;
65         SV *scan_ref;
66 } Zfront_handle;
67
68 SV *init_ref = NULL;
69 SV *close_ref = NULL;
70 SV *sort_ref = NULL;
71 SV *search_ref = NULL;
72 SV *fetch_ref = NULL;
73 SV *present_ref = NULL;
74 SV *esrequest_ref = NULL;
75 SV *delete_ref = NULL;
76 SV *scan_ref = NULL;
77 int MAX_OID = 15;
78
79
80 Z_GenericRecord *read_grs1(char *str, ODR o)
81 {
82         int type, ivalue;
83         char line[512], *buf, *ptr, *original;
84         char value[512];
85         Z_GenericRecord *r = 0;
86
87         original = str;
88         for (;;)
89         {
90                 Z_TaggedElement *t;
91                 Z_ElementData *c;
92         
93                 ptr = strchr(str, '\n');
94                 if (!ptr) {
95                         return r;
96                 }
97                 strncpy(line, str, ptr - str);
98                 line[ptr - str] = 0;
99                 buf = line;
100                 str = ptr + 1;
101                 while (*buf && isspace(*buf))
102                         buf++;
103                 if (*buf == '}') {
104                         memmove(original, str, strlen(str));
105                         return r;
106                 }
107                 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
108                 {
109                         yaz_log(LOG_WARN, "Bad data in '%s'", buf);
110                         return 0;
111                 }
112                 if (!type && *value == '0')
113                         return r;
114                 if (!(buf = strchr(buf, ')')))
115                         return 0;
116                 buf++;
117                 while (*buf && isspace(*buf))
118                         buf++;
119                 if (!*buf)
120                         return 0;
121                 if (!r)
122                 {
123                         r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
124                         r->elements = (Z_TaggedElement **)
125                         odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
126                         r->num_elements = 0;
127                 }
128                 if (r->num_elements > GRS_MAX_FIELDS)
129                 {
130                         yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
131                         exit(0);
132                 }
133                 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
134                 t->tagType = (int *)odr_malloc(o, sizeof(int));
135                 *t->tagType = type;
136                 t->tagValue = (Z_StringOrNumeric *)
137                         odr_malloc(o, sizeof(Z_StringOrNumeric));
138                 if ((ivalue = atoi(value)))
139                 {
140                         t->tagValue->which = Z_StringOrNumeric_numeric;
141                         t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
142                         *t->tagValue->u.numeric = ivalue;
143                 }
144                 else
145                 {
146                         t->tagValue->which = Z_StringOrNumeric_string;
147                         t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
148                         strcpy(t->tagValue->u.string, value);
149                 }
150                 t->tagOccurrence = 0;
151                 t->metaData = 0;
152                 t->appliedVariant = 0;
153                 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
154                 if (*buf == '{')
155                 {
156                         c->which = Z_ElementData_subtree;
157                         c->u.subtree = read_grs1(str, o);
158                 }
159                 else
160                 {
161                         c->which = Z_ElementData_string;
162 /*                      buf[strlen(buf)-1] = '\0';*/
163                         buf[strlen(buf)] = '\0';
164                         c->u.string = odr_strdup(o, buf);
165                 }
166                 r->num_elements++;
167         }
168 }
169
170
171
172
173 static void oid2str(Odr_oid *o, WRBUF buf)
174 {
175     for (; *o >= 0; o++) {
176         char ibuf[16];
177         sprintf(ibuf, "%d", *o);
178         wrbuf_puts(buf, ibuf);
179         if (o[1] > 0)
180             wrbuf_putc(buf, '.');
181     }
182 }
183
184
185 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
186 {
187     switch (s->which) {
188         case Z_RPNStructure_simple: {
189             Z_Operand *o = s->u.simple;
190
191             switch (o->which) {
192                 case Z_Operand_APT: {
193                     Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
194
195                     if (at->attributes) {
196                         int i;
197                         char ibuf[16];
198
199                         for (i = 0; i < at->attributes->num_attributes; i++) {
200                             wrbuf_puts(buf, "@attr ");
201                             if (at->attributes->attributes[i]->attributeSet) {
202                                 oid2str(at->attributes->attributes[i]->attributeSet, buf);
203                                 wrbuf_putc(buf, ' ');
204                             }
205                             sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
206                             assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
207                             wrbuf_puts(buf, ibuf);
208                             sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
209                             wrbuf_puts(buf, ibuf);
210                         }
211                     }
212                     switch (at->term->which) {
213                         case Z_Term_general: {
214                             wrbuf_putc(buf, '"');
215                             wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
216                             wrbuf_puts(buf, "\" ");
217                             break;
218                         }
219                         default: abort();
220                     }
221                     break;
222                 }
223                 default: abort();
224             }
225             break;
226         }
227         case Z_RPNStructure_complex: {
228             Z_Complex *c = s->u.complex;
229
230             switch (c->roperator->which) {
231                 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
232                 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
233                 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
234                 case Z_Operator_prox: abort();
235                 default: abort();
236             }
237             if (!rpn2pquery(c->s1, buf))
238                 return 0;
239             if (!rpn2pquery(c->s2, buf))
240                 return 0;
241             break;
242         }
243         default: abort();
244     }
245     return 1;
246 }
247
248
249 WRBUF zquery2pquery(Z_Query *q)
250 {
251     WRBUF buf = wrbuf_alloc();
252
253     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
254         return 0;
255     if (q->u.type_1->attributeSetId) {
256         /* Output attribute set ID */
257         wrbuf_puts(buf, "@attrset ");
258         oid2str(q->u.type_1->attributeSetId, buf);
259         wrbuf_putc(buf, ' ');
260     }
261     return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
262 }
263
264
265 int bend_sort(void *handle, bend_sort_rr *rr)
266 {
267         HV *href;
268         AV *aref;
269         SV **temp;
270         SV *err_code;
271         SV *err_str;
272         SV *status;
273         STRLEN len;
274         char *ptr;
275         char *ODR_err_str;
276         char **input_setnames;
277         Zfront_handle *zhandle = (Zfront_handle *)handle;
278         int i;
279         
280         dSP;
281         ENTER;
282         SAVETMPS;
283         
284         aref = newAV();
285         input_setnames = rr->input_setnames;
286         for (i = 0; i < rr->num_input_setnames; i++)
287         {
288                 av_push(aref, newSVpv(*input_setnames++, 0));
289         }
290         href = newHV();
291         hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
292         hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
293         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
294         hv_store(href, "STATUS", 6, newSViv(0), 0);
295
296         PUSHMARK(sp);
297
298         XPUSHs(sv_2mortal(newRV( (SV*) href)));
299
300         PUTBACK;
301
302         perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
303
304         SPAGAIN;
305
306         temp = hv_fetch(href, "ERR_CODE", 8, 1);
307         err_code = newSVsv(*temp);
308
309         temp = hv_fetch(href, "ERR_STR", 7, 1);
310         err_str = newSVsv(*temp);
311
312         temp = hv_fetch(href, "STATUS", 6, 1);
313         status = newSVsv(*temp);
314
315
316         
317
318         PUTBACK;
319         FREETMPS;
320         LEAVE;
321
322         hv_undef(href),
323         av_undef(aref);
324         rr->errcode = SvIV(err_code);
325         rr->sort_status = SvIV(status);
326         ptr = SvPV(err_str, len);
327         ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
328         strcpy(ODR_err_str, ptr);
329         rr->errstring = ODR_err_str;
330
331         sv_free(err_code);
332         sv_free(err_str);
333         sv_free(status);
334         
335         return 0;
336 }
337
338
339 int bend_search(void *handle, bend_search_rr *rr)
340 {
341         HV *href;
342         AV *aref;
343         SV **temp;
344         SV *hits;
345         SV *err_code;
346         SV *err_str;
347         char *ODR_errstr;
348         STRLEN len;
349         int i;
350         char **basenames;
351         int n;
352         WRBUF query;
353         char *ptr;
354         SV *point;
355         SV *ODR_point;
356         Zfront_handle *zhandle = (Zfront_handle *)handle;
357
358         dSP;
359         ENTER;
360         SAVETMPS;
361
362         aref = newAV();
363         basenames = rr->basenames;
364         for (i = 0; i < rr->num_bases; i++)
365         {
366                 av_push(aref, newSVpv(*basenames++, 0));
367         }
368         href = newHV();         
369         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
370         hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
371         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
372         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
373         hv_store(href, "HITS", 4, newSViv(0), 0);
374         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
375         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
376         hv_store(href, "PID", 3, newSViv(getpid()), 0);
377         query = zquery2pquery(rr->query);
378         if (query)
379         {
380                 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
381         }
382         else
383         {       
384                 rr->errcode = 108;
385         }
386         PUSHMARK(sp);
387         
388         XPUSHs(sv_2mortal(newRV( (SV*) href)));
389         
390         PUTBACK;
391
392         n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
393
394         SPAGAIN;
395
396         temp = hv_fetch(href, "HITS", 4, 1);
397         hits = newSVsv(*temp);
398
399         temp = hv_fetch(href, "ERR_CODE", 8, 1);
400         err_code = newSVsv(*temp);
401
402         temp = hv_fetch(href, "ERR_STR", 7, 1);
403         err_str = newSVsv(*temp);
404
405         temp = hv_fetch(href, "HANDLE", 6, 1);
406         point = newSVsv(*temp);
407
408         PUTBACK;
409         FREETMPS;
410         LEAVE;
411         
412         hv_undef(href);
413         av_undef(aref);
414         rr->hits = SvIV(hits);
415         rr->errcode = SvIV(err_code);
416         ptr = SvPV(err_str, len);
417         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
418         strcpy(ODR_errstr, ptr);
419         rr->errstring = ODR_errstr;
420 /*      ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
421         memcpy(ODR_point, point, sizeof(*point));
422         zhandle->handle = ODR_point;*/
423         zhandle->handle = point;
424         handle = zhandle;
425         sv_free(hits);
426         sv_free(err_code);
427         sv_free(err_str);
428         sv_free( (SV*) aref);
429         sv_free( (SV*) href);
430         /*sv_free(point);*/
431         wrbuf_free(query, 1);
432         return 0;
433 }
434
435
436 WRBUF oid2dotted(int *oid)
437 {
438
439         WRBUF buf = wrbuf_alloc();
440         int dot = 0;
441
442         for (; *oid != -1 ; oid++)
443         {
444                 char ibuf[16];
445                 if (dot)
446                 {
447                         wrbuf_putc(buf, '.');
448                 }
449                 else
450                 {
451                         dot = 1;
452                 }
453                 sprintf(ibuf, "%d", *oid);
454                 wrbuf_puts(buf, ibuf);
455         }
456         return buf;
457 }
458                 
459
460 int dotted2oid(char *dotted, int *buffer)
461 {
462         int *oid;
463         char ibuf[16];
464         char *ptr;
465         int n = 0;
466
467         ptr = ibuf;
468         oid = buffer;
469         while (*dotted)
470         {
471                 if (*dotted == '.')
472                 {
473                         n++;
474                         if (n == MAX_OID)  /* Terminate if more than MAX_OID entries */
475                         {
476                                 *oid = -1;
477                                 return -1;
478                         }
479                         *ptr = 0;
480                         sscanf(ibuf, "%d", oid++);
481                         ptr = ibuf;
482                         dotted++;
483
484                 }
485                 else
486                 {
487                         *ptr++ = *dotted++;
488                 }
489         }
490         if (n < MAX_OID)
491         {
492                 *ptr = 0;
493                 sscanf(ibuf, "%d", oid++);
494         }
495         *oid = -1;
496         return 0;
497 }
498
499
500 int bend_fetch(void *handle, bend_fetch_rr *rr)
501 {
502         HV *href;
503         SV **temp;
504         SV *basename;
505         SV *record;
506         SV *last;
507         SV *err_code;
508         SV *err_string;
509         SV *sur_flag;
510         SV *point;
511         SV *rep_form;
512         char *ptr;
513         char *ODR_record;
514         char *ODR_basename;
515         char *ODR_errstr;
516         int *ODR_oid_buf;
517         oident *oid;
518         WRBUF oid_dotted;
519         Zfront_handle *zhandle = (Zfront_handle *)handle;
520
521         Z_RecordComposition *composition;
522         Z_ElementSetNames *simple;
523         STRLEN length;
524
525         dSP;
526         ENTER;
527         SAVETMPS;
528
529         rr->errcode = 0;
530         href = newHV();
531         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
532         temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
533         oid_dotted = oid2dotted(rr->request_format_raw);
534         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
535         hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
536         hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
537         hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
538         hv_store(href, "LAST", 4, newSViv(0), 0);
539         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
540         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
541         hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
542         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
543         hv_store(href, "PID", 3, newSViv(getpid()), 0);
544         if (rr->comp)
545         {
546                 composition = rr->comp;
547                 if (composition->which == Z_RecordComp_simple)
548                 {
549                         simple = composition->u.simple;
550                         if (simple->which == Z_ElementSetNames_generic)
551                         {
552                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
553                         } 
554                         else
555                         {
556                                 rr->errcode = 26;
557                         }
558                 }
559                 else
560                 {
561                         rr->errcode = 26;
562                 }
563         }
564
565         PUSHMARK(sp);
566
567         XPUSHs(sv_2mortal(newRV( (SV*) href)));
568
569         PUTBACK;
570         
571         perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
572
573         SPAGAIN;
574
575         temp = hv_fetch(href, "BASENAME", 8, 1);
576         basename = newSVsv(*temp);
577
578         temp = hv_fetch(href, "RECORD", 6, 1);
579         record = newSVsv(*temp);
580
581         temp = hv_fetch(href, "LAST", 4, 1);
582         last = newSVsv(*temp);
583
584         temp = hv_fetch(href, "ERR_CODE", 8, 1);
585         err_code = newSVsv(*temp);
586
587         temp = hv_fetch(href, "ERR_STR", 7, 1),
588         err_string = newSVsv(*temp);
589
590         temp = hv_fetch(href, "SUR_FLAG", 8, 1);
591         sur_flag = newSVsv(*temp);
592
593         temp = hv_fetch(href, "REP_FORM", 8, 1);
594         rep_form = newSVsv(*temp);
595
596         temp = hv_fetch(href, "HANDLE", 6, 1);
597         point = newSVsv(*temp);
598
599         PUTBACK;
600         FREETMPS;
601         LEAVE;
602
603         hv_undef(href);
604         
605         ptr = SvPV(basename, length);
606         ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
607         strcpy(ODR_basename, ptr);
608         rr->basename = ODR_basename;
609
610         ptr = SvPV(rep_form, length);
611         ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
612         if (dotted2oid(ptr, ODR_oid_buf) == -1)         /* Maximum number of OID elements exceeded */
613         {
614                 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
615         }
616         rr->output_format_raw = ODR_oid_buf;    
617         
618         ptr = SvPV(record, length);
619         oid = oid_getentbyoid(ODR_oid_buf);
620         if (oid->value == VAL_GRS1)             /* Treat GRS-1 records separately */
621         {
622                 rr->record = (char *) read_grs1(ptr, rr->stream);
623                 rr->len = -1;
624         }
625         else
626         {
627                 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
628                 strcpy(ODR_record, ptr);
629                 rr->record = ODR_record;
630                 rr->len = length;
631         }
632         zhandle->handle = point;
633         handle = zhandle;
634         rr->last_in_set = SvIV(last);
635         
636         if (!(rr->errcode))
637         {
638                 rr->errcode = SvIV(err_code);
639                 ptr = SvPV(err_string, length);
640                 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
641                 strcpy(ODR_errstr, ptr);
642                 rr->errstring = ODR_errstr;
643         }
644         rr->surrogate_flag = SvIV(sur_flag);
645
646         wrbuf_free(oid_dotted, 1);
647         sv_free((SV*) href);
648         sv_free(basename);
649         sv_free(record);
650         sv_free(last);
651         sv_free(err_string);
652         sv_free(err_code),
653         sv_free(sur_flag);
654         sv_free(rep_form);
655         
656         return 0;
657 }
658
659
660 int bend_present(void *handle, bend_present_rr *rr)
661 {
662
663         HV *href;
664         SV **temp;
665         SV *err_code;
666         SV *err_string;
667         SV *hits;
668         SV *point;
669         STRLEN len;
670         Z_RecordComposition *composition;
671         Z_ElementSetNames *simple;
672         char *ODR_errstr;
673         char *ptr;
674         Zfront_handle *zhandle = (Zfront_handle *)handle;
675
676 /*      WRBUF oid_dotted; */
677
678         dSP;
679         ENTER;
680         SAVETMPS;
681
682         href = newHV();
683         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
684         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
685         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
686         hv_store(href, "START", 5, newSViv(rr->start), 0);
687         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
688         hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
689         /*oid_dotted = oid2dotted(rr->request_format_raw);
690         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
691         hv_store(href, "HITS", 4, newSViv(0), 0);
692         hv_store(href, "PID", 3, newSViv(getpid()), 0);
693         if (rr->comp)
694         {
695                 composition = rr->comp;
696                 if (composition->which == Z_RecordComp_simple)
697                 {
698                         simple = composition->u.simple;
699                         if (simple->which == Z_ElementSetNames_generic)
700                         {
701                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
702                         } 
703                         else
704                         {
705                                 rr->errcode = 26;
706                                 return 0;
707                         }
708                 }
709                 else
710                 {
711                         rr->errcode = 26;
712                         return 0;
713                 }
714         }
715
716         PUSHMARK(sp);
717         
718         XPUSHs(sv_2mortal(newRV( (SV*) href)));
719         
720         PUTBACK;
721         
722         perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
723         
724         SPAGAIN;
725
726         temp = hv_fetch(href, "ERR_CODE", 8, 1);
727         err_code = newSVsv(*temp);
728
729         temp = hv_fetch(href, "ERR_STR", 7, 1);
730         err_string = newSVsv(*temp);
731
732         temp = hv_fetch(href, "HITS", 4, 1);
733         hits = newSVsv(*temp);
734
735         temp = hv_fetch(href, "HANDLE", 6, 1);
736         point = newSVsv(*temp);
737
738         PUTBACK;
739         FREETMPS;
740         LEAVE;
741         
742         hv_undef(href);
743         rr->errcode = SvIV(err_code);
744         rr->hits = SvIV(hits);
745
746         ptr = SvPV(err_string, len);
747         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
748         strcpy(ODR_errstr, ptr);
749         rr->errstring = ODR_errstr;
750 /*      wrbuf_free(oid_dotted, 1);*/
751         zhandle->handle = point;
752         handle = zhandle;
753         sv_free(err_code);
754         sv_free(err_string);
755         sv_free(hits);
756         sv_free( (SV*) href);
757
758         return 0;
759 }
760
761
762 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
763 {
764         perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
765         return 0;
766 }
767
768
769 int bend_delete(void *handle, bend_delete_rr *rr)
770 {
771         perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
772         return 0;
773 }
774
775
776 int bend_scan(void *handle, bend_scan_rr *rr)
777 {
778         perl_call_sv(scan_ref, G_VOID | G_DISCARD | G_NOARGS);
779         return 0;
780 }
781
782
783 bend_initresult *bend_init(bend_initrequest *q)
784 {
785         bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
786         HV *href;
787         SV **temp;
788         SV *name;
789         SV *ver;
790         SV *err_str;
791         SV *status;
792         Zfront_handle *zhandle =  (Zfront_handle *) xmalloc (sizeof(*zhandle));
793         STRLEN len;
794         int n;
795         SV *handle;
796         /*char *name_ptr;
797         char *ver_ptr;*/
798         char *ptr;
799
800         dSP;
801         ENTER;
802         SAVETMPS;
803
804         /*q->bend_sort = bend_sort;*/
805         if (search_ref)
806         {
807                 q->bend_search = bend_search;
808         }
809         if (present_ref)
810         {
811                 q->bend_present = bend_present;
812         }
813         /*q->bend_esrequest = bend_esrequest;*/
814         /*q->bend_delete = bend_delete;*/
815         if (fetch_ref)
816         {
817                 q->bend_fetch = bend_fetch;
818         }
819         /*q->bend_scan = bend_scan;*/
820         href = newHV(); 
821         hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
822         hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
823         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
824         hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
825         hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
826         hv_store(href, "PID", 3, newSViv(getpid()), 0);
827
828         PUSHMARK(sp);   
829
830         XPUSHs(sv_2mortal(newRV( (SV*) href)));
831
832         PUTBACK;
833
834         if (init_ref != NULL)
835         {
836                 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
837         }
838
839         SPAGAIN;
840
841         temp = hv_fetch(href, "IMP_NAME", 8, 1);
842         name = newSVsv(*temp);
843
844         temp = hv_fetch(href, "IMP_VER", 7, 1);
845         ver = newSVsv(*temp);
846
847         temp = hv_fetch(href, "ERR_CODE", 8, 1);
848         status = newSVsv(*temp);
849
850         temp = hv_fetch(href, "HANDLE", 6, 1);
851         handle= newSVsv(*temp);
852
853         hv_undef(href);
854         PUTBACK;
855         FREETMPS;
856         LEAVE;
857         zhandle->handle = handle;
858         r->errcode = SvIV(status);
859         r->handle = zhandle;
860         ptr = SvPV(name, len);
861         q->implementation_name = (char *)xmalloc(len + 1);
862         strcpy(q->implementation_name, ptr);
863 /*      q->implementation_name = SvPV(name, len);*/
864         ptr = SvPV(ver, len);
865         q->implementation_version = (char *)xmalloc(len + 1);
866         strcpy(q->implementation_version, ptr);
867         
868         return r;
869 }
870
871
872 void bend_close(void *handle)
873 {
874         HV *href;
875         Zfront_handle *zhandle = (Zfront_handle *)handle;
876         SV **temp;
877
878         dSP;
879         ENTER;
880         SAVETMPS;
881
882         if (close_ref == NULL)
883         {
884                 return;
885         }
886
887         href = newHV();
888         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
889
890         PUSHMARK(sp);
891
892         XPUSHs(sv_2mortal(newRV((SV *)href)));
893
894         PUTBACK;
895         
896         perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
897         
898         SPAGAIN;
899
900         PUTBACK;
901         FREETMPS;
902         LEAVE;
903
904         xfree(handle);
905         
906         return;
907 }
908
909
910 MODULE = Net::Z3950::SimpleServer       PACKAGE = Net::Z3950::SimpleServer
911
912 void
913 set_init_handler(arg)
914                 SV *arg
915         CODE:
916                 init_ref = newSVsv(arg);
917                 
918
919 void
920 set_close_handler(arg)
921                 SV *arg
922         CODE:
923                 close_ref = newSVsv(arg);
924
925
926 void
927 set_sort_handler(arg)
928                 SV *arg
929         CODE:
930                 sort_ref = newSVsv(arg);
931
932 void
933 set_search_handler(arg)
934                 SV *arg
935         CODE:
936                 search_ref = newSVsv(arg);
937
938
939 void
940 set_fetch_handler(arg)
941                 SV *arg
942         CODE:
943                 fetch_ref = newSVsv(arg);
944
945
946 void
947 set_present_handler(arg)
948                 SV *arg
949         CODE:
950                 present_ref = newSVsv(arg);
951
952
953 void
954 set_esrequest_handler(arg)
955                 SV *arg
956         CODE:
957                 esrequest_ref = newSVsv(arg);
958
959
960 void
961 set_delete_handler(arg)
962                 SV *arg
963         CODE:
964                 delete_ref = newSVsv(arg);
965
966
967 void
968 set_scan_handler(arg)
969                 SV *arg
970         CODE:
971                 scan_ref = newSVsv(arg);
972
973
974 int
975 start_server(...)
976         PREINIT:
977                 char **argv;
978                 char **argv_buf;
979                 char *ptr;
980                 int i;
981                 STRLEN len;
982         CODE:
983                 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
984                 argv = argv_buf;
985                 for (i = 0; i < items; i++)
986                 {
987                         ptr = SvPV(ST(i), len);
988                         *argv_buf = (char *)xmalloc(len + 1);
989                         strcpy(*argv_buf++, ptr); 
990                 }
991                 *argv_buf = NULL;
992                 
993                 RETVAL = statserv_main(items, argv, bend_init, bend_close);
994         OUTPUT:
995                 RETVAL