Request queue.
[ir-tcl-moved-to-github.git] / ir-tcl.c
index 5569990..ddd1cbd 100644 (file)
--- a/ir-tcl.c
+++ b/ir-tcl.c
@@ -5,7 +5,49 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: ir-tcl.c,v $
- * Revision 1.41  1995-06-16 12:28:16  adam
+ * Revision 1.51  1995-08-03 13:22:54  adam
+ * Request queue.
+ *
+ * Revision 1.50  1995/07/20  08:09:49  adam
+ * client.tcl: Targets removed from hotTargets list when targets
+ *  are removed/modified.
+ * ir-tcl.c: More work on triggerResourceControl.
+ *
+ * Revision 1.49  1995/06/30  12:39:21  adam
+ * Bug fix: loadFile didn't set record type.
+ * The MARC routines are a little less strict in the interpretation.
+ * Script display.tcl replaces the old marc.tcl.
+ * New interactive script: shell.tcl.
+ *
+ * Revision 1.48  1995/06/27  19:03:50  adam
+ * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
+ * nextResultSetPosition used instead of setOffset.
+ *
+ * Revision 1.47  1995/06/25  10:25:04  adam
+ * Working on triggerResourceControl. Description of compile/install
+ * procedure moved to ir-tcl.sgml.
+ *
+ * Revision 1.46  1995/06/22  13:15:06  adam
+ * Feature: SUTRS. Setting getSutrs implemented.
+ * Work on display formats.
+ * Preferred record syntax can be set by the user.
+ *
+ * Revision 1.45  1995/06/20  08:07:30  adam
+ * New setting: failInfo.
+ * Working on better cancel mechanism.
+ *
+ * Revision 1.44  1995/06/19  17:01:20  adam
+ * Minor changes.
+ *
+ * Revision 1.43  1995/06/19  13:06:08  adam
+ * New define: IR_TCL_VERSION.
+ *
+ * Revision 1.42  1995/06/19  08:08:52  adam
+ * client.tcl: hotTargets now contain both database and target name.
+ * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
+ * by logf(LOG_WARN, ...) calls.
+ *
+ * Revision 1.41  1995/06/16  12:28:16  adam
  * Implemented preferredRecordSyntax.
  * Minor changes in diagnostic handling.
  * Record list deleted when connection closes.
@@ -158,6 +200,8 @@ typedef struct {
     IrTcl_Method *tab;
 } IrTcl_Methods;
 
+static Tcl_Interp *irTcl_interp;
+
 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
 static int do_disconnect (void *obj, Tcl_Interp *interp, 
                           int argc, char **argv);
@@ -235,6 +279,10 @@ int IrTcl_eval (Tcl_Interp *interp, const char *command)
     }
     strcpy (tmp, command);
     r = Tcl_Eval (interp, tmp);
+    if (r == TCL_ERROR)
+        logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine, 
+              interp->result);
+    Tcl_FreeResult (interp);
     free (tmp);
     return r;
 }
@@ -333,7 +381,8 @@ int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
 
     if (argc <= 0)
         return TCL_OK;
-    Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
+    Tcl_AppendResult (interp, "Bad method: ", argv[1], 
+                      ". Possible methods:", NULL);
     for (tab_i = tab; tab_i->tab; tab_i++)
         for (t = tab_i->tab; t->name; t++)
             Tcl_AppendResult (interp, " ", t->name, NULL);
@@ -470,6 +519,41 @@ static void get_referenceId (char **dst, Z_ReferenceId *src)
 
 /* ------------------------------------------------------- */
 
