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)
161 perl_call_sv(sort_ref, G_VOID | G_DISCARD | G_NOARGS);
166 int bend_search(void *handle, bend_search_rr *rr)
183 Zfront_handle *zhandle = (Zfront_handle *)handle;
190 basenames = rr->basenames;
191 for (i = 0; i < rr->num_bases; i++)
193 av_push(aref, newSVpv(*basenames++, 0));
196 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
197 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
198 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
199 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
200 hv_store(href, "HITS", 4, newSViv(0), 0);
201 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
202 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
203 query = zquery2pquery(rr->query);
206 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
214 XPUSHs(sv_2mortal(newRV( (SV*) href)));
218 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
222 temp = hv_fetch(href, "HITS", 4, 1);
223 hits = newSVsv(*temp);
225 temp = hv_fetch(href, "ERR_CODE", 8, 1);
226 err_code = newSVsv(*temp);
228 temp = hv_fetch(href, "ERR_STR", 7, 1);
229 err_str = newSVsv(*temp);
231 temp = hv_fetch(href, "HANDLE", 6, 1);
232 point = newSVsv(*temp);
240 rr->hits = SvIV(hits);
241 rr->errcode = SvIV(err_code);
242 ptr = SvPV(err_str, len);
243 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
244 strcpy(ODR_errstr, ptr);
245 rr->errstring = ODR_errstr;
246 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
247 memcpy(ODR_point, point, sizeof(*point));
248 zhandle->handle = ODR_point;*/
249 zhandle->handle = point;
254 sv_free( (SV*) aref);
255 sv_free( (SV*) href);
257 wrbuf_free(query, 1);
262 WRBUF oid2dotted(int *oid)
265 WRBUF buf = wrbuf_alloc();
268 for (; *oid != -1 ; oid++)
273 wrbuf_putc(buf, '.');
279 sprintf(ibuf, "%d", *oid);
280 wrbuf_puts(buf, ibuf);
286 int dotted2oid(char *dotted, int *buffer)
300 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
306 sscanf(ibuf, "%d", oid++);
319 sscanf(ibuf, "%d", oid++);
326 int bend_fetch(void *handle, bend_fetch_rr *rr)
344 Zfront_handle *zhandle = (Zfront_handle *)handle;
346 Z_RecordComposition *composition;
347 Z_ElementSetNames *simple;
357 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
358 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
359 oid_dotted = oid2dotted(rr->request_format_raw);
360 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
361 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
362 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
363 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
364 hv_store(href, "LAST", 4, newSViv(0), 0);
365 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
366 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
367 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
368 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
371 composition = rr->comp;
372 if (composition->which == Z_RecordComp_simple)
374 simple = composition->u.simple;
375 if (simple->which == 1)
377 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
392 XPUSHs(sv_2mortal(newRV( (SV*) href)));
396 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
400 temp = hv_fetch(href, "BASENAME", 8, 1);
401 basename = newSVsv(*temp);
403 temp = hv_fetch(href, "RECORD", 6, 1);
404 record = newSVsv(*temp);
406 temp = hv_fetch(href, "LAST", 4, 1);
407 last = newSVsv(*temp);
409 temp = hv_fetch(href, "ERR_CODE", 8, 1);
410 err_code = newSVsv(*temp);
412 temp = hv_fetch(href, "ERR_STR", 7, 1),
413 err_string = newSVsv(*temp);
415 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
416 sur_flag = newSVsv(*temp);
418 temp = hv_fetch(href, "REP_FORM", 8, 1);
419 rep_form = newSVsv(*temp);
421 temp = hv_fetch(href, "HANDLE", 6, 1);
422 point = newSVsv(*temp);
430 ptr = SvPV(basename, length);
431 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
432 strcpy(ODR_basename, ptr);
433 rr->basename = ODR_basename;
435 ptr = SvPV(rep_form, length);
436 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
437 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
439 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
441 rr->output_format_raw = ODR_oid_buf;
443 ptr = SvPV(record, length);
444 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
445 strcpy(ODR_record, ptr);
446 rr->record = ODR_record;
449 zhandle->handle = point;
451 rr->last_in_set = SvIV(last);
455 rr->errcode = SvIV(err_code);
456 ptr = SvPV(err_string, length);
457 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
458 strcpy(ODR_errstr, ptr);
459 rr->errstring = ODR_errstr;
461 rr->surrogate_flag = SvIV(sur_flag);
463 wrbuf_free(oid_dotted, 1);
477 int bend_present(void *handle, bend_present_rr *rr)
486 Z_RecordComposition *composition;
487 Z_ElementSetNames *simple;
496 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
497 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
498 hv_store(href, "START", 5, newSViv(rr->start), 0);
499 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
500 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
503 composition = rr->comp;
504 if (composition->which == 1)
506 simple = composition->u.simple;
507 if (simple->which == 1)
509 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
526 XPUSHs(sv_2mortal(newRV( (SV*) href)));
530 n = perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
534 temp = hv_fetch(href, "ERR_CODE", 8, 1);
535 err_code = newSVsv(*temp);
537 temp = hv_fetch(href, "ERR_STR", 7, 1);
538 err_string = newSVsv(*temp);
545 rr->errcode = SvIV(err_code);
547 ptr = SvPV(err_string, len);
548 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
549 strcpy(ODR_errstr, ptr);
550 rr->errstring = ODR_errstr;
554 sv_free( (SV*) href);
560 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
562 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
567 int bend_delete(void *handle, bend_delete_rr *rr)
569 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
574 int bend_scan(void *handle, bend_scan_rr *rr)
576 perl_call_sv(scan_ref, G_VOID | G_DISCARD | G_NOARGS);
581 bend_initresult *bend_init(bend_initrequest *q)
583 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
590 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
602 /*q->bend_sort = bend_sort;*/
605 q->bend_search = bend_search;
607 /*q->bend_present = present;*/
608 /*q->bend_esrequest = bend_esrequest;*/
609 /*q->bend_delete = bend_delete;*/
612 q->bend_fetch = bend_fetch;
614 /*q->bend_scan = bend_scan;*/
616 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
617 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
618 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
619 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
623 XPUSHs(sv_2mortal(newRV( (SV*) href)));
627 if (init_ref != NULL)
629 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
634 temp = hv_fetch(href, "IMP_NAME", 8, 1);
635 name = newSVsv(*temp);
637 temp = hv_fetch(href, "IMP_VER", 7, 1);
638 ver = newSVsv(*temp);
640 temp = hv_fetch(href, "ERR_CODE", 8, 1);
641 status = newSVsv(*temp);
643 temp = hv_fetch(href, "HANDLE", 6, 1);
644 handle= newSVsv(*temp);
650 zhandle->handle = handle;
651 r->errcode = SvIV(status);
653 ptr = SvPV(name, len);
654 q->implementation_name = (char *)xmalloc(len + 1);
655 strcpy(q->implementation_name, ptr);
656 /* q->implementation_name = SvPV(name, len);*/
657 ptr = SvPV(ver, len);
658 q->implementation_version = (char *)xmalloc(len + 1);
659 strcpy(q->implementation_version, ptr);
665 void bend_close(void *handle)
668 Zfront_handle *zhandle = (Zfront_handle *)handle;
675 if (close_ref == NULL)
681 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
685 XPUSHs(sv_2mortal(newRV((SV *)href)));
689 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
703 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
706 set_init_handler(arg)
709 init_ref = newSVsv(arg);
713 set_close_handler(arg)
716 close_ref = newSVsv(arg);
720 set_sort_handler(arg)
723 sort_ref = newSVsv(arg);
726 set_search_handler(arg)
729 search_ref = newSVsv(arg);
733 set_fetch_handler(arg)
736 fetch_ref = newSVsv(arg);
740 set_present_handler(arg)
743 present_ref = newSVsv(arg);
747 set_esrequest_handler(arg)
750 esrequest_ref = newSVsv(arg);
754 set_delete_handler(arg)
757 delete_ref = newSVsv(arg);
761 set_scan_handler(arg)
764 scan_ref = newSVsv(arg);
776 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
778 for (i = 0; i < items; i++)
780 ptr = SvPV(ST(i), len);
781 *argv_buf = (char *)xmalloc(len + 1);
782 strcpy(*argv_buf++, ptr);
786 RETVAL = statserv_main(items, argv, bend_init, bend_close);