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