+#if 0
+/*
+ * ir-tcl_send_APDU: send APDU
+ */
+static int ir_tcl_send_APDU (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu, 
+                             const char *msg)
+{
+    int r;
+
+    if (!z_APDU (p->odr_out, &apdu, 0))
+    {
+        Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
+                          NULL);
+        odr_reset (p->odr_out);
+        return TCL_ERROR;
+    }
+    p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
+    odr_reset (p->odr_out);
+    if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
+    {     
+        sprintf (interp->result, "cs_put failed in %s", msg);
+        do_disconnect (p, NULL, 2, NULL);
+        return TCL_ERROR;
+    }
+    else if (r == 1)
+    {
+        ir_select_add_write (cs_fileno(p->cs_link), p);
+        logf (LOG_DEBUG, "Sent part of %s (%d bytes)", msg, p->slen);
+    }
+    else
+        logf (LOG_DEBUG, "Sent whole %s (%d bytes)", msg, p->slen);
+    return TCL_OK;
+}
+#endif
+
 /*
  * do_init_request: init method on IR object
  */
@@ -479,7 +563,6 @@ static int do_init_request (void *obj, Tcl_Interp *interp,
     Z_APDU *apdu;
     IrTcl_Obj *p = obj;
     Z_InitRequest *req;
-    int r;
 
     if (argc <= 0)
         return TCL_OK;
@@ -488,7 +571,6 @@ static int do_init_request (void *obj, Tcl_Interp *interp,
         interp->result = "not connected";
         return TCL_ERROR;
     }
-    odr_reset (p->odr_out);
     apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
     req = apdu->u.initRequest;
 
@@ -534,28 +616,7 @@ static int do_init_request (void *obj, Tcl_Interp *interp,
     req->implementationVersion = p->implementationVersion;
     req->userInformationField = 0;
 
-    if (!z_APDU (p->odr_out, &apdu, 0))
-    {
-        Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
-                          NULL);
-        odr_reset (p->odr_out);
-        return TCL_ERROR;
-    }
-    p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
-    if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
-    {     
-        interp->result = "cs_put failed in init";
-        do_disconnect (p, NULL, 2, NULL);
-        return TCL_ERROR;
-    }
-    else if (r == 1)
-    {
-        ir_select_add_write (cs_fileno(p->cs_link), p);
-        logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
-    }
-    else
-        logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
-    return TCL_OK;
+    return ir_tcl_send_APDU (interp, p, apdu, "init");
 }
 
 /*
@@ -564,13 +625,8 @@ static int do_init_request (void *obj, Tcl_Interp *interp,
 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
                                int argc, char **argv)
 {
-    static struct ir_named_entry version_tab[] = {
-    { "1", 0 },
-    { "2", 1 },
-    { "3", 2 },
-    { "4", 3 },
-    { NULL,0}
-    };
+    int version, i;
+    char buf[10];
     IrTcl_Obj *p = obj;
 
     if (argc <= 0)
@@ -580,8 +636,20 @@ static int do_protocolVersion (void *obj, Tcl_Interp *interp,
        ODR_MASK_SET (&p->protocolVersion, 1);
         return TCL_OK;
     }
-    return ir_named_bits (version_tab, &p->protocolVersion,
-                          interp, argc-2, argv+2);
+    if (argc == 3)
+    {
+        if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
+            return TCL_ERROR;
+        ODR_MASK_ZERO (&p->protocolVersion);
+        for (i = 0; i<version; i++)
+            ODR_MASK_SET (&p->protocolVersion, i);
+    }
+    for (i = 4; --i >= 0; )
+        if (ODR_MASK_GET (&p->protocolVersion, i))
+            break;
+    sprintf (buf, "%d", i+1);
+    interp->result = buf;
+    return TCL_OK;
 }
 
 /*
@@ -600,7 +668,7 @@ static int do_options (void *obj, Tcl_Interp *interp,
     { "accessCtrl", 6},
     { "scan", 7},
     { "sort", 8},
-    { "extentedServices", 10},
+    { "extendedServices", 10},
     { "level-1Segmentation", 11},
     { "level-2Segmentation", 12},
     { "concurrentOperations", 13},
@@ -614,6 +682,7 @@ static int do_options (void *obj, Tcl_Interp *interp,
         ODR_MASK_ZERO (&p->options);
        ODR_MASK_SET (&p->options, 0);
        ODR_MASK_SET (&p->options, 1);
+        ODR_MASK_SET (&p->options, 4);
        ODR_MASK_SET (&p->options, 7);
        ODR_MASK_SET (&p->options, 14);
        return TCL_OK;
@@ -622,6 +691,48 @@ static int do_options (void *obj, Tcl_Interp *interp,
 }
 
 /*
+ * do_failInfo: Get fail information
+ */
+static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
+{
+    char buf[16], *cp;
+    IrTcl_Obj *p = obj;
+
+    if (argc <= 0)
+    {
+        p->failInfo = 0;
+       return TCL_OK;
+    }
+    sprintf (buf, "%d", p->failInfo);
+    switch (p->failInfo)
+    {
+    case 0:
+        cp = "ok";
+        break;
+    case IR_TCL_FAIL_CONNECT:
+        cp = "connect failed";
+        break;
+    case IR_TCL_FAIL_READ:
+        cp = "connection closed";
+        break;
+    case IR_TCL_FAIL_WRITE:
+        cp = "connection closed";
+        break;
+    case IR_TCL_FAIL_IN_APDU:
+        cp = "failed to decode incoming APDU";
+        break;
+    case IR_TCL_FAIL_UNKNOWN_APDU:
+        cp = "unknown APDU";
+        break;
+    default:
+        cp = "";
+    } 
+    Tcl_AppendElement (interp, buf);
+    Tcl_AppendElement (interp, cp);
+    return TCL_OK;
+}
+
+/*
  * do_preferredMessageSize: Set/get preferred message size
  */
 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
