Non-blocking i/o used now. Database names popup as cascade items.
[ir-tcl-moved-to-github.git] / ir-tcl.c
index 94f09c1..19bed8c 100644 (file)
--- a/ir-tcl.c
+++ b/ir-tcl.c
@@ -3,7 +3,27 @@
  * (c) Index Data 1995
  *
  * $Log: ir-tcl.c,v $
- * Revision 1.7  1995-03-14 17:32:29  adam
+ * Revision 1.13  1995-03-17 18:26:17  adam
+ * Non-blocking i/o used now. Database names popup as cascade items.
+ *
+ * Revision 1.12  1995/03/17  15:45:00  adam
+ * Improved target/database setup.
+ *
+ * Revision 1.11  1995/03/16  17:54:03  adam
+ * Minor changes really.
+ *
+ * Revision 1.10  1995/03/15  16:14:50  adam
+ * Blocking arg in cs_create changed.
+ *
+ * Revision 1.9  1995/03/15  13:59:24  adam
+ * Minor changes.
+ *
+ * Revision 1.8  1995/03/15  08:25:16  adam
+ * New method presentStatus to check for error on present. Misc. cleanup
+ * of IRRecordList manipulations. Full MARC record presentation in
+ * search.tcl.
+ *
+ * Revision 1.7  1995/03/14  17:32:29  adam
  * Presentation of full Marc record in popup window.
  *
  * Revision 1.6  1995/03/12  19:31:55  adam
@@ -31,6 +51,7 @@
 
 #include <odr.h>
 #include <proto.h>
+#include <diagbib1.h>
 
 #include <tcl.h>
 
@@ -40,7 +61,7 @@ typedef struct {
     COMSTACK cs_link;
 
     int preferredMessageSize;
-    int maximumMessageSize;
+    int maximumRecordSize;
     Odr_bitmask options;
     Odr_bitmask protocolVersion;
     char *idAuthentication;
@@ -55,6 +76,9 @@ typedef struct {
     char *buf_in;
     int  len_in;
 
+    char *sbuf;
+    int  slen;
+
     ODR odr_in;
     ODR odr_out;
     ODR odr_pr;
@@ -83,8 +107,8 @@ typedef struct IRRecordList_ {
             Iso2709Rec rec;
         } marc;
         struct {
-            int code;
-            char *add_info;
+            int  condition;
+            char *addinfo;
         } diag;
     } u;
     struct IRRecordList_ *next;
@@ -97,6 +121,9 @@ typedef struct IRSetObj_ {
     int number;
     int numberOfRecordsReturned;
     Z_Records *z_records;
+    int which;
+    int condition;
+    char *addinfo;
     IRRecordList *record_list;
 } IRSetObj;
 
@@ -108,6 +135,49 @@ typedef struct {
 
 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
 
+static IRRecordList *new_IR_record (IRSetObj *setobj, int no, int which)
+{
+    IRRecordList *rl;
+
+    for (rl = setobj->record_list; rl; rl = rl->next)
+    {
+        if (no == rl->no)
+        {
+            switch (rl->which)
+            {
+            case Z_NamePlusRecord_databaseRecord:
+                iso2709_rm (rl->u.marc.rec);
+                break;
+            case Z_NamePlusRecord_surrogateDiagnostic:
+                free (rl->u.diag.addinfo);
+                rl->u.diag.addinfo = NULL;
+                break;
+            }
+            break;
+        }
+    }
+    if (!rl)
+    {
+        rl = malloc (sizeof(*rl));
+        assert (rl);
+        rl->next = setobj->record_list;
+        rl->no = no;
+        setobj->record_list = rl;
+    }
+    rl->which = which;
+    return rl;
+}
+
+static IRRecordList *find_IR_record (IRSetObj *setobj, int no)
+{
+    IRRecordList *rl;
+
+    for (rl = setobj->record_list; rl; rl = rl->next)
+        if (no == rl->no)
+            return rl;
+    return NULL;
+}
+
 /*
  * get_parent_info: Returns information about parent object.
  */
@@ -235,14 +305,13 @@ static int do_init_request (void *obj, Tcl_Interp *interp,
     Z_APDU apdu, *apdup;
     IRObj *p = obj;
     Z_InitRequest req;
-    char *sbuf;
-    int slen;
+    int r;
 
     req.referenceId = 0;
     req.options = &p->options;
     req.protocolVersion = &p->protocolVersion;
     req.preferredMessageSize = &p->preferredMessageSize;
-    req.maximumRecordSize = &p->maximumMessageSize;
+    req.maximumRecordSize = &p->maximumRecordSize;
 
     req.idAuthentication = p->idAuthentication;
     req.implementationId = p->implementationId;
@@ -261,13 +330,19 @@ static int do_init_request (void *obj, Tcl_Interp *interp,
         odr_reset (p->odr_out);
         return TCL_ERROR;
     }
-    sbuf = odr_getbuf (p->odr_out, &slen);
-    if (cs_put (p->cs_link, sbuf, slen) < 0)
-    {
+    p->sbuf = odr_getbuf (p->odr_out, &p->slen);
+    if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
+    {     
         interp->result = "cs_put failed in init";
         return TCL_ERROR;
     }
-    printf("Sent initializeRequest (%d bytes).\n", slen);
+    else if (r == 1)
+    {
+        ir_select_add_write (cs_fileno(p->cs_link), p);
+        printf("Sent part of initializeRequest (%d bytes).\n", p->slen);
+    }
+    else
+        printf("Sent whole initializeRequest (%d bytes).\n", p->slen);
     return TCL_OK;
 }
 
@@ -312,19 +387,19 @@ static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
 }
 
 /*
- * do_maximumMessageSize: Set/get maximum message size
+ * do_maximumRecordSize: Set/get maximum record size
  */
-static int do_maximumMessageSize (void *obj, Tcl_Interp *interp,
+static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
                                     int argc, char **argv)
 {
     char buf[20];
     if (argc == 3)
     {
         if (Tcl_GetInt (interp, argv[2], 
-                        &((IRObj *)obj)->maximumMessageSize)==TCL_ERROR)
+                        &((IRObj *)obj)->maximumRecordSize)==TCL_ERROR)
             return TCL_ERROR;
     }
-    sprintf (buf, "%d", ((IRObj *)obj)->maximumMessageSize);
+    sprintf (buf, "%d", ((IRObj *)obj)->maximumRecordSize);
     Tcl_AppendResult (interp, buf, NULL);
     return TCL_OK;
 }
