Use only perl_clone if USE_ITHREADS is defined
[simpleserver-moved-to-github.git] / SimpleServer.xs
1 /*
2  * $Id: SimpleServer.xs,v 1.26 2004-05-29 07:04:26 adam Exp $ 
3  * ----------------------------------------------------------------------
4  * 
5  * Copyright (c) 2000-2004, Index Data.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation, in whole or in part, for any purpose, is hereby granted,
9  * provided that:
10  *
11  * 1. This copyright and permission notice appear in all copies of the
12  * software and its documentation. Notices of copyright or attribution
13  * which appear at the beginning of any file must remain unchanged.
14  *
15  * 2. The name of Index Data or the individual authors may not be used to
16  * endorse or promote products derived from this software without specific
17  * prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
21  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
22  * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
23  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
24  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
25  * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
26  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27  * OF THIS SOFTWARE.
28  */
29
30 #include "EXTERN.h"
31 #include "perl.h"
32 #include "proto.h"
33 #include "embed.h"
34 #include "XSUB.h"
35 #include <yaz/backend.h>
36 #include <yaz/log.h>
37 #include <yaz/wrbuf.h>
38 #include <stdio.h>
39 #ifdef WIN32
40 #else
41 #include <unistd.h>
42 #endif
43 #include <stdlib.h>
44 #include <ctype.h>
45 #define GRS_MAX_FIELDS 500 
46 #ifdef ASN_COMPILED
47 #include <yaz/ill.h>
48 #endif
49 #ifndef sv_undef                /* To fix the problem with Perl 5.6.0 */
50 #define sv_undef PL_sv_undef
51 #endif
52
53 NMEM_MUTEX simpleserver_mutex;
54
55 typedef struct {
56         SV *handle;
57
58         SV *init_ref;
59         SV *close_ref;
60         SV *sort_ref;
61         SV *search_ref;
62         SV *fetch_ref;
63         SV *present_ref;
64         SV *esrequest_ref;
65         SV *delete_ref;
66         SV *scan_ref;
67 } Zfront_handle;
68
69 SV *init_ref = NULL;
70 SV *close_ref = NULL;
71 SV *sort_ref = NULL;
72 SV *search_ref = NULL;
73 SV *fetch_ref = NULL;
74 SV *present_ref = NULL;
75 SV *esrequest_ref = NULL;
76 SV *delete_ref = NULL;
77 SV *scan_ref = NULL;
78 PerlInterpreter *root_perl_context;
79 int MAX_OID = 15;
80
81 #define GRS_BUF_SIZE 512
82
83 CV * simpleserver_sv2cv(SV *handler) {
84     STRLEN len;
85     char *buf;
86    
87     if (SvPOK(handler)) {
88         CV *ret;
89         buf = SvPV( handler, len);
90         if ( !( ret = perl_get_cv(buf, FALSE ) ) ) {
91             fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf );
92             exit(1);
93         }
94         
95         return ret;
96     } else {
97         return (CV *) handler;
98     }
99 }
100
101 /* debuggin routine to check for destruction of Perl interpreters */
102 #if 0
103 int tst_clones(void)
104 {
105     int i; 
106     PerlInterpreter *parent = PERL_GET_CONTEXT;
107     for (i = 0; i<500; i++)
108     {
109         PerlInterpreter *perl_interp = perl_clone(parent, 0);
110         PERL_SET_CONTEXT( perl_interp );
111         PL_perl_destruct_level = 2;
112         PERL_SET_CONTEXT( parent );
113         perl_destruct(perl_interp);
114         perl_free(perl_interp);
115     }
116     exit (0);
117 }
118 #endif
119
120 int simpleserver_clone(void) {
121 #ifdef USE_ITHREADS
122      nmem_mutex_enter(simpleserver_mutex);
123      if (1)
124      {
125          PerlInterpreter *current = PERL_GET_CONTEXT;
126
127          /* if current is unset, then we're in a new thread with
128           * no Perl interpreter for it. So we must create one .
129           * This will only happen when threaded is used..
130           */
131          if (!current) {
132              PerlInterpreter *perl_interp;
133              PERL_SET_CONTEXT( root_perl_context );
134              perl_interp = perl_clone(root_perl_context, 0);
135              PERL_SET_CONTEXT( perl_interp );
136          }
137      }
138      nmem_mutex_leave(simpleserver_mutex);
139 #endif
140      return 0;
141 }
142
143
144 void simpleserver_free(void) {
145     nmem_mutex_enter(simpleserver_mutex);
146     if (1)
147     {
148         PerlInterpreter *current_interp = PERL_GET_CONTEXT;
149
150         /* If current Perl Interp is different from root interp, then
151          * we're in threaded mode and we must destroy.. 
152          */
153         if (current_interp != root_perl_context) {
154             PL_perl_destruct_level = 2;
155             PERL_SET_CONTEXT(root_perl_context);
156             perl_destruct(current_interp);
157             perl_free(current_interp);
158         }
159     }
160     nmem_mutex_leave(simpleserver_mutex);
161 }
162
163
164 Z_GenericRecord *read_grs1(char *str, ODR o)
165 {
166         int type, ivalue;
167         char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
168         char value[GRS_BUF_SIZE+1];
169         Z_GenericRecord *r = 0;
170
171         original = str;
172         r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
173         r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
174         r->num_elements = 0;
175         
176         for (;;)
177         {
178                 Z_TaggedElement *t;
179                 Z_ElementData *c;
180                 int len;
181         
182                 ptr = strchr(str, '\n');
183                 if (!ptr) {
184                         return r;
185                 }
186                 len = ptr - str;
187                 if (len > GRS_BUF_SIZE) {
188                     yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
189                     len = GRS_BUF_SIZE;
190                 }
191                 strncpy(line, str, len);
192                 line[len] = 0;
193                 buf = line;
194                 str = ptr + 1;
195                 while (*buf && isspace(*buf))
196                         buf++;
197                 if (*buf == '}') {
198                         memmove(original, str, strlen(str));
199                         return r;
200                 }
201                 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
202                 {
203                         yaz_log(LOG_WARN, "Bad data in '%s'", buf);
204                         return r;
205                 }
206                 if (!type && *value == '0')
207                         return r;
208                 if (!(buf = strchr(buf, ')')))
209                         return r;
210                 buf++;
211                 while (*buf && isspace(*buf))
212                         buf++;
213                 if (r->num_elements >= GRS_MAX_FIELDS)
214                 {
215                         yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
216                         exit(0);
217                 }
218                 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
219                 t->tagType = (int *)odr_malloc(o, sizeof(int));
220                 *t->tagType = type;
221                 t->tagValue = (Z_StringOrNumeric *)
222                         odr_malloc(o, sizeof(Z_StringOrNumeric));
223                 if ((ivalue = atoi(value)))
224                 {
225                         t->tagValue->which = Z_StringOrNumeric_numeric;
226                         t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
227                         *t->tagValue->u.numeric = ivalue;
228                 }
229                 else
230                 {
231                         t->tagValue->which = Z_StringOrNumeric_string;
232                         t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
233                         strcpy(t->tagValue->u.string, value);
234                 }
235                 t->tagOccurrence = 0;
236                 t->metaData = 0;
237                 t->appliedVariant = 0;
238                 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
239                 if (*buf == '{')
240                 {
241                         c->which = Z_ElementData_subtree;
242                         c->u.subtree = read_grs1(str, o);
243                 }
244                 else
245                 {
246                         c->which = Z_ElementData_string;
247                         c->u.string = odr_strdup(o, buf);
248                 }
249                 r->num_elements++;
250         }
251 }
252
253
254
255
256 static void oid2str(Odr_oid *o, WRBUF buf)
257 {
258     for (; *o >= 0; o++) {
259         char ibuf[16];
260         sprintf(ibuf, "%d", *o);
261         wrbuf_puts(buf, ibuf);
262         if (o[1] > 0)
263             wrbuf_putc(buf, '.');
264     }
265 }
266
267
268 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
269 {
270     switch (s->which) {
271         case Z_RPNStructure_simple: {
272             Z_Operand *o = s->u.simple;
273
274             switch (o->which) {
275                 case Z_Operand_APT: {
276                     Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
277
278                     if (at->attributes) {
279                         int i;
280                         char ibuf[16];
281
282                         for (i = 0; i < at->attributes->num_attributes; i++) {
283                             wrbuf_puts(buf, "@attr ");
284                             if (at->attributes->attributes[i]->attributeSet) {
285                                 oid2str(at->attributes->attributes[i]->attributeSet, buf);
286                                 wrbuf_putc(buf, ' ');
287                             }
288                             sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
289                             assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
290                             wrbuf_puts(buf, ibuf);
291                             sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
292                             wrbuf_puts(buf, ibuf);
293                         }
294                     }
295                     switch (at->term->which) {
296                         case Z_Term_general: {
297                             wrbuf_putc(buf, '"');
298                             wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
299                             wrbuf_puts(buf, "\" ");
300                             break;
301                         }
302                         default: abort();
303                     }
304                     break;
305                 }
306                 default: abort();
307             }
308             break;
309         }
310         case Z_RPNStructure_complex: {
311             Z_Complex *c = s->u.complex;
312
313             switch (c->roperator->which) {
314                 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
315                 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
316                 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
317                 case Z_Operator_prox: abort();
318                 default: abort();
319             }
320             if (!rpn2pquery(c->s1, buf))
321                 return 0;
322             if (!rpn2pquery(c->s2, buf))
323                 return 0;
324             break;
325         }
326         default: abort();
327     }
328     return 1;
329 }
330
331
332 WRBUF zquery2pquery(Z_Query *q)
333 {
334     WRBUF buf = wrbuf_alloc();
335
336     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
337         return 0;
338     if (q->u.type_1->attributeSetId) {
339         /* Output attribute set ID */
340         wrbuf_puts(buf, "@attrset ");
341         oid2str(q->u.type_1->attributeSetId, buf);
342         wrbuf_putc(buf, ' ');
343     }
344     return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
345 }
346
347
348 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
349 #include <stdarg.h>
350 void fatal(char *fmt, ...)
351 {
352     va_list ap;
353
354     fprintf(stderr, "FATAL (yazwrap): ");
355     va_start(ap, fmt);
356     vfprintf(stderr, fmt, ap);
357     va_end(ap);
358     fprintf(stderr, "\n");
359     abort();
360 }
361
362
363 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
364 /*
365  * Creates a new Perl object of type `class'; the newly-created scalar
366  * that is a reference to the blessed thingy `referent' is returned.
367  */
368 static SV *newObject(char *class, SV *referent)
369 {
370     HV *stash;
371     SV *sv;
372
373     sv = newRV_noinc((SV*) referent);
374     stash = gv_stashpv(class, 0);
375     if (stash == 0)
376         fatal("attempt to create object of undefined class '%s'", class);
377     /*assert(stash != 0);*/
378     sv_bless(sv, stash);
379     return sv;
380 }
381
382
383 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
384 static void setMember(HV *hv, char *name, SV *val)
385 {
386     /* We don't increment `val's reference count -- I think this is
387      * right because it's created with a refcount of 1, and in fact
388      * the reference via this hash is the only reference to it in
389      * general.
390      */
391     if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
392         fatal("couldn't store member in hash");
393 }
394
395
396 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
397 static SV *translateOID(Odr_oid *x)
398 {
399     /* Yaz represents an OID by an int array terminated by a negative
400      * value, typically -1; we represent it as a reference to a
401      * blessed scalar string of "."-separated elements.
402      */
403     char buf[1000];
404     int i;
405
406     *buf = '\0';
407     for (i = 0; x[i] >= 0; i++) {
408         sprintf(buf + strlen(buf), "%d", (int) x[i]);
409         if (x[i+1] >- 0)
410             strcat(buf, ".");
411     }
412
413     /*
414      * ### We'd like to return a blessed scalar (string) here, but of
415      *  course you can't do that in Perl: only references can be
416      *  blessed, so we'd have to return a _reference_ to a string, and
417      *  bless _that_.  Better to do without the blessing, I think.
418      */
419     if (1) {
420         return newSVpv(buf, 0);
421     } else {
422         return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
423     }
424 }
425
426
427 static SV *rpn2perl(Z_RPNStructure *s)
428 {
429     SV *sv;
430     HV *hv;
431     AV *av;
432
433     switch (s->which) {
434     case Z_RPNStructure_simple: {
435         Z_Operand *o = s->u.simple;
436         Z_AttributesPlusTerm *at;
437         if (o->which != Z_Operand_APT)
438             fatal("can't handle RPN simples other than APT");
439         at = o->u.attributesPlusTerm;
440         if (at->term->which != Z_Term_general)
441             fatal("can't handle RPN terms other than general");
442
443         sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
444         if (at->attributes) {
445             int i;
446             SV *attrs = newObject("Net::Z3950::RPN::Attributes",
447                                   (SV*) (av = newAV()));
448             for (i = 0; i < at->attributes->num_attributes; i++) {
449                 Z_AttributeElement *elem = at->attributes->attributes[i];
450                 HV *hv2;
451                 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
452                                     (SV*) (hv2 = newHV()));
453                 if (elem->attributeSet)
454                     setMember(hv2, "attributeSet",
455                               translateOID(elem->attributeSet));
456                 setMember(hv2, "attributeType",
457                           newSViv(*elem->attributeType));
458                 assert(elem->which == Z_AttributeValue_numeric);
459                 setMember(hv2, "attributeValue",
460                           newSViv(*elem->value.numeric));
461                 av_push(av, tmp);
462             }
463             setMember(hv, "attributes", attrs);
464         }
465         setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
466                                       at->term->u.general->len));
467         return sv;
468     }
469     case Z_RPNStructure_complex: {
470         SV *tmp;
471         Z_Complex *c = s->u.complex;
472         char *type = 0;         /* vacuous assignment satisfies gcc -Wall */
473         switch (c->roperator->which) {
474         case Z_Operator_and:     type = "Net::Z3950::RPN::And"; break;
475         case Z_Operator_or:      type = "Net::Z3950::RPN::Or"; break;
476         case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
477         case Z_Operator_prox:    fatal("proximity not yet supported");
478         default: fatal("unknown RPN operator %d", (int) c->roperator->which);
479         }
480         sv = newObject(type, (SV*) (av = newAV()));
481         if ((tmp = rpn2perl(c->s1)) == 0)
482             return 0;
483         av_push(av, tmp);
484         if ((tmp = rpn2perl(c->s2)) == 0)
485             return 0;
486         av_push(av, tmp);
487         return sv;
488     }
489     default: fatal("unknown RPN node type %d", (int) s->which);
490     }
491
492     return 0;
493 }
494
495
496 static SV *zquery2perl(Z_Query *q)
497 {
498     SV *sv;
499     HV *hv;
500
501     if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) 
502         return 0;
503     sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
504     if (q->u.type_1->attributeSetId)
505         setMember(hv, "attributeSet",
506                   translateOID(q->u.type_1->attributeSetId));
507     setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
508     return sv;
509 }
510
511
512 int bend_sort(void *handle, bend_sort_rr *rr)
513 {
514         HV *href;
515         AV *aref;
516         SV **temp;
517         SV *err_code;
518         SV *err_str;
519         SV *status;
520         STRLEN len;
521         char *ptr;
522         char *ODR_err_str;
523         char **input_setnames;
524         Zfront_handle *zhandle = (Zfront_handle *)handle;
525         int i;
526         
527         dSP;
528         ENTER;
529         SAVETMPS;
530         
531         aref = newAV();
532         input_setnames = rr->input_setnames;
533         for (i = 0; i < rr->num_input_setnames; i++)
534         {
535                 av_push(aref, newSVpv(*input_setnames++, 0));
536         }
537         href = newHV();
538         hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
539         hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
540         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
541         hv_store(href, "STATUS", 6, newSViv(0), 0);
542
543         PUSHMARK(sp);
544
545         XPUSHs(sv_2mortal(newRV( (SV*) href)));
546
547         PUTBACK;
548
549         perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
550
551         SPAGAIN;
552
553         temp = hv_fetch(href, "ERR_CODE", 8, 1);
554         err_code = newSVsv(*temp);
555
556         temp = hv_fetch(href, "ERR_STR", 7, 1);
557         err_str = newSVsv(*temp);
558
559         temp = hv_fetch(href, "STATUS", 6, 1);
560         status = newSVsv(*temp);
561
562
563         
564
565         PUTBACK;
566         FREETMPS;
567         LEAVE;
568
569         hv_undef(href),
570         av_undef(aref);
571         rr->errcode = SvIV(err_code);
572         rr->sort_status = SvIV(status);
573         ptr = SvPV(err_str, len);
574         ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
575         strcpy(ODR_err_str, ptr);
576         rr->errstring = ODR_err_str;
577
578         sv_free(err_code);
579         sv_free(err_str);
580         sv_free(status);
581         
582         return 0;
583 }
584
585
586 int bend_search(void *handle, bend_search_rr *rr)
587 {
588         HV *href;
589         AV *aref;
590         SV **temp;
591         SV *hits;
592         SV *err_code;
593         SV *err_str;
594         char *ODR_errstr;
595         STRLEN len;
596         int i;
597         char **basenames;
598         int n;
599         WRBUF query;
600         char *ptr;
601         SV *point;
602         SV *ODR_point;
603         Zfront_handle *zhandle = (Zfront_handle *)handle;
604         CV* handler_cv = 0;
605
606         dSP;
607         ENTER;
608         SAVETMPS;
609
610         aref = newAV();
611         basenames = rr->basenames;
612         for (i = 0; i < rr->num_bases; i++)
613         {
614                 av_push(aref, newSVpv(*basenames++, 0));
615         }
616         href = newHV();         
617         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
618         hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
619         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
620         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
621         hv_store(href, "HITS", 4, newSViv(0), 0);
622         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
623         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
624         hv_store(href, "PID", 3, newSViv(getpid()), 0);
625         hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
626         query = zquery2pquery(rr->query);
627         if (query)
628         {
629                 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
630         }
631         else
632         {       
633                 rr->errcode = 108;
634         }
635         PUSHMARK(sp);
636         
637         XPUSHs(sv_2mortal(newRV( (SV*) href)));
638         
639         PUTBACK;
640
641         handler_cv = simpleserver_sv2cv( search_ref );
642         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
643
644         SPAGAIN;
645
646         temp = hv_fetch(href, "HITS", 4, 1);
647         hits = newSVsv(*temp);
648
649         temp = hv_fetch(href, "ERR_CODE", 8, 1);
650         err_code = newSVsv(*temp);
651
652         temp = hv_fetch(href, "ERR_STR", 7, 1);
653         err_str = newSVsv(*temp);
654
655         temp = hv_fetch(href, "HANDLE", 6, 1);
656         point = newSVsv(*temp);
657
658         PUTBACK;
659         FREETMPS;
660         LEAVE;
661         
662         hv_undef(href);
663         av_undef(aref);
664         rr->hits = SvIV(hits);
665         rr->errcode = SvIV(err_code);
666         ptr = SvPV(err_str, len);
667         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
668         strcpy(ODR_errstr, ptr);
669         rr->errstring = ODR_errstr;
670 /*      ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
671         memcpy(ODR_point, point, sizeof(*point));
672         zhandle->handle = ODR_point;*/
673         zhandle->handle = point;
674         handle = zhandle;
675         sv_free(hits);
676         sv_free(err_code);
677         sv_free(err_str);
678         sv_free( (SV*) aref);
679         sv_free( (SV*) href);
680         /*sv_free(point);*/
681         wrbuf_free(query, 1);
682         return 0;
683 }
684
685
686 /* ### this is worryingly similar to oid2str() */
687 WRBUF oid2dotted(int *oid)
688 {
689
690         WRBUF buf = wrbuf_alloc();
691         int dot = 0;
692
693         for (; *oid != -1 ; oid++)
694         {
695                 char ibuf[16];
696                 if (dot)
697                 {
698                         wrbuf_putc(buf, '.');
699                 }
700                 else
701                 {
702                         dot = 1;
703                 }
704                 sprintf(ibuf, "%d", *oid);
705                 wrbuf_puts(buf, ibuf);
706         }
707         return buf;
708 }
709                 
710
711 int dotted2oid(char *dotted, int *buffer)
712 {
713         int *oid;
714         char ibuf[16];
715         char *ptr;
716         int n = 0;
717
718         ptr = ibuf;
719         oid = buffer;
720         while (*dotted)
721         {
722                 if (*dotted == '.')
723                 {
724                         n++;
725                         if (n == MAX_OID)  /* Terminate if more than MAX_OID entries */
726                         {
727                                 *oid = -1;
728                                 return -1;
729                         }
730                         *ptr = 0;
731                         sscanf(ibuf, "%d", oid++);
732                         ptr = ibuf;
733                         dotted++;
734
735                 }
736                 else
737                 {
738                         *ptr++ = *dotted++;
739                 }
740         }
741         if (n < MAX_OID)
742         {
743                 *ptr = 0;
744                 sscanf(ibuf, "%d", oid++);
745         }
746         *oid = -1;
747         return 0;
748 }
749
750
751 int bend_fetch(void *handle, bend_fetch_rr *rr)
752 {
753         HV *href;
754         SV **temp;
755         SV *basename;
756         SV *record;
757         SV *last;
758         SV *err_code;
759         SV *err_string;
760         SV *sur_flag;
761         SV *point;
762         SV *rep_form;
763         char *ptr;
764         char *ODR_record;
765         char *ODR_basename;
766         char *ODR_errstr;
767         int *ODR_oid_buf;
768         oident *oid;
769         WRBUF oid_dotted;
770         Zfront_handle *zhandle = (Zfront_handle *)handle;
771         CV* handler_cv = 0;
772
773         Z_RecordComposition *composition;
774         Z_ElementSetNames *simple;
775         STRLEN length;
776
777         dSP;
778         ENTER;
779         SAVETMPS;
780
781         rr->errcode = 0;
782         href = newHV();
783         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
784         temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
785         oid_dotted = oid2dotted(rr->request_format_raw);
786         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
787         hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
788         hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
789         hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
790         hv_store(href, "LAST", 4, newSViv(0), 0);
791         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
792         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
793         hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
794         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
795         hv_store(href, "PID", 3, newSViv(getpid()), 0);
796         if (rr->comp)
797         {
798                 composition = rr->comp;
799                 if (composition->which == Z_RecordComp_simple)
800                 {
801                         simple = composition->u.simple;
802                         if (simple->which == Z_ElementSetNames_generic)
803                         {
804                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
805                         } 
806                         else
807                         {
808                                 rr->errcode = 26;
809                         }
810                 }
811                 else
812                 {
813                         rr->errcode = 26;
814                 }
815         }
816
817         PUSHMARK(sp);
818
819         XPUSHs(sv_2mortal(newRV( (SV*) href)));
820
821         PUTBACK;
822         
823         handler_cv = simpleserver_sv2cv( fetch_ref );
824         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
825
826         SPAGAIN;
827
828         temp = hv_fetch(href, "BASENAME", 8, 1);
829         basename = newSVsv(*temp);
830
831         temp = hv_fetch(href, "RECORD", 6, 1);
832         record = newSVsv(*temp);
833
834         temp = hv_fetch(href, "LAST", 4, 1);
835         last = newSVsv(*temp);
836
837         temp = hv_fetch(href, "ERR_CODE", 8, 1);
838         err_code = newSVsv(*temp);
839
840         temp = hv_fetch(href, "ERR_STR", 7, 1),
841         err_string = newSVsv(*temp);
842
843         temp = hv_fetch(href, "SUR_FLAG", 8, 1);
844         sur_flag = newSVsv(*temp);
845
846         temp = hv_fetch(href, "REP_FORM", 8, 1);
847         rep_form = newSVsv(*temp);
848
849         temp = hv_fetch(href, "HANDLE", 6, 1);
850         point = newSVsv(*temp);
851
852         PUTBACK;
853         FREETMPS;
854         LEAVE;
855
856         hv_undef(href);
857         
858         ptr = SvPV(basename, length);
859         ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
860         strcpy(ODR_basename, ptr);
861         rr->basename = ODR_basename;
862
863         ptr = SvPV(rep_form, length);
864         ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
865         if (dotted2oid(ptr, ODR_oid_buf) == -1)         /* Maximum number of OID elements exceeded */
866         {
867                 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
868         }
869         rr->output_format_raw = ODR_oid_buf;    
870         
871         ptr = SvPV(record, length);
872         oid = oid_getentbyoid(ODR_oid_buf);
873         if (oid->value == VAL_GRS1)             /* Treat GRS-1 records separately */
874         {
875                 rr->record = (char *) read_grs1(ptr, rr->stream);
876                 rr->len = -1;
877         }
878         else
879         {
880                 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
881                 strcpy(ODR_record, ptr);
882                 rr->record = ODR_record;
883                 rr->len = length;
884         }
885         zhandle->handle = point;
886         handle = zhandle;
887         rr->last_in_set = SvIV(last);
888         
889         if (!(rr->errcode))
890         {
891                 rr->errcode = SvIV(err_code);
892                 ptr = SvPV(err_string, length);
893                 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
894                 strcpy(ODR_errstr, ptr);
895                 rr->errstring = ODR_errstr;
896         }
897         rr->surrogate_flag = SvIV(sur_flag);
898
899         wrbuf_free(oid_dotted, 1);
900         sv_free((SV*) href);
901         sv_free(basename);
902         sv_free(record);
903         sv_free(last);
904         sv_free(err_string);
905         sv_free(err_code),
906         sv_free(sur_flag);
907         sv_free(rep_form);
908         
909         return 0;
910 }
911
912
913 int bend_present(void *handle, bend_present_rr *rr)
914 {
915
916         HV *href;
917         SV **temp;
918         SV *err_code;
919         SV *err_string;
920         SV *hits;
921         SV *point;
922         STRLEN len;
923         Z_RecordComposition *composition;
924         Z_ElementSetNames *simple;
925         char *ODR_errstr;
926         char *ptr;
927         Zfront_handle *zhandle = (Zfront_handle *)handle;
928         CV* handler_cv = 0;
929
930 /*      WRBUF oid_dotted; */
931
932         dSP;
933         ENTER;
934         SAVETMPS;
935
936         href = newHV();
937         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
938         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
939         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
940         hv_store(href, "START", 5, newSViv(rr->start), 0);
941         hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
942         hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
943         /*oid_dotted = oid2dotted(rr->request_format_raw);
944         hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
945         hv_store(href, "HITS", 4, newSViv(0), 0);
946         hv_store(href, "PID", 3, newSViv(getpid()), 0);
947         if (rr->comp)
948         {
949                 composition = rr->comp;
950                 if (composition->which == Z_RecordComp_simple)
951                 {
952                         simple = composition->u.simple;
953                         if (simple->which == Z_ElementSetNames_generic)
954                         {
955                                 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
956                         } 
957                         else
958                         {
959                                 rr->errcode = 26;
960                                 return 0;
961                         }
962                 }
963                 else
964                 {
965                         rr->errcode = 26;
966                         return 0;
967                 }
968         }
969
970         PUSHMARK(sp);
971         
972         XPUSHs(sv_2mortal(newRV( (SV*) href)));
973         
974         PUTBACK;
975         
976         handler_cv = simpleserver_sv2cv( present_ref );
977         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
978         
979         SPAGAIN;
980
981         temp = hv_fetch(href, "ERR_CODE", 8, 1);
982         err_code = newSVsv(*temp);
983
984         temp = hv_fetch(href, "ERR_STR", 7, 1);
985         err_string = newSVsv(*temp);
986
987         temp = hv_fetch(href, "HITS", 4, 1);
988         hits = newSVsv(*temp);
989
990         temp = hv_fetch(href, "HANDLE", 6, 1);
991         point = newSVsv(*temp);
992
993         PUTBACK;
994         FREETMPS;
995         LEAVE;
996         
997         hv_undef(href);
998         rr->errcode = SvIV(err_code);
999         rr->hits = SvIV(hits);
1000
1001         ptr = SvPV(err_string, len);
1002         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1003         strcpy(ODR_errstr, ptr);
1004         rr->errstring = ODR_errstr;
1005 /*      wrbuf_free(oid_dotted, 1);*/
1006         zhandle->handle = point;
1007         handle = zhandle;
1008         sv_free(err_code);
1009         sv_free(err_string);
1010         sv_free(hits);
1011         sv_free( (SV*) href);
1012
1013         return 0;
1014 }
1015
1016
1017 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1018 {
1019         perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1020         return 0;
1021 }
1022
1023
1024 int bend_delete(void *handle, bend_delete_rr *rr)
1025 {
1026         perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
1027         return 0;
1028 }
1029
1030
1031 int bend_scan(void *handle, bend_scan_rr *rr)
1032 {
1033         HV *href;
1034         AV *aref;
1035         AV *list;
1036         AV *entries;
1037         HV *scan_item;
1038         struct scan_entry *scan_list;
1039         struct scan_entry *buffer;
1040         int *step_size = rr->step_size;
1041         int i;
1042         char **basenames;
1043         SV **temp;
1044         SV *err_code = sv_newmortal();
1045         SV *err_str = sv_newmortal();
1046         SV *point = sv_newmortal();
1047         SV *status = sv_newmortal();
1048         SV *number = sv_newmortal();
1049         char *ptr;
1050         char *ODR_errstr;
1051         STRLEN len;
1052         int term_len;
1053         SV *term_tmp;
1054         SV *entries_ref;
1055         Zfront_handle *zhandle = (Zfront_handle *)handle;
1056         CV* handler_cv = 0;
1057
1058         dSP;
1059         ENTER;
1060         SAVETMPS;
1061         href = newHV();
1062         list = newAV();
1063         if (rr->term->term->which == Z_Term_general)
1064         {
1065                 term_len = rr->term->term->u.general->len;
1066                 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1067         } else {
1068                 rr->errcode = 229;      /* Unsupported term type */
1069                 return 0;
1070         }
1071         hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1072         hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1073         hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1074         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1075         hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1076         hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1077         hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1078         hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1079         aref = newAV();
1080         basenames = rr->basenames;
1081         for (i = 0; i < rr->num_bases; i++)
1082         {
1083                 av_push(aref, newSVpv(*basenames++, 0));
1084         }
1085         hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1086
1087         PUSHMARK(sp);
1088
1089         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1090
1091         PUTBACK;
1092
1093         handler_cv = simpleserver_sv2cv( scan_ref );
1094         perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1095
1096         SPAGAIN;
1097
1098         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1099         err_code = newSVsv(*temp);
1100
1101         temp = hv_fetch(href, "ERR_STR", 7, 1);
1102         err_str = newSVsv(*temp);
1103
1104         temp = hv_fetch(href, "HANDLE", 6, 1);
1105         point = newSVsv(*temp);
1106
1107         temp = hv_fetch(href, "STATUS", 6, 1);
1108         status = newSVsv(*temp);
1109         
1110         temp = hv_fetch(href, "NUMBER", 6, 1);
1111         number = newSVsv(*temp);
1112
1113         temp = hv_fetch(href, "ENTRIES", 7, 1);
1114         entries_ref = newSVsv(*temp);
1115
1116         PUTBACK;
1117         FREETMPS;
1118         LEAVE;
1119
1120         ptr = SvPV(err_str, len);
1121         ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1122         strcpy(ODR_errstr, ptr);
1123         rr->errstring = ODR_errstr;
1124         rr->errcode = SvIV(err_code);
1125         rr->num_entries = SvIV(number);
1126         rr->status = SvIV(status);
1127         scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1128         buffer = scan_list;
1129         entries = (AV *)SvRV(entries_ref);
1130         for (i = 0; i < rr->num_entries; i++)
1131         {
1132                 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1133                 temp = hv_fetch(scan_item, "TERM", 4, 1);
1134                 ptr = SvPV(*temp, len);
1135                 buffer->term = (char *) odr_malloc (rr->stream, len + 1); 
1136                 strcpy(buffer->term, ptr);
1137                 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1); 
1138                 buffer->occurrences = SvIV(*temp);
1139                 buffer++;
1140                 hv_undef(scan_item);
1141         }
1142         rr->entries = scan_list;
1143         zhandle->handle = point;
1144         handle = zhandle;
1145         sv_free(err_code);
1146         sv_free(err_str);
1147         sv_free(status);
1148         sv_free(number);
1149         hv_undef(href);
1150         sv_free((SV *)href);
1151         av_undef(aref);
1152         sv_free((SV *)aref);
1153         av_undef(list);
1154         sv_free((SV *)list);
1155         av_undef(entries);
1156         /*sv_free((SV *)entries);*/
1157         sv_free(entries_ref);
1158
1159         return 0;
1160 }
1161
1162
1163 bend_initresult *bend_init(bend_initrequest *q)
1164 {
1165         int dummy = simpleserver_clone();
1166         bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1167         HV *href;
1168         SV **temp;
1169         SV *id;
1170         SV *name;
1171         SV *ver;
1172         SV *err_str;
1173         SV *status;
1174         Zfront_handle *zhandle =  (Zfront_handle *) xmalloc (sizeof(*zhandle));
1175         STRLEN len;
1176         int n;
1177         SV *handle;
1178         /*char *name_ptr;
1179         char *ver_ptr;*/
1180         char *ptr;
1181         char *user = NULL;
1182         char *passwd = NULL;
1183         CV* handler_cv = 0;
1184
1185         dSP;
1186         ENTER;
1187         SAVETMPS;
1188
1189         /*q->bend_sort = bend_sort;*/
1190         if (search_ref)
1191         {
1192                 q->bend_search = bend_search;
1193         }
1194         if (present_ref)
1195         {
1196                 q->bend_present = bend_present;
1197         }
1198         /*q->bend_esrequest = bend_esrequest;*/
1199         /*q->bend_delete = bend_delete;*/
1200         if (fetch_ref)
1201         {
1202                 q->bend_fetch = bend_fetch;
1203         }
1204         if (scan_ref)
1205         {
1206                 q->bend_scan = bend_scan;
1207         }
1208         href = newHV(); 
1209         hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1210         hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1211         hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1212         hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1213         hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1214         hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1215         hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1216         hv_store(href, "PID", 3, newSViv(getpid()), 0);
1217         if (q->auth) {
1218             if (q->auth->which == Z_IdAuthentication_open) {
1219                 char *openpass = xstrdup (q->auth->u.open);
1220                 char *cp = strchr (openpass, '/');
1221                 if (cp) {
1222                     *cp = '\0';
1223                     user = nmem_strdup (odr_getmem (q->stream), openpass);
1224                     passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1225                 }
1226                 xfree(openpass);
1227             } else if (q->auth->which == Z_IdAuthentication_idPass) {
1228                 user = q->auth->u.idPass->userId;
1229                 passwd = q->auth->u.idPass->password;
1230             }
1231             /* ### some code paths have user/password unassigned here */
1232             hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1233             hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1234         }
1235
1236         PUSHMARK(sp);   
1237
1238         XPUSHs(sv_2mortal(newRV( (SV*) href)));
1239
1240         PUTBACK;
1241
1242         if (init_ref != NULL)
1243         {
1244              handler_cv = simpleserver_sv2cv( init_ref );
1245              perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1246         }
1247
1248         SPAGAIN;
1249
1250         temp = hv_fetch(href, "IMP_ID", 6, 1);
1251         id = newSVsv(*temp);
1252
1253         temp = hv_fetch(href, "IMP_NAME", 8, 1);
1254         name = newSVsv(*temp);
1255
1256         temp = hv_fetch(href, "IMP_VER", 7, 1);
1257         ver = newSVsv(*temp);
1258
1259         temp = hv_fetch(href, "ERR_CODE", 8, 1);
1260         status = newSVsv(*temp);
1261
1262         temp = hv_fetch(href, "ERR_STR", 7, 1);
1263         err_str = newSVsv(*temp);
1264
1265         temp = hv_fetch(href, "HANDLE", 6, 1);
1266         handle= newSVsv(*temp);
1267
1268         hv_undef(href);
1269         PUTBACK;
1270         FREETMPS;
1271         LEAVE;
1272         zhandle->handle = handle;
1273         r->errcode = SvIV(status);
1274         ptr = SvPV(err_str, len);
1275         r->errstring = (char *)odr_malloc(q->stream, len + 1);
1276         strcpy(r->errstring, ptr);
1277         sv_free(err_str);
1278         r->handle = zhandle;
1279         ptr = SvPV(id, len);
1280         q->implementation_id = (char *)xmalloc(len + 1);
1281         strcpy(q->implementation_id, ptr);
1282         ptr = SvPV(name, len);
1283         q->implementation_name = (char *)xmalloc(len + 1);
1284         strcpy(q->implementation_name, ptr);
1285 /*      q->implementation_name = SvPV(name, len);*/
1286         ptr = SvPV(ver, len);
1287         q->implementation_version = (char *)xmalloc(len + 1);
1288         strcpy(q->implementation_version, ptr);
1289         
1290         return r;
1291 }
1292
1293
1294 void bend_close(void *handle)
1295 {
1296         HV *href;
1297         Zfront_handle *zhandle = (Zfront_handle *)handle;
1298         SV **temp;
1299         CV* handler_cv = 0;
1300
1301
1302         if (close_ref)
1303         {
1304                 dSP;
1305                 ENTER;
1306                 SAVETMPS;
1307                 href = newHV();
1308                 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1309
1310                 PUSHMARK(sp);
1311
1312                 XPUSHs(sv_2mortal(newRV((SV *)href)));
1313
1314                 PUTBACK;
1315         
1316                 handler_cv = simpleserver_sv2cv( close_ref );
1317                 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1318         
1319                 SPAGAIN;
1320
1321                 PUTBACK;
1322                 FREETMPS;
1323                 LEAVE;
1324         }
1325         xfree(handle);
1326         simpleserver_free();
1327         
1328         return;
1329 }
1330
1331
1332 MODULE = Net::Z3950::SimpleServer       PACKAGE = Net::Z3950::SimpleServer
1333
1334 void
1335 set_init_handler(arg)
1336                 SV *arg
1337         CODE:
1338                 init_ref = newSVsv(arg);
1339                 
1340
1341 void
1342 set_close_handler(arg)
1343                 SV *arg
1344         CODE:
1345                 close_ref = newSVsv(arg);
1346
1347
1348 void
1349 set_sort_handler(arg)
1350                 SV *arg
1351         CODE:
1352                 sort_ref = newSVsv(arg);
1353
1354 void
1355 set_search_handler(arg)
1356                 SV *arg
1357         CODE:
1358                 search_ref = newSVsv(arg);
1359
1360
1361 void
1362 set_fetch_handler(arg)
1363                 SV *arg
1364         CODE:
1365                 fetch_ref = newSVsv(arg);
1366
1367
1368 void
1369 set_present_handler(arg)
1370                 SV *arg
1371         CODE:
1372                 present_ref = newSVsv(arg);
1373
1374
1375 void
1376 set_esrequest_handler(arg)
1377                 SV *arg
1378         CODE:
1379                 esrequest_ref = newSVsv(arg);
1380
1381
1382 void
1383 set_delete_handler(arg)
1384                 SV *arg
1385         CODE:
1386                 delete_ref = newSVsv(arg);
1387
1388
1389 void
1390 set_scan_handler(arg)
1391                 SV *arg
1392         CODE:
1393                 scan_ref = newSVsv(arg);
1394
1395
1396 int
1397 start_server(...)
1398         PREINIT:
1399                 char **argv;
1400                 char **argv_buf;
1401                 char *ptr;
1402                 int i;
1403                 STRLEN len;
1404         CODE:
1405                 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1406                 argv = argv_buf;
1407                 for (i = 0; i < items; i++)
1408                 {
1409                         ptr = SvPV(ST(i), len);
1410                         *argv_buf = (char *)xmalloc(len + 1);
1411                         strcpy(*argv_buf++, ptr); 
1412                 }
1413                 *argv_buf = NULL;
1414                 root_perl_context = PERL_GET_CONTEXT;
1415                 nmem_mutex_create(&simpleserver_mutex);
1416 #if 0
1417                 tst_clones();
1418 #endif
1419                 
1420                 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1421         OUTPUT:
1422                 RETVAL
1423
1424
1425 int
1426 ScanSuccess()
1427         CODE:
1428                 RETVAL = BEND_SCAN_SUCCESS;
1429         OUTPUT:
1430                 RETVAL
1431
1432 int
1433 ScanPartial()
1434         CODE:
1435                 RETVAL = BEND_SCAN_PARTIAL;
1436         OUTPUT:
1437                 RETVAL
1438
1439