@@ -631,7 +742,7 @@ static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
 
     if (argc <= 0)
     {
-        p->preferredMessageSize = 4096;
+        p->preferredMessageSize = 30000;
        return TCL_OK;
     }
     return get_set_int (&p->preferredMessageSize, interp, argc, argv);
@@ -647,7 +758,7 @@ static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
 
     if (argc <= 0)
     {
-        p->maximumRecordSize = 32768;
+        p->maximumRecordSize = 30000;
        return TCL_OK;
     }
     return get_set_int (&p->maximumRecordSize, interp, argc, argv);
@@ -677,7 +788,7 @@ static int do_implementationName (void *obj, Tcl_Interp *interp,
 
     if (argc == 0)
         return ir_strdup (interp, &p->implementationName,
-                          "Index Data/TCL/TK on YAZ");
+                          "Index Data/IrTcl on YAZ");
     else if (argc == -1)
         return ir_strdel (interp, &p->implementationName);
     if (argc == 3)
@@ -716,7 +827,8 @@ static int do_implementationVersion (void *obj, Tcl_Interp *interp,
     IrTcl_Obj *p = obj;
 
     if (argc == 0)
-        return ir_strdup (interp, &p->implementationVersion, YAZ_VERSION);
+        return ir_strdup (interp, &p->implementationVersion, 
+                          "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
     else if (argc == -1)
         return ir_strdel (interp, &p->implementationVersion);
     Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
@@ -730,7 +842,7 @@ static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
                                         int argc, char **argv)
 {
     IrTcl_Obj *p = obj;
-
+    
     if (argc == 0)
     {
         p->targetImplementationName = NULL;
@@ -892,7 +1004,7 @@ static int do_connect (void *obj, Tcl_Interp *interp,
             return TCL_ERROR;
         if ((r=cs_connect (p->cs_link, addr)) < 0)
         {
-            interp->result = "cs_connect fail";
+            interp->result = "connect fail";
             do_disconnect (p, NULL, 2, NULL);
             return TCL_ERROR;
         }
@@ -900,17 +1012,15 @@ static int do_connect (void *obj, Tcl_Interp *interp,
         if (r == 1)
         {
             ir_select_add_write (cs_fileno (p->cs_link), p);
-            p->connectFlag = 1;
+            p->state = IR_TCL_R_Connecting;
         }
         else
         {
-            p->connectFlag = 0;
+            p->state = IR_TCL_R_Idle;
             if (p->callback)
                 IrTcl_eval (p->interp, p->callback);
         }
     }
-    if (p->hostname)
-        Tcl_AppendElement (interp, p->hostname);
     return TCL_OK;
 }
 
@@ -924,7 +1034,7 @@ static int do_disconnect (void *obj, Tcl_Interp *interp,
 
     if (argc == 0)
     {
-        p->connectFlag = 0;
+        p->state = IR_TCL_R_Idle;
         p->hostname = NULL;
        p->cs_link = NULL;
         return TCL_OK;
@@ -943,8 +1053,13 @@ static int do_disconnect (void *obj, Tcl_Interp *interp,
         ODR_MASK_ZERO (&p->options);
        ODR_MASK_SET (&p->options, 0);
        ODR_MASK_SET (&p->options, 1);
+       ODR_MASK_SET (&p->options, 4);
        ODR_MASK_SET (&p->options, 7);
        ODR_MASK_SET (&p->options, 14);
+
+        ODR_MASK_ZERO (&p->protocolVersion);
+       ODR_MASK_SET (&p->protocolVersion, 0);
+       ODR_MASK_SET (&p->protocolVersion, 1);
     }
     assert (!p->cs_link);
     return TCL_OK;
@@ -1047,7 +1162,7 @@ static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
     }
     else if (argc == 3)
     {
-        if (!strcmp (argv[2], "Z3950"))
+        if (!strcmp (argv[2], "Z39"))
             p->protocol_type = PROTO_Z3950;
         else if (!strcmp (argv[2], "SR"))
             p->protocol_type = PROTO_SR;
@@ -1061,7 +1176,7 @@ static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
     switch (p->protocol_type)
     {
     case PROTO_Z3950:
-        Tcl_AppendElement (interp, "Z3950");
+        Tcl_AppendElement (interp, "Z39");
         break;
     case PROTO_SR:
         Tcl_AppendElement (interp, "SR");
@@ -1071,6 +1186,32 @@ static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
 }
 
 /*
+ * do_triggerResourceControl:
+ */
+static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
+                                      int argc, char **argv)
+{
+    IrTcl_Obj *p = obj;
+    Z_APDU *apdu;
+    Z_TriggerResourceControlRequest *req;
+    bool_t is_false = 0;
+
+    if (argc <= 0)
+        return TCL_OK;
+    if (!p->cs_link)
+    {
+        interp->result = "not connected";
+        return TCL_ERROR;
+    }
+    apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
+    req = apdu->u.triggerResourceControlRequest;
+    *req->requestedAction = Z_TriggerResourceCtrl_cancel;
+    req->resultSetWanted = &is_false; 
+    
+    return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl");
+}
+
+/*
  * do_databaseNames: specify database names
  */
 static int do_databaseNames (void *obj, Tcl_Interp *interp,
@@ -1280,6 +1421,7 @@ static IrTcl_Method ir_method_tab[] = {
 { 1, "comstack",                    do_comstack },
 { 1, "protocol",                    do_protocol },
 { 0, "failback",                    do_failback },
+{ 0, "failInfo",                    do_failInfo },
 
 { 1, "connect",                     do_connect },
 { 0, "protocolVersion",             do_protocolVersion },
@@ -1298,6 +1440,7 @@ static IrTcl_Method ir_method_tab[] = {
 { 0, "initResult",                  do_initResult },
 { 0, "disconnect",                  do_disconnect },
 { 0, "callback",                    do_callback },
+{ 0, "triggerResourceControl",      do_triggerResourceControl },
 { 0, NULL, NULL}
 };
 
@@ -1357,8 +1500,6 @@ static void ir_obj_delete (ClientData clientData)
     odr_destroy (obj->odr_in);
     odr_destroy (obj->odr_out);
     odr_destroy (obj->odr_pr);
-    free (obj->buf_out);
-    free (obj->buf_in);
     free (obj);
 }
 
@@ -1395,14 +1536,11 @@ static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
     obj->odr_in = odr_createmem (ODR_DECODE);
     obj->odr_out = odr_createmem (ODR_ENCODE);
     obj->odr_pr = odr_createmem (ODR_PRINT);
-
-    obj->len_out = 10000;
-    if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
-        return TCL_ERROR;
-    odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
+    obj->state = IR_TCL_R_Idle;
 
     obj->len_in = 0;
     obj->buf_in = NULL;
+    obj->request_queue = NULL;
 
     tab[0].tab = ir_method_tab;
     tab[0].obj = obj;
@@ -1428,13 +1566,14 @@ static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
     Z_APDU *apdu;
     Odr_oct ccl_query;
     IrTcl_SetObj *obj = o;
-    IrTcl_Obj *p = obj->parent;
+    IrTcl_Obj *p;
     int r;
     oident bib1;
 
     if (argc <= 0)
         return TCL_OK;
 
+    p = obj->parent;
     p->set_child = o;
     if (argc != 3)
     {
@@ -1451,7 +1590,6 @@ static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
         interp->result = "not connected";
         return TCL_ERROR;
     }
-    odr_reset (p->odr_out);
     apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
     req = apdu->u.searchRequest;
 
@@ -1481,6 +1619,7 @@ static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
         ident.proto = p->protocol_type;
         ident.class = CLASS_RECSYN;
         ident.value = *obj->set_inher.preferredRecordSyntax;
+        logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
         req->preferredRecordSyntax = odr_oiddup (p->odr_out, 
                                                  oid_getoidbyent (&ident));
     }
@@ -1540,28 +1679,7 @@ static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
         interp->result = "unknown query method";
         return TCL_ERROR;
     }
-    if (!z_APDU (p->odr_out, &apdu, 0))
-    {
-        interp->result = odr_errlist [odr_geterror (p->odr_out)];
-        odr_reset (p->odr_out);
-        return TCL_ERROR;
-    } 
-    p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
-    if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
-    {
-        interp->result = "cs_put failed in search";
-        return TCL_ERROR;
-    }
-    else if (r == 1)
-    {
-        ir_select_add_write (cs_fileno(p->cs_link), p);
-        logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
-    }
-    else
-    {
-        logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
-    }
-    return TCL_OK;
+    return ir_tcl_send_APDU (interp, p, apdu, "search");
 }
 
 /*
@@ -1613,7 +1731,10 @@ static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
     IrTcl_SetObj *obj = o;
 
     if (argc <= 0)
+    {
+        obj->nextResultSetPosition = 0;
         return TCL_OK;
+    }
     return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
 }
 
@@ -1827,6 +1948,41 @@ static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
     return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
 }
 
+/*
+ * do_getSutrs: Get SUTRS Record
+ */
+static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+    IrTcl_SetObj *obj = o;
+    int offset;
+    IrTcl_RecordList *rl;
+
+    if (argc <= 0)
+        return TCL_OK;
+    if (argc < 3)
+    {
+        sprintf (interp->result, "wrong # args");
+        return TCL_ERROR;
+    }
+    if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
+        return TCL_ERROR;
+    rl = find_IR_record (obj, offset);
+    if (!rl)
+    {
+        Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
+        return TCL_ERROR;
+    }
+    if (rl->which != Z_NamePlusRecord_databaseRecord)
+    {
+        Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
+        return TCL_ERROR;
+    }
+    if (rl->u.dbrec.type != VAL_SUTRS)
+        return TCL_OK;
+    Tcl_AppendElement (interp, rl->u.dbrec.buf);
+    return TCL_OK;
+}
+
 
 /*
  * do_responseStatus: Return response status (present or search)
@@ -1875,12 +2031,11 @@ static int do_present (void *o, Tcl_Interp *interp,
                        int argc, char **argv)
 {
     IrTcl_SetObj *obj = o;
-    IrTcl_Obj *p = obj->parent;
+    IrTcl_Obj *p;
     Z_APDU *apdu;
     Z_PresentRequest *req;
     int start;
     int number;
-    int r;
 
     if (argc <= 0)
         return TCL_OK;
@@ -1903,7 +2058,9 @@ static int do_present (void *o, Tcl_Interp *interp,
         interp->result = "not connected";
         return TCL_ERROR;
     }
-    odr_reset (p->odr_out);
+    p = obj->parent;
+    p->set_child = obj;
+
     obj->start = start;
     obj->number = number;
 
@@ -1917,32 +2074,21 @@ static int do_present (void *o, Tcl_Interp *interp,
     
     req->resultSetStartPoint = &start;
     req->numberOfRecordsRequested = &number;
-    req->preferredRecordSyntax = 0;
-
-    if (!z_APDU (p->odr_out, &apdu, 0))
-    {
-        interp->result = odr_errlist [odr_geterror (p->odr_out)];
-        odr_reset (p->odr_out);
-        return TCL_ERROR;
-    } 
-    p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
-    if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
-    {
-        interp->result = "cs_put failed in present";
-        return TCL_ERROR;
-    }
-    else if (r == 1)
+    if (obj->set_inher.preferredRecordSyntax)
     {
-        ir_select_add_write (cs_fileno(p->cs_link), p);
-        logf (LOG_DEBUG, "Part of present request, start=%d, num=%d" 
-              " (%d bytes)", start, number, p->slen);
+        struct oident ident;
+
+        ident.proto = p->protocol_type;
+        ident.class = CLASS_RECSYN;
+        ident.value = *obj->set_inher.preferredRecordSyntax;
+        logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
+        req->preferredRecordSyntax = odr_oiddup (p->odr_out, 
+                                                 oid_getoidbyent (&ident));
     }
     else
-    {
-        logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
-              " (%d bytes)", start, number, p->slen);
-    }
-    return TCL_OK;
+        req->preferredRecordSyntax = 0;
+     
+    return ir_tcl_send_APDU (interp, p, apdu, "present");
 }
 
 /*
@@ -1976,6 +2122,7 @@ static int do_loadFile (void *o, Tcl_Interp *interp,
         IrTcl_RecordList *rl;
 
         rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
+        rl->u.dbrec.type = VAL_USMARC;
         rl->u.dbrec.buf = buf;
        rl->u.dbrec.size = size;
         no++;
@@ -1996,6 +2143,7 @@ static IrTcl_Method ir_set_method_tab[] = {
     { 0, "present",                 do_present },
     { 0, "type",                    do_type },
     { 0, "getMarc",                 do_getMarc },
+    { 0, "getSutrs",                do_getSutrs },
     { 0, "recordType",              do_recordType },
     { 0, "diag",                    do_diag },
     { 0, "responseStatus",          do_responseStatus },
@@ -2134,7 +2282,6 @@ static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
     Z_APDU *apdu;
     IrTcl_ScanObj *obj = o;
     IrTcl_Obj *p = obj->parent;
-    int r;
     oident bib1;
 #if CCL2RPN
     struct ccl_rpn_node *rpn;
@@ -2159,7 +2306,6 @@ static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
         interp->result = "not connected";
        return TCL_ERROR;
     }
-    odr_reset (p->odr_out);
 
     bib1.proto = p->protocol_type;
     bib1.class = CLASS_ATTSET;
@@ -2199,29 +2345,8 @@ static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
           *req->numberOfTermsRequested);
     logf (LOG_DEBUG, "preferredPositionInResponse=%d",
           *req->preferredPositionInResponse);
-
-    if (!z_APDU (p->odr_out, &apdu, 0))
-    {
-        interp->result = odr_errlist [odr_geterror (p->odr_out)];
-        odr_reset (p->odr_out);
-        return TCL_ERROR;
-    } 
-    p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
-    if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
-    {
-        interp->result = "cs_put failed in scan";
-        return TCL_ERROR;
-    }
-    else if (r == 1)
-    {
-        ir_select_add_write (cs_fileno(p->cs_link), p);
-        logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
-    }
-    else
-    {
-        logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
-    }
-    return TCL_OK;
+    
+    return ir_tcl_send_APDU (interp, p, apdu, "scan");
 }
 
 /*
@@ -2578,22 +2703,43 @@ static void ir_handleRecords (void *o, Z_Records *zrs)
             {
                 Z_DatabaseRecord *zr; 
                 Odr_external *oe;
+                struct oident *ident;
                 
                 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
                     ->u.databaseRecord;
                 oe = (Odr_external*) zr;
                rl->u.dbrec.size = zr->u.octet_aligned->len;
+
                 rl->u.dbrec.type = VAL_USMARC;
+                if ((ident = oid_getentbyoid (oe->direct_reference)))
+                    rl->u.dbrec.type = ident->value;
                 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
                 {
-                    const char *buf = (char*) zr->u.octet_aligned->buf;
+                    char *buf = (char*) zr->u.octet_aligned->buf;
                     if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
                        memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
-                    if (oe->direct_reference)
+                }
+                else if (rl->u.dbrec.type == VAL_SUTRS && 
+                         oe->which == ODR_EXTERNAL_single)
+                {
+                    Odr_oct *rc;
+                    
+                    logf (LOG_DEBUG, "Decoding SUTRS");
+                    odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
+                                oe->u.single_ASN1_type->len, 0);
+                    if (!z_SUTRS(p->odr_in, &rc, 0))
                     {
-                        struct oident *ident = 
-                            oid_getentbyoid (oe->direct_reference);
-                        rl->u.dbrec.type = ident->value;
+                        logf (LOG_WARN, "Cannot decode SUTRS");
+                        rl->u.dbrec.buf = NULL;
+                    }
+                    else 
+                    {
+                        if ((rl->u.dbrec.buf = malloc (rc->len+1)))
+                        {
+                            memcpy (rl->u.dbrec.buf, rc->buf, rc->len);
+                            rl->u.dbrec.buf[rc->len] = '\0';
+                        }
+                        rl->u.dbrec.size = rc->len;
                     }
                 }
                 else
@@ -2774,39 +2920,51 @@ void ir_select_read (ClientData clientData)
     Z_APDU *apdu;
     int r;
 
-    if (p->connectFlag)
+    if (p->state == IR_TCL_R_Connecting)
     {
         r = cs_rcvconnect (p->cs_link);
         if (r == 1)
+        {
+            logf (LOG_WARN, "cs_rcvconnect returned 1");
             return;
-        p->connectFlag = 0;
+        }
+        p->state = IR_TCL_R_Idle;
         ir_select_remove_write (cs_fileno (p->cs_link), p);
         if (r < 0)
         {
             logf (LOG_DEBUG, "cs_rcvconnect error");
             if (p->failback)
+            {
+                p->failInfo = IR_TCL_FAIL_CONNECT;
                 IrTcl_eval (p->interp, p->failback);
+            }
             do_disconnect (p, NULL, 2, NULL);
             return;
         }
         if (p->callback)
            IrTcl_eval (p->interp, p->callback);
+        if (p->cs_link && p->request_queue)
+            ir_tcl_send_q (p, p->request_queue, "x");
         return;
     }
     do
     {
        /* signal one more use of ir object - callbacks must not
           release the ir memory (p pointer) */
+        p->state = IR_TCL_R_Reading;
        ++(p->ref_count);
         if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
         {
             logf (LOG_DEBUG, "cs_get failed, code %d", r);
             ir_select_remove (cs_fileno (p->cs_link), p);
             if (p->failback)
+            {
+                p->failInfo = IR_TCL_FAIL_READ;
                 IrTcl_eval (p->interp, p->failback);
+            }
             do_disconnect (p, NULL, 2, NULL);
 
-           /* relase ir object now if callback deleted it */
+           /* release ir object now if callback deleted it */
            ir_obj_delete (p);
             return;
         }        
@@ -2818,7 +2976,10 @@ void ir_select_read (ClientData clientData)
         {
             logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
             if (p->failback)
+            {
+                p->failInfo = IR_TCL_FAIL_IN_APDU;
                 IrTcl_eval (p->interp, p->failback);
+            }
             do_disconnect (p, NULL, 2, NULL);
 
            /* release ir object now if failback deleted it */
@@ -2842,10 +3003,28 @@ void ir_select_read (ClientData clientData)
         default:
             logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
             if (p->failback)
+            {
+                p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
                 IrTcl_eval (p->interp, p->failback);
+            }
             do_disconnect (p, NULL, 2, NULL);
         }
         odr_reset (p->odr_in);
+        if (p->request_queue)   /* remove queue entry */
+        {
+            IrTcl_Request *rq;
+            rq = p->request_queue;
+            p->request_queue = rq->next;
+            free (rq->buf_out);
+            free (rq);
+            if (!p->request_queue)
+                 p->state = IR_TCL_R_Idle;
+        }
+        else
+        {
+            logf (LOG_FATAL, "Internal error. No queue entry");
+            exit (1);
+        }
         if (p->callback)
            IrTcl_eval (p->interp, p->callback);
        if (p->ref_count == 1)
@@ -2854,7 +3033,9 @@ void ir_select_read (ClientData clientData)
            return;
        }
        --(p->ref_count);