@@ -475,6 +550,7 @@ static int do_comstack (void *obj, Tcl_Interp *interp,
     char *cs_type = NULL;
     if (argc == 3)
     {
+        cs_close (((IRObj*) obj)->cs_link);
         if (!strcmp (argv[2], "tcpip"))
             ((IRObj *)obj)->cs_link = cs_create (tcpip_type, 0);
         else if (!strcmp (argv[2], "mosi"))
@@ -573,7 +649,7 @@ static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
     { 0, "protocolVersion",         do_protocolVersion },
     { 0, "options",                 do_options },
     { 1, "preferredMessageSize",    do_preferredMessageSize },
-    { 1, "maximumMessageSize",      do_maximumMessageSize },
+    { 1, "maximumRecordSize",       do_maximumRecordSize },
     { 1, "implementationName",      do_implementationName },
     { 1, "implementationId",        do_implementationId },
     { 1, "idAuthentication",        do_idAuthentication },
@@ -615,7 +691,7 @@ static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
         return TCL_ERROR;
     obj->cs_link = cs_create (tcpip_type, 0);
 
-    obj->maximumMessageSize = 32768;
+    obj->maximumRecordSize = 32768;
     obj->preferredMessageSize = 4096;
 
     obj->idAuthentication = NULL;
@@ -684,8 +760,7 @@ static int do_search (void *o, Tcl_Interp *interp,
     Odr_oct ccl_query;
     IRSetObj *obj = o;
     IRObj *p = obj->parent;
-    char *sbuf;
-    int slen;
+    int r;
 
     p->child = o;
     if (argc != 3)
@@ -751,13 +826,21 @@ static int do_search (void *o, Tcl_Interp *interp,
         odr_reset (p->odr_out);
         return TCL_ERROR;
     } 
-    sbuf = odr_getbuf (p->odr_out, &slen);
-    if (cs_put (p->cs_link, sbuf, slen) < 0)
+    p->sbuf = odr_getbuf (p->odr_out, &p->slen);
+    if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
     {
         interp->result = "cs_put failed in init";
         return TCL_ERROR;
     }
-    printf ("Search request\n");
+    else if (r == 1)
+    {
+        ir_select_add_write (cs_fileno(p->cs_link), p);
+        printf("Sent part of searchRequest (%d bytes).\n", p->slen);
+    }
+    else
+    {
+        printf ("Whole search request\n");
+    }
     return TCL_OK;
 }
 
@@ -843,14 +926,14 @@ static int get_marc_lines (Tcl_Interp *interp, Iso2709Rec rec,
         for (field = dir->fields; field; field = field->next)
         {
             if (!field->identifier)
-                Tcl_AppendResult (interp, "{{} ", NULL);
+                Tcl_AppendResult (interp, "{{}", NULL);
             else
             {
                 if (argc > 6 && marc_cmp (field->identifier, argv[6]))
                     continue;
-                Tcl_AppendResult (interp, "{", field->identifier, " ", NULL);
+                Tcl_AppendResult (interp, "{", field->identifier, NULL);
             }
-            Tcl_AppendResult (interp, "{", field->data, "}", NULL);
+            Tcl_AppendElement (interp, field->data);
             Tcl_AppendResult (interp, "} ", NULL);
         }
         Tcl_AppendResult (interp, "}} ", NULL);
@@ -874,11 +957,7 @@ static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
     }
     if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
         return TCL_ERROR;
-    for (rl = obj->record_list; rl; rl = rl->next)
-    {
-        if (rl->no == offset)
-            break;
-    }
+    rl = find_IR_record (obj, offset);
     if (!rl)
         return TCL_OK;
     switch (rl->which)
@@ -889,9 +968,6 @@ static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
     case Z_NamePlusRecord_surrogateDiagnostic:
         interp->result = "surrogateDiagnostic";
         break;
-    default:
-        interp->result = "unknown";
-        break;
     }
     return TCL_OK;
 }
