2 * $Id: SimpleServer.xs,v 1.73 2007-08-20 10:59:11 mike 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
36 #include <yaz/backend.h>
38 #include <yaz/wrbuf.h>
39 #include <yaz/querytowrbuf.h>
41 #include <yaz/mutex.h>
42 #include <yaz/oid_db.h>
49 #define GRS_MAX_FIELDS 500
53 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
54 #define sv_undef PL_sv_undef
57 YAZ_MUTEX simpleserver_mutex;
60 SV *ghandle; /* Global handle specified at creation */
61 SV *handle; /* Per-connection handle set at Init */
63 /* ### These callback-reference elements are never used! */
76 int stop_flag; /* is used to stop server prematurely .. */
79 #define ENABLE_STOP_SERVER 0
81 SV *_global_ghandle = NULL; /* To be copied into zhandle then ignored */
85 SV *search_ref = NULL;
87 SV *present_ref = NULL;
88 SV *esrequest_ref = NULL;
89 SV *delete_ref = NULL;
91 SV *explain_ref = NULL;
92 PerlInterpreter *root_perl_context;
94 #define GRS_BUF_SIZE 8192
98 * Inspects the SV indicated by svp, and returns a null pointer if
99 * it's an undefined value, or a string allocation from `stream'
100 * otherwise. Using this when filling in addinfo avoids those
101 * irritating "Use of uninitialized value in subroutine entry"
102 * warnings from Perl.
104 char *string_or_undef(SV **svp, ODR stream) {
111 ptr = SvPV(*svp, len);
112 buf = (char*) odr_malloc(stream, len+1);
118 CV * simpleserver_sv2cv(SV *handler) {
122 if (SvPOK(handler)) {
124 buf = SvPV( handler, len);
125 if ( !( ret = perl_get_cv(buf, FALSE ) ) ) {
126 fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf );
132 return (CV *) handler;
136 /* debugging routine to check for destruction of Perl interpreters */
138 void tst_clones(void)
141 PerlInterpreter *parent = PERL_GET_CONTEXT;
142 for (i = 0; i<5000; i++)
144 PerlInterpreter *perl_interp;
146 PERL_SET_CONTEXT(parent);
147 PL_perl_destruct_level = 2;
148 perl_interp = perl_clone(parent, CLONEf_CLONE_HOST);
149 PL_perl_destruct_level = 2;
150 PERL_SET_CONTEXT(perl_interp);
151 perl_destruct(perl_interp);
152 perl_free(perl_interp);
158 int simpleserver_clone(void) {
160 yaz_mutex_enter(simpleserver_mutex);
163 PerlInterpreter *current = PERL_GET_CONTEXT;
165 /* if current is unset, then we're in a new thread with
166 * no Perl interpreter for it. So we must create one .
167 * This will only happen when threaded is used..
170 PerlInterpreter *perl_interp;
171 PERL_SET_CONTEXT( root_perl_context );
172 perl_interp = perl_clone(root_perl_context, CLONEf_CLONE_HOST);
173 PERL_SET_CONTEXT( perl_interp );
176 yaz_mutex_leave(simpleserver_mutex);
182 void simpleserver_free(void) {
183 yaz_mutex_enter(simpleserver_mutex);
186 PerlInterpreter *current_interp = PERL_GET_CONTEXT;
188 /* If current Perl Interp is different from root interp, then
189 * we're in threaded mode and we must destroy..
191 if (current_interp != root_perl_context) {
192 PL_perl_destruct_level = 2;
193 PERL_SET_CONTEXT(current_interp);
194 perl_destruct(current_interp);
195 perl_free(current_interp);
198 yaz_mutex_leave(simpleserver_mutex);
202 Z_GenericRecord *read_grs1(char *str, ODR o)
205 char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
206 char value[GRS_BUF_SIZE+1];
207 Z_GenericRecord *r = 0;
210 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
211 r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
220 ptr = strchr(str, '\n');
225 if (len > GRS_BUF_SIZE) {
226 yaz_log(YLOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
229 strncpy(line, str, len);
233 while (*buf && isspace(*buf))
236 memmove(original, str, strlen(str));
239 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
241 yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
244 if (!type && *value == '0')
246 if (!(buf = strchr(buf, ')')))
249 while (*buf && isspace(*buf))
251 if (r->num_elements >= GRS_MAX_FIELDS)
253 yaz_log(YLOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
256 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
257 t->tagType = odr_intdup(o, type);
258 t->tagValue = (Z_StringOrNumeric *)
259 odr_malloc(o, sizeof(Z_StringOrNumeric));
260 if ((ivalue = atoi(value)))
262 t->tagValue->which = Z_StringOrNumeric_numeric;
263 t->tagValue->u.numeric = odr_intdup(o, ivalue);
267 t->tagValue->which = Z_StringOrNumeric_string;
268 t->tagValue->u.string = odr_strdup(o, value);
270 t->tagOccurrence = 0;
272 t->appliedVariant = 0;
273 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
276 c->which = Z_ElementData_subtree;
277 c->u.subtree = read_grs1(str, o);
281 c->which = Z_ElementData_string;
282 c->u.string = odr_strdup(o, buf);
290 static void oid2str(Odr_oid *o, WRBUF buf)
292 for (; *o >= 0; o++) {
294 sprintf(ibuf, "%d", *o);
295 wrbuf_puts(buf, ibuf);
297 wrbuf_putc(buf, '.');
301 WRBUF oid2dotted(Odr_oid *oid)
303 WRBUF buf = wrbuf_alloc();
309 WRBUF zquery2pquery(Z_Query *q)
311 WRBUF buf = wrbuf_alloc();
313 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
315 yaz_rpnquery_to_wrbuf(buf, q->u.type_1);
320 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
322 void fatal(char *fmt, ...)
326 fprintf(stderr, "FATAL (SimpleServer): ");
328 vfprintf(stderr, fmt, ap);
330 fprintf(stderr, "\n");
335 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
337 * Creates a new Perl object of type `class'; the newly-created scalar
338 * that is a reference to the blessed thingy `referent' is returned.
340 static SV *newObject(char *class, SV *referent)
345 sv = newRV_noinc((SV*) referent);
346 stash = gv_stashpv(class, 0);
348 fatal("attempt to create object of undefined class '%s'", class);
349 /*assert(stash != 0);*/
355 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
356 static void setMember(HV *hv, char *name, SV *val)
358 /* We don't increment `val's reference count -- I think this is
359 * right because it's created with a refcount of 1, and in fact
360 * the reference via this hash is the only reference to it in
363 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
364 fatal("couldn't store member in hash");
368 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
369 static SV *translateOID(Odr_oid *x)
371 /* Yaz represents an OID by an int array terminated by a negative
372 * value, typically -1; we represent it as a reference to a
373 * blessed scalar string of "."-separated elements.
379 for (i = 0; x[i] >= 0; i++) {
380 sprintf(buf + strlen(buf), "%d", (int) x[i]);
386 * ### We'd like to return a blessed scalar (string) here, but of
387 * course you can't do that in Perl: only references can be
388 * blessed, so we'd have to return a _reference_ to a string, and
389 * bless _that_. Better to do without the blessing, I think.
392 return newSVpv(buf, 0);
394 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
399 static SV *apt2perl(Z_AttributesPlusTerm *at)
405 if (at->term->which != Z_Term_general)
406 fatal("can't handle RPN terms other than general");
408 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
409 if (at->attributes) {
411 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
412 (SV*) (av = newAV()));
413 for (i = 0; i < at->attributes->num_attributes; i++) {
414 Z_AttributeElement *elem = at->attributes->attributes[i];
416 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
417 (SV*) (hv2 = newHV()));
418 if (elem->attributeSet)
419 setMember(hv2, "attributeSet",
420 translateOID(elem->attributeSet));
421 setMember(hv2, "attributeType",
422 newSViv(*elem->attributeType));
423 if (elem->which == Z_AttributeValue_numeric) {
424 setMember(hv2, "attributeValue",
425 newSViv(*elem->value.numeric));
427 assert(elem->which == Z_AttributeValue_complex);
428 Z_ComplexAttribute *c = elem->value.complex;
429 Z_StringOrNumeric *son;
430 /* We ignore semantic actions and multiple values */
431 assert(c->num_list > 0);
433 if (son->which == Z_StringOrNumeric_numeric) {
434 setMember(hv2, "attributeValue",
435 newSViv(*son->u.numeric));
436 } else { /*Z_StringOrNumeric_string*/
437 setMember(hv2, "attributeValue",
438 newSVpv(son->u.string, 0));
443 setMember(hv, "attributes", attrs);
445 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
446 at->term->u.general->len));
451 static SV *rpn2perl(Z_RPNStructure *s)
459 case Z_RPNStructure_simple:
462 case Z_Operand_resultSetId: {
463 /* This code causes a SIGBUS on my machine, and I have no
464 idea why. It seems as clear as day to me */
466 char *rsid = (char*) o->u.resultSetId;
467 /*printf("Encoding resultSetId '%s'\n", rsid);*/
468 sv = newObject("Net::Z3950::RPN::RSID", (SV*) (hv = newHV()));
469 /*printf("Made sv=0x%lx, hv=0x%lx\n", (unsigned long) sv ,(unsigned long) hv);*/
470 sv2 = newSVpv(rsid, strlen(rsid));
471 setMember(hv, "id", sv2);
472 /*printf("Set hv{id} to 0x%lx\n", (unsigned long) sv2);*/
477 return apt2perl(o->u.attributesPlusTerm);
480 fatal("unknown RPN simple type %d", (int) o->which);
483 case Z_RPNStructure_complex: {
485 Z_Complex *c = s->u.complex;
486 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
487 switch (c->roperator->which) {
488 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
489 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
490 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
491 case Z_Operator_prox: fatal("proximity not yet supported");
492 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
494 sv = newObject(type, (SV*) (av = newAV()));
495 if ((tmp = rpn2perl(c->s1)) == 0)
498 if ((tmp = rpn2perl(c->s2)) == 0)
505 fatal("unknown RPN node type %d", (int) s->which);
512 /* Decode the Z_SortAttributes struct and store the whole thing into the
515 int simpleserver_ExpandSortAttributes (HV *sort_spec, Z_SortAttributes *sattr)
517 WRBUF attrset_wr = wrbuf_alloc();
519 Z_AttributeList *attr_list = sattr->list;
522 oid2str(sattr->id, attrset_wr);
523 hv_store(sort_spec, "ATTRSET", 7,
524 newSVpv(attrset_wr->buf, attrset_wr->pos), 0);
525 wrbuf_destroy(attrset_wr);
527 hv_store(sort_spec, "SORT_ATTR", 9, newRV( sv_2mortal( (SV*) list ) ), 0);
529 for (i = 0; i < attr_list->num_attributes; i++)
531 Z_AttributeElement *attr = *attr_list->attributes++;
532 HV *attr_spec = newHV();
534 av_push(list, newRV( sv_2mortal( (SV*) attr_spec ) ));
535 hv_store(attr_spec, "ATTR_TYPE", 9, newSViv(*attr->attributeType), 0);
537 if (attr->which == Z_AttributeValue_numeric)
539 hv_store(attr_spec, "ATTR_VALUE", 10,
540 newSViv(*attr->value.numeric), 0);
550 /* Decode the Z_SortKeySpec struct and store the whole thing in a perl hash */
551 int simpleserver_SortKeySpecToHash (HV *sort_spec, Z_SortKeySpec *spec)
553 Z_SortElement *element = spec->sortElement;
555 hv_store(sort_spec, "RELATION", 8, newSViv(*spec->sortRelation), 0);
556 hv_store(sort_spec, "CASE", 4, newSViv(*spec->caseSensitivity), 0);
557 hv_store(sort_spec, "MISSING", 7, newSViv(spec->which), 0);
559 if (element->which == Z_SortElement_generic)
561 Z_SortKey *key = element->u.generic;
563 if (key->which == Z_SortKey_sortField)
565 hv_store(sort_spec, "SORTFIELD", 9,
566 newSVpv((char *) key->u.sortField, 0), 0);
568 else if (key->which == Z_SortKey_elementSpec)
570 Z_Specification *zspec = key->u.elementSpec;
572 hv_store(sort_spec, "ELEMENTSPEC_TYPE", 16,
573 newSViv(zspec->which), 0);
575 if (zspec->which == Z_Schema_oid)
577 WRBUF elementSpec = wrbuf_alloc();
579 oid2str(zspec->schema.oid, elementSpec);
580 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
581 newSVpv(elementSpec->buf, elementSpec->pos), 0);
582 wrbuf_destroy(elementSpec);
584 else if (zspec->which == Z_Schema_uri)
586 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
587 newSVpv((char *) zspec->schema.uri, 0), 0);
590 else if (key->which == Z_SortKey_sortAttributes)
592 return simpleserver_ExpandSortAttributes(sort_spec,
593 key->u.sortAttributes);
609 static SV *zquery2perl(Z_Query *q)
614 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
616 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
617 if (q->u.type_1->attributeSetId)
618 setMember(hv, "attributeSet",
619 translateOID(q->u.type_1->attributeSetId));
620 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
625 int bend_sort(void *handle, bend_sort_rr *rr)
638 char **input_setnames;
639 Zfront_handle *zhandle = (Zfront_handle *)handle;
640 Z_SortKeySpecList *sort_spec = rr->sort_sequence;
648 input_setnames = rr->input_setnames;
649 for (i = 0; i < rr->num_input_setnames; i++)
651 av_push(aref, newSVpv(*input_setnames++, 0));
655 for (i = 0; i < sort_spec->num_specs; i++)
657 Z_SortKeySpec *spec = *sort_spec->specs++;
658 HV *sort_spec = newHV();
660 if ( simpleserver_SortKeySpecToHash(sort_spec, spec) )
661 av_push(sort_seq, newRV( sv_2mortal( (SV*) sort_spec ) ));
670 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
671 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
672 hv_store(href, "SEQUENCE", 8, newRV( (SV*) sort_seq), 0);
673 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
674 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
675 hv_store(href, "STATUS", 6, newSViv(0), 0);
676 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
677 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
681 XPUSHs(sv_2mortal(newRV( (SV*) href)));
685 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
689 temp = hv_fetch(href, "ERR_CODE", 8, 1);
690 err_code = newSVsv(*temp);
692 temp = hv_fetch(href, "ERR_STR", 7, 1);
693 err_str = newSVsv(*temp);
695 temp = hv_fetch(href, "STATUS", 6, 1);
696 status = newSVsv(*temp);
698 temp = hv_fetch(href, "HANDLE", 6, 1);
699 point = newSVsv(*temp);
705 sv_free( (SV*) aref);
706 sv_free( (SV*) href);
707 sv_free( (SV*) sort_seq);
709 rr->errcode = SvIV(err_code);
710 rr->sort_status = SvIV(status);
712 ptr = SvPV(err_str, len);
713 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
714 strcpy(ODR_err_str, ptr);
715 rr->errstring = ODR_err_str;
716 zhandle->handle = point;
730 int bend_search(void *handle, bend_search_rr *rr)
739 Zfront_handle *zhandle = (Zfront_handle *)handle;
748 basenames = rr->basenames;
749 for (i = 0; i < rr->num_bases; i++)
751 av_push(aref, newSVpv(*basenames++, 0));
753 #if ENABLE_STOP_SERVER
754 if (rr->num_bases == 1 && !strcmp(rr->basenames[0], "XXstop"))
756 zhandle->stop_flag = 1;
760 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
761 if (rr->srw_sortKeys && *rr->srw_sortKeys)
762 hv_store(href, "SRW_SORTKEYS", 12, newSVpv(rr->srw_sortKeys, 0), 0);
763 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
764 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
765 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
766 hv_store(href, "HITS", 4, newSViv(0), 0);
767 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
768 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
769 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
770 hv_store(href, "PID", 3, newSViv(getpid()), 0);
771 if ((rpnSV = zquery2perl(rr->query)) != 0) {
772 hv_store(href, "RPN", 3, rpnSV, 0);
774 query = zquery2pquery(rr->query);
777 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
779 else if (rr->query->which == Z_Query_type_104 &&
780 rr->query->u.type_104->which == Z_External_CQL) {
781 hv_store(href, "CQL", 3,
782 newSVpv(rr->query->u.type_104->u.cql, 0), 0);
791 XPUSHs(sv_2mortal(newRV( (SV*) href)));
795 handler_cv = simpleserver_sv2cv( search_ref );
796 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
800 temp = hv_fetch(href, "HITS", 4, 1);
801 rr->hits = SvIV(*temp);
803 temp = hv_fetch(href, "ERR_CODE", 8, 1);
804 rr->errcode = SvIV(*temp);
806 temp = hv_fetch(href, "ERR_STR", 7, 1);
807 rr->errstring = string_or_undef(temp, rr->stream);
809 temp = hv_fetch(href, "HANDLE", 6, 1);
810 point = newSVsv(*temp);
815 zhandle->handle = point;
816 sv_free( (SV*) aref);
817 sv_free( (SV*) href);
819 wrbuf_destroy(query);
827 int bend_fetch(void *handle, bend_fetch_rr *rr)
845 Zfront_handle *zhandle = (Zfront_handle *)handle;
848 Z_RecordComposition *composition;
849 Z_ElementSetNames *simple;
859 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
861 hv_store(href, "SCHEMA", 6, newSVpv(rr->schema, 0), 0);
863 hv_store(href, "SCHEMA", 6, newSVpv("", 0), 0);
865 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
866 if (rr->request_format != 0) {
867 oid_dotted = oid2dotted(rr->request_format);
869 /* Probably an SRU request: assume XML is required */
870 oid_dotted = wrbuf_alloc();
871 wrbuf_puts(oid_dotted, "1.2.840.10003.5.109.10");
873 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
874 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
875 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
876 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
877 hv_store(href, "LAST", 4, newSViv(0), 0);
878 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
879 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
880 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
881 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
882 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
883 hv_store(href, "PID", 3, newSViv(getpid()), 0);
886 composition = rr->comp;
887 if (composition->which == Z_RecordComp_simple)
889 simple = composition->u.simple;
890 if (simple->which == Z_ElementSetNames_generic)
892 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
899 else if (composition->which == Z_RecordComp_complex)
901 if (composition->u.complex->generic &&
903 composition->u.complex->generic &&
904 composition->u.complex->generic->elementSpec &&
905 composition->u.complex->generic->elementSpec->which ==
906 Z_ElementSpec_elementSetName)
908 complex = composition->u.complex;
909 hv_store(href, "COMP", 4,
910 newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
914 #if 0 /* For now ignore this error, which is ubiquitous in SRU */
915 fprintf(stderr, "complex is weird\n");
930 XPUSHs(sv_2mortal(newRV( (SV*) href)));
934 handler_cv = simpleserver_sv2cv( fetch_ref );
935 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
939 temp = hv_fetch(href, "BASENAME", 8, 1);
940 basename = newSVsv(*temp);
942 temp = hv_fetch(href, "RECORD", 6, 1);
943 record = newSVsv(*temp);
945 temp = hv_fetch(href, "LAST", 4, 1);
946 last = newSVsv(*temp);
948 temp = hv_fetch(href, "ERR_CODE", 8, 1);
949 err_code = newSVsv(*temp);
951 temp = hv_fetch(href, "ERR_STR", 7, 1),
952 err_string = newSVsv(*temp);
954 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
955 sur_flag = newSVsv(*temp);
957 temp = hv_fetch(href, "REP_FORM", 8, 1);
958 rep_form = newSVsv(*temp);
960 temp = hv_fetch(href, "SCHEMA", 6, 1);
962 schema = newSVsv(*temp);
963 ptr = SvPV(schema, length);
965 rr->schema = (char *)odr_malloc(rr->stream, length + 1);
966 strcpy(rr->schema, ptr);
970 temp = hv_fetch(href, "HANDLE", 6, 1);
971 point = newSVsv(*temp);
976 ptr = SvPV(basename, length);
977 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
978 strcpy(ODR_basename, ptr);
979 rr->basename = ODR_basename;
981 ptr = SvPV(rep_form, length);
983 rr->output_format = yaz_string_to_oid_odr(yaz_oid_std(),
984 CLASS_RECSYN, ptr, rr->stream);
985 if (!rr->output_format)
987 printf("Net::Z3950::SimpleServer: WARNING: Bad OID %s\n", ptr);
989 odr_oiddup(rr->stream, yaz_oid_recsyn_sutrs);
991 ptr = SvPV(record, length);
992 /* Treat GRS-1 records separately */
993 if (!oid_oidcmp(rr->output_format, yaz_oid_recsyn_grs_1))
995 rr->record = (char *) read_grs1(ptr, rr->stream);
1000 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
1001 strcpy(ODR_record, ptr);
1002 rr->record = ODR_record;
1005 zhandle->handle = point;
1007 rr->last_in_set = SvIV(last);
1011 rr->errcode = SvIV(err_code);
1012 ptr = SvPV(err_string, length);
1013 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
1014 strcpy(ODR_errstr, ptr);
1015 rr->errstring = ODR_errstr;
1017 rr->surrogate_flag = SvIV(sur_flag);
1019 wrbuf_destroy(oid_dotted);
1020 sv_free((SV*) href);
1024 sv_free(err_string);
1040 int bend_present(void *handle, bend_present_rr *rr)
1049 Z_RecordComposition *composition;
1050 Z_ElementSetNames *simple;
1051 Z_CompSpec *complex;
1054 Zfront_handle *zhandle = (Zfront_handle *)handle;
1057 /* WRBUF oid_dotted; */
1064 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1065 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1066 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1067 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1068 hv_store(href, "START", 5, newSViv(rr->start), 0);
1069 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
1070 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
1071 /*oid_dotted = oid2dotted(rr->request_format_raw);
1072 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
1073 hv_store(href, "HITS", 4, newSViv(0), 0);
1074 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1077 composition = rr->comp;
1078 if (composition->which == Z_RecordComp_simple)
1080 simple = composition->u.simple;
1081 if (simple->which == Z_ElementSetNames_generic)
1083 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
1091 else if (composition->which == Z_RecordComp_complex)
1093 if (composition->u.complex->generic &&
1095 composition->u.complex->generic &&
1096 composition->u.complex->generic->elementSpec &&
1097 composition->u.complex->generic->elementSpec->which ==
1098 Z_ElementSpec_elementSetName)
1100 complex = composition->u.complex;
1101 hv_store(href, "COMP", 4,
1102 newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
1119 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1123 handler_cv = simpleserver_sv2cv( present_ref );
1124 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1128 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1129 err_code = newSVsv(*temp);
1131 temp = hv_fetch(href, "ERR_STR", 7, 1);
1132 err_string = newSVsv(*temp);
1134 temp = hv_fetch(href, "HITS", 4, 1);
1135 hits = newSVsv(*temp);
1137 temp = hv_fetch(href, "HANDLE", 6, 1);
1138 point = newSVsv(*temp);
1145 rr->errcode = SvIV(err_code);
1146 rr->hits = SvIV(hits);
1148 ptr = SvPV(err_string, len);
1149 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1150 strcpy(ODR_errstr, ptr);
1151 rr->errstring = ODR_errstr;
1152 /* wrbuf_free(oid_dotted, 1);*/
1153 zhandle->handle = point;
1156 sv_free(err_string);
1158 sv_free( (SV*) href);
1164 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1166 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1171 int bend_delete(void *handle, bend_delete_rr *rr)
1173 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
1178 int bend_scan(void *handle, bend_scan_rr *rr)
1185 struct scan_entry *scan_list;
1186 struct scan_entry *buffer;
1187 int *step_size = rr->step_size;
1191 SV *err_code = sv_newmortal();
1192 SV *err_str = sv_newmortal();
1193 SV *point = sv_newmortal();
1194 SV *status = sv_newmortal();
1195 SV *number = sv_newmortal();
1201 Zfront_handle *zhandle = (Zfront_handle *)handle;
1211 /* RPN is better than TERM since it includes attributes */
1212 if ((rpnSV = apt2perl(rr->term)) != 0) {
1213 setMember(href, "RPN", rpnSV);
1216 if (rr->term->term->which == Z_Term_general)
1218 term_len = rr->term->term->u.general->len;
1219 hv_store(href, "TERM", 4, newSVpv((char*) rr->term->term->u.general->buf, term_len), 0);
1221 rr->errcode = 229; /* Unsupported term type */
1224 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1225 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1226 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1227 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1228 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1229 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1230 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1231 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1232 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1234 basenames = rr->basenames;
1235 for (i = 0; i < rr->num_bases; i++)
1237 av_push(aref, newSVpv(*basenames++, 0));
1239 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1243 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1247 handler_cv = simpleserver_sv2cv( scan_ref );
1248 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1252 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1253 err_code = newSVsv(*temp);
1255 temp = hv_fetch(href, "ERR_STR", 7, 1);
1256 err_str = newSVsv(*temp);
1258 temp = hv_fetch(href, "HANDLE", 6, 1);
1259 point = newSVsv(*temp);
1261 temp = hv_fetch(href, "STATUS", 6, 1);
1262 status = newSVsv(*temp);
1264 temp = hv_fetch(href, "NUMBER", 6, 1);
1265 number = newSVsv(*temp);
1267 temp = hv_fetch(href, "ENTRIES", 7, 1);
1268 entries_ref = newSVsv(*temp);
1274 ptr = SvPV(err_str, len);
1275 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1276 strcpy(ODR_errstr, ptr);
1277 rr->errstring = ODR_errstr;
1278 rr->errcode = SvIV(err_code);
1279 rr->num_entries = SvIV(number);
1280 rr->status = SvIV(status);
1281 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1283 entries = (AV *)SvRV(entries_ref);
1284 if (rr->errcode == 0) for (i = 0; i < rr->num_entries; i++)
1286 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1287 temp = hv_fetch(scan_item, "TERM", 4, 1);
1288 ptr = SvPV(*temp, len);
1289 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1290 strcpy(buffer->term, ptr);
1291 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1292 buffer->occurrences = SvIV(*temp);
1294 hv_undef(scan_item);
1296 rr->entries = scan_list;
1297 zhandle->handle = point;
1304 sv_free((SV *)href);
1306 sv_free((SV *)aref);
1308 sv_free((SV *)list);
1310 /*sv_free((SV *)entries);*/
1311 sv_free(entries_ref);
1316 int bend_explain(void *handle, bend_explain_rr *q)
1324 Zfront_handle *zhandle = (Zfront_handle *)handle;
1331 hv_store(href, "EXPLAIN", 7, newSVpv("", 0), 0);
1332 hv_store(href, "DATABASE", 8, newSVpv(q->database, 0), 0);
1333 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1334 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1337 XPUSHs(sv_2mortal(newRV((SV*) href)));
1340 handler_cv = simpleserver_sv2cv(explain_ref);
1341 perl_call_sv((SV*) handler_cv, G_SCALAR | G_DISCARD);
1345 temp = hv_fetch(href, "EXPLAIN", 7, 1);
1346 explainsv = newSVsv(*temp);
1352 explain = SvPV(explainsv, len);
1353 q->explain_buf = (char*) odr_malloc(q->stream, len + 1);
1354 strcpy(q->explain_buf, explain);
1359 bend_initresult *bend_init(bend_initrequest *q)
1361 int dummy = simpleserver_clone();
1362 bend_initresult *r = (bend_initresult *)
1363 odr_malloc (q->stream, sizeof(*r));
1368 NMEM nmem = nmem_create();
1369 Zfront_handle *zhandle = (Zfront_handle *) nmem_malloc (nmem,
1378 zhandle->ghandle = _global_ghandle;
1379 zhandle->nmem = nmem;
1380 zhandle->stop_flag = 0;
1384 q->bend_sort = bend_sort;
1388 q->bend_search = bend_search;
1392 q->bend_present = bend_present;
1394 /*q->bend_esrequest = bend_esrequest;*/
1395 /*q->bend_delete = bend_delete;*/
1398 q->bend_fetch = bend_fetch;
1402 q->bend_scan = bend_scan;
1406 q->bend_explain = bend_explain;
1410 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1411 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1412 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1413 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1414 hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1415 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1416 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1417 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1418 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1421 char *passwd = NULL;
1422 if (q->auth->which == Z_IdAuthentication_open) {
1424 user = nmem_strdup (odr_getmem (q->stream), q->auth->u.open);
1425 cp = strchr (user, '/');
1427 /* password after / given */
1431 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1432 user = q->auth->u.idPass->userId;
1433 passwd = q->auth->u.idPass->password;
1435 /* ### some code paths have user/password unassigned here */
1437 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1439 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1444 XPUSHs(sv_2mortal(newRV((SV*) href)));
1448 if (init_ref != NULL)
1450 handler_cv = simpleserver_sv2cv( init_ref );
1451 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1456 temp = hv_fetch(href, "IMP_ID", 6, 1);
1457 ptr = SvPV(*temp, len);
1458 q->implementation_id = nmem_strdup(nmem, ptr);
1460 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1461 ptr = SvPV(*temp, len);
1462 q->implementation_name = nmem_strdup(nmem, ptr);
1464 temp = hv_fetch(href, "IMP_VER", 7, 1);
1465 ptr = SvPV(*temp, len);
1466 q->implementation_version = nmem_strdup(nmem, ptr);
1468 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1469 r->errcode = SvIV(*temp);
1471 temp = hv_fetch(href, "ERR_STR", 7, 1);
1472 ptr = SvPV(*temp, len);
1473 r->errstring = (char *)odr_malloc(q->stream, len + 1);
1474 strcpy(r->errstring, ptr);
1476 temp = hv_fetch(href, "HANDLE", 6, 1);
1477 handle= newSVsv(*temp);
1478 zhandle->handle = handle;
1480 r->handle = zhandle;
1483 sv_free((SV*) href);
1492 void bend_close(void *handle)
1495 Zfront_handle *zhandle = (Zfront_handle *)handle;
1505 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1506 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1510 XPUSHs(sv_2mortal(newRV((SV *)href)));
1514 handler_cv = simpleserver_sv2cv( close_ref );
1515 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1519 sv_free((SV*) href);
1522 sv_free(zhandle->handle);
1526 stop_flag = zhandle->stop_flag;
1527 nmem_destroy(zhandle->nmem);
1528 simpleserver_free();
1536 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1545 _global_ghandle = newSVsv(arg);
1549 set_init_handler(arg)
1552 init_ref = newSVsv(arg);
1556 set_close_handler(arg)
1559 close_ref = newSVsv(arg);
1563 set_sort_handler(arg)
1566 sort_ref = newSVsv(arg);
1569 set_search_handler(arg)
1572 search_ref = newSVsv(arg);
1576 set_fetch_handler(arg)
1579 fetch_ref = newSVsv(arg);
1583 set_present_handler(arg)
1586 present_ref = newSVsv(arg);
1590 set_esrequest_handler(arg)
1593 esrequest_ref = newSVsv(arg);
1597 set_delete_handler(arg)
1600 delete_ref = newSVsv(arg);
1604 set_scan_handler(arg)
1607 scan_ref = newSVsv(arg);
1610 set_explain_handler(arg)
1613 explain_ref = newSVsv(arg);
1624 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1626 for (i = 0; i < items; i++)
1628 ptr = SvPV(ST(i), len);
1629 *argv_buf = (char *)xmalloc(len + 1);
1630 strcpy(*argv_buf++, ptr);
1633 root_perl_context = PERL_GET_CONTEXT;
1634 yaz_mutex_create(&simpleserver_mutex);
1636 /* only for debugging perl_clone .. */
1640 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1648 RETVAL = BEND_SCAN_SUCCESS;
1655 RETVAL = BEND_SCAN_PARTIAL;
1666 ptr = SvPV(arg, len);
1667 yaz_log(YLOG_LOG, "%.*s", len, ptr);
1670 yaz_diag_srw_to_bib1(srw_code)
1674 yaz_diag_bib1_to_srw(bib1_code)