Fixed bugs.
[egate.git] / zlayer / zaccess-yaz.c
index 1dd02b2..c13bdf9 100644 (file)
@@ -4,7 +4,16 @@
  * Z39.50 API for the Email gateway - YAZ version
  *
  * $Log: zaccess-yaz.c,v $
- * Revision 1.4  1995/04/19 16:02:28  adam
+ * Revision 1.7  1995/04/21 16:29:32  quinn
+ * Fixed bugs.
+ *
+ * Revision 1.6  1995/04/21  12:58:36  adam
+ * Bug fix.
+ *
+ * Revision 1.5  1995/04/20  15:25:32  quinn
+ * Asynch. API
+ *
+ * Revision 1.4  1995/04/19  16:02:28  adam
  * Record type hack.
  *
  * Revision 1.3  1995/04/19  12:55:15  quinn
  * Revision 1.1  1995/04/17  11:26:53  quinn
  * Added YAZ version of zaccess
  *
- *
  */
 
 /*
- * Interface to the Z39.50 toolkit.
+ * Interface to the YAZ Z39.50 toolkit.
  */
 
 #include <stdlib.h>
@@ -45,14 +53,14 @@ struct zass    /* Z-assoc */
     ODR encode;
     ODR decode;
     int fd;                         /* low-level socket (for select only) */
+    int nonblocking;
     int maxrecordsize;
     int preferredmessagesize;
-    char *outbuf;                      /* intermediary buffer */
     char *inbuf;
     int inbuflen;
 };
 
-static Z_APDU *get_apdu(struct zass *z)
+static Z_APDU *get_apdu(struct zass *z, int *complete)
 {
     int res;
     Z_APDU *ap;
@@ -60,16 +68,28 @@ static Z_APDU *get_apdu(struct zass *z)
     if ((res = cs_get(z->ass, &z->inbuf, &z->inbuflen)) <= 0)
     {
        gw_log(GW_LOG_WARN, ZASS_TYPE, "cs_get failed");
+       if (complete)
+           *complete = 1;
        return 0;
     }
+    else if (res == 1)
+    {
+       if (complete)
+           *complete = 0;
+       return 0;
+    }
     odr_reset(z->decode);
     odr_setbuf(z->decode, z->inbuf, res);
     if (!z_APDU(z->decode, &ap, 0))
     {
        gw_log(GW_LOG_WARN, ZASS_TYPE, "decode: %s",
            odr_errlist[odr_geterror(z->decode)]);
+       if (complete)
+           *complete = 0;
        return 0;
     }
+    if (complete)
+       *complete = 1;
     return ap;
 }
 
@@ -89,7 +109,7 @@ static int send_apdu(struct zass *z, Z_APDU *a)
        gw_log(GW_LOG_FATAL, ZASS_TYPE, "cs_put");
        return -1;
     }
-    odr_reset(z->encode);
+    odr_reset(z->encode);  /* release odr_allocated structures */
     return 0;
 }
 
@@ -115,14 +135,16 @@ static int send_initreq(struct zass *p, char *auth)
     ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
     init.preferredMessageSize = &p->preferredmessagesize;
     init.maximumRecordSize = &p->maxrecordsize;
-    if (!auth)
-       init.idAuthentication = 0;
-    else
+
+    if (auth)
     {
        init.idAuthentication = &idauth;
        idauth.which = Z_IdAuthentication_open;
        idauth.u.open = auth;
     }
+    else
+       init.idAuthentication = 0;
+    init.idAuthentication = 0;
     init.implementationId = ZASS_ID;
     sprintf(name, "%s (YAZ protocol layer)", ZASS_NAME);
     init.implementationName = name;
@@ -133,12 +155,17 @@ static int send_initreq(struct zass *p, char *auth)
     return 0;
 }
 
-static int receive_initres(struct zass *p)
+int zass_fileno(ZASS a)
+{
+    return a->fd;
+}
+
+int zass_openresult(ZASS p, int *complete)
 {
     Z_APDU *ap;
     Z_InitResponse *res;
 
-    if (!(ap = get_apdu(p)))
+    if (!(ap = get_apdu(p, complete)))
        return -1;
     if (ap->which != Z_APDU_initResponse)
     {
@@ -166,7 +193,7 @@ static int receive_initres(struct zass *p)
     return 0;
 }
 
-ZASS zass_open(char *host, int port, char *auth)
+ZASS zass_open(char *host, int port, int *complete, char *auth)
 {
     struct zass *p;
     char addstr[512];
@@ -177,6 +204,13 @@ ZASS zass_open(char *host, int port, char *auth)
        gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
        return 0;
     }
+    if (complete)
+    {
+       *complete = 1;
+       p->nonblocking = 1;
+    }
+    else
+       p->nonblocking = 0;
     if (!(p->encode = odr_createmem(ODR_ENCODE)) ||
        !(p->decode = odr_createmem(ODR_DECODE)))
     {
@@ -185,17 +219,13 @@ ZASS zass_open(char *host, int port, char *auth)
     }
     p->maxrecordsize = ZASS_MAXRECORDSIZE;
     p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
-    if (!(p->outbuf = malloc(p->maxrecordsize + 1024)))
-    {
-       gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
-       return 0;
-    }
-    odr_setbuf(p->encode, p->outbuf, p->maxrecordsize + 1024);
     if (!(p->ass = cs_create(tcpip_type, 1, CS_Z3950)))
     {
        gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "cs_create");
        return 0;
     }
