Added a PID member to the argument hashed.
[simpleserver-moved-to-github.git] / SimpleServer.c
index 80dd5c7..34b7e1b 100644 (file)
@@ -7,6 +7,33 @@
  */
 
 #line 1 "SimpleServer.xs"
+/*
+ * Copyright (c) 2000, Index Data.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation, in whole or in part, for any purpose, is hereby granted,
+ * provided that:
+ *
+ * 1. This copyright and permission notice appear in all copies of the
+ * software and its documentation. Notices of copyright or attribution
+ * which appear at the beginning of any file must remain unchanged.
+ *
+ * 2. The name of Index Data or the individual authors may not be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
+ * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+
 #include "EXTERN.h"
 #include "perl.h"
 #include "XSUB.h"
 #include <yaz/log.h>
 #include <yaz/wrbuf.h>
 #include <stdio.h>
+#include <unistd.h>
 #include <stdlib.h>
 #include <ctype.h>
 #ifdef ASN_COMPILED
 #include <yaz/ill.h>
 #endif
-
+#ifndef sv_undef               /* To fix the problem with Perl 5.6.0 */
+#define sv_undef PL_sv_undef
+#endif
 
 typedef struct {
        SV *handle;
@@ -140,8 +170,75 @@ WRBUF zquery2pquery(Z_Query *q)
 
 int bend_sort(void *handle, bend_sort_rr *rr)
 {
-       perl_call_sv(sort_ref, G_VOID | G_DISCARD | G_NOARGS);
-       return;
+       HV *href;
+       AV *aref;
+       SV **temp;
+       SV *err_code;
+       SV *err_str;
+       SV *status;
+       STRLEN len;
+       char *ptr;
+       char *ODR_err_str;
+       char **input_setnames;
+       Zfront_handle *zhandle = (Zfront_handle *)handle;
+       int i;
+       
+       dSP;
+       ENTER;
+       SAVETMPS;
+       
+       aref = newAV();
+       input_setnames = rr->input_setnames;
+       for (i = 0; i < rr->num_input_setnames; i++)
+       {
+               av_push(aref, newSVpv(*input_setnames++, 0));
+       }
+       href = newHV();
+       hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0);
+       hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0);
+       hv_store(href, "HANDLE", 6, zhandle->handle, 0);
+       hv_store(href, "STATUS", 6, newSViv(0), 0);
+
+       PUSHMARK(sp);
+
+       XPUSHs(sv_2mortal(newRV( (SV*) href)));
+
+       PUTBACK;
+
+       perl_call_sv(sort_ref, G_SCALAR | G_DISCARD);
+
+       SPAGAIN;
+
+       temp = hv_fetch(href, "ERR_CODE", 8, 1);
+       err_code = newSVsv(*temp);
+
+       temp = hv_fetch(href, "ERR_STR", 7, 1);
+       err_str = newSVsv(*temp);
+
+       temp = hv_fetch(href, "STATUS", 6, 1);
+       status = newSVsv(*temp);
+
+
+       
+
+       PUTBACK;
+       FREETMPS;
+       LEAVE;
+
+       hv_undef(href),
+       av_undef(aref);
+       rr->errcode = SvIV(err_code);
+       rr->sort_status = SvIV(status);
+       ptr = SvPV(err_str, len);
+       ODR_err_str = (char *)odr_malloc(rr->stream, len + 1);
+       strcpy(ODR_err_str, ptr);
+       rr->errstring = ODR_err_str;
+
+       sv_free(err_code);
+       sv_free(err_str);
+       sv_free(status);
+       
+       return 0;
 }
 
 
@@ -182,6 +279,7 @@ int bend_search(void *handle, bend_search_rr *rr)
        hv_store(href, "HITS", 4, newSViv(0), 0);
        hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0);
        hv_store(href, "HANDLE", 6, zhandle->handle, 0);
+       hv_store(href, "PID", 3, newSViv(getpid()), 0);
        query = zquery2pquery(rr->query);
        if (query)
        {
@@ -310,7 +408,6 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        HV *href;
        SV **temp;
        SV *basename;
-       SV *len;
        SV *record;
        SV *last;
        SV *err_code;
@@ -343,20 +440,20 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
        hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);
        hv_store(href, "BASENAME", 8, newSVpv("", 0), 0);
