Extended maximum numbers of GRS-1 elements. Should be done dynamically.
[simpleserver-moved-to-github.git] / SimpleServer.xs
index 88056f8..74d2281 100644 (file)
  * OF THIS SOFTWARE.
  */
 
+/*$Log: SimpleServer.xs,v $
+/*Revision 1.8  2001-05-21 11:07:02  sondberg
+/*Extended maximum numbers of GRS-1 elements. Should be done dynamically.
+/*
+/*Revision 1.7  2001/03/13 14:17:15  sondberg
+/*Added support for GRS-1.
+/**/
+
 
 #include "EXTERN.h"
 #include "perl.h"
 #include <yaz/log.h>
 #include <yaz/wrbuf.h>
 #include <stdio.h>
+#include <unistd.h>
 #include <stdlib.h>
 #include <ctype.h>
+#define GRS_MAX_FIELDS 500 
 #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;
@@ -64,6 +76,100 @@ SV *delete_ref = NULL;
 SV *scan_ref = NULL;
 int MAX_OID = 15;
 
+
+Z_GenericRecord *read_grs1(char *str, ODR o)
+{
+       int type, ivalue;
+       char line[512], *buf, *ptr, *original;
+       char value[512];
+       Z_GenericRecord *r = 0;
+
+       original = str;
+       for (;;)
+       {
+               Z_TaggedElement *t;
+               Z_ElementData *c;
+       
+               ptr = strchr(str, '\n');
+               if (!ptr) {
+                       return r;
+               }
+               strncpy(line, str, ptr - str);
+               line[ptr - str] = 0;
+               buf = line;
+               str = ptr + 1;
+               while (*buf && isspace(*buf))
+                       buf++;
+               if (*buf == '}') {
+                       memmove(original, str, strlen(str));
+                       return r;
+               }
+               if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
+               {
+                       yaz_log(LOG_WARN, "Bad data in '%s'", buf);
+                       return 0;
+               }
+               if (!type && *value == '0')
+                       return r;
+               if (!(buf = strchr(buf, ')')))
+                       return 0;
+               buf++;
+               while (*buf && isspace(*buf))
+                       buf++;
+               if (!*buf)
+                       return 0;
+               if (!r)
+               {
+                       r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
+                       r->elements = (Z_TaggedElement **)
+                       odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
+                       r->num_elements = 0;
+               }
+               if (r->num_elements > GRS_MAX_FIELDS)
+               {
+                       yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
+                       exit(0);
+               }
+               r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
+               t->tagType = (int *)odr_malloc(o, sizeof(int));
+               *t->tagType = type;
+               t->tagValue = (Z_StringOrNumeric *)
+                       odr_malloc(o, sizeof(Z_StringOrNumeric));
+               if ((ivalue = atoi(value)))
+               {
+                       t->tagValue->which = Z_StringOrNumeric_numeric;
+                       t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
+                       *t->tagValue->u.numeric = ivalue;
+               }
+               else
+               {
+                       t->tagValue->which = Z_StringOrNumeric_string;
+                       t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
+                       strcpy(t->tagValue->u.string, value);
+               }
+               t->tagOccurrence = 0;
+               t->metaData = 0;
+               t->appliedVariant = 0;
+               t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
+               if (*buf == '{')
+               {
+                       c->which = Z_ElementData_subtree;
+                       c->u.subtree = read_grs1(str, o);
+               }
+               else
+               {
+                       c->which = Z_ElementData_string;
+/*                     buf[strlen(buf)-1] = '\0';*/
+                       buf[strlen(buf)] = '\0';
+                       c->u.string = odr_strdup(o, buf);
+               }
+               r->num_elements++;
+       }
+}
+
+
+
+
 static void oid2str(Odr_oid *o, WRBUF buf)
 {
     for (; *o >= 0; o++) {
@@ -158,8 +264,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;
 }
 
 
@@ -200,6 +373,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)
        {
@@ -340,13 +514,13 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        char *ODR_basename;
        char *ODR_errstr;
        int *ODR_oid_buf;
+       oident *oid;
        WRBUF oid_dotted;
        Zfront_handle *zhandle = (Zfront_handle *)handle;
 
        Z_RecordComposition *composition;
        Z_ElementSetNames *simple;
        STRLEN length;
-       int oid;
 
        dSP;
        ENTER;
@@ -366,6 +540,7 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        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;
@@ -441,11 +616,19 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        rr->output_format_raw = ODR_oid_buf;    
        
        ptr = SvPV(record, length);
-       ODR_record = (char *)odr_malloc(rr->stream, length + 1);
-       strcpy(ODR_record, ptr);
-       rr->record = ODR_record;
-       rr->len = length;
-
+       oid = oid_getentbyoid(ODR_oid_buf);
+       if (oid->value == VAL_GRS1)             /* Treat GRS-1 records separately */
+       {
+               rr->record = (char *) read_grs1(ptr, rr->stream);
+               rr->len = -1;
+       }
+       else
+       {
+               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;
        rr->last_in_set = SvIV(last);
@@ -477,34 +660,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);
                        } 
@@ -527,7 +719,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;
 
@@ -537,20 +729,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;
@@ -604,7 +806,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)
@@ -616,7 +821,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);