2 * $Id: SimpleServer.xs,v 1.26 2004-05-29 07:04:26 adam Exp $
3 * ----------------------------------------------------------------------
5 * Copyright (c) 2000-2004, Index Data.
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,
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.
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.
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
35 #include <yaz/backend.h>
37 #include <yaz/wrbuf.h>
45 #define GRS_MAX_FIELDS 500
49 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
50 #define sv_undef PL_sv_undef
53 NMEM_MUTEX simpleserver_mutex;
72 SV *search_ref = NULL;
74 SV *present_ref = NULL;
75 SV *esrequest_ref = NULL;
76 SV *delete_ref = NULL;
78 PerlInterpreter *root_perl_context;
81 #define GRS_BUF_SIZE 512
83 CV * simpleserver_sv2cv(SV *handler) {
89 buf = SvPV( handler, len);
90 if ( !( ret = perl_get_cv(buf, FALSE ) ) ) {
91 fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf );
97 return (CV *) handler;
101 /* debuggin routine to check for destruction of Perl interpreters */
106 PerlInterpreter *parent = PERL_GET_CONTEXT;
107 for (i = 0; i<500; i++)
109 PerlInterpreter *perl_interp = perl_clone(parent, 0);
110 PERL_SET_CONTEXT( perl_interp );
111 PL_perl_destruct_level = 2;
112 PERL_SET_CONTEXT( parent );
113 perl_destruct(perl_interp);
114 perl_free(perl_interp);
120 int simpleserver_clone(void) {
122 nmem_mutex_enter(simpleserver_mutex);
125 PerlInterpreter *current = PERL_GET_CONTEXT;
127 /* if current is unset, then we're in a new thread with
128 * no Perl interpreter for it. So we must create one .
129 * This will only happen when threaded is used..
132 PerlInterpreter *perl_interp;
133 PERL_SET_CONTEXT( root_perl_context );
134 perl_interp = perl_clone(root_perl_context, 0);
135 PERL_SET_CONTEXT( perl_interp );
138 nmem_mutex_leave(simpleserver_mutex);
144 void simpleserver_free(void) {
145 nmem_mutex_enter(simpleserver_mutex);
148 PerlInterpreter *current_interp = PERL_GET_CONTEXT;
150 /* If current Perl Interp is different from root interp, then
151 * we're in threaded mode and we must destroy..
153 if (current_interp != root_perl_context) {
154 PL_perl_destruct_level = 2;
155 PERL_SET_CONTEXT(root_perl_context);
156 perl_destruct(current_interp);
157 perl_free(current_interp);
160 nmem_mutex_leave(simpleserver_mutex);
164 Z_GenericRecord *read_grs1(char *str, ODR o)
167 char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
168 char value[GRS_BUF_SIZE+1];
169 Z_GenericRecord *r = 0;
172 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
173 r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
182 ptr = strchr(str, '\n');
187 if (len > GRS_BUF_SIZE) {
188 yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
191 strncpy(line, str, len);
195 while (*buf && isspace(*buf))
198 memmove(original, str, strlen(str));
201 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
203 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
206 if (!type && *value == '0')
208 if (!(buf = strchr(buf, ')')))
211 while (*buf && isspace(*buf))
213 if (r->num_elements >= GRS_MAX_FIELDS)
215 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
218 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
219 t->tagType = (int *)odr_malloc(o, sizeof(int));
221 t->tagValue = (Z_StringOrNumeric *)
222 odr_malloc(o, sizeof(Z_StringOrNumeric));
223 if ((ivalue = atoi(value)))
225 t->tagValue->which = Z_StringOrNumeric_numeric;
226 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
227 *t->tagValue->u.numeric = ivalue;
231 t->tagValue->which = Z_StringOrNumeric_string;
232 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
233 strcpy(t->tagValue->u.string, value);
235 t->tagOccurrence = 0;
237 t->appliedVariant = 0;
238 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
241 c->which = Z_ElementData_subtree;
242 c->u.subtree = read_grs1(str, o);
246 c->which = Z_ElementData_string;
247 c->u.string = odr_strdup(o, buf);
256 static void oid2str(Odr_oid *o, WRBUF buf)
258 for (; *o >= 0; o++) {
260 sprintf(ibuf, "%d", *o);
261 wrbuf_puts(buf, ibuf);
263 wrbuf_putc(buf, '.');
268 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
271 case Z_RPNStructure_simple: {
272 Z_Operand *o = s->u.simple;
275 case Z_Operand_APT: {
276 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
278 if (at->attributes) {
282 for (i = 0; i < at->attributes->num_attributes; i++) {
283 wrbuf_puts(buf, "@attr ");
284 if (at->attributes->attributes[i]->attributeSet) {
285 oid2str(at->attributes->attributes[i]->attributeSet, buf);
286 wrbuf_putc(buf, ' ');
288 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
289 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
290 wrbuf_puts(buf, ibuf);
291 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
292 wrbuf_puts(buf, ibuf);
295 switch (at->term->which) {
296 case Z_Term_general: {
297 wrbuf_putc(buf, '"');
298 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
299 wrbuf_puts(buf, "\" ");
310 case Z_RPNStructure_complex: {
311 Z_Complex *c = s->u.complex;
313 switch (c->roperator->which) {
314 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
315 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
316 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
317 case Z_Operator_prox: abort();
320 if (!rpn2pquery(c->s1, buf))
322 if (!rpn2pquery(c->s2, buf))
332 WRBUF zquery2pquery(Z_Query *q)
334 WRBUF buf = wrbuf_alloc();
336 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
338 if (q->u.type_1->attributeSetId) {
339 /* Output attribute set ID */
340 wrbuf_puts(buf, "@attrset ");
341 oid2str(q->u.type_1->attributeSetId, buf);
342 wrbuf_putc(buf, ' ');
344 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
348 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
350 void fatal(char *fmt, ...)
354 fprintf(stderr, "FATAL (yazwrap): ");
356 vfprintf(stderr, fmt, ap);
358 fprintf(stderr, "\n");
363 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
365 * Creates a new Perl object of type `class'; the newly-created scalar
366 * that is a reference to the blessed thingy `referent' is returned.
368 static SV *newObject(char *class, SV *referent)
373 sv = newRV_noinc((SV*) referent);
374 stash = gv_stashpv(class, 0);
376 fatal("attempt to create object of undefined class '%s'", class);
377 /*assert(stash != 0);*/
383 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
384 static void setMember(HV *hv, char *name, SV *val)
386 /* We don't increment `val's reference count -- I think this is
387 * right because it's created with a refcount of 1, and in fact
388 * the reference via this hash is the only reference to it in
391 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
392 fatal("couldn't store member in hash");
396 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
397 static SV *translateOID(Odr_oid *x)
399 /* Yaz represents an OID by an int array terminated by a negative
400 * value, typically -1; we represent it as a reference to a
401 * blessed scalar string of "."-separated elements.
407 for (i = 0; x[i] >= 0; i++) {
408 sprintf(buf + strlen(buf), "%d", (int) x[i]);
414 * ### We'd like to return a blessed scalar (string) here, but of
415 * course you can't do that in Perl: only references can be
416 * blessed, so we'd have to return a _reference_ to a string, and
417 * bless _that_. Better to do without the blessing, I think.
420 return newSVpv(buf, 0);
422 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
427 static SV *rpn2perl(Z_RPNStructure *s)
434 case Z_RPNStructure_simple: {
435 Z_Operand *o = s->u.simple;
436 Z_AttributesPlusTerm *at;
437 if (o->which != Z_Operand_APT)
438 fatal("can't handle RPN simples other than APT");
439 at = o->u.attributesPlusTerm;
440 if (at->term->which != Z_Term_general)
441 fatal("can't handle RPN terms other than general");
443 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
444 if (at->attributes) {
446 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
447 (SV*) (av = newAV()));
448 for (i = 0; i < at->attributes->num_attributes; i++) {
449 Z_AttributeElement *elem = at->attributes->attributes[i];
451 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
452 (SV*) (hv2 = newHV()));
453 if (elem->attributeSet)
454 setMember(hv2, "attributeSet",
455 translateOID(elem->attributeSet));
456 setMember(hv2, "attributeType",
457 newSViv(*elem->attributeType));
458 assert(elem->which == Z_AttributeValue_numeric);
459 setMember(hv2, "attributeValue",
460 newSViv(*elem->value.numeric));
463 setMember(hv, "attributes", attrs);
465 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
466 at->term->u.general->len));
469 case Z_RPNStructure_complex: {
471 Z_Complex *c = s->u.complex;
472 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
473 switch (c->roperator->which) {
474 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
475 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
476 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
477 case Z_Operator_prox: fatal("proximity not yet supported");
478 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
480 sv = newObject(type, (SV*) (av = newAV()));
481 if ((tmp = rpn2perl(c->s1)) == 0)
484 if ((tmp = rpn2perl(c->s2)) == 0)
489 default: fatal("unknown RPN node type %d", (int) s->which);
496 static SV *zquery2perl(Z_Query *q)
501 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
503 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
504 if (q->u.type_1->attributeSetId)
505 setMember(hv, "attributeSet",
506 translateOID(q->u.type_1->attributeSetId));
507 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
512 int bend_sort(void *handle, bend_sort_rr *rr)
523 char **input_setnames;
524 Zfront_handle *zhandle = (Zfront_handle *)handle;
532 input_setnames = rr->input_setnames;
533 for (i = 0; i < rr->num_input_setnames; i++)
535 av_push(aref, newSVpv(*input_setnames++, 0));
538 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
539 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
540 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
541 hv_store(href, "STATUS", 6, newSViv(0), 0);
545 XPUSHs(sv_2mortal(newRV( (SV*) href)));
549 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
553 temp = hv_fetch(href, "ERR_CODE", 8, 1);
554 err_code = newSVsv(*temp);
556 temp = hv_fetch(href, "ERR_STR", 7, 1);
557 err_str = newSVsv(*temp);
559 temp = hv_fetch(href, "STATUS", 6, 1);
560 status = newSVsv(*temp);
571 rr->errcode = SvIV(err_code);
572 rr->sort_status = SvIV(status);
573 ptr = SvPV(err_str, len);
574 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
575 strcpy(ODR_err_str, ptr);
576 rr->errstring = ODR_err_str;
586 int bend_search(void *handle, bend_search_rr *rr)
603 Zfront_handle *zhandle = (Zfront_handle *)handle;
611 basenames = rr->basenames;
612 for (i = 0; i < rr->num_bases; i++)
614 av_push(aref, newSVpv(*basenames++, 0));
617 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
618 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
619 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
620 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
621 hv_store(href, "HITS", 4, newSViv(0), 0);
622 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
623 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
624 hv_store(href, "PID", 3, newSViv(getpid()), 0);
625 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
626 query = zquery2pquery(rr->query);
629 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
637 XPUSHs(sv_2mortal(newRV( (SV*) href)));
641 handler_cv = simpleserver_sv2cv( search_ref );
642 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
646 temp = hv_fetch(href, "HITS", 4, 1);
647 hits = newSVsv(*temp);
649 temp = hv_fetch(href, "ERR_CODE", 8, 1);
650 err_code = newSVsv(*temp);
652 temp = hv_fetch(href, "ERR_STR", 7, 1);
653 err_str = newSVsv(*temp);
655 temp = hv_fetch(href, "HANDLE", 6, 1);
656 point = newSVsv(*temp);
664 rr->hits = SvIV(hits);
665 rr->errcode = SvIV(err_code);
666 ptr = SvPV(err_str, len);
667 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
668 strcpy(ODR_errstr, ptr);
669 rr->errstring = ODR_errstr;
670 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
671 memcpy(ODR_point, point, sizeof(*point));
672 zhandle->handle = ODR_point;*/
673 zhandle->handle = point;
678 sv_free( (SV*) aref);
679 sv_free( (SV*) href);
681 wrbuf_free(query, 1);
686 /* ### this is worryingly similar to oid2str() */
687 WRBUF oid2dotted(int *oid)
690 WRBUF buf = wrbuf_alloc();
693 for (; *oid != -1 ; oid++)
698 wrbuf_putc(buf, '.');
704 sprintf(ibuf, "%d", *oid);
705 wrbuf_puts(buf, ibuf);
711 int dotted2oid(char *dotted, int *buffer)
725 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
731 sscanf(ibuf, "%d", oid++);
744 sscanf(ibuf, "%d", oid++);
751 int bend_fetch(void *handle, bend_fetch_rr *rr)
770 Zfront_handle *zhandle = (Zfront_handle *)handle;
773 Z_RecordComposition *composition;
774 Z_ElementSetNames *simple;
783 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
784 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
785 oid_dotted = oid2dotted(rr->request_format_raw);
786 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
787 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
788 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
789 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
790 hv_store(href, "LAST", 4, newSViv(0), 0);
791 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
792 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
793 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
794 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
795 hv_store(href, "PID", 3, newSViv(getpid()), 0);
798 composition = rr->comp;
799 if (composition->which == Z_RecordComp_simple)
801 simple = composition->u.simple;
802 if (simple->which == Z_ElementSetNames_generic)
804 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
819 XPUSHs(sv_2mortal(newRV( (SV*) href)));
823 handler_cv = simpleserver_sv2cv( fetch_ref );
824 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
828 temp = hv_fetch(href, "BASENAME", 8, 1);
829 basename = newSVsv(*temp);
831 temp = hv_fetch(href, "RECORD", 6, 1);
832 record = newSVsv(*temp);
834 temp = hv_fetch(href, "LAST", 4, 1);
835 last = newSVsv(*temp);
837 temp = hv_fetch(href, "ERR_CODE", 8, 1);
838 err_code = newSVsv(*temp);
840 temp = hv_fetch(href, "ERR_STR", 7, 1),
841 err_string = newSVsv(*temp);
843 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
844 sur_flag = newSVsv(*temp);
846 temp = hv_fetch(href, "REP_FORM", 8, 1);
847 rep_form = newSVsv(*temp);
849 temp = hv_fetch(href, "HANDLE", 6, 1);
850 point = newSVsv(*temp);
858 ptr = SvPV(basename, length);
859 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
860 strcpy(ODR_basename, ptr);
861 rr->basename = ODR_basename;
863 ptr = SvPV(rep_form, length);
864 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
865 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
867 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
869 rr->output_format_raw = ODR_oid_buf;
871 ptr = SvPV(record, length);
872 oid = oid_getentbyoid(ODR_oid_buf);
873 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
875 rr->record = (char *) read_grs1(ptr, rr->stream);
880 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
881 strcpy(ODR_record, ptr);
882 rr->record = ODR_record;
885 zhandle->handle = point;
887 rr->last_in_set = SvIV(last);
891 rr->errcode = SvIV(err_code);
892 ptr = SvPV(err_string, length);
893 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
894 strcpy(ODR_errstr, ptr);
895 rr->errstring = ODR_errstr;
897 rr->surrogate_flag = SvIV(sur_flag);
899 wrbuf_free(oid_dotted, 1);
913 int bend_present(void *handle, bend_present_rr *rr)
923 Z_RecordComposition *composition;
924 Z_ElementSetNames *simple;
927 Zfront_handle *zhandle = (Zfront_handle *)handle;
930 /* WRBUF oid_dotted; */
937 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
938 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
939 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
940 hv_store(href, "START", 5, newSViv(rr->start), 0);
941 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
942 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
943 /*oid_dotted = oid2dotted(rr->request_format_raw);
944 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
945 hv_store(href, "HITS", 4, newSViv(0), 0);
946 hv_store(href, "PID", 3, newSViv(getpid()), 0);
949 composition = rr->comp;
950 if (composition->which == Z_RecordComp_simple)
952 simple = composition->u.simple;
953 if (simple->which == Z_ElementSetNames_generic)
955 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
972 XPUSHs(sv_2mortal(newRV( (SV*) href)));
976 handler_cv = simpleserver_sv2cv( present_ref );
977 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
981 temp = hv_fetch(href, "ERR_CODE", 8, 1);
982 err_code = newSVsv(*temp);
984 temp = hv_fetch(href, "ERR_STR", 7, 1);
985 err_string = newSVsv(*temp);
987 temp = hv_fetch(href, "HITS", 4, 1);
988 hits = newSVsv(*temp);
990 temp = hv_fetch(href, "HANDLE", 6, 1);
991 point = newSVsv(*temp);
998 rr->errcode = SvIV(err_code);
999 rr->hits = SvIV(hits);
1001 ptr = SvPV(err_string, len);
1002 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1003 strcpy(ODR_errstr, ptr);
1004 rr->errstring = ODR_errstr;
1005 /* wrbuf_free(oid_dotted, 1);*/
1006 zhandle->handle = point;
1009 sv_free(err_string);
1011 sv_free( (SV*) href);
1017 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1019 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1024 int bend_delete(void *handle, bend_delete_rr *rr)
1026 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
1031 int bend_scan(void *handle, bend_scan_rr *rr)
1038 struct scan_entry *scan_list;
1039 struct scan_entry *buffer;
1040 int *step_size = rr->step_size;
1044 SV *err_code = sv_newmortal();
1045 SV *err_str = sv_newmortal();
1046 SV *point = sv_newmortal();
1047 SV *status = sv_newmortal();
1048 SV *number = sv_newmortal();
1055 Zfront_handle *zhandle = (Zfront_handle *)handle;
1063 if (rr->term->term->which == Z_Term_general)
1065 term_len = rr->term->term->u.general->len;
1066 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1068 rr->errcode = 229; /* Unsupported term type */
1071 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1072 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1073 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1074 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1075 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1076 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1077 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1078 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1080 basenames = rr->basenames;
1081 for (i = 0; i < rr->num_bases; i++)
1083 av_push(aref, newSVpv(*basenames++, 0));
1085 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1089 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1093 handler_cv = simpleserver_sv2cv( scan_ref );
1094 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1098 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1099 err_code = newSVsv(*temp);
1101 temp = hv_fetch(href, "ERR_STR", 7, 1);
1102 err_str = newSVsv(*temp);
1104 temp = hv_fetch(href, "HANDLE", 6, 1);
1105 point = newSVsv(*temp);
1107 temp = hv_fetch(href, "STATUS", 6, 1);
1108 status = newSVsv(*temp);
1110 temp = hv_fetch(href, "NUMBER", 6, 1);
1111 number = newSVsv(*temp);
1113 temp = hv_fetch(href, "ENTRIES", 7, 1);
1114 entries_ref = newSVsv(*temp);
1120 ptr = SvPV(err_str, len);
1121 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1122 strcpy(ODR_errstr, ptr);
1123 rr->errstring = ODR_errstr;
1124 rr->errcode = SvIV(err_code);
1125 rr->num_entries = SvIV(number);
1126 rr->status = SvIV(status);
1127 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1129 entries = (AV *)SvRV(entries_ref);
1130 for (i = 0; i < rr->num_entries; i++)
1132 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1133 temp = hv_fetch(scan_item, "TERM", 4, 1);
1134 ptr = SvPV(*temp, len);
1135 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1136 strcpy(buffer->term, ptr);
1137 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1138 buffer->occurrences = SvIV(*temp);
1140 hv_undef(scan_item);
1142 rr->entries = scan_list;
1143 zhandle->handle = point;
1150 sv_free((SV *)href);
1152 sv_free((SV *)aref);
1154 sv_free((SV *)list);
1156 /*sv_free((SV *)entries);*/
1157 sv_free(entries_ref);
1163 bend_initresult *bend_init(bend_initrequest *q)
1165 int dummy = simpleserver_clone();
1166 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1174 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1182 char *passwd = NULL;
1189 /*q->bend_sort = bend_sort;*/
1192 q->bend_search = bend_search;
1196 q->bend_present = bend_present;
1198 /*q->bend_esrequest = bend_esrequest;*/
1199 /*q->bend_delete = bend_delete;*/
1202 q->bend_fetch = bend_fetch;
1206 q->bend_scan = bend_scan;
1209 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1210 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1211 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1212 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1213 hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1214 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1215 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1216 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1218 if (q->auth->which == Z_IdAuthentication_open) {
1219 char *openpass = xstrdup (q->auth->u.open);
1220 char *cp = strchr (openpass, '/');
1223 user = nmem_strdup (odr_getmem (q->stream), openpass);
1224 passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1227 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1228 user = q->auth->u.idPass->userId;
1229 passwd = q->auth->u.idPass->password;
1231 /* ### some code paths have user/password unassigned here */
1232 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1233 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1238 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1242 if (init_ref != NULL)
1244 handler_cv = simpleserver_sv2cv( init_ref );
1245 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1250 temp = hv_fetch(href, "IMP_ID", 6, 1);
1251 id = newSVsv(*temp);
1253 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1254 name = newSVsv(*temp);
1256 temp = hv_fetch(href, "IMP_VER", 7, 1);
1257 ver = newSVsv(*temp);
1259 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1260 status = newSVsv(*temp);
1262 temp = hv_fetch(href, "ERR_STR", 7, 1);
1263 err_str = newSVsv(*temp);
1265 temp = hv_fetch(href, "HANDLE", 6, 1);
1266 handle= newSVsv(*temp);
1272 zhandle->handle = handle;
1273 r->errcode = SvIV(status);
1274 ptr = SvPV(err_str, len);
1275 r->errstring = (char *)odr_malloc(q->stream, len + 1);
1276 strcpy(r->errstring, ptr);
1278 r->handle = zhandle;
1279 ptr = SvPV(id, len);
1280 q->implementation_id = (char *)xmalloc(len + 1);
1281 strcpy(q->implementation_id, ptr);
1282 ptr = SvPV(name, len);
1283 q->implementation_name = (char *)xmalloc(len + 1);
1284 strcpy(q->implementation_name, ptr);
1285 /* q->implementation_name = SvPV(name, len);*/
1286 ptr = SvPV(ver, len);
1287 q->implementation_version = (char *)xmalloc(len + 1);
1288 strcpy(q->implementation_version, ptr);
1294 void bend_close(void *handle)
1297 Zfront_handle *zhandle = (Zfront_handle *)handle;
1308 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1312 XPUSHs(sv_2mortal(newRV((SV *)href)));
1316 handler_cv = simpleserver_sv2cv( close_ref );
1317 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1326 simpleserver_free();
1332 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1335 set_init_handler(arg)
1338 init_ref = newSVsv(arg);
1342 set_close_handler(arg)
1345 close_ref = newSVsv(arg);
1349 set_sort_handler(arg)
1352 sort_ref = newSVsv(arg);
1355 set_search_handler(arg)
1358 search_ref = newSVsv(arg);
1362 set_fetch_handler(arg)
1365 fetch_ref = newSVsv(arg);
1369 set_present_handler(arg)
1372 present_ref = newSVsv(arg);
1376 set_esrequest_handler(arg)
1379 esrequest_ref = newSVsv(arg);
1383 set_delete_handler(arg)
1386 delete_ref = newSVsv(arg);
1390 set_scan_handler(arg)
1393 scan_ref = newSVsv(arg);
1405 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1407 for (i = 0; i < items; i++)
1409 ptr = SvPV(ST(i), len);
1410 *argv_buf = (char *)xmalloc(len + 1);
1411 strcpy(*argv_buf++, ptr);
1414 root_perl_context = PERL_GET_CONTEXT;
1415 nmem_mutex_create(&simpleserver_mutex);
1420 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1428 RETVAL = BEND_SCAN_SUCCESS;
1435 RETVAL = BEND_SCAN_PARTIAL;