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
27 /*$Log: SimpleServer.xs,v $
28 /*Revision 1.7 2001-03-13 14:17:15 sondberg
29 /*Added support for GRS-1.
36 #include <yaz/backend.h>
38 #include <yaz/wrbuf.h>
43 #define GRS_MAX_FIELDS 50
47 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
48 #define sv_undef PL_sv_undef
68 SV *search_ref = NULL;
70 SV *present_ref = NULL;
71 SV *esrequest_ref = NULL;
72 SV *delete_ref = NULL;
77 Z_GenericRecord *read_grs1(char *str, ODR o)
80 char line[512], *buf, *ptr, *original;
82 Z_GenericRecord *r = 0;
90 ptr = strchr(str, '\n');
94 strncpy(line, str, ptr - str);
98 while (*buf && isspace(*buf))
101 memmove(original, str, strlen(str));
104 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
106 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
109 if (!type && *value == '0')
111 if (!(buf = strchr(buf, ')')))
114 while (*buf && isspace(*buf))
120 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
121 r->elements = (Z_TaggedElement **)
122 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
125 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
126 t->tagType = (int *)odr_malloc(o, sizeof(int));
128 t->tagValue = (Z_StringOrNumeric *)
129 odr_malloc(o, sizeof(Z_StringOrNumeric));
130 if ((ivalue = atoi(value)))
132 t->tagValue->which = Z_StringOrNumeric_numeric;
133 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
134 *t->tagValue->u.numeric = ivalue;
138 t->tagValue->which = Z_StringOrNumeric_string;
139 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
140 strcpy(t->tagValue->u.string, value);
142 t->tagOccurrence = 0;
144 t->appliedVariant = 0;
145 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
148 c->which = Z_ElementData_subtree;
149 c->u.subtree = read_grs1(str, o);
153 c->which = Z_ElementData_string;
154 /* buf[strlen(buf)-1] = '\0';*/
155 buf[strlen(buf)] = '\0';
156 c->u.string = odr_strdup(o, buf);
165 static void oid2str(Odr_oid *o, WRBUF buf)
167 for (; *o >= 0; o++) {
169 sprintf(ibuf, "%d", *o);
170 wrbuf_puts(buf, ibuf);
172 wrbuf_putc(buf, '.');
177 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
180 case Z_RPNStructure_simple: {
181 Z_Operand *o = s->u.simple;
184 case Z_Operand_APT: {
185 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
187 if (at->attributes) {
191 for (i = 0; i < at->attributes->num_attributes; i++) {
192 wrbuf_puts(buf, "@attr ");
193 if (at->attributes->attributes[i]->attributeSet) {
194 oid2str(at->attributes->attributes[i]->attributeSet, buf);
195 wrbuf_putc(buf, ' ');
197 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
198 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
199 wrbuf_puts(buf, ibuf);
200 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
201 wrbuf_puts(buf, ibuf);
204 switch (at->term->which) {
205 case Z_Term_general: {
206 wrbuf_putc(buf, '"');
207 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
208 wrbuf_puts(buf, "\" ");
219 case Z_RPNStructure_complex: {
220 Z_Complex *c = s->u.complex;
222 switch (c->roperator->which) {
223 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
224 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
225 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
226 case Z_Operator_prox: abort();
229 if (!rpn2pquery(c->s1, buf))
231 if (!rpn2pquery(c->s2, buf))
241 WRBUF zquery2pquery(Z_Query *q)
243 WRBUF buf = wrbuf_alloc();
245 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
247 if (q->u.type_1->attributeSetId) {
248 /* Output attribute set ID */
249 wrbuf_puts(buf, "@attrset ");
250 oid2str(q->u.type_1->attributeSetId, buf);
251 wrbuf_putc(buf, ' ');
253 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
257 int bend_sort(void *handle, bend_sort_rr *rr)
268 char **input_setnames;
269 Zfront_handle *zhandle = (Zfront_handle *)handle;
277 input_setnames = rr->input_setnames;
278 for (i = 0; i < rr->num_input_setnames; i++)
280 av_push(aref, newSVpv(*input_setnames++, 0));
283 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
284 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
285 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
286 hv_store(href, "STATUS", 6, newSViv(0), 0);
290 XPUSHs(sv_2mortal(newRV( (SV*) href)));
294 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
298 temp = hv_fetch(href, "ERR_CODE", 8, 1);
299 err_code = newSVsv(*temp);
301 temp = hv_fetch(href, "ERR_STR", 7, 1);
302 err_str = newSVsv(*temp);
304 temp = hv_fetch(href, "STATUS", 6, 1);
305 status = newSVsv(*temp);
316 rr->errcode = SvIV(err_code);
317 rr->sort_status = SvIV(status);
318 ptr = SvPV(err_str, len);
319 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
320 strcpy(ODR_err_str, ptr);
321 rr->errstring = ODR_err_str;
331 int bend_search(void *handle, bend_search_rr *rr)
348 Zfront_handle *zhandle = (Zfront_handle *)handle;
355 basenames = rr->basenames;
356 for (i = 0; i < rr->num_bases; i++)
358 av_push(aref, newSVpv(*basenames++, 0));
361 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
362 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
363 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
364 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
365 hv_store(href, "HITS", 4, newSViv(0), 0);
366 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
367 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
368 hv_store(href, "PID", 3, newSViv(getpid()), 0);
369 query = zquery2pquery(rr->query);
372 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
380 XPUSHs(sv_2mortal(newRV( (SV*) href)));
384 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
388 temp = hv_fetch(href, "HITS", 4, 1);
389 hits = newSVsv(*temp);
391 temp = hv_fetch(href, "ERR_CODE", 8, 1);
392 err_code = newSVsv(*temp);
394 temp = hv_fetch(href, "ERR_STR", 7, 1);
395 err_str = newSVsv(*temp);
397 temp = hv_fetch(href, "HANDLE", 6, 1);
398 point = newSVsv(*temp);
406 rr->hits = SvIV(hits);
407 rr->errcode = SvIV(err_code);
408 ptr = SvPV(err_str, len);
409 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
410 strcpy(ODR_errstr, ptr);
411 rr->errstring = ODR_errstr;
412 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
413 memcpy(ODR_point, point, sizeof(*point));
414 zhandle->handle = ODR_point;*/
415 zhandle->handle = point;
420 sv_free( (SV*) aref);
421 sv_free( (SV*) href);
423 wrbuf_free(query, 1);
428 WRBUF oid2dotted(int *oid)
431 WRBUF buf = wrbuf_alloc();
434 for (; *oid != -1 ; oid++)
439 wrbuf_putc(buf, '.');
445 sprintf(ibuf, "%d", *oid);
446 wrbuf_puts(buf, ibuf);
452 int dotted2oid(char *dotted, int *buffer)
466 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
472 sscanf(ibuf, "%d", oid++);
485 sscanf(ibuf, "%d", oid++);
492 int bend_fetch(void *handle, bend_fetch_rr *rr)
511 Zfront_handle *zhandle = (Zfront_handle *)handle;
513 Z_RecordComposition *composition;
514 Z_ElementSetNames *simple;
523 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
524 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
525 oid_dotted = oid2dotted(rr->request_format_raw);
526 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
527 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
528 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
529 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
530 hv_store(href, "LAST", 4, newSViv(0), 0);
531 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
532 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
533 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
534 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
535 hv_store(href, "PID", 3, newSViv(getpid()), 0);
538 composition = rr->comp;
539 if (composition->which == Z_RecordComp_simple)
541 simple = composition->u.simple;
542 if (simple->which == Z_ElementSetNames_generic)
544 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
559 XPUSHs(sv_2mortal(newRV( (SV*) href)));
563 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
567 temp = hv_fetch(href, "BASENAME", 8, 1);
568 basename = newSVsv(*temp);
570 temp = hv_fetch(href, "RECORD", 6, 1);
571 record = newSVsv(*temp);
573 temp = hv_fetch(href, "LAST", 4, 1);
574 last = newSVsv(*temp);
576 temp = hv_fetch(href, "ERR_CODE", 8, 1);
577 err_code = newSVsv(*temp);
579 temp = hv_fetch(href, "ERR_STR", 7, 1),
580 err_string = newSVsv(*temp);
582 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
583 sur_flag = newSVsv(*temp);
585 temp = hv_fetch(href, "REP_FORM", 8, 1);
586 rep_form = newSVsv(*temp);
588 temp = hv_fetch(href, "HANDLE", 6, 1);
589 point = newSVsv(*temp);
597 ptr = SvPV(basename, length);
598 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
599 strcpy(ODR_basename, ptr);
600 rr->basename = ODR_basename;
602 ptr = SvPV(rep_form, length);
603 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
604 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
606 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
608 rr->output_format_raw = ODR_oid_buf;
610 ptr = SvPV(record, length);
611 oid = oid_getentbyoid(ODR_oid_buf);
612 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
614 rr->record = (char *) read_grs1(ptr, rr->stream);
619 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
620 strcpy(ODR_record, ptr);
621 rr->record = ODR_record;
624 zhandle->handle = point;
626 rr->last_in_set = SvIV(last);
630 rr->errcode = SvIV(err_code);
631 ptr = SvPV(err_string, length);
632 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
633 strcpy(ODR_errstr, ptr);
634 rr->errstring = ODR_errstr;
636 rr->surrogate_flag = SvIV(sur_flag);
638 wrbuf_free(oid_dotted, 1);
652 int bend_present(void *handle, bend_present_rr *rr)
662 Z_RecordComposition *composition;
663 Z_ElementSetNames *simple;
666 Zfront_handle *zhandle = (Zfront_handle *)handle;
668 /* WRBUF oid_dotted; */
675 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
676 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
677 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
678 hv_store(href, "START", 5, newSViv(rr->start), 0);
679 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
680 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
681 /*oid_dotted = oid2dotted(rr->request_format_raw);
682 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
683 hv_store(href, "HITS", 4, newSViv(0), 0);
684 hv_store(href, "PID", 3, newSViv(getpid()), 0);
687 composition = rr->comp;
688 if (composition->which == Z_RecordComp_simple)
690 simple = composition->u.simple;
691 if (simple->which == Z_ElementSetNames_generic)
693 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
710 XPUSHs(sv_2mortal(newRV( (SV*) href)));
714 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
718 temp = hv_fetch(href, "ERR_CODE", 8, 1);
719 err_code = newSVsv(*temp);
721 temp = hv_fetch(href, "ERR_STR", 7, 1);
722 err_string = newSVsv(*temp);
724 temp = hv_fetch(href, "HITS", 4, 1);
725 hits = newSVsv(*temp);
727 temp = hv_fetch(href, "HANDLE", 6, 1);
728 point = newSVsv(*temp);
735 rr->errcode = SvIV(err_code);
736 rr->hits = SvIV(hits);
738 ptr = SvPV(err_string, len);
739 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
740 strcpy(ODR_errstr, ptr);
741 rr->errstring = ODR_errstr;
742 /* wrbuf_free(oid_dotted, 1);*/
743 zhandle->handle = point;
748 sv_free( (SV*) href);
754 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
756 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
761 int bend_delete(void *handle, bend_delete_rr *rr)
763 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
768 int bend_scan(void *handle, bend_scan_rr *rr)
770 perl_call_sv(scan_ref, G_VOID | G_DISCARD | G_NOARGS);
775 bend_initresult *bend_init(bend_initrequest *q)
777 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
784 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
796 /*q->bend_sort = bend_sort;*/
799 q->bend_search = bend_search;
803 q->bend_present = bend_present;
805 /*q->bend_esrequest = bend_esrequest;*/
806 /*q->bend_delete = bend_delete;*/
809 q->bend_fetch = bend_fetch;
811 /*q->bend_scan = bend_scan;*/
813 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
814 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
815 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
816 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
817 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
818 hv_store(href, "PID", 3, newSViv(getpid()), 0);
822 XPUSHs(sv_2mortal(newRV( (SV*) href)));
826 if (init_ref != NULL)
828 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
833 temp = hv_fetch(href, "IMP_NAME", 8, 1);
834 name = newSVsv(*temp);
836 temp = hv_fetch(href, "IMP_VER", 7, 1);
837 ver = newSVsv(*temp);
839 temp = hv_fetch(href, "ERR_CODE", 8, 1);
840 status = newSVsv(*temp);
842 temp = hv_fetch(href, "HANDLE", 6, 1);
843 handle= newSVsv(*temp);
849 zhandle->handle = handle;
850 r->errcode = SvIV(status);
852 ptr = SvPV(name, len);
853 q->implementation_name = (char *)xmalloc(len + 1);
854 strcpy(q->implementation_name, ptr);
855 /* q->implementation_name = SvPV(name, len);*/
856 ptr = SvPV(ver, len);
857 q->implementation_version = (char *)xmalloc(len + 1);
858 strcpy(q->implementation_version, ptr);
864 void bend_close(void *handle)
867 Zfront_handle *zhandle = (Zfront_handle *)handle;
874 if (close_ref == NULL)
880 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
884 XPUSHs(sv_2mortal(newRV((SV *)href)));
888 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
902 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
905 set_init_handler(arg)
908 init_ref = newSVsv(arg);
912 set_close_handler(arg)
915 close_ref = newSVsv(arg);
919 set_sort_handler(arg)
922 sort_ref = newSVsv(arg);
925 set_search_handler(arg)
928 search_ref = newSVsv(arg);
932 set_fetch_handler(arg)
935 fetch_ref = newSVsv(arg);
939 set_present_handler(arg)
942 present_ref = newSVsv(arg);
946 set_esrequest_handler(arg)
949 esrequest_ref = newSVsv(arg);
953 set_delete_handler(arg)
956 delete_ref = newSVsv(arg);
960 set_scan_handler(arg)
963 scan_ref = newSVsv(arg);
975 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
977 for (i = 0; i < items; i++)
979 ptr = SvPV(ST(i), len);
980 *argv_buf = (char *)xmalloc(len + 1);
981 strcpy(*argv_buf++, ptr);
985 RETVAL = statserv_main(items, argv, bend_init, bend_close);