2 * $Id: SimpleServer.xs,v 1.83 2008-09-02 15:16:34 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 Z_ComplexAttribute *c;
428 Z_StringOrNumeric *son;
429 assert(elem->which == Z_AttributeValue_complex);
430 c = elem->value.complex;
431 /* We ignore semantic actions and multiple values */
432 assert(c->num_list > 0);
434 if (son->which == Z_StringOrNumeric_numeric) {
435 setMember(hv2, "attributeValue",
436 newSViv(*son->u.numeric));
437 } else { /*Z_StringOrNumeric_string*/
438 setMember(hv2, "attributeValue",
439 newSVpv(son->u.string, 0));
444 setMember(hv, "attributes", attrs);
446 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
447 at->term->u.general->len));
452 static SV *rpn2perl(Z_RPNStructure *s)
460 case Z_RPNStructure_simple:
463 case Z_Operand_resultSetId: {
464 /* This code causes a SIGBUS on my machine, and I have no
465 idea why. It seems as clear as day to me */
467 char *rsid = (char*) o->u.resultSetId;
468 /*printf("Encoding resultSetId '%s'\n", rsid);*/
469 sv = newObject("Net::Z3950::RPN::RSID", (SV*) (hv = newHV()));
470 /*printf("Made sv=0x%lx, hv=0x%lx\n", (unsigned long) sv ,(unsigned long) hv);*/
471 sv2 = newSVpv(rsid, strlen(rsid));
472 setMember(hv, "id", sv2);
473 /*printf("Set hv{id} to 0x%lx\n", (unsigned long) sv2);*/
478 return apt2perl(o->u.attributesPlusTerm);
481 fatal("unknown RPN simple type %d", (int) o->which);
484 case Z_RPNStructure_complex: {
486 Z_Complex *c = s->u.complex;
487 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
488 switch (c->roperator->which) {
489 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
490 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
491 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
492 case Z_Operator_prox: fatal("proximity not yet supported");
493 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
495 sv = newObject(type, (SV*) (av = newAV()));
496 if ((tmp = rpn2perl(c->s1)) == 0)
499 if ((tmp = rpn2perl(c->s2)) == 0)
506 fatal("unknown RPN node type %d", (int) s->which);
513 /* Decode the Z_SortAttributes struct and store the whole thing into the
516 int simpleserver_ExpandSortAttributes (HV *sort_spec, Z_SortAttributes *sattr)
518 WRBUF attrset_wr = wrbuf_alloc();
520 Z_AttributeList *attr_list = sattr->list;
523 oid2str(sattr->id, attrset_wr);
524 hv_store(sort_spec, "ATTRSET", 7,
525 newSVpv(attrset_wr->buf, attrset_wr->pos), 0);
526 wrbuf_destroy(attrset_wr);
528 hv_store(sort_spec, "SORT_ATTR", 9, newRV( sv_2mortal( (SV*) list ) ), 0);
530 for (i = 0; i < attr_list->num_attributes; i++)
532 Z_AttributeElement *attr = *attr_list->attributes++;
533 HV *attr_spec = newHV();
535 av_push(list, newRV( sv_2mortal( (SV*) attr_spec ) ));
536 hv_store(attr_spec, "ATTR_TYPE", 9, newSViv(*attr->attributeType), 0);
538 if (attr->which == Z_AttributeValue_numeric)
540 hv_store(attr_spec, "ATTR_VALUE", 10,
541 newSViv(*attr->value.numeric), 0);
551 /* Decode the Z_SortKeySpec struct and store the whole thing in a perl hash */
552 int simpleserver_SortKeySpecToHash (HV *sort_spec, Z_SortKeySpec *spec)
554 Z_SortElement *element = spec->sortElement;
556 hv_store(sort_spec, "RELATION", 8, newSViv(*spec->sortRelation), 0);
557 hv_store(sort_spec, "CASE", 4, newSViv(*spec->caseSensitivity), 0);
558 hv_store(sort_spec, "MISSING", 7, newSViv(spec->which), 0);
560 if (element->which == Z_SortElement_generic)
562 Z_SortKey *key = element->u.generic;
564 if (key->which == Z_SortKey_sortField)
566 hv_store(sort_spec, "SORTFIELD", 9,
567 newSVpv((char *) key->u.sortField, 0), 0);
569 else if (key->which == Z_SortKey_elementSpec)
571 Z_Specification *zspec = key->u.elementSpec;
573 hv_store(sort_spec, "ELEMENTSPEC_TYPE", 16,
574 newSViv(zspec->which), 0);
576 if (zspec->which == Z_Schema_oid)
578 WRBUF elementSpec = wrbuf_alloc();
580 oid2str(zspec->schema.oid, elementSpec);
581 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
582 newSVpv(elementSpec->buf, elementSpec->pos), 0);
583 wrbuf_destroy(elementSpec);
585 else if (zspec->which == Z_Schema_uri)
587 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
588 newSVpv((char *) zspec->schema.uri, 0), 0);
591 else if (key->which == Z_SortKey_sortAttributes)
593 return simpleserver_ExpandSortAttributes(sort_spec,
594 key->u.sortAttributes);
610 static SV *zquery2perl(Z_Query *q)
615 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
617 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
618 if (q->u.type_1->attributeSetId)
619 setMember(hv, "attributeSet",
620 translateOID(q->u.type_1->attributeSetId));
621 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
626 int bend_sort(void *handle, bend_sort_rr *rr)
639 char **input_setnames;
640 Zfront_handle *zhandle = (Zfront_handle *)handle;
641 Z_SortKeySpecList *sort_spec = rr->sort_sequence;
649 input_setnames = rr->input_setnames;
650 for (i = 0; i < rr->num_input_setnames; i++)
652 av_push(aref, newSVpv(*input_setnames++, 0));
656 for (i = 0; i < sort_spec->num_specs; i++)
658 Z_SortKeySpec *spec = *sort_spec->specs++;
659 HV *sort_spec = newHV();
661 if ( simpleserver_SortKeySpecToHash(sort_spec, spec) )
662 av_push(sort_seq, newRV( sv_2mortal( (SV*) sort_spec ) ));
671 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
672 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
673 hv_store(href, "SEQUENCE", 8, newRV( (SV*) sort_seq), 0);
674 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
675 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
676 hv_store(href, "STATUS", 6, newSViv(0), 0);
677 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
678 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
682 XPUSHs(sv_2mortal(newRV( (SV*) href)));
686 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
690 temp = hv_fetch(href, "ERR_CODE", 8, 1);
691 err_code = newSVsv(*temp);
693 temp = hv_fetch(href, "ERR_STR", 7, 1);
694 err_str = newSVsv(*temp);
696 temp = hv_fetch(href, "STATUS", 6, 1);
697 status = newSVsv(*temp);
699 temp = hv_fetch(href, "HANDLE", 6, 1);
700 point = newSVsv(*temp);
706 sv_free( (SV*) aref);
707 sv_free( (SV*) href);
708 sv_free( (SV*) sort_seq);
710 rr->errcode = SvIV(err_code);
711 rr->sort_status = SvIV(status);
713 ptr = SvPV(err_str, len);
714 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
715 strcpy(ODR_err_str, ptr);
716 rr->errstring = ODR_err_str;
717 zhandle->handle = point;
731 int bend_search(void *handle, bend_search_rr *rr)
740 Zfront_handle *zhandle = (Zfront_handle *)handle;
749 basenames = rr->basenames;
750 for (i = 0; i < rr->num_bases; i++)
752 av_push(aref, newSVpv(*basenames++, 0));
754 #if ENABLE_STOP_SERVER
755 if (rr->num_bases == 1 && !strcmp(rr->basenames[0], "XXstop"))
757 zhandle->stop_flag = 1;
761 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
762 if (rr->srw_sortKeys && *rr->srw_sortKeys)
763 hv_store(href, "SRW_SORTKEYS", 12, newSVpv(rr->srw_sortKeys, 0), 0);
764 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
765 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
766 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
767 hv_store(href, "HITS", 4, newSViv(0), 0);
768 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
769 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
770 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
771 hv_store(href, "PID", 3, newSViv(getpid()), 0);
772 if ((rpnSV = zquery2perl(rr->query)) != 0) {
773 hv_store(href, "RPN", 3, rpnSV, 0);
775 query = zquery2pquery(rr->query);
778 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
780 else if (rr->query->which == Z_Query_type_104 &&
781 rr->query->u.type_104->which == Z_External_CQL) {
782 hv_store(href, "CQL", 3,
783 newSVpv(rr->query->u.type_104->u.cql, 0), 0);
792 XPUSHs(sv_2mortal(newRV( (SV*) href)));
796 handler_cv = simpleserver_sv2cv( search_ref );
797 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
801 temp = hv_fetch(href, "HITS", 4, 1);
802 rr->hits = SvIV(*temp);
804 temp = hv_fetch(href, "ERR_CODE", 8, 1);
805 rr->errcode = SvIV(*temp);
807 temp = hv_fetch(href, "ERR_STR", 7, 1);
808 rr->errstring = string_or_undef(temp, rr->stream);
810 temp = hv_fetch(href, "HANDLE", 6, 1);
811 point = newSVsv(*temp);
816 zhandle->handle = point;
817 sv_free( (SV*) aref);
818 sv_free( (SV*) href);
820 wrbuf_destroy(query);
828 /* ### I am not 100% about the memory management in this handler */
829 int bend_delete(void *handle, bend_delete_rr *rr)
831 Zfront_handle *zhandle = (Zfront_handle *)handle;
843 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
844 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
845 hv_store(href, "STATUS", 6, newSViv(0), 0);
848 XPUSHs(sv_2mortal(newRV( (SV*) href)));
851 handler_cv = simpleserver_sv2cv(delete_ref);
853 if (rr->function == 1) {
854 /* Delete all result sets in the session */
855 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
856 temp = hv_fetch(href, "STATUS", 6, 1);
857 rr->delete_status = SvIV(*temp);
859 rr->delete_status = 0;
861 * For some reason, deleting two or more result-sets in
862 * one operation goes horribly wrong, and ### I don't have
863 * time to debug it right now.
865 if (rr->num_setnames > 1) {
866 rr->delete_status = 3; /* "System problem at target" */
867 /* There's no way to sent delete-msg using the GFS */
871 for (i = 0; i < rr->num_setnames; i++) {
872 hv_store(href, "SETNAME", 7, newSVpv(rr->setnames[i], 0), 0);
873 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
874 temp = hv_fetch(href, "STATUS", 6, 1);
875 rr->statuses[i] = SvIV(*temp);
876 if (rr->statuses[i] != 0)
877 rr->delete_status = rr->statuses[i];
883 temp = hv_fetch(href, "HANDLE", 6, 1);
884 point = newSVsv(*temp);
888 zhandle->handle = point;
890 sv_free( (SV*) href);
900 int bend_fetch(void *handle, bend_fetch_rr *rr)
918 Zfront_handle *zhandle = (Zfront_handle *)handle;
921 Z_RecordComposition *composition;
922 Z_ElementSetNames *simple;
932 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
934 hv_store(href, "SCHEMA", 6, newSVpv(rr->schema, 0), 0);
936 hv_store(href, "SCHEMA", 6, newSVpv("", 0), 0);
938 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
939 if (rr->request_format != 0) {
940 oid_dotted = oid2dotted(rr->request_format);
942 /* Probably an SRU request: assume XML is required */
943 oid_dotted = wrbuf_alloc();
944 wrbuf_puts(oid_dotted, "1.2.840.10003.5.109.10");
946 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
947 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
948 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
949 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
950 hv_store(href, "LAST", 4, newSViv(0), 0);
951 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
952 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
953 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
954 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
955 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
956 hv_store(href, "PID", 3, newSViv(getpid()), 0);
959 composition = rr->comp;
960 if (composition->which == Z_RecordComp_simple)
962 simple = composition->u.simple;
963 if (simple->which == Z_ElementSetNames_generic)
965 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
970 rr->errstring = odr_strdup(rr->stream, "non-generic 'simple' composition");
974 else if (composition->which == Z_RecordComp_complex)
976 if (composition->u.complex->generic &&
978 composition->u.complex->generic &&
979 composition->u.complex->generic->elementSpec &&
980 composition->u.complex->generic->elementSpec->which ==
981 Z_ElementSpec_elementSetName)
983 complex = composition->u.complex;
984 hv_store(href, "COMP", 4,
985 newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
989 #if 0 /* For now ignore this error, which is ubiquitous in SRU */
991 rr->errstring = odr_strdup(rr->stream, "'complex' composition is not generic ESN");
999 rr->errstring = odr_strdup(rr->stream, "composition neither simple nor complex");
1006 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1010 handler_cv = simpleserver_sv2cv( fetch_ref );
1011 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1015 temp = hv_fetch(href, "BASENAME", 8, 1);
1016 basename = newSVsv(*temp);
1018 temp = hv_fetch(href, "RECORD", 6, 1);
1019 record = newSVsv(*temp);
1021 temp = hv_fetch(href, "LAST", 4, 1);
1022 last = newSVsv(*temp);
1024 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1025 err_code = newSVsv(*temp);
1027 temp = hv_fetch(href, "ERR_STR", 7, 1),
1028 err_string = newSVsv(*temp);
1030 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
1031 sur_flag = newSVsv(*temp);
1033 temp = hv_fetch(href, "REP_FORM", 8, 1);
1034 rep_form = newSVsv(*temp);
1036 temp = hv_fetch(href, "SCHEMA", 6, 1);
1038 schema = newSVsv(*temp);
1039 ptr = SvPV(schema, length);
1041 rr->schema = (char *)odr_malloc(rr->stream, length + 1);
1042 strcpy(rr->schema, ptr);
1046 temp = hv_fetch(href, "HANDLE", 6, 1);
1047 point = newSVsv(*temp);
1052 ptr = SvPV(basename, length);
1053 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
1054 strcpy(ODR_basename, ptr);
1055 rr->basename = ODR_basename;
1057 ptr = SvPV(rep_form, length);
1059 rr->output_format = yaz_string_to_oid_odr(yaz_oid_std(),
1060 CLASS_RECSYN, ptr, rr->stream);
1061 if (!rr->output_format)
1063 printf("Net::Z3950::SimpleServer: WARNING: Bad OID %s\n", ptr);
1065 odr_oiddup(rr->stream, yaz_oid_recsyn_sutrs);
1067 ptr = SvPV(record, length);
1068 /* Treat GRS-1 records separately */
1069 if (!oid_oidcmp(rr->output_format, yaz_oid_recsyn_grs_1))
1071 rr->record = (char *) read_grs1(ptr, rr->stream);
1076 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
1077 strcpy(ODR_record, ptr);
1078 rr->record = ODR_record;
1081 zhandle->handle = point;
1083 rr->last_in_set = SvIV(last);
1087 rr->errcode = SvIV(err_code);
1088 ptr = SvPV(err_string, length);
1089 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
1090 strcpy(ODR_errstr, ptr);
1091 rr->errstring = ODR_errstr;
1093 rr->surrogate_flag = SvIV(sur_flag);
1095 wrbuf_destroy(oid_dotted);
1096 sv_free((SV*) href);
1100 sv_free(err_string);
1116 int bend_present(void *handle, bend_present_rr *rr)
1125 Z_RecordComposition *composition;
1126 Z_ElementSetNames *simple;
1127 Z_CompSpec *complex;
1130 Zfront_handle *zhandle = (Zfront_handle *)handle;
1133 /* WRBUF oid_dotted; */
1140 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1141 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1142 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1143 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1144 hv_store(href, "START", 5, newSViv(rr->start), 0);
1145 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
1146 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
1147 /*oid_dotted = oid2dotted(rr->request_format_raw);
1148 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
1149 hv_store(href, "HITS", 4, newSViv(0), 0);
1150 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1153 composition = rr->comp;
1154 if (composition->which == Z_RecordComp_simple)
1156 simple = composition->u.simple;
1157 if (simple->which == Z_ElementSetNames_generic)
1159 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
1164 rr->errstring = odr_strdup(rr->stream, "non-generic 'simple' composition");
1168 else if (composition->which == Z_RecordComp_complex)
1170 if (composition->u.complex->generic &&
1172 composition->u.complex->generic &&
1173 composition->u.complex->generic->elementSpec &&
1174 composition->u.complex->generic->elementSpec->which ==
1175 Z_ElementSpec_elementSetName)
1177 complex = composition->u.complex;
1178 hv_store(href, "COMP", 4,
1179 newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
1184 rr->errstring = odr_strdup(rr->stream, "'complex' composition is not generic ESN");
1191 rr->errstring = odr_strdup(rr->stream, "composition neither simple nor complex");
1198 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1202 handler_cv = simpleserver_sv2cv( present_ref );
1203 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1207 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1208 err_code = newSVsv(*temp);
1210 temp = hv_fetch(href, "ERR_STR", 7, 1);
1211 err_string = newSVsv(*temp);
1213 temp = hv_fetch(href, "HITS", 4, 1);
1214 hits = newSVsv(*temp);
1216 temp = hv_fetch(href, "HANDLE", 6, 1);
1217 point = newSVsv(*temp);
1224 rr->errcode = SvIV(err_code);
1225 rr->hits = SvIV(hits);
1227 ptr = SvPV(err_string, len);
1228 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1229 strcpy(ODR_errstr, ptr);
1230 rr->errstring = ODR_errstr;
1231 /* wrbuf_free(oid_dotted, 1);*/
1232 zhandle->handle = point;
1235 sv_free(err_string);
1237 sv_free( (SV*) href);
1243 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1245 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1250 int bend_scan(void *handle, bend_scan_rr *rr)
1257 struct scan_entry *scan_list;
1258 struct scan_entry *buffer;
1259 int *step_size = rr->step_size;
1263 SV *err_code = sv_newmortal();
1264 SV *err_str = sv_newmortal();
1265 SV *point = sv_newmortal();
1266 SV *status = sv_newmortal();
1267 SV *number = sv_newmortal();
1273 Zfront_handle *zhandle = (Zfront_handle *)handle;
1283 /* RPN is better than TERM since it includes attributes */
1284 if ((rpnSV = apt2perl(rr->term)) != 0) {
1285 setMember(href, "RPN", rpnSV);
1288 if (rr->term->term->which == Z_Term_general)
1290 term_len = rr->term->term->u.general->len;
1291 hv_store(href, "TERM", 4, newSVpv((char*) rr->term->term->u.general->buf, term_len), 0);
1293 rr->errcode = 229; /* Unsupported term type */
1296 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1297 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1298 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1299 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1300 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1301 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1302 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1303 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1304 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1306 basenames = rr->basenames;
1307 for (i = 0; i < rr->num_bases; i++)
1309 av_push(aref, newSVpv(*basenames++, 0));
1311 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1315 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1319 handler_cv = simpleserver_sv2cv( scan_ref );
1320 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1324 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1325 err_code = newSVsv(*temp);
1327 temp = hv_fetch(href, "ERR_STR", 7, 1);
1328 err_str = newSVsv(*temp);
1330 temp = hv_fetch(href, "HANDLE", 6, 1);
1331 point = newSVsv(*temp);
1333 temp = hv_fetch(href, "STATUS", 6, 1);
1334 status = newSVsv(*temp);
1336 temp = hv_fetch(href, "NUMBER", 6, 1);
1337 number = newSVsv(*temp);
1339 temp = hv_fetch(href, "ENTRIES", 7, 1);
1340 entries_ref = newSVsv(*temp);
1346 ptr = SvPV(err_str, len);
1347 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1348 strcpy(ODR_errstr, ptr);
1349 rr->errstring = ODR_errstr;
1350 rr->errcode = SvIV(err_code);
1351 rr->num_entries = SvIV(number);
1352 rr->status = SvIV(status);
1353 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1355 entries = (AV *)SvRV(entries_ref);
1356 if (rr->errcode == 0) for (i = 0; i < rr->num_entries; i++)
1358 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1359 temp = hv_fetch(scan_item, "TERM", 4, 1);
1360 ptr = SvPV(*temp, len);
1361 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1362 strcpy(buffer->term, ptr);
1363 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1364 buffer->occurrences = SvIV(*temp);
1366 hv_undef(scan_item);
1368 rr->entries = scan_list;
1369 zhandle->handle = point;
1376 sv_free((SV *)href);
1378 sv_free((SV *)aref);
1380 sv_free((SV *)list);
1382 /*sv_free((SV *)entries);*/
1383 sv_free(entries_ref);
1388 int bend_explain(void *handle, bend_explain_rr *q)
1396 Zfront_handle *zhandle = (Zfront_handle *)handle;
1403 hv_store(href, "EXPLAIN", 7, newSVpv("", 0), 0);
1404 hv_store(href, "DATABASE", 8, newSVpv(q->database, 0), 0);
1405 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1406 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1409 XPUSHs(sv_2mortal(newRV((SV*) href)));
1412 handler_cv = simpleserver_sv2cv(explain_ref);
1413 perl_call_sv((SV*) handler_cv, G_SCALAR | G_DISCARD);
1417 temp = hv_fetch(href, "EXPLAIN", 7, 1);
1418 explainsv = newSVsv(*temp);
1424 explain = SvPV(explainsv, len);
1425 q->explain_buf = (char*) odr_malloc(q->stream, len + 1);
1426 strcpy(q->explain_buf, explain);
1433 * You'll laugh when I tell you this ... Astonishingly, it turns out
1434 * that ActivePerl (which is widely used on Windows) has, in the
1435 * header file Perl\lib\CORE\XSUB.h, the following heinous crime:
1436 * # define open PerlLIO_open
1437 * This of course screws up the use of the "open" member of the
1438 * Z_IdAuthentication structure below, so we have to undo this
1446 bend_initresult *bend_init(bend_initrequest *q)
1448 int dummy = simpleserver_clone();
1449 bend_initresult *r = (bend_initresult *)
1450 odr_malloc (q->stream, sizeof(*r));
1455 NMEM nmem = nmem_create();
1456 Zfront_handle *zhandle = (Zfront_handle *) nmem_malloc (nmem,
1465 zhandle->ghandle = _global_ghandle;
1466 zhandle->nmem = nmem;
1467 zhandle->stop_flag = 0;
1471 q->bend_sort = bend_sort;
1475 q->bend_search = bend_search;
1479 q->bend_present = bend_present;
1481 /*q->bend_esrequest = bend_esrequest;*/
1483 q->bend_delete = bend_delete;
1487 q->bend_fetch = bend_fetch;
1491 q->bend_scan = bend_scan;
1495 q->bend_explain = bend_explain;
1500 /* ### These should be given initial values from the client */
1501 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1502 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1503 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1505 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1506 hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1507 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1508 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1509 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1510 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1513 char *passwd = NULL;
1514 if (q->auth->which == Z_IdAuthentication_open) {
1516 user = nmem_strdup (odr_getmem (q->stream), q->auth->u.open);
1517 cp = strchr (user, '/');
1519 /* password after / given */
1523 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1524 user = q->auth->u.idPass->userId;
1525 passwd = q->auth->u.idPass->password;
1527 /* ### some code paths have user/password unassigned here */
1529 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1531 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1536 XPUSHs(sv_2mortal(newRV((SV*) href)));
1540 if (init_ref != NULL)
1542 handler_cv = simpleserver_sv2cv( init_ref );
1543 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1548 temp = hv_fetch(href, "IMP_ID", 6, 1);
1549 ptr = SvPV(*temp, len);
1550 q->implementation_id = nmem_strdup(nmem, ptr);
1552 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1553 ptr = SvPV(*temp, len);
1554 q->implementation_name = nmem_strdup(nmem, ptr);
1556 temp = hv_fetch(href, "IMP_VER", 7, 1);
1557 ptr = SvPV(*temp, len);
1558 q->implementation_version = nmem_strdup(nmem, ptr);
1560 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1561 r->errcode = SvIV(*temp);
1563 temp = hv_fetch(href, "ERR_STR", 7, 1);
1564 ptr = SvPV(*temp, len);
1565 r->errstring = (char *)odr_malloc(q->stream, len + 1);
1566 strcpy(r->errstring, ptr);
1568 temp = hv_fetch(href, "HANDLE", 6, 1);
1569 handle= newSVsv(*temp);
1570 zhandle->handle = handle;
1572 r->handle = zhandle;
1575 sv_free((SV*) href);
1584 void bend_close(void *handle)
1587 Zfront_handle *zhandle = (Zfront_handle *)handle;
1597 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1598 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1602 XPUSHs(sv_2mortal(newRV((SV *)href)));
1606 handler_cv = simpleserver_sv2cv( close_ref );
1607 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1611 sv_free((SV*) href);
1614 sv_free(zhandle->handle);
1618 stop_flag = zhandle->stop_flag;
1619 nmem_destroy(zhandle->nmem);
1620 simpleserver_free();
1628 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1637 _global_ghandle = newSVsv(arg);
1641 set_init_handler(arg)
1644 init_ref = newSVsv(arg);
1648 set_close_handler(arg)
1651 close_ref = newSVsv(arg);
1655 set_sort_handler(arg)
1658 sort_ref = newSVsv(arg);
1661 set_search_handler(arg)
1664 search_ref = newSVsv(arg);
1668 set_fetch_handler(arg)
1671 fetch_ref = newSVsv(arg);
1675 set_present_handler(arg)
1678 present_ref = newSVsv(arg);
1682 set_esrequest_handler(arg)
1685 esrequest_ref = newSVsv(arg);
1689 set_delete_handler(arg)
1692 delete_ref = newSVsv(arg);
1696 set_scan_handler(arg)
1699 scan_ref = newSVsv(arg);
1702 set_explain_handler(arg)
1705 explain_ref = newSVsv(arg);
1716 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1718 for (i = 0; i < items; i++)
1720 ptr = SvPV(ST(i), len);
1721 *argv_buf = (char *)xmalloc(len + 1);
1722 strcpy(*argv_buf++, ptr);
1725 root_perl_context = PERL_GET_CONTEXT;
1726 yaz_mutex_create(&simpleserver_mutex);
1728 /* only for debugging perl_clone .. */
1732 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1740 RETVAL = BEND_SCAN_SUCCESS;
1747 RETVAL = BEND_SCAN_PARTIAL;
1758 ptr = SvPV(arg, len);
1759 yaz_log(YLOG_LOG, "%.*s", len, ptr);
1762 yaz_diag_srw_to_bib1(srw_code)
1766 yaz_diag_bib1_to_srw(bib1_code)