Updated for YAZ 1.7. HTML output tidy up. Added LOC target.
[egate.git] / zlayer-yaz / zaccess.c
index 8d07fa6..e50435e 100644 (file)
@@ -47,6 +47,9 @@
  * Z39.50 API for the Email gateway - YAZ version
  *
  * $Log: zaccess.c,v $
+ * Revision 1.6  2001/02/26 14:32:36  adam
+ * Updated for YAZ 1.7. HTML output tidy up. Added LOC target.
+ *
  * Revision 1.5  1996/01/03 08:58:12  adam
  * Updated to use new version of Yaz (some names changed to avoid C++ conflict).
  *
 #include <ctype.h>
 
 #include <gw-log.h>
-#include <proto.h>
-#include <comstack.h>
-#include <tcpip.h>
+#include <yaz/proto.h>
+#include <yaz/comstack.h>
+#include <yaz/tcpip.h>
 #ifdef USE_XTIMOSI
 #include <xmosi.h>
 #endif
-#include <oid.h>
+#include <yaz/oid.h>
 
 #include <ccl.h>
 #include <zaccess.h>
@@ -148,7 +151,7 @@ static Z_APDU *get_apdu(struct zass *z, int *complete)
     }
     odr_reset(z->decode);
     odr_setbuf(z->decode, z->inbuf, res, 0);
