2 * $Id: SimpleServer.xs,v 1.24 2004-05-13 19:04:28 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
34 #include <yaz/backend.h>
36 #include <yaz/wrbuf.h>
44 #define GRS_MAX_FIELDS 500
48 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
49 #define sv_undef PL_sv_undef
52 NMEM_MUTEX simpleserver_mutex;
71 SV *search_ref = NULL;
73 SV *present_ref = NULL;
74 SV *esrequest_ref = NULL;
75 SV *delete_ref = NULL;
77 PerlInterpreter *root_perl_context;
80 #define GRS_BUF_SIZE 512
82 CV * simpleserver_sv2cv(SV *handler) {
88 buf = SvPV( handler, len);
89 if ( !( ret = perl_get_cv(buf, FALSE ) ) ) {
90 fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf );
96 return (CV *) handler;
100 /* debuggin routine to check for destruction of Perl interpreters */
105 PerlInterpreter *parent = PERL_GET_CONTEXT;
106 for (i = 0; i<500; i++)
108 PerlInterpreter *perl_interp = perl_clone(parent, 0);
109 PERL_SET_CONTEXT( perl_interp );
110 PL_perl_destruct_level = 2;
111 PERL_SET_CONTEXT( parent );
112 perl_destruct(perl_interp);
113 perl_free(perl_interp);
119 int simpleserver_clone(void) {
120 nmem_mutex_enter(simpleserver_mutex);
123 PerlInterpreter *current = PERL_GET_CONTEXT;
125 /* if current is unset, then we're in a new thread with
126 * no Perl interpreter for it. So we must create one .
127 * This will only happen when threaded is used..
130 PERL_SET_CONTEXT( root_perl_context );
131 PerlInterpreter *perl_interp = perl_clone(root_perl_context, 0);
132 PERL_SET_CONTEXT( perl_interp );
135 nmem_mutex_leave(simpleserver_mutex);
140 void simpleserver_free(void) {
141 nmem_mutex_enter(simpleserver_mutex);
144 PerlInterpreter *current_interp = PERL_GET_CONTEXT;
146 /* If current Perl Interp is different from root interp, then
147 * we're in threaded mode and we must destroy..
149 if (current_interp != root_perl_context) {
150 PL_perl_destruct_level = 2;
151 PERL_SET_CONTEXT(root_perl_context);
152 perl_destruct(current_interp);
153 perl_free(current_interp);
156 nmem_mutex_leave(simpleserver_mutex);
160 Z_GenericRecord *read_grs1(char *str, ODR o)
163 char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
164 char value[GRS_BUF_SIZE+1];
165 Z_GenericRecord *r = 0;
168 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
169 r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
178 ptr = strchr(str, '\n');
183 if (len > GRS_BUF_SIZE) {
184 yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
187 strncpy(line, str, len);
191 while (*buf && isspace(*buf))
194 memmove(original, str, strlen(str));
197 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
199 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
202 if (!type && *value == '0')
204 if (!(buf = strchr(buf, ')')))
207 while (*buf && isspace(*buf))
209 if (r->num_elements >= GRS_MAX_FIELDS)
211 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
214 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
215 t->tagType = (int *)odr_malloc(o, sizeof(int));
217 t->tagValue = (Z_StringOrNumeric *)
218 odr_malloc(o, sizeof(Z_StringOrNumeric));
219 if ((ivalue = atoi(value)))
221 t->tagValue->which = Z_StringOrNumeric_numeric;
222 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
223 *t->tagValue->u.numeric = ivalue;
227 t->tagValue->which = Z_StringOrNumeric_string;
228 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
229 strcpy(t->tagValue->u.string, value);
231 t->tagOccurrence = 0;
233 t->appliedVariant = 0;
234 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
237 c->which = Z_ElementData_subtree;
238 c->u.subtree = read_grs1(str, o);
242 c->which = Z_ElementData_string;
243 c->u.string = odr_strdup(o, buf);
252 static void oid2str(Odr_oid *o, WRBUF buf)
254 for (; *o >= 0; o++) {
256 sprintf(ibuf, "%d", *o);
257 wrbuf_puts(buf, ibuf);
259 wrbuf_putc(buf, '.');
264 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
267 case Z_RPNStructure_simple: {
268 Z_Operand *o = s->u.simple;
271 case Z_Operand_APT: {
272 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
274 if (at->attributes) {
278 for (i = 0; i < at->attributes->num_attributes; i++) {
279 wrbuf_puts(buf, "@attr ");
280 if (at->attributes->attributes[i]->attributeSet) {
281 oid2str(at->attributes->attributes[i]->attributeSet, buf);
282 wrbuf_putc(buf, ' ');
284 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
285 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
286 wrbuf_puts(buf, ibuf);
287 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
288 wrbuf_puts(buf, ibuf);
291 switch (at->term->which) {
292 case Z_Term_general: {
293 wrbuf_putc(buf, '"');
294 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
295 wrbuf_puts(buf, "\" ");
306 case Z_RPNStructure_complex: {
307 Z_Complex *c = s->u.complex;
309 switch (c->roperator->which) {
310 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
311 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
312 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
313 case Z_Operator_prox: abort();
316 if (!rpn2pquery(c->s1, buf))
318 if (!rpn2pquery(c->s2, buf))
328 WRBUF zquery2pquery(Z_Query *q)
330 WRBUF buf = wrbuf_alloc();
332 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
334 if (q->u.type_1->attributeSetId) {
335 /* Output attribute set ID */
336 wrbuf_puts(buf, "@attrset ");
337 oid2str(q->u.type_1->attributeSetId, buf);
338 wrbuf_putc(buf, ' ');
340 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
344 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
346 void fatal(char *fmt, ...)
350 fprintf(stderr, "FATAL (yazwrap): ");
352 vfprintf(stderr, fmt, ap);
354 fprintf(stderr, "\n");
359 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
361 * Creates a new Perl object of type `class'; the newly-created scalar
362 * that is a reference to the blessed thingy `referent' is returned.
364 static SV *newObject(char *class, SV *referent)
369 sv = newRV_noinc((SV*) referent);
370 stash = gv_stashpv(class, 0);
372 fatal("attempt to create object of undefined class '%s'", class);
373 /*assert(stash != 0);*/
379 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
380 static void setMember(HV *hv, char *name, SV *val)
382 /* We don't increment `val's reference count -- I think this is
383 * right because it's created with a refcount of 1, and in fact
384 * the reference via this hash is the only reference to it in
387 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
388 fatal("couldn't store member in hash");
392 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
393 static SV *translateOID(Odr_oid *x)
395 /* Yaz represents an OID by an int array terminated by a negative
396 * value, typically -1; we represent it as a reference to a
397 * blessed scalar string of "."-separated elements.
403 for (i = 0; x[i] >= 0; i++) {
404 sprintf(buf + strlen(buf), "%d", (int) x[i]);
410 * ### We'd like to return a blessed scalar (string) here, but of
411 * course you can't do that in Perl: only references can be
412 * blessed, so we'd have to return a _reference_ to a string, and
413 * bless _that_. Better to do without the blessing, I think.
416 return newSVpv(buf, 0);
418 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
423 static SV *rpn2perl(Z_RPNStructure *s)
430 case Z_RPNStructure_simple: {
431 Z_Operand *o = s->u.simple;
432 Z_AttributesPlusTerm *at;
433 if (o->which != Z_Operand_APT)
434 fatal("can't handle RPN simples other than APT");
435 at = o->u.attributesPlusTerm;
436 if (at->term->which != Z_Term_general)
437 fatal("can't handle RPN terms other than general");
439 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
440 if (at->attributes) {
442 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
443 (SV*) (av = newAV()));
444 for (i = 0; i < at->attributes->num_attributes; i++) {
445 Z_AttributeElement *elem = at->attributes->attributes[i];
447 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
448 (SV*) (hv2 = newHV()));
449 if (elem->attributeSet)
450 setMember(hv2, "attributeSet",
451 translateOID(elem->attributeSet));
452 setMember(hv2, "attributeType",
453 newSViv(*elem->attributeType));
454 assert(elem->which == Z_AttributeValue_numeric);
455 setMember(hv2, "attributeValue",
456 newSViv(*elem->value.numeric));
459 setMember(hv, "attributes", attrs);
461 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
462 at->term->u.general->len));
465 case Z_RPNStructure_complex: {
467 Z_Complex *c = s->u.complex;
468 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
469 switch (c->roperator->which) {
470 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
471 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
472 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
473 case Z_Operator_prox: fatal("proximity not yet supported");
474 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
476 sv = newObject(type, (SV*) (av = newAV()));
477 if ((tmp = rpn2perl(c->s1)) == 0)
480 if ((tmp = rpn2perl(c->s2)) == 0)
485 default: fatal("unknown RPN node type %d", (int) s->which);
492 static SV *zquery2perl(Z_Query *q)
497 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
499 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
500 if (q->u.type_1->attributeSetId)
501 setMember(hv, "attributeSet",
502 translateOID(q->u.type_1->attributeSetId));
503 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
508 int bend_sort(void *handle, bend_sort_rr *rr)
519 char **input_setnames;
520 Zfront_handle *zhandle = (Zfront_handle *)handle;
528 input_setnames = rr->input_setnames;
529 for (i = 0; i < rr->num_input_setnames; i++)
531 av_push(aref, newSVpv(*input_setnames++, 0));
534 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
535 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
536 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
537 hv_store(href, "STATUS", 6, newSViv(0), 0);
541 XPUSHs(sv_2mortal(newRV( (SV*) href)));
545 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
549 temp = hv_fetch(href, "ERR_CODE", 8, 1);
550 err_code = newSVsv(*temp);
552 temp = hv_fetch(href, "ERR_STR", 7, 1);
553 err_str = newSVsv(*temp);
555 temp = hv_fetch(href, "STATUS", 6, 1);
556 status = newSVsv(*temp);
567 rr->errcode = SvIV(err_code);
568 rr->sort_status = SvIV(status);
569 ptr = SvPV(err_str, len);
570 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
571 strcpy(ODR_err_str, ptr);
572 rr->errstring = ODR_err_str;
582 int bend_search(void *handle, bend_search_rr *rr)
599 Zfront_handle *zhandle = (Zfront_handle *)handle;
607 basenames = rr->basenames;
608 for (i = 0; i < rr->num_bases; i++)
610 av_push(aref, newSVpv(*basenames++, 0));
613 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
614 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
615 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
616 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
617 hv_store(href, "HITS", 4, newSViv(0), 0);
618 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
619 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
620 hv_store(href, "PID", 3, newSViv(getpid()), 0);
621 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
622 query = zquery2pquery(rr->query);
625 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
633 XPUSHs(sv_2mortal(newRV( (SV*) href)));
637 handler_cv = simpleserver_sv2cv( search_ref );
638 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
642 temp = hv_fetch(href, "HITS", 4, 1);
643 hits = newSVsv(*temp);
645 temp = hv_fetch(href, "ERR_CODE", 8, 1);
646 err_code = newSVsv(*temp);
648 temp = hv_fetch(href, "ERR_STR", 7, 1);
649 err_str = newSVsv(*temp);
651 temp = hv_fetch(href, "HANDLE", 6, 1);
652 point = newSVsv(*temp);
660 rr->hits = SvIV(hits);
661 rr->errcode = SvIV(err_code);
662 ptr = SvPV(err_str, len);
663 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
664 strcpy(ODR_errstr, ptr);
665 rr->errstring = ODR_errstr;
666 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
667 memcpy(ODR_point, point, sizeof(*point));
668 zhandle->handle = ODR_point;*/
669 zhandle->handle = point;
674 sv_free( (SV*) aref);
675 sv_free( (SV*) href);
677 wrbuf_free(query, 1);
682 /* ### this is worryingly similar to oid2str() */
683 WRBUF oid2dotted(int *oid)
686 WRBUF buf = wrbuf_alloc();
689 for (; *oid != -1 ; oid++)
694 wrbuf_putc(buf, '.');
700 sprintf(ibuf, "%d", *oid);
701 wrbuf_puts(buf, ibuf);
707 int dotted2oid(char *dotted, int *buffer)
721 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
727 sscanf(ibuf, "%d", oid++);
740 sscanf(ibuf, "%d", oid++);
747 int bend_fetch(void *handle, bend_fetch_rr *rr)
766 Zfront_handle *zhandle = (Zfront_handle *)handle;
769 Z_RecordComposition *composition;
770 Z_ElementSetNames *simple;
779 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
780 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
781 oid_dotted = oid2dotted(rr->request_format_raw);
782 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
783 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
784 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
785 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
786 hv_store(href, "LAST", 4, newSViv(0), 0);
787 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
788 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
789 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
790 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
791 hv_store(href, "PID", 3, newSViv(getpid()), 0);
794 composition = rr->comp;
795 if (composition->which == Z_RecordComp_simple)
797 simple = composition->u.simple;
798 if (simple->which == Z_ElementSetNames_generic)
800 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
815 XPUSHs(sv_2mortal(newRV( (SV*) href)));
819 handler_cv = simpleserver_sv2cv( fetch_ref );
820 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
824 temp = hv_fetch(href, "BASENAME", 8, 1);
825 basename = newSVsv(*temp);
827 temp = hv_fetch(href, "RECORD", 6, 1);
828 record = newSVsv(*temp);
830 temp = hv_fetch(href, "LAST", 4, 1);
831 last = newSVsv(*temp);
833 temp = hv_fetch(href, "ERR_CODE", 8, 1);
834 err_code = newSVsv(*temp);
836 temp = hv_fetch(href, "ERR_STR", 7, 1),
837 err_string = newSVsv(*temp);
839 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
840 sur_flag = newSVsv(*temp);
842 temp = hv_fetch(href, "REP_FORM", 8, 1);
843 rep_form = newSVsv(*temp);
845 temp = hv_fetch(href, "HANDLE", 6, 1);
846 point = newSVsv(*temp);
854 ptr = SvPV(basename, length);
855 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
856 strcpy(ODR_basename, ptr);
857 rr->basename = ODR_basename;
859 ptr = SvPV(rep_form, length);
860 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
861 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
863 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
865 rr->output_format_raw = ODR_oid_buf;
867 ptr = SvPV(record, length);
868 oid = oid_getentbyoid(ODR_oid_buf);
869 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
871 rr->record = (char *) read_grs1(ptr, rr->stream);
876 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
877 strcpy(ODR_record, ptr);
878 rr->record = ODR_record;
881 zhandle->handle = point;
883 rr->last_in_set = SvIV(last);
887 rr->errcode = SvIV(err_code);
888 ptr = SvPV(err_string, length);
889 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
890 strcpy(ODR_errstr, ptr);
891 rr->errstring = ODR_errstr;
893 rr->surrogate_flag = SvIV(sur_flag);
895 wrbuf_free(oid_dotted, 1);
909 int bend_present(void *handle, bend_present_rr *rr)
919 Z_RecordComposition *composition;
920 Z_ElementSetNames *simple;
923 Zfront_handle *zhandle = (Zfront_handle *)handle;
926 /* WRBUF oid_dotted; */
933 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
934 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
935 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
936 hv_store(href, "START", 5, newSViv(rr->start), 0);
937 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
938 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
939 /*oid_dotted = oid2dotted(rr->request_format_raw);
940 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
941 hv_store(href, "HITS", 4, newSViv(0), 0);
942 hv_store(href, "PID", 3, newSViv(getpid()), 0);
945 composition = rr->comp;
946 if (composition->which == Z_RecordComp_simple)
948 simple = composition->u.simple;
949 if (simple->which == Z_ElementSetNames_generic)
951 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
968 XPUSHs(sv_2mortal(newRV( (SV*) href)));
972 handler_cv = simpleserver_sv2cv( present_ref );
973 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
977 temp = hv_fetch(href, "ERR_CODE", 8, 1);
978 err_code = newSVsv(*temp);
980 temp = hv_fetch(href, "ERR_STR", 7, 1);
981 err_string = newSVsv(*temp);
983 temp = hv_fetch(href, "HITS", 4, 1);
984 hits = newSVsv(*temp);
986 temp = hv_fetch(href, "HANDLE", 6, 1);
987 point = newSVsv(*temp);
994 rr->errcode = SvIV(err_code);
995 rr->hits = SvIV(hits);
997 ptr = SvPV(err_string, len);
998 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
999 strcpy(ODR_errstr, ptr);
1000 rr->errstring = ODR_errstr;
1001 /* wrbuf_free(oid_dotted, 1);*/
1002 zhandle->handle = point;
1005 sv_free(err_string);
1007 sv_free( (SV*) href);
1013 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1015 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1020 int bend_delete(void *handle, bend_delete_rr *rr)
1022 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
1027 int bend_scan(void *handle, bend_scan_rr *rr)
1034 struct scan_entry *scan_list;
1035 struct scan_entry *buffer;
1036 int *step_size = rr->step_size;
1040 SV *err_code = sv_newmortal();
1041 SV *err_str = sv_newmortal();
1042 SV *point = sv_newmortal();
1043 SV *status = sv_newmortal();
1044 SV *number = sv_newmortal();
1051 Zfront_handle *zhandle = (Zfront_handle *)handle;
1059 if (rr->term->term->which == Z_Term_general)
1061 term_len = rr->term->term->u.general->len;
1062 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1064 rr->errcode = 229; /* Unsupported term type */
1067 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1068 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1069 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1070 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1071 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1072 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1073 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1074 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1076 basenames = rr->basenames;
1077 for (i = 0; i < rr->num_bases; i++)
1079 av_push(aref, newSVpv(*basenames++, 0));
1081 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1085 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1089 handler_cv = simpleserver_sv2cv( scan_ref );
1090 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1094 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1095 err_code = newSVsv(*temp);
1097 temp = hv_fetch(href, "ERR_STR", 7, 1);
1098 err_str = newSVsv(*temp);
1100 temp = hv_fetch(href, "HANDLE", 6, 1);
1101 point = newSVsv(*temp);
1103 temp = hv_fetch(href, "STATUS", 6, 1);
1104 status = newSVsv(*temp);
1106 temp = hv_fetch(href, "NUMBER", 6, 1);
1107 number = newSVsv(*temp);
1109 temp = hv_fetch(href, "ENTRIES", 7, 1);
1110 entries_ref = newSVsv(*temp);
1116 ptr = SvPV(err_str, len);
1117 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1118 strcpy(ODR_errstr, ptr);
1119 rr->errstring = ODR_errstr;
1120 rr->errcode = SvIV(err_code);
1121 rr->num_entries = SvIV(number);
1122 rr->status = SvIV(status);
1123 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1125 entries = (AV *)SvRV(entries_ref);
1126 for (i = 0; i < rr->num_entries; i++)
1128 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1129 temp = hv_fetch(scan_item, "TERM", 4, 1);
1130 ptr = SvPV(*temp, len);
1131 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1132 strcpy(buffer->term, ptr);
1133 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1134 buffer->occurrences = SvIV(*temp);
1136 hv_undef(scan_item);
1138 rr->entries = scan_list;
1139 zhandle->handle = point;
1146 sv_free((SV *)href);
1148 sv_free((SV *)aref);
1150 sv_free((SV *)list);
1152 /*sv_free((SV *)entries);*/
1153 sv_free(entries_ref);
1159 bend_initresult *bend_init(bend_initrequest *q)
1161 int dummy = simpleserver_clone();
1162 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1170 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1178 char *passwd = NULL;
1185 /*q->bend_sort = bend_sort;*/
1188 q->bend_search = bend_search;
1192 q->bend_present = bend_present;
1194 /*q->bend_esrequest = bend_esrequest;*/
1195 /*q->bend_delete = bend_delete;*/
1198 q->bend_fetch = bend_fetch;
1202 q->bend_scan = bend_scan;
1205 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1206 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1207 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1208 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1209 hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1210 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1211 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1212 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1214 if (q->auth->which == Z_IdAuthentication_open) {
1215 char *openpass = xstrdup (q->auth->u.open);
1216 char *cp = strchr (openpass, '/');
1219 user = nmem_strdup (odr_getmem (q->stream), openpass);
1220 passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1223 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1224 user = q->auth->u.idPass->userId;
1225 passwd = q->auth->u.idPass->password;
1227 /* ### some code paths have user/password unassigned here */
1228 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1229 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1234 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1238 if (init_ref != NULL)
1240 handler_cv = simpleserver_sv2cv( init_ref );
1241 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1246 temp = hv_fetch(href, "IMP_ID", 6, 1);
1247 id = newSVsv(*temp);
1249 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1250 name = newSVsv(*temp);
1252 temp = hv_fetch(href, "IMP_VER", 7, 1);
1253 ver = newSVsv(*temp);
1255 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1256 status = newSVsv(*temp);
1258 temp = hv_fetch(href, "ERR_STR", 7, 1);
1259 err_str = newSVsv(*temp);
1261 temp = hv_fetch(href, "HANDLE", 6, 1);
1262 handle= newSVsv(*temp);
1268 zhandle->handle = handle;
1269 r->errcode = SvIV(status);
1270 ptr = SvPV(err_str, len);
1271 r->errstring = (char *)odr_malloc(q->stream, len + 1);
1272 strcpy(r->errstring, ptr);
1274 r->handle = zhandle;
1275 ptr = SvPV(id, len);
1276 q->implementation_id = (char *)xmalloc(len + 1);
1277 strcpy(q->implementation_id, ptr);
1278 ptr = SvPV(name, len);
1279 q->implementation_name = (char *)xmalloc(len + 1);
1280 strcpy(q->implementation_name, ptr);
1281 /* q->implementation_name = SvPV(name, len);*/
1282 ptr = SvPV(ver, len);
1283 q->implementation_version = (char *)xmalloc(len + 1);
1284 strcpy(q->implementation_version, ptr);
1290 void bend_close(void *handle)
1293 Zfront_handle *zhandle = (Zfront_handle *)handle;
1304 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1308 XPUSHs(sv_2mortal(newRV((SV *)href)));
1312 handler_cv = simpleserver_sv2cv( close_ref );
1313 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1322 simpleserver_free();
1328 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1331 set_init_handler(arg)
1334 init_ref = newSVsv(arg);
1338 set_close_handler(arg)
1341 close_ref = newSVsv(arg);
1345 set_sort_handler(arg)
1348 sort_ref = newSVsv(arg);
1351 set_search_handler(arg)
1354 search_ref = newSVsv(arg);
1358 set_fetch_handler(arg)
1361 fetch_ref = newSVsv(arg);
1365 set_present_handler(arg)
1368 present_ref = newSVsv(arg);
1372 set_esrequest_handler(arg)
1375 esrequest_ref = newSVsv(arg);
1379 set_delete_handler(arg)
1382 delete_ref = newSVsv(arg);
1386 set_scan_handler(arg)
1389 scan_ref = newSVsv(arg);
1401 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1403 for (i = 0; i < items; i++)
1405 ptr = SvPV(ST(i), len);
1406 *argv_buf = (char *)xmalloc(len + 1);
1407 strcpy(*argv_buf++, ptr);
1410 root_perl_context = PERL_GET_CONTEXT;
1411 nmem_mutex_create(&simpleserver_mutex);
1416 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1424 RETVAL = BEND_SCAN_SUCCESS;
1431 RETVAL = BEND_SCAN_PARTIAL;