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