2 * $Id: SimpleServer.xs,v 1.65 2007-08-10 00:00:14 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
35 #include <yaz/backend.h>
37 #include <yaz/wrbuf.h>
38 #include <yaz/querytowrbuf.h>
40 #include <yaz/mutex.h>
41 #include <yaz/oid_db.h>
48 #define GRS_MAX_FIELDS 500
52 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
53 #define sv_undef PL_sv_undef
56 YAZ_MUTEX simpleserver_mutex;
59 SV *ghandle; /* Global handle specified at creation */
60 SV *handle; /* Per-connection handle set at Init */
62 /* ### These callback-reference elements are never used! */
75 int stop_flag; /* is used to stop server prematurely .. */
78 #define ENABLE_STOP_SERVER 0
80 SV *_global_ghandle = NULL; /* To be copied into zhandle then ignored */
84 SV *search_ref = NULL;
86 SV *present_ref = NULL;
87 SV *esrequest_ref = NULL;
88 SV *delete_ref = NULL;
90 SV *explain_ref = NULL;
91 PerlInterpreter *root_perl_context;
93 #define GRS_BUF_SIZE 8192
97 * Inspects the SV indicated by svp, and returns a null pointer if
98 * it's an undefined value, or a string allocation from `stream'
99 * otherwise. Using this when filling in addinfo avoids those
100 * irritating "Use of uninitialized value in subroutine entry"
101 * warnings from Perl.
103 char *string_or_undef(SV **svp, ODR stream) {
110 ptr = SvPV(*svp, len);
111 buf = (char*) odr_malloc(stream, len+1);
117 CV * simpleserver_sv2cv(SV *handler) {
121 if (SvPOK(handler)) {
123 buf = SvPV( handler, len);
124 if ( !( ret = perl_get_cv(buf, FALSE ) ) ) {
125 fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf );
131 return (CV *) handler;
135 /* debugging routine to check for destruction of Perl interpreters */
137 void tst_clones(void)
140 PerlInterpreter *parent = PERL_GET_CONTEXT;
141 for (i = 0; i<5000; i++)
143 PerlInterpreter *perl_interp;
145 PERL_SET_CONTEXT(parent);
146 PL_perl_destruct_level = 2;
147 perl_interp = perl_clone(parent, CLONEf_CLONE_HOST);
148 PL_perl_destruct_level = 2;
149 PERL_SET_CONTEXT(perl_interp);
150 perl_destruct(perl_interp);
151 perl_free(perl_interp);
157 int simpleserver_clone(void) {
159 yaz_mutex_enter(simpleserver_mutex);
162 PerlInterpreter *current = PERL_GET_CONTEXT;
164 /* if current is unset, then we're in a new thread with
165 * no Perl interpreter for it. So we must create one .
166 * This will only happen when threaded is used..
169 PerlInterpreter *perl_interp;
170 PERL_SET_CONTEXT( root_perl_context );
171 perl_interp = perl_clone(root_perl_context, CLONEf_CLONE_HOST);
172 PERL_SET_CONTEXT( perl_interp );
175 yaz_mutex_leave(simpleserver_mutex);
181 void simpleserver_free(void) {
182 yaz_mutex_enter(simpleserver_mutex);
185 PerlInterpreter *current_interp = PERL_GET_CONTEXT;
187 /* If current Perl Interp is different from root interp, then
188 * we're in threaded mode and we must destroy..
190 if (current_interp != root_perl_context) {
191 PL_perl_destruct_level = 2;
192 PERL_SET_CONTEXT(current_interp);
193 perl_destruct(current_interp);
194 perl_free(current_interp);
197 yaz_mutex_leave(simpleserver_mutex);
201 Z_GenericRecord *read_grs1(char *str, ODR o)
204 char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
205 char value[GRS_BUF_SIZE+1];
206 Z_GenericRecord *r = 0;
209 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
210 r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
219 ptr = strchr(str, '\n');
224 if (len > GRS_BUF_SIZE) {
225 yaz_log(YLOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
228 strncpy(line, str, len);
232 while (*buf && isspace(*buf))
235 memmove(original, str, strlen(str));
238 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
240 yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
243 if (!type && *value == '0')
245 if (!(buf = strchr(buf, ')')))
248 while (*buf && isspace(*buf))
250 if (r->num_elements >= GRS_MAX_FIELDS)
252 yaz_log(YLOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
255 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
256 t->tagType = odr_intdup(o, type);
257 t->tagValue = (Z_StringOrNumeric *)
258 odr_malloc(o, sizeof(Z_StringOrNumeric));
259 if ((ivalue = atoi(value)))
261 t->tagValue->which = Z_StringOrNumeric_numeric;
262 t->tagValue->u.numeric = odr_intdup(o, ivalue);
266 t->tagValue->which = Z_StringOrNumeric_string;
267 t->tagValue->u.string = odr_strdup(o, value);
269 t->tagOccurrence = 0;
271 t->appliedVariant = 0;
272 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
275 c->which = Z_ElementData_subtree;
276 c->u.subtree = read_grs1(str, o);
280 c->which = Z_ElementData_string;
281 c->u.string = odr_strdup(o, buf);
289 static void oid2str(Odr_oid *o, WRBUF buf)
291 for (; *o >= 0; o++) {
293 sprintf(ibuf, "%d", *o);
294 wrbuf_puts(buf, ibuf);
296 wrbuf_putc(buf, '.');
300 WRBUF oid2dotted(Odr_oid *oid)
302 WRBUF buf = wrbuf_alloc();
308 WRBUF zquery2pquery(Z_Query *q)
310 WRBUF buf = wrbuf_alloc();
312 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
314 yaz_rpnquery_to_wrbuf(buf, q->u.type_1);
319 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
321 void fatal(char *fmt, ...)
325 fprintf(stderr, "FATAL (SimpleServer): ");
327 vfprintf(stderr, fmt, ap);
329 fprintf(stderr, "\n");
334 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
336 * Creates a new Perl object of type `class'; the newly-created scalar
337 * that is a reference to the blessed thingy `referent' is returned.
339 static SV *newObject(char *class, SV *referent)
344 sv = newRV_noinc((SV*) referent);
345 stash = gv_stashpv(class, 0);
347 fatal("attempt to create object of undefined class '%s'", class);
348 /*assert(stash != 0);*/
354 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
355 static void setMember(HV *hv, char *name, SV *val)
357 /* We don't increment `val's reference count -- I think this is
358 * right because it's created with a refcount of 1, and in fact
359 * the reference via this hash is the only reference to it in
362 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
363 fatal("couldn't store member in hash");
367 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
368 static SV *translateOID(Odr_oid *x)
370 /* Yaz represents an OID by an int array terminated by a negative
371 * value, typically -1; we represent it as a reference to a
372 * blessed scalar string of "."-separated elements.
378 for (i = 0; x[i] >= 0; i++) {
379 sprintf(buf + strlen(buf), "%d", (int) x[i]);
385 * ### We'd like to return a blessed scalar (string) here, but of
386 * course you can't do that in Perl: only references can be
387 * blessed, so we'd have to return a _reference_ to a string, and
388 * bless _that_. Better to do without the blessing, I think.
391 return newSVpv(buf, 0);
393 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
398 static SV *rpn2perl(Z_RPNStructure *s)
405 case Z_RPNStructure_simple: {
406 Z_Operand *o = s->u.simple;
407 Z_AttributesPlusTerm *at;
408 if (o->which == Z_Operand_resultSetId) {
410 /* This code causes a SIGBUS on my machine, and I have no
411 idea why. It seems as clear as day to me */
412 char *rsid = (char*) o->u.resultSetId;
413 printf("Encoding resultSetId '%s'\n", rsid);
414 sv = newObject("Net::Z3950::RPN::RSID", (SV*) (hv = newHV()));
415 printf("Made sv=0x%lx, hv=0x%lx\n",
416 (unsigned long) sv ,(unsigned long) hv);
417 sv2 = newSVpv(rsid, strlen(rsid));
418 setMember(hv, "id", sv2);
419 printf("Set hv{id} to 0x%lx\n", (unsigned long) sv2);
422 if (o->which != Z_Operand_APT)
423 fatal("can't handle RPN simples other than APT and RSID");
424 at = o->u.attributesPlusTerm;
425 if (at->term->which != Z_Term_general)
426 fatal("can't handle RPN terms other than general");
428 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
429 if (at->attributes) {
431 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
432 (SV*) (av = newAV()));
433 for (i = 0; i < at->attributes->num_attributes; i++) {
434 Z_AttributeElement *elem = at->attributes->attributes[i];
436 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
437 (SV*) (hv2 = newHV()));
438 if (elem->attributeSet)
439 setMember(hv2, "attributeSet",
440 translateOID(elem->attributeSet));
441 setMember(hv2, "attributeType",
442 newSViv(*elem->attributeType));
443 assert(elem->which == Z_AttributeValue_numeric);
444 setMember(hv2, "attributeValue",
445 newSViv(*elem->value.numeric));
448 setMember(hv, "attributes", attrs);
450 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
451 at->term->u.general->len));
454 case Z_RPNStructure_complex: {
456 Z_Complex *c = s->u.complex;
457 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
458 switch (c->roperator->which) {
459 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
460 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
461 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
462 case Z_Operator_prox: fatal("proximity not yet supported");
463 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
465 sv = newObject(type, (SV*) (av = newAV()));
466 if ((tmp = rpn2perl(c->s1)) == 0)
469 if ((tmp = rpn2perl(c->s2)) == 0)
474 default: fatal("unknown RPN node type %d", (int) s->which);
481 /* Decode the Z_SortAttributes struct and store the whole thing into the
484 int simpleserver_ExpandSortAttributes (HV *sort_spec, Z_SortAttributes *sattr)
486 WRBUF attrset_wr = wrbuf_alloc();
488 Z_AttributeList *attr_list = sattr->list;
491 oid2str(sattr->id, attrset_wr);
492 hv_store(sort_spec, "ATTRSET", 7,
493 newSVpv(attrset_wr->buf, attrset_wr->pos), 0);
494 wrbuf_destroy(attrset_wr);
496 hv_store(sort_spec, "SORT_ATTR", 9, newRV( sv_2mortal( (SV*) list ) ), 0);
498 for (i = 0; i < attr_list->num_attributes; i++)
500 Z_AttributeElement *attr = *attr_list->attributes++;
501 HV *attr_spec = newHV();
503 av_push(list, newRV( sv_2mortal( (SV*) attr_spec ) ));
504 hv_store(attr_spec, "ATTR_TYPE", 9, newSViv(*attr->attributeType), 0);
506 if (attr->which == Z_AttributeValue_numeric)
508 hv_store(attr_spec, "ATTR_VALUE", 10,
509 newSViv(*attr->value.numeric), 0);
519 /* Decode the Z_SortKeySpec struct and store the whole thing in a perl hash */
520 int simpleserver_SortKeySpecToHash (HV *sort_spec, Z_SortKeySpec *spec)
522 Z_SortElement *element = spec->sortElement;
524 hv_store(sort_spec, "RELATION", 8, newSViv(*spec->sortRelation), 0);
525 hv_store(sort_spec, "CASE", 4, newSViv(*spec->caseSensitivity), 0);
526 hv_store(sort_spec, "MISSING", 7, newSViv(spec->which), 0);
528 if (element->which == Z_SortElement_generic)
530 Z_SortKey *key = element->u.generic;
532 if (key->which == Z_SortKey_sortField)
534 hv_store(sort_spec, "SORTFIELD", 9,
535 newSVpv((char *) key->u.sortField, 0), 0);
537 else if (key->which == Z_SortKey_elementSpec)
539 Z_Specification *zspec = key->u.elementSpec;
541 hv_store(sort_spec, "ELEMENTSPEC_TYPE", 16,
542 newSViv(zspec->which), 0);
544 if (zspec->which == Z_Schema_oid)
546 WRBUF elementSpec = wrbuf_alloc();
548 oid2str(zspec->schema.oid, elementSpec);
549 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
550 newSVpv(elementSpec->buf, elementSpec->pos), 0);
551 wrbuf_destroy(elementSpec);
553 else if (zspec->which == Z_Schema_uri)
555 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
556 newSVpv((char *) zspec->schema.uri, 0), 0);
559 else if (key->which == Z_SortKey_sortAttributes)
561 return simpleserver_ExpandSortAttributes(sort_spec,
562 key->u.sortAttributes);
578 static SV *zquery2perl(Z_Query *q)
583 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
585 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
586 if (q->u.type_1->attributeSetId)
587 setMember(hv, "attributeSet",
588 translateOID(q->u.type_1->attributeSetId));
589 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
594 int bend_sort(void *handle, bend_sort_rr *rr)
607 char **input_setnames;
608 Zfront_handle *zhandle = (Zfront_handle *)handle;
609 Z_SortKeySpecList *sort_spec = rr->sort_sequence;
617 input_setnames = rr->input_setnames;
618 for (i = 0; i < rr->num_input_setnames; i++)
620 av_push(aref, newSVpv(*input_setnames++, 0));
624 for (i = 0; i < sort_spec->num_specs; i++)
626 Z_SortKeySpec *spec = *sort_spec->specs++;
627 HV *sort_spec = newHV();
629 if ( simpleserver_SortKeySpecToHash(sort_spec, spec) )
630 av_push(sort_seq, newRV( sv_2mortal( (SV*) sort_spec ) ));
639 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
640 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
641 hv_store(href, "SEQUENCE", 8, newRV( (SV*) sort_seq), 0);
642 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
643 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
644 hv_store(href, "STATUS", 6, newSViv(0), 0);
645 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
646 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
650 XPUSHs(sv_2mortal(newRV( (SV*) href)));
654 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
658 temp = hv_fetch(href, "ERR_CODE", 8, 1);
659 err_code = newSVsv(*temp);
661 temp = hv_fetch(href, "ERR_STR", 7, 1);
662 err_str = newSVsv(*temp);
664 temp = hv_fetch(href, "STATUS", 6, 1);
665 status = newSVsv(*temp);
667 temp = hv_fetch(href, "HANDLE", 6, 1);
668 point = newSVsv(*temp);
674 sv_free( (SV*) aref);
675 sv_free( (SV*) href);
676 sv_free( (SV*) sort_seq);
678 rr->errcode = SvIV(err_code);
679 rr->sort_status = SvIV(status);
681 ptr = SvPV(err_str, len);
682 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
683 strcpy(ODR_err_str, ptr);
684 rr->errstring = ODR_err_str;
685 zhandle->handle = point;
699 int bend_search(void *handle, bend_search_rr *rr)
708 Zfront_handle *zhandle = (Zfront_handle *)handle;
717 basenames = rr->basenames;
718 for (i = 0; i < rr->num_bases; i++)
720 av_push(aref, newSVpv(*basenames++, 0));
722 #if ENABLE_STOP_SERVER
723 if (rr->num_bases == 1 && !strcmp(rr->basenames[0], "XXstop"))
725 zhandle->stop_flag = 1;
729 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
730 if (rr->srw_sortKeys && *rr->srw_sortKeys)
731 hv_store(href, "SRW_SORTKEYS", 12, newSVpv(rr->srw_sortKeys, 0), 0);
732 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
733 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
734 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
735 hv_store(href, "HITS", 4, newSViv(0), 0);
736 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
737 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
738 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
739 hv_store(href, "PID", 3, newSViv(getpid()), 0);
740 if ((rpnSV = zquery2perl(rr->query)) != 0) {
741 hv_store(href, "RPN", 3, rpnSV, 0);
743 query = zquery2pquery(rr->query);
746 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
748 else if (rr->query->which == Z_Query_type_104 &&
749 rr->query->u.type_104->which == Z_External_CQL) {
750 hv_store(href, "CQL", 3,
751 newSVpv(rr->query->u.type_104->u.cql, 0), 0);
760 XPUSHs(sv_2mortal(newRV( (SV*) href)));
764 handler_cv = simpleserver_sv2cv( search_ref );
765 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
769 temp = hv_fetch(href, "HITS", 4, 1);
770 rr->hits = SvIV(*temp);
772 temp = hv_fetch(href, "ERR_CODE", 8, 1);
773 rr->errcode = SvIV(*temp);
775 temp = hv_fetch(href, "ERR_STR", 7, 1);
776 rr->errstring = string_or_undef(temp, rr->stream);
778 temp = hv_fetch(href, "HANDLE", 6, 1);
779 point = newSVsv(*temp);
784 zhandle->handle = point;
785 sv_free( (SV*) aref);
786 sv_free( (SV*) href);
788 wrbuf_destroy(query);
796 int bend_fetch(void *handle, bend_fetch_rr *rr)
814 Zfront_handle *zhandle = (Zfront_handle *)handle;
817 Z_RecordComposition *composition;
818 Z_ElementSetNames *simple;
828 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
830 hv_store(href, "SCHEMA", 6, newSVpv(rr->schema, 0), 0);
832 hv_store(href, "SCHEMA", 6, newSVpv("", 0), 0);
834 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
835 if (rr->request_format != 0) {
836 oid_dotted = oid2dotted(rr->request_format);
838 /* Probably an SRU request: assume XML is required */
839 oid_dotted = wrbuf_alloc();
840 wrbuf_puts(oid_dotted, "1.2.840.10003.5.109.10");
842 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
843 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
844 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
845 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
846 hv_store(href, "LAST", 4, newSViv(0), 0);
847 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
848 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
849 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
850 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
851 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
852 hv_store(href, "PID", 3, newSViv(getpid()), 0);
855 composition = rr->comp;
856 if (composition->which == Z_RecordComp_simple)
858 simple = composition->u.simple;
859 if (simple->which == Z_ElementSetNames_generic)
861 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
868 else if (composition->which == Z_RecordComp_complex)
870 if (composition->u.complex->generic &&
872 composition->u.complex->generic &&
873 composition->u.complex->generic->elementSpec &&
874 composition->u.complex->generic->elementSpec->which ==
875 Z_ElementSpec_elementSetName)
877 complex = composition->u.complex;
878 hv_store(href, "COMP", 4,
879 newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
883 #if 0 /* For now ignore this error, which is ubiquitous in SRU */
884 fprintf(stderr, "complex is weird\n");
899 XPUSHs(sv_2mortal(newRV( (SV*) href)));
903 handler_cv = simpleserver_sv2cv( fetch_ref );
904 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
908 temp = hv_fetch(href, "BASENAME", 8, 1);
909 basename = newSVsv(*temp);
911 temp = hv_fetch(href, "RECORD", 6, 1);
912 record = newSVsv(*temp);
914 temp = hv_fetch(href, "LAST", 4, 1);
915 last = newSVsv(*temp);
917 temp = hv_fetch(href, "ERR_CODE", 8, 1);
918 err_code = newSVsv(*temp);
920 temp = hv_fetch(href, "ERR_STR", 7, 1),
921 err_string = newSVsv(*temp);
923 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
924 sur_flag = newSVsv(*temp);
926 temp = hv_fetch(href, "REP_FORM", 8, 1);
927 rep_form = newSVsv(*temp);
929 temp = hv_fetch(href, "SCHEMA", 6, 1);
931 schema = newSVsv(*temp);
932 ptr = SvPV(schema, length);
934 rr->schema = (char *)odr_malloc(rr->stream, length + 1);
935 strcpy(rr->schema, ptr);
939 temp = hv_fetch(href, "HANDLE", 6, 1);
940 point = newSVsv(*temp);
945 ptr = SvPV(basename, length);
946 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
947 strcpy(ODR_basename, ptr);
948 rr->basename = ODR_basename;
950 ptr = SvPV(rep_form, length);
952 rr->output_format = yaz_string_to_oid_odr(yaz_oid_std(),
953 CLASS_RECSYN, ptr, rr->stream);
954 if (!rr->output_format)
956 printf("Net::Z3950::SimpleServer: WARNING: Bad OID %s\n", ptr);
958 odr_oiddup(rr->stream, yaz_oid_recsyn_sutrs);
960 ptr = SvPV(record, length);
961 /* Treat GRS-1 records separately */
962 if (!oid_oidcmp(rr->output_format, yaz_oid_recsyn_grs_1))
964 rr->record = (char *) read_grs1(ptr, rr->stream);
969 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
970 strcpy(ODR_record, ptr);
971 rr->record = ODR_record;
974 zhandle->handle = point;
976 rr->last_in_set = SvIV(last);
980 rr->errcode = SvIV(err_code);
981 ptr = SvPV(err_string, length);
982 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
983 strcpy(ODR_errstr, ptr);
984 rr->errstring = ODR_errstr;
986 rr->surrogate_flag = SvIV(sur_flag);
988 wrbuf_destroy(oid_dotted);
1009 int bend_present(void *handle, bend_present_rr *rr)
1018 Z_RecordComposition *composition;
1019 Z_ElementSetNames *simple;
1020 Z_CompSpec *complex;
1023 Zfront_handle *zhandle = (Zfront_handle *)handle;
1026 /* WRBUF oid_dotted; */
1033 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1034 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1035 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1036 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1037 hv_store(href, "START", 5, newSViv(rr->start), 0);
1038 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
1039 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
1040 /*oid_dotted = oid2dotted(rr->request_format_raw);
1041 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
1042 hv_store(href, "HITS", 4, newSViv(0), 0);
1043 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1046 composition = rr->comp;
1047 if (composition->which == Z_RecordComp_simple)
1049 simple = composition->u.simple;
1050 if (simple->which == Z_ElementSetNames_generic)
1052 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
1060 else if (composition->which == Z_RecordComp_complex)
1062 if (composition->u.complex->generic &&
1064 composition->u.complex->generic &&
1065 composition->u.complex->generic->elementSpec &&
1066 composition->u.complex->generic->elementSpec->which ==
1067 Z_ElementSpec_elementSetName)
1069 complex = composition->u.complex;
1070 hv_store(href, "COMP", 4,
1071 newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
1088 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1092 handler_cv = simpleserver_sv2cv( present_ref );
1093 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1097 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1098 err_code = newSVsv(*temp);
1100 temp = hv_fetch(href, "ERR_STR", 7, 1);
1101 err_string = newSVsv(*temp);
1103 temp = hv_fetch(href, "HITS", 4, 1);
1104 hits = newSVsv(*temp);
1106 temp = hv_fetch(href, "HANDLE", 6, 1);
1107 point = newSVsv(*temp);
1114 rr->errcode = SvIV(err_code);
1115 rr->hits = SvIV(hits);
1117 ptr = SvPV(err_string, len);
1118 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1119 strcpy(ODR_errstr, ptr);
1120 rr->errstring = ODR_errstr;
1121 /* wrbuf_free(oid_dotted, 1);*/
1122 zhandle->handle = point;
1125 sv_free(err_string);
1127 sv_free( (SV*) href);
1133 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1135 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1140 int bend_delete(void *handle, bend_delete_rr *rr)
1142 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
1147 int bend_scan(void *handle, bend_scan_rr *rr)
1154 struct scan_entry *scan_list;
1155 struct scan_entry *buffer;
1156 int *step_size = rr->step_size;
1160 SV *err_code = sv_newmortal();
1161 SV *err_str = sv_newmortal();
1162 SV *point = sv_newmortal();
1163 SV *status = sv_newmortal();
1164 SV *number = sv_newmortal();
1170 Zfront_handle *zhandle = (Zfront_handle *)handle;
1178 if (rr->term->term->which == Z_Term_general)
1180 term_len = rr->term->term->u.general->len;
1181 hv_store(href, "TERM", 4, newSVpv((char*) rr->term->term->u.general->buf, term_len), 0);
1183 rr->errcode = 229; /* Unsupported term type */
1186 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1187 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1188 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1189 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1190 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1191 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1192 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1193 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1194 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1196 basenames = rr->basenames;
1197 for (i = 0; i < rr->num_bases; i++)
1199 av_push(aref, newSVpv(*basenames++, 0));
1201 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1205 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1209 handler_cv = simpleserver_sv2cv( scan_ref );
1210 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1214 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1215 err_code = newSVsv(*temp);
1217 temp = hv_fetch(href, "ERR_STR", 7, 1);
1218 err_str = newSVsv(*temp);
1220 temp = hv_fetch(href, "HANDLE", 6, 1);
1221 point = newSVsv(*temp);
1223 temp = hv_fetch(href, "STATUS", 6, 1);
1224 status = newSVsv(*temp);
1226 temp = hv_fetch(href, "NUMBER", 6, 1);
1227 number = newSVsv(*temp);
1229 temp = hv_fetch(href, "ENTRIES", 7, 1);
1230 entries_ref = newSVsv(*temp);
1236 ptr = SvPV(err_str, len);
1237 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1238 strcpy(ODR_errstr, ptr);
1239 rr->errstring = ODR_errstr;
1240 rr->errcode = SvIV(err_code);
1241 rr->num_entries = SvIV(number);
1242 rr->status = SvIV(status);
1243 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1245 entries = (AV *)SvRV(entries_ref);
1246 for (i = 0; i < rr->num_entries; i++)
1248 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1249 temp = hv_fetch(scan_item, "TERM", 4, 1);
1250 ptr = SvPV(*temp, len);
1251 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1252 strcpy(buffer->term, ptr);
1253 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1254 buffer->occurrences = SvIV(*temp);
1256 hv_undef(scan_item);
1258 rr->entries = scan_list;
1259 zhandle->handle = point;
1266 sv_free((SV *)href);
1268 sv_free((SV *)aref);
1270 sv_free((SV *)list);
1272 /*sv_free((SV *)entries);*/
1273 sv_free(entries_ref);
1278 int bend_explain(void *handle, bend_explain_rr *q)
1286 Zfront_handle *zhandle = (Zfront_handle *)handle;
1293 hv_store(href, "EXPLAIN", 7, newSVpv("", 0), 0);
1294 hv_store(href, "DATABASE", 8, newSVpv(q->database, 0), 0);
1295 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1296 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1299 XPUSHs(sv_2mortal(newRV((SV*) href)));
1302 handler_cv = simpleserver_sv2cv(explain_ref);
1303 perl_call_sv((SV*) handler_cv, G_SCALAR | G_DISCARD);
1307 temp = hv_fetch(href, "EXPLAIN", 7, 1);
1308 explainsv = newSVsv(*temp);
1314 explain = SvPV(explainsv, len);
1315 q->explain_buf = (char*) odr_malloc(q->stream, len + 1);
1316 strcpy(q->explain_buf, explain);
1321 bend_initresult *bend_init(bend_initrequest *q)
1323 int dummy = simpleserver_clone();
1324 bend_initresult *r = (bend_initresult *)
1325 odr_malloc (q->stream, sizeof(*r));
1330 NMEM nmem = nmem_create();
1331 Zfront_handle *zhandle = (Zfront_handle *) nmem_malloc (nmem,
1340 zhandle->ghandle = _global_ghandle;
1341 zhandle->nmem = nmem;
1342 zhandle->stop_flag = 0;
1346 q->bend_sort = bend_sort;
1350 q->bend_search = bend_search;
1354 q->bend_present = bend_present;
1356 /*q->bend_esrequest = bend_esrequest;*/
1357 /*q->bend_delete = bend_delete;*/
1360 q->bend_fetch = bend_fetch;
1364 q->bend_scan = bend_scan;
1368 q->bend_explain = bend_explain;
1372 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1373 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1374 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1375 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1376 hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1377 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1378 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1379 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1380 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1383 char *passwd = NULL;
1384 if (q->auth->which == Z_IdAuthentication_open) {
1386 user = nmem_strdup (odr_getmem (q->stream), q->auth->u.open);
1387 cp = strchr (user, '/');
1389 /* password after / given */
1393 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1394 user = q->auth->u.idPass->userId;
1395 passwd = q->auth->u.idPass->password;
1397 /* ### some code paths have user/password unassigned here */
1399 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1401 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1406 XPUSHs(sv_2mortal(newRV((SV*) href)));
1410 if (init_ref != NULL)
1412 handler_cv = simpleserver_sv2cv( init_ref );
1413 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1418 temp = hv_fetch(href, "IMP_ID", 6, 1);
1419 ptr = SvPV(*temp, len);
1420 q->implementation_id = nmem_strdup(nmem, ptr);
1422 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1423 ptr = SvPV(*temp, len);
1424 q->implementation_name = nmem_strdup(nmem, ptr);
1426 temp = hv_fetch(href, "IMP_VER", 7, 1);
1427 ptr = SvPV(*temp, len);
1428 q->implementation_version = nmem_strdup(nmem, ptr);
1430 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1431 r->errcode = SvIV(*temp);
1433 temp = hv_fetch(href, "ERR_STR", 7, 1);
1434 ptr = SvPV(*temp, len);
1435 r->errstring = (char *)odr_malloc(q->stream, len + 1);
1436 strcpy(r->errstring, ptr);
1438 temp = hv_fetch(href, "HANDLE", 6, 1);
1439 handle= newSVsv(*temp);
1440 zhandle->handle = handle;
1442 r->handle = zhandle;
1445 sv_free((SV*) href);
1454 void bend_close(void *handle)
1457 Zfront_handle *zhandle = (Zfront_handle *)handle;
1467 hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0);
1468 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1472 XPUSHs(sv_2mortal(newRV((SV *)href)));
1476 handler_cv = simpleserver_sv2cv( close_ref );
1477 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1481 sv_free((SV*) href);
1484 sv_free(zhandle->handle);
1488 stop_flag = zhandle->stop_flag;
1489 nmem_destroy(zhandle->nmem);
1490 simpleserver_free();
1498 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1507 _global_ghandle = newSVsv(arg);
1511 set_init_handler(arg)
1514 init_ref = newSVsv(arg);
1518 set_close_handler(arg)
1521 close_ref = newSVsv(arg);
1525 set_sort_handler(arg)
1528 sort_ref = newSVsv(arg);
1531 set_search_handler(arg)
1534 search_ref = newSVsv(arg);
1538 set_fetch_handler(arg)
1541 fetch_ref = newSVsv(arg);
1545 set_present_handler(arg)
1548 present_ref = newSVsv(arg);
1552 set_esrequest_handler(arg)
1555 esrequest_ref = newSVsv(arg);
1559 set_delete_handler(arg)
1562 delete_ref = newSVsv(arg);
1566 set_scan_handler(arg)
1569 scan_ref = newSVsv(arg);
1572 set_explain_handler(arg)
1575 explain_ref = newSVsv(arg);
1586 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1588 for (i = 0; i < items; i++)
1590 ptr = SvPV(ST(i), len);
1591 *argv_buf = (char *)xmalloc(len + 1);
1592 strcpy(*argv_buf++, ptr);
1595 root_perl_context = PERL_GET_CONTEXT;
1596 yaz_mutex_create(&simpleserver_mutex);
1598 /* only for debugging perl_clone .. */
1602 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1610 RETVAL = BEND_SCAN_SUCCESS;
1617 RETVAL = BEND_SCAN_PARTIAL;
1628 ptr = SvPV(arg, len);
1629 yaz_log(YLOG_LOG, "%.*s", len, ptr);