-    if (!z_APDU(z->decode, &ap, 0))
+    if (!z_APDU(z->decode, &ap, 0, 0))
     {
        gw_log(GW_LOG_WARN, ZASS_TYPE, "decode: %s",
            odr_errlist[odr_geterror(z->decode)]);
@@ -166,7 +169,7 @@ static int send_apdu(struct zass *z, Z_APDU *a)
     char *buf;
     int len;
 
-    if (!z_APDU(z->encode, &a, 0))
+    if (!z_APDU(z->encode, &a, 0, 0))
     {
        gw_log(GW_LOG_FATAL, ZASS_TYPE, "encoding initreq");
        return -1;
@@ -326,65 +329,72 @@ static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
     struct ccl_rpn_attr *i;
     int len;
     static int op[] = { Z_Operator_and, Z_Operator_or, Z_Operator_and_not };
+    Z_AttributeElement **elements;
+    int num_attributes = 0;
 
     switch (q->kind)
     {
-       case CCL_RPN_TERM:
-           r->which = Z_RPNStructure_simple;
-           r->u.simple = odr_malloc(o, sizeof(Z_Operand));
-           r->u.simple->which = Z_Operand_APT;
-           r->u.simple->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
-           t->term = odr_malloc(o, sizeof(Z_Term));
-           t->term->which = Z_Term_general;
-           t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
-           t->term->u.general->len = t->term->u.general->size =
-               strlen(q->u.t.term);
-           t->term->u.general->buf = odr_malloc(o, t->term->u.general->size);
-           memcpy(t->term->u.general->buf, q->u.t.term,
-               t->term->u.general->len);
-           t->num_attributes = 0;
-           t->attributeList = odr_malloc(o, sizeof(Z_AttributeElement*) * 100);
-           for (i = q->u.t.attr_list; i && t->num_attributes < 100;
-               i = i->next)
-           {
-               Z_AttributeElement *a;
-               
-               t->attributeList[t->num_attributes++] = a =
-                   odr_malloc(o, sizeof(*a));
-               a->attributeType = odr_malloc(o, sizeof(int));
-               *a->attributeType = i->type;
-#ifdef Z_95
-               a->attributeSet = 0;
-               a->which = Z_AttributeValue_numeric;
-               a->value.numeric = odr_malloc(o, sizeof(*a->value.numeric));
-                *a->value.numeric = i->value;
+    case CCL_RPN_TERM:
+       r->which = Z_RPNStructure_simple;
+       r->u.simple = odr_malloc(o, sizeof(Z_Operand));
+       r->u.simple->which = Z_Operand_APT;
+       r->u.simple->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
+       t->term = odr_malloc(o, sizeof(Z_Term));
+       t->term->which = Z_Term_general;
+       t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
+       t->term->u.general->len = t->term->u.general->size =
+           strlen(q->u.t.term);
+       t->term->u.general->buf = odr_malloc(o, t->term->u.general->size);
+       memcpy(t->term->u.general->buf, q->u.t.term,
+              t->term->u.general->len);
+       elements = odr_malloc(o, sizeof(Z_AttributeElement*) * 100);
+#ifdef ASN_COMPILED
+       t->attributes = (Z_AttributeList *)
+           odr_malloc (o, sizeof(*t->attributes));
+       t->attributes->attributes = elements;
 #else
-               a->attributeValue = odr_malloc(o, sizeof(*a->attributeValue));
-               *a->attributeValue = i->value;
+       t->attributeList = elements;
 #endif
-           }
-           return r;
-       case CCL_RPN_SET:
-           r->which = Z_RPNStructure_simple;
-           r->u.simple = odr_malloc(o, sizeof(Z_Operand));
-           r->u.simple->which = Z_Operand_resultSetId;
-           r->u.simple->u.resultSetId = odr_malloc(o, len =
-               strlen(q->u.setname) + 1);
-           memcpy(r->u.simple->u.resultSetId, q->u.setname, len);
-           return r;
-       case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
-           r->which = Z_RPNStructure_complex;
-           r->u.complex = odr_malloc(o, sizeof(Z_Complex));
-           if (!(r->u.complex->s1 = rpn2rpn(o, q->u.p[0])) ||
-               !(r->u.complex->s2 = rpn2rpn(o, q->u.p[1])))
-                   return 0;
-           r->u.complex->roperator = odr_malloc(o, sizeof(Z_Operator));
-           r->u.complex->roperator->which = op[q->kind];
-           r->u.complex->roperator->u.and = "";
-           return r;
-       default:
-           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Bad operator in RPN");
+       for (i = q->u.t.attr_list; i && num_attributes < 100; i = i->next)
+       {
+           Z_AttributeElement *a;
+           
+           elements[num_attributes++] = a =
+               odr_malloc(o, sizeof(*a));
+           a->attributeType = odr_malloc(o, sizeof(int));
+           *a->attributeType = i->type;
+           a->attributeSet = 0;
+           a->which = Z_AttributeValue_numeric;
+           a->value.numeric = odr_malloc(o, sizeof(*a->value.numeric));
+           *a->value.numeric = i->value;
+       }
+#ifdef ASN_COMPILED
+       t->attributes->num_attributes = num_attributes;
+#else
+       t->num_attributes = num_attributes;
+#endif
+       return r;
+    case CCL_RPN_SET:
+       r->which = Z_RPNStructure_simple;
+       r->u.simple = odr_malloc(o, sizeof(Z_Operand));
+       r->u.simple->which = Z_Operand_resultSetId;
+       r->u.simple->u.resultSetId = odr_malloc(o, len =
+                                               strlen(q->u.setname) + 1);
+       memcpy(r->u.simple->u.resultSetId, q->u.setname, len);
+       return r;
+    case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
+       r->which = Z_RPNStructure_complex;
+       r->u.complex = odr_malloc(o, sizeof(Z_Complex));
+       if (!(r->u.complex->s1 = rpn2rpn(o, q->u.p[0])) ||
+           !(r->u.complex->s2 = rpn2rpn(o, q->u.p[1])))
            return 0;
+       r->u.complex->roperator = odr_malloc(o, sizeof(Z_Operator));
+       r->u.complex->roperator->which = op[q->kind];
+       r->u.complex->roperator->u.op_and = "";
+       return r;
+    default:
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Bad operator in RPN");
+       return 0;
     }
 }
 
@@ -415,9 +425,14 @@ const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
        else
        {
            oident *id;
+#ifdef ASN_COMPILED
+           Z_DiagRec dr_info;
+           Z_DiagRec *dr = &dr_info;
+           dr_info.which = Z_DiagRec_defaultFormat;
+           dr_info.u.defaultFormat = res->records->u.nonSurrogateDiagnostic;
+#else
            Z_DiagRec *dr = res->records->u.nonSurrogateDiagnostic;
-
-#ifdef Z_95
+#endif
             if (dr->which != Z_DiagRec_defaultFormat || 
                 !(id = oid_getentbyoid 
                        (dr->u.defaultFormat->diagnosticSetId)) ||
@@ -428,27 +443,28 @@ const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
             else
             {
                r.errcode = *dr->u.defaultFormat->condition;
-               if (dr->u.defaultFormat->addinfo)
+#ifdef ASN_COMPILED
+               switch (dr->u.defaultFormat->which)
                {
-                   strncpy(r.errstring, dr->u.defaultFormat->addinfo, 512);
+               case Z_DefaultDiagFormat_v2Addinfo:
+                   strncpy(r.errstring, dr->u.defaultFormat->u.v2Addinfo,
+                           512);
                    r.errstring[511] = '\0';
+                   break;
+               case Z_DefaultDiagFormat_v3Addinfo:
+                   strncpy(r.errstring, dr->u.defaultFormat->u.v3Addinfo,
+                           512);
+                   r.errstring[511] = '\0';
+                   break;
                }
-           }
 #else
-           if (!(id = oid_getentbyoid(dr->diagnosticSetId)) ||
-               id->class != CLASS_DIAGSET || id->value != VAL_BIB1)
-                   gw_log(GW_LOG_WARN, ZASS_TYPE,
-                       "Missing or unknown diagset - ignoring error!");
-           else
-           {
-               r.errcode = *dr->condition;
-               if (dr->addinfo)
+               if (dr->u.defaultFormat->addinfo)
                {
-                   strncpy(r.errstring, dr->addinfo, 512);
+                   strncpy(r.errstring, dr->u.defaultFormat->addinfo, 512);
                    r.errstring[511] = '\0';
                }
-           }
 #endif
+           }
        }
     }
     return &r;
