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