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>
59 SV *search_ref = NULL;
61 SV *present_ref = NULL;
62 SV *esrequest_ref = NULL;
63 SV *delete_ref = NULL;
67 static void oid2str(Odr_oid *o, WRBUF buf)
69 for (; *o >= 0; o++) {
71 sprintf(ibuf, "%d", *o);
72 wrbuf_puts(buf, ibuf);
79 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
82 case Z_RPNStructure_simple: {
83 Z_Operand *o = s->u.simple;
87 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
93 for (i = 0; i < at->attributes->num_attributes; i++) {
94 wrbuf_puts(buf, "@attr ");
95 if (at->attributes->attributes[i]->attributeSet) {
96 oid2str(at->attributes->attributes[i]->attributeSet, buf);
99 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
100 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
101 wrbuf_puts(buf, ibuf);
102 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
103 wrbuf_puts(buf, ibuf);
106 switch (at->term->which) {
107 case Z_Term_general: {
108 wrbuf_putc(buf, '"');
109 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
110 wrbuf_puts(buf, "\" ");
121 case Z_RPNStructure_complex: {
122 Z_Complex *c = s->u.complex;
124 switch (c->roperator->which) {
125 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
126 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
127 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
128 case Z_Operator_prox: abort();
131 if (!rpn2pquery(c->s1, buf))
133 if (!rpn2pquery(c->s2, buf))
143 WRBUF zquery2pquery(Z_Query *q)
145 WRBUF buf = wrbuf_alloc();
147 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
149 if (q->u.type_1->attributeSetId) {
150 /* Output attribute set ID */
151 wrbuf_puts(buf, "@attrset ");
152 oid2str(q->u.type_1->attributeSetId, buf);
153 wrbuf_putc(buf, ' ');
155 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
159 int bend_sort(void *handle, bend_sort_rr *rr)
170 char **input_setnames;
171 Zfront_handle *zhandle = (Zfront_handle *)handle;
179 input_setnames = rr->input_setnames;
180 for (i = 0; i < rr->num_input_setnames; i++)
182 av_push(aref, newSVpv(*input_setnames++, 0));
185 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
186 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
187 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
188 hv_store(href, "STATUS", 6, newSViv(0), 0);
192 XPUSHs(sv_2mortal(newRV( (SV*) href)));
196 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
200 temp = hv_fetch(href, "ERR_CODE", 8, 1);
201 err_code = newSVsv(*temp);
203 temp = hv_fetch(href, "ERR_STR", 7, 1);
204 err_str = newSVsv(*temp);
206 temp = hv_fetch(href, "STATUS", 6, 1);
207 status = newSVsv(*temp);
218 rr->errcode = SvIV(err_code);
219 rr->sort_status = SvIV(status);
220 ptr = SvPV(err_str, len);
221 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
222 strcpy(ODR_err_str, ptr);
223 rr->errstring = ODR_err_str;
233 int bend_search(void *handle, bend_search_rr *rr)
250 Zfront_handle *zhandle = (Zfront_handle *)handle;
257 basenames = rr->basenames;
258 for (i = 0; i < rr->num_bases; i++)
260 av_push(aref, newSVpv(*basenames++, 0));
263 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
264 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
265 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
266 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
267 hv_store(href, "HITS", 4, newSViv(0), 0);
268 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
269 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
270 query = zquery2pquery(rr->query);
273 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
281 XPUSHs(sv_2mortal(newRV( (SV*) href)));
285 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
289 temp = hv_fetch(href, "HITS", 4, 1);
290 hits = newSVsv(*temp);
292 temp = hv_fetch(href, "ERR_CODE", 8, 1);
293 err_code = newSVsv(*temp);
295 temp = hv_fetch(href, "ERR_STR", 7, 1);
296 err_str = newSVsv(*temp);
298 temp = hv_fetch(href, "HANDLE", 6, 1);
299 point = newSVsv(*temp);
307 rr->hits = SvIV(hits);
308 rr->errcode = SvIV(err_code);
309 ptr = SvPV(err_str, len);
310 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
311 strcpy(ODR_errstr, ptr);
312 rr->errstring = ODR_errstr;
313 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
314 memcpy(ODR_point, point, sizeof(*point));
315 zhandle->handle = ODR_point;*/
316 zhandle->handle = point;
321 sv_free( (SV*) aref);
322 sv_free( (SV*) href);
324 wrbuf_free(query, 1);
329 WRBUF oid2dotted(int *oid)
332 WRBUF buf = wrbuf_alloc();
335 for (; *oid != -1 ; oid++)
340 wrbuf_putc(buf, '.');
346 sprintf(ibuf, "%d", *oid);
347 wrbuf_puts(buf, ibuf);
353 int dotted2oid(char *dotted, int *buffer)
367 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
373 sscanf(ibuf, "%d", oid++);
386 sscanf(ibuf, "%d", oid++);
393 int bend_fetch(void *handle, bend_fetch_rr *rr)
411 Zfront_handle *zhandle = (Zfront_handle *)handle;
413 Z_RecordComposition *composition;
414 Z_ElementSetNames *simple;
424 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
425 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
426 oid_dotted = oid2dotted(rr->request_format_raw);
427 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
428 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
429 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
430 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
431 hv_store(href, "LAST", 4, newSViv(0), 0);
432 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
433 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
434 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
435 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
438 composition = rr->comp;
439 if (composition->which == Z_RecordComp_simple)
441 simple = composition->u.simple;
442 if (simple->which == Z_ElementSetNames_generic)
444 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
459 XPUSHs(sv_2mortal(newRV( (SV*) href)));
463 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
467 temp = hv_fetch(href, "BASENAME", 8, 1);
468 basename = newSVsv(*temp);
470 temp = hv_fetch(href, "RECORD", 6, 1);
471 record = newSVsv(*temp);
473 temp = hv_fetch(href, "LAST", 4, 1);
474 last = newSVsv(*temp);
476 temp = hv_fetch(href, "ERR_CODE", 8, 1);
477 err_code = newSVsv(*temp);
479 temp = hv_fetch(href, "ERR_STR", 7, 1),
480 err_string = newSVsv(*temp);
482 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
483 sur_flag = newSVsv(*temp);
485 temp = hv_fetch(href, "REP_FORM", 8, 1);
486 rep_form = newSVsv(*temp);
488 temp = hv_fetch(href, "HANDLE", 6, 1);
489 point = newSVsv(*temp);
497 ptr = SvPV(basename, length);
498 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
499 strcpy(ODR_basename, ptr);
500 rr->basename = ODR_basename;
502 ptr = SvPV(rep_form, length);
503 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
504 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
506 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
508 rr->output_format_raw = ODR_oid_buf;
510 ptr = SvPV(record, length);
511 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
512 strcpy(ODR_record, ptr);
513 rr->record = ODR_record;
516 zhandle->handle = point;
518 rr->last_in_set = SvIV(last);
522 rr->errcode = SvIV(err_code);
523 ptr = SvPV(err_string, length);
524 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
525 strcpy(ODR_errstr, ptr);
526 rr->errstring = ODR_errstr;
528 rr->surrogate_flag = SvIV(sur_flag);
530 wrbuf_free(oid_dotted, 1);
544 int bend_present(void *handle, bend_present_rr *rr)
554 Z_RecordComposition *composition;
555 Z_ElementSetNames *simple;
558 Zfront_handle *zhandle = (Zfront_handle *)handle;
560 /* WRBUF oid_dotted; */
567 hv_store(href, "HANDLE", 6, zhandle->handle, 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, "START", 5, newSViv(rr->start), 0);
571 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
572 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
573 /*oid_dotted = oid2dotted(rr->request_format_raw);
574 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
575 hv_store(href, "HITS", 4, newSViv(0), 0);
578 composition = rr->comp;
579 if (composition->which == Z_RecordComp_simple)
581 simple = composition->u.simple;
582 if (simple->which == Z_ElementSetNames_generic)
584 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
601 XPUSHs(sv_2mortal(newRV( (SV*) href)));
605 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
609 temp = hv_fetch(href, "ERR_CODE", 8, 1);
610 err_code = newSVsv(*temp);
612 temp = hv_fetch(href, "ERR_STR", 7, 1);
613 err_string = newSVsv(*temp);
615 temp = hv_fetch(href, "HITS", 4, 1);
616 hits = newSVsv(*temp);
618 temp = hv_fetch(href, "HANDLE", 6, 1);
619 point = newSVsv(*temp);
626 rr->errcode = SvIV(err_code);
627 rr->hits = SvIV(hits);
629 ptr = SvPV(err_string, len);
630 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
631 strcpy(ODR_errstr, ptr);
632 rr->errstring = ODR_errstr;
633 /* wrbuf_free(oid_dotted, 1);*/
634 zhandle->handle = point;
639 sv_free( (SV*) href);
645 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
647 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
652 int bend_delete(void *handle, bend_delete_rr *rr)
654 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
659 int bend_scan(void *handle, bend_scan_rr *rr)
661 perl_call_sv(scan_ref, G_VOID | G_DISCARD | G_NOARGS);
666 bend_initresult *bend_init(bend_initrequest *q)
668 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
675 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
687 /*q->bend_sort = bend_sort;*/
690 q->bend_search = bend_search;
694 q->bend_present = bend_present;
696 /*q->bend_esrequest = bend_esrequest;*/
697 /*q->bend_delete = bend_delete;*/
700 q->bend_fetch = bend_fetch;
702 /*q->bend_scan = bend_scan;*/
704 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
705 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
706 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
707 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
708 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
712 XPUSHs(sv_2mortal(newRV( (SV*) href)));
716 if (init_ref != NULL)
718 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
723 temp = hv_fetch(href, "IMP_NAME", 8, 1);
724 name = newSVsv(*temp);
726 temp = hv_fetch(href, "IMP_VER", 7, 1);
727 ver = newSVsv(*temp);
729 temp = hv_fetch(href, "ERR_CODE", 8, 1);
730 status = newSVsv(*temp);
732 temp = hv_fetch(href, "HANDLE", 6, 1);
733 handle= newSVsv(*temp);
739 zhandle->handle = handle;
740 r->errcode = SvIV(status);
742 ptr = SvPV(name, len);
743 q->implementation_name = (char *)xmalloc(len + 1);
744 strcpy(q->implementation_name, ptr);
745 /* q->implementation_name = SvPV(name, len);*/
746 ptr = SvPV(ver, len);
747 q->implementation_version = (char *)xmalloc(len + 1);
748 strcpy(q->implementation_version, ptr);
754 void bend_close(void *handle)
757 Zfront_handle *zhandle = (Zfront_handle *)handle;
764 if (close_ref == NULL)
770 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
774 XPUSHs(sv_2mortal(newRV((SV *)href)));
778 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
792 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
795 set_init_handler(arg)
798 init_ref = newSVsv(arg);
802 set_close_handler(arg)
805 close_ref = newSVsv(arg);
809 set_sort_handler(arg)
812 sort_ref = newSVsv(arg);
815 set_search_handler(arg)
818 search_ref = newSVsv(arg);
822 set_fetch_handler(arg)
825 fetch_ref = newSVsv(arg);
829 set_present_handler(arg)
832 present_ref = newSVsv(arg);
836 set_esrequest_handler(arg)
839 esrequest_ref = newSVsv(arg);
843 set_delete_handler(arg)
846 delete_ref = newSVsv(arg);
850 set_scan_handler(arg)
853 scan_ref = newSVsv(arg);
865 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
867 for (i = 0; i < items; i++)
869 ptr = SvPV(ST(i), len);
870 *argv_buf = (char *)xmalloc(len + 1);
871 strcpy(*argv_buf++, ptr);
875 RETVAL = statserv_main(items, argv, bend_init, bend_close);