+    p->inbuf = 0;
+    p->inbuflen = 0;
     sprintf(addstr, "%s:%d", host, port);
     if (!(address = tcpip_strtoaddr(addstr)))
     {
@@ -209,18 +239,25 @@ ZASS zass_open(char *host, int port, char *auth)
        gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to connect to %s", addstr);
        return 0;
     }
-    gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok");
-    p->inbuf = 0;
-    p->inbuflen = 0;
-    if (send_initreq(p, auth) < 0 || receive_initres(p) < 0)
+    gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok... initializing");
+    if (send_initreq(p, auth) < 0)
     {
-       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to initialize");
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send init");
        return 0;
     }
-    gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent init request");
-    return p;
+    if (p->nonblocking)
+    {
+       *complete = 0;
+       return p;
+    }
+    if (zass_openresult(p, complete) < 0)
+       return 0;
+    return p; /* all done */
 }
 
+/*
+ * Convert the egate (ccl2rpn) version of RPN to YAZ RPN.
+ */
 static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
 {
     Z_RPNStructure *r = odr_malloc(o, sizeof(*r));
@@ -264,7 +301,7 @@ static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
            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));
+               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:
@@ -283,13 +320,13 @@ static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
     }
 }
 
-static const struct zass_searchent *search_result(ZASS a)
+const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
 {
     static struct zass_searchent r;
     Z_APDU *apdu;
     Z_SearchResponse *res;
 
-    if (!(apdu = get_apdu(a)))
+    if (!(apdu = get_apdu(a, complete)))
        return 0;
     if (apdu->which != Z_APDU_searchResponse)
     {
@@ -331,7 +368,7 @@ static const struct zass_searchent *search_result(ZASS a)
 }
 
 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
-    char *resname, char *databases)
+    char *resname, char *databases, int *complete)
 {
     Z_Query q;
     Z_RPNQuery rpnq;
@@ -384,12 +421,20 @@ const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
     bib1.class = CLASS_ATTSET;
     bib1.value = VAL_BIB1;
     rpnq.attributeSetId = oid_getoidbyent(&bib1);
+
+    if (complete)
+       *complete = 1;
     if (!(rpnq.RPNStructure = rpn2rpn(a->encode, query)))
        return 0;
     if (send_apdu(a, &apdu) < 0)
        return 0;
-
-    return search_result(a);
+    if (complete)
+    {
+       *complete = 0;
+       return 0;
+    }
+    else
+       return zass_searchresult(a, complete);
 }
 
 /*
@@ -477,7 +522,9 @@ static int send_present(ZASS a, char *name, int start, int num,
 {
     Z_APDU apdu;
     Z_PresentRequest req;
+#if 0
     oident recsyn;
+#endif
 
     apdu.which = Z_APDU_presentRequest;
     apdu.u.presentRequest = &req;
@@ -499,13 +546,16 @@ static int send_present(ZASS a, char *name, int start, int num,
 
 /*
  * Note that 1== first record.
+ * TODO: make this baby operate in nonblocking mode, too.
  */
 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
-    int num)
+    int num, int *complete)
 {
     static struct zass_presentent r;
     zass_record **rec = &r.records;
 
+    if (complete)
+       *complete = 1;
     r.num = 0;
     if (r.records)
     {
@@ -521,8 +571,12 @@ const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
            "Fetching %d records from # %d", num - r.num, start);
        if (send_present(a, resname, start, num - r.num, VAL_USMARC) < 0)
            return 0;
-       if (!(apdu = get_apdu(a)))
+       if (!(apdu = get_apdu(a, complete)))
+       {
+           if (complete)
+               *complete = 1;
            return 0;
+       }
        if (apdu->which != Z_APDU_presentResponse)
        {
            gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse, got #%d",
@@ -556,3 +610,9 @@ const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
     while (num - r.num && start);
     return &r;
 }
+
+const struct zass_presentent *zass_presentresult(ZASS a, int *complete)
+{
+    *complete = 1;
+    return 0;
+}