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