@@ -913,11 +989,7 @@ static int do_recordDiag (void *o, Tcl_Interp *interp, int argc, char **argv)
     }
     if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
         return TCL_ERROR;
-    for (rl = obj->record_list; rl; rl = rl->next)
-    {
-        if (rl->no == offset)
-            break;
-    }
+    rl = find_IR_record (obj, offset);
     if (!rl)
     {
         Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
@@ -928,12 +1000,10 @@ static int do_recordDiag (void *o, Tcl_Interp *interp, int argc, char **argv)
         Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
         return TCL_ERROR;
     }
-    sprintf (buf, "%d ", rl->u.diag.code);
-    Tcl_AppendResult (interp, buf, NULL);
-    if (rl->u.diag.add_info)
-        Tcl_AppendElement (interp, rl->u.diag.add_info);
-    else
-        Tcl_AppendElement (interp, "");
+    sprintf (buf, "%d", rl->u.diag.condition);
+    Tcl_AppendResult (interp, buf, " {", 
+                      (rl->u.diag.addinfo ? rl->u.diag.addinfo : ""),
+                      "}", NULL);
     return TCL_OK;
 }
 
@@ -953,9 +1023,7 @@ static int do_recordMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
     }
     if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
         return TCL_ERROR;
-    for (rl = obj->record_list; rl; rl = rl->next)
-        if (rl->no == offset)
-            break;
+    rl = find_IR_record (obj, offset);
     if (!rl)
     {
         Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
@@ -978,6 +1046,39 @@ static int do_recordMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
 }
 
 /*
+ * do_presentStatus: Return present status (after present response)
+ */
+static int do_presentStatus (void *o, Tcl_Interp *interp, 
+                             int argc, char **argv)
+{
+    IRSetObj *obj = o;
+    const char *cp;
+    char buf[28];
+
+    switch (obj->which)
+    {
+    case Z_Records_DBOSD:
+       Tcl_AppendElement (interp, "DBOSD");
+        break;
+    case Z_Records_NSD:
+        Tcl_AppendElement (interp, "NSD");
+        sprintf (buf, "%d", obj->condition);
+        Tcl_AppendElement (interp, buf);
+        cp = diagbib1_str (obj->condition);
+        if (cp)
+            Tcl_AppendElement (interp, (char*) cp);
+        else
+            Tcl_AppendElement (interp, "");
+        if (obj->addinfo)
+            Tcl_AppendElement (interp, obj->addinfo);
+        else
+            Tcl_AppendElement (interp, "");
+        break;
+    }
+    return TCL_OK;
+}
+
+/*
  * do_present: Perform Present Request
  */
 
