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