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.13 2002-02-28 11:21:57 mike
29 /*Add RPN structure to search-handler argument hash.
31 /*Revision 1.12 2001/08/30 14:02:10 sondberg
34 /*Revision 1.11 2001/08/30 13:15:11 sondberg
35 /*Corrected a memory leak, one more to go.
37 /*Revision 1.10 2001/08/29 11:48:36 sondberg
40 /* Net::Z3950::SimpleServer::ScanSuccess
41 /* Net::Z3950::SimpleServer::ScanPartial
43 /*and a bit of documentation.
45 /*Revision 1.9 2001/08/24 14:00:20 sondberg
46 /*Added support for scan.
48 /*Revision 1.8 2001/05/21 11:07:02 sondberg
49 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
51 /*Revision 1.7 2001/03/13 14:17:15 sondberg
52 /*Added support for GRS-1.
59 #include <yaz/backend.h>
61 #include <yaz/wrbuf.h>
66 #define GRS_MAX_FIELDS 500
70 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
71 #define sv_undef PL_sv_undef
91 SV *search_ref = NULL;
93 SV *present_ref = NULL;
94 SV *esrequest_ref = NULL;
95 SV *delete_ref = NULL;
100 Z_GenericRecord *read_grs1(char *str, ODR o)
103 char line[512], *buf, *ptr, *original;
105 Z_GenericRecord *r = 0;
113 ptr = strchr(str, '\n');
117 strncpy(line, str, ptr - str);
121 while (*buf && isspace(*buf))
124 memmove(original, str, strlen(str));
127 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
129 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
132 if (!type && *value == '0')
134 if (!(buf = strchr(buf, ')')))
137 while (*buf && isspace(*buf))
143 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
144 r->elements = (Z_TaggedElement **)
145 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
148 if (r->num_elements > GRS_MAX_FIELDS)
150 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
153 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
154 t->tagType = (int *)odr_malloc(o, sizeof(int));
156 t->tagValue = (Z_StringOrNumeric *)
157 odr_malloc(o, sizeof(Z_StringOrNumeric));
158 if ((ivalue = atoi(value)))
160 t->tagValue->which = Z_StringOrNumeric_numeric;
161 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
162 *t->tagValue->u.numeric = ivalue;
166 t->tagValue->which = Z_StringOrNumeric_string;
167 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
168 strcpy(t->tagValue->u.string, value);
170 t->tagOccurrence = 0;
172 t->appliedVariant = 0;
173 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
176 c->which = Z_ElementData_subtree;
177 c->u.subtree = read_grs1(str, o);
181 c->which = Z_ElementData_string;
182 /* buf[strlen(buf)-1] = '\0';*/
183 buf[strlen(buf)] = '\0';
184 c->u.string = odr_strdup(o, buf);
193 static void oid2str(Odr_oid *o, WRBUF buf)
195 for (; *o >= 0; o++) {
197 sprintf(ibuf, "%d", *o);
198 wrbuf_puts(buf, ibuf);
200 wrbuf_putc(buf, '.');
205 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
208 case Z_RPNStructure_simple: {
209 Z_Operand *o = s->u.simple;
212 case Z_Operand_APT: {
213 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
215 if (at->attributes) {
219 for (i = 0; i < at->attributes->num_attributes; i++) {
220 wrbuf_puts(buf, "@attr ");
221 if (at->attributes->attributes[i]->attributeSet) {
222 oid2str(at->attributes->attributes[i]->attributeSet, buf);
223 wrbuf_putc(buf, ' ');
225 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
226 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
227 wrbuf_puts(buf, ibuf);
228 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
229 wrbuf_puts(buf, ibuf);
232 switch (at->term->which) {
233 case Z_Term_general: {
234 wrbuf_putc(buf, '"');
235 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
236 wrbuf_puts(buf, "\" ");
247 case Z_RPNStructure_complex: {
248 Z_Complex *c = s->u.complex;
250 switch (c->roperator->which) {
251 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
252 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
253 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
254 case Z_Operator_prox: abort();
257 if (!rpn2pquery(c->s1, buf))
259 if (!rpn2pquery(c->s2, buf))
269 WRBUF zquery2pquery(Z_Query *q)
271 WRBUF buf = wrbuf_alloc();
273 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
275 if (q->u.type_1->attributeSetId) {
276 /* Output attribute set ID */
277 wrbuf_puts(buf, "@attrset ");
278 oid2str(q->u.type_1->attributeSetId, buf);
279 wrbuf_putc(buf, ' ');
281 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
285 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
287 void fatal(char *fmt, ...)
291 fprintf(stderr, "FATAL (yazwrap): ");
293 vfprintf(stderr, fmt, ap);
295 fprintf(stderr, "\n");
300 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
302 * Creates a new Perl object of type `class'; the newly-created scalar
303 * that is a reference to the blessed thingy `referent' is returned.
305 static SV *newObject(char *class, SV *referent)
310 sv = newRV_noinc((SV*) referent);
311 stash = gv_stashpv(class, 0);
313 fatal("attempt to create object of undefined class '%s'", class);
314 /*assert(stash != 0);*/
320 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
321 static void setMember(HV *hv, char *name, SV *val)
323 /* We don't increment `val's reference count -- I think this is
324 * right because it's created with a refcount of 1, and in fact
325 * the reference via this hash is the only reference to it in
328 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
329 fatal("couldn't store member in hash");
333 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
334 static SV *translateOID(Odr_oid *x)
336 /* Yaz represents an OID by an int array terminated by a negative
337 * value, typically -1; we represent it as a reference to a
338 * blessed scalar string of "."-separated elements.
344 for (i = 0; x[i] >= 0; i++) {
345 sprintf(buf + strlen(buf), "%d", (int) x[i]);
351 * ### We'd like to return a blessed scalar (string) here, but of
352 * course you can't do that in Perl: only references can be
353 * blessed, so we'd have to return a _reference_ to a string, and
354 * bless _that_. Better to do without the blessing, I think.
357 return newSVpv(buf, 0);
359 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
364 static SV *rpn2perl(Z_RPNStructure *s)
371 case Z_RPNStructure_simple: {
372 Z_Operand *o = s->u.simple;
373 Z_AttributesPlusTerm *at;
374 if (o->which != Z_Operand_APT)
375 fatal("can't handle RPN simples other than APT");
376 at = o->u.attributesPlusTerm;
377 if (at->term->which != Z_Term_general)
378 fatal("can't handle RPN terms other than general");
380 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
381 if (at->attributes) {
383 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
384 (SV*) (av = newAV()));
385 for (i = 0; i < at->attributes->num_attributes; i++) {
386 Z_AttributeElement *elem = at->attributes->attributes[i];
388 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
389 (SV*) (hv2 = newHV()));
390 if (elem->attributeSet)
391 setMember(hv2, "attributeSet",
392 translateOID(elem->attributeSet));
393 setMember(hv2, "attributeType",
394 newSViv(*elem->attributeType));
395 assert(elem->which == Z_AttributeValue_numeric);
396 setMember(hv2, "attributeValue",
397 newSViv(*elem->value.numeric));
400 setMember(hv, "attributes", attrs);
402 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
403 at->term->u.general->len));
406 case Z_RPNStructure_complex: {
408 Z_Complex *c = s->u.complex;
409 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
410 switch (c->roperator->which) {
411 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
412 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
413 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
414 case Z_Operator_prox: fatal("proximity not yet supported");
415 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
417 sv = newObject(type, (SV*) (av = newAV()));
418 if ((tmp = rpn2perl(c->s1)) == 0)
421 if ((tmp = rpn2perl(c->s2)) == 0)
426 default: fatal("unknown RPN node type %d", (int) s->which);
433 static SV *zquery2perl(Z_Query *q)
438 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
440 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
441 if (q->u.type_1->attributeSetId)
442 setMember(hv, "attributeSet",
443 translateOID(q->u.type_1->attributeSetId));
444 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
449 int bend_sort(void *handle, bend_sort_rr *rr)
460 char **input_setnames;
461 Zfront_handle *zhandle = (Zfront_handle *)handle;
469 input_setnames = rr->input_setnames;
470 for (i = 0; i < rr->num_input_setnames; i++)
472 av_push(aref, newSVpv(*input_setnames++, 0));
475 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
476 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
477 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
478 hv_store(href, "STATUS", 6, newSViv(0), 0);
482 XPUSHs(sv_2mortal(newRV( (SV*) href)));
486 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
490 temp = hv_fetch(href, "ERR_CODE", 8, 1);
491 err_code = newSVsv(*temp);
493 temp = hv_fetch(href, "ERR_STR", 7, 1);
494 err_str = newSVsv(*temp);
496 temp = hv_fetch(href, "STATUS", 6, 1);
497 status = newSVsv(*temp);
508 rr->errcode = SvIV(err_code);
509 rr->sort_status = SvIV(status);
510 ptr = SvPV(err_str, len);
511 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
512 strcpy(ODR_err_str, ptr);
513 rr->errstring = ODR_err_str;
523 int bend_search(void *handle, bend_search_rr *rr)
540 Zfront_handle *zhandle = (Zfront_handle *)handle;
547 basenames = rr->basenames;
548 for (i = 0; i < rr->num_bases; i++)
550 av_push(aref, newSVpv(*basenames++, 0));
553 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
554 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
555 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
556 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
557 hv_store(href, "HITS", 4, newSViv(0), 0);
558 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
559 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
560 hv_store(href, "PID", 3, newSViv(getpid()), 0);
561 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
562 query = zquery2pquery(rr->query);
565 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
573 XPUSHs(sv_2mortal(newRV( (SV*) href)));
577 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
581 temp = hv_fetch(href, "HITS", 4, 1);
582 hits = newSVsv(*temp);
584 temp = hv_fetch(href, "ERR_CODE", 8, 1);
585 err_code = newSVsv(*temp);
587 temp = hv_fetch(href, "ERR_STR", 7, 1);
588 err_str = newSVsv(*temp);
590 temp = hv_fetch(href, "HANDLE", 6, 1);
591 point = newSVsv(*temp);
599 rr->hits = SvIV(hits);
600 rr->errcode = SvIV(err_code);
601 ptr = SvPV(err_str, len);
602 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
603 strcpy(ODR_errstr, ptr);
604 rr->errstring = ODR_errstr;
605 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
606 memcpy(ODR_point, point, sizeof(*point));
607 zhandle->handle = ODR_point;*/
608 zhandle->handle = point;
613 sv_free( (SV*) aref);
614 sv_free( (SV*) href);
616 wrbuf_free(query, 1);
621 /* ### this is worryingly similar to oid2str() */
622 WRBUF oid2dotted(int *oid)
625 WRBUF buf = wrbuf_alloc();
628 for (; *oid != -1 ; oid++)
633 wrbuf_putc(buf, '.');
639 sprintf(ibuf, "%d", *oid);
640 wrbuf_puts(buf, ibuf);
646 int dotted2oid(char *dotted, int *buffer)
660 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
666 sscanf(ibuf, "%d", oid++);
679 sscanf(ibuf, "%d", oid++);
686 int bend_fetch(void *handle, bend_fetch_rr *rr)
705 Zfront_handle *zhandle = (Zfront_handle *)handle;
707 Z_RecordComposition *composition;
708 Z_ElementSetNames *simple;
717 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
718 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
719 oid_dotted = oid2dotted(rr->request_format_raw);
720 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
721 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
722 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
723 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
724 hv_store(href, "LAST", 4, newSViv(0), 0);
725 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
726 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
727 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
728 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
729 hv_store(href, "PID", 3, newSViv(getpid()), 0);
732 composition = rr->comp;
733 if (composition->which == Z_RecordComp_simple)
735 simple = composition->u.simple;
736 if (simple->which == Z_ElementSetNames_generic)
738 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
753 XPUSHs(sv_2mortal(newRV( (SV*) href)));
757 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
761 temp = hv_fetch(href, "BASENAME", 8, 1);
762 basename = newSVsv(*temp);
764 temp = hv_fetch(href, "RECORD", 6, 1);
765 record = newSVsv(*temp);
767 temp = hv_fetch(href, "LAST", 4, 1);
768 last = newSVsv(*temp);
770 temp = hv_fetch(href, "ERR_CODE", 8, 1);
771 err_code = newSVsv(*temp);
773 temp = hv_fetch(href, "ERR_STR", 7, 1),
774 err_string = newSVsv(*temp);
776 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
777 sur_flag = newSVsv(*temp);
779 temp = hv_fetch(href, "REP_FORM", 8, 1);
780 rep_form = newSVsv(*temp);
782 temp = hv_fetch(href, "HANDLE", 6, 1);
783 point = newSVsv(*temp);
791 ptr = SvPV(basename, length);
792 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
793 strcpy(ODR_basename, ptr);
794 rr->basename = ODR_basename;
796 ptr = SvPV(rep_form, length);
797 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
798 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
800 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
802 rr->output_format_raw = ODR_oid_buf;
804 ptr = SvPV(record, length);
805 oid = oid_getentbyoid(ODR_oid_buf);
806 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
808 rr->record = (char *) read_grs1(ptr, rr->stream);
813 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
814 strcpy(ODR_record, ptr);
815 rr->record = ODR_record;
818 zhandle->handle = point;
820 rr->last_in_set = SvIV(last);
824 rr->errcode = SvIV(err_code);
825 ptr = SvPV(err_string, length);
826 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
827 strcpy(ODR_errstr, ptr);
828 rr->errstring = ODR_errstr;
830 rr->surrogate_flag = SvIV(sur_flag);
832 wrbuf_free(oid_dotted, 1);
846 int bend_present(void *handle, bend_present_rr *rr)
856 Z_RecordComposition *composition;
857 Z_ElementSetNames *simple;
860 Zfront_handle *zhandle = (Zfront_handle *)handle;
862 /* WRBUF oid_dotted; */
869 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
870 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
871 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
872 hv_store(href, "START", 5, newSViv(rr->start), 0);
873 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
874 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
875 /*oid_dotted = oid2dotted(rr->request_format_raw);
876 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
877 hv_store(href, "HITS", 4, newSViv(0), 0);
878 hv_store(href, "PID", 3, newSViv(getpid()), 0);
881 composition = rr->comp;
882 if (composition->which == Z_RecordComp_simple)
884 simple = composition->u.simple;
885 if (simple->which == Z_ElementSetNames_generic)
887 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
904 XPUSHs(sv_2mortal(newRV( (SV*) href)));
908 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
912 temp = hv_fetch(href, "ERR_CODE", 8, 1);
913 err_code = newSVsv(*temp);
915 temp = hv_fetch(href, "ERR_STR", 7, 1);
916 err_string = newSVsv(*temp);
918 temp = hv_fetch(href, "HITS", 4, 1);
919 hits = newSVsv(*temp);
921 temp = hv_fetch(href, "HANDLE", 6, 1);
922 point = newSVsv(*temp);
929 rr->errcode = SvIV(err_code);
930 rr->hits = SvIV(hits);
932 ptr = SvPV(err_string, len);
933 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
934 strcpy(ODR_errstr, ptr);
935 rr->errstring = ODR_errstr;
936 /* wrbuf_free(oid_dotted, 1);*/
937 zhandle->handle = point;
942 sv_free( (SV*) href);
948 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
950 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
955 int bend_delete(void *handle, bend_delete_rr *rr)
957 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
962 int bend_scan(void *handle, bend_scan_rr *rr)
969 struct scan_entry *scan_list;
970 struct scan_entry *buffer;
971 int *step_size = rr->step_size;
975 SV *err_code = sv_newmortal();
976 SV *err_str = sv_newmortal();
977 SV *point = sv_newmortal();
978 SV *status = sv_newmortal();
979 SV *number = sv_newmortal();
987 Zfront_handle *zhandle = (Zfront_handle *)handle;
994 if (rr->term->term->which == Z_Term_general)
996 term_len = rr->term->term->u.general->len;
997 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
999 rr->errcode = 229; /* Unsupported term type */
1002 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1003 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1004 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1005 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1006 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1007 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1008 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1009 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1011 basenames = rr->basenames;
1012 for (i = 0; i < rr->num_bases; i++)
1014 av_push(aref, newSVpv(*basenames++, 0));
1016 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1020 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1024 perl_call_sv(scan_ref, G_SCALAR | G_DISCARD);
1028 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1029 err_code = newSVsv(*temp);
1031 temp = hv_fetch(href, "ERR_STR", 7, 1);
1032 err_str = newSVsv(*temp);
1034 temp = hv_fetch(href, "HANDLE", 6, 1);
1035 point = newSVsv(*temp);
1037 temp = hv_fetch(href, "STATUS", 6, 1);
1038 status = newSVsv(*temp);
1040 temp = hv_fetch(href, "NUMBER", 6, 1);
1041 number = newSVsv(*temp);
1043 temp = hv_fetch(href, "ENTRIES", 7, 1);
1044 entries_ref = newSVsv(*temp);
1050 ptr = SvPV(err_str, len);
1051 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1052 strcpy(ODR_errstr, ptr);
1053 rr->errstring = ODR_errstr;
1054 rr->errcode = SvIV(err_code);
1055 rr->num_entries = SvIV(number);
1056 rr->status = SvIV(status);
1057 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1059 entries = (AV *)SvRV(entries_ref);
1060 for (i = 0; i < rr->num_entries; i++)
1062 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1063 temp = hv_fetch(scan_item, "TERM", 4, 1);
1064 ptr = SvPV(*temp, len);
1065 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1066 strcpy(buffer->term, ptr);
1067 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1068 buffer->occurrences = SvIV(*temp);
1070 hv_undef(scan_item);
1072 rr->entries = scan_list;
1073 zhandle->handle = point;
1080 sv_free((SV *)href);
1082 sv_free((SV *)aref);
1084 sv_free((SV *)list);
1086 /*sv_free((SV *)entries);*/
1087 sv_free(entries_ref);
1093 bend_initresult *bend_init(bend_initrequest *q)
1095 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1102 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1114 /*q->bend_sort = bend_sort;*/
1117 q->bend_search = bend_search;
1121 q->bend_present = bend_present;
1123 /*q->bend_esrequest = bend_esrequest;*/
1124 /*q->bend_delete = bend_delete;*/
1127 q->bend_fetch = bend_fetch;
1131 q->bend_scan = bend_scan;
1134 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1135 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1136 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1137 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1138 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1139 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1143 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1147 if (init_ref != NULL)
1149 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
1154 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1155 name = newSVsv(*temp);
1157 temp = hv_fetch(href, "IMP_VER", 7, 1);
1158 ver = newSVsv(*temp);
1160 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1161 status = newSVsv(*temp);
1163 temp = hv_fetch(href, "HANDLE", 6, 1);
1164 handle= newSVsv(*temp);
1170 zhandle->handle = handle;
1171 r->errcode = SvIV(status);
1172 r->handle = zhandle;
1173 ptr = SvPV(name, len);
1174 q->implementation_name = (char *)xmalloc(len + 1);
1175 strcpy(q->implementation_name, ptr);
1176 /* q->implementation_name = SvPV(name, len);*/
1177 ptr = SvPV(ver, len);
1178 q->implementation_version = (char *)xmalloc(len + 1);
1179 strcpy(q->implementation_version, ptr);
1185 void bend_close(void *handle)
1188 Zfront_handle *zhandle = (Zfront_handle *)handle;
1195 if (close_ref == NULL)
1201 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1205 XPUSHs(sv_2mortal(newRV((SV *)href)));
1209 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
1223 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1226 set_init_handler(arg)
1229 init_ref = newSVsv(arg);
1233 set_close_handler(arg)
1236 close_ref = newSVsv(arg);
1240 set_sort_handler(arg)
1243 sort_ref = newSVsv(arg);
1246 set_search_handler(arg)
1249 search_ref = newSVsv(arg);
1253 set_fetch_handler(arg)
1256 fetch_ref = newSVsv(arg);
1260 set_present_handler(arg)
1263 present_ref = newSVsv(arg);
1267 set_esrequest_handler(arg)
1270 esrequest_ref = newSVsv(arg);
1274 set_delete_handler(arg)
1277 delete_ref = newSVsv(arg);
1281 set_scan_handler(arg)
1284 scan_ref = newSVsv(arg);
1296 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1298 for (i = 0; i < items; i++)
1300 ptr = SvPV(ST(i), len);
1301 *argv_buf = (char *)xmalloc(len + 1);
1302 strcpy(*argv_buf++, ptr);
1306 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1314 RETVAL = BEND_SCAN_SUCCESS;
1321 RETVAL = BEND_SCAN_PARTIAL;