Scan request changed a bit. This version works with RLG.
[ir-tcl-moved-to-github.git] / ir-tcl.c
index b027eb1..21e90a9 100644 (file)
--- a/ir-tcl.c
+++ b/ir-tcl.c
@@ -4,7 +4,16 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: ir-tcl.c,v $
- * Revision 1.24  1995-04-11 14:16:42  adam
+ * Revision 1.27  1995-05-11 15:34:47  adam
+ * Scan request changed a bit. This version works with RLG.
+ *
+ * Revision 1.26  1995/04/18  16:11:51  adam
+ * First version of graphical Scan. Some work on query-by-form.
+ *
+ * Revision 1.25  1995/04/17  09:37:17  adam
+ * Further development of scan.
+ *
+ * Revision 1.24  1995/04/11  14:16:42  adam
  * Further work on scan. Response works. Entries aren't saved yet.
  *
  * Revision 1.23  1995/04/10  10:50:27  adam
@@ -82,6 +91,8 @@
 #include <sys/time.h>
 #include <assert.h>
 
+#include <tcl.h>
+
 #include <yaz-ccl.h>
 #include <iso2709.h>
 #include <comstack.h>
 #include <oid.h>
 #include <diagbib1.h>
 
-#include <tcl.h>
-
 #include "ir-tcl.h"
 
 #define CS_BLOCK 0
@@ -180,6 +189,24 @@ typedef struct IRSetObj_ {
     IRRecordList *record_list;
 } IRSetObj;
 
+typedef struct IRScanEntry_ {
+    int         which;
+    union {
+        struct {
+           char *buf;
+           int  globalOccurrences;
+       } term;
+       struct {
+           int  condition;
+           char *addinfo;
+       } diag;
+    } u;
+} IRScanEntry;
+
+typedef struct IRScanDiag_ {
+    int         dummy;
+} IRScanDiag;
+
 typedef struct IRScanObj_ {
     IRObj      *parent;
     int         stepSize;
@@ -192,6 +219,12 @@ typedef struct IRScanObj_ {
 
     int         entries_flag;
     int         which;
+
+    int         num_entries;
+    int         num_diagRecs;
+
+    IRScanEntry *entries;
+    IRScanDiag  *nonSurrogateDiagnostics;
 } IRScanObj;
 
 typedef struct {
@@ -263,6 +296,31 @@ static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
 }
 
 /*
+ * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
+ */
+static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp, 
+                                       int condition,
+                                      const char *addinfo)
+{
+    char buf[20];
+    const char *cp;
+
+    Tcl_AppendElement (interp, "NSD");
+    sprintf (buf, "%d", condition);
+    Tcl_AppendElement (interp, buf);
+    cp = diagbib1_str (condition);
+    if (cp)
+        Tcl_AppendElement (interp, (char*) cp);
+    else
+        Tcl_AppendElement (interp, "");
+    if (addinfo)
+        Tcl_AppendElement (interp, (char*) addinfo);
+    else
+        Tcl_AppendElement (interp, "");
+    return TCL_OK;
+}
+
+/*
  * get_parent_info: Returns information about parent object.
  */
 static int get_parent_info (Tcl_Interp *interp, const char *name,
@@ -968,6 +1026,8 @@ static int do_search (void *o, Tcl_Interp *interp,
             Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg(error),NULL);
             return TCL_ERROR;
         }
+        ccl_pr_tree (rpn, stderr);
+        fprintf (stderr, "\n");
         query.which = Z_Query_type_1;
         assert((RPNquery = ccl_rpn_query(rpn)));
         RPNquery->attributeSetId = oid_getoidbyent (&p->bib1);
@@ -1230,6 +1290,7 @@ static int do_recordMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
     }
 }
 
+
 /*
  * do_responseStatus: Return response status (present or search)
  */
@@ -1237,8 +1298,6 @@ 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;
@@ -1248,19 +1307,8 @@ static int do_responseStatus (void *o, Tcl_Interp *interp,
        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 mk_nonSurrogateDiagnostics (interp, obj->condition, 
+                                          obj->addinfo);
     }
     return TCL_OK;
 }
@@ -1457,7 +1505,8 @@ static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
     Z_APDU apdu, *apdup = &apdu;
     IRScanObj *obj = o;
     IRObj *p = obj->parent;
-    int r;
+    int r, pos;
+    struct ccl_rpn_node *rpn;
 
     p->scan_child = o;
     if (argc != 3)
@@ -1482,6 +1531,7 @@ static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
     req.databaseNames = p->databaseNames;
     req.attributeSet = oid_getoidbyent (&p->bib1);
 
+#if 0
     if (!(req.termListAndStartPoint =
           ir_malloc (interp, sizeof(*req.termListAndStartPoint))))
         return TCL_ERROR;
@@ -1500,6 +1550,18 @@ static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
         return TCL_ERROR;
     req.termListAndStartPoint->term->u.general->len = 
         req.termListAndStartPoint->term->u.general->size = strlen(argv[2]);
