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.19 2003-09-09 11:40:10 mike
29 /*(Finally!) support implementation-ID
31 /*Revision 1.18 2003/01/03 09:05:41 sondberg
32 /*Applied Dave's GRS-1 patch - actually this was already done in revision 1.17.
34 /*Revision 1.16 2002/11/26 17:09:18 mike
35 /*basic support for idPass authentication
37 /*Revision 1.15 2002/09/16 13:55:53 sondberg
38 /*Added support for authentication into SimpleServer.
40 /*Revision 1.14 2002/03/05 00:34:13 mike
41 /*Support for implementation_id (commented out until it's
44 /*Revision 1.13 2002/02/28 11:21:57 mike
45 /*Add RPN structure to search-handler argument hash.
47 /*Revision 1.12 2001/08/30 14:02:10 sondberg
50 /*Revision 1.11 2001/08/30 13:15:11 sondberg
51 /*Corrected a memory leak, one more to go.
53 /*Revision 1.10 2001/08/29 11:48:36 sondberg
56 /* Net::Z3950::SimpleServer::ScanSuccess
57 /* Net::Z3950::SimpleServer::ScanPartial
59 /*and a bit of documentation.
61 /*Revision 1.9 2001/08/24 14:00:20 sondberg
62 /*Added support for scan.
64 /*Revision 1.8 2001/05/21 11:07:02 sondberg
65 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
67 /*Revision 1.7 2001/03/13 14:17:15 sondberg
68 /*Added support for GRS-1.
75 #include <yaz/backend.h>
77 #include <yaz/wrbuf.h>
82 #define GRS_MAX_FIELDS 500
86 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
87 #define sv_undef PL_sv_undef
105 SV *close_ref = NULL;
107 SV *search_ref = NULL;
108 SV *fetch_ref = NULL;
109 SV *present_ref = NULL;
110 SV *esrequest_ref = NULL;
111 SV *delete_ref = NULL;
115 #define GRS_BUF_SIZE 512
117 Z_GenericRecord *read_grs1(char *str, ODR o)
120 char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
121 char value[GRS_BUF_SIZE+1];
122 Z_GenericRecord *r = 0;
125 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
126 r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
135 ptr = strchr(str, '\n');
140 if (len > GRS_BUF_SIZE) {
141 yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
144 strncpy(line, str, len);
148 while (*buf && isspace(*buf))
151 memmove(original, str, strlen(str));
154 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
156 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
159 if (!type && *value == '0')
161 if (!(buf = strchr(buf, ')')))
164 while (*buf && isspace(*buf))
166 if (r->num_elements >= GRS_MAX_FIELDS)
168 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
171 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
172 t->tagType = (int *)odr_malloc(o, sizeof(int));
174 t->tagValue = (Z_StringOrNumeric *)
175 odr_malloc(o, sizeof(Z_StringOrNumeric));
176 if ((ivalue = atoi(value)))
178 t->tagValue->which = Z_StringOrNumeric_numeric;
179 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
180 *t->tagValue->u.numeric = ivalue;
184 t->tagValue->which = Z_StringOrNumeric_string;
185 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
186 strcpy(t->tagValue->u.string, value);
188 t->tagOccurrence = 0;
190 t->appliedVariant = 0;
191 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
194 c->which = Z_ElementData_subtree;
195 c->u.subtree = read_grs1(str, o);
199 c->which = Z_ElementData_string;
200 c->u.string = odr_strdup(o, buf);
209 static void oid2str(Odr_oid *o, WRBUF buf)
211 for (; *o >= 0; o++) {
213 sprintf(ibuf, "%d", *o);
214 wrbuf_puts(buf, ibuf);
216 wrbuf_putc(buf, '.');
221 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
224 case Z_RPNStructure_simple: {
225 Z_Operand *o = s->u.simple;
228 case Z_Operand_APT: {
229 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
231 if (at->attributes) {
235 for (i = 0; i < at->attributes->num_attributes; i++) {
236 wrbuf_puts(buf, "@attr ");
237 if (at->attributes->attributes[i]->attributeSet) {
238 oid2str(at->attributes->attributes[i]->attributeSet, buf);
239 wrbuf_putc(buf, ' ');
241 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
242 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
243 wrbuf_puts(buf, ibuf);
244 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
245 wrbuf_puts(buf, ibuf);
248 switch (at->term->which) {
249 case Z_Term_general: {
250 wrbuf_putc(buf, '"');
251 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
252 wrbuf_puts(buf, "\" ");
263 case Z_RPNStructure_complex: {
264 Z_Complex *c = s->u.complex;
266 switch (c->roperator->which) {
267 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
268 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
269 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
270 case Z_Operator_prox: abort();
273 if (!rpn2pquery(c->s1, buf))
275 if (!rpn2pquery(c->s2, buf))
285 WRBUF zquery2pquery(Z_Query *q)
287 WRBUF buf = wrbuf_alloc();
289 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
291 if (q->u.type_1->attributeSetId) {
292 /* Output attribute set ID */
293 wrbuf_puts(buf, "@attrset ");
294 oid2str(q->u.type_1->attributeSetId, buf);
295 wrbuf_putc(buf, ' ');
297 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
301 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
303 void fatal(char *fmt, ...)
307 fprintf(stderr, "FATAL (yazwrap): ");
309 vfprintf(stderr, fmt, ap);
311 fprintf(stderr, "\n");
316 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
318 * Creates a new Perl object of type `class'; the newly-created scalar
319 * that is a reference to the blessed thingy `referent' is returned.
321 static SV *newObject(char *class, SV *referent)
326 sv = newRV_noinc((SV*) referent);
327 stash = gv_stashpv(class, 0);
329 fatal("attempt to create object of undefined class '%s'", class);
330 /*assert(stash != 0);*/
336 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
337 static void setMember(HV *hv, char *name, SV *val)
339 /* We don't increment `val's reference count -- I think this is
340 * right because it's created with a refcount of 1, and in fact
341 * the reference via this hash is the only reference to it in
344 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
345 fatal("couldn't store member in hash");
349 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
350 static SV *translateOID(Odr_oid *x)
352 /* Yaz represents an OID by an int array terminated by a negative
353 * value, typically -1; we represent it as a reference to a
354 * blessed scalar string of "."-separated elements.
360 for (i = 0; x[i] >= 0; i++) {
361 sprintf(buf + strlen(buf), "%d", (int) x[i]);
367 * ### We'd like to return a blessed scalar (string) here, but of
368 * course you can't do that in Perl: only references can be
369 * blessed, so we'd have to return a _reference_ to a string, and
370 * bless _that_. Better to do without the blessing, I think.
373 return newSVpv(buf, 0);
375 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
380 static SV *rpn2perl(Z_RPNStructure *s)
387 case Z_RPNStructure_simple: {
388 Z_Operand *o = s->u.simple;
389 Z_AttributesPlusTerm *at;
390 if (o->which != Z_Operand_APT)
391 fatal("can't handle RPN simples other than APT");
392 at = o->u.attributesPlusTerm;
393 if (at->term->which != Z_Term_general)
394 fatal("can't handle RPN terms other than general");
396 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
397 if (at->attributes) {
399 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
400 (SV*) (av = newAV()));
401 for (i = 0; i < at->attributes->num_attributes; i++) {
402 Z_AttributeElement *elem = at->attributes->attributes[i];
404 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
405 (SV*) (hv2 = newHV()));
406 if (elem->attributeSet)
407 setMember(hv2, "attributeSet",
408 translateOID(elem->attributeSet));
409 setMember(hv2, "attributeType",
410 newSViv(*elem->attributeType));
411 assert(elem->which == Z_AttributeValue_numeric);
412 setMember(hv2, "attributeValue",
413 newSViv(*elem->value.numeric));
416 setMember(hv, "attributes", attrs);
418 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
419 at->term->u.general->len));
422 case Z_RPNStructure_complex: {
424 Z_Complex *c = s->u.complex;
425 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
426 switch (c->roperator->which) {
427 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
428 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
429 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
430 case Z_Operator_prox: fatal("proximity not yet supported");
431 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
433 sv = newObject(type, (SV*) (av = newAV()));
434 if ((tmp = rpn2perl(c->s1)) == 0)
437 if ((tmp = rpn2perl(c->s2)) == 0)
442 default: fatal("unknown RPN node type %d", (int) s->which);
449 static SV *zquery2perl(Z_Query *q)
454 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
456 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
457 if (q->u.type_1->attributeSetId)
458 setMember(hv, "attributeSet",
459 translateOID(q->u.type_1->attributeSetId));
460 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
465 int bend_sort(void *handle, bend_sort_rr *rr)
476 char **input_setnames;
477 Zfront_handle *zhandle = (Zfront_handle *)handle;
485 input_setnames = rr->input_setnames;
486 for (i = 0; i < rr->num_input_setnames; i++)
488 av_push(aref, newSVpv(*input_setnames++, 0));
491 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
492 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
493 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
494 hv_store(href, "STATUS", 6, newSViv(0), 0);
498 XPUSHs(sv_2mortal(newRV( (SV*) href)));
502 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
506 temp = hv_fetch(href, "ERR_CODE", 8, 1);
507 err_code = newSVsv(*temp);
509 temp = hv_fetch(href, "ERR_STR", 7, 1);
510 err_str = newSVsv(*temp);
512 temp = hv_fetch(href, "STATUS", 6, 1);
513 status = newSVsv(*temp);
524 rr->errcode = SvIV(err_code);
525 rr->sort_status = SvIV(status);
526 ptr = SvPV(err_str, len);
527 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
528 strcpy(ODR_err_str, ptr);
529 rr->errstring = ODR_err_str;
539 int bend_search(void *handle, bend_search_rr *rr)
556 Zfront_handle *zhandle = (Zfront_handle *)handle;
563 basenames = rr->basenames;
564 for (i = 0; i < rr->num_bases; i++)
566 av_push(aref, newSVpv(*basenames++, 0));
569 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
570 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
571 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
572 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
573 hv_store(href, "HITS", 4, newSViv(0), 0);
574 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
575 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
576 hv_store(href, "PID", 3, newSViv(getpid()), 0);
577 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
578 query = zquery2pquery(rr->query);
581 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
589 XPUSHs(sv_2mortal(newRV( (SV*) href)));
593 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
597 temp = hv_fetch(href, "HITS", 4, 1);
598 hits = newSVsv(*temp);
600 temp = hv_fetch(href, "ERR_CODE", 8, 1);
601 err_code = newSVsv(*temp);
603 temp = hv_fetch(href, "ERR_STR", 7, 1);
604 err_str = newSVsv(*temp);
606 temp = hv_fetch(href, "HANDLE", 6, 1);
607 point = newSVsv(*temp);
615 rr->hits = SvIV(hits);
616 rr->errcode = SvIV(err_code);
617 ptr = SvPV(err_str, len);
618 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
619 strcpy(ODR_errstr, ptr);
620 rr->errstring = ODR_errstr;
621 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
622 memcpy(ODR_point, point, sizeof(*point));
623 zhandle->handle = ODR_point;*/
624 zhandle->handle = point;
629 sv_free( (SV*) aref);
630 sv_free( (SV*) href);
632 wrbuf_free(query, 1);
637 /* ### this is worryingly similar to oid2str() */
638 WRBUF oid2dotted(int *oid)
641 WRBUF buf = wrbuf_alloc();
644 for (; *oid != -1 ; oid++)
649 wrbuf_putc(buf, '.');
655 sprintf(ibuf, "%d", *oid);
656 wrbuf_puts(buf, ibuf);
662 int dotted2oid(char *dotted, int *buffer)
676 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
682 sscanf(ibuf, "%d", oid++);
695 sscanf(ibuf, "%d", oid++);
702 int bend_fetch(void *handle, bend_fetch_rr *rr)
721 Zfront_handle *zhandle = (Zfront_handle *)handle;
723 Z_RecordComposition *composition;
724 Z_ElementSetNames *simple;
733 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
734 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
735 oid_dotted = oid2dotted(rr->request_format_raw);
736 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
737 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
738 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
739 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
740 hv_store(href, "LAST", 4, newSViv(0), 0);
741 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
742 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
743 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
744 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
745 hv_store(href, "PID", 3, newSViv(getpid()), 0);
748 composition = rr->comp;
749 if (composition->which == Z_RecordComp_simple)
751 simple = composition->u.simple;
752 if (simple->which == Z_ElementSetNames_generic)
754 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
769 XPUSHs(sv_2mortal(newRV( (SV*) href)));
773 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
777 temp = hv_fetch(href, "BASENAME", 8, 1);
778 basename = newSVsv(*temp);
780 temp = hv_fetch(href, "RECORD", 6, 1);
781 record = newSVsv(*temp);
783 temp = hv_fetch(href, "LAST", 4, 1);
784 last = newSVsv(*temp);
786 temp = hv_fetch(href, "ERR_CODE", 8, 1);
787 err_code = newSVsv(*temp);
789 temp = hv_fetch(href, "ERR_STR", 7, 1),
790 err_string = newSVsv(*temp);
792 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
793 sur_flag = newSVsv(*temp);
795 temp = hv_fetch(href, "REP_FORM", 8, 1);
796 rep_form = newSVsv(*temp);
798 temp = hv_fetch(href, "HANDLE", 6, 1);
799 point = newSVsv(*temp);
807 ptr = SvPV(basename, length);
808 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
809 strcpy(ODR_basename, ptr);
810 rr->basename = ODR_basename;
812 ptr = SvPV(rep_form, length);
813 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
814 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
816 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
818 rr->output_format_raw = ODR_oid_buf;
820 ptr = SvPV(record, length);
821 oid = oid_getentbyoid(ODR_oid_buf);
822 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
824 rr->record = (char *) read_grs1(ptr, rr->stream);
829 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
830 strcpy(ODR_record, ptr);
831 rr->record = ODR_record;
834 zhandle->handle = point;
836 rr->last_in_set = SvIV(last);
840 rr->errcode = SvIV(err_code);
841 ptr = SvPV(err_string, length);
842 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
843 strcpy(ODR_errstr, ptr);
844 rr->errstring = ODR_errstr;
846 rr->surrogate_flag = SvIV(sur_flag);
848 wrbuf_free(oid_dotted, 1);
862 int bend_present(void *handle, bend_present_rr *rr)
872 Z_RecordComposition *composition;
873 Z_ElementSetNames *simple;
876 Zfront_handle *zhandle = (Zfront_handle *)handle;
878 /* WRBUF oid_dotted; */
885 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
886 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
887 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
888 hv_store(href, "START", 5, newSViv(rr->start), 0);
889 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
890 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
891 /*oid_dotted = oid2dotted(rr->request_format_raw);
892 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
893 hv_store(href, "HITS", 4, newSViv(0), 0);
894 hv_store(href, "PID", 3, newSViv(getpid()), 0);
897 composition = rr->comp;
898 if (composition->which == Z_RecordComp_simple)
900 simple = composition->u.simple;
901 if (simple->which == Z_ElementSetNames_generic)
903 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
920 XPUSHs(sv_2mortal(newRV( (SV*) href)));
924 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
928 temp = hv_fetch(href, "ERR_CODE", 8, 1);
929 err_code = newSVsv(*temp);
931 temp = hv_fetch(href, "ERR_STR", 7, 1);
932 err_string = newSVsv(*temp);
934 temp = hv_fetch(href, "HITS", 4, 1);
935 hits = newSVsv(*temp);
937 temp = hv_fetch(href, "HANDLE", 6, 1);
938 point = newSVsv(*temp);
945 rr->errcode = SvIV(err_code);
946 rr->hits = SvIV(hits);
948 ptr = SvPV(err_string, len);
949 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
950 strcpy(ODR_errstr, ptr);
951 rr->errstring = ODR_errstr;
952 /* wrbuf_free(oid_dotted, 1);*/
953 zhandle->handle = point;
958 sv_free( (SV*) href);
964 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
966 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
971 int bend_delete(void *handle, bend_delete_rr *rr)
973 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
978 int bend_scan(void *handle, bend_scan_rr *rr)
985 struct scan_entry *scan_list;
986 struct scan_entry *buffer;
987 int *step_size = rr->step_size;
991 SV *err_code = sv_newmortal();
992 SV *err_str = sv_newmortal();
993 SV *point = sv_newmortal();
994 SV *status = sv_newmortal();
995 SV *number = sv_newmortal();
1003 Zfront_handle *zhandle = (Zfront_handle *)handle;
1010 if (rr->term->term->which == Z_Term_general)
1012 term_len = rr->term->term->u.general->len;
1013 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1015 rr->errcode = 229; /* Unsupported term type */
1018 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1019 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1020 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1021 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1022 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1023 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1024 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1025 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1027 basenames = rr->basenames;
1028 for (i = 0; i < rr->num_bases; i++)
1030 av_push(aref, newSVpv(*basenames++, 0));
1032 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1036 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1040 perl_call_sv(scan_ref, G_SCALAR | G_DISCARD);
1044 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1045 err_code = newSVsv(*temp);
1047 temp = hv_fetch(href, "ERR_STR", 7, 1);
1048 err_str = newSVsv(*temp);
1050 temp = hv_fetch(href, "HANDLE", 6, 1);
1051 point = newSVsv(*temp);
1053 temp = hv_fetch(href, "STATUS", 6, 1);
1054 status = newSVsv(*temp);
1056 temp = hv_fetch(href, "NUMBER", 6, 1);
1057 number = newSVsv(*temp);
1059 temp = hv_fetch(href, "ENTRIES", 7, 1);
1060 entries_ref = newSVsv(*temp);
1066 ptr = SvPV(err_str, len);
1067 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1068 strcpy(ODR_errstr, ptr);
1069 rr->errstring = ODR_errstr;
1070 rr->errcode = SvIV(err_code);
1071 rr->num_entries = SvIV(number);
1072 rr->status = SvIV(status);
1073 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1075 entries = (AV *)SvRV(entries_ref);
1076 for (i = 0; i < rr->num_entries; i++)
1078 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1079 temp = hv_fetch(scan_item, "TERM", 4, 1);
1080 ptr = SvPV(*temp, len);
1081 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1082 strcpy(buffer->term, ptr);
1083 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1084 buffer->occurrences = SvIV(*temp);
1086 hv_undef(scan_item);
1088 rr->entries = scan_list;
1089 zhandle->handle = point;
1096 sv_free((SV *)href);
1098 sv_free((SV *)aref);
1100 sv_free((SV *)list);
1102 /*sv_free((SV *)entries);*/
1103 sv_free(entries_ref);
1109 bend_initresult *bend_init(bend_initrequest *q)
1111 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1119 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1127 char *passwd = NULL;
1133 /*q->bend_sort = bend_sort;*/
1136 q->bend_search = bend_search;
1140 q->bend_present = bend_present;
1142 /*q->bend_esrequest = bend_esrequest;*/
1143 /*q->bend_delete = bend_delete;*/
1146 q->bend_fetch = bend_fetch;
1150 q->bend_scan = bend_scan;
1153 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1154 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1155 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1156 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1157 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1158 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1159 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1161 if (q->auth->which == Z_IdAuthentication_open) {
1162 char *openpass = xstrdup (q->auth->u.open);
1163 char *cp = strchr (openpass, '/');
1166 user = nmem_strdup (odr_getmem (q->stream), openpass);
1167 passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1170 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1171 user = q->auth->u.idPass->userId;
1172 passwd = q->auth->u.idPass->password;
1174 /* ### some code paths have user/password unassigned here */
1175 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1176 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1181 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1185 if (init_ref != NULL)
1187 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
1192 temp = hv_fetch(href, "IMP_ID", 6, 1);
1193 id = newSVsv(*temp);
1195 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1196 name = newSVsv(*temp);
1198 temp = hv_fetch(href, "IMP_VER", 7, 1);
1199 ver = newSVsv(*temp);
1201 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1202 status = newSVsv(*temp);
1204 temp = hv_fetch(href, "HANDLE", 6, 1);
1205 handle= newSVsv(*temp);
1211 zhandle->handle = handle;
1212 r->errcode = SvIV(status);
1213 r->handle = zhandle;
1214 ptr = SvPV(id, len);
1215 q->implementation_id = (char *)xmalloc(len + 1);
1216 strcpy(q->implementation_id, ptr);
1217 ptr = SvPV(name, len);
1218 q->implementation_name = (char *)xmalloc(len + 1);
1219 strcpy(q->implementation_name, ptr);
1220 /* q->implementation_name = SvPV(name, len);*/
1221 ptr = SvPV(ver, len);
1222 q->implementation_version = (char *)xmalloc(len + 1);
1223 strcpy(q->implementation_version, ptr);
1229 void bend_close(void *handle)
1232 Zfront_handle *zhandle = (Zfront_handle *)handle;
1239 if (close_ref == NULL)
1245 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1249 XPUSHs(sv_2mortal(newRV((SV *)href)));
1253 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
1267 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1270 set_init_handler(arg)
1273 init_ref = newSVsv(arg);
1277 set_close_handler(arg)
1280 close_ref = newSVsv(arg);
1284 set_sort_handler(arg)
1287 sort_ref = newSVsv(arg);
1290 set_search_handler(arg)
1293 search_ref = newSVsv(arg);
1297 set_fetch_handler(arg)
1300 fetch_ref = newSVsv(arg);
1304 set_present_handler(arg)
1307 present_ref = newSVsv(arg);
1311 set_esrequest_handler(arg)
1314 esrequest_ref = newSVsv(arg);
1318 set_delete_handler(arg)
1321 delete_ref = newSVsv(arg);
1325 set_scan_handler(arg)
1328 scan_ref = newSVsv(arg);
1340 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1342 for (i = 0; i < items; i++)
1344 ptr = SvPV(ST(i), len);
1345 *argv_buf = (char *)xmalloc(len + 1);
1346 strcpy(*argv_buf++, ptr);
1350 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1358 RETVAL = BEND_SCAN_SUCCESS;
1365 RETVAL = BEND_SCAN_PARTIAL;