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