@@ -526,33 +542,37 @@ const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
  * Triple indirection - that's kinda heavy. We'll fix it later.
  * There are worse things around, though. Like ZDist.
  */
-void get_diagrec(zass_record ***p, Z_DiagRec *r)
+void get_diagrecs(zass_record ***p, Z_DiagRec **r, int num)
 {
     **p = malloc(sizeof(***p));
     (**p)->next = 0;
-#ifdef Z_95
+
     (**p)->errstring[0] = '\0';
-    if (r->which == Z_DiagRec_defaultFormat)
+    if ((*r)->which == Z_DiagRec_defaultFormat)
     {
-        (**p)->errcode = *r->u.defaultFormat->condition;
-        if (r->u.defaultFormat->addinfo)
+        (**p)->errcode = *(*r)->u.defaultFormat->condition;
+#ifdef ASN_COMPILED
+       switch ((*r)->u.defaultFormat->which)
+       {
+       case Z_DefaultDiagFormat_v2Addinfo:
+           strncpy((**p)->errstring, (*r)->u.defaultFormat->u.v2Addinfo, 200);
+           (**p)->errstring[200] = 0;
+           break;
+       case Z_DefaultDiagFormat_v3Addinfo:
+           strncpy((**p)->errstring, (*r)->u.defaultFormat->u.v3Addinfo, 200);
+           (**p)->errstring[200] = 0;
+           break;
+       }
+#else
+        if ((*r)->u.defaultFormat->addinfo)
         {
-           strncpy((**p)->errstring, r->u.defaultFormat->addinfo, 200);
+           strncpy((**p)->errstring, (*r)->u.defaultFormat->addinfo, 200);
            (**p)->errstring[200] = 0;
         }
+#endif
     }
     else
         (**p)->errcode = -1;
-#else
-    (**p)->errcode = *r->condition;
-    if (r->addinfo)
-    {
-       strncpy((**p)->errstring, r->addinfo, 200);
-       (**p)->errstring[200] = 0;
-    }
-    else
-       (**p)->errstring[0] = '\0';
-#endif
     (**p)->which = ZASS_REC_DIAG;
     *p = &(**p)->next;
 }