@@ -990,8 +1091,7 @@ static int do_present (void *o, Tcl_Interp *interp,
     Z_PresentRequest req;
     int start;
     int number;
-    char *sbuf;
-    int slen;
+    int r;
 
     if (argc >= 3)
     {
@@ -1027,13 +1127,23 @@ static int do_present (void *o, Tcl_Interp *interp,
         odr_reset (p->odr_out);
         return TCL_ERROR;
     } 
-    sbuf = odr_getbuf (p->odr_out, &slen);
-    if (cs_put (p->cs_link, sbuf, slen) < 0)
+    p->sbuf = odr_getbuf (p->odr_out, &p->slen);
+    if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
     {
         interp->result = "cs_put failed in init";
         return TCL_ERROR;
     }
-    printf ("Present request, start=%d, num=%d\n", start, number);
+    else if (r == 1)
+    {
+        ir_select_add_write (cs_fileno(p->cs_link), p);
+        printf ("Part of present request, start=%d, num=%d (%d bytes)\n",
+                start, number, p->slen);
+    }
+    else
+    {
+        printf ("Whole present request, start=%d, num=%d (%d bytes)\n",
+                start, number, p->slen);
+    }
     return TCL_OK;
 }
 
@@ -1068,24 +1178,7 @@ static int do_loadFile (void *o, Tcl_Interp *interp,
         rec = iso2709_cvt (buf);
         if (!rec)
             break;
-        for (rl = setobj->record_list; rl; rl = rl->next)
-        {
-            if (no == rl->no)
-            {
-                if (rl->which == Z_NamePlusRecord_databaseRecord)
-                    iso2709_rm (rl->u.marc.rec);
-                break;
-            }
-        }
-        if (!rl)
-        {
-            rl = malloc (sizeof(*rl));
-            assert (rl);
-            rl->next = setobj->record_list;
-            rl->no = no;
-            setobj->record_list = rl;
-        }
-        rl->which = Z_NamePlusRecord_databaseRecord;
+        rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
         rl->u.marc.rec = rec;
         no++;
     }
@@ -1109,6 +1202,7 @@ static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
     { 0, "recordType",              do_recordType },
     { 0, "recordMarc",              do_recordMarc },
     { 0, "recordDiag",              do_recordDiag },
+    { 0, "presentStatus",           do_presentStatus },
     { 0, "loadFile",                do_loadFile },
     { 0, NULL, NULL}
     };
@@ -1149,6 +1243,7 @@ static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
         return TCL_ERROR;
     obj->z_records = NULL;
     obj->record_list = NULL;
+    obj->addinfo = NULL;
     obj->parent = (IRObj *) parent_info.clientData;
     Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
                        (ClientData) obj, ir_set_obj_delete);
@@ -1183,6 +1278,10 @@ static void ir_initResponse (void *obj, Z_InitResponse *initrs)
         printf("Name   : %s\n", initrs->implementationName);
     if (initrs->implementationVersion)
         printf("Version: %s\n", initrs->implementationVersion);
