Bug fix: shell might terminate even though new request was initiated
[egate.git] / www / z39util.tcl
index bfbe03b..fcd6bdb 100644 (file)
@@ -1,5 +1,5 @@
 #
-# $Id: z39util.tcl,v 1.8 1995/11/13 15:41:46 adam Exp $
+# $Id: z39util.tcl,v 1.12 1995/12/20 16:31:34 adam Exp $
 #
 proc saveState {} {
     uplevel #0 {
@@ -26,16 +26,17 @@ proc saveState {} {
     }
 }
 
-proc search-response {sno} {
+proc search-response {zz} {
     global sessionWait
 
-    set status [z39.$sno responseStatus]
+    set status [$zz responseStatus]
     if {[lindex $status 0] == "NSD"} {
-        z39.$sno nextResultSetPosition 0
+        $zz nextResultSetPosition 0
         set code [lindex $status 1]
         set msg [lindex $status 2]
         set addinfo [lindex $status 3]
-        html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
+        displayError "Diagnostic message" \
+                "$msg: $addinfo<br>\n(error code $code)"
         set sessionWait -2
     } else {
         set sessionWait 1
@@ -117,7 +118,7 @@ proc display-raw {zset no tno} {
         set indicator [lindex $line 1]
         set fields [lindex $line 2]
         set l [string length $indicator]
-        html "$tag "
+        html "<tt>$tag "
         if {$l > 0} {
             for {set i 0} {$i < $l} {incr i} {
                 if {[string index $indicator $i] == " "} {
@@ -127,6 +128,7 @@ proc display-raw {zset no tno} {
                 }
             }
         }
+        html "</tt>"
         foreach field $fields {
             set id [lindex $field 0]
             set data [lindex $field 1]
@@ -154,7 +156,7 @@ proc put-marc-contents {cc} {
     }
     html $cc
     if {$ref != ""} {
-        html {">} $urltype { reference</a>}
+        html {">} $cc {</a>}
     }
 }
 
@@ -363,13 +365,13 @@ proc build-query {t} {
             }
             switch $op {
             And
-                { set q "@and $q ${attr} \{${term}\}" }
+                { set q "@and $q ${attr} ${term}" }
             Or
-                { set q "@or $q ${attr} \{${term}\}" }
+                { set q "@or $q ${attr} ${term}" }
             {And not}
-                { set q "@not $q ${attr} \{${term}\}" }
+                { set q "@not $q ${attr} ${term}" }
             {}
-                { set q "${attr} \{${term}\}" }
+                { set q "${attr} ${term}" }
             }
             set op [wform logic$i]
         }
@@ -380,6 +382,7 @@ proc build-query {t} {
 proc z39search {setNo piggy tno elements} {
     global hist
     global sessionWait
+    global targets
 
     if {$tno > 0} {
         set zz z39$tno
@@ -407,51 +410,62 @@ proc z39search {setNo piggy tno elements} {
 
         set sessionWait 0
         if {[catch [list $zz connect $host]]} {
-            html "Cannot connect to target ${host} <br>\n"
+            displayError "Cannot connect to target" $host
             return 0
         } elseif {$sessionWait == 0} {
-            zwait sessionWait
+            if {[catch {zwait sessionWait 300}]} {
+               $zz disconnect
+                displayError "Cannot connect to target" $host
+                return 0
+            }
             if {$sessionWait != 1} {
-                html "Cannot connect to target ${host} <br>\n"
+                displayError "Cannot connect to target" $host
                 return 0
             }
         }
         $zz idAuthentication $idAuth
         set sessionWait 0
-        if {[catch [list $zz init]]} {
-            html "Cannot initialize with target ${host} <br>\n"
+        if {[catch {$zz init}]} {
+            displayError "Cannot initialize target" $host
+           $zz disconnect
             return 0
         }
         if {[catch {zwait sessionWait 60}]} {
-            html "Cannot initialize with target ${host} <br>\n"
+            displayError "Cannot initialize target" $host
            $zz disconnect
             return 0
         }
         if {$sessionWait != "1"} {
-            html "Cannot initialize with target ${host} <br>\n"
+            displayError "Cannot initialize target" $host
            $zz disconnect
             return 0
         }
         if {![$zz initResult]} {
             set u [$zz userInformationField]
             $zz disconnect
-            html "Connection rejected by target: $u <br>\n"
+            displayError "Cannot initialize target $host" $u
             return 0
         }
-    }
-    if {![catch [list $zz.$setNo smallSetUpperBound 0]]} {
-        return 1
+    } else {
+        if {![catch [list $zz.$setNo smallSetUpperBound 0]]} {
+            return 1
+        }
     }
     ir-set $zz.$setNo $zz
+    
+    if {![lindex $targets($host) 5]} {
+        set elements {}
+    }
     $zz.$setNo smallSetElementSetNames $elements
     $zz.$setNo mediumSetElementSetNames $elements
     $zz.$setNo recordElements $elements
-    eval $zz.$setNo databaseNames $database
 
+    wlog debug "database=$database"
+    eval $zz.$setNo databaseNames $database
 
     $zz.$setNo preferredRecordSyntax USMARC
 
-    $zz callback search-response $setNo
+    $zz callback [list search-response $zz.$setNo]
     if {$piggy} {
         $zz.$setNo largeSetLowerBound 999999
         $zz.$setNo smallSetUpperBound 0
@@ -465,23 +479,20 @@ proc z39search {setNo piggy tno elements} {
     $zz.$setNo search $query
 
     if {[catch {zwait sessionWait 600}]} {
+        wlog debug "timeout/cancel in search"
+        displayError "Timeout in search" {}
         html "</body></html>\n"
         $zz disconnect
         return 0
     }
         
-    if {$sessionWait != 1} {
+    if {$sessionWait == -1} {
+        displayError "Search fail" "Connection closed"
         html "</body></html>\n"
         $zz disconnect
-        return 0
     }
-    set status [$zz.$setNo responseStatus]
-    if {[lindex $status 0] == "NSD"} {
-        set code [lindex $status 1]
-        set msg [lindex $status 2]
-        set addinfo [lindex $status 3]
-        html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
-        return 0
+    if {$sessionWait != 1} {
+       return 0
     }
     set hist($setNo,hits) [$zz.$setNo resultCount]
     return 1
@@ -530,6 +541,7 @@ proc z39msearch {setNo piggy elements} {
     global zleft
     global zstatus
     global hist
+    global targets
 
     set not $hist($setNo,0,host)
 
@@ -577,9 +589,14 @@ proc z39msearch {setNo piggy elements} {
             set hist($setNo,$i,offset) 0
             eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
 
-            z39$i.$setNo smallSetElementSetNames $elements
-            z39$i.$setNo mediumSetElementSetNames $elements
-            z39$i.$setNo recordElements $elements
+            if {![lindex $targets($hist($setNo,$i,host)) 5]} {
+                set thisElements {}
+            } else {
+                set thisElements $elements
+            }
+            z39$i.$setNo smallSetElementSetNames $thisElements
+            z39$i.$setNo mediumSetElementSetNames $thisElements
+            z39$i.$setNo recordElements $thisElements
 
             z39$i.$setNo preferredRecordSyntax USMARC
             z39$i callback [list search-m-response $setNo $i]
@@ -619,16 +636,26 @@ proc z39msearch {setNo piggy elements} {
 proc z39present {setNo tno setOffset setMax dfunc elements} {
     global hist
     global sessionWait
+    global targets
 
     if {$tno > 0} {
         set zz z39$tno
+        set host $hist($setNo,$tno,host)
     } else {
         set zz z39
+        set host $hist($setNo,host)
+    }
+
+    if {![lindex $targets($host) 5]} {
+        set elements {}
     }
 
     $zz.$setNo elementSetNames $elements
     $zz.$setNo recordElements $elements
     set toGet [expr 1 + $setMax - $setOffset]
+
+    $zz callback [list search-response $zz.$setNo]
+
     while {$setMax > 0 && $toGet > 0} {
         for {set got 0} {$got < $toGet} {incr got} {
             if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
@@ -639,9 +666,13 @@ proc z39present {setNo tno setOffset setMax dfunc elements} {
             set sessionWait 0
             $zz.$setNo present $setOffset $toGet
             if {[catch {zwait sessionWait 300}]} {
+                wlog debug "timeout/cancel in present"
                $zz disconnect
                 break
            }
+            if {$sessionWait == "0"} {
+                $zz disconnect
+            }
             if {$sessionWait != "1"} {
                 break
             }
@@ -671,7 +702,7 @@ proc z39history {} {
     for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
         html {<dt> <a href="http:} $env(SCRIPT_NAME)
         html / $sessionId {/search.egw/} $setNo + 1
-        html + [expr $hist($setNo,maxPresent) - 1]
+        html + $hist($setNo,maxPresent)
         html {"> } [lindex $targets($hist($setNo,host)) 0]
         if {[llength $hist($setNo,database)] > 1} {
             html ": "
@@ -690,3 +721,15 @@ proc z39history {} {
     }
     html "</dl>\n"
 }
+
+proc displayError {msga msgb} {
+    html "<p><center>\n"
+    html {<img src="/gif/noway.gif">}
+    html "<h2>" $msga "</h2>\n"
+    if {$msgb != ""} {
+        html "<h3>" $msgb "</h3>\n"
+    }
+    html "</center><p>\n"
+}
+
+set useIcons 1
\ No newline at end of file