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