Towards 1.4.
[ir-tcl-moved-to-github.git] / ir-tcl.c
index 66eebf3..aa3fcec 100644 (file)
--- a/ir-tcl.c
+++ b/ir-tcl.c
@@ -1,11 +1,25 @@
 /*
  * IR toolkit for tcl/tk
- * (c) Index Data 1995-1999
+ * (c) Index Data 1995-2001
  * See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: ir-tcl.c,v $
- * Revision 1.114  1999-05-17 20:37:41  adam
+ * Revision 1.119  2001-12-03 00:31:06  adam
+ * Towards 1.4. Configure updates.
+ *
+ * Revision 1.118  2001/03/27 16:27:21  adam
+ * Fixed bug in do_responseStatus.
+ *
+ * Revision 1.117  2001/03/26 11:39:34  adam
+ * Fixed bug in ir_deleteDiags - crash when receiving multiple diags.
+ *
+ * Revision 1.116  2001/02/09 11:58:04  adam
+ * Updated for Tcl8.1 and higher where internal encoding is UTF-8.
+ *
+ * Revision 1.115  2000/09/13 12:18:49  adam
+ * Logging utility patch (YAZ version 1.7).
+ *
+ * Revision 1.114  1999/05/17 20:37:41  adam
  * Fixed problem with ASN code.
  *
  * Revision 1.113  1999/04/20 10:01:46  adam
@@ -1027,7 +1041,7 @@ static int do_implementationName (void *obj, Tcl_Interp *interp,
 
     if (argc == 0)
         return ir_tcl_strdup (interp, &p->implementationName,
-                          "Index Data/IrTcl on YAZ");
+                          "IrTcl/YAZ");
     else if (argc == -1)
         return ir_tcl_strdel (interp, &p->implementationName);
     if (argc == 3)
@@ -1050,7 +1064,7 @@ static int do_implementationId (void *obj, Tcl_Interp *interp,
     IrTcl_Obj *p = obj;
 
     if (argc == 0)
-        return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
+        return ir_tcl_strdup (interp, &p->implementationId, "81");
     else if (argc == -1)
         return ir_tcl_strdel (interp, &p->implementationId);
     Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
@@ -1067,10 +1081,10 @@ static int do_implementationVersion (void *obj, Tcl_Interp *interp,
 
     if (argc == 0)
         return ir_tcl_strdup (interp, &p->implementationVersion, 
-                          "YAZ: " YAZ_VERSION
 #ifdef IR_TCL_VERSION
-                          " / Irtcl: " IR_TCL_VERSION
+                          IR_TCL_VERSION "/"
 #endif
+                          YAZ_VERSION
                           );
     else if (argc == -1)
         return ir_tcl_strdel (interp, &p->implementationVersion);
@@ -1367,11 +1381,11 @@ static int do_logLevel (void *o, Tcl_Interp *interp,
     if (argc <= 2)
         return TCL_OK;
     if (argc == 3)
-        log_init (log_mask_str (argv[2]), "", NULL);
+        yaz_log_init (yaz_log_mask_str (argv[2]), "", NULL);
     else if (argc == 4)
-        log_init (log_mask_str (argv[2]), argv[3], NULL);
+        yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], NULL);
     else if (argc == 5)
-        log_init (log_mask_str (argv[2]), argv[3], argv[4]);
+        yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], argv[4]);
     return TCL_OK;
 }
 
@@ -2030,7 +2044,11 @@ static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
     Odr_oct ccl_query;
     IrTcl_SetObj *obj = o;
     IrTcl_Obj *p;
-    int r;
+    int r, code;
+#if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
+    Tcl_DString ds;
+#endif
+    char *query_str;
 
     if (argc <= 0)
         return TCL_OK;
@@ -2043,16 +2061,23 @@ static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
                           NULL);
         return TCL_ERROR;
     }
-    logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
+#if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
+    query_str = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
+#else
+    query_str = argv[2];
+#endif
+    logf (LOG_DEBUG, "search %s %s", *argv, query_str);
     if (!obj->set_inher.num_databaseNames)
     {
         Tcl_AppendResult (interp, "no databaseNames", NULL);
-        return ir_tcl_error_exec (interp, argc, argv);
+        code = ir_tcl_error_exec (interp, argc, argv);
+       goto out;
     }
     if (!p->cs_link)
     {
         Tcl_AppendResult (interp, "not connected", NULL);
-        return ir_tcl_error_exec (interp, argc, argv);
+        code = ir_tcl_error_exec (interp, argc, argv);
+       goto out;
     }
     apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
     req = apdu->u.searchRequest;
@@ -2117,11 +2142,12 @@ static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
     {
         Z_RPNQuery *RPNquery;
 
-        RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
+        RPNquery = p_query_rpn (p->odr_out, p->protocol_type, query_str);
         if (!RPNquery)
         {
             Tcl_AppendResult (interp, "query syntax error", NULL);
-            return ir_tcl_error_exec (interp, argc, argv);
+            code = ir_tcl_error_exec (interp, argc, argv);
+           goto out;
         }
         query.which = Z_Query_type_1;
         query.u.type_1 = RPNquery;
@@ -2139,18 +2165,19 @@ static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
         bib1.oclass = CLASS_ATTSET;
         bib1.value = VAL_BIB1;
 
-        rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
+        rpn = ccl_find_str(p->bibset, query_str, &error, &pos);
         if (error)
         {
             Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
                               NULL);
-            return ir_tcl_error_exec (interp, argc, argv);
+            code = ir_tcl_error_exec (interp, argc, argv);
+           goto out;
         }
 #if 0
         ccl_pr_tree (rpn, stderr);
         fprintf (stderr, "\n");
 #endif
-        assert((RPNquery = ccl_rpn_query(rpn)));
+        RPNquery = ccl_rpn_query(p->odr_out, rpn);
         RPNquery->attributeSetId = oid_getoidbyent (&bib1);
         query.which = Z_Query_type_1;
         query.u.type_1 = RPNquery;
@@ -2160,16 +2187,22 @@ static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
     {
         query.which = Z_Query_type_2;
         query.u.type_2 = &ccl_query;
-        ccl_query.buf = (unsigned char *) argv[2];
-        ccl_query.len = strlen (argv[2]);
+        ccl_query.buf = (unsigned char *) query_str;
+        ccl_query.len = strlen (query_str);
     }
     else
     {
         Tcl_AppendResult (interp, "invalid query method ",
                           obj->set_inher.queryType, NULL);
-        return ir_tcl_error_exec (interp, argc, argv);
+        code = ir_tcl_error_exec (interp, argc, argv);
+       goto out;
     }
-    return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
+    code = ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
+ out:
+#if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
+    Tcl_DStringFree (&ds);
+#endif
+    return code;
 }
 
 /*
@@ -2759,6 +2792,10 @@ static int do_responseStatus (void *o, Tcl_Interp *interp,
         Tcl_AppendElement (interp, "NSD");
         return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
                               obj->nonSurrogateDiagnosticNum);
+    case Z_Records_multipleNSD:
+        Tcl_AppendElement (interp, "NSD");
+        return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
+                               obj->nonSurrogateDiagnosticNum);
     }
     return TCL_OK;
 }
@@ -3377,10 +3414,16 @@ 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;
+    char *start_term;
+    int code;
 #if CCL2RPN
     oident bib1;
     struct ccl_rpn_node *rpn;
     int pos;
+    int r;
+#endif
+#if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
+    Tcl_DString ds;
 #endif
 
     if (argc <= 0)
@@ -3391,16 +3434,23 @@ static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
                           " scanQuery\"", NULL);
         return TCL_ERROR;
     }
-    logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
+#if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
+    start_term = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
+#else
+    start_term = argv[2];
+#endif
+    logf (LOG_DEBUG, "scan %s %s", *argv, start_term);
     if (!p->set_inher.num_databaseNames)
     {
         Tcl_AppendResult (interp, "no databaseNames", NULL);
-        return ir_tcl_error_exec (interp, argc, argv);
+        code = ir_tcl_error_exec (interp, argc, argv);
+       goto out;
     }
     if (!p->cs_link)
     {
         Tcl_AppendResult (interp, "not connected", NULL);
-        return ir_tcl_error_exec (interp, argc, argv);
+        code = ir_tcl_error_exec (interp, argc, argv);
+       goto out;
     }
 
     apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
@@ -3410,28 +3460,34 @@ static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
     req->num_databaseNames = p->set_inher.num_databaseNames;
     req->databaseNames = p->set_inher.databaseNames;
 
-#if !CCL2RPN
+#if 1 
+/* !CCL2RPN */
     if (!(req->termListAndStartPoint =
           p_query_scan (p->odr_out, p->protocol_type,
-                        &req->attributeSet, argv[2])))
+                        &req->attributeSet, start_term)))
     {
         Tcl_AppendResult (interp, "query syntax error", NULL);
-        return ir_tcl_error_exec (interp, argc, argv);
+       code = ir_tcl_error_exec (interp, argc, argv);
+       goto out;
     }
 #else
