+proc z39scan {setNo scanNo tno scanLines scanPos cache} {
+ global hist
+ global sessionWait
+ global targets
+
+ if {$tno > 0} {
+ set zz z39$tno
+ set host $hist($setNo,$tno,host)
+ set idAuth $hist($setNo,$tno,idAuthentication)
+ set database $hist($setNo,$tno,database)
+ set scanAttr $hist($setNo,$tno,scanAttr)
+ set scanTerm $hist($setNo,$tno,$scanNo,scanTerm)
+ } else {
+ set zz z39
+ set host $hist($setNo,host)
+ set idAuth $hist($setNo,idAuthentication)
+ set database $hist($setNo,database)
+ set scanAttr $hist($setNo,scanAttr)
+ set scanTerm $hist($setNo,$scanNo,scanTerm)
+ }
+ if {[catch [list $zz failback fail-response]]} {
+ ir $zz
+ }
+ if {[catch [list set oldHost [$zz connect]]]} {
+ set oldHost ""
+ }
+ set zs $zz.s$scanNo.$setNo
+ $zz callback ok-response
+ $zz failback fail-response
+ if {$oldHost != $host} {
+ catch [list $zz disconnect]
+
+ set sessionWait 0
+ if {[catch [list $zz connect $host]]} {
+ displayError "Cannot connect to target" $host
+ return 0
+ } elseif {$sessionWait == 0} {
+ if {[catch {egw_wait sessionWait 300}]} {
+ $zz disconnect
+ displayError "Cannot connect to target" $host
+ return 0
+ }
+ if {$sessionWait != 1} {
+ displayError "Cannot connect to target" $host
+ return 0
+ }
+ }
+ $zz idAuthentication $idAuth
+ set sessionWait 0
+ if {[catch {$zz init}]} {
+ displayError "Cannot initialize target" $host
+ $zz disconnect
+ return 0
+ }
+ if {[catch {egw_wait sessionWait 60}]} {
+ displayError "Cannot initialize target" $host
+ $zz disconnect
+ return 0
+ }
+ if {$sessionWait != "1"} {
+ displayError "Cannot initialize target" $host
+ $zz disconnect
+ return 0
+ }
+ if {![$zz initResult]} {
+ set u [$zz userInformationField]
+ $zz disconnect
+ displayError "Cannot initialize target $host" $u
+ return 0
+ }
+ } else {
+ if {$cache && ![catch [list $zs numberOfTermsRequested 5]]} {
+ return 1
+ }
+ }
+ eval $zz databaseNames $database
+
+ ir-scan $zs $zz
+
+ $zs numberOfTermsRequested $scanLines
+ $zs preferredPositionInResponse $scanPos
+
+ $zz callback [list scan-response $zs]
+
+ set sessionWait 0
+ $zs scan "${scanAttr} ${scanTerm}"
+
+ if {[catch {egw_wait sessionWait 60}]} {
+ egw_log debug "timeout/cancel in scan"
+ displayError "Timeout in scan" {}
+ html "</body></html>\n"
+ $zz disconnect
+ return 0
+ }
+ if {$sessionWait == -1} {
+ displayError "Scan fail" "Connection closed"
+ html "</body></html>\n"
+ $zz disconnect
+ }
+ if {$sessionWait != 1} {
+ return 0
+ }
+ return 1
+}
+
+proc display-scan {setNo scanNo tno} {
+ global hist
+ global targets
+ global env
+ global sessionId
+
+ if {$tno > 0} {
+ set zz z39$tno
+ } else {
+ set zz z39
+ }
+ set zs $zz.s$scanNo.$setNo
+ set m [$zs numberOfEntriesReturned]
+
+ if {$m > 0} {
+ set t [lindex [$zs scanLine 0] 1]
+ if {$tno > 0} {
+ set hist($setNo,$tno,[expr $scanNo - 1],scanTerm) $t
+ } else {
+ set hist($setNo,[expr $scanNo - 1],scanTerm) $t
+ }
+ set t [lindex [$zs scanLine [expr $m - 1]] 1]
+ if {$tno > 0} {
+ set hist($setNo,$tno,[expr $scanNo + 1],scanTerm) $t
+ } else {
+ set hist($setNo,[expr $scanNo + 1],scanTerm) $t
+ }
+ }
+ for {set i 0} {$i < $m} {incr i} {
+ regsub -all {\ } [lindex [$zs scanLine $i] 1] + tterm
+ html {<a href="http:} $env(SCRIPT_NAME)
+ html / $sessionId {/query.egw/} $hist($setNo,host) + $setNo +
+ html $hist($setNo,scan) + $tterm {">}
+ html [lindex [$zs scanLine $i] 1]
+ html {</a>: <em>}
+ html [lindex [$zs scanLine $i] 2]
+ html "</em><br>\n"
+ }
+}
+