-    } while (p->cs_link && cs_more (p->cs_link));    
+    } while (p->cs_link && cs_more (p->cs_link));
+    if (p->cs_link && p->request_queue)
+        ir_tcl_send_q (p, p->request_queue, "x");
 }
 
 /*
@@ -2864,20 +3045,24 @@ void ir_select_write (ClientData clientData)
 {
     IrTcl_Obj *p = clientData;
     int r;
+    IrTcl_Request *rq;
 
     logf (LOG_DEBUG, "In write handler");
-    if (p->connectFlag)
+    if (p->state == IR_TCL_R_Connecting)
     {
         r = cs_rcvconnect (p->cs_link);
         if (r == 1)
             return;
-        p->connectFlag = 0;
+        p->state = IR_TCL_R_Idle;
         if (r < 0)
         {
             logf (LOG_DEBUG, "cs_rcvconnect error");
             ir_select_remove_write (cs_fileno (p->cs_link), p);
             if (p->failback)
+            {
+                p->failInfo = IR_TCL_FAIL_CONNECT;
                 IrTcl_eval (p->interp, p->failback);
+            }
             do_disconnect (p, NULL, 2, NULL);
             return;
         }
@@ -2886,16 +3071,39 @@ void ir_select_write (ClientData clientData)
            IrTcl_eval (p->interp, p->callback);
         return;
     }
+#if 0
     if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
     {   
         logf (LOG_DEBUG, "select write fail");
         if (p->failback)
+        {
+            p->failInfo = IR_TCL_FAIL_WRITE;
             IrTcl_eval (p->interp, p->failback);
+        }
         do_disconnect (p, NULL, 2, NULL);
     }
+#else
+    rq = p->request_queue;
+    assert (rq);
+    if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
+    {
+        logf (LOG_DEBUG, "select write fail");
+        if (p->failback)
+        {
+            p->failInfo = IR_TCL_FAIL_WRITE;
+            IrTcl_eval (p->interp, p->failback);
+        }
+        free (rq->buf_out);
+        rq->buf_out = NULL;
+        do_disconnect (p, NULL, 2, NULL);
+    }
+#endif
     else if (r == 0)            /* remove select bit */
     {
+        p->state = IR_TCL_R_Waiting;
         ir_select_remove_write (cs_fileno (p->cs_link), p);
+        free (rq->buf_out);
+        rq->buf_out = NULL;
     }
 }
 
@@ -2912,6 +3120,7 @@ int ir_tcl_init (Tcl_Interp *interp)
                       (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
     Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
                       (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
+    irTcl_interp = interp;
     return TCL_OK;
 }