-       hv_store(href, "LEN", 3, newSViv(0), 0);
        hv_store(href, "RECORD", 6, newSVpv("", 0), 0);
        hv_store(href, "LAST", 4, newSViv(0), 0);
        hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
        hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
        hv_store(href, "SUR_FLAG", 8, newSViv(0), 0);
        hv_store(href, "HANDLE", 6, zhandle->handle, 0);
+       hv_store(href, "PID", 3, newSViv(getpid()), 0);
        if (rr->comp)
        {
                composition = rr->comp;
-               if (composition->which == 1)
+               if (composition->which == Z_RecordComp_simple)
                {
                        simple = composition->u.simple;
-                       if (simple->which == 1)
+                       if (simple->which == Z_ElementSetNames_generic)
                        {
                                hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
                        } 
@@ -384,9 +481,6 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        temp = hv_fetch(href, "BASENAME", 8, 1);
        basename = newSVsv(*temp);
 
-       temp = hv_fetch(href, "LEN", 3, 1);
-       len = newSVsv(*temp);
-
        temp = hv_fetch(href, "RECORD", 6, 1);
        record = newSVsv(*temp);
 
@@ -427,12 +521,11 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        }
        rr->output_format_raw = ODR_oid_buf;    
        
-       rr->len = SvIV(len);
-
        ptr = SvPV(record, length);
        ODR_record = (char *)odr_malloc(rr->stream, length + 1);
        strcpy(ODR_record, ptr);
        rr->record = ODR_record;
+       rr->len = length;
 
        zhandle->handle = point;
        handle = zhandle;
@@ -448,11 +541,9 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        }
        rr->surrogate_flag = SvIV(sur_flag);
 
-       /*sv_free(point);*/
        wrbuf_free(oid_dotted, 1);
        sv_free((SV*) href);
        sv_free(basename);
-       sv_free(len);
        sv_free(record);
        sv_free(last);
        sv_free(err_string);
@@ -467,34 +558,43 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
 int bend_present(void *handle, bend_present_rr *rr)
 {
 
-       int n;
        HV *href;
        SV **temp;
        SV *err_code;
        SV *err_string;
+       SV *hits;
+       SV *point;
        STRLEN len;
        Z_RecordComposition *composition;
        Z_ElementSetNames *simple;
        char *ODR_errstr;
        char *ptr;
+       Zfront_handle *zhandle = (Zfront_handle *)handle;
+
+/*     WRBUF oid_dotted; */
 
        dSP;
        ENTER;
        SAVETMPS;
 
        href = newHV();
+        hv_store(href, "HANDLE", 6, zhandle->handle, 0);
        hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
        hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0);
        hv_store(href, "START", 5, newSViv(rr->start), 0);
        hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0);
        hv_store(href, "NUMBER", 6, newSViv(rr->number), 0);
+       /*oid_dotted = oid2dotted(rr->request_format_raw);
+        hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/
+       hv_store(href, "HITS", 4, newSViv(0), 0);
+       hv_store(href, "PID", 3, newSViv(getpid()), 0);
        if (rr->comp)
        {
                composition = rr->comp;
-               if (composition->which == 1)
+               if (composition->which == Z_RecordComp_simple)
                {
                        simple = composition->u.simple;
-                       if (simple->which == 1)
+                       if (simple->which == Z_ElementSetNames_generic)
                        {
                                hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0);
                        } 
@@ -517,7 +617,7 @@ int bend_present(void *handle, bend_present_rr *rr)
        
        PUTBACK;
        
-       n = perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
+       perl_call_sv(present_ref, G_SCALAR | G_DISCARD);
        
        SPAGAIN;
 
@@ -527,20 +627,30 @@ int bend_present(void *handle, bend_present_rr *rr)
        temp = hv_fetch(href, "ERR_STR", 7, 1);
        err_string = newSVsv(*temp);
 
+       temp = hv_fetch(href, "HITS", 4, 1);
+       hits = newSVsv(*temp);
+
+       temp = hv_fetch(href, "HANDLE", 6, 1);
+       point = newSVsv(*temp);
+
        PUTBACK;
        FREETMPS;
        LEAVE;
        
        hv_undef(href);
        rr->errcode = SvIV(err_code);
+       rr->hits = SvIV(hits);
 
        ptr = SvPV(err_string, len);
        ODR_errstr = (char *)odr_malloc(rr->stream, len + 1);
        strcpy(ODR_errstr, ptr);
        rr->errstring = ODR_errstr;
-
+/*     wrbuf_free(oid_dotted, 1);*/
+       zhandle->handle = point;
+       handle = zhandle;
        sv_free(err_code);
        sv_free(err_string);
