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