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.18 2003-01-03 09:05:41 sondberg
29 /*Applied Dave's GRS-1 patch - actually this was already done in revision 1.17.
31 /*Revision 1.16 2002/11/26 17:09:18 mike
32 /*basic support for idPass authentication
34 /*Revision 1.15 2002/09/16 13:55:53 sondberg
35 /*Added support for authentication into SimpleServer.
37 /*Revision 1.14 2002/03/05 00:34:13 mike
38 /*Support for implementation_id (commented out until it's
41 /*Revision 1.13 2002/02/28 11:21:57 mike
42 /*Add RPN structure to search-handler argument hash.
44 /*Revision 1.12 2001/08/30 14:02:10 sondberg
47 /*Revision 1.11 2001/08/30 13:15:11 sondberg
48 /*Corrected a memory leak, one more to go.
50 /*Revision 1.10 2001/08/29 11:48:36 sondberg
53 /* Net::Z3950::SimpleServer::ScanSuccess
54 /* Net::Z3950::SimpleServer::ScanPartial
56 /*and a bit of documentation.
58 /*Revision 1.9 2001/08/24 14:00:20 sondberg
59 /*Added support for scan.
61 /*Revision 1.8 2001/05/21 11:07:02 sondberg
62 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
64 /*Revision 1.7 2001/03/13 14:17:15 sondberg
65 /*Added support for GRS-1.
72 #include <yaz/backend.h>
74 #include <yaz/wrbuf.h>
79 #define GRS_MAX_FIELDS 500
83 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
84 #define sv_undef PL_sv_undef
102 SV *close_ref = NULL;
104 SV *search_ref = NULL;
105 SV *fetch_ref = NULL;
106 SV *present_ref = NULL;
107 SV *esrequest_ref = NULL;
108 SV *delete_ref = NULL;
112 #define GRS_BUF_SIZE 512
114 Z_GenericRecord *read_grs1(char *str, ODR o)
117 char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
118 char value[GRS_BUF_SIZE+1];
119 Z_GenericRecord *r = 0;
122 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
123 r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
132 ptr = strchr(str, '\n');
137 if (len > GRS_BUF_SIZE) {
138 yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
141 strncpy(line, str, len);
145 while (*buf && isspace(*buf))
148 memmove(original, str, strlen(str));
151 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
153 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
156 if (!type && *value == '0')
158 if (!(buf = strchr(buf, ')')))
161 while (*buf && isspace(*buf))
163 if (r->num_elements >= GRS_MAX_FIELDS)
165 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
168 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
169 t->tagType = (int *)odr_malloc(o, sizeof(int));
171 t->tagValue = (Z_StringOrNumeric *)
172 odr_malloc(o, sizeof(Z_StringOrNumeric));
173 if ((ivalue = atoi(value)))
175 t->tagValue->which = Z_StringOrNumeric_numeric;
176 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
177 *t->tagValue->u.numeric = ivalue;
181 t->tagValue->which = Z_StringOrNumeric_string;
182 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
183 strcpy(t->tagValue->u.string, value);
185 t->tagOccurrence = 0;
187 t->appliedVariant = 0;
188 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
191 c->which = Z_ElementData_subtree;
192 c->u.subtree = read_grs1(str, o);
196 c->which = Z_ElementData_string;
197 c->u.string = odr_strdup(o, buf);
206 static void oid2str(Odr_oid *o, WRBUF buf)
208 for (; *o >= 0; o++) {
210 sprintf(ibuf, "%d", *o);
211 wrbuf_puts(buf, ibuf);
213 wrbuf_putc(buf, '.');
218 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
221 case Z_RPNStructure_simple: {
222 Z_Operand *o = s->u.simple;
225 case Z_Operand_APT: {
226 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
228 if (at->attributes) {
232 for (i = 0; i < at->attributes->num_attributes; i++) {
233 wrbuf_puts(buf, "@attr ");
234 if (at->attributes->attributes[i]->attributeSet) {
235 oid2str(at->attributes->attributes[i]->attributeSet, buf);
236 wrbuf_putc(buf, ' ');
238 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
239 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
240 wrbuf_puts(buf, ibuf);
241 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
242 wrbuf_puts(buf, ibuf);
245 switch (at->term->which) {
246 case Z_Term_general: {
247 wrbuf_putc(buf, '"');
248 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
249 wrbuf_puts(buf, "\" ");
260 case Z_RPNStructure_complex: {
261 Z_Complex *c = s->u.complex;
263 switch (c->roperator->which) {
264 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
265 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
266 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
267 case Z_Operator_prox: abort();
270 if (!rpn2pquery(c->s1, buf))
272 if (!rpn2pquery(c->s2, buf))
282 WRBUF zquery2pquery(Z_Query *q)
284 WRBUF buf = wrbuf_alloc();
286 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
288 if (q->u.type_1->attributeSetId) {
289 /* Output attribute set ID */
290 wrbuf_puts(buf, "@attrset ");
291 oid2str(q->u.type_1->attributeSetId, buf);
292 wrbuf_putc(buf, ' ');
294 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
298 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
300 void fatal(char *fmt, ...)
304 fprintf(stderr, "FATAL (yazwrap): ");
306 vfprintf(stderr, fmt, ap);
308 fprintf(stderr, "\n");
313 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
315 * Creates a new Perl object of type `class'; the newly-created scalar
316 * that is a reference to the blessed thingy `referent' is returned.
318 static SV *newObject(char *class, SV *referent)
323 sv = newRV_noinc((SV*) referent);
324 stash = gv_stashpv(class, 0);
326 fatal("attempt to create object of undefined class '%s'", class);
327 /*assert(stash != 0);*/
333 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
334 static void setMember(HV *hv, char *name, SV *val)
336 /* We don't increment `val's reference count -- I think this is
337 * right because it's created with a refcount of 1, and in fact
338 * the reference via this hash is the only reference to it in
341 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
342 fatal("couldn't store member in hash");
346 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
347 static SV *translateOID(Odr_oid *x)
349 /* Yaz represents an OID by an int array terminated by a negative
350 * value, typically -1; we represent it as a reference to a
351 * blessed scalar string of "."-separated elements.
357 for (i = 0; x[i] >= 0; i++) {
358 sprintf(buf + strlen(buf), "%d", (int) x[i]);
364 * ### We'd like to return a blessed scalar (string) here, but of
365 * course you can't do that in Perl: only references can be
366 * blessed, so we'd have to return a _reference_ to a string, and
367 * bless _that_. Better to do without the blessing, I think.
370 return newSVpv(buf, 0);
372 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
377 static SV *rpn2perl(Z_RPNStructure *s)
384 case Z_RPNStructure_simple: {
385 Z_Operand *o = s->u.simple;
386 Z_AttributesPlusTerm *at;
387 if (o->which != Z_Operand_APT)
388 fatal("can't handle RPN simples other than APT");
389 at = o->u.attributesPlusTerm;
390 if (at->term->which != Z_Term_general)
391 fatal("can't handle RPN terms other than general");
393 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
394 if (at->attributes) {
396 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
397 (SV*) (av = newAV()));
398 for (i = 0; i < at->attributes->num_attributes; i++) {
399 Z_AttributeElement *elem = at->attributes->attributes[i];
401 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
402 (SV*) (hv2 = newHV()));
403 if (elem->attributeSet)
404 setMember(hv2, "attributeSet",
405 translateOID(elem->attributeSet));
406 setMember(hv2, "attributeType",
407 newSViv(*elem->attributeType));
408 assert(elem->which == Z_AttributeValue_numeric);
409 setMember(hv2, "attributeValue",
410 newSViv(*elem->value.numeric));
413 setMember(hv, "attributes", attrs);
415 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
416 at->term->u.general->len));
419 case Z_RPNStructure_complex: {
421 Z_Complex *c = s->u.complex;
422 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
423 switch (c->roperator->which) {
424 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
425 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
426 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
427 case Z_Operator_prox: fatal("proximity not yet supported");
428 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
430 sv = newObject(type, (SV*) (av = newAV()));
431 if ((tmp = rpn2perl(c->s1)) == 0)
434 if ((tmp = rpn2perl(c->s2)) == 0)
439 default: fatal("unknown RPN node type %d", (int) s->which);
446 static SV *zquery2perl(Z_Query *q)
451 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
453 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
454 if (q->u.type_1->attributeSetId)
455 setMember(hv, "attributeSet",
456 translateOID(q->u.type_1->attributeSetId));
457 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
462 int bend_sort(void *handle, bend_sort_rr *rr)
473 char **input_setnames;
474 Zfront_handle *zhandle = (Zfront_handle *)handle;
482 input_setnames = rr->input_setnames;
483 for (i = 0; i < rr->num_input_setnames; i++)
485 av_push(aref, newSVpv(*input_setnames++, 0));
488 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
489 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
490 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
491 hv_store(href, "STATUS", 6, newSViv(0), 0);
495 XPUSHs(sv_2mortal(newRV( (SV*) href)));
499 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
503 temp = hv_fetch(href, "ERR_CODE", 8, 1);
504 err_code = newSVsv(*temp);
506 temp = hv_fetch(href, "ERR_STR", 7, 1);
507 err_str = newSVsv(*temp);
509 temp = hv_fetch(href, "STATUS", 6, 1);
510 status = newSVsv(*temp);
521 rr->errcode = SvIV(err_code);
522 rr->sort_status = SvIV(status);
523 ptr = SvPV(err_str, len);
524 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
525 strcpy(ODR_err_str, ptr);
526 rr->errstring = ODR_err_str;
536 int bend_search(void *handle, bend_search_rr *rr)
553 Zfront_handle *zhandle = (Zfront_handle *)handle;
560 basenames = rr->basenames;
561 for (i = 0; i < rr->num_bases; i++)
563 av_push(aref, newSVpv(*basenames++, 0));
566 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
567 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
568 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
569 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
570 hv_store(href, "HITS", 4, newSViv(0), 0);
571 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
572 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
573 hv_store(href, "PID", 3, newSViv(getpid()), 0);
574 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
575 query = zquery2pquery(rr->query);
578 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
586 XPUSHs(sv_2mortal(newRV( (SV*) href)));
590 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
594 temp = hv_fetch(href, "HITS", 4, 1);
595 hits = newSVsv(*temp);
597 temp = hv_fetch(href, "ERR_CODE", 8, 1);
598 err_code = newSVsv(*temp);
600 temp = hv_fetch(href, "ERR_STR", 7, 1);
601 err_str = newSVsv(*temp);
603 temp = hv_fetch(href, "HANDLE", 6, 1);
604 point = newSVsv(*temp);
612 rr->hits = SvIV(hits);
613 rr->errcode = SvIV(err_code);
614 ptr = SvPV(err_str, len);
615 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
616 strcpy(ODR_errstr, ptr);
617 rr->errstring = ODR_errstr;
618 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
619 memcpy(ODR_point, point, sizeof(*point));
620 zhandle->handle = ODR_point;*/
621 zhandle->handle = point;
626 sv_free( (SV*) aref);
627 sv_free( (SV*) href);
629 wrbuf_free(query, 1);
634 /* ### this is worryingly similar to oid2str() */
635 WRBUF oid2dotted(int *oid)
638 WRBUF buf = wrbuf_alloc();
641 for (; *oid != -1 ; oid++)
646 wrbuf_putc(buf, '.');
652 sprintf(ibuf, "%d", *oid);
653 wrbuf_puts(buf, ibuf);
659 int dotted2oid(char *dotted, int *buffer)
673 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
679 sscanf(ibuf, "%d", oid++);
692 sscanf(ibuf, "%d", oid++);
699 int bend_fetch(void *handle, bend_fetch_rr *rr)
718 Zfront_handle *zhandle = (Zfront_handle *)handle;
720 Z_RecordComposition *composition;
721 Z_ElementSetNames *simple;
730 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
731 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
732 oid_dotted = oid2dotted(rr->request_format_raw);
733 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
734 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
735 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
736 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
737 hv_store(href, "LAST", 4, newSViv(0), 0);
738 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
739 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
740 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
741 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
742 hv_store(href, "PID", 3, newSViv(getpid()), 0);
745 composition = rr->comp;
746 if (composition->which == Z_RecordComp_simple)
748 simple = composition->u.simple;
749 if (simple->which == Z_ElementSetNames_generic)
751 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
766 XPUSHs(sv_2mortal(newRV( (SV*) href)));
770 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
774 temp = hv_fetch(href, "BASENAME", 8, 1);
775 basename = newSVsv(*temp);
777 temp = hv_fetch(href, "RECORD", 6, 1);
778 record = newSVsv(*temp);
780 temp = hv_fetch(href, "LAST", 4, 1);
781 last = newSVsv(*temp);
783 temp = hv_fetch(href, "ERR_CODE", 8, 1);
784 err_code = newSVsv(*temp);
786 temp = hv_fetch(href, "ERR_STR", 7, 1),
787 err_string = newSVsv(*temp);
789 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
790 sur_flag = newSVsv(*temp);
792 temp = hv_fetch(href, "REP_FORM", 8, 1);
793 rep_form = newSVsv(*temp);
795 temp = hv_fetch(href, "HANDLE", 6, 1);
796 point = newSVsv(*temp);
804 ptr = SvPV(basename, length);
805 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
806 strcpy(ODR_basename, ptr);
807 rr->basename = ODR_basename;
809 ptr = SvPV(rep_form, length);
810 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
811 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
813 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
815 rr->output_format_raw = ODR_oid_buf;
817 ptr = SvPV(record, length);
818 oid = oid_getentbyoid(ODR_oid_buf);
819 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
821 rr->record = (char *) read_grs1(ptr, rr->stream);
826 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
827 strcpy(ODR_record, ptr);
828 rr->record = ODR_record;
831 zhandle->handle = point;
833 rr->last_in_set = SvIV(last);
837 rr->errcode = SvIV(err_code);
838 ptr = SvPV(err_string, length);
839 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
840 strcpy(ODR_errstr, ptr);
841 rr->errstring = ODR_errstr;
843 rr->surrogate_flag = SvIV(sur_flag);
845 wrbuf_free(oid_dotted, 1);
859 int bend_present(void *handle, bend_present_rr *rr)
869 Z_RecordComposition *composition;
870 Z_ElementSetNames *simple;
873 Zfront_handle *zhandle = (Zfront_handle *)handle;
875 /* WRBUF oid_dotted; */
882 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
883 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
884 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
885 hv_store(href, "START", 5, newSViv(rr->start), 0);
886 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
887 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
888 /*oid_dotted = oid2dotted(rr->request_format_raw);
889 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
890 hv_store(href, "HITS", 4, newSViv(0), 0);
891 hv_store(href, "PID", 3, newSViv(getpid()), 0);
894 composition = rr->comp;
895 if (composition->which == Z_RecordComp_simple)
897 simple = composition->u.simple;
898 if (simple->which == Z_ElementSetNames_generic)
900 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
917 XPUSHs(sv_2mortal(newRV( (SV*) href)));
921 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
925 temp = hv_fetch(href, "ERR_CODE", 8, 1);
926 err_code = newSVsv(*temp);
928 temp = hv_fetch(href, "ERR_STR", 7, 1);
929 err_string = newSVsv(*temp);
931 temp = hv_fetch(href, "HITS", 4, 1);
932 hits = newSVsv(*temp);
934 temp = hv_fetch(href, "HANDLE", 6, 1);
935 point = newSVsv(*temp);
942 rr->errcode = SvIV(err_code);
943 rr->hits = SvIV(hits);
945 ptr = SvPV(err_string, len);
946 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
947 strcpy(ODR_errstr, ptr);
948 rr->errstring = ODR_errstr;
949 /* wrbuf_free(oid_dotted, 1);*/
950 zhandle->handle = point;
955 sv_free( (SV*) href);
961 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
963 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
968 int bend_delete(void *handle, bend_delete_rr *rr)
970 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
975 int bend_scan(void *handle, bend_scan_rr *rr)
982 struct scan_entry *scan_list;
983 struct scan_entry *buffer;
984 int *step_size = rr->step_size;
988 SV *err_code = sv_newmortal();
989 SV *err_str = sv_newmortal();
990 SV *point = sv_newmortal();
991 SV *status = sv_newmortal();
992 SV *number = sv_newmortal();
1000 Zfront_handle *zhandle = (Zfront_handle *)handle;
1007 if (rr->term->term->which == Z_Term_general)
1009 term_len = rr->term->term->u.general->len;
1010 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1012 rr->errcode = 229; /* Unsupported term type */
1015 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1016 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1017 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1018 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1019 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1020 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1021 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1022 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1024 basenames = rr->basenames;
1025 for (i = 0; i < rr->num_bases; i++)
1027 av_push(aref, newSVpv(*basenames++, 0));
1029 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1033 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1037 perl_call_sv(scan_ref, G_SCALAR | G_DISCARD);
1041 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1042 err_code = newSVsv(*temp);
1044 temp = hv_fetch(href, "ERR_STR", 7, 1);
1045 err_str = newSVsv(*temp);
1047 temp = hv_fetch(href, "HANDLE", 6, 1);
1048 point = newSVsv(*temp);
1050 temp = hv_fetch(href, "STATUS", 6, 1);
1051 status = newSVsv(*temp);
1053 temp = hv_fetch(href, "NUMBER", 6, 1);
1054 number = newSVsv(*temp);
1056 temp = hv_fetch(href, "ENTRIES", 7, 1);
1057 entries_ref = newSVsv(*temp);
1063 ptr = SvPV(err_str, len);
1064 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1065 strcpy(ODR_errstr, ptr);
1066 rr->errstring = ODR_errstr;
1067 rr->errcode = SvIV(err_code);
1068 rr->num_entries = SvIV(number);
1069 rr->status = SvIV(status);
1070 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1072 entries = (AV *)SvRV(entries_ref);
1073 for (i = 0; i < rr->num_entries; i++)
1075 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1076 temp = hv_fetch(scan_item, "TERM", 4, 1);
1077 ptr = SvPV(*temp, len);
1078 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1079 strcpy(buffer->term, ptr);
1080 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1081 buffer->occurrences = SvIV(*temp);
1083 hv_undef(scan_item);
1085 rr->entries = scan_list;
1086 zhandle->handle = point;
1093 sv_free((SV *)href);
1095 sv_free((SV *)aref);
1097 sv_free((SV *)list);
1099 /*sv_free((SV *)entries);*/
1100 sv_free(entries_ref);
1106 bend_initresult *bend_init(bend_initrequest *q)
1108 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1116 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1124 char *passwd = NULL;
1130 /*q->bend_sort = bend_sort;*/
1133 q->bend_search = bend_search;
1137 q->bend_present = bend_present;
1139 /*q->bend_esrequest = bend_esrequest;*/
1140 /*q->bend_delete = bend_delete;*/
1143 q->bend_fetch = bend_fetch;
1147 q->bend_scan = bend_scan;
1150 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1151 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1152 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1153 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1154 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1155 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1156 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1158 if (q->auth->which == Z_IdAuthentication_open) {
1159 char *openpass = xstrdup (q->auth->u.open);
1160 char *cp = strchr (openpass, '/');
1163 user = nmem_strdup (odr_getmem (q->stream), openpass);
1164 passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1167 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1168 user = q->auth->u.idPass->userId;
1169 passwd = q->auth->u.idPass->password;
1171 /* ### some code paths have user/password unassigned here */
1172 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1173 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1178 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1182 if (init_ref != NULL)
1184 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
1189 temp = hv_fetch(href, "IMP_ID", 6, 1);
1190 id = newSVsv(*temp);
1192 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1193 name = newSVsv(*temp);
1195 temp = hv_fetch(href, "IMP_VER", 7, 1);
1196 ver = newSVsv(*temp);
1198 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1199 status = newSVsv(*temp);
1201 temp = hv_fetch(href, "HANDLE", 6, 1);
1202 handle= newSVsv(*temp);
1208 zhandle->handle = handle;
1209 r->errcode = SvIV(status);
1210 r->handle = zhandle;
1211 #if 0 /* implementation_id support is not yet in mainstream Yaz */
1212 ptr = SvPV(id, len);
1213 q->implementation_id = (char *)xmalloc(len + 1);
1214 strcpy(q->implementation_id, ptr);
1216 ptr = SvPV(name, len);
1217 q->implementation_name = (char *)xmalloc(len + 1);
1218 strcpy(q->implementation_name, ptr);
1219 /* q->implementation_name = SvPV(name, len);*/
1220 ptr = SvPV(ver, len);
1221 q->implementation_version = (char *)xmalloc(len + 1);
1222 strcpy(q->implementation_version, ptr);
1228 void bend_close(void *handle)
1231 Zfront_handle *zhandle = (Zfront_handle *)handle;
1238 if (close_ref == NULL)
1244 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1248 XPUSHs(sv_2mortal(newRV((SV *)href)));
1252 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
1266 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1269 set_init_handler(arg)
1272 init_ref = newSVsv(arg);
1276 set_close_handler(arg)
1279 close_ref = newSVsv(arg);
1283 set_sort_handler(arg)
1286 sort_ref = newSVsv(arg);
1289 set_search_handler(arg)
1292 search_ref = newSVsv(arg);
1296 set_fetch_handler(arg)
1299 fetch_ref = newSVsv(arg);
1303 set_present_handler(arg)
1306 present_ref = newSVsv(arg);
1310 set_esrequest_handler(arg)
1313 esrequest_ref = newSVsv(arg);
1317 set_delete_handler(arg)
1320 delete_ref = newSVsv(arg);
1324 set_scan_handler(arg)
1327 scan_ref = newSVsv(arg);
1339 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1341 for (i = 0; i < items; i++)
1343 ptr = SvPV(ST(i), len);
1344 *argv_buf = (char *)xmalloc(len + 1);
1345 strcpy(*argv_buf++, ptr);
1349 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1357 RETVAL = BEND_SCAN_SUCCESS;
1364 RETVAL = BEND_SCAN_PARTIAL;