3 * $Id: SimpleServer.xs,v 1.22 2004-05-11 12:56:37 adam Exp $
4 * ----------------------------------------------------------------------
6 * Copyright (c) 2000, Index Data.
8 * Permission to use, copy, modify, distribute, and sell this software and
9 * its documentation, in whole or in part, for any purpose, is hereby granted,
12 * 1. This copyright and permission notice appear in all copies of the
13 * software and its documentation. Notices of copyright or attribution
14 * which appear at the beginning of any file must remain unchanged.
16 * 2. The name of Index Data or the individual authors may not be used to
17 * endorse or promote products derived from this software without specific
18 * prior written permission.
20 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
22 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
23 * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
24 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
25 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
26 * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
27 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 #include <yaz/backend.h>
39 #include <yaz/wrbuf.h>
47 #define GRS_MAX_FIELDS 500
51 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
52 #define sv_undef PL_sv_undef
72 SV *search_ref = NULL;
74 SV *present_ref = NULL;
75 SV *esrequest_ref = NULL;
76 SV *delete_ref = NULL;
78 PerlInterpreter *root_perl_context;
81 #define GRS_BUF_SIZE 512
83 CV * simpleserver_sv2cv(SV *handler) {
89 buf = SvPV( handler, len);
90 if ( !( ret = perl_get_cv(buf, FALSE ) ) ) {
91 fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf );
97 return (CV *) handler;
102 int simpleserver_clone(void) {
103 PerlInterpreter *current = PERL_GET_CONTEXT;
106 PerlInterpreter *perl_interp = perl_clone(root_perl_context, CLONEf_COPY_STACKS);
107 PERL_SET_CONTEXT( perl_interp );
113 void simpleserver_free(void) {
114 PerlInterpreter *current_interp = PERL_GET_CONTEXT;
116 perl_destruct(current_interp);
117 perl_free(current_interp);
122 Z_GenericRecord *read_grs1(char *str, ODR o)
125 char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
126 char value[GRS_BUF_SIZE+1];
127 Z_GenericRecord *r = 0;
130 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
131 r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
140 ptr = strchr(str, '\n');
145 if (len > GRS_BUF_SIZE) {
146 yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
149 strncpy(line, str, len);
153 while (*buf && isspace(*buf))
156 memmove(original, str, strlen(str));
159 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
161 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
164 if (!type && *value == '0')
166 if (!(buf = strchr(buf, ')')))
169 while (*buf && isspace(*buf))
171 if (r->num_elements >= GRS_MAX_FIELDS)
173 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
176 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
177 t->tagType = (int *)odr_malloc(o, sizeof(int));
179 t->tagValue = (Z_StringOrNumeric *)
180 odr_malloc(o, sizeof(Z_StringOrNumeric));
181 if ((ivalue = atoi(value)))
183 t->tagValue->which = Z_StringOrNumeric_numeric;
184 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
185 *t->tagValue->u.numeric = ivalue;
189 t->tagValue->which = Z_StringOrNumeric_string;
190 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
191 strcpy(t->tagValue->u.string, value);
193 t->tagOccurrence = 0;
195 t->appliedVariant = 0;
196 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
199 c->which = Z_ElementData_subtree;
200 c->u.subtree = read_grs1(str, o);
204 c->which = Z_ElementData_string;
205 c->u.string = odr_strdup(o, buf);
214 static void oid2str(Odr_oid *o, WRBUF buf)
216 for (; *o >= 0; o++) {
218 sprintf(ibuf, "%d", *o);
219 wrbuf_puts(buf, ibuf);
221 wrbuf_putc(buf, '.');
226 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
229 case Z_RPNStructure_simple: {
230 Z_Operand *o = s->u.simple;
233 case Z_Operand_APT: {
234 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
236 if (at->attributes) {
240 for (i = 0; i < at->attributes->num_attributes; i++) {
241 wrbuf_puts(buf, "@attr ");
242 if (at->attributes->attributes[i]->attributeSet) {
243 oid2str(at->attributes->attributes[i]->attributeSet, buf);
244 wrbuf_putc(buf, ' ');
246 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
247 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
248 wrbuf_puts(buf, ibuf);
249 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
250 wrbuf_puts(buf, ibuf);
253 switch (at->term->which) {
254 case Z_Term_general: {
255 wrbuf_putc(buf, '"');
256 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
257 wrbuf_puts(buf, "\" ");
268 case Z_RPNStructure_complex: {
269 Z_Complex *c = s->u.complex;
271 switch (c->roperator->which) {
272 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
273 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
274 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
275 case Z_Operator_prox: abort();
278 if (!rpn2pquery(c->s1, buf))
280 if (!rpn2pquery(c->s2, buf))
290 WRBUF zquery2pquery(Z_Query *q)
292 WRBUF buf = wrbuf_alloc();
294 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
296 if (q->u.type_1->attributeSetId) {
297 /* Output attribute set ID */
298 wrbuf_puts(buf, "@attrset ");
299 oid2str(q->u.type_1->attributeSetId, buf);
300 wrbuf_putc(buf, ' ');
302 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
306 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
308 void fatal(char *fmt, ...)
312 fprintf(stderr, "FATAL (yazwrap): ");
314 vfprintf(stderr, fmt, ap);
316 fprintf(stderr, "\n");
321 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
323 * Creates a new Perl object of type `class'; the newly-created scalar
324 * that is a reference to the blessed thingy `referent' is returned.
326 static SV *newObject(char *class, SV *referent)
331 sv = newRV_noinc((SV*) referent);
332 stash = gv_stashpv(class, 0);
334 fatal("attempt to create object of undefined class '%s'", class);
335 /*assert(stash != 0);*/
341 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
342 static void setMember(HV *hv, char *name, SV *val)
344 /* We don't increment `val's reference count -- I think this is
345 * right because it's created with a refcount of 1, and in fact
346 * the reference via this hash is the only reference to it in
349 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
350 fatal("couldn't store member in hash");
354 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
355 static SV *translateOID(Odr_oid *x)
357 /* Yaz represents an OID by an int array terminated by a negative
358 * value, typically -1; we represent it as a reference to a
359 * blessed scalar string of "."-separated elements.
365 for (i = 0; x[i] >= 0; i++) {
366 sprintf(buf + strlen(buf), "%d", (int) x[i]);
372 * ### We'd like to return a blessed scalar (string) here, but of
373 * course you can't do that in Perl: only references can be
374 * blessed, so we'd have to return a _reference_ to a string, and
375 * bless _that_. Better to do without the blessing, I think.
378 return newSVpv(buf, 0);
380 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
385 static SV *rpn2perl(Z_RPNStructure *s)
392 case Z_RPNStructure_simple: {
393 Z_Operand *o = s->u.simple;
394 Z_AttributesPlusTerm *at;
395 if (o->which != Z_Operand_APT)
396 fatal("can't handle RPN simples other than APT");
397 at = o->u.attributesPlusTerm;
398 if (at->term->which != Z_Term_general)
399 fatal("can't handle RPN terms other than general");
401 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
402 if (at->attributes) {
404 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
405 (SV*) (av = newAV()));
406 for (i = 0; i < at->attributes->num_attributes; i++) {
407 Z_AttributeElement *elem = at->attributes->attributes[i];
409 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
410 (SV*) (hv2 = newHV()));
411 if (elem->attributeSet)
412 setMember(hv2, "attributeSet",
413 translateOID(elem->attributeSet));
414 setMember(hv2, "attributeType",
415 newSViv(*elem->attributeType));
416 assert(elem->which == Z_AttributeValue_numeric);
417 setMember(hv2, "attributeValue",
418 newSViv(*elem->value.numeric));
421 setMember(hv, "attributes", attrs);
423 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
424 at->term->u.general->len));
427 case Z_RPNStructure_complex: {
429 Z_Complex *c = s->u.complex;
430 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
431 switch (c->roperator->which) {
432 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
433 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
434 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
435 case Z_Operator_prox: fatal("proximity not yet supported");
436 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
438 sv = newObject(type, (SV*) (av = newAV()));
439 if ((tmp = rpn2perl(c->s1)) == 0)
442 if ((tmp = rpn2perl(c->s2)) == 0)
447 default: fatal("unknown RPN node type %d", (int) s->which);
454 static SV *zquery2perl(Z_Query *q)
459 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
461 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
462 if (q->u.type_1->attributeSetId)
463 setMember(hv, "attributeSet",
464 translateOID(q->u.type_1->attributeSetId));
465 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
470 int bend_sort(void *handle, bend_sort_rr *rr)
481 char **input_setnames;
482 Zfront_handle *zhandle = (Zfront_handle *)handle;
490 input_setnames = rr->input_setnames;
491 for (i = 0; i < rr->num_input_setnames; i++)
493 av_push(aref, newSVpv(*input_setnames++, 0));
496 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
497 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
498 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
499 hv_store(href, "STATUS", 6, newSViv(0), 0);
503 XPUSHs(sv_2mortal(newRV( (SV*) href)));
507 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
511 temp = hv_fetch(href, "ERR_CODE", 8, 1);
512 err_code = newSVsv(*temp);
514 temp = hv_fetch(href, "ERR_STR", 7, 1);
515 err_str = newSVsv(*temp);
517 temp = hv_fetch(href, "STATUS", 6, 1);
518 status = newSVsv(*temp);
529 rr->errcode = SvIV(err_code);
530 rr->sort_status = SvIV(status);
531 ptr = SvPV(err_str, len);
532 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
533 strcpy(ODR_err_str, ptr);
534 rr->errstring = ODR_err_str;
544 int bend_search(void *handle, bend_search_rr *rr)
561 Zfront_handle *zhandle = (Zfront_handle *)handle;
569 basenames = rr->basenames;
570 for (i = 0; i < rr->num_bases; i++)
572 av_push(aref, newSVpv(*basenames++, 0));
575 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
576 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
577 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
578 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
579 hv_store(href, "HITS", 4, newSViv(0), 0);
580 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
581 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
582 hv_store(href, "PID", 3, newSViv(getpid()), 0);
583 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
584 query = zquery2pquery(rr->query);
587 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
595 XPUSHs(sv_2mortal(newRV( (SV*) href)));
599 handler_cv = simpleserver_sv2cv( search_ref );
600 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
604 temp = hv_fetch(href, "HITS", 4, 1);
605 hits = newSVsv(*temp);
607 temp = hv_fetch(href, "ERR_CODE", 8, 1);
608 err_code = newSVsv(*temp);
610 temp = hv_fetch(href, "ERR_STR", 7, 1);
611 err_str = newSVsv(*temp);
613 temp = hv_fetch(href, "HANDLE", 6, 1);
614 point = newSVsv(*temp);
622 rr->hits = SvIV(hits);
623 rr->errcode = SvIV(err_code);
624 ptr = SvPV(err_str, len);
625 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
626 strcpy(ODR_errstr, ptr);
627 rr->errstring = ODR_errstr;
628 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
629 memcpy(ODR_point, point, sizeof(*point));
630 zhandle->handle = ODR_point;*/
631 zhandle->handle = point;
636 sv_free( (SV*) aref);
637 sv_free( (SV*) href);
639 wrbuf_free(query, 1);
644 /* ### this is worryingly similar to oid2str() */
645 WRBUF oid2dotted(int *oid)
648 WRBUF buf = wrbuf_alloc();
651 for (; *oid != -1 ; oid++)
656 wrbuf_putc(buf, '.');
662 sprintf(ibuf, "%d", *oid);
663 wrbuf_puts(buf, ibuf);
669 int dotted2oid(char *dotted, int *buffer)
683 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
689 sscanf(ibuf, "%d", oid++);
702 sscanf(ibuf, "%d", oid++);
709 int bend_fetch(void *handle, bend_fetch_rr *rr)
728 Zfront_handle *zhandle = (Zfront_handle *)handle;
731 Z_RecordComposition *composition;
732 Z_ElementSetNames *simple;
741 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
742 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
743 oid_dotted = oid2dotted(rr->request_format_raw);
744 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
745 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
746 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
747 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
748 hv_store(href, "LAST", 4, newSViv(0), 0);
749 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
750 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
751 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
752 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
753 hv_store(href, "PID", 3, newSViv(getpid()), 0);
756 composition = rr->comp;
757 if (composition->which == Z_RecordComp_simple)
759 simple = composition->u.simple;
760 if (simple->which == Z_ElementSetNames_generic)
762 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
777 XPUSHs(sv_2mortal(newRV( (SV*) href)));
781 handler_cv = simpleserver_sv2cv( fetch_ref );
782 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
786 temp = hv_fetch(href, "BASENAME", 8, 1);
787 basename = newSVsv(*temp);
789 temp = hv_fetch(href, "RECORD", 6, 1);
790 record = newSVsv(*temp);
792 temp = hv_fetch(href, "LAST", 4, 1);
793 last = newSVsv(*temp);
795 temp = hv_fetch(href, "ERR_CODE", 8, 1);
796 err_code = newSVsv(*temp);
798 temp = hv_fetch(href, "ERR_STR", 7, 1),
799 err_string = newSVsv(*temp);
801 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
802 sur_flag = newSVsv(*temp);
804 temp = hv_fetch(href, "REP_FORM", 8, 1);
805 rep_form = newSVsv(*temp);
807 temp = hv_fetch(href, "HANDLE", 6, 1);
808 point = newSVsv(*temp);
816 ptr = SvPV(basename, length);
817 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
818 strcpy(ODR_basename, ptr);
819 rr->basename = ODR_basename;
821 ptr = SvPV(rep_form, length);
822 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
823 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
825 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
827 rr->output_format_raw = ODR_oid_buf;
829 ptr = SvPV(record, length);
830 oid = oid_getentbyoid(ODR_oid_buf);
831 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
833 rr->record = (char *) read_grs1(ptr, rr->stream);
838 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
839 strcpy(ODR_record, ptr);
840 rr->record = ODR_record;
843 zhandle->handle = point;
845 rr->last_in_set = SvIV(last);
849 rr->errcode = SvIV(err_code);
850 ptr = SvPV(err_string, length);
851 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
852 strcpy(ODR_errstr, ptr);
853 rr->errstring = ODR_errstr;
855 rr->surrogate_flag = SvIV(sur_flag);
857 wrbuf_free(oid_dotted, 1);
871 int bend_present(void *handle, bend_present_rr *rr)
881 Z_RecordComposition *composition;
882 Z_ElementSetNames *simple;
885 Zfront_handle *zhandle = (Zfront_handle *)handle;
888 /* WRBUF oid_dotted; */
895 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
896 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
897 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
898 hv_store(href, "START", 5, newSViv(rr->start), 0);
899 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
900 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
901 /*oid_dotted = oid2dotted(rr->request_format_raw);
902 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
903 hv_store(href, "HITS", 4, newSViv(0), 0);
904 hv_store(href, "PID", 3, newSViv(getpid()), 0);
907 composition = rr->comp;
908 if (composition->which == Z_RecordComp_simple)
910 simple = composition->u.simple;
911 if (simple->which == Z_ElementSetNames_generic)
913 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
930 XPUSHs(sv_2mortal(newRV( (SV*) href)));
934 handler_cv = simpleserver_sv2cv( present_ref );
935 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
939 temp = hv_fetch(href, "ERR_CODE", 8, 1);
940 err_code = newSVsv(*temp);
942 temp = hv_fetch(href, "ERR_STR", 7, 1);
943 err_string = newSVsv(*temp);
945 temp = hv_fetch(href, "HITS", 4, 1);
946 hits = newSVsv(*temp);
948 temp = hv_fetch(href, "HANDLE", 6, 1);
949 point = newSVsv(*temp);
956 rr->errcode = SvIV(err_code);
957 rr->hits = SvIV(hits);
959 ptr = SvPV(err_string, len);
960 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
961 strcpy(ODR_errstr, ptr);
962 rr->errstring = ODR_errstr;
963 /* wrbuf_free(oid_dotted, 1);*/
964 zhandle->handle = point;
969 sv_free( (SV*) href);
975 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
977 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
982 int bend_delete(void *handle, bend_delete_rr *rr)
984 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
989 int bend_scan(void *handle, bend_scan_rr *rr)
996 struct scan_entry *scan_list;
997 struct scan_entry *buffer;
998 int *step_size = rr->step_size;
1002 SV *err_code = sv_newmortal();
1003 SV *err_str = sv_newmortal();
1004 SV *point = sv_newmortal();
1005 SV *status = sv_newmortal();
1006 SV *number = sv_newmortal();
1013 Zfront_handle *zhandle = (Zfront_handle *)handle;
1021 if (rr->term->term->which == Z_Term_general)
1023 term_len = rr->term->term->u.general->len;
1024 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1026 rr->errcode = 229; /* Unsupported term type */
1029 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1030 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1031 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1032 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1033 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1034 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1035 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1036 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1038 basenames = rr->basenames;
1039 for (i = 0; i < rr->num_bases; i++)
1041 av_push(aref, newSVpv(*basenames++, 0));
1043 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1047 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1051 handler_cv = simpleserver_sv2cv( scan_ref );
1052 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1056 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1057 err_code = newSVsv(*temp);
1059 temp = hv_fetch(href, "ERR_STR", 7, 1);
1060 err_str = newSVsv(*temp);
1062 temp = hv_fetch(href, "HANDLE", 6, 1);
1063 point = newSVsv(*temp);
1065 temp = hv_fetch(href, "STATUS", 6, 1);
1066 status = newSVsv(*temp);
1068 temp = hv_fetch(href, "NUMBER", 6, 1);
1069 number = newSVsv(*temp);
1071 temp = hv_fetch(href, "ENTRIES", 7, 1);
1072 entries_ref = newSVsv(*temp);
1078 ptr = SvPV(err_str, len);
1079 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1080 strcpy(ODR_errstr, ptr);
1081 rr->errstring = ODR_errstr;
1082 rr->errcode = SvIV(err_code);
1083 rr->num_entries = SvIV(number);
1084 rr->status = SvIV(status);
1085 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1087 entries = (AV *)SvRV(entries_ref);
1088 for (i = 0; i < rr->num_entries; i++)
1090 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1091 temp = hv_fetch(scan_item, "TERM", 4, 1);
1092 ptr = SvPV(*temp, len);
1093 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1094 strcpy(buffer->term, ptr);
1095 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1096 buffer->occurrences = SvIV(*temp);
1098 hv_undef(scan_item);
1100 rr->entries = scan_list;
1101 zhandle->handle = point;
1108 sv_free((SV *)href);
1110 sv_free((SV *)aref);
1112 sv_free((SV *)list);
1114 /*sv_free((SV *)entries);*/
1115 sv_free(entries_ref);
1121 bend_initresult *bend_init(bend_initrequest *q)
1123 int dummy = simpleserver_clone();
1124 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1132 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1140 char *passwd = NULL;
1147 /*q->bend_sort = bend_sort;*/
1150 q->bend_search = bend_search;
1154 q->bend_present = bend_present;
1156 /*q->bend_esrequest = bend_esrequest;*/
1157 /*q->bend_delete = bend_delete;*/
1160 q->bend_fetch = bend_fetch;
1164 q->bend_scan = bend_scan;
1167 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1168 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1169 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1170 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1171 hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1172 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1173 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1174 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1176 if (q->auth->which == Z_IdAuthentication_open) {
1177 char *openpass = xstrdup (q->auth->u.open);
1178 char *cp = strchr (openpass, '/');
1181 user = nmem_strdup (odr_getmem (q->stream), openpass);
1182 passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1185 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1186 user = q->auth->u.idPass->userId;
1187 passwd = q->auth->u.idPass->password;
1189 /* ### some code paths have user/password unassigned here */
1190 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1191 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1196 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1200 if (init_ref != NULL)
1202 handler_cv = simpleserver_sv2cv( init_ref );
1203 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1208 temp = hv_fetch(href, "IMP_ID", 6, 1);
1209 id = newSVsv(*temp);
1211 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1212 name = newSVsv(*temp);
1214 temp = hv_fetch(href, "IMP_VER", 7, 1);
1215 ver = newSVsv(*temp);
1217 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1218 status = newSVsv(*temp);
1220 temp = hv_fetch(href, "ERR_STR", 7, 1);
1221 err_str = newSVsv(*temp);
1223 temp = hv_fetch(href, "HANDLE", 6, 1);
1224 handle= newSVsv(*temp);
1230 zhandle->handle = handle;
1231 r->errcode = SvIV(status);
1232 ptr = SvPV(err_str, len);
1233 r->errstring = (char *)odr_malloc(q->stream, len + 1);
1234 strcpy(r->errstring, ptr);
1236 r->handle = zhandle;
1237 ptr = SvPV(id, len);
1238 q->implementation_id = (char *)xmalloc(len + 1);
1239 strcpy(q->implementation_id, ptr);
1240 ptr = SvPV(name, len);
1241 q->implementation_name = (char *)xmalloc(len + 1);
1242 strcpy(q->implementation_name, ptr);
1243 /* q->implementation_name = SvPV(name, len);*/
1244 ptr = SvPV(ver, len);
1245 q->implementation_version = (char *)xmalloc(len + 1);
1246 strcpy(q->implementation_version, ptr);
1252 void bend_close(void *handle)
1255 Zfront_handle *zhandle = (Zfront_handle *)handle;
1263 if (close_ref == NULL)
1269 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1273 XPUSHs(sv_2mortal(newRV((SV *)href)));
1277 handler_cv = simpleserver_sv2cv( close_ref );
1278 perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD);
1287 simpleserver_free();
1293 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1296 set_init_handler(arg)
1299 init_ref = newSVsv(arg);
1303 set_close_handler(arg)
1306 close_ref = newSVsv(arg);
1310 set_sort_handler(arg)
1313 sort_ref = newSVsv(arg);
1316 set_search_handler(arg)
1319 search_ref = newSVsv(arg);
1323 set_fetch_handler(arg)
1326 fetch_ref = newSVsv(arg);
1330 set_present_handler(arg)
1333 present_ref = newSVsv(arg);
1337 set_esrequest_handler(arg)
1340 esrequest_ref = newSVsv(arg);
1344 set_delete_handler(arg)
1347 delete_ref = newSVsv(arg);
1351 set_scan_handler(arg)
1354 scan_ref = newSVsv(arg);
1366 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1368 for (i = 0; i < items; i++)
1370 ptr = SvPV(ST(i), len);
1371 *argv_buf = (char *)xmalloc(len + 1);
1372 strcpy(*argv_buf++, ptr);
1375 root_perl_context = PERL_GET_CONTEXT;
1377 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1385 RETVAL = BEND_SCAN_SUCCESS;
1392 RETVAL = BEND_SCAN_PARTIAL;