New button "Search".
[ir-tcl-moved-to-github.git] / ir-tcl.c
index d9c4908..4136cfa 100644 (file)
--- a/ir-tcl.c
+++ b/ir-tcl.c
@@ -4,7 +4,29 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: ir-tcl.c,v $
- * Revision 1.15  1995-03-20 15:24:07  adam
+ * Revision 1.21  1995-03-31 08:56:37  adam
+ * New button "Search".
+ *
+ * Revision 1.20  1995/03/29  16:07:09  adam
+ * Bug fix: Didn't use setName in present request.
+ *
+ * Revision 1.19  1995/03/28  12:45:23  adam
+ * New ir method failback: called on disconnect/protocol error.
+ * New ir set/get method: protocol: SR / Z3950.
+ * Simple popup and disconnect when failback is invoked.
+ *
+ * Revision 1.18  1995/03/21  15:50:12  adam
+ * Minor changes.
+ *
+ * Revision 1.17  1995/03/21  13:41:03  adam
+ * Comstack cs_create not used too often. Non-blocking connect.
+ *
+ * Revision 1.16  1995/03/21  08:26:06  adam
+ * New method, setName, to specify the result set name (other than Default).
+ * New method, responseStatus, which returns diagnostic info, if any, after
+ * present response / search response.
+ *
+ * Revision 1.15  1995/03/20  15:24:07  adam
  * Diagnostic records saved on searchResponse.
  *
  * Revision 1.14  1995/03/20  08:53:22  adam
 #include <assert.h>
 
 #include <yaz-ccl.h>
-#include <iso2709p.h>
+#include <iso2709.h>
 #include <comstack.h>
 #include <tcpip.h>
+
+#if MOSI
 #include <xmosi.h>
+#endif
 
 #include <odr.h>
 #include <proto.h>
 #define CS_BLOCK 0
 
 typedef struct {
-    COMSTACK cs_link;
+    char       *cs_type;
+    char       *protocol_type;
+    int         connectFlag;
+    COMSTACK    cs_link;
 
-    int preferredMessageSize;
-    int maximumRecordSize;
+
+    int         preferredMessageSize;
+    int         maximumRecordSize;
     Odr_bitmask options;
     Odr_bitmask protocolVersion;
-    char *idAuthentication;
-    char *implementationName;
-    char *implementationId;
+    char       *idAuthentication;
+    char       *implementationName;
+    char       *implementationId;
 
-    char *hostname;
+    char       *hostname;
    
-    char *buf_out;
-    int  len_out;
+    char       *buf_out;
+    int         len_out;
 
-    char *buf_in;
-    int  len_in;
+    char       *buf_in;
+    int         len_in;
 
-    char *sbuf;
-    int  slen;
+    char       *sbuf;
+    int         slen;
 
-    ODR odr_in;
-    ODR odr_out;
-    ODR odr_pr;
+    ODR         odr_in;
+    ODR         odr_out;
+    ODR         odr_pr;
 
     Tcl_Interp *interp;
-    char *callback;
+    char       *callback;
+    char       *failback;
 
-    int smallSetUpperBound;
-    int largeSetLowerBound;
-    int mediumSetPresentNumber;
-    int replaceIndicator;
-    char **databaseNames;
-    int num_databaseNames;
-    char *query_method;
+    int         smallSetUpperBound;
+    int         largeSetLowerBound;
+    int         mediumSetPresentNumber;
+    int         replaceIndicator;
+    char      **databaseNames;
+    int         num_databaseNames;
+    char       *query_method;
 
-    CCL_bibset bibset;
+    CCL_bibset  bibset;
 
     struct IRSetObj_ *child;
 } IRObj;
