First presentRequest attempts. Hot-target list.
[ir-tcl-moved-to-github.git] / ir-tcl.c
index 85577da..9746fe9 100644 (file)
--- a/ir-tcl.c
+++ b/ir-tcl.c
@@ -2,7 +2,10 @@
  * IR toolkit for tcl/tk
  * (c) Index Data 1995
  *
- * $Id: ir-tcl.c,v 1.2 1995-03-08 07:28:29 adam Exp $
+ * $Log: ir-tcl.c,v $
+ * Revision 1.4  1995-03-09 16:15:08  adam
+ * First presentRequest attempts. Hot-target list.
+ *
  */
 
 #include <stdlib.h>
@@ -44,10 +47,20 @@ typedef struct {
 
     Tcl_Interp *interp;
     char *callback;
+
+    int smallSetUpperBound;
+    int largeSetLowerBound;
+    int mediumSetPresentNumber;
+    int replaceIndicator;
+    char **databaseNames;
+    int num_databaseNames;
+
+    struct IRSetObj_ *child;
 } IRObj;
 
-typedef struct {
+typedef struct IRSetObj_ {
     IRObj *parent;
+    int resultCount;
 } IRSetObj;
 
 typedef struct {
@@ -78,7 +91,10 @@ static int get_parent_info (Tcl_Interp *interp, const char *name,
     memcpy (parent_name, name, pos);
     parent_name[pos] = '\0';
     if (!Tcl_GetCommandInfo (interp, parent_name, parent_info))
+    {
+        interp->result = "No parent";
         return TCL_ERROR;
+    }
     return TCL_OK;
 }
 
@@ -400,6 +416,42 @@ static int do_callback (void *obj, Tcl_Interp *interp,
     return TCL_OK;
 }
 
+/*
+ * do_databaseNames: specify database names
+ */
+static int do_databaseNames (void *obj, Tcl_Interp *interp,
+                          int argc, char **argv)
+{
+    int i;
+    IRObj *p = obj;
+
+    if (argc < 3)
+    {
+        interp->result = "wrong # args";
+        return TCL_ERROR;
+    }
+    if (p->databaseNames)
+    {
+        for (i=0; i<p->num_databaseNames; i++)
+            free (p->databaseNames[i]);
+        free (p->databaseNames);
+    }
+    p->num_databaseNames = argc - 2;
+    if (!(p->databaseNames = malloc (sizeof(*p->databaseNames) *
+                               p->num_databaseNames)))
+    {
+        interp->result = "malloc fail";
+        return TCL_ERROR;
+    }
+    for (i=0; i<p->num_databaseNames; i++)
+    {
+        if (ir_strdup (interp, &p->databaseNames[i], argv[2+i]) 
+            == TCL_ERROR)
+            return TCL_ERROR;
+    }
+    return TCL_OK;
+}
+
 /* 
  * ir_obj_method: IR Object methods
  */
@@ -419,6 +471,7 @@ static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
     { "init", do_init_request },
     { "disconnect", do_disconnect },
     { "callback", do_callback },
+    { "databaseNames", do_databaseNames},
     { NULL, NULL}
     };
     if (argc < 2)
@@ -458,7 +511,7 @@ static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
     }
     obj->cs_link = cs_create (tcpip_type);
 
-    obj->maximumMessageSize = 10000;
+    obj->maximumMessageSize = 9000;
     obj->preferredMessageSize = 4096;
 
     obj->idAuthentication = NULL;
@@ -470,6 +523,13 @@ static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
     if (ir_strdup (interp, &obj->implementationId, "TCL/TK/YAZ")
         == TCL_ERROR)
         return TCL_ERROR;
+    
+    obj->smallSetUpperBound = 0;
+    obj->largeSetLowerBound = 2;
+    obj->mediumSetPresentNumber = 0;
+    obj->replaceIndicator = 1;
+    obj->databaseNames = NULL;
+    obj->num_databaseNames = 0; 
 
     ODR_MASK_ZERO (&obj->protocolVersion);
     ODR_MASK_SET (&obj->protocolVersion, 0);
