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>
40 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
41 #define sv_undef PL_sv_undef
61 SV *search_ref = NULL;
63 SV *present_ref = NULL;
64 SV *esrequest_ref = NULL;
65 SV *delete_ref = NULL;
69 static void oid2str(Odr_oid *o, WRBUF buf)
71 for (; *o >= 0; o++) {
73 sprintf(ibuf, "%d", *o);
74 wrbuf_puts(buf, ibuf);
81 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
84 case Z_RPNStructure_simple: {
85 Z_Operand *o = s->u.simple;
89 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
95 for (i = 0; i < at->attributes->num_attributes; i++) {
96 wrbuf_puts(buf, "@attr ");
97 if (at->attributes->attributes[i]->attributeSet) {
98 oid2str(at->attributes->attributes[i]->attributeSet, buf);
101 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
102 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
103 wrbuf_puts(buf, ibuf);
104 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
105 wrbuf_puts(buf, ibuf);
108 switch (at->term->which) {
109 case Z_Term_general: {
110 wrbuf_putc(buf, '"');
111 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
112 wrbuf_puts(buf, "\" ");
123 case Z_RPNStructure_complex: {
124 Z_Complex *c = s->u.complex;
126 switch (c->roperator->which) {
127 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
128 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
129 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
130 case Z_Operator_prox: abort();
133 if (!rpn2pquery(c->s1, buf))
135 if (!rpn2pquery(c->s2, buf))
145 WRBUF zquery2pquery(Z_Query *q)
147 WRBUF buf = wrbuf_alloc();
149 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
151 if (q->u.type_1->attributeSetId) {
152 /* Output attribute set ID */
153 wrbuf_puts(buf, "@attrset ");
154 oid2str(q->u.type_1->attributeSetId, buf);
155 wrbuf_putc(buf, ' ');
157 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
161 int bend_sort(void *handle, bend_sort_rr *rr)
172 char **input_setnames;
173 Zfront_handle *zhandle = (Zfront_handle *)handle;
181 input_setnames = rr->input_setnames;
182 for (i = 0; i < rr->num_input_setnames; i++)
184 av_push(aref, newSVpv(*input_setnames++, 0));
187 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
188 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
189 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
190 hv_store(href, "STATUS", 6, newSViv(0), 0);
194 XPUSHs(sv_2mortal(newRV( (SV*) href)));
198 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
202 temp = hv_fetch(href, "ERR_CODE", 8, 1);
203 err_code = newSVsv(*temp);
205 temp = hv_fetch(href, "ERR_STR", 7, 1);
206 err_str = newSVsv(*temp);
208 temp = hv_fetch(href, "STATUS", 6, 1);
209 status = newSVsv(*temp);
220 rr->errcode = SvIV(err_code);
221 rr->sort_status = SvIV(status);
222 ptr = SvPV(err_str, len);
223 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
224 strcpy(ODR_err_str, ptr);
225 rr->errstring = ODR_err_str;
235 int bend_search(void *handle, bend_search_rr *rr)
252 Zfront_handle *zhandle = (Zfront_handle *)handle;
259 basenames = rr->basenames;
260 for (i = 0; i < rr->num_bases; i++)
262 av_push(aref, newSVpv(*basenames++, 0));
265 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
266 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
267 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
268 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
269 hv_store(href, "HITS", 4, newSViv(0), 0);
270 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
271 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
272 query = zquery2pquery(rr->query);
275 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
283 XPUSHs(sv_2mortal(newRV( (SV*) href)));
287 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
291 temp = hv_fetch(href, "HITS", 4, 1);
292 hits = newSVsv(*temp);
294 temp = hv_fetch(href, "ERR_CODE", 8, 1);
295 err_code = newSVsv(*temp);
297 temp = hv_fetch(href, "ERR_STR", 7, 1);
298 err_str = newSVsv(*temp);
300 temp = hv_fetch(href, "HANDLE", 6, 1);
301 point = newSVsv(*temp);
309 rr->hits = SvIV(hits);
310 rr->errcode = SvIV(err_code);
311 ptr = SvPV(err_str, len);
312 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
313 strcpy(ODR_errstr, ptr);
314 rr->errstring = ODR_errstr;
315 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
316 memcpy(ODR_point, point, sizeof(*point));
317 zhandle->handle = ODR_point;*/
318 zhandle->handle = point;
323 sv_free( (SV*) aref);
324 sv_free( (SV*) href);
326 wrbuf_free(query, 1);
331 WRBUF oid2dotted(int *oid)
334 WRBUF buf = wrbuf_alloc();
337 for (; *oid != -1 ; oid++)
342 wrbuf_putc(buf, '.');
348 sprintf(ibuf, "%d", *oid);
349 wrbuf_puts(buf, ibuf);
355 int dotted2oid(char *dotted, int *buffer)
369 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
375 sscanf(ibuf, "%d", oid++);
388 sscanf(ibuf, "%d", oid++);
395 int bend_fetch(void *handle, bend_fetch_rr *rr)
413 Zfront_handle *zhandle = (Zfront_handle *)handle;
415 Z_RecordComposition *composition;
416 Z_ElementSetNames *simple;
426 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
427 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
428 oid_dotted = oid2dotted(rr->request_format_raw);
429 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
430 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
431 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
432 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
433 hv_store(href, "LAST", 4, newSViv(0), 0);
434 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
435 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
436 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
437 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
440 composition = rr->comp;
441 if (composition->which == Z_RecordComp_simple)
443 simple = composition->u.simple;
444 if (simple->which == Z_ElementSetNames_generic)
446 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
461 XPUSHs(sv_2mortal(newRV( (SV*) href)));
465 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
469 temp = hv_fetch(href, "BASENAME", 8, 1);
470 basename = newSVsv(*temp);
472 temp = hv_fetch(href, "RECORD", 6, 1);
473 record = newSVsv(*temp);
475 temp = hv_fetch(href, "LAST", 4, 1);
476 last = newSVsv(*temp);
478 temp = hv_fetch(href, "ERR_CODE", 8, 1);
479 err_code = newSVsv(*temp);
481 temp = hv_fetch(href, "ERR_STR", 7, 1),
482 err_string = newSVsv(*temp);
484 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
485 sur_flag = newSVsv(*temp);
487 temp = hv_fetch(href, "REP_FORM", 8, 1);
488 rep_form = newSVsv(*temp);
490 temp = hv_fetch(href, "HANDLE", 6, 1);
491 point = newSVsv(*temp);
499 ptr = SvPV(basename, length);
500 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
501 strcpy(ODR_basename, ptr);
502 rr->basename = ODR_basename;
504 ptr = SvPV(rep_form, length);
505 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
506 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
508 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
510 rr->output_format_raw = ODR_oid_buf;
512 ptr = SvPV(record, length);
513 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
514 strcpy(ODR_record, ptr);
515 rr->record = ODR_record;
518 zhandle->handle = point;
520 rr->last_in_set = SvIV(last);
524 rr->errcode = SvIV(err_code);
525 ptr = SvPV(err_string, length);
526 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
527 strcpy(ODR_errstr, ptr);
528 rr->errstring = ODR_errstr;
530 rr->surrogate_flag = SvIV(sur_flag);
532 wrbuf_free(oid_dotted, 1);
546 int bend_present(void *handle, bend_present_rr *rr)
556 Z_RecordComposition *composition;
557 Z_ElementSetNames *simple;
560 Zfront_handle *zhandle = (Zfront_handle *)handle;
562 /* WRBUF oid_dotted; */
569 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
570 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
571 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
572 hv_store(href, "START", 5, newSViv(rr->start), 0);
573 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
574 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
575 /*oid_dotted = oid2dotted(rr->request_format_raw);
576 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
577 hv_store(href, "HITS", 4, newSViv(0), 0);
580 composition = rr->comp;
581 if (composition->which == Z_RecordComp_simple)
583 simple = composition->u.simple;
584 if (simple->which == Z_ElementSetNames_generic)
586 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
603 XPUSHs(sv_2mortal(newRV( (SV*) href)));
607 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
611 temp = hv_fetch(href, "ERR_CODE", 8, 1);
612 err_code = newSVsv(*temp);
614 temp = hv_fetch(href, "ERR_STR", 7, 1);
615 err_string = newSVsv(*temp);
617 temp = hv_fetch(href, "HITS", 4, 1);
618 hits = newSVsv(*temp);
620 temp = hv_fetch(href, "HANDLE", 6, 1);
621 point = newSVsv(*temp);
628 rr->errcode = SvIV(err_code);
629 rr->hits = SvIV(hits);
631 ptr = SvPV(err_string, len);
632 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
633 strcpy(ODR_errstr, ptr);
634 rr->errstring = ODR_errstr;
635 /* wrbuf_free(oid_dotted, 1);*/
636 zhandle->handle = point;
641 sv_free( (SV*) href);
647 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
649 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
654 int bend_delete(void *handle, bend_delete_rr *rr)
656 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
661 int bend_scan(void *handle, bend_scan_rr *rr)
663 perl_call_sv(scan_ref, G_VOID | G_DISCARD | G_NOARGS);
668 bend_initresult *bend_init(bend_initrequest *q)
670 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
677 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
689 /*q->bend_sort = bend_sort;*/
692 q->bend_search = bend_search;
696 q->bend_present = bend_present;
698 /*q->bend_esrequest = bend_esrequest;*/
699 /*q->bend_delete = bend_delete;*/
702 q->bend_fetch = bend_fetch;
704 /*q->bend_scan = bend_scan;*/
706 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
707 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
708 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
709 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
710 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
714 XPUSHs(sv_2mortal(newRV( (SV*) href)));
718 if (init_ref != NULL)
720 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
725 temp = hv_fetch(href, "IMP_NAME", 8, 1);
726 name = newSVsv(*temp);
728 temp = hv_fetch(href, "IMP_VER", 7, 1);
729 ver = newSVsv(*temp);
731 temp = hv_fetch(href, "ERR_CODE", 8, 1);
732 status = newSVsv(*temp);
734 temp = hv_fetch(href, "HANDLE", 6, 1);
735 handle= newSVsv(*temp);
741 zhandle->handle = handle;
742 r->errcode = SvIV(status);
744 ptr = SvPV(name, len);
745 q->implementation_name = (char *)xmalloc(len + 1);
746 strcpy(q->implementation_name, ptr);
747 /* q->implementation_name = SvPV(name, len);*/
748 ptr = SvPV(ver, len);
749 q->implementation_version = (char *)xmalloc(len + 1);
750 strcpy(q->implementation_version, ptr);
756 void bend_close(void *handle)
759 Zfront_handle *zhandle = (Zfront_handle *)handle;
766 if (close_ref == NULL)
772 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
776 XPUSHs(sv_2mortal(newRV((SV *)href)));
780 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
794 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
797 set_init_handler(arg)
800 init_ref = newSVsv(arg);
804 set_close_handler(arg)
807 close_ref = newSVsv(arg);
811 set_sort_handler(arg)
814 sort_ref = newSVsv(arg);
817 set_search_handler(arg)
820 search_ref = newSVsv(arg);
824 set_fetch_handler(arg)
827 fetch_ref = newSVsv(arg);
831 set_present_handler(arg)
834 present_ref = newSVsv(arg);
838 set_esrequest_handler(arg)
841 esrequest_ref = newSVsv(arg);
845 set_delete_handler(arg)
848 delete_ref = newSVsv(arg);
852 set_scan_handler(arg)
855 scan_ref = newSVsv(arg);
867 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
869 for (i = 0; i < items; i++)
871 ptr = SvPV(ST(i), len);
872 *argv_buf = (char *)xmalloc(len + 1);
873 strcpy(*argv_buf++, ptr);
877 RETVAL = statserv_main(items, argv, bend_init, bend_close);