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.8 2001-05-21 11:07:02 sondberg
29 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
31 /*Revision 1.7 2001/03/13 14:17:15 sondberg
32 /*Added support for GRS-1.
39 #include <yaz/backend.h>
41 #include <yaz/wrbuf.h>
46 #define GRS_MAX_FIELDS 500
50 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
51 #define sv_undef PL_sv_undef
71 SV *search_ref = NULL;
73 SV *present_ref = NULL;
74 SV *esrequest_ref = NULL;
75 SV *delete_ref = NULL;
80 Z_GenericRecord *read_grs1(char *str, ODR o)
83 char line[512], *buf, *ptr, *original;
85 Z_GenericRecord *r = 0;
93 ptr = strchr(str, '\n');
97 strncpy(line, str, ptr - str);
101 while (*buf && isspace(*buf))
104 memmove(original, str, strlen(str));
107 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
109 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
112 if (!type && *value == '0')
114 if (!(buf = strchr(buf, ')')))
117 while (*buf && isspace(*buf))
123 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
124 r->elements = (Z_TaggedElement **)
125 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
128 if (r->num_elements > GRS_MAX_FIELDS)
130 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
133 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
134 t->tagType = (int *)odr_malloc(o, sizeof(int));
136 t->tagValue = (Z_StringOrNumeric *)
137 odr_malloc(o, sizeof(Z_StringOrNumeric));
138 if ((ivalue = atoi(value)))
140 t->tagValue->which = Z_StringOrNumeric_numeric;
141 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
142 *t->tagValue->u.numeric = ivalue;
146 t->tagValue->which = Z_StringOrNumeric_string;
147 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
148 strcpy(t->tagValue->u.string, value);
150 t->tagOccurrence = 0;
152 t->appliedVariant = 0;
153 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
156 c->which = Z_ElementData_subtree;
157 c->u.subtree = read_grs1(str, o);
161 c->which = Z_ElementData_string;
162 /* buf[strlen(buf)-1] = '\0';*/
163 buf[strlen(buf)] = '\0';
164 c->u.string = odr_strdup(o, buf);
173 static void oid2str(Odr_oid *o, WRBUF buf)
175 for (; *o >= 0; o++) {
177 sprintf(ibuf, "%d", *o);
178 wrbuf_puts(buf, ibuf);
180 wrbuf_putc(buf, '.');
185 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
188 case Z_RPNStructure_simple: {
189 Z_Operand *o = s->u.simple;
192 case Z_Operand_APT: {
193 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
195 if (at->attributes) {
199 for (i = 0; i < at->attributes->num_attributes; i++) {
200 wrbuf_puts(buf, "@attr ");
201 if (at->attributes->attributes[i]->attributeSet) {
202 oid2str(at->attributes->attributes[i]->attributeSet, buf);
203 wrbuf_putc(buf, ' ');
205 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
206 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
207 wrbuf_puts(buf, ibuf);
208 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
209 wrbuf_puts(buf, ibuf);
212 switch (at->term->which) {
213 case Z_Term_general: {
214 wrbuf_putc(buf, '"');
215 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
216 wrbuf_puts(buf, "\" ");
227 case Z_RPNStructure_complex: {
228 Z_Complex *c = s->u.complex;
230 switch (c->roperator->which) {
231 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
232 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
233 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
234 case Z_Operator_prox: abort();
237 if (!rpn2pquery(c->s1, buf))
239 if (!rpn2pquery(c->s2, buf))
249 WRBUF zquery2pquery(Z_Query *q)
251 WRBUF buf = wrbuf_alloc();
253 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
255 if (q->u.type_1->attributeSetId) {
256 /* Output attribute set ID */
257 wrbuf_puts(buf, "@attrset ");
258 oid2str(q->u.type_1->attributeSetId, buf);
259 wrbuf_putc(buf, ' ');
261 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
265 int bend_sort(void *handle, bend_sort_rr *rr)
276 char **input_setnames;
277 Zfront_handle *zhandle = (Zfront_handle *)handle;
285 input_setnames = rr->input_setnames;
286 for (i = 0; i < rr->num_input_setnames; i++)
288 av_push(aref, newSVpv(*input_setnames++, 0));
291 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
292 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
293 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
294 hv_store(href, "STATUS", 6, newSViv(0), 0);
298 XPUSHs(sv_2mortal(newRV( (SV*) href)));
302 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
306 temp = hv_fetch(href, "ERR_CODE", 8, 1);
307 err_code = newSVsv(*temp);
309 temp = hv_fetch(href, "ERR_STR", 7, 1);
310 err_str = newSVsv(*temp);
312 temp = hv_fetch(href, "STATUS", 6, 1);
313 status = newSVsv(*temp);
324 rr->errcode = SvIV(err_code);
325 rr->sort_status = SvIV(status);
326 ptr = SvPV(err_str, len);
327 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
328 strcpy(ODR_err_str, ptr);
329 rr->errstring = ODR_err_str;
339 int bend_search(void *handle, bend_search_rr *rr)
356 Zfront_handle *zhandle = (Zfront_handle *)handle;
363 basenames = rr->basenames;
364 for (i = 0; i < rr->num_bases; i++)
366 av_push(aref, newSVpv(*basenames++, 0));
369 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
370 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
371 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
372 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
373 hv_store(href, "HITS", 4, newSViv(0), 0);
374 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
375 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
376 hv_store(href, "PID", 3, newSViv(getpid()), 0);
377 query = zquery2pquery(rr->query);
380 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
388 XPUSHs(sv_2mortal(newRV( (SV*) href)));
392 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
396 temp = hv_fetch(href, "HITS", 4, 1);
397 hits = newSVsv(*temp);
399 temp = hv_fetch(href, "ERR_CODE", 8, 1);
400 err_code = newSVsv(*temp);
402 temp = hv_fetch(href, "ERR_STR", 7, 1);
403 err_str = newSVsv(*temp);
405 temp = hv_fetch(href, "HANDLE", 6, 1);
406 point = newSVsv(*temp);
414 rr->hits = SvIV(hits);
415 rr->errcode = SvIV(err_code);
416 ptr = SvPV(err_str, len);
417 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
418 strcpy(ODR_errstr, ptr);
419 rr->errstring = ODR_errstr;
420 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
421 memcpy(ODR_point, point, sizeof(*point));
422 zhandle->handle = ODR_point;*/
423 zhandle->handle = point;
428 sv_free( (SV*) aref);
429 sv_free( (SV*) href);
431 wrbuf_free(query, 1);
436 WRBUF oid2dotted(int *oid)
439 WRBUF buf = wrbuf_alloc();
442 for (; *oid != -1 ; oid++)
447 wrbuf_putc(buf, '.');
453 sprintf(ibuf, "%d", *oid);
454 wrbuf_puts(buf, ibuf);
460 int dotted2oid(char *dotted, int *buffer)
474 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
480 sscanf(ibuf, "%d", oid++);
493 sscanf(ibuf, "%d", oid++);
500 int bend_fetch(void *handle, bend_fetch_rr *rr)
519 Zfront_handle *zhandle = (Zfront_handle *)handle;
521 Z_RecordComposition *composition;
522 Z_ElementSetNames *simple;
531 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
532 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
533 oid_dotted = oid2dotted(rr->request_format_raw);
534 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
535 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
536 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
537 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
538 hv_store(href, "LAST", 4, newSViv(0), 0);
539 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
540 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
541 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
542 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
543 hv_store(href, "PID", 3, newSViv(getpid()), 0);
546 composition = rr->comp;
547 if (composition->which == Z_RecordComp_simple)
549 simple = composition->u.simple;
550 if (simple->which == Z_ElementSetNames_generic)
552 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
567 XPUSHs(sv_2mortal(newRV( (SV*) href)));
571 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
575 temp = hv_fetch(href, "BASENAME", 8, 1);
576 basename = newSVsv(*temp);
578 temp = hv_fetch(href, "RECORD", 6, 1);
579 record = newSVsv(*temp);
581 temp = hv_fetch(href, "LAST", 4, 1);
582 last = newSVsv(*temp);
584 temp = hv_fetch(href, "ERR_CODE", 8, 1);
585 err_code = newSVsv(*temp);
587 temp = hv_fetch(href, "ERR_STR", 7, 1),
588 err_string = newSVsv(*temp);
590 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
591 sur_flag = newSVsv(*temp);
593 temp = hv_fetch(href, "REP_FORM", 8, 1);
594 rep_form = newSVsv(*temp);
596 temp = hv_fetch(href, "HANDLE", 6, 1);
597 point = newSVsv(*temp);
605 ptr = SvPV(basename, length);
606 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
607 strcpy(ODR_basename, ptr);
608 rr->basename = ODR_basename;
610 ptr = SvPV(rep_form, length);
611 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
612 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
614 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
616 rr->output_format_raw = ODR_oid_buf;
618 ptr = SvPV(record, length);
619 oid = oid_getentbyoid(ODR_oid_buf);
620 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
622 rr->record = (char *) read_grs1(ptr, rr->stream);
627 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
628 strcpy(ODR_record, ptr);
629 rr->record = ODR_record;
632 zhandle->handle = point;
634 rr->last_in_set = SvIV(last);
638 rr->errcode = SvIV(err_code);
639 ptr = SvPV(err_string, length);
640 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
641 strcpy(ODR_errstr, ptr);
642 rr->errstring = ODR_errstr;
644 rr->surrogate_flag = SvIV(sur_flag);
646 wrbuf_free(oid_dotted, 1);
660 int bend_present(void *handle, bend_present_rr *rr)
670 Z_RecordComposition *composition;
671 Z_ElementSetNames *simple;
674 Zfront_handle *zhandle = (Zfront_handle *)handle;
676 /* WRBUF oid_dotted; */
683 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
684 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
685 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
686 hv_store(href, "START", 5, newSViv(rr->start), 0);
687 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
688 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
689 /*oid_dotted = oid2dotted(rr->request_format_raw);
690 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
691 hv_store(href, "HITS", 4, newSViv(0), 0);
692 hv_store(href, "PID", 3, newSViv(getpid()), 0);
695 composition = rr->comp;
696 if (composition->which == Z_RecordComp_simple)
698 simple = composition->u.simple;
699 if (simple->which == Z_ElementSetNames_generic)
701 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
718 XPUSHs(sv_2mortal(newRV( (SV*) href)));
722 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
726 temp = hv_fetch(href, "ERR_CODE", 8, 1);
727 err_code = newSVsv(*temp);
729 temp = hv_fetch(href, "ERR_STR", 7, 1);
730 err_string = newSVsv(*temp);
732 temp = hv_fetch(href, "HITS", 4, 1);
733 hits = newSVsv(*temp);
735 temp = hv_fetch(href, "HANDLE", 6, 1);
736 point = newSVsv(*temp);
743 rr->errcode = SvIV(err_code);
744 rr->hits = SvIV(hits);
746 ptr = SvPV(err_string, len);
747 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
748 strcpy(ODR_errstr, ptr);
749 rr->errstring = ODR_errstr;
750 /* wrbuf_free(oid_dotted, 1);*/
751 zhandle->handle = point;
756 sv_free( (SV*) href);
762 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
764 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
769 int bend_delete(void *handle, bend_delete_rr *rr)
771 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
776 int bend_scan(void *handle, bend_scan_rr *rr)
778 perl_call_sv(scan_ref, G_VOID | G_DISCARD | G_NOARGS);
783 bend_initresult *bend_init(bend_initrequest *q)
785 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
792 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
804 /*q->bend_sort = bend_sort;*/
807 q->bend_search = bend_search;
811 q->bend_present = bend_present;
813 /*q->bend_esrequest = bend_esrequest;*/
814 /*q->bend_delete = bend_delete;*/
817 q->bend_fetch = bend_fetch;
819 /*q->bend_scan = bend_scan;*/
821 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
822 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
823 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
824 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
825 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
826 hv_store(href, "PID", 3, newSViv(getpid()), 0);
830 XPUSHs(sv_2mortal(newRV( (SV*) href)));
834 if (init_ref != NULL)
836 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
841 temp = hv_fetch(href, "IMP_NAME", 8, 1);
842 name = newSVsv(*temp);
844 temp = hv_fetch(href, "IMP_VER", 7, 1);
845 ver = newSVsv(*temp);
847 temp = hv_fetch(href, "ERR_CODE", 8, 1);
848 status = newSVsv(*temp);
850 temp = hv_fetch(href, "HANDLE", 6, 1);
851 handle= newSVsv(*temp);
857 zhandle->handle = handle;
858 r->errcode = SvIV(status);
860 ptr = SvPV(name, len);
861 q->implementation_name = (char *)xmalloc(len + 1);
862 strcpy(q->implementation_name, ptr);
863 /* q->implementation_name = SvPV(name, len);*/
864 ptr = SvPV(ver, len);
865 q->implementation_version = (char *)xmalloc(len + 1);
866 strcpy(q->implementation_version, ptr);
872 void bend_close(void *handle)
875 Zfront_handle *zhandle = (Zfront_handle *)handle;
882 if (close_ref == NULL)
888 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
892 XPUSHs(sv_2mortal(newRV((SV *)href)));
896 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
910 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
913 set_init_handler(arg)
916 init_ref = newSVsv(arg);
920 set_close_handler(arg)
923 close_ref = newSVsv(arg);
927 set_sort_handler(arg)
930 sort_ref = newSVsv(arg);
933 set_search_handler(arg)
936 search_ref = newSVsv(arg);
940 set_fetch_handler(arg)
943 fetch_ref = newSVsv(arg);
947 set_present_handler(arg)
950 present_ref = newSVsv(arg);
954 set_esrequest_handler(arg)
957 esrequest_ref = newSVsv(arg);
961 set_delete_handler(arg)
964 delete_ref = newSVsv(arg);
968 set_scan_handler(arg)
971 scan_ref = newSVsv(arg);
983 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
985 for (i = 0; i < items; i++)
987 ptr = SvPV(ST(i), len);
988 *argv_buf = (char *)xmalloc(len + 1);
989 strcpy(*argv_buf++, ptr);
993 RETVAL = statserv_main(items, argv, bend_init, bend_close);