@@ -567,7 +587,7 @@ void get_responserecords(zass_record ***p, Z_NamePlusRecordList *recs)
 
        record = recs->records[i];
        if (record->which == Z_NamePlusRecord_surrogateDiagnostic)
-           get_diagrec(p, record->u.surrogateDiagnostic);
+           get_diagrecs(p, &record->u.surrogateDiagnostic, 1);
        else
        {
            Z_DatabaseRecord *r = record->u.databaseRecord;
@@ -585,10 +605,11 @@ void get_responserecords(zass_record ***p, Z_NamePlusRecordList *recs)
            else
                switch (recform->value)
                {
-                   case VAL_USMARC: (**p)->which = ZASS_REC_USMARC; break;
-                   default:
-                       gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown recsyn");
-                       (**p)->which = ZASS_REC_UNKNOWN;
+               case VAL_USMARC: (**p)->which = ZASS_REC_USMARC; break;
+               case VAL_DANMARC: (**p)->which = ZASS_REC_DANMARC; break;
+               default:
+                   gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown recsyn");
+                   (**p)->which = ZASS_REC_UNKNOWN;
                }
            if (r->which != ODR_EXTERNAL_octet)
            {
@@ -622,7 +643,7 @@ static int send_present(ZASS a, char *name, int start, int num,
 {
     Z_APDU *apdu;
     Z_PresentRequest *req;
-#if 0
+#if 1
     oident recsyn;
 #endif
 
@@ -632,9 +653,9 @@ static int send_present(ZASS a, char *name, int start, int num,
     req->resultSetId = name;
     req->resultSetStartPoint = &start;
     req->numberOfRecordsRequested = &num;
-#if 0
+#if 1
     recsyn.proto = PROTO_Z3950;
-    recsyn.class = CLASS_RECSYN;
+    recsyn.oclass = CLASS_RECSYN;
     recsyn.value = form;
     req->preferredRecordSyntax = oid_getoidbyent(&recsyn);
 #else
@@ -665,6 +686,9 @@ const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
     {
        Z_APDU *apdu;
        Z_PresentResponse *res;
+#ifdef ASN_COMPILED
+       Z_DiagRec dr, *dr_p = &dr;
+#endif
 
        gw_log(ZASS_DEBUG, ZASS_TYPE,
            "Fetching %d records from # %d", num - r.num, start);
@@ -694,16 +718,22 @@ const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
        start = r.nextpos;
        switch (res->records->which)
        {
-           case Z_Records_DBOSD:
-               get_responserecords(&rec,
-                   res->records->u.databaseOrSurDiagnostics);
-               break;
-           case Z_Records_NSD:
-               get_diagrec(&rec, res->records->u.nonSurrogateDiagnostic);
-               break;
-           default:
-               gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
-               return 0;
+       case Z_Records_DBOSD:
+           get_responserecords(&rec,
+                               res->records->u.databaseOrSurDiagnostics);
+           break;
+       case Z_Records_NSD:
+#ifdef ASN_COMPILED
+           dr.which = Z_DiagRec_defaultFormat;
+           dr.u.defaultFormat = res->records->u.nonSurrogateDiagnostic;
+           get_diagrecs(&rec, &dr_p, 1);
+#else
+           get_diagrecs(&rec, &res->records->u.nonSurrogateDiagnostic, 1);
+#endif
+           break;
+       default:
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
+           return 0;
        }
     }
     while (num - r.num && start);