+    if (initrs->maximumRecordSize)
+        printf ("MaximumRecordSize=%d\n", *initrs->maximumRecordSize);
+    if (initrs->preferredMessageSize)
+        printf ("PreferredMessageSize=%d\n", *initrs->preferredMessageSize);
 #if 0
     if (initrs->userInformationField)
     {
@@ -1203,18 +1302,20 @@ static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
     printf ("Received presentResponse\n");
     if (zrs)
     {
+        setobj->which = zrs->which;
         if (zrs->which == Z_Records_NSD)
         {
-            setobj->numberOfRecordsReturned = 0;
+            const char *addinfo;
+
             printf ("They are diagnostic!!!\n");
-            /*            
-               char buf[16];
-               sprintf (buf, "%d", *zrs->u.nonSurrogateDiagnostic->condition);
-               Tcl_AppendResult (interp, "Diagnostic message: ", buf,
-               " : ",
-               zrs->u.nonSurrogateDiagnostic->addinfo, NULL);
-               return TCL_ERROR;
-               */
+
+            setobj->numberOfRecordsReturned = 0;
+            setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
+            free (setobj->addinfo);
+            setobj->addinfo = NULL;
+            addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
+            if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
+                strcpy (setobj->addinfo, addinfo);
             return;
         }
         else
@@ -1227,31 +1328,20 @@ static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
             printf ("Got %d records\n", setobj->numberOfRecordsReturned);
             for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
             {
-                int no = setobj->start + offset;
-                
-                for (rl = setobj->record_list; rl; rl = rl->next)
-                {
-                    if (no == rl->no)
-                    {
-                        if (rl->which == Z_NamePlusRecord_databaseRecord)
-                            iso2709_rm (rl->u.marc.rec);
-                        break;
-                    }
-                }
-                if (!rl)
-                {
-                    rl = malloc (sizeof(*rl));
-                    assert (rl);
-                    rl->next = setobj->record_list;
-                    rl->no = no;
-                    setobj->record_list = rl;
-                }
-                rl->which = zrs->u.databaseOrSurDiagnostics->
-                            records[offset]->which;
+                rl = new_IR_record (setobj, setobj->start + offset,
+                                    zrs->u.databaseOrSurDiagnostics->
+                                    records[offset]->which);
                 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
                 {
-                    rl->u.diag.code = 0;
-                    rl->u.diag.add_info = NULL;
+                    Z_DiagRec *diagrec;
+
+                    diagrec = zrs->u.databaseOrSurDiagnostics->
+                              records[offset]->u.surrogateDiagnostic;
+
+                    rl->u.diag.condition = *diagrec->condition;
+                    if (diagrec->addinfo && (rl->u.diag.addinfo =
+                        malloc (strlen (diagrec->addinfo)+1)))
+                        strcpy (rl->u.diag.addinfo, diagrec->addinfo);
                 }
                 else
                 {
@@ -1279,7 +1369,10 @@ static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
     }
 }
 
-void ir_select_proc (ClientData clientData)
+/*
+ * ir_select_read: handle incoming packages
+ */
+void ir_select_read (ClientData clientData)
 {
     IRObj *p = clientData;
     Z_APDU *apdu;
@@ -1293,6 +1386,8 @@ void ir_select_proc (ClientData clientData)
             ir_select_remove (cs_fileno (p->cs_link), p);
             return;
         }        
+        if (r == 1)
+            return ;
         odr_setbuf (p->odr_in, p->buf_in, r);
         printf ("cs_get ok, got %d\n", r);
         if (!z_APDU (p->odr_in, &apdu, 0))
@@ -1320,6 +1415,25 @@ void ir_select_proc (ClientData clientData)
     } while (cs_more (p->cs_link));    
 }
 
+/*
+ * ir_select_write: handle outgoing packages - not yet written.
+ */
+void ir_select_write (ClientData clientData)
+{
+    IRObj *p = clientData;
+    int r;
+    
+    if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
+    {   
+        printf ("select write fail\n");
+        cs_close (p->cs_link);
+    }
+    else if (r == 0)            /* remove select bit */
+    {
+        ir_select_remove_write (cs_fileno (p->cs_link), p);
+    }
+}
+
 /* ------------------------------------------------------- */
 
 /*
@@ -1333,3 +1447,5 @@ int ir_tcl_init (Tcl_Interp *interp)
                       (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
     return TCL_OK;
 }
+
+