-    rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
+    rpn = ccl_find_str(p->bibset, start_term, &r, &pos);
     if (r)
     {
         Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
-        return ir_tcl_error_exec (interp, argc, argv);
+        code = ir_tcl_error_exec (interp, argc, argv);
+       goto out;
     }
     bib1.proto = p->protocol_type;
     bib1.oclass = CLASS_ATTSET;
     bib1.value = VAL_BIB1;
 
     req->attributeSet = oid_getoidbyent (&bib1);
-    if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
-        return TCL_ERROR;
+    if (!(req->termListAndStartPoint = ccl_scan_query (p->odr_out, rpn)))
+    {
+        code = TCL_ERROR;
+       goto out;
+    }
 #endif
     req->stepSize = &obj->stepSize;
     req->numberOfTermsRequested = &obj->numberOfTermsRequested;
@@ -3442,7 +3498,12 @@ static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
     logf (LOG_DEBUG, "preferredPositionInResponse=%d",
           *req->preferredPositionInResponse);
     
-    return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
+    code = ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
+ out:
+#if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
+    Tcl_DStringFree (&ds);
+#endif
+    return code;
 }
 
 /*
@@ -3724,11 +3785,11 @@ static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
         return TCL_OK;
     }
     if (argc == 2)
-        log_init (log_mask_str (argv[1]), "", NULL);
+        yaz_log_init (yaz_log_mask_str (argv[1]), "", NULL);
     else if (argc == 3)
-        log_init (log_mask_str (argv[1]), argv[2], NULL);
+        yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], NULL);
     else 
-        log_init (log_mask_str (argv[1]), argv[2], argv[3]);
+        yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], argv[3]);
     return TCL_OK;
 }
 
@@ -3745,7 +3806,7 @@ static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
                           " level string\"", NULL);
         return TCL_OK;
     }
-    mask = log_mask_str_x (argv[1], 0);
+    mask = yaz_log_mask_str_x (argv[1], 0);
     logf (LOG_DEBUG, "%s", argv[2]);
     return TCL_OK;
 }
@@ -3816,7 +3877,7 @@ static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
 {
     int i;
     for (i = 0; i<*dst_num; i++)
-        xfree (dst_list[i]->addinfo);
+        xfree ((*dst_list)[i].addinfo);
     xfree (*dst_list);
     *dst_list = NULL;
     *dst_num = 0;