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.20 2003-09-09 20:12:38 mike
29 /*Return diagnostics on Init failure
31 /*Revision 1.19 2003/09/09 11:40:10 mike
32 /*(Finally!) support implementation-ID
34 /*Revision 1.18 2003/01/03 09:05:41 sondberg
35 /*Applied Dave's GRS-1 patch - actually this was already done in revision 1.17.
37 /*Revision 1.16 2002/11/26 17:09:18 mike
38 /*basic support for idPass authentication
40 /*Revision 1.15 2002/09/16 13:55:53 sondberg
41 /*Added support for authentication into SimpleServer.
43 /*Revision 1.14 2002/03/05 00:34:13 mike
44 /*Support for implementation_id (commented out until it's
47 /*Revision 1.13 2002/02/28 11:21:57 mike
48 /*Add RPN structure to search-handler argument hash.
50 /*Revision 1.12 2001/08/30 14:02:10 sondberg
53 /*Revision 1.11 2001/08/30 13:15:11 sondberg
54 /*Corrected a memory leak, one more to go.
56 /*Revision 1.10 2001/08/29 11:48:36 sondberg
59 /* Net::Z3950::SimpleServer::ScanSuccess
60 /* Net::Z3950::SimpleServer::ScanPartial
62 /*and a bit of documentation.
64 /*Revision 1.9 2001/08/24 14:00:20 sondberg
65 /*Added support for scan.
67 /*Revision 1.8 2001/05/21 11:07:02 sondberg
68 /*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
70 /*Revision 1.7 2001/03/13 14:17:15 sondberg
71 /*Added support for GRS-1.
78 #include <yaz/backend.h>
80 #include <yaz/wrbuf.h>
85 #define GRS_MAX_FIELDS 500
89 #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */
90 #define sv_undef PL_sv_undef
108 SV *close_ref = NULL;
110 SV *search_ref = NULL;
111 SV *fetch_ref = NULL;
112 SV *present_ref = NULL;
113 SV *esrequest_ref = NULL;
114 SV *delete_ref = NULL;
118 #define GRS_BUF_SIZE 512
120 Z_GenericRecord *read_grs1(char *str, ODR o)
123 char line[GRS_BUF_SIZE+1], *buf, *ptr, *original;
124 char value[GRS_BUF_SIZE+1];
125 Z_GenericRecord *r = 0;
128 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
129 r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
138 ptr = strchr(str, '\n');
143 if (len > GRS_BUF_SIZE) {
144 yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
147 strncpy(line, str, len);
151 while (*buf && isspace(*buf))
154 memmove(original, str, strlen(str));
157 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
159 yaz_log(LOG_WARN, "Bad data in '%s'", buf);
162 if (!type && *value == '0')
164 if (!(buf = strchr(buf, ')')))
167 while (*buf && isspace(*buf))
169 if (r->num_elements >= GRS_MAX_FIELDS)
171 yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
174 r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
175 t->tagType = (int *)odr_malloc(o, sizeof(int));
177 t->tagValue = (Z_StringOrNumeric *)
178 odr_malloc(o, sizeof(Z_StringOrNumeric));
179 if ((ivalue = atoi(value)))
181 t->tagValue->which = Z_StringOrNumeric_numeric;
182 t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
183 *t->tagValue->u.numeric = ivalue;
187 t->tagValue->which = Z_StringOrNumeric_string;
188 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
189 strcpy(t->tagValue->u.string, value);
191 t->tagOccurrence = 0;
193 t->appliedVariant = 0;
194 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
197 c->which = Z_ElementData_subtree;
198 c->u.subtree = read_grs1(str, o);
202 c->which = Z_ElementData_string;
203 c->u.string = odr_strdup(o, buf);
212 static void oid2str(Odr_oid *o, WRBUF buf)
214 for (; *o >= 0; o++) {
216 sprintf(ibuf, "%d", *o);
217 wrbuf_puts(buf, ibuf);
219 wrbuf_putc(buf, '.');
224 static int rpn2pquery(Z_RPNStructure *s, WRBUF buf)
227 case Z_RPNStructure_simple: {
228 Z_Operand *o = s->u.simple;
231 case Z_Operand_APT: {
232 Z_AttributesPlusTerm *at = o->u.attributesPlusTerm;
234 if (at->attributes) {
238 for (i = 0; i < at->attributes->num_attributes; i++) {
239 wrbuf_puts(buf, "@attr ");
240 if (at->attributes->attributes[i]->attributeSet) {
241 oid2str(at->attributes->attributes[i]->attributeSet, buf);
242 wrbuf_putc(buf, ' ');
244 sprintf(ibuf, "%d=", *at->attributes->attributes[i]->attributeType);
245 assert(at->attributes->attributes[i]->which == Z_AttributeValue_numeric);
246 wrbuf_puts(buf, ibuf);
247 sprintf(ibuf, "%d ", *at->attributes->attributes[i]->value.numeric);
248 wrbuf_puts(buf, ibuf);
251 switch (at->term->which) {
252 case Z_Term_general: {
253 wrbuf_putc(buf, '"');
254 wrbuf_write(buf, (char*) at->term->u.general->buf, at->term->u.general->len);
255 wrbuf_puts(buf, "\" ");
266 case Z_RPNStructure_complex: {
267 Z_Complex *c = s->u.complex;
269 switch (c->roperator->which) {
270 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
271 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
272 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
273 case Z_Operator_prox: abort();
276 if (!rpn2pquery(c->s1, buf))
278 if (!rpn2pquery(c->s2, buf))
288 WRBUF zquery2pquery(Z_Query *q)
290 WRBUF buf = wrbuf_alloc();
292 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
294 if (q->u.type_1->attributeSetId) {
295 /* Output attribute set ID */
296 wrbuf_puts(buf, "@attrset ");
297 oid2str(q->u.type_1->attributeSetId, buf);
298 wrbuf_putc(buf, ' ');
300 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
304 /* Lifted verbatim from Net::Z3950 yazwrap/util.c */
306 void fatal(char *fmt, ...)
310 fprintf(stderr, "FATAL (yazwrap): ");
312 vfprintf(stderr, fmt, ap);
314 fprintf(stderr, "\n");
319 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
321 * Creates a new Perl object of type `class'; the newly-created scalar
322 * that is a reference to the blessed thingy `referent' is returned.
324 static SV *newObject(char *class, SV *referent)
329 sv = newRV_noinc((SV*) referent);
330 stash = gv_stashpv(class, 0);
332 fatal("attempt to create object of undefined class '%s'", class);
333 /*assert(stash != 0);*/
339 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
340 static void setMember(HV *hv, char *name, SV *val)
342 /* We don't increment `val's reference count -- I think this is
343 * right because it's created with a refcount of 1, and in fact
344 * the reference via this hash is the only reference to it in
347 if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0))
348 fatal("couldn't store member in hash");
352 /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */
353 static SV *translateOID(Odr_oid *x)
355 /* Yaz represents an OID by an int array terminated by a negative
356 * value, typically -1; we represent it as a reference to a
357 * blessed scalar string of "."-separated elements.
363 for (i = 0; x[i] >= 0; i++) {
364 sprintf(buf + strlen(buf), "%d", (int) x[i]);
370 * ### We'd like to return a blessed scalar (string) here, but of
371 * course you can't do that in Perl: only references can be
372 * blessed, so we'd have to return a _reference_ to a string, and
373 * bless _that_. Better to do without the blessing, I think.
376 return newSVpv(buf, 0);
378 return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0));
383 static SV *rpn2perl(Z_RPNStructure *s)
390 case Z_RPNStructure_simple: {
391 Z_Operand *o = s->u.simple;
392 Z_AttributesPlusTerm *at;
393 if (o->which != Z_Operand_APT)
394 fatal("can't handle RPN simples other than APT");
395 at = o->u.attributesPlusTerm;
396 if (at->term->which != Z_Term_general)
397 fatal("can't handle RPN terms other than general");
399 sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV()));
400 if (at->attributes) {
402 SV *attrs = newObject("Net::Z3950::RPN::Attributes",
403 (SV*) (av = newAV()));
404 for (i = 0; i < at->attributes->num_attributes; i++) {
405 Z_AttributeElement *elem = at->attributes->attributes[i];
407 SV *tmp = newObject("Net::Z3950::RPN::Attribute",
408 (SV*) (hv2 = newHV()));
409 if (elem->attributeSet)
410 setMember(hv2, "attributeSet",
411 translateOID(elem->attributeSet));
412 setMember(hv2, "attributeType",
413 newSViv(*elem->attributeType));
414 assert(elem->which == Z_AttributeValue_numeric);
415 setMember(hv2, "attributeValue",
416 newSViv(*elem->value.numeric));
419 setMember(hv, "attributes", attrs);
421 setMember(hv, "term", newSVpv((char*) at->term->u.general->buf,
422 at->term->u.general->len));
425 case Z_RPNStructure_complex: {
427 Z_Complex *c = s->u.complex;
428 char *type = 0; /* vacuous assignment satisfies gcc -Wall */
429 switch (c->roperator->which) {
430 case Z_Operator_and: type = "Net::Z3950::RPN::And"; break;
431 case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break;
432 case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break;
433 case Z_Operator_prox: fatal("proximity not yet supported");
434 default: fatal("unknown RPN operator %d", (int) c->roperator->which);
436 sv = newObject(type, (SV*) (av = newAV()));
437 if ((tmp = rpn2perl(c->s1)) == 0)
440 if ((tmp = rpn2perl(c->s2)) == 0)
445 default: fatal("unknown RPN node type %d", (int) s->which);
452 static SV *zquery2perl(Z_Query *q)
457 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
459 sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV()));
460 if (q->u.type_1->attributeSetId)
461 setMember(hv, "attributeSet",
462 translateOID(q->u.type_1->attributeSetId));
463 setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure));
468 int bend_sort(void *handle, bend_sort_rr *rr)
479 char **input_setnames;
480 Zfront_handle *zhandle = (Zfront_handle *)handle;
488 input_setnames = rr->input_setnames;
489 for (i = 0; i < rr->num_input_setnames; i++)
491 av_push(aref, newSVpv(*input_setnames++, 0));
494 hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
495 hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
496 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
497 hv_store(href, "STATUS", 6, newSViv(0), 0);
501 XPUSHs(sv_2mortal(newRV( (SV*) href)));
505 perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
509 temp = hv_fetch(href, "ERR_CODE", 8, 1);
510 err_code = newSVsv(*temp);
512 temp = hv_fetch(href, "ERR_STR", 7, 1);
513 err_str = newSVsv(*temp);
515 temp = hv_fetch(href, "STATUS", 6, 1);
516 status = newSVsv(*temp);
527 rr->errcode = SvIV(err_code);
528 rr->sort_status = SvIV(status);
529 ptr = SvPV(err_str, len);
530 ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
531 strcpy(ODR_err_str, ptr);
532 rr->errstring = ODR_err_str;
542 int bend_search(void *handle, bend_search_rr *rr)
559 Zfront_handle *zhandle = (Zfront_handle *)handle;
566 basenames = rr->basenames;
567 for (i = 0; i < rr->num_bases; i++)
569 av_push(aref, newSVpv(*basenames++, 0));
572 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
573 hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0);
574 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
575 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
576 hv_store(href, "HITS", 4, newSViv(0), 0);
577 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
578 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
579 hv_store(href, "PID", 3, newSViv(getpid()), 0);
580 hv_store(href, "RPN", 3, zquery2perl(rr->query), 0);
581 query = zquery2pquery(rr->query);
584 hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0);
592 XPUSHs(sv_2mortal(newRV( (SV*) href)));
596 n = perl_call_sv(search_ref, G_SCALAR | G_DISCARD);
600 temp = hv_fetch(href, "HITS", 4, 1);
601 hits = newSVsv(*temp);
603 temp = hv_fetch(href, "ERR_CODE", 8, 1);
604 err_code = newSVsv(*temp);
606 temp = hv_fetch(href, "ERR_STR", 7, 1);
607 err_str = newSVsv(*temp);
609 temp = hv_fetch(href, "HANDLE", 6, 1);
610 point = newSVsv(*temp);
618 rr->hits = SvIV(hits);
619 rr->errcode = SvIV(err_code);
620 ptr = SvPV(err_str, len);
621 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
622 strcpy(ODR_errstr, ptr);
623 rr->errstring = ODR_errstr;
624 /* ODR_point = (SV *)odr_malloc(rr->stream, sizeof(*point));
625 memcpy(ODR_point, point, sizeof(*point));
626 zhandle->handle = ODR_point;*/
627 zhandle->handle = point;
632 sv_free( (SV*) aref);
633 sv_free( (SV*) href);
635 wrbuf_free(query, 1);
640 /* ### this is worryingly similar to oid2str() */
641 WRBUF oid2dotted(int *oid)
644 WRBUF buf = wrbuf_alloc();
647 for (; *oid != -1 ; oid++)
652 wrbuf_putc(buf, '.');
658 sprintf(ibuf, "%d", *oid);
659 wrbuf_puts(buf, ibuf);
665 int dotted2oid(char *dotted, int *buffer)
679 if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
685 sscanf(ibuf, "%d", oid++);
698 sscanf(ibuf, "%d", oid++);
705 int bend_fetch(void *handle, bend_fetch_rr *rr)
724 Zfront_handle *zhandle = (Zfront_handle *)handle;
726 Z_RecordComposition *composition;
727 Z_ElementSetNames *simple;
736 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
737 temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
738 oid_dotted = oid2dotted(rr->request_format_raw);
739 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
740 hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
741 hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
742 hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
743 hv_store(href, "LAST", 4, newSViv(0), 0);
744 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
745 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
746 hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
747 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
748 hv_store(href, "PID", 3, newSViv(getpid()), 0);
751 composition = rr->comp;
752 if (composition->which == Z_RecordComp_simple)
754 simple = composition->u.simple;
755 if (simple->which == Z_ElementSetNames_generic)
757 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
772 XPUSHs(sv_2mortal(newRV( (SV*) href)));
776 perl_call_sv(fetch_ref, G_SCALAR | G_DISCARD);
780 temp = hv_fetch(href, "BASENAME", 8, 1);
781 basename = newSVsv(*temp);
783 temp = hv_fetch(href, "RECORD", 6, 1);
784 record = newSVsv(*temp);
786 temp = hv_fetch(href, "LAST", 4, 1);
787 last = newSVsv(*temp);
789 temp = hv_fetch(href, "ERR_CODE", 8, 1);
790 err_code = newSVsv(*temp);
792 temp = hv_fetch(href, "ERR_STR", 7, 1),
793 err_string = newSVsv(*temp);
795 temp = hv_fetch(href, "SUR_FLAG", 8, 1);
796 sur_flag = newSVsv(*temp);
798 temp = hv_fetch(href, "REP_FORM", 8, 1);
799 rep_form = newSVsv(*temp);
801 temp = hv_fetch(href, "HANDLE", 6, 1);
802 point = newSVsv(*temp);
810 ptr = SvPV(basename, length);
811 ODR_basename = (char *)odr_malloc(rr->stream, length + 1);
812 strcpy(ODR_basename, ptr);
813 rr->basename = ODR_basename;
815 ptr = SvPV(rep_form, length);
816 ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
817 if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
819 printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
821 rr->output_format_raw = ODR_oid_buf;
823 ptr = SvPV(record, length);
824 oid = oid_getentbyoid(ODR_oid_buf);
825 if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
827 rr->record = (char *) read_grs1(ptr, rr->stream);
832 ODR_record = (char *)odr_malloc(rr->stream, length + 1);
833 strcpy(ODR_record, ptr);
834 rr->record = ODR_record;
837 zhandle->handle = point;
839 rr->last_in_set = SvIV(last);
843 rr->errcode = SvIV(err_code);
844 ptr = SvPV(err_string, length);
845 ODR_errstr = (char *)odr_malloc(rr->stream, length + 1);
846 strcpy(ODR_errstr, ptr);
847 rr->errstring = ODR_errstr;
849 rr->surrogate_flag = SvIV(sur_flag);
851 wrbuf_free(oid_dotted, 1);
865 int bend_present(void *handle, bend_present_rr *rr)
875 Z_RecordComposition *composition;
876 Z_ElementSetNames *simple;
879 Zfront_handle *zhandle = (Zfront_handle *)handle;
881 /* WRBUF oid_dotted; */
888 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
889 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
890 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
891 hv_store(href, "START", 5, newSViv(rr->start), 0);
892 hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
893 hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
894 /*oid_dotted = oid2dotted(rr->request_format_raw);
895 hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
896 hv_store(href, "HITS", 4, newSViv(0), 0);
897 hv_store(href, "PID", 3, newSViv(getpid()), 0);
900 composition = rr->comp;
901 if (composition->which == Z_RecordComp_simple)
903 simple = composition->u.simple;
904 if (simple->which == Z_ElementSetNames_generic)
906 hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
923 XPUSHs(sv_2mortal(newRV( (SV*) href)));
927 perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
931 temp = hv_fetch(href, "ERR_CODE", 8, 1);
932 err_code = newSVsv(*temp);
934 temp = hv_fetch(href, "ERR_STR", 7, 1);
935 err_string = newSVsv(*temp);
937 temp = hv_fetch(href, "HITS", 4, 1);
938 hits = newSVsv(*temp);
940 temp = hv_fetch(href, "HANDLE", 6, 1);
941 point = newSVsv(*temp);
948 rr->errcode = SvIV(err_code);
949 rr->hits = SvIV(hits);
951 ptr = SvPV(err_string, len);
952 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
953 strcpy(ODR_errstr, ptr);
954 rr->errstring = ODR_errstr;
955 /* wrbuf_free(oid_dotted, 1);*/
956 zhandle->handle = point;
961 sv_free( (SV*) href);
967 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
969 perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS);
974 int bend_delete(void *handle, bend_delete_rr *rr)
976 perl_call_sv(delete_ref, G_VOID | G_DISCARD | G_NOARGS);
981 int bend_scan(void *handle, bend_scan_rr *rr)
988 struct scan_entry *scan_list;
989 struct scan_entry *buffer;
990 int *step_size = rr->step_size;
994 SV *err_code = sv_newmortal();
995 SV *err_str = sv_newmortal();
996 SV *point = sv_newmortal();
997 SV *status = sv_newmortal();
998 SV *number = sv_newmortal();
1006 Zfront_handle *zhandle = (Zfront_handle *)handle;
1013 if (rr->term->term->which == Z_Term_general)
1015 term_len = rr->term->term->u.general->len;
1016 hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
1018 rr->errcode = 229; /* Unsupported term type */
1021 hv_store(href, "STEP", 4, newSViv(*step_size), 0);
1022 hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0);
1023 hv_store(href, "POS", 3, newSViv(rr->term_position), 0);
1024 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1025 hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
1026 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1027 hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0);
1028 hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0);
1030 basenames = rr->basenames;
1031 for (i = 0; i < rr->num_bases; i++)
1033 av_push(aref, newSVpv(*basenames++, 0));
1035 hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
1039 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1043 perl_call_sv(scan_ref, G_SCALAR | G_DISCARD);
1047 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1048 err_code = newSVsv(*temp);
1050 temp = hv_fetch(href, "ERR_STR", 7, 1);
1051 err_str = newSVsv(*temp);
1053 temp = hv_fetch(href, "HANDLE", 6, 1);
1054 point = newSVsv(*temp);
1056 temp = hv_fetch(href, "STATUS", 6, 1);
1057 status = newSVsv(*temp);
1059 temp = hv_fetch(href, "NUMBER", 6, 1);
1060 number = newSVsv(*temp);
1062 temp = hv_fetch(href, "ENTRIES", 7, 1);
1063 entries_ref = newSVsv(*temp);
1069 ptr = SvPV(err_str, len);
1070 ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
1071 strcpy(ODR_errstr, ptr);
1072 rr->errstring = ODR_errstr;
1073 rr->errcode = SvIV(err_code);
1074 rr->num_entries = SvIV(number);
1075 rr->status = SvIV(status);
1076 scan_list = (struct scan_entry *) odr_malloc (rr->stream, rr->num_entries * sizeof(*scan_list));
1078 entries = (AV *)SvRV(entries_ref);
1079 for (i = 0; i < rr->num_entries; i++)
1081 scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries)));
1082 temp = hv_fetch(scan_item, "TERM", 4, 1);
1083 ptr = SvPV(*temp, len);
1084 buffer->term = (char *) odr_malloc (rr->stream, len + 1);
1085 strcpy(buffer->term, ptr);
1086 temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1);
1087 buffer->occurrences = SvIV(*temp);
1089 hv_undef(scan_item);
1091 rr->entries = scan_list;
1092 zhandle->handle = point;
1099 sv_free((SV *)href);
1101 sv_free((SV *)aref);
1103 sv_free((SV *)list);
1105 /*sv_free((SV *)entries);*/
1106 sv_free(entries_ref);
1112 bend_initresult *bend_init(bend_initrequest *q)
1114 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
1122 Zfront_handle *zhandle = (Zfront_handle *) xmalloc (sizeof(*zhandle));
1130 char *passwd = NULL;
1136 /*q->bend_sort = bend_sort;*/
1139 q->bend_search = bend_search;
1143 q->bend_present = bend_present;
1145 /*q->bend_esrequest = bend_esrequest;*/
1146 /*q->bend_delete = bend_delete;*/
1149 q->bend_fetch = bend_fetch;
1153 q->bend_scan = bend_scan;
1156 hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0);
1157 hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
1158 hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
1159 hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
1160 hv_store(href, "ERR_STR", 7, newSViv(0), 0);
1161 hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
1162 hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
1163 hv_store(href, "PID", 3, newSViv(getpid()), 0);
1165 if (q->auth->which == Z_IdAuthentication_open) {
1166 char *openpass = xstrdup (q->auth->u.open);
1167 char *cp = strchr (openpass, '/');
1170 user = nmem_strdup (odr_getmem (q->stream), openpass);
1171 passwd = nmem_strdup (odr_getmem (q->stream), cp + 1);
1174 } else if (q->auth->which == Z_IdAuthentication_idPass) {
1175 user = q->auth->u.idPass->userId;
1176 passwd = q->auth->u.idPass->password;
1178 /* ### some code paths have user/password unassigned here */
1179 hv_store(href, "USER", 4, newSVpv(user, 0), 0);
1180 hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0);
1185 XPUSHs(sv_2mortal(newRV( (SV*) href)));
1189 if (init_ref != NULL)
1191 perl_call_sv(init_ref, G_SCALAR | G_DISCARD);
1196 temp = hv_fetch(href, "IMP_ID", 6, 1);
1197 id = newSVsv(*temp);
1199 temp = hv_fetch(href, "IMP_NAME", 8, 1);
1200 name = newSVsv(*temp);
1202 temp = hv_fetch(href, "IMP_VER", 7, 1);
1203 ver = newSVsv(*temp);
1205 temp = hv_fetch(href, "ERR_CODE", 8, 1);
1206 status = newSVsv(*temp);
1208 temp = hv_fetch(href, "ERR_STR", 7, 1);
1209 err_str = newSVsv(*temp);
1211 temp = hv_fetch(href, "HANDLE", 6, 1);
1212 handle= newSVsv(*temp);
1218 zhandle->handle = handle;
1219 r->errcode = SvIV(status);
1220 ptr = SvPV(err_str, len);
1221 r->errstring = (char *)odr_malloc(q->stream, len + 1);
1222 strcpy(r->errstring, ptr);
1224 r->handle = zhandle;
1225 ptr = SvPV(id, len);
1226 q->implementation_id = (char *)xmalloc(len + 1);
1227 strcpy(q->implementation_id, ptr);
1228 ptr = SvPV(name, len);
1229 q->implementation_name = (char *)xmalloc(len + 1);
1230 strcpy(q->implementation_name, ptr);
1231 /* q->implementation_name = SvPV(name, len);*/
1232 ptr = SvPV(ver, len);
1233 q->implementation_version = (char *)xmalloc(len + 1);
1234 strcpy(q->implementation_version, ptr);
1240 void bend_close(void *handle)
1243 Zfront_handle *zhandle = (Zfront_handle *)handle;
1250 if (close_ref == NULL)
1256 hv_store(href, "HANDLE", 6, zhandle->handle, 0);
1260 XPUSHs(sv_2mortal(newRV((SV *)href)));
1264 perl_call_sv(close_ref, G_SCALAR | G_DISCARD);
1278 MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer
1281 set_init_handler(arg)
1284 init_ref = newSVsv(arg);
1288 set_close_handler(arg)
1291 close_ref = newSVsv(arg);
1295 set_sort_handler(arg)
1298 sort_ref = newSVsv(arg);
1301 set_search_handler(arg)
1304 search_ref = newSVsv(arg);
1308 set_fetch_handler(arg)
1311 fetch_ref = newSVsv(arg);
1315 set_present_handler(arg)
1318 present_ref = newSVsv(arg);
1322 set_esrequest_handler(arg)
1325 esrequest_ref = newSVsv(arg);
1329 set_delete_handler(arg)
1332 delete_ref = newSVsv(arg);
1336 set_scan_handler(arg)
1339 scan_ref = newSVsv(arg);
1351 argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
1353 for (i = 0; i < items; i++)
1355 ptr = SvPV(ST(i), len);
1356 *argv_buf = (char *)xmalloc(len + 1);
1357 strcpy(*argv_buf++, ptr);
1361 RETVAL = statserv_main(items, argv, bend_init, bend_close);
1369 RETVAL = BEND_SCAN_SUCCESS;
1376 RETVAL = BEND_SCAN_PARTIAL;