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.16 2002-11-26 17:09:18 mike
29 /*basic support for idPass authentication
31 /*Revision 1.15 2002/09/16 13:55:53 sondberg
32 /*Added support for authentication into SimpleServer.
34 /*Revision 1.14 2002/03/05 00:34:13 mike
35 /*Support for implementation_id (commented out until it's
38 /*Revision 1.13 2002/02/28 11:21:57 mike
39 /*Add RPN structure to search-handler argument hash.
41 /*Revision 1.12 2001/08/30 14:02:10 sondberg
44 /*Revision 1.11 2001/08/30 13:15:11 sondberg
45 /*Corrected a memory leak, one more to go.
47 /*Revision 1.10 2001/08/29 11:48:36 sondberg
50 /* Net::Z3950::SimpleServer::ScanSuccess
51 /* Net::Z3950::SimpleServer::ScanPartial
53 /*and a bit of documentation.
55 /*Revision 1.9 2001/08/24 14:00:20 sondberg
56 /*Added support for scan.
58 /*Revision 1.8 2001/05/21 11:07:02 sondberg
59 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
61 /*Revision 1.7 2001/03/13 14:17:15 sondberg
62 /*Added support for GRS-1.
69 #include <yaz/backend.h>
71 #include <yaz/wrbuf.h>
76 #define GRS_MAX_FIELDS 500
80 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
81 #define sv_undef PL_sv_undef
101 SV *search_ref = NULL;
102 SV *fetch_ref = NULL;
103 SV *present_ref = NULL;
104 SV *esrequest_ref = NULL;
105 SV *delete_ref = NULL;
110 Z_GenericRecord *read_grs1(char *str, ODR o)
113 char line[512], *buf, *ptr, *original;
115 Z_GenericRecord *r = 0;
123 ptr = strchr(str, '\n');
127 strncpy(line, str, ptr - str);
131 while (*buf && isspace(*buf))
134 memmove(original, str, strlen(str));
137 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
139 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
142 if (!type && *value == '0')
144 if (!(buf = strchr(buf, ')')))
147 while (*buf && isspace(*buf))
153 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
154 r->elements = (Z_TaggedElement **)
155 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
158 if (r->num_elements > GRS_MAX_FIELDS)
160 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
163 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
164 t->tagType = (int *)odr_malloc(o, sizeof(int));
166 t->tagValue = (Z_StringOrNumeric *)
167 odr_malloc(o, sizeof(Z_StringOrNumeric));
168 if ((ivalue = atoi(value)))
170 t->tagValue->which = Z_StringOrNumeric_numeric;
171 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
172 *t->tagValue->u.numeric = ivalue;
176 t->tagValue->which = Z_StringOrNumeric_string;
177 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
178 strcpy(t->tagValue->u.string, value);
180 t->tagOccurrence = 0;
182 t->appliedVariant = 0;
183 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
186 c->which = Z_ElementData_subtree;
187 c->u.subtree = read_grs1(str, o);
191 c->which = Z_ElementData_string;
192 /* buf[strlen(buf)-1] = '\0';*/
193 buf[strlen(buf)] = '\0';
194 c->u.string = odr_strdup(o, buf);
203 static void oid2str(Odr_oid *o, WRBUF buf)
205 for (; *o >= 0; o++) {
207 sprintf(ibuf, "%d", *o);
208 wrbuf_puts(buf, ibuf);
210 wrbuf_putc(buf, '.');
215 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
218 case Z_RPNStructure_simple: {
219 Z_Operand *o = s->u.simple;
222 case Z_Operand_APT: {
223 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
225 if (at->attributes) {
229 for (i = 0; i < at->attributes->num_attributes; i++) {
230 wrbuf_puts(buf, "@attr ");
231 if (at->attributes->attributes[i]->attributeSet) {
232 oid2str(at->attributes->attributes[i]->attributeSet, buf);
233 wrbuf_putc(buf, ' ');
235 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
236 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
237 wrbuf_puts(buf, ibuf);
238 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
239 wrbuf_puts(buf, ibuf);
242 switch (at->term->which) {
243 case Z_Term_general: {
244 wrbuf_putc(buf, '"');
245 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
246 wrbuf_puts(buf, "\" ");
257 case Z_RPNStructure_complex: {
258 Z_Complex *c = s->u.complex;
260 switch (c->roperator->which) {
261 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
262 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
263 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
264 case Z_Operator_prox: abort();
267 if (!rpn2pquery(c->s1, buf))
269 if (!rpn2pquery(c->s2, buf))
279 WRBUF zquery2pquery(Z_Query *q)
281 WRBUF buf = wrbuf_alloc();
283 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
285 if (q->u.type_1->attributeSetId) {
286 /* Output attribute set ID */
287 wrbuf_puts(buf, "@attrset ");
288 oid2str(q->u.type_1->attributeSetId, buf);
289 wrbuf_putc(buf, ' ');
291 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
295 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
297 void fatal(char *fmt, ...)
301 fprintf(stderr, "FATAL (yazwrap): ");
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_APT)
385 fatal("can't handle RPN simples other than APT");
386 at = o->u.attributesPlusTerm;
387 if (at->term->which != Z_Term_general)
388 fatal("can't handle RPN terms other than general");
390 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
391 if (at->attributes) {
393 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
394 (SV*) (av = newAV()));
395 for (i = 0; i < at->attributes->num_attributes; i++) {
396 Z_AttributeElement *elem = at->attributes->attributes[i];
398 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
399 (SV*) (hv2 = newHV()));
400 if (elem->attributeSet)
401 setMember(hv2, "attributeSet",
402 translateOID(elem->attributeSet));
403 setMember(hv2, "attributeType",
404 newSViv(*elem->attributeType));
405 assert(elem->which == Z_AttributeValue_numeric);
406 setMember(hv2, "attributeValue",
407 newSViv(*elem->value.numeric));
410 setMember(hv, "attributes", attrs);
412 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
413 at->term->u.general->len));
416 case Z_RPNStructure_complex: {
418 Z_Complex *c = s->u.complex;
419 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
420 switch (c->roperator->which) {
421 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
422 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
423 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
424 case Z_Operator_prox: fatal("proximity not yet supported");
425 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
427 sv = newObject(type, (SV*) (av = newAV()));
428 if ((tmp = rpn2perl(c->s1)) == 0)
431 if ((tmp = rpn2perl(c->s2)) == 0)
436 default: fatal("unknown RPN node type %d", (int) s->which);
443 static SV *zquery2perl(Z_Query *q)
448 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
450 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
451 if (q->u.type_1->attributeSetId)
452 setMember(hv, "attributeSet",
453 translateOID(q->u.type_1->attributeSetId));
454 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
459 int bend_sort(void *handle, bend_sort_rr *rr)
470 char **input_setnames;
471 Zfront_handle *zhandle = (Zfront_handle *)handle;
479 input_setnames = rr->input_setnames;
480 for (i = 0; i < rr->num_input_setnames; i++)
482 av_push(aref, newSVpv(*input_setnames++, 0));
485 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
486 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
487 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
488 hv_store(href, "STATUS", 6, newSViv(0), 0);
492 XPUSHs(sv_2mortal(newRV( (SV*) href)));
496 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
500 temp = hv_fetch(href, "ERR_CODE", 8, 1);
501 err_code = newSVsv(*temp);
503 temp = hv_fetch(href, "ERR_STR", 7, 1);
504 err_str = newSVsv(*temp);
506 temp = hv_fetch(href, "STATUS", 6, 1);
507 status = newSVsv(*temp);
518 rr->errcode = SvIV(err_code);
519 rr->sort_status = SvIV(status);
520 ptr = SvPV(err_str, len);
521 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
522 strcpy(ODR_err_str, ptr);
523 rr->errstring = ODR_err_str;
533 int bend_search(void *handle, bend_search_rr *rr)
550 Zfront_handle *zhandle = (Zfront_handle *)handle;
557 basenames = rr->basenames;
558 for (i = 0; i < rr->num_bases; i++)
560 av_push(aref, newSVpv(*basenames++, 0));
563 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
564 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
565 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
566 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
567 hv_store(href, "HITS", 4, newSViv(0), 0);
568 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
569 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
570 hv_store(href, "PID", 3, newSViv(getpid()), 0);
571 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
572 query = zquery2pquery(rr->query);
575 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
583 XPUSHs(sv_2mortal(newRV( (SV*) href)));
587 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
591 temp = hv_fetch(href, "HITS", 4, 1);
592 hits = newSVsv(*temp);
594 temp = hv_fetch(href, "ERR_CODE", 8, 1);
595 err_code = newSVsv(*temp);
597 temp = hv_fetch(href, "ERR_STR", 7, 1);
598 err_str = newSVsv(*temp);
600 temp = hv_fetch(href, "HANDLE", 6, 1);
601 point = newSVsv(*temp);
609 rr->hits = SvIV(hits);
610 rr->errcode = SvIV(err_code);
611 ptr = SvPV(err_str, len);
612 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
613 strcpy(ODR_errstr, ptr);
614 rr->errstring = ODR_errstr;
615 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
616 memcpy(ODR_point, point, sizeof(*point));
617 zhandle->handle = ODR_point;*/
618 zhandle->handle = point;
623 sv_free( (SV*) aref);
624 sv_free( (SV*) href);
626 wrbuf_free(query, 1);
631 /* ### this is worryingly similar to oid2str() */
632 WRBUF oid2dotted(int *oid)
635 WRBUF buf = wrbuf_alloc();
638 for (; *oid != -1 ; oid++)
643 wrbuf_putc(buf, '.');
649 sprintf(ibuf, "%d", *oid);
650 wrbuf_puts(buf, ibuf);
656 int dotted2oid(char *dotted, int *buffer)
670 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
676 sscanf(ibuf, "%d", oid++);
689 sscanf(ibuf, "%d", oid++);
696 int bend_fetch(void *handle, bend_fetch_rr *rr)
715 Zfront_handle *zhandle = (Zfront_handle *)handle;
717 Z_RecordComposition *composition;
718 Z_ElementSetNames *simple;
727 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
728 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
729 oid_dotted = oid2dotted(rr->request_format_raw);
730 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
731 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
732 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
733 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
734 hv_store(href, "LAST", 4, newSViv(0), 0);
735 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
736 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
737 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
738 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
739 hv_store(href, "PID", 3, newSViv(getpid()), 0);
742 composition = rr->comp;
743 if (composition->which == Z_RecordComp_simple)
745 simple = composition->u.simple;
746 if (simple->which == Z_ElementSetNames_generic)
748 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
763 XPUSHs(sv_2mortal(newRV( (SV*) href)));
767 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
771 temp = hv_fetch(href, "BASENAME", 8, 1);
772 basename = newSVsv(*temp);
774 temp = hv_fetch(href, "RECORD", 6, 1);
775 record = newSVsv(*temp);
777 temp = hv_fetch(href, "LAST", 4, 1);
778 last = newSVsv(*temp);
780 temp = hv_fetch(href, "ERR_CODE", 8, 1);
781 err_code = newSVsv(*temp);
783 temp = hv_fetch(href, "ERR_STR", 7, 1),
784 err_string = newSVsv(*temp);
786 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
787 sur_flag = newSVsv(*temp);
789 temp = hv_fetch(href, "REP_FORM", 8, 1);
790 rep_form = newSVsv(*temp);
792 temp = hv_fetch(href, "HANDLE", 6, 1);
793 point = newSVsv(*temp);
801 ptr = SvPV(basename, length);
802 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
803 strcpy(ODR_basename, ptr);
804 rr->basename = ODR_basename;
806 ptr = SvPV(rep_form, length);
807 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
808 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
810 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
812 rr->output_format_raw = ODR_oid_buf;
814 ptr = SvPV(record, length);
815 oid = oid_getentbyoid(ODR_oid_buf);
816 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
818 rr->record = (char *) read_grs1(ptr, rr->stream);
823 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
824 strcpy(ODR_record, ptr);
825 rr->record = ODR_record;
828 zhandle->handle = point;
830 rr->last_in_set = SvIV(last);
834 rr->errcode = SvIV(err_code);
835 ptr = SvPV(err_string, length);
836 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
837 strcpy(ODR_errstr, ptr);
838 rr->errstring = ODR_errstr;
840 rr->surrogate_flag = SvIV(sur_flag);
842 wrbuf_free(oid_dotted, 1);
856 int bend_present(void *handle, bend_present_rr *rr)
866 Z_RecordComposition *composition;
867 Z_ElementSetNames *simple;
870 Zfront_handle *zhandle = (Zfront_handle *)handle;
872 /* WRBUF oid_dotted; */
879 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
880 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
881 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
882 hv_store(href, "START", 5, newSViv(rr->start), 0);
883 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
884 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
885 /*oid_dotted = oid2dotted(rr->request_format_raw);
886 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
887 hv_store(href, "HITS", 4, newSViv(0), 0);
888 hv_store(href, "PID", 3, newSViv(getpid()), 0);
891 composition = rr->comp;
892 if (composition->which == Z_RecordComp_simple)
894 simple = composition->u.simple;
895 if (simple->which == Z_ElementSetNames_generic)
897 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
914 XPUSHs(sv_2mortal(newRV( (SV*) href)));
918 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
922 temp = hv_fetch(href, "ERR_CODE", 8, 1);
923 err_code = newSVsv(*temp);
925 temp = hv_fetch(href, "ERR_STR", 7, 1);
926 err_string = newSVsv(*temp);
928 temp = hv_fetch(href, "HITS", 4, 1);
929 hits = newSVsv(*temp);
931 temp = hv_fetch(href, "HANDLE", 6, 1);
932 point = newSVsv(*temp);
939 rr->errcode = SvIV(err_code);
940 rr->hits = SvIV(hits);
942 ptr = SvPV(err_string, len);
943 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
944 strcpy(ODR_errstr, ptr);
945 rr->errstring = ODR_errstr;
946 /* wrbuf_free(oid_dotted, 1);*/
947 zhandle->handle = point;
952 sv_free( (SV*) href);
958 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
960 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
965 int bend_delete(void *handle, bend_delete_rr *rr)
967 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
972 int bend_scan(void *handle, bend_scan_rr *rr)
979 struct scan_entry *scan_list;
980 struct scan_entry *buffer;
981 int *step_size = rr->step_size;
985 SV *err_code = sv_newmortal();
986 SV *err_str = sv_newmortal();
987 SV *point = sv_newmortal();
988 SV *status = sv_newmortal();
989 SV *number = sv_newmortal();
997 Zfront_handle *zhandle = (Zfront_handle *)handle;
1004 if (rr->term->term->which == Z_Term_general)
1006 term_len = rr->term->term->u.general->len;
1007 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1009 rr->errcode = 229; /* Unsupported term type */
1012 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1013 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1014 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1015 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1016 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1017 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1018 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1019 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1021 basenames = rr->basenames;
1022 for (i = 0; i < rr->num_bases; i++)
1024 av_push(aref, newSVpv(*basenames++, 0));
1026 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1030 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1034 perl_call_sv(scan_ref, G_SCALAR | G_DISCARD);
1038 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1039 err_code = newSVsv(*temp);
1041 temp = hv_fetch(href, "ERR_STR", 7, 1);
1042 err_str = newSVsv(*temp);
1044 temp = hv_fetch(href, "HANDLE", 6, 1);
1045 point = newSVsv(*temp);
1047 temp = hv_fetch(href, "STATUS", 6, 1);
1048 status = newSVsv(*temp);
1050 temp = hv_fetch(href, "NUMBER", 6, 1);
1051 number = newSVsv(*temp);
1053 temp = hv_fetch(href, "ENTRIES", 7, 1);
1054 entries_ref = newSVsv(*temp);
1060 ptr = SvPV(err_str, len);
1061 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1062 strcpy(ODR_errstr, ptr);
1063 rr->errstring = ODR_errstr;
1064 rr->errcode = SvIV(err_code);
1065 rr->num_entries = SvIV(number);
1066 rr->status = SvIV(status);
1067 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1069 entries = (AV *)SvRV(entries_ref);
1070 for (i = 0; i < rr->num_entries; i++)
1072 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1073 temp = hv_fetch(scan_item, "TERM", 4, 1);
1074 ptr = SvPV(*temp, len);
1075 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1076 strcpy(buffer->term, ptr);
1077 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1078 buffer->occurrences = SvIV(*temp);
1080 hv_undef(scan_item);
1082 rr->entries = scan_list;
1083 zhandle->handle = point;
1090 sv_free((SV *)href);
1092 sv_free((SV *)aref);
1094 sv_free((SV *)list);
1096 /*sv_free((SV *)entries);*/
1097 sv_free(entries_ref);
1103 bend_initresult *bend_init(bend_initrequest *q)
1105 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1113 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1121 char *passwd = NULL;
1127 /*q->bend_sort = bend_sort;*/
1130 q->bend_search = bend_search;
1134 q->bend_present = bend_present;
1136 /*q->bend_esrequest = bend_esrequest;*/
1137 /*q->bend_delete = bend_delete;*/
1140 q->bend_fetch = bend_fetch;
1144 q->bend_scan = bend_scan;
1147 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1148 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1149 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1150 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1151 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1152 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1153 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1155 if (q->auth->which == Z_IdAuthentication_open) {
1156 char *openpass = xstrdup (q->auth->u.open);
1157 char *cp = strchr (openpass, '/');
1160 user = nmem_strdup (odr_getmem (q->stream), openpass);
1161 passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1164 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1165 user = q->auth->u.idPass->userId;
1166 passwd = q->auth->u.idPass->password;
1168 /* ### some code paths have user/password unassigned here */
1169 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1170 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1175 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1179 if (init_ref != NULL)
1181 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
1186 temp = hv_fetch(href, "IMP_ID", 6, 1);
1187 id = newSVsv(*temp);
1189 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1190 name = newSVsv(*temp);
1192 temp = hv_fetch(href, "IMP_VER", 7, 1);
1193 ver = newSVsv(*temp);
1195 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1196 status = newSVsv(*temp);
1198 temp = hv_fetch(href, "HANDLE", 6, 1);
1199 handle= newSVsv(*temp);
1205 zhandle->handle = handle;
1206 r->errcode = SvIV(status);
1207 r->handle = zhandle;
1208 #if 0 /* implementation_id support is not yet in mainstream Yaz */
1209 ptr = SvPV(id, len);
1210 q->implementation_id = (char *)xmalloc(len + 1);
1211 strcpy(q->implementation_id, ptr);
1213 ptr = SvPV(name, len);
1214 q->implementation_name = (char *)xmalloc(len + 1);
1215 strcpy(q->implementation_name, ptr);
1216 /* q->implementation_name = SvPV(name, len);*/
1217 ptr = SvPV(ver, len);
1218 q->implementation_version = (char *)xmalloc(len + 1);
1219 strcpy(q->implementation_version, ptr);
1225 void bend_close(void *handle)
1228 Zfront_handle *zhandle = (Zfront_handle *)handle;
1235 if (close_ref == NULL)
1241 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1245 XPUSHs(sv_2mortal(newRV((SV *)href)));
1249 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
1263 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1266 set_init_handler(arg)
1269 init_ref = newSVsv(arg);
1273 set_close_handler(arg)
1276 close_ref = newSVsv(arg);
1280 set_sort_handler(arg)
1283 sort_ref = newSVsv(arg);
1286 set_search_handler(arg)
1289 search_ref = newSVsv(arg);
1293 set_fetch_handler(arg)
1296 fetch_ref = newSVsv(arg);
1300 set_present_handler(arg)
1303 present_ref = newSVsv(arg);
1307 set_esrequest_handler(arg)
1310 esrequest_ref = newSVsv(arg);
1314 set_delete_handler(arg)
1317 delete_ref = newSVsv(arg);
1321 set_scan_handler(arg)
1324 scan_ref = newSVsv(arg);
1336 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1338 for (i = 0; i < items; i++)
1340 ptr = SvPV(ST(i), len);
1341 *argv_buf = (char *)xmalloc(len + 1);
1342 strcpy(*argv_buf++, ptr);
1346 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1354 RETVAL = BEND_SCAN_SUCCESS;
1361 RETVAL = BEND_SCAN_PARTIAL;