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
31 #include <yaz/backend.h>
33 #include <yaz/wrbuf.h>
41 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
42 #define sv_undef PL_sv_undef
62 SV *search_ref = NULL;
64 SV *present_ref = NULL;
65 SV *esrequest_ref = NULL;
66 SV *delete_ref = NULL;
70 static void oid2str(Odr_oid *o, WRBUF buf)
72 for (; *o >= 0; o++) {
74 sprintf(ibuf, "%d", *o);
75 wrbuf_puts(buf, ibuf);
82 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
85 case Z_RPNStructure_simple: {
86 Z_Operand *o = s->u.simple;
90 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
96 for (i = 0; i < at->attributes->num_attributes; i++) {
97 wrbuf_puts(buf, "@attr ");
98 if (at->attributes->attributes[i]->attributeSet) {
99 oid2str(at->attributes->attributes[i]->attributeSet, buf);
100 wrbuf_putc(buf, ' ');
102 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
103 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
104 wrbuf_puts(buf, ibuf);
105 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
106 wrbuf_puts(buf, ibuf);
109 switch (at->term->which) {
110 case Z_Term_general: {
111 wrbuf_putc(buf, '"');
112 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
113 wrbuf_puts(buf, "\" ");
124 case Z_RPNStructure_complex: {
125 Z_Complex *c = s->u.complex;
127 switch (c->roperator->which) {
128 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
129 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
130 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
131 case Z_Operator_prox: abort();
134 if (!rpn2pquery(c->s1, buf))
136 if (!rpn2pquery(c->s2, buf))
146 WRBUF zquery2pquery(Z_Query *q)
148 WRBUF buf = wrbuf_alloc();
150 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
152 if (q->u.type_1->attributeSetId) {
153 /* Output attribute set ID */
154 wrbuf_puts(buf, "@attrset ");
155 oid2str(q->u.type_1->attributeSetId, buf);
156 wrbuf_putc(buf, ' ');
158 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
162 int bend_sort(void *handle, bend_sort_rr *rr)
173 char **input_setnames;
174 Zfront_handle *zhandle = (Zfront_handle *)handle;
182 input_setnames = rr->input_setnames;
183 for (i = 0; i < rr->num_input_setnames; i++)
185 av_push(aref, newSVpv(*input_setnames++, 0));
188 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
189 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
190 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
191 hv_store(href, "STATUS", 6, newSViv(0), 0);
195 XPUSHs(sv_2mortal(newRV( (SV*) href)));
199 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
203 temp = hv_fetch(href, "ERR_CODE", 8, 1);
204 err_code = newSVsv(*temp);
206 temp = hv_fetch(href, "ERR_STR", 7, 1);
207 err_str = newSVsv(*temp);
209 temp = hv_fetch(href, "STATUS", 6, 1);
210 status = newSVsv(*temp);
221 rr->errcode = SvIV(err_code);
222 rr->sort_status = SvIV(status);
223 ptr = SvPV(err_str, len);
224 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
225 strcpy(ODR_err_str, ptr);
226 rr->errstring = ODR_err_str;
236 int bend_search(void *handle, bend_search_rr *rr)
253 Zfront_handle *zhandle = (Zfront_handle *)handle;
260 basenames = rr->basenames;
261 for (i = 0; i < rr->num_bases; i++)
263 av_push(aref, newSVpv(*basenames++, 0));
266 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
267 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
268 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
269 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
270 hv_store(href, "HITS", 4, newSViv(0), 0);
271 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
272 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
273 hv_store(href, "PID", 3, newSViv(getpid()), 0);
274 query = zquery2pquery(rr->query);
277 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
285 XPUSHs(sv_2mortal(newRV( (SV*) href)));
289 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
293 temp = hv_fetch(href, "HITS", 4, 1);
294 hits = newSVsv(*temp);
296 temp = hv_fetch(href, "ERR_CODE", 8, 1);
297 err_code = newSVsv(*temp);
299 temp = hv_fetch(href, "ERR_STR", 7, 1);
300 err_str = newSVsv(*temp);
302 temp = hv_fetch(href, "HANDLE", 6, 1);
303 point = newSVsv(*temp);
311 rr->hits = SvIV(hits);
312 rr->errcode = SvIV(err_code);
313 ptr = SvPV(err_str, len);
314 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
315 strcpy(ODR_errstr, ptr);
316 rr->errstring = ODR_errstr;
317 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
318 memcpy(ODR_point, point, sizeof(*point));
319 zhandle->handle = ODR_point;*/
320 zhandle->handle = point;
325 sv_free( (SV*) aref);
326 sv_free( (SV*) href);
328 wrbuf_free(query, 1);
333 WRBUF oid2dotted(int *oid)
336 WRBUF buf = wrbuf_alloc();
339 for (; *oid != -1 ; oid++)
344 wrbuf_putc(buf, '.');
350 sprintf(ibuf, "%d", *oid);
351 wrbuf_puts(buf, ibuf);
357 int dotted2oid(char *dotted, int *buffer)
371 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
377 sscanf(ibuf, "%d", oid++);
390 sscanf(ibuf, "%d", oid++);
397 int bend_fetch(void *handle, bend_fetch_rr *rr)
415 Zfront_handle *zhandle = (Zfront_handle *)handle;
417 Z_RecordComposition *composition;
418 Z_ElementSetNames *simple;
428 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
429 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
430 oid_dotted = oid2dotted(rr->request_format_raw);
431 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
432 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
433 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
434 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
435 hv_store(href, "LAST", 4, newSViv(0), 0);
436 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
437 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
438 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
439 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
440 hv_store(href, "PID", 3, newSViv(getpid()), 0);
443 composition = rr->comp;
444 if (composition->which == Z_RecordComp_simple)
446 simple = composition->u.simple;
447 if (simple->which == Z_ElementSetNames_generic)
449 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
464 XPUSHs(sv_2mortal(newRV( (SV*) href)));
468 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
472 temp = hv_fetch(href, "BASENAME", 8, 1);
473 basename = newSVsv(*temp);
475 temp = hv_fetch(href, "RECORD", 6, 1);
476 record = newSVsv(*temp);
478 temp = hv_fetch(href, "LAST", 4, 1);
479 last = newSVsv(*temp);
481 temp = hv_fetch(href, "ERR_CODE", 8, 1);
482 err_code = newSVsv(*temp);
484 temp = hv_fetch(href, "ERR_STR", 7, 1),
485 err_string = newSVsv(*temp);
487 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
488 sur_flag = newSVsv(*temp);
490 temp = hv_fetch(href, "REP_FORM", 8, 1);
491 rep_form = newSVsv(*temp);
493 temp = hv_fetch(href, "HANDLE", 6, 1);
494 point = newSVsv(*temp);
502 ptr = SvPV(basename, length);
503 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
504 strcpy(ODR_basename, ptr);
505 rr->basename = ODR_basename;
507 ptr = SvPV(rep_form, length);
508 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
509 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
511 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
513 rr->output_format_raw = ODR_oid_buf;
515 ptr = SvPV(record, length);
516 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
517 strcpy(ODR_record, ptr);
518 rr->record = ODR_record;
521 zhandle->handle = point;
523 rr->last_in_set = SvIV(last);
527 rr->errcode = SvIV(err_code);
528 ptr = SvPV(err_string, length);
529 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
530 strcpy(ODR_errstr, ptr);
531 rr->errstring = ODR_errstr;
533 rr->surrogate_flag = SvIV(sur_flag);
535 wrbuf_free(oid_dotted, 1);
549 int bend_present(void *handle, bend_present_rr *rr)
559 Z_RecordComposition *composition;
560 Z_ElementSetNames *simple;
563 Zfront_handle *zhandle = (Zfront_handle *)handle;
565 /* WRBUF oid_dotted; */
572 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
573 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
574 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
575 hv_store(href, "START", 5, newSViv(rr->start), 0);
576 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
577 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
578 /*oid_dotted = oid2dotted(rr->request_format_raw);
579 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
580 hv_store(href, "HITS", 4, newSViv(0), 0);
581 hv_store(href, "PID", 3, newSViv(getpid()), 0);
584 composition = rr->comp;
585 if (composition->which == Z_RecordComp_simple)
587 simple = composition->u.simple;
588 if (simple->which == Z_ElementSetNames_generic)
590 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
607 XPUSHs(sv_2mortal(newRV( (SV*) href)));
611 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
615 temp = hv_fetch(href, "ERR_CODE", 8, 1);
616 err_code = newSVsv(*temp);
618 temp = hv_fetch(href, "ERR_STR", 7, 1);
619 err_string = newSVsv(*temp);
621 temp = hv_fetch(href, "HITS", 4, 1);
622 hits = newSVsv(*temp);
624 temp = hv_fetch(href, "HANDLE", 6, 1);
625 point = newSVsv(*temp);
632 rr->errcode = SvIV(err_code);
633 rr->hits = SvIV(hits);
635 ptr = SvPV(err_string, len);
636 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
637 strcpy(ODR_errstr, ptr);
638 rr->errstring = ODR_errstr;
639 /* wrbuf_free(oid_dotted, 1);*/
640 zhandle->handle = point;
645 sv_free( (SV*) href);
651 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
653 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
658 int bend_delete(void *handle, bend_delete_rr *rr)
660 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
665 int bend_scan(void *handle, bend_scan_rr *rr)
667 perl_call_sv(scan_ref, G_VOID | G_DISCARD | G_NOARGS);
672 bend_initresult *bend_init(bend_initrequest *q)
674 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
681 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
693 /*q->bend_sort = bend_sort;*/
696 q->bend_search = bend_search;
700 q->bend_present = bend_present;
702 /*q->bend_esrequest = bend_esrequest;*/
703 /*q->bend_delete = bend_delete;*/
706 q->bend_fetch = bend_fetch;
708 /*q->bend_scan = bend_scan;*/
710 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
711 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
712 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
713 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
714 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
715 hv_store(href, "PID", 3, newSViv(getpid()), 0);
719 XPUSHs(sv_2mortal(newRV( (SV*) href)));
723 if (init_ref != NULL)
725 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
730 temp = hv_fetch(href, "IMP_NAME", 8, 1);
731 name = newSVsv(*temp);
733 temp = hv_fetch(href, "IMP_VER", 7, 1);
734 ver = newSVsv(*temp);
736 temp = hv_fetch(href, "ERR_CODE", 8, 1);
737 status = newSVsv(*temp);
739 temp = hv_fetch(href, "HANDLE", 6, 1);
740 handle= newSVsv(*temp);
746 zhandle->handle = handle;
747 r->errcode = SvIV(status);
749 ptr = SvPV(name, len);
750 q->implementation_name = (char *)xmalloc(len + 1);
751 strcpy(q->implementation_name, ptr);
752 /* q->implementation_name = SvPV(name, len);*/
753 ptr = SvPV(ver, len);
754 q->implementation_version = (char *)xmalloc(len + 1);
755 strcpy(q->implementation_version, ptr);
761 void bend_close(void *handle)
764 Zfront_handle *zhandle = (Zfront_handle *)handle;
771 if (close_ref == NULL)
777 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
781 XPUSHs(sv_2mortal(newRV((SV *)href)));
785 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
799 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
802 set_init_handler(arg)
805 init_ref = newSVsv(arg);
809 set_close_handler(arg)
812 close_ref = newSVsv(arg);
816 set_sort_handler(arg)
819 sort_ref = newSVsv(arg);
822 set_search_handler(arg)
825 search_ref = newSVsv(arg);
829 set_fetch_handler(arg)
832 fetch_ref = newSVsv(arg);
836 set_present_handler(arg)
839 present_ref = newSVsv(arg);
843 set_esrequest_handler(arg)
846 esrequest_ref = newSVsv(arg);
850 set_delete_handler(arg)
853 delete_ref = newSVsv(arg);
857 set_scan_handler(arg)
860 scan_ref = newSVsv(arg);
872 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
874 for (i = 0; i < items; i++)
876 ptr = SvPV(ST(i), len);
877 *argv_buf = (char *)xmalloc(len + 1);
878 strcpy(*argv_buf++, ptr);
882 RETVAL = statserv_main(items, argv, bend_init, bend_close);