+#else
+    rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
+    if (r)
+    {
+        Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
+        return TCL_ERROR;
+    }
+    ccl_pr_tree (rpn, stderr);
+    fprintf (stderr, "\n");
+    if (!(req.termListAndStartPoint = ccl_scan_query (rpn)))
+        return TCL_ERROR;
+#endif
     req.stepSize = &obj->stepSize;
     req.numberOfTermsRequested = &obj->numberOfTermsRequested;
     req.preferredPositionInResponse = &obj->preferredPositionInResponse;
@@ -1593,6 +1655,45 @@ static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
     return get_set_int (&p->positionOfTerm, interp, argc, argv);
 }
 
+/*
+ * do_scanLine: get Scan Line (surrogate or normal) after response
+ */
+static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
+{
+    IRScanObj *p = obj;
+    int i;
+    char numstr[20];
+
+    if (argc != 3)
+    {
+        interp->result = "wrong # args";
+       return TCL_ERROR;
+    }
+    if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
+        return TCL_ERROR;
+    if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
+        || i >= p->num_entries || i < 0)
+        return TCL_OK;
+    switch (p->entries[i].which)
+    {
+    case Z_Entry_termInfo:
+        Tcl_AppendElement (interp, "T");
+       if (p->entries[i].u.term.buf)
+           Tcl_AppendElement (interp, p->entries[i].u.term.buf);
+       else
+           Tcl_AppendElement (interp, "");
+       sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
+       Tcl_AppendElement (interp, numstr);
+       break;
+    case Z_Entry_surrogateDiagnostic:
+        return 
+           mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
+                                       p->entries[i].u.diag.addinfo);
+       break;
+    }
+    return TCL_OK;
+}
+
 /* 
  * ir_scan_obj_method: IR Scan Object methods
  */
@@ -1607,6 +1708,7 @@ static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
     { 0, "scanStatus",              do_scanStatus },
     { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
     { 0, "positionOfTerm",          do_positionOfTerm },
+    { 0, "scanLine",                do_scanLine },
     { 0, NULL, NULL}
     };
 
@@ -1649,6 +1751,9 @@ static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
     obj->numberOfTermsRequested = 20;
     obj->preferredPositionInResponse = 1;
 
+    obj->entries = NULL;
+    obj->nonSurrogateDiagnostics = NULL;
+
     obj->parent = (IRObj *) parent_info.clientData;
     Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
                        (ClientData) obj, ir_scan_obj_delete);
@@ -1812,11 +1917,68 @@ static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
     else
         scanobj->positionOfTerm = -1;
     printf ("positionOfTerm=%d\n", scanobj->positionOfTerm);
-    
+
+    free (scanobj->entries);
+    scanobj->entries = NULL;
+    free (scanobj->nonSurrogateDiagnostics);
+    scanobj->nonSurrogateDiagnostics = NULL;
+
     if (scanrs->entries)
     {
+        int i;
+       Z_Entry *ze;
+
         scanobj->entries_flag = 1;
         scanobj->which = scanrs->entries->which;
+       switch (scanobj->which)
+       {
+       case Z_ListEntries_entries:
+           scanobj->num_entries = scanrs->entries->u.entries->num_entries;
+           scanobj->entries = malloc (scanobj->num_entries * 
+                                      sizeof(*scanobj->entries));
+            for (i=0; i<scanobj->num_entries; i++)
+           {
+               ze = scanrs->entries->u.entries->entries[i];
+                scanobj->entries[i].which = ze->which;
+               switch (ze->which)
+               {
+               case Z_Entry_termInfo:
+                   if (ze->u.termInfo->term->which == Z_Term_general)
+                   {
+                        int l = ze->u.termInfo->term->u.general->len;
+                        scanobj->entries[i].u.term.buf = malloc (1+l);
+                       memcpy (scanobj->entries[i].u.term.buf, 
+                               ze->u.termInfo->term->u.general->buf,
+                                l);
+                        scanobj->entries[i].u.term.buf[l] = '\0';
+                   }
+                   else
+                        scanobj->entries[i].u.term.buf = NULL;
+                   if (ze->u.termInfo->globalOccurrences)
+                       scanobj->entries[i].u.term.globalOccurrences = 
+                           *ze->u.termInfo->globalOccurrences;
+                   else
+                       scanobj->entries[i].u.term.globalOccurrences = 0;
+                    break;
+               case Z_Entry_surrogateDiagnostic:
+                   scanobj->entries[i].u.diag.addinfo = 
+                           malloc (1+strlen(ze->u.surrogateDiagnostic->
+                                            addinfo));
+                    strcpy (scanobj->entries[i].u.diag.addinfo,
+                           ze->u.surrogateDiagnostic->addinfo);
+                   scanobj->entries[i].u.diag.condition = 
+                       *ze->u.surrogateDiagnostic->condition;
+                   break;
+               }
+           }
+            break;
+       case Z_ListEntries_nonSurrogateDiagnostics:
+           scanobj->num_diagRecs = scanrs->entries->
+                                 u.nonSurrogateDiagnostics->num_diagRecs;
+           scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
+                                 sizeof(*scanobj->nonSurrogateDiagnostics));
+            break;
+       }
     }
     else
         scanobj->entries_flag = 0;