+       sv_free(hits);
        sv_free( (SV*) href);
 
        return 0;
@@ -594,7 +704,10 @@ bend_initresult *bend_init(bend_initrequest *q)
        {
                q->bend_search = bend_search;
        }
-       /*q->bend_present = present;*/
+       if (present_ref)
+       {
+               q->bend_present = bend_present;
+       }
        /*q->bend_esrequest = bend_esrequest;*/
        /*q->bend_delete = bend_delete;*/
        if (fetch_ref)
@@ -606,7 +719,9 @@ bend_initresult *bend_init(bend_initrequest *q)
        hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0);
        hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0);
        hv_store(href, "ERR_CODE", 8, newSViv(0), 0);
+       hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0);
        hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0);
+       hv_store(href, "PID", 3, newSViv(getpid()), 0);
 
        PUSHMARK(sp);   
 
@@ -690,17 +805,17 @@ void bend_close(void *handle)
 }
 
 
-#line 694 "SimpleServer.c"
+#line 809 "SimpleServer.c"
 XS(XS_Net__Z3950__SimpleServer_set_init_handler)
 {
     dXSARGS;
     if (items != 1)
-       croak("Usage: Net::Z3950::SimpleServer::set_init_handler(arg)");
+       Perl_croak(aTHX_ "Usage: Net::Z3950::SimpleServer::set_init_handler(arg)");
     {
        SV *    arg = ST(0);
-#line 690 "SimpleServer.xs"
+#line 805 "SimpleServer.xs"
                init_ref = newSVsv(arg);
-#line 704 "SimpleServer.c"
+#line 819 "SimpleServer.c"
     }
     XSRETURN_EMPTY;
 }
@@ -709,12 +824,12 @@ XS(XS_Net__Z3950__SimpleServer_set_close_handler)
 {
     dXSARGS;
     if (items != 1)
-       croak("Usage: Net::Z3950::SimpleServer::set_close_handler(arg)");
+       Perl_croak(aTHX_ "Usage: Net::Z3950::SimpleServer::set_close_handler(arg)");
     {
        SV *    arg = ST(0);
-#line 697 "SimpleServer.xs"
+#line 812 "SimpleServer.xs"
                close_ref = newSVsv(arg);
-#line 718 "SimpleServer.c"
+#line 833 "SimpleServer.c"
     }
     XSRETURN_EMPTY;
 }
@@ -723,12 +838,12 @@ XS(XS_Net__Z3950__SimpleServer_set_sort_handler)
 {
     dXSARGS;
     if (items != 1)
-       croak("Usage: Net::Z3950::SimpleServer::set_sort_handler(arg)");
+       Perl_croak(aTHX_ "Usage: Net::Z3950::SimpleServer::set_sort_handler(arg)");
     {
        SV *    arg = ST(0);
-#line 704 "SimpleServer.xs"
+#line 819 "SimpleServer.xs"
                sort_ref = newSVsv(arg);
-#line 732 "SimpleServer.c"
+#line 847 "SimpleServer.c"
     }
     XSRETURN_EMPTY;
 }
@@ -737,12 +852,12 @@ XS(XS_Net__Z3950__SimpleServer_set_search_handler)
 {
     dXSARGS;
     if (items != 1)
-       croak("Usage: Net::Z3950::SimpleServer::set_search_handler(arg)");
+       Perl_croak(aTHX_ "Usage: Net::Z3950::SimpleServer::set_search_handler(arg)");
     {
        SV *    arg = ST(0);
-#line 710 "SimpleServer.xs"
+#line 825 "SimpleServer.xs"
                search_ref = newSVsv(arg);
-#line 746 "SimpleServer.c"
+#line 861 "SimpleServer.c"
     }
     XSRETURN_EMPTY;
 }
@@ -751,12 +866,12 @@ XS(XS_Net__Z3950__SimpleServer_set_fetch_handler)
 {
     dXSARGS;
     if (items != 1)
-       croak("Usage: Net::Z3950::SimpleServer::set_fetch_handler(arg)");
+       Perl_croak(aTHX_ "Usage: Net::Z3950::SimpleServer::set_fetch_handler(arg)");
     {
        SV *    arg = ST(0);
-#line 717 "SimpleServer.xs"
+#line 832 "SimpleServer.xs"
                fetch_ref = newSVsv(arg);
-#line 760 "SimpleServer.c"
+#line 875 "SimpleServer.c"
     }
     XSRETURN_EMPTY;
 }