@@ -503,6 +563,70 @@ static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
 
 /* ------------------------------------------------------- */
 /*
+ * do_search: Do search request
+ */
+static int do_search (void *o, Tcl_Interp *interp,
+                      int argc, char **argv)
+{
+    Z_SearchRequest req;
+    Z_Query query;
+    Z_APDU apdu, *apdup;
+    static Odr_oid bib1[] = {1, 2, 840, 10003, 3, 1, -1};
+    Odr_oct ccl_query;
+    IRSetObj *obj = o;
+    IRObj *p = obj->parent;
+    char *sbuf;
+    int slen;
+
+    p->child = o;
+    if (argc != 3)
+    {
+        interp->result = "wrong # args";
+        return TCL_ERROR;
+    }
+    if (!p->num_databaseNames)
+    {
+        interp->result = "no databaseNames";
+        return TCL_ERROR;
+    }
+    apdu.which = Z_APDU_searchRequest;
+    apdu.u.searchRequest = &req;
+    apdup = &apdu;
+
+    req.referenceId = 0;
+    req.smallSetUpperBound = &p->smallSetUpperBound;
+    req.largeSetLowerBound = &p->largeSetLowerBound;
+    req.mediumSetPresentNumber = &p->mediumSetPresentNumber;
+    req.replaceIndicator = &p->replaceIndicator;
+    req.resultSetName = "Default";
+    req.num_databaseNames = p->num_databaseNames;
+    req.databaseNames = p->databaseNames;
+    req.smallSetElementSetNames = 0;
+    req.mediumSetElementSetNames = 0;
+    req.preferredRecordSyntax = 0;
+    req.query = &query;
+
+    query.which = Z_Query_type_2;
+    query.u.type_2 = &ccl_query;
+    ccl_query.buf = argv[2];
+    ccl_query.len = strlen (argv[2]);
+
+    if (!z_APDU (p->odr_out, &apdup, 0))
+    {
+        interp->result = odr_errlist [odr_geterror (p->odr_out)];
+        odr_reset (p->odr_out);
+        return TCL_ERROR;
+    } 
+    sbuf = odr_getbuf (p->odr_out, &slen);
+    if (cs_put (p->cs_link, sbuf, slen) < 0)
+    {
+        interp->result = "cs_put failed in init";
+        return TCL_ERROR;
+    }
+    return TCL_OK;
+}
+
+/*
  * do_query: Set query for a Set Object
  */
 static int do_query (void *obj, Tcl_Interp *interp,
@@ -511,6 +635,75 @@ static int do_query (void *obj, Tcl_Interp *interp,
     return TCL_OK;
 }
 
+/*
+ * do_resultCount: Get number of hits
+ */
+static int do_resultCount (void *o, Tcl_Interp *interp,
+                      int argc, char **argv)
+{
+    IRSetObj *obj = o;
+
+    sprintf (interp->result, "%d", obj->resultCount);
+    return TCL_OK;
+}
+
+
+/*
+ * do_present: Perform present Request
+ */
+
+static int do_present (void *o, Tcl_Interp *interp,
+                       int argc, char **argv)
+{
+    IRSetObj *obj = o;
+    IRObj *p = obj->parent;
+    Z_APDU apdu, *apdup;
+    Z_PresentRequest req;
+    int start;
+    int number;
+    char *sbuf;
+    int slen;
+
+    if (argc >= 3)
+    {
+        if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
+            return TCL_ERROR;
+    }
+    else
+        start = 1;
+    if (argc >= 4)
+    {
+        if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
+            return TCL_ERROR;
+    }
+    else 
+        number = 10;
+    apdup = &apdu;
+    apdu.which = Z_APDU_presentRequest;
+    apdu.u.presentRequest = &req;
+    req.referenceId = 0;
+    /* sprintf(setstring, "%d", setnumber); */
+    req.resultSetId = "Default";
+    req.resultSetStartPoint = &start;
+    req.numberOfRecordsRequested = &number;
+    req.elementSetNames = 0;
+    req.preferredRecordSyntax = 0;
+
+    if (!z_APDU (p->odr_out, &apdup, 0))
+    {
+        interp->result = odr_errlist [odr_geterror (p->odr_out)];
+        odr_reset (p->odr_out);
+        return TCL_ERROR;
+    } 
+    sbuf = odr_getbuf (p->odr_out, &slen);
+    if (cs_put (p->cs_link, sbuf, slen) < 0)
+    {
+        interp->result = "cs_put failed in init";
+        return TCL_ERROR;
+    }
+    printf ("Present request\n");
+    return TCL_OK;
+}
 
 /* 
  * ir_set_obj_method: IR Set Object methods
@@ -520,6 +713,9 @@ static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
 {
     static IRMethod tab[] = {
     { "query", do_query },
+    { "search", do_search },
+    { "resultCount", do_resultCount },
+    { "present", do_present },
     { NULL, NULL}
     };
 
@@ -554,10 +750,7 @@ static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
         return TCL_ERROR;
     }
     if (get_parent_info (interp, argv[1], &parent_info) == TCL_ERROR)
-    {
-        interp->result = "No parent";
         return TCL_ERROR;
-    }
     obj = malloc (sizeof(*obj));
     if (!obj)
     {
@@ -572,14 +765,18 @@ static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
 
 /* ------------------------------------------------------- */
 
-static void ir_searchResponse (void *obj, Z_SearchResponse *searchrs)
+static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
 {    
+    IRObj *p = o;
+    IRSetObj *obj = p->child;
+
+    if (obj)
+        obj->resultCount = *searchrs->resultCount;
     if (searchrs->searchStatus)
         printf("Search was a success.\n");
     else
-            printf("Search was a bloomin' failure.\n");
-    printf("Number of hits: %d, setno %d\n",
-           *searchrs->resultCount, 1);
+        printf("Search was a bloomin' failure.\n");
+    printf("Number of hits: %d\n", *searchrs->resultCount);
 #if 0
     if (searchrs->records)
         display_records(searchrs->records);
@@ -588,6 +785,8 @@ static void ir_searchResponse (void *obj, Z_SearchResponse *searchrs)
 
 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
 {
+    IRObj *p = obj;
+
     if (!*initrs->result)
         printf("Connection rejected by target.\n");
     else
@@ -608,8 +807,11 @@ static void ir_initResponse (void *obj, Z_InitResponse *initrs)
 #endif
 }
 
-static void ir_presentResponse (void *obj, Z_PresentResponse *presrs)
+static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
 {
+    IRObj *p = o;
+    IRSetObj *obj = p->child;
+
     printf("Received presentResponse.\n");
     if (presrs->records)
         printf ("Got records\n");
@@ -628,6 +830,7 @@ void ir_select_proc (ClientData clientData)
         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);
             return;
         }        
         odr_setbuf (p->odr_in, p->buf_in, r);
@@ -637,25 +840,23 @@ void ir_select_proc (ClientData clientData)
             printf ("%s\n", odr_errlist [odr_geterror (p->odr_in)]);
             return;
         }
-        if (p->callback)
-        {
-           Tcl_Eval (p->interp, p->callback);
-       }
         switch(apdu->which)
         {
         case Z_APDU_initResponse:
-            ir_initResponse (NULL, apdu->u.initResponse);
+            ir_initResponse (p, apdu->u.initResponse);
             break;
         case Z_APDU_searchResponse:
-            ir_searchResponse (NULL, apdu->u.searchResponse);
+            ir_searchResponse (p, apdu->u.searchResponse);
             break;
         case Z_APDU_presentResponse:
-            ir_presentResponse (NULL, apdu->u.presentResponse);
+            ir_presentResponse (p, apdu->u.presentResponse);
             break;
         default:
             printf("Received unknown APDU type (%d).\n", 
                    apdu->which);
         }
+        if (p->callback)
+           Tcl_Eval (p->interp, p->callback);
     } while (cs_more (p->cs_link));    
 }
 
@@ -672,5 +873,3 @@ int ir_tcl_init (Tcl_Interp *interp)
                       (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
     return TCL_OK;
 }
-
-