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.15 2002-09-16 13:55:53 sondberg
29 /*Added support for authentication into SimpleServer.
31 /*Revision 1.14 2002/03/05 00:34:13 mike
32 /*Support for implementation_id (commented out until it's
35 /*Revision 1.13 2002/02/28 11:21:57 mike
36 /*Add RPN structure to search-handler argument hash.
38 /*Revision 1.12 2001/08/30 14:02:10 sondberg
41 /*Revision 1.11 2001/08/30 13:15:11 sondberg
42 /*Corrected a memory leak, one more to go.
44 /*Revision 1.10 2001/08/29 11:48:36 sondberg
47 /* Net::Z3950::SimpleServer::ScanSuccess
48 /* Net::Z3950::SimpleServer::ScanPartial
50 /*and a bit of documentation.
52 /*Revision 1.9 2001/08/24 14:00:20 sondberg
53 /*Added support for scan.
55 /*Revision 1.8 2001/05/21 11:07:02 sondberg
56 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
58 /*Revision 1.7 2001/03/13 14:17:15 sondberg
59 /*Added support for GRS-1.
66 #include <yaz/backend.h>
68 #include <yaz/wrbuf.h>
73 #define GRS_MAX_FIELDS 500
77 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
78 #define sv_undef PL_sv_undef
98 SV *search_ref = NULL;
100 SV *present_ref = NULL;
101 SV *esrequest_ref = NULL;
102 SV *delete_ref = NULL;
107 Z_GenericRecord *read_grs1(char *str, ODR o)
110 char line[512], *buf, *ptr, *original;
112 Z_GenericRecord *r = 0;
120 ptr = strchr(str, '\n');
124 strncpy(line, str, ptr - str);
128 while (*buf && isspace(*buf))
131 memmove(original, str, strlen(str));
134 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
136 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
139 if (!type && *value == '0')
141 if (!(buf = strchr(buf, ')')))
144 while (*buf && isspace(*buf))
150 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
151 r->elements = (Z_TaggedElement **)
152 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
155 if (r->num_elements > GRS_MAX_FIELDS)
157 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
160 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
161 t->tagType = (int *)odr_malloc(o, sizeof(int));
163 t->tagValue = (Z_StringOrNumeric *)
164 odr_malloc(o, sizeof(Z_StringOrNumeric));
165 if ((ivalue = atoi(value)))
167 t->tagValue->which = Z_StringOrNumeric_numeric;
168 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
169 *t->tagValue->u.numeric = ivalue;
173 t->tagValue->which = Z_StringOrNumeric_string;
174 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
175 strcpy(t->tagValue->u.string, value);
177 t->tagOccurrence = 0;
179 t->appliedVariant = 0;
180 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
183 c->which = Z_ElementData_subtree;
184 c->u.subtree = read_grs1(str, o);
188 c->which = Z_ElementData_string;
189 /* buf[strlen(buf)-1] = '\0';*/
190 buf[strlen(buf)] = '\0';
191 c->u.string = odr_strdup(o, buf);
200 static void oid2str(Odr_oid *o, WRBUF buf)
202 for (; *o >= 0; o++) {
204 sprintf(ibuf, "%d", *o);
205 wrbuf_puts(buf, ibuf);
207 wrbuf_putc(buf, '.');
212 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
215 case Z_RPNStructure_simple: {
216 Z_Operand *o = s->u.simple;
219 case Z_Operand_APT: {
220 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
222 if (at->attributes) {
226 for (i = 0; i < at->attributes->num_attributes; i++) {
227 wrbuf_puts(buf, "@attr ");
228 if (at->attributes->attributes[i]->attributeSet) {
229 oid2str(at->attributes->attributes[i]->attributeSet, buf);
230 wrbuf_putc(buf, ' ');
232 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
233 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
234 wrbuf_puts(buf, ibuf);
235 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
236 wrbuf_puts(buf, ibuf);
239 switch (at->term->which) {
240 case Z_Term_general: {
241 wrbuf_putc(buf, '"');
242 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
243 wrbuf_puts(buf, "\" ");
254 case Z_RPNStructure_complex: {
255 Z_Complex *c = s->u.complex;
257 switch (c->roperator->which) {
258 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
259 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
260 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
261 case Z_Operator_prox: abort();
264 if (!rpn2pquery(c->s1, buf))
266 if (!rpn2pquery(c->s2, buf))
276 WRBUF zquery2pquery(Z_Query *q)
278 WRBUF buf = wrbuf_alloc();
280 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
282 if (q->u.type_1->attributeSetId) {
283 /* Output attribute set ID */
284 wrbuf_puts(buf, "@attrset ");
285 oid2str(q->u.type_1->attributeSetId, buf);
286 wrbuf_putc(buf, ' ');
288 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
292 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
294 void fatal(char *fmt, ...)
298 fprintf(stderr, "FATAL (yazwrap): ");
300 vfprintf(stderr, fmt, ap);
302 fprintf(stderr, "\n");
307 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
309 * Creates a new Perl object of type `class'; the newly-created scalar
310 * that is a reference to the blessed thingy `referent' is returned.
312 static SV *newObject(char *class, SV *referent)
317 sv = newRV_noinc((SV*) referent);
318 stash = gv_stashpv(class, 0);
320 fatal("attempt to create object of undefined class '%s'", class);
321 /*assert(stash != 0);*/
327 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
328 static void setMember(HV *hv, char *name, SV *val)
330 /* We don't increment `val's reference count -- I think this is
331 * right because it's created with a refcount of 1, and in fact
332 * the reference via this hash is the only reference to it in
335 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
336 fatal("couldn't store member in hash");
340 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
341 static SV *translateOID(Odr_oid *x)
343 /* Yaz represents an OID by an int array terminated by a negative
344 * value, typically -1; we represent it as a reference to a
345 * blessed scalar string of "."-separated elements.
351 for (i = 0; x[i] >= 0; i++) {
352 sprintf(buf + strlen(buf), "%d", (int) x[i]);
358 * ### We'd like to return a blessed scalar (string) here, but of
359 * course you can't do that in Perl: only references can be
360 * blessed, so we'd have to return a _reference_ to a string, and
361 * bless _that_. Better to do without the blessing, I think.
364 return newSVpv(buf, 0);
366 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
371 static SV *rpn2perl(Z_RPNStructure *s)
378 case Z_RPNStructure_simple: {
379 Z_Operand *o = s->u.simple;
380 Z_AttributesPlusTerm *at;
381 if (o->which != Z_Operand_APT)
382 fatal("can't handle RPN simples other than APT");
383 at = o->u.attributesPlusTerm;
384 if (at->term->which != Z_Term_general)
385 fatal("can't handle RPN terms other than general");
387 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
388 if (at->attributes) {
390 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
391 (SV*) (av = newAV()));
392 for (i = 0; i < at->attributes->num_attributes; i++) {
393 Z_AttributeElement *elem = at->attributes->attributes[i];
395 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
396 (SV*) (hv2 = newHV()));
397 if (elem->attributeSet)
398 setMember(hv2, "attributeSet",
399 translateOID(elem->attributeSet));
400 setMember(hv2, "attributeType",
401 newSViv(*elem->attributeType));
402 assert(elem->which == Z_AttributeValue_numeric);
403 setMember(hv2, "attributeValue",
404 newSViv(*elem->value.numeric));
407 setMember(hv, "attributes", attrs);
409 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
410 at->term->u.general->len));
413 case Z_RPNStructure_complex: {
415 Z_Complex *c = s->u.complex;
416 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
417 switch (c->roperator->which) {
418 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
419 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
420 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
421 case Z_Operator_prox: fatal("proximity not yet supported");
422 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
424 sv = newObject(type, (SV*) (av = newAV()));
425 if ((tmp = rpn2perl(c->s1)) == 0)
428 if ((tmp = rpn2perl(c->s2)) == 0)
433 default: fatal("unknown RPN node type %d", (int) s->which);
440 static SV *zquery2perl(Z_Query *q)
445 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
447 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
448 if (q->u.type_1->attributeSetId)
449 setMember(hv, "attributeSet",
450 translateOID(q->u.type_1->attributeSetId));
451 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
456 int bend_sort(void *handle, bend_sort_rr *rr)
467 char **input_setnames;
468 Zfront_handle *zhandle = (Zfront_handle *)handle;
476 input_setnames = rr->input_setnames;
477 for (i = 0; i < rr->num_input_setnames; i++)
479 av_push(aref, newSVpv(*input_setnames++, 0));
482 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
483 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
484 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
485 hv_store(href, "STATUS", 6, newSViv(0), 0);
489 XPUSHs(sv_2mortal(newRV( (SV*) href)));
493 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
497 temp = hv_fetch(href, "ERR_CODE", 8, 1);
498 err_code = newSVsv(*temp);
500 temp = hv_fetch(href, "ERR_STR", 7, 1);
501 err_str = newSVsv(*temp);
503 temp = hv_fetch(href, "STATUS", 6, 1);
504 status = newSVsv(*temp);
515 rr->errcode = SvIV(err_code);
516 rr->sort_status = SvIV(status);
517 ptr = SvPV(err_str, len);
518 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
519 strcpy(ODR_err_str, ptr);
520 rr->errstring = ODR_err_str;
530 int bend_search(void *handle, bend_search_rr *rr)
547 Zfront_handle *zhandle = (Zfront_handle *)handle;
554 basenames = rr->basenames;
555 for (i = 0; i < rr->num_bases; i++)
557 av_push(aref, newSVpv(*basenames++, 0));
560 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
561 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
562 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
563 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
564 hv_store(href, "HITS", 4, newSViv(0), 0);
565 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
566 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
567 hv_store(href, "PID", 3, newSViv(getpid()), 0);
568 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
569 query = zquery2pquery(rr->query);
572 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
580 XPUSHs(sv_2mortal(newRV( (SV*) href)));
584 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
588 temp = hv_fetch(href, "HITS", 4, 1);
589 hits = newSVsv(*temp);
591 temp = hv_fetch(href, "ERR_CODE", 8, 1);
592 err_code = newSVsv(*temp);
594 temp = hv_fetch(href, "ERR_STR", 7, 1);
595 err_str = newSVsv(*temp);
597 temp = hv_fetch(href, "HANDLE", 6, 1);
598 point = newSVsv(*temp);
606 rr->hits = SvIV(hits);
607 rr->errcode = SvIV(err_code);
608 ptr = SvPV(err_str, len);
609 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
610 strcpy(ODR_errstr, ptr);
611 rr->errstring = ODR_errstr;
612 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
613 memcpy(ODR_point, point, sizeof(*point));
614 zhandle->handle = ODR_point;*/
615 zhandle->handle = point;
620 sv_free( (SV*) aref);
621 sv_free( (SV*) href);
623 wrbuf_free(query, 1);
628 /* ### this is worryingly similar to oid2str() */
629 WRBUF oid2dotted(int *oid)
632 WRBUF buf = wrbuf_alloc();
635 for (; *oid != -1 ; oid++)
640 wrbuf_putc(buf, '.');
646 sprintf(ibuf, "%d", *oid);
647 wrbuf_puts(buf, ibuf);
653 int dotted2oid(char *dotted, int *buffer)
667 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
673 sscanf(ibuf, "%d", oid++);
686 sscanf(ibuf, "%d", oid++);
693 int bend_fetch(void *handle, bend_fetch_rr *rr)
712 Zfront_handle *zhandle = (Zfront_handle *)handle;
714 Z_RecordComposition *composition;
715 Z_ElementSetNames *simple;
724 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
725 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
726 oid_dotted = oid2dotted(rr->request_format_raw);
727 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
728 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
729 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
730 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
731 hv_store(href, "LAST", 4, newSViv(0), 0);
732 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
733 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
734 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
735 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
736 hv_store(href, "PID", 3, newSViv(getpid()), 0);
739 composition = rr->comp;
740 if (composition->which == Z_RecordComp_simple)
742 simple = composition->u.simple;
743 if (simple->which == Z_ElementSetNames_generic)
745 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
760 XPUSHs(sv_2mortal(newRV( (SV*) href)));
764 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
768 temp = hv_fetch(href, "BASENAME", 8, 1);
769 basename = newSVsv(*temp);
771 temp = hv_fetch(href, "RECORD", 6, 1);
772 record = newSVsv(*temp);
774 temp = hv_fetch(href, "LAST", 4, 1);
775 last = newSVsv(*temp);
777 temp = hv_fetch(href, "ERR_CODE", 8, 1);
778 err_code = newSVsv(*temp);
780 temp = hv_fetch(href, "ERR_STR", 7, 1),
781 err_string = newSVsv(*temp);
783 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
784 sur_flag = newSVsv(*temp);
786 temp = hv_fetch(href, "REP_FORM", 8, 1);
787 rep_form = newSVsv(*temp);
789 temp = hv_fetch(href, "HANDLE", 6, 1);
790 point = newSVsv(*temp);
798 ptr = SvPV(basename, length);
799 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
800 strcpy(ODR_basename, ptr);
801 rr->basename = ODR_basename;
803 ptr = SvPV(rep_form, length);
804 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
805 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
807 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
809 rr->output_format_raw = ODR_oid_buf;
811 ptr = SvPV(record, length);
812 oid = oid_getentbyoid(ODR_oid_buf);
813 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
815 rr->record = (char *) read_grs1(ptr, rr->stream);
820 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
821 strcpy(ODR_record, ptr);
822 rr->record = ODR_record;
825 zhandle->handle = point;
827 rr->last_in_set = SvIV(last);
831 rr->errcode = SvIV(err_code);
832 ptr = SvPV(err_string, length);
833 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
834 strcpy(ODR_errstr, ptr);
835 rr->errstring = ODR_errstr;
837 rr->surrogate_flag = SvIV(sur_flag);
839 wrbuf_free(oid_dotted, 1);
853 int bend_present(void *handle, bend_present_rr *rr)
863 Z_RecordComposition *composition;
864 Z_ElementSetNames *simple;
867 Zfront_handle *zhandle = (Zfront_handle *)handle;
869 /* WRBUF oid_dotted; */
876 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
877 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
878 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
879 hv_store(href, "START", 5, newSViv(rr->start), 0);
880 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
881 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
882 /*oid_dotted = oid2dotted(rr->request_format_raw);
883 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
884 hv_store(href, "HITS", 4, newSViv(0), 0);
885 hv_store(href, "PID", 3, newSViv(getpid()), 0);
888 composition = rr->comp;
889 if (composition->which == Z_RecordComp_simple)
891 simple = composition->u.simple;
892 if (simple->which == Z_ElementSetNames_generic)
894 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
911 XPUSHs(sv_2mortal(newRV( (SV*) href)));
915 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
919 temp = hv_fetch(href, "ERR_CODE", 8, 1);
920 err_code = newSVsv(*temp);
922 temp = hv_fetch(href, "ERR_STR", 7, 1);
923 err_string = newSVsv(*temp);
925 temp = hv_fetch(href, "HITS", 4, 1);
926 hits = newSVsv(*temp);
928 temp = hv_fetch(href, "HANDLE", 6, 1);
929 point = newSVsv(*temp);
936 rr->errcode = SvIV(err_code);
937 rr->hits = SvIV(hits);
939 ptr = SvPV(err_string, len);
940 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
941 strcpy(ODR_errstr, ptr);
942 rr->errstring = ODR_errstr;
943 /* wrbuf_free(oid_dotted, 1);*/
944 zhandle->handle = point;
949 sv_free( (SV*) href);
955 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
957 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
962 int bend_delete(void *handle, bend_delete_rr *rr)
964 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
969 int bend_scan(void *handle, bend_scan_rr *rr)
976 struct scan_entry *scan_list;
977 struct scan_entry *buffer;
978 int *step_size = rr->step_size;
982 SV *err_code = sv_newmortal();
983 SV *err_str = sv_newmortal();
984 SV *point = sv_newmortal();
985 SV *status = sv_newmortal();
986 SV *number = sv_newmortal();
994 Zfront_handle *zhandle = (Zfront_handle *)handle;
1001 if (rr->term->term->which == Z_Term_general)
1003 term_len = rr->term->term->u.general->len;
1004 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1006 rr->errcode = 229; /* Unsupported term type */
1009 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1010 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1011 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1012 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1013 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1014 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1015 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1016 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1018 basenames = rr->basenames;
1019 for (i = 0; i < rr->num_bases; i++)
1021 av_push(aref, newSVpv(*basenames++, 0));
1023 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1027 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1031 perl_call_sv(scan_ref, G_SCALAR | G_DISCARD);
1035 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1036 err_code = newSVsv(*temp);
1038 temp = hv_fetch(href, "ERR_STR", 7, 1);
1039 err_str = newSVsv(*temp);
1041 temp = hv_fetch(href, "HANDLE", 6, 1);
1042 point = newSVsv(*temp);
1044 temp = hv_fetch(href, "STATUS", 6, 1);
1045 status = newSVsv(*temp);
1047 temp = hv_fetch(href, "NUMBER", 6, 1);
1048 number = newSVsv(*temp);
1050 temp = hv_fetch(href, "ENTRIES", 7, 1);
1051 entries_ref = newSVsv(*temp);
1057 ptr = SvPV(err_str, len);
1058 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1059 strcpy(ODR_errstr, ptr);
1060 rr->errstring = ODR_errstr;
1061 rr->errcode = SvIV(err_code);
1062 rr->num_entries = SvIV(number);
1063 rr->status = SvIV(status);
1064 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1066 entries = (AV *)SvRV(entries_ref);
1067 for (i = 0; i < rr->num_entries; i++)
1069 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1070 temp = hv_fetch(scan_item, "TERM", 4, 1);
1071 ptr = SvPV(*temp, len);
1072 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1073 strcpy(buffer->term, ptr);
1074 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1075 buffer->occurrences = SvIV(*temp);
1077 hv_undef(scan_item);
1079 rr->entries = scan_list;
1080 zhandle->handle = point;
1087 sv_free((SV *)href);
1089 sv_free((SV *)aref);
1091 sv_free((SV *)list);
1093 /*sv_free((SV *)entries);*/
1094 sv_free(entries_ref);
1100 bend_initresult *bend_init(bend_initrequest *q)
1102 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1110 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1118 char *passwd = NULL;
1124 /*q->bend_sort = bend_sort;*/
1127 q->bend_search = bend_search;
1131 q->bend_present = bend_present;
1133 /*q->bend_esrequest = bend_esrequest;*/
1134 /*q->bend_delete = bend_delete;*/
1137 q->bend_fetch = bend_fetch;
1141 q->bend_scan = bend_scan;
1144 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1145 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1146 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1147 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1148 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1149 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1150 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1152 if (q->auth->which == Z_IdAuthentication_open) {
1153 char *openpass = xstrdup (q->auth->u.open);
1154 char *cp = strchr (openpass, '/');
1157 user = nmem_strdup (odr_getmem (q->stream), openpass);
1158 passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1162 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1163 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1168 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1172 if (init_ref != NULL)
1174 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
1179 temp = hv_fetch(href, "IMP_ID", 6, 1);
1180 id = newSVsv(*temp);
1182 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1183 name = newSVsv(*temp);
1185 temp = hv_fetch(href, "IMP_VER", 7, 1);
1186 ver = newSVsv(*temp);
1188 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1189 status = newSVsv(*temp);
1191 temp = hv_fetch(href, "HANDLE", 6, 1);
1192 handle= newSVsv(*temp);
1198 zhandle->handle = handle;
1199 r->errcode = SvIV(status);
1200 r->handle = zhandle;
1201 #if 0 /* implementation_id support is not yet in mainstream Yaz */
1202 ptr = SvPV(id, len);
1203 q->implementation_id = (char *)xmalloc(len + 1);
1204 strcpy(q->implementation_id, ptr);
1206 ptr = SvPV(name, len);
1207 q->implementation_name = (char *)xmalloc(len + 1);
1208 strcpy(q->implementation_name, ptr);
1209 /* q->implementation_name = SvPV(name, len);*/
1210 ptr = SvPV(ver, len);
1211 q->implementation_version = (char *)xmalloc(len + 1);
1212 strcpy(q->implementation_version, ptr);
1218 void bend_close(void *handle)
1221 Zfront_handle *zhandle = (Zfront_handle *)handle;
1228 if (close_ref == NULL)
1234 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1238 XPUSHs(sv_2mortal(newRV((SV *)href)));
1242 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
1256 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1259 set_init_handler(arg)
1262 init_ref = newSVsv(arg);
1266 set_close_handler(arg)
1269 close_ref = newSVsv(arg);
1273 set_sort_handler(arg)
1276 sort_ref = newSVsv(arg);
1279 set_search_handler(arg)
1282 search_ref = newSVsv(arg);
1286 set_fetch_handler(arg)
1289 fetch_ref = newSVsv(arg);
1293 set_present_handler(arg)
1296 present_ref = newSVsv(arg);
1300 set_esrequest_handler(arg)
1303 esrequest_ref = newSVsv(arg);
1307 set_delete_handler(arg)
1310 delete_ref = newSVsv(arg);
1314 set_scan_handler(arg)
1317 scan_ref = newSVsv(arg);
1329 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1331 for (i = 0; i < items; i++)
1333 ptr = SvPV(ST(i), len);
1334 *argv_buf = (char *)xmalloc(len + 1);
1335 strcpy(*argv_buf++, ptr);
1339 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1347 RETVAL = BEND_SCAN_SUCCESS;
1354 RETVAL = BEND_SCAN_PARTIAL;