2 * $Id: SimpleServer.xs,v 1.59 2007-04-17 08:06:47 adam Exp $
3 * ----------------------------------------------------------------------
5 * Copyright (c) 2000-2004, Index Data.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation, in whole or in part, for any purpose, is hereby granted,
11 * 1. This copyright and permission notice appear in all copies of the
12 * software and its documentation. Notices of copyright or attribution
13 * which appear at the beginning of any file must remain unchanged.
15 * 2. The name of Index Data or the individual authors may not be used to
16 * endorse or promote products derived from this software without specific
17 * prior written permission.
19 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
21 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
22 * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
23 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
24 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
25 * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
26 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
35 #include <yaz/backend.h>
37 #include <yaz/wrbuf.h>
38 #include <yaz/querytowrbuf.h>
46 #define GRS_MAX_FIELDS 500
50 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
51 #define sv_undef PL_sv_undef
54 NMEM_MUTEX simpleserver_mutex;
70 int stop_flag; /* is used to stop server prematurely .. */
73 #define ENABLE_STOP_SERVER 0
78 SV *search_ref = NULL;
80 SV *present_ref = NULL;
81 SV *esrequest_ref = NULL;
82 SV *delete_ref = NULL;
84 SV *explain_ref = NULL;
85 PerlInterpreter *root_perl_context;
88 #define GRS_BUF_SIZE 8192
90 CV * simpleserver_sv2cv(SV *handler) {
96 buf = SvPV( handler, len);
97 if ( !( ret = perl_get_cv(buf, FALSE ) ) ) {
98 fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf );
104 return (CV *) handler;
108 /* debugging routine to check for destruction of Perl interpreters */
110 void tst_clones(void)
113 PerlInterpreter *parent = PERL_GET_CONTEXT;
114 for (i = 0; i<5000; i++)
116 PerlInterpreter *perl_interp;
118 PERL_SET_CONTEXT(parent);
119 PL_perl_destruct_level = 2;
120 perl_interp = perl_clone(parent, CLONEf_CLONE_HOST);
121 PL_perl_destruct_level = 2;
122 PERL_SET_CONTEXT(perl_interp);
123 perl_destruct(perl_interp);
124 perl_free(perl_interp);
130 int simpleserver_clone(void) {
132 nmem_mutex_enter(simpleserver_mutex);
135 PerlInterpreter *current = PERL_GET_CONTEXT;
137 /* if current is unset, then we're in a new thread with
138 * no Perl interpreter for it. So we must create one .
139 * This will only happen when threaded is used..
142 PerlInterpreter *perl_interp;
143 PERL_SET_CONTEXT( root_perl_context );
144 perl_interp = perl_clone(root_perl_context, CLONEf_CLONE_HOST);
145 PERL_SET_CONTEXT( perl_interp );
148 nmem_mutex_leave(simpleserver_mutex);
154 void simpleserver_free(void) {
155 nmem_mutex_enter(simpleserver_mutex);
158 PerlInterpreter *current_interp = PERL_GET_CONTEXT;
160 /* If current Perl Interp is different from root interp, then
161 * we're in threaded mode and we must destroy..
163 if (current_interp != root_perl_context) {
164 PL_perl_destruct_level = 2;
165 PERL_SET_CONTEXT(current_interp);
166 perl_destruct(current_interp);
167 perl_free(current_interp);
170 nmem_mutex_leave(simpleserver_mutex);
174 Z_GenericRecord *read_grs1(char *str, ODR o)
177 char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
178 char value[GRS_BUF_SIZE+1];
179 Z_GenericRecord *r = 0;
182 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
183 r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
192 ptr = strchr(str, '\n');
197 if (len > GRS_BUF_SIZE) {
198 yaz_log(YLOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
201 strncpy(line, str, len);
205 while (*buf && isspace(*buf))
208 memmove(original, str, strlen(str));
211 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
213 yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
216 if (!type && *value == '0')
218 if (!(buf = strchr(buf, ')')))
221 while (*buf && isspace(*buf))
223 if (r->num_elements >= GRS_MAX_FIELDS)
225 yaz_log(YLOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
228 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
229 t->tagType = (int *)odr_malloc(o, sizeof(int));
231 t->tagValue = (Z_StringOrNumeric *)
232 odr_malloc(o, sizeof(Z_StringOrNumeric));
233 if ((ivalue = atoi(value)))
235 t->tagValue->which = Z_StringOrNumeric_numeric;
236 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
237 *t->tagValue->u.numeric = ivalue;
241 t->tagValue->which = Z_StringOrNumeric_string;
242 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
243 strcpy(t->tagValue->u.string, value);
245 t->tagOccurrence = 0;
247 t->appliedVariant = 0;
248 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
251 c->which = Z_ElementData_subtree;
252 c->u.subtree = read_grs1(str, o);
256 c->which = Z_ElementData_string;
257 c->u.string = odr_strdup(o, buf);
265 static void oid2str(Odr_oid *o, WRBUF buf)
267 for (; *o >= 0; o++) {
269 sprintf(ibuf, "%d", *o);
270 wrbuf_puts(buf, ibuf);
272 wrbuf_putc(buf, '.');
276 WRBUF oid2dotted(int *oid)
278 WRBUF buf = wrbuf_alloc();
284 WRBUF zquery2pquery(Z_Query *q)
286 WRBUF buf = wrbuf_alloc();
288 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
290 yaz_rpnquery_to_wrbuf(buf, q->u.type_1);
295 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
297 void fatal(char *fmt, ...)
301 fprintf(stderr, "FATAL (SimpleServer): ");
303 vfprintf(stderr, fmt, ap);
305 fprintf(stderr, "\n");
310 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
312 * Creates a new Perl object of type `class'; the newly-created scalar
313 * that is a reference to the blessed thingy `referent' is returned.
315 static SV *newObject(char *class, SV *referent)
320 sv = newRV_noinc((SV*) referent);
321 stash = gv_stashpv(class, 0);
323 fatal("attempt to create object of undefined class '%s'", class);
324 /*assert(stash != 0);*/
330 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
331 static void setMember(HV *hv, char *name, SV *val)
333 /* We don't increment `val's reference count -- I think this is
334 * right because it's created with a refcount of 1, and in fact
335 * the reference via this hash is the only reference to it in
338 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
339 fatal("couldn't store member in hash");
343 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
344 static SV *translateOID(Odr_oid *x)
346 /* Yaz represents an OID by an int array terminated by a negative
347 * value, typically -1; we represent it as a reference to a
348 * blessed scalar string of "."-separated elements.
354 for (i = 0; x[i] >= 0; i++) {
355 sprintf(buf + strlen(buf), "%d", (int) x[i]);
361 * ### We'd like to return a blessed scalar (string) here, but of
362 * course you can't do that in Perl: only references can be
363 * blessed, so we'd have to return a _reference_ to a string, and
364 * bless _that_. Better to do without the blessing, I think.
367 return newSVpv(buf, 0);
369 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
374 static SV *rpn2perl(Z_RPNStructure *s)
381 case Z_RPNStructure_simple: {
382 Z_Operand *o = s->u.simple;
383 Z_AttributesPlusTerm *at;
384 if (o->which == Z_Operand_resultSetId) {
386 /* This code causes a SIGBUS on my machine, and I have no
387 idea why. It seems as clear as day to me */
388 char *rsid = (char*) o->u.resultSetId;
389 printf("Encoding resultSetId '%s'\n", rsid);
390 sv = newObject("Net::Z3950::RPN::RSID", (SV*) (hv = newHV()));
391 printf("Made sv=0x%lx, hv=0x%lx\n",
392 (unsigned long) sv ,(unsigned long) hv);
393 sv2 = newSVpv(rsid, strlen(rsid));
394 setMember(hv, "id", sv2);
395 printf("Set hv{id} to 0x%lx\n", (unsigned long) sv2);
398 if (o->which != Z_Operand_APT)
399 fatal("can't handle RPN simples other than APT and RSID");
400 at = o->u.attributesPlusTerm;
401 if (at->term->which != Z_Term_general)
402 fatal("can't handle RPN terms other than general");
404 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
405 if (at->attributes) {
407 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
408 (SV*) (av = newAV()));
409 for (i = 0; i < at->attributes->num_attributes; i++) {
410 Z_AttributeElement *elem = at->attributes->attributes[i];
412 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
413 (SV*) (hv2 = newHV()));
414 if (elem->attributeSet)
415 setMember(hv2, "attributeSet",
416 translateOID(elem->attributeSet));
417 setMember(hv2, "attributeType",
418 newSViv(*elem->attributeType));
419 assert(elem->which == Z_AttributeValue_numeric);
420 setMember(hv2, "attributeValue",
421 newSViv(*elem->value.numeric));
424 setMember(hv, "attributes", attrs);
426 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
427 at->term->u.general->len));
430 case Z_RPNStructure_complex: {
432 Z_Complex *c = s->u.complex;
433 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
434 switch (c->roperator->which) {
435 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
436 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
437 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
438 case Z_Operator_prox: fatal("proximity not yet supported");
439 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
441 sv = newObject(type, (SV*) (av = newAV()));
442 if ((tmp = rpn2perl(c->s1)) == 0)
445 if ((tmp = rpn2perl(c->s2)) == 0)
450 default: fatal("unknown RPN node type %d", (int) s->which);
457 /* Decode the Z_SortAttributes struct and store the whole thing into the
460 int simpleserver_ExpandSortAttributes (HV *sort_spec, Z_SortAttributes *sattr)
462 WRBUF attrset_wr = wrbuf_alloc();
464 Z_AttributeList *attr_list = sattr->list;
467 oid2str(sattr->id, attrset_wr);
468 hv_store(sort_spec, "ATTRSET", 7,
469 newSVpv(attrset_wr->buf, attrset_wr->pos), 0);
470 wrbuf_destroy(attrset_wr);
472 hv_store(sort_spec, "SORT_ATTR", 9, newRV( sv_2mortal( (SV*) list ) ), 0);
474 for (i = 0; i < attr_list->num_attributes; i++)
476 Z_AttributeElement *attr = *attr_list->attributes++;
477 HV *attr_spec = newHV();
479 av_push(list, newRV( sv_2mortal( (SV*) attr_spec ) ));
480 hv_store(attr_spec, "ATTR_TYPE", 9, newSViv(*attr->attributeType), 0);
482 if (attr->which == Z_AttributeValue_numeric)
484 hv_store(attr_spec, "ATTR_VALUE", 10,
485 newSViv(*attr->value.numeric), 0);
495 /* Decode the Z_SortKeySpec struct and store the whole thing in a perl hash */
496 int simpleserver_SortKeySpecToHash (HV *sort_spec, Z_SortKeySpec *spec)
498 Z_SortElement *element = spec->sortElement;
500 hv_store(sort_spec, "RELATION", 8, newSViv(*spec->sortRelation), 0);
501 hv_store(sort_spec, "CASE", 4, newSViv(*spec->caseSensitivity), 0);
502 hv_store(sort_spec, "MISSING", 7, newSViv(spec->which), 0);
504 if (element->which == Z_SortElement_generic)
506 Z_SortKey *key = element->u.generic;
508 if (key->which == Z_SortKey_sortField)
510 hv_store(sort_spec, "SORTFIELD", 9,
511 newSVpv((char *) key->u.sortField, 0), 0);
513 else if (key->which == Z_SortKey_elementSpec)
515 Z_Specification *zspec = key->u.elementSpec;
517 hv_store(sort_spec, "ELEMENTSPEC_TYPE", 16,
518 newSViv(zspec->which), 0);
520 if (zspec->which == Z_Schema_oid)
522 WRBUF elementSpec = wrbuf_alloc();
524 oid2str(zspec->schema.oid, elementSpec);
525 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
526 newSVpv(elementSpec->buf, elementSpec->pos), 0);
527 wrbuf_destroy(elementSpec);
529 else if (zspec->which == Z_Schema_uri)
531 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
532 newSVpv((char *) zspec->schema.uri, 0), 0);
535 else if (key->which == Z_SortKey_sortAttributes)
537 return simpleserver_ExpandSortAttributes(sort_spec,
538 key->u.sortAttributes);
554 static SV *zquery2perl(Z_Query *q)
559 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
561 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
562 if (q->u.type_1->attributeSetId)
563 setMember(hv, "attributeSet",
564 translateOID(q->u.type_1->attributeSetId));
565 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
570 int bend_sort(void *handle, bend_sort_rr *rr)
583 char **input_setnames;
584 Zfront_handle *zhandle = (Zfront_handle *)handle;
585 Z_SortKeySpecList *sort_spec = rr->sort_sequence;
593 input_setnames = rr->input_setnames;
594 for (i = 0; i < rr->num_input_setnames; i++)
596 av_push(aref, newSVpv(*input_setnames++, 0));
600 for (i = 0; i < sort_spec->num_specs; i++)
602 Z_SortKeySpec *spec = *sort_spec->specs++;
603 HV *sort_spec = newHV();
605 if ( simpleserver_SortKeySpecToHash(sort_spec, spec) )
606 av_push(sort_seq, newRV( sv_2mortal( (SV*) sort_spec ) ));
615 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
616 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
617 hv_store(href, "SEQUENCE", 8, newRV( (SV*) sort_seq), 0);
618 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
619 hv_store(href, "STATUS", 6, newSViv(0), 0);
620 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
621 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
625 XPUSHs(sv_2mortal(newRV( (SV*) href)));
629 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
633 temp = hv_fetch(href, "ERR_CODE", 8, 1);
634 err_code = newSVsv(*temp);
636 temp = hv_fetch(href, "ERR_STR", 7, 1);
637 err_str = newSVsv(*temp);
639 temp = hv_fetch(href, "STATUS", 6, 1);
640 status = newSVsv(*temp);
642 temp = hv_fetch(href, "HANDLE", 6, 1);
643 point = newSVsv(*temp);
649 sv_free( (SV*) aref);
650 sv_free( (SV*) href);
651 sv_free( (SV*) sort_seq);
653 rr->errcode = SvIV(err_code);
654 rr->sort_status = SvIV(status);
656 ptr = SvPV(err_str, len);
657 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
658 strcpy(ODR_err_str, ptr);
659 rr->errstring = ODR_err_str;
660 zhandle->handle = point;
674 int bend_search(void *handle, bend_search_rr *rr)
686 Zfront_handle *zhandle = (Zfront_handle *)handle;
695 basenames = rr->basenames;
696 for (i = 0; i < rr->num_bases; i++)
698 av_push(aref, newSVpv(*basenames++, 0));
700 #if ENABLE_STOP_SERVER
701 if (rr->num_bases == 1 && !strcmp(rr->basenames[0], "XXstop"))
703 zhandle->stop_flag = 1;
707 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
708 if (rr->srw_sortKeys && *rr->srw_sortKeys)
709 hv_store(href, "SRW_SORTKEYS", 12, newSVpv(rr->srw_sortKeys, 0), 0);
710 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
711 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
712 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
713 hv_store(href, "HITS", 4, newSViv(0), 0);
714 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
715 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
716 hv_store(href, "PID", 3, newSViv(getpid()), 0);
717 if ((rpnSV = zquery2perl(rr->query)) != 0) {
718 hv_store(href, "RPN", 3, rpnSV, 0);
720 query = zquery2pquery(rr->query);
723 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
725 else if (rr->query->which == Z_Query_type_104 &&
726 rr->query->u.type_104->which == Z_External_CQL) {
727 hv_store(href, "CQL", 3,
728 newSVpv(rr->query->u.type_104->u.cql, 0), 0);
737 XPUSHs(sv_2mortal(newRV( (SV*) href)));
741 handler_cv = simpleserver_sv2cv( search_ref );
742 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
746 temp = hv_fetch(href, "HITS", 4, 1);
747 rr->hits = SvIV(*temp);
749 temp = hv_fetch(href, "ERR_CODE", 8, 1);
750 rr->errcode = SvIV(*temp);
752 temp = hv_fetch(href, "ERR_STR", 7, 1);
753 ptr = SvPV(*temp, len);
754 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
755 strcpy(ODR_errstr, ptr);
756 rr->errstring = ODR_errstr;
758 temp = hv_fetch(href, "HANDLE", 6, 1);
759 point = newSVsv(*temp);
764 zhandle->handle = point;
765 sv_free( (SV*) aref);
766 sv_free( (SV*) href);
768 wrbuf_destroy(query);
776 int bend_fetch(void *handle, bend_fetch_rr *rr)
795 Zfront_handle *zhandle = (Zfront_handle *)handle;
798 Z_RecordComposition *composition;
799 Z_ElementSetNames *simple;
809 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
811 hv_store(href, "SCHEMA", 6, newSVpv(rr->schema, 0), 0);
812 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
813 if (rr->request_format != 0) {
814 oid_dotted = oid2dotted(rr->request_format);
816 /* Probably an SRU request: assume XML is required */
817 oid_dotted = wrbuf_alloc();
818 wrbuf_puts(oid_dotted, "1.2.840.10003.5.109.10");
820 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
821 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
822 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
823 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
824 hv_store(href, "LAST", 4, newSViv(0), 0);
825 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
826 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
827 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
828 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
829 hv_store(href, "PID", 3, newSViv(getpid()), 0);
832 composition = rr->comp;
833 if (composition->which == Z_RecordComp_simple)
835 simple = composition->u.simple;
836 if (simple->which == Z_ElementSetNames_generic)
838 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
845 else if (composition->which == Z_RecordComp_complex)
847 if (composition->u.complex->generic &&
849 composition->u.complex->generic &&
850 composition->u.complex->generic->elementSpec &&
851 composition->u.complex->generic->elementSpec->which ==
852 Z_ElementSpec_elementSetName)
854 complex = composition->u.complex;
855 hv_store(href, "COMP", 4,
856 newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
860 #if 0 /* For now ignore this error, which is ubiquitous in SRU */
861 fprintf(stderr, "complex is weird\n");
876 XPUSHs(sv_2mortal(newRV( (SV*) href)));
880 handler_cv = simpleserver_sv2cv( fetch_ref );
881 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
885 temp = hv_fetch(href, "BASENAME", 8, 1);
886 basename = newSVsv(*temp);
888 temp = hv_fetch(href, "RECORD", 6, 1);
889 record = newSVsv(*temp);
891 temp = hv_fetch(href, "LAST", 4, 1);
892 last = newSVsv(*temp);
894 temp = hv_fetch(href, "ERR_CODE", 8, 1);
895 err_code = newSVsv(*temp);
897 temp = hv_fetch(href, "ERR_STR", 7, 1),
898 err_string = newSVsv(*temp);
900 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
901 sur_flag = newSVsv(*temp);
903 temp = hv_fetch(href, "REP_FORM", 8, 1);
904 rep_form = newSVsv(*temp);
906 temp = hv_fetch(href, "SCHEMA", 6, 1);
908 schema = newSVsv(*temp);
909 ptr = SvPV(schema, length);
911 rr->schema = (char *)odr_malloc(rr->stream, length + 1);
912 strcpy(rr->schema, ptr);
916 temp = hv_fetch(href, "HANDLE", 6, 1);
917 point = newSVsv(*temp);
922 ptr = SvPV(basename, length);
923 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
924 strcpy(ODR_basename, ptr);
925 rr->basename = ODR_basename;
927 ptr = SvPV(rep_form, length);
929 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
930 if (oid_dotstring_to_oid(ptr, ODR_oid_buf))
932 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
934 rr->output_format = ODR_oid_buf;
936 ptr = SvPV(record, length);
937 /* Treat GRS-1 records separately */
938 if (!oid_oidcmp(ODR_oid_buf, yaz_oid_recsyn_grs_1))
940 rr->record = (char *) read_grs1(ptr, rr->stream);
945 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
946 strcpy(ODR_record, ptr);
947 rr->record = ODR_record;
950 zhandle->handle = point;
952 rr->last_in_set = SvIV(last);
956 rr->errcode = SvIV(err_code);
957 ptr = SvPV(err_string, length);
958 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
959 strcpy(ODR_errstr, ptr);
960 rr->errstring = ODR_errstr;
962 rr->surrogate_flag = SvIV(sur_flag);
964 wrbuf_destroy(oid_dotted);
985 int bend_present(void *handle, bend_present_rr *rr)
994 Z_RecordComposition *composition;
995 Z_ElementSetNames *simple;
999 Zfront_handle *zhandle = (Zfront_handle *)handle;
1002 /* WRBUF oid_dotted; */
1009 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1010 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1011 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1012 hv_store(href, "START", 5, newSViv(rr->start), 0);
1013 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
1014 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
1015 /*oid_dotted = oid2dotted(rr->request_format_raw);
1016 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
1017 hv_store(href, "HITS", 4, newSViv(0), 0);
1018 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1021 composition = rr->comp;
1022 if (composition->which == Z_RecordComp_simple)
1024 simple = composition->u.simple;
1025 if (simple->which == Z_ElementSetNames_generic)
1027 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
1035 else if (composition->which == Z_RecordComp_complex)
1037 if (composition->u.complex->generic &&
1039 composition->u.complex->generic &&
1040 composition->u.complex->generic->elementSpec &&
1041 composition->u.complex->generic->elementSpec->which ==
1042 Z_ElementSpec_elementSetName)
1044 complex = composition->u.complex;
1045 hv_store(href, "COMP", 4,
1046 newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0);
1063 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1067 handler_cv = simpleserver_sv2cv( present_ref );
1068 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1072 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1073 err_code = newSVsv(*temp);
1075 temp = hv_fetch(href, "ERR_STR", 7, 1);
1076 err_string = newSVsv(*temp);
1078 temp = hv_fetch(href, "HITS", 4, 1);
1079 hits = newSVsv(*temp);
1081 temp = hv_fetch(href, "HANDLE", 6, 1);
1082 point = newSVsv(*temp);
1089 rr->errcode = SvIV(err_code);
1090 rr->hits = SvIV(hits);
1092 ptr = SvPV(err_string, len);
1093 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1094 strcpy(ODR_errstr, ptr);
1095 rr->errstring = ODR_errstr;
1096 /* wrbuf_free(oid_dotted, 1);*/
1097 zhandle->handle = point;
1100 sv_free(err_string);
1102 sv_free( (SV*) href);
1108 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
1110 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
1115 int bend_delete(void *handle, bend_delete_rr *rr)
1117 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
1122 int bend_scan(void *handle, bend_scan_rr *rr)
1129 struct scan_entry *scan_list;
1130 struct scan_entry *buffer;
1131 int *step_size = rr->step_size;
1135 SV *err_code = sv_newmortal();
1136 SV *err_str = sv_newmortal();
1137 SV *point = sv_newmortal();
1138 SV *status = sv_newmortal();
1139 SV *number = sv_newmortal();
1145 Zfront_handle *zhandle = (Zfront_handle *)handle;
1153 if (rr->term->term->which == Z_Term_general)
1155 term_len = rr->term->term->u.general->len;
1156 hv_store(href, "TERM", 4, newSVpv((char*) rr->term->term->u.general->buf, term_len), 0);
1158 rr->errcode = 229; /* Unsupported term type */
1161 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1162 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1163 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1164 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1165 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1166 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1167 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1168 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1170 basenames = rr->basenames;
1171 for (i = 0; i < rr->num_bases; i++)
1173 av_push(aref, newSVpv(*basenames++, 0));
1175 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1179 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1183 handler_cv = simpleserver_sv2cv( scan_ref );
1184 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1188 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1189 err_code = newSVsv(*temp);
1191 temp = hv_fetch(href, "ERR_STR", 7, 1);
1192 err_str = newSVsv(*temp);
1194 temp = hv_fetch(href, "HANDLE", 6, 1);
1195 point = newSVsv(*temp);
1197 temp = hv_fetch(href, "STATUS", 6, 1);
1198 status = newSVsv(*temp);
1200 temp = hv_fetch(href, "NUMBER", 6, 1);
1201 number = newSVsv(*temp);
1203 temp = hv_fetch(href, "ENTRIES", 7, 1);
1204 entries_ref = newSVsv(*temp);
1210 ptr = SvPV(err_str, len);
1211 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1212 strcpy(ODR_errstr, ptr);
1213 rr->errstring = ODR_errstr;
1214 rr->errcode = SvIV(err_code);
1215 rr->num_entries = SvIV(number);
1216 rr->status = SvIV(status);
1217 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1219 entries = (AV *)SvRV(entries_ref);
1220 for (i = 0; i < rr->num_entries; i++)
1222 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1223 temp = hv_fetch(scan_item, "TERM", 4, 1);
1224 ptr = SvPV(*temp, len);
1225 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1226 strcpy(buffer->term, ptr);
1227 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1228 buffer->occurrences = SvIV(*temp);
1230 hv_undef(scan_item);
1232 rr->entries = scan_list;
1233 zhandle->handle = point;
1240 sv_free((SV *)href);
1242 sv_free((SV *)aref);
1244 sv_free((SV *)list);
1246 /*sv_free((SV *)entries);*/
1247 sv_free(entries_ref);
1252 int bend_explain(void *handle, bend_explain_rr *q)
1260 Zfront_handle *zhandle = (Zfront_handle *)handle;
1267 hv_store(href, "EXPLAIN", 7, newSVpv("", 0), 0);
1268 hv_store(href, "DATABASE", 8, newSVpv(q->database, 0), 0);
1269 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1272 XPUSHs(sv_2mortal(newRV((SV*) href)));
1275 handler_cv = simpleserver_sv2cv(explain_ref);
1276 perl_call_sv((SV*) handler_cv, G_SCALAR | G_DISCARD);
1280 temp = hv_fetch(href, "EXPLAIN", 7, 1);
1281 explainsv = newSVsv(*temp);
1287 explain = SvPV(explainsv, len);
1288 q->explain_buf = (char*) odr_malloc(q->stream, len + 1);
1289 strcpy(q->explain_buf, explain);
1294 bend_initresult *bend_init(bend_initrequest *q)
1296 int dummy = simpleserver_clone();
1297 bend_initresult *r = (bend_initresult *)
1298 odr_malloc (q->stream, sizeof(*r));
1303 NMEM nmem = nmem_create();
1304 Zfront_handle *zhandle = (Zfront_handle *) nmem_malloc (nmem,
1313 zhandle->nmem = nmem;
1314 zhandle->stop_flag = 0;
1318 q->bend_sort = bend_sort;
1322 q->bend_search = bend_search;
1326 q->bend_present = bend_present;
1328 /*q->bend_esrequest = bend_esrequest;*/
1329 /*q->bend_delete = bend_delete;*/
1332 q->bend_fetch = bend_fetch;
1336 q->bend_scan = bend_scan;
1340 q->bend_explain = bend_explain;
1344 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1345 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1346 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1347 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1348 hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1349 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1350 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1351 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1354 char *passwd = NULL;
1355 if (q->auth->which == Z_IdAuthentication_open) {
1357 user = nmem_strdup (odr_getmem (q->stream), q->auth->u.open);
1358 cp = strchr (user, '/');
1360 /* password after / given */
1364 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1365 user = q->auth->u.idPass->userId;
1366 passwd = q->auth->u.idPass->password;
1368 /* ### some code paths have user/password unassigned here */
1370 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1372 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1377 XPUSHs(sv_2mortal(newRV((SV*) href)));
1381 if (init_ref != NULL)
1383 handler_cv = simpleserver_sv2cv( init_ref );
1384 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1389 temp = hv_fetch(href, "IMP_ID", 6, 1);
1390 ptr = SvPV(*temp, len);
1391 q->implementation_id = nmem_strdup(nmem, ptr);
1393 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1394 ptr = SvPV(*temp, len);
1395 q->implementation_name = nmem_strdup(nmem, ptr);
1397 temp = hv_fetch(href, "IMP_VER", 7, 1);
1398 ptr = SvPV(*temp, len);
1399 q->implementation_version = nmem_strdup(nmem, ptr);
1401 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1402 r->errcode = SvIV(*temp);
1404 temp = hv_fetch(href, "ERR_STR", 7, 1);
1405 ptr = SvPV(*temp, len);
1406 r->errstring = (char *)odr_malloc(q->stream, len + 1);
1407 strcpy(r->errstring, ptr);
1409 temp = hv_fetch(href, "HANDLE", 6, 1);
1410 handle= newSVsv(*temp);
1411 zhandle->handle = handle;
1413 r->handle = zhandle;
1416 sv_free((SV*) href);
1425 void bend_close(void *handle)
1428 Zfront_handle *zhandle = (Zfront_handle *)handle;
1438 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1442 XPUSHs(sv_2mortal(newRV((SV *)href)));
1446 handler_cv = simpleserver_sv2cv( close_ref );
1447 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1451 sv_free((SV*) href);
1454 sv_free(zhandle->handle);
1458 stop_flag = zhandle->stop_flag;
1459 nmem_destroy(zhandle->nmem);
1460 simpleserver_free();
1468 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1474 set_init_handler(arg)
1477 init_ref = newSVsv(arg);
1481 set_close_handler(arg)
1484 close_ref = newSVsv(arg);
1488 set_sort_handler(arg)
1491 sort_ref = newSVsv(arg);
1494 set_search_handler(arg)
1497 search_ref = newSVsv(arg);
1501 set_fetch_handler(arg)
1504 fetch_ref = newSVsv(arg);
1508 set_present_handler(arg)
1511 present_ref = newSVsv(arg);
1515 set_esrequest_handler(arg)
1518 esrequest_ref = newSVsv(arg);
1522 set_delete_handler(arg)
1525 delete_ref = newSVsv(arg);
1529 set_scan_handler(arg)
1532 scan_ref = newSVsv(arg);
1535 set_explain_handler(arg)
1538 explain_ref = newSVsv(arg);
1549 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1551 for (i = 0; i < items; i++)
1553 ptr = SvPV(ST(i), len);
1554 *argv_buf = (char *)xmalloc(len + 1);
1555 strcpy(*argv_buf++, ptr);
1558 root_perl_context = PERL_GET_CONTEXT;
1559 nmem_mutex_create(&simpleserver_mutex);
1561 /* only for debugging perl_clone .. */
1565 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1573 RETVAL = BEND_SCAN_SUCCESS;
1580 RETVAL = BEND_SCAN_PARTIAL;
1591 ptr = SvPV(arg, len);
1592 yaz_log(YLOG_LOG, "%.*s", len, ptr);