2 * Copyright (c) 2000, Index Data.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation, in whole or in part, for any purpose, is hereby granted,
8 * 1. This copyright and permission notice appear in all copies of the
9 * software and its documentation. Notices of copyright or attribution
10 * which appear at the beginning of any file must remain unchanged.
12 * 2. The name of Index Data or the individual authors may not be used to
13 * endorse or promote products derived from this software without specific
14 * prior written permission.
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
20 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
22 * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 /*$Log: SimpleServer.xs,v $
28 /*Revision 1.14 2002-03-05 00:34:13 mike
29 /*Support for implementation_id (commented out until it's
32 /*Revision 1.13 2002/02/28 11:21:57 mike
33 /*Add RPN structure to search-handler argument hash.
35 /*Revision 1.12 2001/08/30 14:02:10 sondberg
38 /*Revision 1.11 2001/08/30 13:15:11 sondberg
39 /*Corrected a memory leak, one more to go.
41 /*Revision 1.10 2001/08/29 11:48:36 sondberg
44 /* Net::Z3950::SimpleServer::ScanSuccess
45 /* Net::Z3950::SimpleServer::ScanPartial
47 /*and a bit of documentation.
49 /*Revision 1.9 2001/08/24 14:00:20 sondberg
50 /*Added support for scan.
52 /*Revision 1.8 2001/05/21 11:07:02 sondberg
53 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
55 /*Revision 1.7 2001/03/13 14:17:15 sondberg
56 /*Added support for GRS-1.
63 #include <yaz/backend.h>
65 #include <yaz/wrbuf.h>
70 #define GRS_MAX_FIELDS 500
74 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
75 #define sv_undef PL_sv_undef
95 SV *search_ref = NULL;
97 SV *present_ref = NULL;
98 SV *esrequest_ref = NULL;
99 SV *delete_ref = NULL;
104 Z_GenericRecord *read_grs1(char *str, ODR o)
107 char line[512], *buf, *ptr, *original;
109 Z_GenericRecord *r = 0;
117 ptr = strchr(str, '\n');
121 strncpy(line, str, ptr - str);
125 while (*buf && isspace(*buf))
128 memmove(original, str, strlen(str));
131 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
133 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
136 if (!type && *value == '0')
138 if (!(buf = strchr(buf, ')')))
141 while (*buf && isspace(*buf))
147 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
148 r->elements = (Z_TaggedElement **)
149 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
152 if (r->num_elements > GRS_MAX_FIELDS)
154 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
157 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
158 t->tagType = (int *)odr_malloc(o, sizeof(int));
160 t->tagValue = (Z_StringOrNumeric *)
161 odr_malloc(o, sizeof(Z_StringOrNumeric));
162 if ((ivalue = atoi(value)))
164 t->tagValue->which = Z_StringOrNumeric_numeric;
165 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
166 *t->tagValue->u.numeric = ivalue;
170 t->tagValue->which = Z_StringOrNumeric_string;
171 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
172 strcpy(t->tagValue->u.string, value);
174 t->tagOccurrence = 0;
176 t->appliedVariant = 0;
177 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
180 c->which = Z_ElementData_subtree;
181 c->u.subtree = read_grs1(str, o);
185 c->which = Z_ElementData_string;
186 /* buf[strlen(buf)-1] = '\0';*/
187 buf[strlen(buf)] = '\0';
188 c->u.string = odr_strdup(o, buf);
197 static void oid2str(Odr_oid *o, WRBUF buf)
199 for (; *o >= 0; o++) {
201 sprintf(ibuf, "%d", *o);
202 wrbuf_puts(buf, ibuf);
204 wrbuf_putc(buf, '.');
209 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
212 case Z_RPNStructure_simple: {
213 Z_Operand *o = s->u.simple;
216 case Z_Operand_APT: {
217 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
219 if (at->attributes) {
223 for (i = 0; i < at->attributes->num_attributes; i++) {
224 wrbuf_puts(buf, "@attr ");
225 if (at->attributes->attributes[i]->attributeSet) {
226 oid2str(at->attributes->attributes[i]->attributeSet, buf);
227 wrbuf_putc(buf, ' ');
229 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
230 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
231 wrbuf_puts(buf, ibuf);
232 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
233 wrbuf_puts(buf, ibuf);
236 switch (at->term->which) {
237 case Z_Term_general: {
238 wrbuf_putc(buf, '"');
239 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
240 wrbuf_puts(buf, "\" ");
251 case Z_RPNStructure_complex: {
252 Z_Complex *c = s->u.complex;
254 switch (c->roperator->which) {
255 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
256 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
257 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
258 case Z_Operator_prox: abort();
261 if (!rpn2pquery(c->s1, buf))
263 if (!rpn2pquery(c->s2, buf))
273 WRBUF zquery2pquery(Z_Query *q)
275 WRBUF buf = wrbuf_alloc();
277 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
279 if (q->u.type_1->attributeSetId) {
280 /* Output attribute set ID */
281 wrbuf_puts(buf, "@attrset ");
282 oid2str(q->u.type_1->attributeSetId, buf);
283 wrbuf_putc(buf, ' ');
285 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
289 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
291 void fatal(char *fmt, ...)
295 fprintf(stderr, "FATAL (yazwrap): ");
297 vfprintf(stderr, fmt, ap);
299 fprintf(stderr, "\n");
304 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
306 * Creates a new Perl object of type `class'; the newly-created scalar
307 * that is a reference to the blessed thingy `referent' is returned.
309 static SV *newObject(char *class, SV *referent)
314 sv = newRV_noinc((SV*) referent);
315 stash = gv_stashpv(class, 0);
317 fatal("attempt to create object of undefined class '%s'", class);
318 /*assert(stash != 0);*/
324 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
325 static void setMember(HV *hv, char *name, SV *val)
327 /* We don't increment `val's reference count -- I think this is
328 * right because it's created with a refcount of 1, and in fact
329 * the reference via this hash is the only reference to it in
332 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
333 fatal("couldn't store member in hash");
337 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
338 static SV *translateOID(Odr_oid *x)
340 /* Yaz represents an OID by an int array terminated by a negative
341 * value, typically -1; we represent it as a reference to a
342 * blessed scalar string of "."-separated elements.
348 for (i = 0; x[i] >= 0; i++) {
349 sprintf(buf + strlen(buf), "%d", (int) x[i]);
355 * ### We'd like to return a blessed scalar (string) here, but of
356 * course you can't do that in Perl: only references can be
357 * blessed, so we'd have to return a _reference_ to a string, and
358 * bless _that_. Better to do without the blessing, I think.
361 return newSVpv(buf, 0);
363 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
368 static SV *rpn2perl(Z_RPNStructure *s)
375 case Z_RPNStructure_simple: {
376 Z_Operand *o = s->u.simple;
377 Z_AttributesPlusTerm *at;
378 if (o->which != Z_Operand_APT)
379 fatal("can't handle RPN simples other than APT");
380 at = o->u.attributesPlusTerm;
381 if (at->term->which != Z_Term_general)
382 fatal("can't handle RPN terms other than general");
384 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
385 if (at->attributes) {
387 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
388 (SV*) (av = newAV()));
389 for (i = 0; i < at->attributes->num_attributes; i++) {
390 Z_AttributeElement *elem = at->attributes->attributes[i];
392 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
393 (SV*) (hv2 = newHV()));
394 if (elem->attributeSet)
395 setMember(hv2, "attributeSet",
396 translateOID(elem->attributeSet));
397 setMember(hv2, "attributeType",
398 newSViv(*elem->attributeType));
399 assert(elem->which == Z_AttributeValue_numeric);
400 setMember(hv2, "attributeValue",
401 newSViv(*elem->value.numeric));
404 setMember(hv, "attributes", attrs);
406 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
407 at->term->u.general->len));
410 case Z_RPNStructure_complex: {
412 Z_Complex *c = s->u.complex;
413 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
414 switch (c->roperator->which) {
415 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
416 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
417 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
418 case Z_Operator_prox: fatal("proximity not yet supported");
419 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
421 sv = newObject(type, (SV*) (av = newAV()));
422 if ((tmp = rpn2perl(c->s1)) == 0)
425 if ((tmp = rpn2perl(c->s2)) == 0)
430 default: fatal("unknown RPN node type %d", (int) s->which);
437 static SV *zquery2perl(Z_Query *q)
442 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
444 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
445 if (q->u.type_1->attributeSetId)
446 setMember(hv, "attributeSet",
447 translateOID(q->u.type_1->attributeSetId));
448 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
453 int bend_sort(void *handle, bend_sort_rr *rr)
464 char **input_setnames;
465 Zfront_handle *zhandle = (Zfront_handle *)handle;
473 input_setnames = rr->input_setnames;
474 for (i = 0; i < rr->num_input_setnames; i++)
476 av_push(aref, newSVpv(*input_setnames++, 0));
479 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
480 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
481 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
482 hv_store(href, "STATUS", 6, newSViv(0), 0);
486 XPUSHs(sv_2mortal(newRV( (SV*) href)));
490 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
494 temp = hv_fetch(href, "ERR_CODE", 8, 1);
495 err_code = newSVsv(*temp);
497 temp = hv_fetch(href, "ERR_STR", 7, 1);
498 err_str = newSVsv(*temp);
500 temp = hv_fetch(href, "STATUS", 6, 1);
501 status = newSVsv(*temp);
512 rr->errcode = SvIV(err_code);
513 rr->sort_status = SvIV(status);
514 ptr = SvPV(err_str, len);
515 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
516 strcpy(ODR_err_str, ptr);
517 rr->errstring = ODR_err_str;
527 int bend_search(void *handle, bend_search_rr *rr)
544 Zfront_handle *zhandle = (Zfront_handle *)handle;
551 basenames = rr->basenames;
552 for (i = 0; i < rr->num_bases; i++)
554 av_push(aref, newSVpv(*basenames++, 0));
557 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
558 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
559 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
560 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
561 hv_store(href, "HITS", 4, newSViv(0), 0);
562 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
563 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
564 hv_store(href, "PID", 3, newSViv(getpid()), 0);
565 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
566 query = zquery2pquery(rr->query);
569 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
577 XPUSHs(sv_2mortal(newRV( (SV*) href)));
581 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
585 temp = hv_fetch(href, "HITS", 4, 1);
586 hits = newSVsv(*temp);
588 temp = hv_fetch(href, "ERR_CODE", 8, 1);
589 err_code = newSVsv(*temp);
591 temp = hv_fetch(href, "ERR_STR", 7, 1);
592 err_str = newSVsv(*temp);
594 temp = hv_fetch(href, "HANDLE", 6, 1);
595 point = newSVsv(*temp);
603 rr->hits = SvIV(hits);
604 rr->errcode = SvIV(err_code);
605 ptr = SvPV(err_str, len);
606 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
607 strcpy(ODR_errstr, ptr);
608 rr->errstring = ODR_errstr;
609 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
610 memcpy(ODR_point, point, sizeof(*point));
611 zhandle->handle = ODR_point;*/
612 zhandle->handle = point;
617 sv_free( (SV*) aref);
618 sv_free( (SV*) href);
620 wrbuf_free(query, 1);
625 /* ### this is worryingly similar to oid2str() */
626 WRBUF oid2dotted(int *oid)
629 WRBUF buf = wrbuf_alloc();
632 for (; *oid != -1 ; oid++)
637 wrbuf_putc(buf, '.');
643 sprintf(ibuf, "%d", *oid);
644 wrbuf_puts(buf, ibuf);
650 int dotted2oid(char *dotted, int *buffer)
664 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
670 sscanf(ibuf, "%d", oid++);
683 sscanf(ibuf, "%d", oid++);
690 int bend_fetch(void *handle, bend_fetch_rr *rr)
709 Zfront_handle *zhandle = (Zfront_handle *)handle;
711 Z_RecordComposition *composition;
712 Z_ElementSetNames *simple;
721 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
722 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
723 oid_dotted = oid2dotted(rr->request_format_raw);
724 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
725 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
726 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
727 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
728 hv_store(href, "LAST", 4, newSViv(0), 0);
729 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
730 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
731 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
732 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
733 hv_store(href, "PID", 3, newSViv(getpid()), 0);
736 composition = rr->comp;
737 if (composition->which == Z_RecordComp_simple)
739 simple = composition->u.simple;
740 if (simple->which == Z_ElementSetNames_generic)
742 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
757 XPUSHs(sv_2mortal(newRV( (SV*) href)));
761 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
765 temp = hv_fetch(href, "BASENAME", 8, 1);
766 basename = newSVsv(*temp);
768 temp = hv_fetch(href, "RECORD", 6, 1);
769 record = newSVsv(*temp);
771 temp = hv_fetch(href, "LAST", 4, 1);
772 last = newSVsv(*temp);
774 temp = hv_fetch(href, "ERR_CODE", 8, 1);
775 err_code = newSVsv(*temp);
777 temp = hv_fetch(href, "ERR_STR", 7, 1),
778 err_string = newSVsv(*temp);
780 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
781 sur_flag = newSVsv(*temp);
783 temp = hv_fetch(href, "REP_FORM", 8, 1);
784 rep_form = newSVsv(*temp);
786 temp = hv_fetch(href, "HANDLE", 6, 1);
787 point = newSVsv(*temp);
795 ptr = SvPV(basename, length);
796 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
797 strcpy(ODR_basename, ptr);
798 rr->basename = ODR_basename;
800 ptr = SvPV(rep_form, length);
801 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
802 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
804 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
806 rr->output_format_raw = ODR_oid_buf;
808 ptr = SvPV(record, length);
809 oid = oid_getentbyoid(ODR_oid_buf);
810 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
812 rr->record = (char *) read_grs1(ptr, rr->stream);
817 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
818 strcpy(ODR_record, ptr);
819 rr->record = ODR_record;
822 zhandle->handle = point;
824 rr->last_in_set = SvIV(last);
828 rr->errcode = SvIV(err_code);
829 ptr = SvPV(err_string, length);
830 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
831 strcpy(ODR_errstr, ptr);
832 rr->errstring = ODR_errstr;
834 rr->surrogate_flag = SvIV(sur_flag);
836 wrbuf_free(oid_dotted, 1);
850 int bend_present(void *handle, bend_present_rr *rr)
860 Z_RecordComposition *composition;
861 Z_ElementSetNames *simple;
864 Zfront_handle *zhandle = (Zfront_handle *)handle;
866 /* WRBUF oid_dotted; */
873 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
874 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
875 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
876 hv_store(href, "START", 5, newSViv(rr->start), 0);
877 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
878 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
879 /*oid_dotted = oid2dotted(rr->request_format_raw);
880 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
881 hv_store(href, "HITS", 4, newSViv(0), 0);
882 hv_store(href, "PID", 3, newSViv(getpid()), 0);
885 composition = rr->comp;
886 if (composition->which == Z_RecordComp_simple)
888 simple = composition->u.simple;
889 if (simple->which == Z_ElementSetNames_generic)
891 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
908 XPUSHs(sv_2mortal(newRV( (SV*) href)));
912 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
916 temp = hv_fetch(href, "ERR_CODE", 8, 1);
917 err_code = newSVsv(*temp);
919 temp = hv_fetch(href, "ERR_STR", 7, 1);
920 err_string = newSVsv(*temp);
922 temp = hv_fetch(href, "HITS", 4, 1);
923 hits = newSVsv(*temp);
925 temp = hv_fetch(href, "HANDLE", 6, 1);
926 point = newSVsv(*temp);
933 rr->errcode = SvIV(err_code);
934 rr->hits = SvIV(hits);
936 ptr = SvPV(err_string, len);
937 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
938 strcpy(ODR_errstr, ptr);
939 rr->errstring = ODR_errstr;
940 /* wrbuf_free(oid_dotted, 1);*/
941 zhandle->handle = point;
946 sv_free( (SV*) href);
952 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
954 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
959 int bend_delete(void *handle, bend_delete_rr *rr)
961 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
966 int bend_scan(void *handle, bend_scan_rr *rr)
973 struct scan_entry *scan_list;
974 struct scan_entry *buffer;
975 int *step_size = rr->step_size;
979 SV *err_code = sv_newmortal();
980 SV *err_str = sv_newmortal();
981 SV *point = sv_newmortal();
982 SV *status = sv_newmortal();
983 SV *number = sv_newmortal();
991 Zfront_handle *zhandle = (Zfront_handle *)handle;
998 if (rr->term->term->which == Z_Term_general)
1000 term_len = rr->term->term->u.general->len;
1001 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1003 rr->errcode = 229; /* Unsupported term type */
1006 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1007 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1008 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1009 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1010 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1011 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1012 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1013 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1015 basenames = rr->basenames;
1016 for (i = 0; i < rr->num_bases; i++)
1018 av_push(aref, newSVpv(*basenames++, 0));
1020 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1024 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1028 perl_call_sv(scan_ref, G_SCALAR | G_DISCARD);
1032 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1033 err_code = newSVsv(*temp);
1035 temp = hv_fetch(href, "ERR_STR", 7, 1);
1036 err_str = newSVsv(*temp);
1038 temp = hv_fetch(href, "HANDLE", 6, 1);
1039 point = newSVsv(*temp);
1041 temp = hv_fetch(href, "STATUS", 6, 1);
1042 status = newSVsv(*temp);
1044 temp = hv_fetch(href, "NUMBER", 6, 1);
1045 number = newSVsv(*temp);
1047 temp = hv_fetch(href, "ENTRIES", 7, 1);
1048 entries_ref = newSVsv(*temp);
1054 ptr = SvPV(err_str, len);
1055 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1056 strcpy(ODR_errstr, ptr);
1057 rr->errstring = ODR_errstr;
1058 rr->errcode = SvIV(err_code);
1059 rr->num_entries = SvIV(number);
1060 rr->status = SvIV(status);
1061 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1063 entries = (AV *)SvRV(entries_ref);
1064 for (i = 0; i < rr->num_entries; i++)
1066 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1067 temp = hv_fetch(scan_item, "TERM", 4, 1);
1068 ptr = SvPV(*temp, len);
1069 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1070 strcpy(buffer->term, ptr);
1071 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1072 buffer->occurrences = SvIV(*temp);
1074 hv_undef(scan_item);
1076 rr->entries = scan_list;
1077 zhandle->handle = point;
1084 sv_free((SV *)href);
1086 sv_free((SV *)aref);
1088 sv_free((SV *)list);
1090 /*sv_free((SV *)entries);*/
1091 sv_free(entries_ref);
1097 bend_initresult *bend_init(bend_initrequest *q)
1099 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1107 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1119 /*q->bend_sort = bend_sort;*/
1122 q->bend_search = bend_search;
1126 q->bend_present = bend_present;
1128 /*q->bend_esrequest = bend_esrequest;*/
1129 /*q->bend_delete = bend_delete;*/
1132 q->bend_fetch = bend_fetch;
1136 q->bend_scan = bend_scan;
1139 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1140 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1141 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1142 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1143 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1144 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1145 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1149 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1153 if (init_ref != NULL)
1155 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
1160 temp = hv_fetch(href, "IMP_ID", 6, 1);
1161 id = newSVsv(*temp);
1163 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1164 name = newSVsv(*temp);
1166 temp = hv_fetch(href, "IMP_VER", 7, 1);
1167 ver = newSVsv(*temp);
1169 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1170 status = newSVsv(*temp);
1172 temp = hv_fetch(href, "HANDLE", 6, 1);
1173 handle= newSVsv(*temp);
1179 zhandle->handle = handle;
1180 r->errcode = SvIV(status);
1181 r->handle = zhandle;
1182 #if 0 /* implementation_id support is not yet in mainstream Yaz */
1183 ptr = SvPV(id, len);
1184 q->implementation_id = (char *)xmalloc(len + 1);
1185 strcpy(q->implementation_id, ptr);
1187 ptr = SvPV(name, len);
1188 q->implementation_name = (char *)xmalloc(len + 1);
1189 strcpy(q->implementation_name, ptr);
1190 /* q->implementation_name = SvPV(name, len);*/
1191 ptr = SvPV(ver, len);
1192 q->implementation_version = (char *)xmalloc(len + 1);
1193 strcpy(q->implementation_version, ptr);
1199 void bend_close(void *handle)
1202 Zfront_handle *zhandle = (Zfront_handle *)handle;
1209 if (close_ref == NULL)
1215 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1219 XPUSHs(sv_2mortal(newRV((SV *)href)));
1223 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
1237 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1240 set_init_handler(arg)
1243 init_ref = newSVsv(arg);
1247 set_close_handler(arg)
1250 close_ref = newSVsv(arg);
1254 set_sort_handler(arg)
1257 sort_ref = newSVsv(arg);
1260 set_search_handler(arg)
1263 search_ref = newSVsv(arg);
1267 set_fetch_handler(arg)
1270 fetch_ref = newSVsv(arg);
1274 set_present_handler(arg)
1277 present_ref = newSVsv(arg);
1281 set_esrequest_handler(arg)
1284 esrequest_ref = newSVsv(arg);
1288 set_delete_handler(arg)
1291 delete_ref = newSVsv(arg);
1295 set_scan_handler(arg)
1298 scan_ref = newSVsv(arg);
1310 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1312 for (i = 0; i < items; i++)
1314 ptr = SvPV(ST(i), len);
1315 *argv_buf = (char *)xmalloc(len + 1);
1316 strcpy(*argv_buf++, ptr);
1320 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1328 RETVAL = BEND_SCAN_SUCCESS;
1335 RETVAL = BEND_SCAN_PARTIAL;