@@ -765,12 +880,12 @@ XS(XS_Net__Z3950__SimpleServer_set_present_handler)
 {
     dXSARGS;
     if (items != 1)
-       croak("Usage: Net::Z3950::SimpleServer::set_present_handler(arg)");
+       Perl_croak(aTHX_ "Usage: Net::Z3950::SimpleServer::set_present_handler(arg)");
     {
        SV *    arg = ST(0);
-#line 724 "SimpleServer.xs"
+#line 839 "SimpleServer.xs"
                present_ref = newSVsv(arg);
-#line 774 "SimpleServer.c"
+#line 889 "SimpleServer.c"
     }
     XSRETURN_EMPTY;
 }
@@ -779,12 +894,12 @@ XS(XS_Net__Z3950__SimpleServer_set_esrequest_handler)
 {
     dXSARGS;
     if (items != 1)
-       croak("Usage: Net::Z3950::SimpleServer::set_esrequest_handler(arg)");
+       Perl_croak(aTHX_ "Usage: Net::Z3950::SimpleServer::set_esrequest_handler(arg)");
     {
        SV *    arg = ST(0);
-#line 731 "SimpleServer.xs"
+#line 846 "SimpleServer.xs"
                esrequest_ref = newSVsv(arg);
-#line 788 "SimpleServer.c"
+#line 903 "SimpleServer.c"
     }
     XSRETURN_EMPTY;
 }
@@ -793,12 +908,12 @@ XS(XS_Net__Z3950__SimpleServer_set_delete_handler)
 {
     dXSARGS;
     if (items != 1)
-       croak("Usage: Net::Z3950::SimpleServer::set_delete_handler(arg)");
+       Perl_croak(aTHX_ "Usage: Net::Z3950::SimpleServer::set_delete_handler(arg)");
     {
        SV *    arg = ST(0);
-#line 738 "SimpleServer.xs"
+#line 853 "SimpleServer.xs"
                delete_ref = newSVsv(arg);
-#line 802 "SimpleServer.c"
+#line 917 "SimpleServer.c"
     }
     XSRETURN_EMPTY;
 }
@@ -807,12 +922,12 @@ XS(XS_Net__Z3950__SimpleServer_set_scan_handler)
 {
     dXSARGS;
     if (items != 1)
-       croak("Usage: Net::Z3950::SimpleServer::set_scan_handler(arg)");
+       Perl_croak(aTHX_ "Usage: Net::Z3950::SimpleServer::set_scan_handler(arg)");
     {
        SV *    arg = ST(0);
-#line 745 "SimpleServer.xs"
+#line 860 "SimpleServer.xs"
                scan_ref = newSVsv(arg);
-#line 816 "SimpleServer.c"
+#line 931 "SimpleServer.c"
     }
     XSRETURN_EMPTY;
 }
@@ -821,15 +936,16 @@ XS(XS_Net__Z3950__SimpleServer_start_server)
 {
     dXSARGS;
     {
-#line 751 "SimpleServer.xs"
+#line 866 "SimpleServer.xs"
                char **argv;
                char **argv_buf;
                char *ptr;
                int i;
                STRLEN len;
-#line 831 "SimpleServer.c"
+#line 946 "SimpleServer.c"
        int     RETVAL;
-#line 757 "SimpleServer.xs"
+       dXSTARG;
+#line 872 "SimpleServer.xs"
                argv_buf = (char **)xmalloc((items + 1) * sizeof(char *));
                argv = argv_buf;
                for (i = 0; i < items; i++)
@@ -841,9 +957,8 @@ XS(XS_Net__Z3950__SimpleServer_start_server)
                *argv_buf = NULL;
 
                RETVAL = statserv_main(items, argv, bend_init, bend_close);
-#line 845 "SimpleServer.c"
-       ST(0) = sv_newmortal();
-       sv_setiv(ST(0), (IV)RETVAL);
+#line 961 "SimpleServer.c"
+       XSprePUSH; PUSHi((IV)RETVAL);
     }
     XSRETURN(1);
 }