@@ -124,23 +154,24 @@ typedef struct IRRecordList_ {
 } IRRecordList;
 
 typedef struct IRSetObj_ {
-    IRObj *parent;
-    int searchStatus;
-    int resultCount;
-    int start;
-    int number;
-    int numberOfRecordsReturned;
-    Z_Records *z_records;
-    int which;
-    int condition;
-    char *addinfo;
+    IRObj      *parent;
+    int         searchStatus;
+    int         resultCount;
+    int         start;
+    int         number;
+    int         numberOfRecordsReturned;
+    char       *setName;
+    int         recordFlag;
+    int         which;
+    int         condition;
+    char       *addinfo;
     IRRecordList *record_list;
 } IRSetObj;
 
 typedef struct {
     int type;
     char *name;
-    int (*method) (void * obj, Tcl_Interp *interp, int argc, char **argv);
+    int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
 } IRMethod;
 
 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
@@ -317,6 +348,11 @@ static int do_init_request (void *obj, Tcl_Interp *interp,
     Z_InitRequest req;
     int r;
 
+    if (!p->cs_link)
+    {
+        interp->result = "not connected";
+        return TCL_ERROR;
+    }
     req.referenceId = 0;
     req.options = &p->options;
     req.protocolVersion = &p->protocolVersion;
@@ -344,6 +380,7 @@ static int do_init_request (void *obj, Tcl_Interp *interp,
     if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
     {     
         interp->result = "cs_put failed in init";
+        do_disconnect (p, NULL, 0, NULL);
         return TCL_ERROR;
     }
     else if (r == 1)
@@ -477,6 +514,8 @@ static int do_connect (void *obj, Tcl_Interp *interp,
 {
     void *addr;
     IRObj *p = obj;
+    int r;
+    int protocol_type = CS_Z3950;
 
     if (argc == 3)
     {
@@ -485,8 +524,18 @@ static int do_connect (void *obj, Tcl_Interp *interp,
             interp->result = "already connected";
             return TCL_ERROR;
         }
-        if (cs_type(p->cs_link) == tcpip_type)
+        if (!strcmp (p->protocol_type, "Z3950"))
+            protocol_type = CS_Z3950;
+        else if (!strcmp (p->protocol_type, "SR"))
+            protocol_type = CS_SR;
+        else
+        {
+            interp->result = "bad protocol type";
+            return TCL_ERROR;
+        }
+        if (!strcmp (p->cs_type, "tcpip"))
         {
+            p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
             addr = tcpip_strtoaddr (argv[2]);
             if (!addr)
             {
@@ -495,8 +544,10 @@ static int do_connect (void *obj, Tcl_Interp *interp,
             }
             printf ("tcp/ip connect %s\n", argv[2]);
         }
-        else if (cs_type (p->cs_link) == mosi_type)
+#if MOSI
+        else if (!strcmp (p->cs_type, "mosi"))
         {
+            p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
             addr = mosi_strtoaddr (argv[2]);
             if (!addr)
             {
@@ -505,17 +556,35 @@ static int do_connect (void *obj, Tcl_Interp *interp,
             }
             printf ("mosi connect %s\n", argv[2]);
         }
-        if (cs_connect (p->cs_link, addr) < 0)
+#endif
+        else 
         {
-            interp->result = "cs_connect fail";
-            do_disconnect (p, interp, argc, argv);
+            interp->result = "unknown comstack type";
             return TCL_ERROR;
         }
         if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
             return TCL_ERROR;
+        if ((r=cs_connect (p->cs_link, addr)) < 0)
+        {
+            interp->result = "cs_connect fail";
+            do_disconnect (p, NULL, 0, NULL);
+            return TCL_ERROR;
+        }
         ir_select_add (cs_fileno (p->cs_link), p);
+        if (r == 1)
+        {
+            ir_select_add_write (cs_fileno (p->cs_link), p);
+            p->connectFlag = 1;
+        }
+        else
+        {
+            p->connectFlag = 0;
+            if (p->callback)
+                Tcl_Eval (p->interp, p->callback);
+        }
     }
-    Tcl_AppendResult (interp, p->hostname, NULL);
+    if (p->hostname)
+        Tcl_AppendElement (interp, p->hostname);
     return TCL_OK;
 }
 
@@ -531,51 +600,50 @@ static int do_disconnect (void *obj, Tcl_Interp *interp,
     {
         free (p->hostname);
         p->hostname = NULL;
+        ir_select_remove_write (cs_fileno (p->cs_link), p);
         ir_select_remove (cs_fileno (p->cs_link), p);
-    }
-    if (cs_type (p->cs_link) == tcpip_type)
-    {
-        cs_close (p->cs_link);
-        p->cs_link = cs_create (tcpip_type, CS_BLOCK);
-    }
-    else if (cs_type (p->cs_link) == mosi_type)
-    {
+
+        assert (p->cs_link);
         cs_close (p->cs_link);
-        p->cs_link = cs_create (mosi_type, CS_BLOCK);
+        p->cs_link = NULL;
     }
-    else
+    assert (!p->cs_link);
+    return TCL_OK;
+}
+
+/*
+ * do_comstack: Set/get comstack method on IR object
+ */
+static int do_comstack (void *o, Tcl_Interp *interp,
+                       int argc, char **argv)
+{
+    IRObj *obj = o;
+
+    if (argc == 3)
     {
-        interp->result = "unknown comstack type";
-        return TCL_ERROR;
+        free (obj->cs_type);
+        if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
+            return TCL_ERROR;
     }
+    Tcl_AppendElement (interp, obj->cs_type);
     return TCL_OK;
 }
 
 /*
- * do_comstack: Set/get comstack method on IR object
+ * do_protocol: Set/get protocol method on IR object
  */
-static int do_comstack (void *obj, Tcl_Interp *interp,
+static int do_protocol (void *o, Tcl_Interp *interp,
                        int argc, char **argv)
 {
-    char *cs_type = NULL;
+    IRObj *obj = o;
+
     if (argc == 3)
     {
-        cs_close (((IRObj*) obj)->cs_link);
-        if (!strcmp (argv[2], "tcpip"))
-            ((IRObj *)obj)->cs_link = cs_create (tcpip_type, CS_BLOCK);
-        else if (!strcmp (argv[2], "mosi"))
-            ((IRObj *)obj)->cs_link = cs_create (mosi_type, CS_BLOCK);
-        else
-        {
-            interp->result = "wrong comstack type";
+        free (obj->protocol_type);
+        if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
             return TCL_ERROR;
-        }
     }
-    if (cs_type(((IRObj *)obj)->cs_link) == tcpip_type)
-        cs_type = "tcpip";
-    else if (cs_type(((IRObj *)obj)->cs_link) == mosi_type)
-        cs_type = "comstack";
-    Tcl_AppendResult (interp, cs_type, NULL);
+    Tcl_AppendElement (interp, obj->protocol_type);
     return TCL_OK;
 }
 
@@ -598,6 +666,24 @@ static int do_callback (void *obj, Tcl_Interp *interp,
 }
 
 /*
+ * do_failback: add error handle callback
+ */
+static int do_failback (void *obj, Tcl_Interp *interp,
+                          int argc, char **argv)
+{
+    IRObj *p = obj;
+
+    if (argc == 3)
+    {
+        free (p->failback);
+        if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
+            return TCL_ERROR;
+        p->interp = interp;
+    }
+    return TCL_OK;
+}
+
+/*
  * do_databaseNames: specify database names
  */
 static int do_databaseNames (void *obj, Tcl_Interp *interp,
@@ -648,6 +734,26 @@ static int do_query (void *obj, Tcl_Interp *interp,
     return TCL_OK;
 }
 
+/*
+ * do_replaceIndicator: Set/get replace Set indicator
+ */
+static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
+                                int argc, char **argv)
+{
+    IRObj *p = obj;
+    char buf[20];
+
+    if (argc == 3)
+    {
+        if (Tcl_GetInt (interp, argv[2], 
+                        &p->replaceIndicator)==TCL_ERROR)
+            return TCL_ERROR;
+    }
+    sprintf (buf, "%d", p->replaceIndicator);
+    Tcl_AppendResult (interp, buf, NULL);
+    return TCL_OK;
+}
+
 /* 
  * ir_obj_method: IR Object methods
  */
@@ -656,6 +762,7 @@ static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
 {
     static IRMethod tab[] = {
     { 1, "comstack",                do_comstack },
+    { 1, "protocol",                do_protocol },
     { 1, "connect",                 do_connect },
     { 0, "protocolVersion",         do_protocolVersion },
     { 0, "options",                 do_options },
@@ -667,7 +774,9 @@ static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
     { 0, "init",                    do_init_request },
     { 0, "disconnect",              do_disconnect },
     { 0, "callback",                do_callback },
+    { 0, "failback",                do_failback },
     { 1, "databaseNames",           do_databaseNames},
+    { 1, "replaceIndicator",        do_replaceIndicator},
     { 1, "query",                   do_query },
     { 0, NULL, NULL}
     };
@@ -700,10 +809,15 @@ static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
     }
     if (!(obj = ir_malloc (interp, sizeof(*obj))))
         return TCL_ERROR;
-    obj->cs_link = cs_create (tcpip_type, CS_BLOCK);
+    if (ir_strdup (interp, &obj->cs_type, "tcpip") == TCL_ERROR)
+        return TCL_ERROR;
+    if (ir_strdup (interp, &obj->protocol_type, "Z3950") == TCL_ERROR)
+        return TCL_ERROR;
+    obj->cs_link = NULL;
 
     obj->maximumRecordSize = 32768;
     obj->preferredMessageSize = 4096;
+    obj->connectFlag = 0;
 
     obj->idAuthentication = NULL;
 
@@ -752,6 +866,7 @@ static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
     obj->buf_in = NULL;
 
     obj->callback = NULL;
+    obj->failback = NULL;
     Tcl_CreateCommand (interp, argv[1], ir_obj_method,
                        (ClientData) obj, ir_obj_delete);
     return TCL_OK;
@@ -784,6 +899,11 @@ static int do_search (void *o, Tcl_Interp *interp,
         interp->result = "no databaseNames";
         return TCL_ERROR;
     }
+    if (!p->cs_link)
+    {
+        interp->result = "not connected";
+        return TCL_ERROR;
+    }
     apdu.which = Z_APDU_searchRequest;
     apdu.u.searchRequest = &req;
     apdup = &apdu;
@@ -793,7 +913,7 @@ static int do_search (void *o, Tcl_Interp *interp,
     req.largeSetLowerBound = &p->largeSetLowerBound;
     req.mediumSetPresentNumber = &p->mediumSetPresentNumber;
     req.replaceIndicator = &p->replaceIndicator;
-    req.resultSetName = "Default";
+    req.resultSetName = obj->setName ? obj->setName : "Default";
     req.num_databaseNames = p->num_databaseNames;
     req.databaseNames = p->databaseNames;
     printf ("Search:");
@@ -829,7 +949,7 @@ static int do_search (void *o, Tcl_Interp *interp,
     {
         query.which = Z_Query_type_2;
         query.u.type_2 = &ccl_query;
-        ccl_query.buf = argv[2];
+        ccl_query.buf = (unsigned char *) argv[2];
         ccl_query.len = strlen (argv[2]);
         printf ("- CCL\n");
     }
@@ -887,87 +1007,92 @@ static int do_searchStatus (void *o, Tcl_Interp *interp,
 }
 
 /*
- * do_numberOfRecordsReturned: Get number of records returned
+ * do_setName: Set result Set name
  */
-static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
+static int do_setName (void *o, Tcl_Interp *interp,
                       int argc, char **argv)
 {
     IRSetObj *obj = o;
 
-    sprintf (interp->result, "%d", obj->numberOfRecordsReturned);
+    if (argc == 3)
+    {
+        free (obj->setName);
+        if (ir_strdup (interp, &obj->setName, argv[2])
+            == TCL_ERROR)
+            return TCL_ERROR;
+    }
+    Tcl_AppendElement (interp, obj->setName);
     return TCL_OK;
 }
 
-static int marc_cmp (const char *field, const char *pattern)
+/*
+ * do_numberOfRecordsReturned: Get number of records returned
+ */
+static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
+                      int argc, char **argv)
 {
-    if (*pattern == '*')
-        return 0;
-    for (; *field && *pattern; field++, pattern++)
-    {
-        if (*pattern == '?')
-            continue;
-        if (*pattern != *field)
-            break;
-    }
-    return *field - *pattern;
+    IRSetObj *obj = o;
+
+    sprintf (interp->result, "%d", obj->numberOfRecordsReturned);
+    return TCL_OK;
 }
 
 static int get_marc_fields(Tcl_Interp *interp, Iso2709Rec rec,
                            int argc, char **argv)
 {
-    struct iso2709_dir *dir;
-    struct iso2709_field *field;
+    Iso2709Anchor a;
+    char *data;
 
-    for (dir = rec->directory; dir; dir = dir->next)
+    a = iso2709_a_mk (rec);
+    while (iso2709_a_search (a, argv[4], argv[5], argv[6]))
     {
-        if (argc > 4 && marc_cmp (dir->tag, argv[4]))
-            continue;
-        if (argc > 5 && marc_cmp (dir->indicator, argv[5]))
-            continue;
-        for (field = dir->fields; field; field = field->next)
-        {
-            if (argc > 6 && marc_cmp (field->identifier, argv[6]))
-                continue;
-            Tcl_AppendElement (interp, field->data);
-        }
+        if (!(iso2709_a_info_field (a, NULL, NULL, NULL, &data)))
+            break;
+        Tcl_AppendElement (interp, data);
+        iso2709_a_next (a);
     }
+
+    iso2709_a_rm (a);
     return TCL_OK;
 }
 
-static int get_marc_lines (Tcl_Interp *interp, Iso2709Rec rec,
-                           int argc, char **argv)
+static int get_marc_lines(Tcl_Interp *interp, Iso2709Rec rec,
+                         int argc, char **argv)
 {
-    struct iso2709_dir *dir;
-    struct iso2709_field *field;
+    Iso2709Anchor a;
+    char *tag;
+    char *indicator;
+    char *identifier;
+    char *data;
+    char *ptag = "";
     
-    for (dir = rec->directory; dir; dir = dir->next)
+    a = iso2709_a_mk (rec);
+    while (iso2709_a_search (a, argv[4], argv[5], argv[6]))
     {
-        if (argc > 4 && marc_cmp (dir->tag, argv[4]))
-            continue;
-        if (!dir->indicator)
-            Tcl_AppendResult (interp, "{", dir->tag, " {} {", NULL);
-        else
-        {
-            if (argc > 5 && marc_cmp (dir->indicator, argv[5]))
-                continue;
-            Tcl_AppendResult (interp, "{", dir->tag, " {", dir->indicator, 
-                              "} {", NULL);
-        }
-        for (field = dir->fields; field; field = field->next)
+        if (!(iso2709_a_info_field (a, &tag, &indicator, &identifier, &data)))
+            break;
+        if (strcmp (tag, ptag))
         {
-            if (!field->identifier)
-                Tcl_AppendResult (interp, "{{}", NULL);
+            if (*ptag)
+                Tcl_AppendResult (interp, "}} ", NULL);
+            if (!indicator)
+                Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
             else
-            {
-                if (argc > 6 && marc_cmp (field->identifier, argv[6]))
-                    continue;
-                Tcl_AppendResult (interp, "{", field->identifier, NULL);
-            }
-            Tcl_AppendElement (interp, field->data);
-            Tcl_AppendResult (interp, "} ", NULL);
+                Tcl_AppendResult (interp, "{", tag, " {", indicator, 
+                                  "} {", NULL);
+            ptag = tag;
         }
-        Tcl_AppendResult (interp, "}} ", NULL);
+        if (!identifier)
+            Tcl_AppendResult (interp, "{{}", NULL);
+        else
+            Tcl_AppendResult (interp, "{", identifier, NULL);
+        Tcl_AppendElement (interp, data);
+        Tcl_AppendResult (interp, "} ", NULL);
+        iso2709_a_next (a);
     }
+    if (*ptag)
+        Tcl_AppendResult (interp, "}} ", NULL);
+    iso2709_a_rm (a);
     return TCL_OK;
 }
 
@@ -1076,15 +1201,17 @@ static int do_recordMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
 }
 
 /*
- * do_presentStatus: Return present status (after present response)
+ * do_responseStatus: Return response status (present or search)
  */
-static int do_presentStatus (void *o, Tcl_Interp *interp, 
+static int do_responseStatus (void *o, Tcl_Interp *interp, 
                              int argc, char **argv)
 {
     IRSetObj *obj = o;
     const char *cp;
     char buf[28];
 
+    if (!obj->recordFlag)
+        return TCL_OK;
     switch (obj->which)
     {
     case Z_Records_DBOSD:
@@ -1137,6 +1264,11 @@ static int do_present (void *o, Tcl_Interp *interp,
     }
     else 
         number = 10;
+    if (!p->cs_link)
+    {
+        interp->result = "not connected";
+        return TCL_ERROR;
+    }
     obj->start = start;
     obj->number = number;
 
@@ -1145,7 +1277,9 @@ static int do_present (void *o, Tcl_Interp *interp,
     apdu.u.presentRequest = &req;
     req.referenceId = 0;
     /* sprintf(setstring, "%d", setnumber); */
-    req.resultSetId = "Default";
+
+    req.resultSetId = obj->setName ? obj->setName : "Default";
+    
     req.resultSetStartPoint = &start;
     req.numberOfRecordsRequested = &number;
     req.elementSetNames = 0;
@@ -1227,13 +1361,14 @@ static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
     static IRMethod tab[] = {
     { 0, "search",                  do_search },
     { 0, "searchStatus",            do_searchStatus },
+    { 0, "setName",                 do_setName },
     { 0, "resultCount",             do_resultCount },
     { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
     { 0, "present",                 do_present },
     { 0, "recordType",              do_recordType },
     { 0, "recordMarc",              do_recordMarc },
     { 0, "recordDiag",              do_recordDiag },
-    { 0, "presentStatus",           do_presentStatus },
+    { 0, "responseStatus",          do_responseStatus },
     { 0, "loadFile",                do_loadFile },
     { 0, NULL, NULL}
     };
@@ -1272,7 +1407,7 @@ static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
         return TCL_ERROR;
     if (!(obj = ir_malloc (interp, sizeof(*obj))))
         return TCL_ERROR;
-    obj->z_records = NULL;
+    obj->setName = NULL;
     obj->record_list = NULL;
     obj->addinfo = NULL;
     obj->parent = (IRObj *) parent_info.clientData;
@@ -1314,6 +1449,8 @@ static void ir_handleRecords (void *o, Z_Records *zrs)
     IRObj *p = o;
     IRSetObj *setobj = p->child;
 
+    setobj->which = zrs->which;
+    setobj->recordFlag = 1;
     if (zrs->which == Z_Records_NSD)
     {
         const char *addinfo;
@@ -1325,7 +1462,7 @@ static void ir_handleRecords (void *o, Z_Records *zrs)
         addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
         if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
             strcpy (setobj->addinfo, addinfo);
-        printf ("Diagnostic response. %s (%d), info %s\n",
+        printf ("Diagnostic response. %s (%d): %s\n",
                 diagbib1_str (setobj->condition),
                 setobj->condition,
                 setobj->addinfo ? setobj->addinfo : "");
@@ -1379,16 +1516,19 @@ static void ir_handleRecords (void *o, Z_Records *zrs)
 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
 {    
     IRObj *p = o;
-    IRSetObj *obj = p->child;
+    IRSetObj *setobj = p->child;
+    Z_Records *zrs = searchrs->records;
 
-    if (obj)
+    if (setobj)
     {
-        obj->searchStatus = searchrs->searchStatus ? 1 : 0;
-        obj->resultCount = *searchrs->resultCount;
+        setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
+        setobj->resultCount = *searchrs->resultCount;
         printf ("Search response %d, %d hits\n", 
-                 obj->searchStatus, obj->resultCount);
-        if (searchrs->records)
-            ir_handleRecords (o, searchrs->records);
+                 setobj->searchStatus, setobj->resultCount);
+        if (zrs)
+            ir_handleRecords (o, zrs);
+        else
+            setobj->recordFlag = 0;
     }
     else
         printf ("Search response, no object!\n");
@@ -1400,16 +1540,13 @@ static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
     IRObj *p = o;
     IRSetObj *setobj = p->child;
     Z_Records *zrs = presrs->records;
-    setobj->z_records = presrs->records;
     
     printf ("Received presentResponse\n");
     if (zrs)
-    {
-        setobj->which = zrs->which;
         ir_handleRecords (o, zrs);
-    }
     else
     {
+        setobj->recordFlag = 0;
         printf ("No records!\n");
     }
 }
@@ -1422,13 +1559,35 @@ void ir_select_read (ClientData clientData)
     IRObj *p = clientData;
     Z_APDU *apdu;
     int r;
-    
+
+    if (p->connectFlag)
+    {
+        r = cs_rcvconnect (p->cs_link);
+        if (r == 1)
+            return;
+        p->connectFlag = 0;
+        ir_select_remove_write (cs_fileno (p->cs_link), p);
+        if (r < 0)
+        {
+            printf ("cs_rcvconnect error\n");
+            if (p->failback)
+                Tcl_Eval (p->interp, p->failback);
+            do_disconnect (p, NULL, 0, NULL);
+            return;
+        }
+        if (p->callback)
+           Tcl_Eval (p->interp, p->callback);
+        return;
+    }
     do
     {
         if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in))  <= 0)
         {
             printf ("cs_get failed\n");
             ir_select_remove (cs_fileno (p->cs_link), p);
+            if (p->failback)
+                Tcl_Eval (p->interp, p->failback);
+            do_disconnect (p, NULL, 0, NULL);
             return;
         }        
         if (r == 1)
@@ -1438,6 +1597,9 @@ void ir_select_read (ClientData clientData)
         if (!z_APDU (p->odr_in, &apdu, 0))
         {
             printf ("%s\n", odr_errlist [odr_geterror (p->odr_in)]);
+            if (p->failback)
+                Tcl_Eval (p->interp, p->failback);
+            do_disconnect (p, NULL, 0, NULL);
             return;
         }
         switch(apdu->which)
@@ -1454,6 +1616,9 @@ void ir_select_read (ClientData clientData)
         default:
             printf("Received unknown APDU type (%d).\n", 
                    apdu->which);
+            if (p->failback)
+                Tcl_Eval (p->interp, p->failback);
+            do_disconnect (p, NULL, 0, NULL);
         }
         if (p->callback)
            Tcl_Eval (p->interp, p->callback);
@@ -1469,10 +1634,32 @@ void ir_select_write (ClientData clientData)
     int r;
 
     printf ("In write handler.....\n");
+    if (p->connectFlag)
+    {
+        r = cs_rcvconnect (p->cs_link);
+        if (r == 1)
+            return;
+        p->connectFlag = 0;
+        if (r < 0)
+        {
+            printf ("cs_rcvconnect error\n");
+            ir_select_remove_write (cs_fileno (p->cs_link), p);
+            if (p->failback)
+                Tcl_Eval (p->interp, p->failback);
+            do_disconnect (p, NULL, 0, NULL);
+            return;
+        }
+        ir_select_remove_write (cs_fileno (p->cs_link), p);
+        if (p->callback)
+           Tcl_Eval (p->interp, p->callback);
+        return;
+    }
     if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
     {   
         printf ("select write fail\n");
-        cs_close (p->cs_link);
+        if (p->failback)
+            Tcl_Eval (p->interp, p->failback);
+        do_disconnect (p, NULL, 0, NULL);
     }
     else if (r == 0)            /* remove select bit */
     {